LameWriter(Stream outputStream, string inputFile) : base(outputStream) { // GetSoundFileType input file int rtn = libSndFile.GetSoundFileType(inputFile, ref soundInfo); // Initialize beConfig = new LameWrapper.BE_CONFIG(soundInfo); try { uint rslt = LameWrapper.beInitStream(beConfig, ref samples, ref bufferSize, ref hLameStream); if (rslt != LameWrapper.BE_ERR_SUCCESSFUL) { throw new ApplicationException( string.Format( "LameWrapper.beInitStream failed with code: {0}", rslt)); } // Samples are 2 bytes wide: sampleSize = (int)samples * 2; // MP3 buffer mp3Buffer = new byte[bufferSize]; } catch { throw; } }
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); }
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); } } }
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(); } }