private void DisposeWave() { if (waveOut != null) { if (waveOut.PlaybackState == PlaybackState.Playing) { waveOut.Stop(); } waveOut.Dispose(); waveOut = null; } if (waveReader != null) { waveReader.Flush(); waveReader.Dispose(); waveReader = null; } if (waveWriter != null) { waveWriter.Flush(); waveWriter.Dispose(); waveWriter = null; } if (axWindowsMediaPlayer1.IsDisposed != true) { axWindowsMediaPlayer1.URL = null; axWindowsMediaPlayer1.currentPlaylist.clear(); //axWindowsMediaPlayer1.Dispose(); //axWindowsMediaPlayer1.Visible = true; AxWMPLib.AxWindowsMediaPlayer myMediaPlayer = new AxWMPLib.AxWindowsMediaPlayer(); this.Controls.Add(myMediaPlayer); } }
public async Task Say([Remainder]string text = null) { await Stop(); var outFormat = new WaveFormat(48000, 16, 2); using (var ss = new SpeechSynthesizer()) using (var discord = _vClient.CreatePCMStream(AudioApplication.Music)) using (var tstream = new MemoryStream()) { ss.Volume = 100; ss.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 0, new System.Globalization.CultureInfo("nl-NL")); ss.SetOutputToWaveStream(tstream); ss.Speak(text); tstream.Flush(); tstream.Seek(0, SeekOrigin.Begin); using (var wave = new WaveFileReader(tstream)) using (var resampler = new MediaFoundationResampler(wave, outFormat)) { resampler.ResamplerQuality = 60; int blockSize = outFormat.AverageBytesPerSecond / 50; byte[] buffer = new byte[blockSize]; int byteCount; while ((byteCount = resampler.Read(buffer, 0, blockSize)) > 0) { if (byteCount < blockSize) { for (int i = byteCount; i < blockSize; i++) { buffer[i] = 0; } } discord.Write(buffer, 0, blockSize); } wave.Flush(); } tstream.Flush(); discord.Flush(); } }
/// <summary> /// Converts the recorded WAV file to MP3 /// </summary> private void ConvertWaveToMp3(string source, string destination) { using (var waveStream = new WaveFileReader(source)) using (var fileWriter = new LameMP3FileWriter(destination, waveStream.WaveFormat, 128)) { waveStream.CopyTo(fileWriter); waveStream.Flush(); } /*Delete the temporary WAV file*/ File.Delete(source); }
private void bPlay_Click(object sender, EventArgs e) { if (File.Exists(PathToFile)) { var player = new WaveOutEvent(); var waveFile = new WaveFileReader(PathToFile); _operation.PlayWaveFile(player, waveFile); Thread.Sleep(2000); waveFile.Flush(); waveFile.Close(); } }