Send Speech.Synthesizer to a specific device

I am using Microsoft Speech Synthesis and want to redirect the output to an audio output device of my choice.

So far, I have the following code:

SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
speechSynthesizer.SpeakAsync("Yea it works!");

      

I am currently using:

speechSynthesizer.SetOutputToDefaultAudioDevice();

      

but I really want to send it to the device of my choice. I'm looking for an example cscore for how to route the output device of my choice. I see that I can use:

speechSynthesizer.SetOutputToWaveStream();

      

This accepts a "stream" but I don't know how to serve it.

Thank.

+3


source to share


1 answer


You can create a MemoryStream and attach it to the CSCore WaveOut . WaveOut requires an IWaveSource argument, so you can use CSCore's MediaFoundationDecoder to convert the wave stream from SpeechSynthesizer . I made a small console application to illustrate:



using System;
using System.IO;
using System.Speech.Synthesis;
using CSCore;
using CSCore.MediaFoundation;
using CSCore.SoundOut;

namespace WaveOutTest
{
    class Program
    {
        static void Main()
        {
            using (var stream = new MemoryStream())
            using (var speechEngine = new SpeechSynthesizer())
            {
                Console.WriteLine("Available devices:");
                foreach (var device in WaveOutDevice.EnumerateDevices())
                {
                    Console.WriteLine("{0}: {1}", device.DeviceId, device.Name);
                }
                Console.WriteLine("\nEnter device for speech output:");
                var deviceId = (int)char.GetNumericValue(Console.ReadKey().KeyChar);

                speechEngine.SetOutputToWaveStream(stream);
                speechEngine.Speak("Testing 1 2 3");

                using (var waveOut = new WaveOut { Device = new WaveOutDevice(deviceId) })
                using (var waveSource = new MediaFoundationDecoder(stream))
                {
                    waveOut.Initialize(waveSource);
                    waveOut.Play();
                    waveOut.WaitForStopped();
                }
            }
        }
    }
}

      

+2


source







All Articles