private IWaveProvider GetOggWaveProvider() { Logger.Info($"Reading Ogg @ {opts.File}"); var oggReader = new VorbisWaveReader(opts.File); int bytes = (int)oggReader.Length; byte[] buffer = new byte[bytes]; Logger.Info($"Read Ogg - Sample Rate {oggReader.WaveFormat.SampleRate}"); if (oggReader.WaveFormat.SampleRate < INPUT_SAMPLE_RATE) { Logger.Error($"Ogg Sample rate must be at least 16000 but is {oggReader.WaveFormat.SampleRate} - Quitting. Use Audacity or another tool to resample as 16000 or Higher"); Environment.Exit(1); } int read = oggReader.Read(buffer, 0, (int)bytes); BufferedWaveProvider bufferedWaveProvider = new BufferedWaveProvider(oggReader.WaveFormat) { BufferLength = read * 2, ReadFully = false, DiscardOnBufferOverflow = true }; bufferedWaveProvider.AddSamples(buffer, 0, read); VolumeSampleProvider volumeSample = new VolumeSampleProvider(bufferedWaveProvider.ToSampleProvider()) { Volume = opts.Volume }; oggReader.Close(); oggReader.Dispose(); Logger.Info($"Convert to Mono 16bit PCM"); //after this we've got 16 bit PCM Mono - just need to sort sample rate return(volumeSample.ToMono().ToWaveProvider16()); }
private IWaveProvider GetMP3WaveProvider() { Logger.Info($"Reading MP3 @ {path}"); var mp3Reader = new Mp3FileReader(path); int bytes = (int)mp3Reader.Length; byte[] buffer = new byte[bytes]; Logger.Info($"Read MP3 @ {mp3Reader.WaveFormat}"); if (mp3Reader.WaveFormat.SampleRate < INPUT_SAMPLE_RATE) { Logger.Error($"Sample rate of MP3 too low - must be 16000 or higher and was - {mp3Reader.WaveFormat}"); Environment.Exit(1); } int read = mp3Reader.Read(buffer, 0, (int)bytes); BufferedWaveProvider bufferedWaveProvider = new BufferedWaveProvider(mp3Reader.WaveFormat) { BufferLength = read * 2, ReadFully = false, DiscardOnBufferOverflow = true }; bufferedWaveProvider.AddSamples(buffer, 0, read); VolumeSampleProvider volumeSample = new VolumeSampleProvider(bufferedWaveProvider.ToSampleProvider()) { Volume = volume }; mp3Reader.Close(); mp3Reader.Dispose(); Logger.Info($"Convert to Mono 16bit PCM"); //after this we've got 16 bit PCM Mono - just need to sort sample rate return(volumeSample.ToMono().ToWaveProvider16()); }