private static Task <byte[]> CompressWav(this byte[] wav) { var task = Task.Run(() => { try { using (var ms = new MemoryStream(wav)) { using (var reader = new WaveFileReader(ms)) { var newFormat = new WaveFormat(8000, 16, 1); using (var conversionStream = new WaveFormatConversionStream(newFormat, reader)) { using (var outms = new MemoryStream()) { using (var writer = new LameMP3FileWriter(outms, conversionStream.WaveFormat, 24, null)) { conversionStream.CopyTo(writer); return(outms.ToArray()); } } } } } } catch (Exception ex) { throw; } }); return(task); }
private static byte[] ConvertWavTo16000Hz16BitMonoWav(byte[] inArray) { { using (var mem = new MemoryStream(inArray)) { using (var reader = new WaveFileReader(mem)) { using (var converter = WaveFormatConversionStream.CreatePcmStream(reader)) { using (var upsampler = new WaveFormatConversionStream(new WaveFormat(16000, 16, 1), converter)) { byte[] data; using (var m = new MemoryStream()) { upsampler.CopyTo(m); data = m.ToArray(); } using (var m = new MemoryStream()) { // to create a propper WAV header (44 bytes), which begins with RIFF var w = new WaveFileWriter(m, upsampler.WaveFormat); // append WAV data body w.Write(data, 0, data.Length); w.Dispose(); return(m.ToArray()); } } } } } } }
public static byte[] EncodeCwav(byte[] wavData) { if (!ThirdPartyTools.CtrWaveConveter.Present) { return(EmptyData); } using (var sourceStream = new MemoryStream(wavData)) // using (var tempBcwav = new TempFile("bcwav")) using (var tempWav = new TempFile("wav")) // { using (var tempWavStream = File.Open(tempWav.FilePath, FileMode.Create)) { if (Settings.Default.EXP_ResampleWav) { var nwf = new WaveFormat(22050, 8, 1); //22Khz Mono PCM8 var wr = new WaveFileReader(sourceStream); var conv = new WaveFormatConversionStream(nwf, wr); conv.CopyTo(tempWavStream); } else { sourceStream.CopyTo(tempWavStream); } tempWavStream.Flush(true); } try { var psi = new ProcessStartInfo { UseShellExecute = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, FileName = ThirdPartyTools.CtrWaveConveter.Path, Arguments = $"-o \"{tempBcwav.FilePath}\" \"{tempWav.FilePath}\"" }; var process = Process.Start(psi); process.WaitForExit(); using (var tempBcwavStream = File.Open(tempBcwav.FilePath, FileMode.Open)) { byte[] converted = new byte[tempBcwavStream.Length]; tempBcwavStream.Read(converted, 0, converted.Length); return(converted); } } catch { return(EmptyData); } } }
byte[] ConvertToMp3(MemoryStream file) { var mem = new MemoryStream(file.ToArray()); var target = new WaveFormat(8000, 16, 1); using (var outPutStream = new MemoryStream()) using (var waveStream = new WaveFileReader(mem)) using (var conversionStream = new WaveFormatConversionStream(target, waveStream)) using (var writer = new LameMP3FileWriter(outPutStream, conversionStream.WaveFormat, 32, null)) { conversionStream.CopyTo(writer); return(outPutStream.ToArray()); } }
private void speakFile(string path, int volume) { if (voiceStream == null) { voiceStream = audioClient.CreatePCMStream(AudioApplication.Voice, 128 * 1024); } try { WaveFileReader wav = new WaveFileReader(path); WaveFormat waveFormat = new WaveFormat(48000, 16, 2); WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(wav); WaveFormatConversionStream output = new WaveFormatConversionStream(waveFormat, pcm); output.CopyTo(voiceStream); voiceStream.Flush(); } catch (Exception ex) { logBox.AppendText("Unable to read file.\n" + ex.Message + "\n"); } }
public static void SpeakFile(string path) { lock (speaklock) { if (voiceStream == null) { voiceStream = audioClient.CreatePCMStream(AudioApplication.Voice, 128 * 1024); } try { WaveFileReader wav = new WaveFileReader(path); WaveFormat waveFormat = new WaveFormat(48000, 16, 2); WaveStream pcm = WaveFormatConversionStream.CreatePcmStream(wav); WaveFormatConversionStream output = new WaveFormatConversionStream(waveFormat, pcm); output.CopyTo(voiceStream); voiceStream.Flush(); } catch (Exception ex) { Log?.Invoke("Unable to read file: " + ex.Message); } } }