public void TestRandomVoice()
{
EventWaitHandle waitHandle = new AutoResetEvent(false);
List<InstalledVoice> availableVoices = new List<InstalledVoice>();
foreach (InstalledVoice voice in new SpeechSynthesizer().GetInstalledVoices())
{
if (voice.Enabled == true && voice.VoiceInfo.Culture.TwoLetterISOLanguageName == "en" && (voice.VoiceInfo.Name.StartsWith("IVONA") || voice.VoiceInfo.Name.StartsWith("CereVoice") || voice.VoiceInfo.Name == "Microsoft Anna"))
{
availableVoices.Add(voice);
}
}
foreach (InstalledVoice availableVoice in availableVoices)
{
Console.WriteLine(availableVoice.VoiceInfo.Name);
}
for (int i = 0; i < 10; i++)
{
using (MemoryStream stream = new MemoryStream())
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
string selectedVoice = availableVoices.OrderBy(x => Guid.NewGuid()).FirstOrDefault().VoiceInfo.Name;
Console.WriteLine("Selected voice is " + selectedVoice);
synth.SelectVoice(selectedVoice);
synth.SetOutputToWaveStream(stream);
//synth.Speak("Anaconda golf foxtrot lima one niner six eight requesting docking.");
synth.Speak("Anaconda.");
stream.Seek(0, SeekOrigin.Begin);
IWaveSource source = new WaveFileReader(stream);
var soundOut = new WasapiOut();
soundOut.Stopped += (s, e) => waitHandle.Set();
soundOut.Initialize(source);
soundOut.Play();
waitHandle.WaitOne();
soundOut.Dispose();
source.Dispose();
}
}
}