This project has moved. For the latest updates, please go here.

Playing Microsoft TTS with WASAPI



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);


                // Configure the synthesizer to output to an audio stream.

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

                _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);
I hope somebody can help me :)


Jojobanana wrote Aug 8, 2014 at 12:33 PM

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);


markheath wrote Aug 8, 2014 at 1:21 PM

great. glad you got it working