Send Speech.Synthesizer to a specific Device
audio, c#, cscore, text-to-speech
Solution
You can create a MemoryStream and attach it to CSCore's WaveOut class. WaveOut requires an IWaveSource argument, so you can use CSCore's MediaFoundationDecoder to convert the wave stream from SpeechSynthesizer. I made a little 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();
}
}
}
}
}
Problem
I'm using Microsoft Speech Synthesis and want to redirect the output to the output audio device of my choosing. So far I have the following code: ``` SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(); speechSynthesizer.SpeakAsync("Yea it works!"); ``` Currently I'm using: ``` speechSynthesizer.SetOutputToDefaultAudioDevice(); ``` but I actually want to send it to the device of my choosing. I am looking for a cscore example for how to direct the output device of my choice. I see that I can use: ``` speechSynthesizer.SetOutputToWaveStream(); ``` This takes a "Stream", but I don't know how to feed it that. Thanks.