Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save text-to-speech as a wav with Microsoft SAPI?

I need to turn a text into speech and then save it as wav file.

like image 509
Tarik Avatar asked Jun 08 '09 05:06

Tarik


People also ask

How do I use Microsoft text-to-Speech?

To use Speak: Select a word or block of text in your document. In the Quick Access Toolbar, select the Speak selected text icon.

Can Windows 10 do text-to-speech?

Narrator reads aloud the text on your PC screen. It also describes events such as notifications and calendar appointments, which lets you use your PC without a display. To start or stop Narrator, press Windows logo key + Ctrl + Enter. To see all Narrator commands, press Caps Lock + F1 after you open Narrator.

How do I use text-to-speech in Windows 11?

1. To start Narrator press the Windows key + Ctrl + Enter on your keyboard. Narrator will read aloud items on the screen, including buttons and menus, as you select them or navigate through them using the keyboard. It will also read aloud any text that you select.


3 Answers

The following C# code uses the System.Speech namespace in the .Net framework. It is necessary to reference the namespace before using it, because it is not automatically referenced by Visual Studio.

        SpeechSynthesizer ss = new SpeechSynthesizer();
        ss.Volume = 100;
        ss.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
        ss.SetOutputToWaveFile(@"C:\MyAudioFile.wav");
        ss.Speak("Hello World");

I hope this is relevant and helpful.

like image 192
Mackenzie Avatar answered Oct 08 '22 03:10

Mackenzie


This is from a few moments' play, so caveat emptor. Worked well for me. I did notice that SpFileStream (which doesn't implement IDisposable, thus the try/finally) prefers absolute paths to relative. C#.

   SpFileStream fs = null;
    try
    {
        SpVoice voice = new SpVoice();
        fs = new SpFileStream();
        fs.Open(@"c:\hello.wav", SpeechStreamFileMode.SSFMCreateForWrite, false);
        voice.AudioOutputStream = fs;
        voice.Speak("Hello world.", SpeechVoiceSpeakFlags.SVSFDefault);
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
        }
    }
like image 37
Michael Petrotta Avatar answered Oct 08 '22 02:10

Michael Petrotta


And as I've found for how to change output format, we code something like this :

SpeechAudioFormatInfo info = new SpeechAudioFormatInfo(6, AudioBitsPerSample.Sixteen, AudioChannel.Mono);

//Same code comes here 

ss.SetOutputToWaveFile(@"C:\MyAudioFile.wav",info);

That's pretty easy and comprehensible.

Cool .net

like image 5
Tarik Avatar answered Oct 08 '22 03:10

Tarik