public async Task <byte[]> ConvertAsync(byte[] content) { _environmentVariablesConfig.CheckAddBinPath(); var target = new WaveFormat(8000, 16, 1); await using var outPutStream = new MemoryStream(); await using var mp3Reader = new Mp3FileReader(new MemoryStream(content)); await using var conversionStream = new WaveFormatConversionStream(target, mp3Reader); await using var writer = new WaveFileWriter(outPutStream, conversionStream.WaveFormat); await conversionStream.CopyToAsync(writer); return(outPutStream.ToArray()); }
/// <summary> /// Gets the job's media PCM data. /// </summary> /// <returns></returns> static async Task <Tuple <byte[], int> > GetMediaAsWav(Stream src) { // convert PCM stream into WAV using (var dst = new MemoryStream()) using (var pcm = new WaveFileReader(src)) using (var cnv = new WaveFormatConversionStream(new WaveFormat(8000, 16, pcm.WaveFormat.Channels), pcm)) using (var wav = new WaveFileWriter(dst, cnv.WaveFormat)) { // run the conversion into the memory stream await cnv.CopyToAsync(wav, 65536 - (65536 % cnv.BlockAlign)); await wav.FlushAsync(); wav.Close(); // execute the recognition return(Tuple.Create(dst.ToArray(), cnv.WaveFormat.Channels)); } }