private void SpeakButton_Click(object sender, System.EventArgs e) { int index; // Try to use the user selected device. if (OutputCombo.SelectedIndex != -1) { try { Voice.AllowAudioOutputFormatChangesOnNextSet = true; Voice.AudioOutput = Voice.GetAudioOutputs("", "").Item(OutputCombo.SelectedIndex); } catch (Exception) { AddEventMessage("ERROR: Error while attempting to set output device."); } } // Update the combobox to reflect the device SAPI is // actually using. index = OutputCombo.FindStringExact(Voice.AudioOutput.GetDescription(0)); if (index != -1) { OutputCombo.SelectedIndex = index; } Type formatType = typeof(SpeechAudioFormatType); // Try to use the user selected format. if (FormatCombo.SelectedIndex != -1) { try { SpeechAudioFormatType newFormat = (SpeechAudioFormatType) Enum.Parse(formatType, (string)FormatCombo.SelectedItem); Voice.AllowAudioOutputFormatChangesOnNextSet = false; Voice.AudioOutputStream.Format.Type = newFormat; Voice.AudioOutputStream = Voice.AudioOutputStream; } catch (Exception) { AddEventMessage("ERROR: Error while attempting to set audio format."); } } // Update the combobox to reflect the format // actually being used. string formatName = Enum.GetName(formatType, Voice.AudioOutputStream.Format.Type); index = FormatCombo.FindStringExact(formatName); if (index != -1) { FormatCombo.SelectedIndex = index; } this.Speak(); }
/// <summary> /// Convert xml to WAV bytes. WAV won't have the header, so you have to add it separatelly. /// </summary> byte[] ConvertSapiXmlToWav(string xml, int sampleRate, int bitsPerSample, int channelCount) { SpeechAudioFormatType t = SpeechAudioFormatType.SAFT48kHz16BitMono; switch (channelCount) { case 1: // Mono switch (sampleRate) { case 11025: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT11kHz8BitMono : SpeechAudioFormatType.SAFT11kHz16BitMono; break; case 22050: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT22kHz8BitMono : SpeechAudioFormatType.SAFT22kHz16BitMono; break; case 44100: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT44kHz8BitMono : SpeechAudioFormatType.SAFT44kHz16BitMono; break; case 48000: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT48kHz8BitMono : SpeechAudioFormatType.SAFT48kHz16BitMono; break; } break; case 2: // Stereo switch (sampleRate) { case 11025: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT11kHz8BitStereo : SpeechAudioFormatType.SAFT11kHz16BitStereo; break; case 22050: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT22kHz8BitStereo : SpeechAudioFormatType.SAFT22kHz16BitStereo; break; case 44100: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT44kHz8BitStereo : SpeechAudioFormatType.SAFT44kHz16BitStereo; break; case 48000: t = bitsPerSample == 8 ? SpeechAudioFormatType.SAFT48kHz8BitStereo : SpeechAudioFormatType.SAFT48kHz16BitStereo; break; } break; } byte[] bytes; var voice = new SpeechLib.SpVoice(); // Write into memory. var stream = new SpeechLib.SpMemoryStream(); stream.Format.Type = t; voice.AudioOutputStream = stream; try { voice.Speak(xml, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak); } catch (Exception ex) { ex.Data.Add("Voice", "voiceName"); LastException = ex; return(null); } var spStream = (SpMemoryStream)voice.AudioOutputStream; spStream.Seek(0, SpeechStreamSeekPositionType.SSSPTRelativeToStart); bytes = (byte[])(object)spStream.GetData(); return(bytes); }
public bool SaveAsWAV(string path, string str, SpeechAudioFormatType SpAudioType) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpAudioFormat SpAudio = new SpAudioFormat(); SpAudio.Type = SpAudioType; SpFileStream.Format = SpAudio; SpFileStream.Open(path, SpFileMode, false); mVoice.AudioOutputStream = SpFileStream; mVoice.Speak(str, SpFlags); mVoice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); return File.Exists(path); }
private void SaveWavButton_Click(object sender, System.EventArgs e) { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "All Files (*.*)|*.*|Wave Files (*.wav)|*.wav"; saveFile.FilterIndex = 2; saveFile.RestoreDirectory = true; if (saveFile.ShowDialog() == DialogResult.OK) { AddEventMessage("INFO: Saving to a wave file..."); // We need a file stream. SpFileStreamClass spFile = new SpFileStreamClass(); // Try and use the user specified format for the // wave file. if (FormatCombo.SelectedIndex != -1) { try { SpeechAudioFormatType newFormat = (SpeechAudioFormatType) Enum.Parse(typeof(SpeechAudioFormatType), (string)FormatCombo.SelectedItem); spFile.Format.Type = newFormat; } catch (Exception) { AddEventMessage("ERROR: Error while attempting to set audio format."); } } spFile.Open(saveFile.FileName, SpeechStreamFileMode.SSFMCreateForWrite, false); // We don't want SAPI to change the format, because // we might have done so already. Voice.AllowAudioOutputFormatChangesOnNextSet = false; Voice.AudioOutputStream = spFile; this.Speak(); Voice.WaitUntilDone(-1); spFile.Close(); AddEventMessage("INFO: Wave file written successfully."); } }
/* SAFT8kHz8BitMono = 4 SAFT8kHz8BitStereo = 5 SAFT8kHz16BitMono = 6 SAFT8kHz16BitStereo = 7 SAFT11kHz8BitMono = 8 SAFT11kHz8BitStereo = 9 SAFT11kHz16BitMono = 10 SAFT11kHz16BitStereo = 11 SAFT12kHz8BitMono = 12 SAFT12kHz8BitStereo = 13 SAFT12kHz16BitMono = 14 SAFT12kHz16BitStereo = 15 SAFT16kHz8BitMono = 16 SAFT16kHz8BitStereo = 17 SAFT16kHz16BitMono = 18 SAFT16kHz16BitStereo = 19 SAFT22kHz8BitMono = 20 SAFT22kHz8BitStereo = 21 SAFT22kHz16BitMono = 22 SAFT22kHz16BitStereo = 23 SAFT24kHz8BitMono = 24 SAFT24kHz8BitStereo = 25 SAFT24kHz16BitMono = 26 SAFT24kHz16BitStereo = 27 SAFT32kHz8BitMono = 28 SAFT32kHz8BitStereo = 29 SAFT32kHz16BitMono = 30 SAFT32kHz16BitStereo = 31 SAFT44kHz8BitMono = 32 SAFT44kHz8BitStereo = 33 SAFT44kHz16BitMono = 34 SAFT44kHz16BitStereo = 35 SAFT48kHz8BitMono = 36 SAFT48kHz8BitStereo = 37 SAFT48kHz16BitMono = 38 SAFT48kHz16BitStereo = 39 */ public void SetWaveFormat(WaveFormat wf) { waveType = Audio.ConvertWaveFormatToSpeechFormat(wf); }
public int put_Type(SpeechAudioFormatType AudioFormat) { return(((delegate * unmanaged <ISpeechAudioFormat *, SpeechAudioFormatType, int>)(lpVtbl[8]))((ISpeechAudioFormat *)Unsafe.AsPointer(ref this), AudioFormat)); }
public void SetWaveFormat(WaveFormat wf) { waveType = Audio.ConvertWaveFormatToSpeechFormat(wf); }