public static async Task wavConvert(string mp4Conv, Channel voice) { // convert source audio to AAC // create media foundation reader to read the source (can be any supported format, mp3, wav, ...) /*using (MediaFoundationReader reader = new MediaFoundationReader(@"d:\source.mp3")) * { * MediaFoundationEncoder.EncodeToAac(reader, @"D:\test.mp4"); * }*/ var channelCount = discord.GetService <AudioService>().Config.Channels; // convert "back" to WAV // create media foundation reader to read the AAC encoded file using (MediaFoundationReader reader = new MediaFoundationReader(mp4Conv)) // resample the file to PCM with same sample rate, channels and bits per sample using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader, new WaveFormat(48000, 16, channelCount))) // create WAVe file using (WaveFileWriter waveWriter = new WaveFileWriter(@"C:\music\tempaud.wav", resampledReader.WaveFormat)) { // copy samples resampledReader.CopyTo(waveWriter); } await SendAudio(@"C:\music\tempaud.wav", voice); }
public string ConvertAACToWAV(string songTitle, string cacheDir) { try { // im going to add this in an atempt to have easier playback though naudio // https://stackoverflow.com/questions/13486747/convert-aac-to-wav // create media foundation reader to read the AAC encoded file using (MediaFoundationReader reader = new MediaFoundationReader(cacheDir + songTitle + ".aac")) // resample the file to PCM with same sample rate, channels and bits per sample using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader, new WaveFormat(reader.WaveFormat.SampleRate, reader.WaveFormat.BitsPerSample, reader.WaveFormat.Channels))) // create WAVe file using (WaveFileWriter waveWriter = new WaveFileWriter(cacheDir + songTitle + ".wav", resampledReader.WaveFormat)) { // copy samples resampledReader.CopyTo(waveWriter); } return(cacheDir + songTitle + ".wav"); } catch (Exception error) { _logs.logMessage("Error", "downloader.ConvertAACToWAV", error.ToString(), "system"); return(null); } }
static void Main(string[] args) { // convert source audio to AAC // create media foundation reader to read the source (can be any supported format, mp3, wav, ...) using (MediaFoundationReader reader = new MediaFoundationReader(@"d:\source.mp3")) { MediaFoundationEncoder.EncodeToAac(reader, @"D:\test.mp4"); } // convert "back" to WAV // create media foundation reader to read the AAC encoded file using (MediaFoundationReader reader = new MediaFoundationReader(@"D:\test.mp4")) // resample the file to PCM with same sample rate, channels and bits per sample using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader, new WaveFormat(reader.WaveFormat.SampleRate, reader.WaveFormat.BitsPerSample, reader.WaveFormat.Channels))) // create WAVe file using (WaveFileWriter waveWriter = new WaveFileWriter(@"d:\test.wav", resampledReader.WaveFormat)) { // copy samples resampledReader.CopyTo(waveWriter); } }
private async Task <Tuple <string, TimeSpan> > convertToWav(string file, CancellationToken token) { string fileWithouExtension = Path.Combine(Path.GetFullPath(file).Replace(Path.GetFileName(file), ""), Path.GetFileNameWithoutExtension(file)); string outFile = file; if (file.ToLower().EndsWith(".mp3")) { outFile = fileWithouExtension + ".wav"; token.ThrowIfCancellationRequested(); using (Mp3FileReader reader = new Mp3FileReader(file)) { WaveFileWriter.CreateWaveFile(outFile, reader); } } else if (file.ToLower().EndsWith(".m4a")) { outFile = fileWithouExtension + ".wav"; token.ThrowIfCancellationRequested(); using (MediaFoundationReader reader = new MediaFoundationReader(file)) { using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader, new WaveFormat(reader.WaveFormat.SampleRate, reader.WaveFormat.BitsPerSample, reader.WaveFormat.Channels))) using (WaveFileWriter waveWriter = new WaveFileWriter(outFile, resampledReader.WaveFormat)) { resampledReader.CopyTo(waveWriter); } } } token.ThrowIfCancellationRequested(); TimeSpan totalTime; using (WaveFileReader maleReader = new WaveFileReader(outFile)) { totalTime = maleReader.TotalTime; } return(await Task.FromResult <Tuple <string, TimeSpan> >(new Tuple <string, TimeSpan>(outFile, totalTime))); }
private static string Convertm4aWav(string fullFilePath) { var wavFileName = String.Format(@"{0}\{1}{2}", Path.GetDirectoryName(fullFilePath), Path.GetFileNameWithoutExtension(fullFilePath), ".wav"); // convert "back" to WAV // create media foundation reader to read the AAC encoded file using (MediaFoundationReader reader = new MediaFoundationReader(fullFilePath)) { // resample the file to PCM with same sample rate, channels and bits per sample using (ResamplerDmoStream resampledReader = new ResamplerDmoStream(reader, new WaveFormat(reader.WaveFormat.SampleRate, reader.WaveFormat.BitsPerSample, reader.WaveFormat.Channels))) { // create WAVe file using (WaveFileWriter waveWriter = new WaveFileWriter(wavFileName, resampledReader.WaveFormat)) { // copy samples resampledReader.CopyTo(waveWriter); waveWriter.Flush();; } } } return(wavFileName); }
public byte[] Decode(string audioType, byte[] input) { byte[] output = null; _calls++; lock (_decoderLock) { string fileType = ""; switch (audioType) { case "audio/mpg": case "audio/mpeg": fileType = "mp3"; break; case "audio/aac": case "audio/aacp": fileType = "aac"; break; } try { string _inFile = PathUtils.GetTempFilePath(string.Format("opm-dec-in-{1}.{0}", fileType, _calls % 10)); string _outFile = PathUtils.GetTempFilePath(string.Format("opm-dec-out.wav")); DeleteFile(_outFile); DeleteFile(_inFile); IO.File.WriteAllBytes(_inFile, input); WaveFormat wf = new WaveFormat(WaveFormatEx.Cdda.nSamplesPerSec, 2); using (MediaFoundationReader mfr = new MediaFoundationReader(_inFile)) using (ResamplerDmoStream res = new ResamplerDmoStream(mfr, wf)) using (WaveFileWriter wav = new WaveFileWriter(_outFile, wf)) { res.CopyTo(wav); wav.Close(); res.Close(); mfr.Close(); } if (IO.File.Exists(_outFile)) { byte[] outWav = IO.File.ReadAllBytes(_outFile); if (outWav.Length > 44) { output = new byte[outWav.Length]; Array.Copy(outWav, 44, output, 0, outWav.Length - 44); } } } catch (Exception ex) { Logger.LogException(ex); } } return(output); }