1
Vote

Playing Microsoft TTS with WASAPI

description

Hallo,

I have a problem playing Microsoft Text to Speech with WASAPI. I basically get an error (Not a WAVE file - no RIFF header) when the WaveFileReader tries to read the audioStream. As you can see in the code below I tried to convert the audioStream coming from Microsoft Text to Speech by using the RawSourceWaveStream and the WaveFormatConversionStream. Unfortunately that doesn't work. I need to play the audio with WASAPI because I want to select a specific device to play the audio with.
using (SpeechSynthesizer synth = new SpeechSynthesizer())
            using (MemoryStream streamAudio = new MemoryStream())
            {
                // Create a SoundPlayer instance to play the output audio file.
                System.Media.SoundPlayer m_SoundPlayer = new System.Media.SoundPlayer();

                SpeechAudioFormatInfo speechFormat = new SpeechAudioFormatInfo(EncodingFormat.Pcm, 32000, 8, 2, 8000, 2, null);

               //synth.SelectVoice(AppSettings.VoiceName);

                // Configure the synthesizer to output to an audio stream.
                synth.SetOutputToAudioStream(streamAudio,speechFormat);

                // Speak a phrase.
                synth.Speak("Hallo Jonas wie gehts es dir?");
                streamAudio.Position = 0;
                synth.SetOutputToNull();

                _wasapiOut = new WasapiOut(device, AudioClientShareMode.Shared, false, 2000);

                RawSourceWaveStream s = new RawSourceWaveStream(streamAudio, new WaveFormat(32000, 8, 2));
                WaveStream ws = WaveFormatConversionStream.CreatePcmStream(s);

                _reader = new WaveFileReader(streamAudio);
                _wasapiOut.Init(_reader);
                _wasapiOut.Play();
            }
I hope somebody can help me :)

comments

Jojobanana wrote Aug 8 at 11:33 AM

I solved it. I had to set the Encoding of the SpeechAudioFormatInfo to ALaw and the RawSourceWaveStream has to instantiate also a Alaw Format. The WaveFileReader is then pointless and wasapi can simply Init it.
ALawFormat = new SpeechAudioFormatInfo(EncodingFormat.ALaw, 44100, 8, 1, 1, 2, null);

...

RawSourceWaveStream s = new RawSourceWaveStream(memoryStream, WaveFormat.CreateALawFormat(44100, 1));

ws = WaveFormatConversionStream.CreatePcmStream(reader);

_wasapiOut.Init(ws);
_wasapiOut.Play();

markheath wrote Aug 8 at 12:21 PM

great. glad you got it working