public override void Grab(CDDrive cd, Track track, string destFile, bool generateTags) { if (MustCancel()) { return; } byte[] buff = base.GetTrackData(cd, track); if (MustCancel()) { return; } ID3FileInfoSlim ifiSlim = new ID3FileInfoSlim(); ifiSlim.Album = track.Album; ifiSlim.Artist = track.Artist; ifiSlim.Genre = track.Genre; ifiSlim.Title = track.Title; ifiSlim.Track = (short)track.Index; short year = 1900; if (short.TryParse(track.Year, out year)) { ifiSlim.Year = year; } this.Options.WaveFormat = WaveFormatEx.Cdda; EncodeBuffer(buff, destFile, generateTags, ifiSlim); }
public void DoTranscoding(EncoderSettings encoderSettings, string inputFile) { _grabber = CdRipper.CreateGrabber(OutputFormat); if (_grabber == null) { throw new NotSupportedException(string.Format("TXT_UNSUPPORTED_OUTPUT_FORMAT: {0}", InputFormat)); } switch (InputFormat) { case AudioMediaFormatType.WAV: switch (OutputFormat) { case AudioMediaFormatType.MP3: { // Transcode WAV => MP3 i.o.w encode the wav WaveFormatEx wfex = WaveFormatEx.Cdda; byte[] buff = WaveFile.ReadWaveData(inputFile, ref wfex); GrabberToMP3 grabber = (_grabber as GrabberToMP3); grabber.Options = (encoderSettings as Mp3EncoderSettings).Options; // Resample is not supported at this time. // Specify the same settings as the input WAV file, otherwise we'll be failing. grabber.Options.WaveFormat = wfex; grabber.EncodeBuffer(buff, Path.ChangeExtension(inputFile, "MP3"), false, null); return; } } break; case AudioMediaFormatType.MP3: switch (OutputFormat) { case AudioMediaFormatType.WAV: // Transcode MP3 => WAV i.o.w decode the MP3 string outputFile = Path.ChangeExtension(inputFile, "WAV"); if (DecodeMP3ToWAV(inputFile, outputFile) == false) { throw new Exception("TXT_FAILED_CONVERSION_MP3_WAV"); } return; case AudioMediaFormatType.MP3: { // Transcode MP3 => MP3 i.o.w adjust MP3 encoding string tempWavFile = Path.GetTempFileName(); if (DecodeMP3ToWAV(inputFile, tempWavFile) == false) { throw new Exception("TXT_FAILED_CONVERSION_MP3_TEMP_WAV"); } WaveFormatEx wfex = WaveFormatEx.Cdda; byte[] buff = WaveFile.ReadWaveData(tempWavFile, ref wfex); GrabberToMP3 grabber = (_grabber as GrabberToMP3); grabber.Options = (encoderSettings as Mp3EncoderSettings).Options; ID3FileInfoSlim ifiSlim = new ID3FileInfoSlim(MediaFileInfo.FromPath(inputFile, false)); grabber.EncodeBuffer(buff, Path.ChangeExtension(inputFile, "REENC.MP3"), (encoderSettings as Mp3EncoderSettings).CopyInputFileMetadata, ifiSlim); if (File.Exists(tempWavFile)) { File.Delete(tempWavFile); } return; } } break; } throw new NotSupportedException(string.Format("TXT_UNSUPPORTED_TRANSCODING: {0}", this)); }
public void EncodeBuffer(byte[] buff, string destFile, bool generateTags, ID3FileInfoSlim ifiSlim) { string summary = ""; LameEncConfig cfg = this.Options.GetConfig(ref summary); if (ifiSlim != null && ifiSlim.Frequency.HasValue) { cfg.format.dwSampleRate = (uint)ifiSlim.Frequency; } uint LameResult = Lame_encDll.Init(cfg, ref m_InputSamples, ref m_OutBufferSize, ref m_hLameStream); if (LameResult != Lame_encDll.ERR_SUCCESSFUL) { throw new ApplicationException(string.Format("Lame_encDll.Init failed with the error code {0}", LameResult)); } using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None)) using (BinaryWriter bw = new BinaryWriter(fs)) { uint EncodedSize = 0; m_OutBuffer = new byte[m_OutBufferSize]; if (MustCancel()) { return; } try { int buffPos = 0; while (buffPos < buff.Length) { if (MustCancel()) { return; } m_OutBuffer = new byte[m_OutBufferSize]; uint bytesToCopy = Math.Min(2 * m_InputSamples, (uint)buff.Length - (uint)buffPos); if ((LameResult = Lame_encDll.Encode(m_hLameStream, buff, buffPos, (uint)bytesToCopy, m_OutBuffer, ref EncodedSize)) == Lame_encDll.ERR_SUCCESSFUL) { if (EncodedSize > 0) { bw.Write(m_OutBuffer, 0, (int)EncodedSize); } } else { throw new ApplicationException(string.Format("Lame_encDll.Encode failed with the error code {0}", LameResult)); } buffPos += (int)bytesToCopy; } } finally { EncodedSize = 0; if (Lame_encDll.Release(m_hLameStream, m_OutBuffer, ref EncodedSize) == Lame_encDll.ERR_SUCCESSFUL) { if (EncodedSize > 0) { bw.Write(m_OutBuffer, 0, (int)EncodedSize); } } Lame_encDll.Close(m_hLameStream); } } if (!MustCancel() && cfg.format.bWriteVBRHeader != 0) { uint err = Lame_encDll.WriteVBRHeader(destFile); } if (!MustCancel() && generateTags && ifiSlim != null) { ID3FileInfo ifi = new ID3FileInfo(destFile, false); ifi.Album = StringUtils.Capitalize(ifiSlim.Album, WordCasing.CapitalizeWords); ifi.Artist = StringUtils.Capitalize(ifiSlim.Artist, WordCasing.CapitalizeWords); ifi.Genre = StringUtils.Capitalize(ifiSlim.Genre, WordCasing.CapitalizeWords); ifi.Title = StringUtils.Capitalize(ifiSlim.Title, WordCasing.CapitalizeWords); ifi.Track = ifiSlim.Track.GetValueOrDefault(); ifi.Year = ifiSlim.Year.GetValueOrDefault(); ifi.Save(); } }