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

2020-02-23 05:08发布

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

3条回答
▲ chillily
2楼-- · 2020-02-23 05:36

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.

查看更多
一夜七次
3楼-- · 2020-02-23 05:48

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();
        }
    }
查看更多
劳资没心,怎么记你
4楼-- · 2020-02-23 05:52

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

查看更多
登录 后发表回答