void SpeechDone(int StreamNumber, object StreamPosition) { byte[] data = (byte[])spMemoryStream.GetData(); waveStream = new MemoryStream(data, 0, data.Length, false); this.DialogResult = DialogResult.OK; this.Close(); }
public static byte[] GetSound(string text) { const SpeechVoiceSpeakFlags speechFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; var synth = new SpVoice(); var wave = new SpMemoryStream(); var voices = synth.GetVoices(); try { // synth setup synth.Volume = 100; synth.Rate = -3; foreach (SpObjectToken voice in voices) { if (voice.GetAttribute("Name") == "Microsoft Matej") { synth.Voice = voice; break; } } wave.Format.Type = SpeechAudioFormatType.SAFT22kHz16BitMono; synth.AudioOutputStream = wave; synth.Speak(text, speechFlags); synth.WaitUntilDone(Timeout.Infinite); var waveFormat = new WaveFormat(22050, 16, 1); using (var ms = new MemoryStream((byte[])wave.GetData())) using (var reader = new RawSourceWaveStream(ms, waveFormat)) using (var outStream = new MemoryStream()) using (var writer = new WaveFileWriter(outStream, waveFormat)) { reader.CopyTo(writer); //return o.Mp3 ? ConvertToMp3(outStream) : outStream.GetBuffer(); //var bytes = ConvertToMp3(outStream); File.WriteAllBytes("speak.wav", outStream.GetBuffer()); return(null); } } finally { Marshal.ReleaseComObject(voices); Marshal.ReleaseComObject(wave); Marshal.ReleaseComObject(synth); } }
private void OutputWaveStream(SpMemoryStream waveStream) { using var sourceStream = new MemoryStream((byte[])waveStream.GetData()); using var output = new WasapiOut(audioConfiguration.SelectedTTSDevice, AudioClientShareMode.Shared, true, 100); using var provider = new RawSourceWaveStream(sourceStream, new WaveFormat(44100, BitResolution, 2)); sourceStream.Seek(0, SeekOrigin.Begin); output.Volume = 1f; output.Init(provider); output.Play(); while (output.PlaybackState == PlaybackState.Playing) { Thread.Sleep(10); } }
private bool TryGetBytes(string textLine, out byte[] data) { try { var spFileStream = new SpMemoryStream(); Voice.AudioOutputStream = spFileStream; Voice.Speak(textLine); Voice.WaitUntilDone(Timeout.Infinite); data = spFileStream.GetData() as byte[]; return true; } catch (Exception ex) { Log.Error(string.Format("{0}: \r\n {1}", ex, textLine)); } data = null; return false; }