public BE_CONFIG(LibsndfileWrapper.SF_INFO soundInfo)
     : this(soundInfo, 128)
 {
 }
 public struct_un(LibsndfileWrapper.SF_INFO soundInfo, uint mp3bitrate)
 {
     lhv1 = new LHV1(soundInfo, mp3bitrate);
 }
 // Constructors
 public BE_CONFIG(LibsndfileWrapper.SF_INFO soundInfo, uint mp3bitrate)
 {
     this.dwConfig = BE_CONFIG_LAME;
     this.union = new struct_un(soundInfo, mp3bitrate);
 }
            // Constructor:
            public LHV1(LibsndfileWrapper.SF_INFO soundInfo, uint mp3bitrate)
            {
                // Error if not WAV, 16bit

                if (soundInfo.format !=
                        ((int)LibsndfileWrapper.soundFormat.SF_FORMAT_WAV |
                         (int)LibsndfileWrapper.soundFormat.SF_FORMAT_PCM_16) )
                {
                    throw new ArgumentOutOfRangeException(
                            "format",
                            "Only WAV 16 bit uncompressed supported. " +
                            "You gave format " + soundInfo.format);
                }

                  dwStructVersion	= 1;
                  dwStructSize		= (uint)Marshal.SizeOf(typeof(BE_CONFIG));

                  // Handle sample rate

                  			  switch (soundInfo.samplerate)
                  {
                case 16000 :
                case 22050 :
                case 24000 :
                  dwMpegVersion		= MPEG2;
                  break;
                case 32000 :
                case 44100 :
                case 48000 :
                  dwMpegVersion		= MPEG1;
                  break;
                default :
                  throw new ArgumentOutOfRangeException(
                          "format", "Sample rate " +
                          soundInfo.samplerate + " not supported");
                  }

                  dwSampleRate = (uint)soundInfo.samplerate;
                  dwReSampleRate = 0;

                  	  // Handle channels

                  switch (soundInfo.channels)
                  {

                case 1 :
                  	mpgmode = mpeg_mode.MONO;
                    break;
                case 2 :
                    mpgmode = mpeg_mode.STEREO;
                    break;
                default:
                    throw new ArgumentOutOfRangeException(
                            "format",
                            "Invalid number of channels:" + soundInfo.channels);

                  }

                  // Handle bit rate

                  switch (mp3bitrate)
                  {

                case 32 :
                case 40 :
                case 48 :
                case 56 :
                case 64 :
                case 80 :
                case 96 :
                case 112 :
                case 128 :

                // Allowed bit rates in MPEG1 and MPEG2:

                case 160 :
                  break;

                case 192 :
                case 224 :
                case 256 :

                // Allowed only in MPEG1:

                case 320 :
                  if (dwMpegVersion	!= MPEG1)
                  {
                    throw new ArgumentOutOfRangeException(
                            "mp3bitrate", "Incompatible bit rate:"+mp3bitrate);
                  }
                  break;

                case 8 :
                case 16 :
                case 24 :

                // Allowed only in MPEG2:

                case 144 :
                  if (dwMpegVersion	!= MPEG2)
                  {
                    throw new ArgumentOutOfRangeException(
                            "mp3bitrate", "Incompatible bit rate:"+mp3bitrate);
                  }
                  break;

                default :
                  throw new ArgumentOutOfRangeException(
                          "mp3bitrate", "Can't support bit rate");

                  }

                  // MINIMUM BIT RATE:

                  dwBitrate	= mp3bitrate;

                  // QUALITY PRESET SETTING:

                  nPreset = LAME_QUALITY_PRESET.LQP_NORMAL_QUALITY;

                  // USE DEFAULT PSYCHOACOUSTIC MODEL:

                  dwPsyModel = 0;

                  // NO EMPHASIS TURNED ON:

                  dwEmphasis = 0;

                  // SET ORIGINAL FLAG:

                  bOriginal = 1;

                  bWriteVBRHeader	= 0;

                  // No bit reservoir:

                  bNoRes = 0;

                  bCopyright = 0;
                  bCRC = 0;
                  bEnableVBR = 0;
                  bPrivate = 0;
                  bStrictIso = 0;
                  dwMaxBitrate = 0;
                  dwVbrAbr_bps = 0;
                  nQuality = 0;
                  nVbrMethod = VBRMETHOD.VBR_METHOD_NONE;
                  nVBRQuality = 0;
            }
Beispiel #5
0
        // Initialize default settings
        // for MP3 (call to initialize
        // structure for customized MP3
        // settings). After this call,
        // settings are changed in the
        // calling code. Takes the
        // input WAV file.
        public void SetMP3(string inputFile)
        {
            // Get information about inputFile,
            // use to give beConfig initial
            // values:

            LibsndfileWrapper.SF_INFO soundInfo =
                new LibsndfileWrapper.SF_INFO();

            LibsndfileWrapper libSndFile = new LibsndfileWrapper();
            libSndFile.GetSoundFileType(inputFile, ref soundInfo);
            beConfig = new LameWrapper.BE_CONFIG(soundInfo);
        }
Beispiel #6
0
        // Conversion routine the first
        // (lame, libsndfile)
        //
        // Convert() default routine
        // Handles WAV->MP3 encoding,
        // !MP3->!MP3 audio encoding.
        public void Convert(string inputFile, int convertFrom,
            string outputFile, int convertTo, Reporter updateStatus)
        {
            // Verify existence of inputFilenm, throw
            // exception if not exist

            if (!System.IO.File.Exists(inputFile))
            {
                throw new FileNotFoundException(
                        "Cannot find file " + inputFile);
            }

            // Input file information
            // (Set input file size)

            FileInfo fi = new FileInfo(inputFile);
            int soundFileSize = (int)fi.Length;

            // Select conversion routines based
            // on input/output file types

            // If outfile = MP3 then use LameWriter
            // to encode, using the settings in the
            // structure mp3set (essentially the LHV1
            // struct from LameWrapper):

            if ((soundFormat)convertTo == soundFormat.MP3)
            {

                // File to convert from must be WAV

                if ( !( (convertFrom >= (int)soundFormat.WAV) &&
                            (convertFrom < (int)soundFormat.AIFF) ) )
                {
                    throw new Exception(
                            "Cannot encode to MP3 directly from this format ("
                            + convertFrom + "). Convert to WAV first");
                }

                LameWriter mp3Writer = null;

                // Instantiate LameWriter object
                // with output filename

                if (beConfig == null)
                {

                    // Use default MP3 output settings
                    mp3Writer = new LameWriter(
                        new FileStream(outputFile, FileMode.Create),
                        inputFile);

                }
                else
                {

                    // Use custom settings

                    mp3Writer = new LameWriter(
                        new FileStream(outputFile, FileMode.Create),
                        inputFile,
                        beConfig);

                }

                // open input file for binary reading

                Stream s = new FileStream(inputFile,
                        FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(s);

                // setup byte buffer -- use mp3Writer.sampleSize
                // to ensure correct buffer size

                byte[] wavBuffer = new byte[mp3Writer.sampleSize];

                // skip WAV header

                s.Seek(LameWrapper.SKIP_WAV_HEADER, SeekOrigin.Begin);

                // write mp3 file

                int index = 0;
                int processedBytes = 0;

                while
                ((index = br.Read(wavBuffer, 0, mp3Writer.sampleSize)) > 0)
                {

                    processedBytes += mp3Writer.sampleSize;

                    // Send to callback:

                    updateStatus(soundFileSize, processedBytes, this);

                    // Check for kill

                    if (this.killSwitch) break;

                    mp3Writer.Write(wavBuffer, 0, index);

                }

                // Finish up

                mp3Writer.Close();
                mp3Writer.WriteTags(outputFile);

            }

            // Assume libsndfile conversion:

            else
            {

                // Check and make sure we are not
                // trying to decode MP3->WAV

                // Instantiate object

                LibsndfileWrapper libSndFile = new LibsndfileWrapper();
                LibsndfileWrapper.SF_INFO soundInfo =
                    new LibsndfileWrapper.SF_INFO();

                // Calculate total frames to convert

                int i = libSndFile.GetSoundFileType(inputFile, ref soundInfo);

                // Each frame is 16bit:

                long totalFrames = soundInfo.frames*2;

                // Initialize

                libSndFile.Initialize (inputFile,
                        outputFile, (LibsndfileWrapper.soundFormat)convertTo);

                // The main decoding loop

                long readCount;
                long readIndex = 0;

                while ( (readCount = libSndFile.Read()) > 0)
                {

                    readIndex += readCount;

                    // Send update to delegate.
                    // Note that this is in
                    // libsndfile specific frames
                    // rather than in actual bytes.
                    //
                    // 	readIndex / totalFrames =
                    // 	percentage complete

                    updateStatus((int)totalFrames, (int)readIndex, this);

                    // Check for kill

                    if (this.killSwitch) break;

                    // Write to file

                    libSndFile.Write();

                }

                // Close up shop

                libSndFile.Close();

            }
        }
Beispiel #7
0
        // Given a file, return
        // the sound file format
        public soundFormat CheckSoundFormat(string inputFile)
        {
            // Verify existence of inputFilenm, throw
            // exception if not exist

            if (!System.IO.File.Exists(inputFile))
            {
                throw new FileNotFoundException(
                        "Cannot find file " + inputFile);
            }

            // Check for non-MP3 sound file

            LibsndfileWrapper libSndFile = new LibsndfileWrapper();
            LibsndfileWrapper.SF_INFO soundInfo =
                new LibsndfileWrapper.SF_INFO();

            int i = libSndFile.GetSoundFileType(inputFile, ref soundInfo);

            // First check to see if it's an audio
            // format !MP3. Then check for MP3.
            // If no valid format found, throw
            // exception.

            if (soundInfo.format != 0)
            {
                return (soundFormat)soundInfo.format;
            }
            else
            {

                MP3Check verifyMP3 = new MP3Check(inputFile, 2);

                if (!verifyMP3.Check())
                {
                    throw new Exception("Cannot determine sound file type: " +
                        inputFile);

                }
                else
                {
                    return soundFormat.MP3;
                }

            }
        }