Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Speech / Src / Synthesis / SpeechSynthesizer.cs / 1 / SpeechSynthesizer.cs
//------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Speech.AudioFormat;
using System.Speech.Internal;
using System.Speech.Internal.ObjectTokens;
using System.Speech.Internal.Synthesis;
using System.Speech.Synthesis.TtsEngine;
using System.Threading;
using RegistryDataKey = System.Speech.Internal.ObjectTokens.RegistryDataKey;
using RegistryEntry = System.Collections.Generic.KeyValuePair;
namespace System.Speech.Synthesis
{
///
/// TODOC
///
public sealed class SpeechSynthesizer : IDisposable
#if SPEECHSERVER || PROMPT_ENGINE
, ISesSynthesizer
#endif
{
//*******************************************************************
//
// Constructors
//
//*******************************************************************
#region Constructors
///
/// TODOC
///
public SpeechSynthesizer ()
{
}
#if SPEECHSERVER
///
/// TODOC
///
internal SpeechSynthesizer (bool outputAsText) : this ()
{
VoiceSynthesizer._outputAsText = outputAsText;
}
#endif
///
///
///
~SpeechSynthesizer ()
{
Dispose (false);
}
///
/// TODOC
///
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
#endregion
//********************************************************************
//
// Public Methods
//
//*******************************************************************
#region public Methods
///
/// TODOC
///
///
public void SelectVoice (string name)
{
Helpers.ThrowIfEmptyOrNull (name, "name");
TTSVoice ttsVoice = VoiceSynthesizer.GetEngine (name, CultureInfo.CurrentUICulture, VoiceGender.NotSet, VoiceAge.NotSet, 1, true);
if (ttsVoice == null || name != ttsVoice.VoiceInfo.Name)
{
// No match - throw
throw new ArgumentException (SR.Get (SRID.SynthesizerSetVoiceNoMatch));
}
VoiceSynthesizer.Voice = ttsVoice;
}
///
/// TODOC
///
///
public void SelectVoiceByHints (VoiceGender gender)
{
SelectVoiceByHints (gender, VoiceAge.NotSet, 1, CultureInfo.CurrentUICulture);
}
///
/// TODOC
///
///
///
public void SelectVoiceByHints (VoiceGender gender, VoiceAge age)
{
SelectVoiceByHints (gender, age, 1, CultureInfo.CurrentUICulture);
}
///
/// TODOC
///
///
///
///
public void SelectVoiceByHints (VoiceGender gender, VoiceAge age, int voiceAlternate)
{
SelectVoiceByHints (gender, age, voiceAlternate, CultureInfo.CurrentUICulture);
}
///
/// TODOC
///
///
///
///
///
public void SelectVoiceByHints (VoiceGender gender, VoiceAge age, int voiceAlternate, CultureInfo culture)
{
Helpers.ThrowIfNull (culture, "culture");
if (voiceAlternate < 0)
{
throw new ArgumentOutOfRangeException ("voiceAlternate", SR.Get (SRID.PromptBuilderInvalidVariant));
}
if (!VoiceInfo.ValidateGender (gender))
{
throw new ArgumentException (SR.Get (SRID.EnumInvalid, "VoiceGender"), "gender");
}
if (!VoiceInfo.ValidateAge (age))
{
throw new ArgumentException (SR.Get (SRID.EnumInvalid, "VoiceAge"), "age");
}
TTSVoice ttsVoice = VoiceSynthesizer.GetEngine (null, culture, gender, age, voiceAlternate, true);
if (ttsVoice == null)
{
// No match - throw
throw new InvalidOperationException (SR.Get (SRID.SynthesizerSetVoiceNoMatch));
}
VoiceSynthesizer.Voice = ttsVoice;
}
///
/// TODOC
///
///
///
public Prompt SpeakAsync (string textToSpeak)
{
Helpers.ThrowIfNull (textToSpeak, "textToSpeak");
Prompt prompt = new Prompt (textToSpeak, SynthesisTextFormat.Text);
SpeakAsync (prompt);
return prompt;
}
///
/// TODOC
///
///
///
public void SpeakAsync (Prompt prompt)
{
Helpers.ThrowIfNull (prompt, "prompt");
prompt.Synthesizer = this;
VoiceSynthesizer.SpeakAsync (prompt);
}
///
/// TODOC
///
///
///
public Prompt SpeakSsmlAsync (string textToSpeak)
{
Helpers.ThrowIfNull (textToSpeak, "textToSpeak");
Prompt prompt = new Prompt (textToSpeak, SynthesisTextFormat.Ssml);
SpeakAsync (prompt);
return prompt;
}
///
/// TODOC
///
///
///
public Prompt SpeakAsync (PromptBuilder promptBuilder)
{
Helpers.ThrowIfNull (promptBuilder, "promptBuilder");
Prompt prompt = new Prompt (promptBuilder);
SpeakAsync (prompt);
return prompt;
}
///
/// TODOC
///
///
///
public void Speak (string textToSpeak)
{
Speak (new Prompt (textToSpeak, SynthesisTextFormat.Text));
}
///
/// TODOC
///
///
///
public void Speak (Prompt prompt)
{
Helpers.ThrowIfNull (prompt, "prompt");
// Avoid a dead lock if the synthesizer is Paused
if (State == SynthesizerState.Paused)
{
throw new InvalidOperationException (SR.Get (SRID.SynthesizerSyncSpeakWhilePaused));
}
prompt.Synthesizer = this;
prompt._syncSpeak = true;
VoiceSynthesizer.Speak (prompt);
}
///
/// TODOC
///
///
///
public void Speak (PromptBuilder promptBuilder)
{
Speak (new Prompt (promptBuilder));
}
///
/// TODOC
///
///
///
public void SpeakSsml (string textToSpeak)
{
Speak (new Prompt (textToSpeak, SynthesisTextFormat.Ssml));
}
///
/// Pause the playback of all speech in this synthesizer.
///
public void Pause ()
{
// Increment the Paused count
if (!paused)
{
VoiceSynthesizer.Pause ();
paused = true;
}
}
///
/// Resume the playback of all speech in this synthesizer.
///
public void Resume ()
{
if (paused)
{
VoiceSynthesizer.Resume ();
paused = false;
}
}
///
/// Cancel playback of all Prompts currently in the queue.
///
///
public void SpeakAsyncCancel (Prompt prompt)
{
Helpers.ThrowIfNull (prompt, "prompt");
VoiceSynthesizer.Abort (prompt);
}
///
/// Cancel playback of all Prompts currently in the queue.
///
public void SpeakAsyncCancelAll ()
{
VoiceSynthesizer.Abort ();
}
///
/// TODOC
///
///
// The stream is disposed when the speech synthesizer is disposed
public void SetOutputToWaveFile (string path)
{
Helpers.ThrowIfEmptyOrNull (path, "path");
SetOutputToNull ();
SetOutputStream (new FileStream (path, FileMode.Create, FileAccess.Write), null, true, true);
}
#if !SPEECHSERVER
///
/// TODOC
///
///
///
// The stream is disposed when the speech synthesizer is disposed
public void SetOutputToWaveFile (string path, SpeechAudioFormatInfo formatInfo)
{
Helpers.ThrowIfEmptyOrNull (path, "path");
Helpers.ThrowIfNull (formatInfo, "formatInfo");
SetOutputToNull ();
SetOutputStream (new FileStream (path, FileMode.Create, FileAccess.Write), formatInfo, true, true);
}
#endif
///
/// TODOC
///
///
public void SetOutputToWaveStream (Stream audioDestination)
{
Helpers.ThrowIfNull (audioDestination, "audioDestination");
SetOutputStream (audioDestination, null, true, false);
}
///
/// TODOC
///
///
///
#if !SPEECHSERVER
public void SetOutputToAudioStream (Stream audioDestination, SpeechAudioFormatInfo formatInfo)
#else
internal void SetOutputToAudioStream (Stream audioDestination, SpeechAudioFormatInfo formatInfo)
#endif
{
Helpers.ThrowIfNull (audioDestination, "audioDestination");
Helpers.ThrowIfNull (formatInfo, "formatInfo");
SetOutputStream (audioDestination, formatInfo, false, false);
}
///
/// TODOC
///
public void SetOutputToDefaultAudioDevice ()
{
SetOutputStream (null, null, true, false);
}
///
/// TODOC
///
// The stream is disposed when the speech synthesizer is disposed
public void SetOutputToNull ()
{
// Close the existing stream
if (_outputStream != Stream.Null)
{
VoiceSynthesizer.SetOutput (Stream.Null, null, true);
}
if (_outputStream != null)
{
if (_closeStreamOnExit)
{
_outputStream.Close ();
}
}
_outputStream = Stream.Null;
}
///
/// TODOC
///
///
// Dynamic content, use a method instead of a property to denote that fact
public Prompt GetCurrentlySpokenPrompt ()
{
return VoiceSynthesizer.Prompt;
}
///
/// TODOC
///
///
public ReadOnlyCollection GetInstalledVoices ()
{
return VoiceSynthesizer.GetInstalledVoices (null);
}
///
/// TODOC
///
///
///
public ReadOnlyCollection GetInstalledVoices (CultureInfo culture)
{
Helpers.ThrowIfNull (culture, "culture");
if (culture.Equals (CultureInfo.InvariantCulture))
{
throw new ArgumentException (SR.Get (SRID.InvariantCultureInfo), "culture");
}
return VoiceSynthesizer.GetInstalledVoices (culture);
}
///
/// TODOC
///
///
///
public void AddLexicon (Uri uri, string mediaType)
{
Helpers.ThrowIfNull (uri, "uri");
VoiceSynthesizer.AddLexicon (uri, mediaType);
}
///
/// TODOC
///
///
public void RemoveLexicon (Uri uri)
{
Helpers.ThrowIfNull (uri, "uri");
VoiceSynthesizer.RemoveLexicon (uri);
}
#if SPEECHSERVER || PROMPT_ENGINE
///
/// TODOC
///
///
///
void ISesSynthesizer.LoadDatabase (string localName, string alias)
{
VoiceSynthesizer.LoadDatabase (localName, alias);
}
///
/// TODOC
///
///
void ISesSynthesizer.UnloadDatabase (string alias)
{
VoiceSynthesizer.UnloadDatabase (alias);
}
///
/// TODOC
///
///
void ISesSynthesizer.SetResourceLoader (ISpeechResourceLoader resourceLoader)
{
VoiceSynthesizer.SetResourceLoader (resourceLoader);
}
#endif
//********************************************************************
//
// Public Events
//
//********************************************************************
#region public Events
///
/// TODOC
///
public event EventHandler SpeakStarted
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakStarted += value;
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakStarted -= value;
}
}
///
/// TODOC
///
public event EventHandler SpeakCompleted
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakCompleted += value;
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakCompleted -= value;
}
}
///
/// TODOC
///
public event EventHandler SpeakProgress
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.WordBoundary, ref VoiceSynthesizer._speakProgress, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.WordBoundary, ref VoiceSynthesizer._speakProgress, value);
}
}
///
/// TODOC
///
public event EventHandler BookmarkReached
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Bookmark, ref VoiceSynthesizer._bookmarkReached, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Bookmark, ref VoiceSynthesizer._bookmarkReached, value);
}
}
///
/// TODOC
///
public event EventHandler VoiceChange
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.VoiceChange, ref VoiceSynthesizer._voiceChange, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.VoiceChange, ref VoiceSynthesizer._voiceChange, value);
}
}
#if !SPEECHSERVER
#region WinFx
///
/// TODOC
///
public event EventHandler PhonemeReached
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Phoneme, ref VoiceSynthesizer._phonemeReached, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Phoneme, ref VoiceSynthesizer._phonemeReached, value);
}
}
///
/// TODOC
///
public event EventHandler VisemeReached
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Viseme, ref VoiceSynthesizer._visemeReached, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Viseme, ref VoiceSynthesizer._visemeReached, value);
}
}
#endregion
#else
///
/// TODOC
///
public event EventHandler ProprietaryEngineEvent
{
[MethodImplAttribute(MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Private, ref VoiceSynthesizer._proprietaryEngineEvent, value);
}
[MethodImplAttribute(MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Private, ref VoiceSynthesizer._proprietaryEngineEvent, value);
}
}
#endif
///
/// TODOC
///
///
public event EventHandler StateChanged
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._stateChanged += value;
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._stateChanged -= value;
}
}
#endregion
#endregion Events
//*******************************************************************
//
// Public Properties
//
//********************************************************************
#region public Properties
///
/// TODOC
///
///
public SynthesizerState State
{
get
{
return VoiceSynthesizer.State;
}
}
///
/// TODOC
///
public int Rate
{
set
{
if (value < -10 || value > 10)
{
throw new ArgumentOutOfRangeException ("value", SR.Get (SRID.RateOutOfRange));
}
VoiceSynthesizer.Rate = value;
}
get
{
return VoiceSynthesizer.Rate;
}
}
///
/// TODOC
///
public int Volume
{
set
{
if (value < 0 || value > 100)
{
throw new ArgumentOutOfRangeException ("value", SR.Get (SRID.ResourceUsageOutOfRange));
}
VoiceSynthesizer.Volume = value;
}
get
{
return VoiceSynthesizer.Volume;
}
}
///
/// TODOC
///
///
public VoiceInfo Voice
{
get
{
// Get the sapi voice
return VoiceSynthesizer.CurrentVoice (true).VoiceInfo;
}
}
//*******************************************************************
//
// Internal Properties
//
//*******************************************************************
#region Internal Properties
#if SPEECHSERVER || PROMPT_ENGINE
static internal string PromptVoices
{
set
{
_promptVoices = value;
}
get
{
return _promptVoices;
}
}
#endif
#endregion
#endregion
//*******************************************************************
//
// Private Methods
//
//********************************************************************
#region Private Methods
///
/// TODOC
///
///
///
///
///
private void SetOutputStream (Stream stream, SpeechAudioFormatInfo formatInfo, bool headerInfo, bool closeStreamOnExit)
{
SetOutputToNull ();
_outputStream = stream;
_closeStreamOnExit = closeStreamOnExit;
// Need to serialize into a proper wav file before closing the stream
VoiceSynthesizer.SetOutput (stream, formatInfo, headerInfo);
}
///
/// TODOC
///
///
private void Dispose (bool disposing)
{
if (!_isDisposed && disposing)
{
if (_voiceSynthesis != null)
{
// flag it first so asynchronous operation has more time to finish
_isDisposed = true;
SpeakAsyncCancelAll ();
// Flush the Output stream
if (_outputStream != null)
{
if (_closeStreamOnExit)
{
_outputStream.Close ();
}
else
{
_outputStream.Flush ();
}
_outputStream = null;
}
}
}
if (_voiceSynthesis != null)
{
// Terminate the background synthesis object the thread.
_voiceSynthesis.Dispose ();
_voiceSynthesis = null;
}
_isDisposed = true;
}
#endregion
//*******************************************************************
//
// Private Properties
//
//********************************************************************
#region Private Properties
private VoiceSynthesis VoiceSynthesizer
{
get
{
if (_voiceSynthesis == null && _isDisposed)
{
throw new ObjectDisposedException ("SpeechSynthesizer");
}
if (_voiceSynthesis == null)
{
WeakReference wr = new WeakReference (this);
_voiceSynthesis = new VoiceSynthesis (wr);
}
return _voiceSynthesis;
}
}
#endregion
//********************************************************************
//
// Private Fields
//
//*******************************************************************
#region Private Fields
// SpVoice for this synthesizer
private VoiceSynthesis _voiceSynthesis;
// Is the object disposed?
private bool _isDisposed;
// Count of number of consecutive calls to Paused
private bool paused;
// .Net Stream - keep a reference to it to avoid it to be GC
private Stream _outputStream;
// If stream were created in SpeechFx then close it, otherwise it should remain open.
private bool _closeStreamOnExit;
#if SPEECHSERVER || PROMPT_ENGINE
private static string _promptVoices = SAPICategories.PromptVoices;
#endif
#endregion Fields
}
//********************************************************************
//
// Public Enums
//
//*******************************************************************
#region Public Enums
///
/// TODOC
///
public enum SynthesizerState
{
///
/// TODOC
///
Ready,
///
/// TODOC
///
Speaking,
///
/// TODOC
///
Paused
}
#if !SPEECHSERVER
///
/// TODOC
///
[Flags]
public enum SynthesizerEmphasis
{
///
/// TODOC
///
Stressed = 1,
///
/// TODOC
///
Emphasized = 2
}
#endif
#endregion
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Speech.AudioFormat;
using System.Speech.Internal;
using System.Speech.Internal.ObjectTokens;
using System.Speech.Internal.Synthesis;
using System.Speech.Synthesis.TtsEngine;
using System.Threading;
using RegistryDataKey = System.Speech.Internal.ObjectTokens.RegistryDataKey;
using RegistryEntry = System.Collections.Generic.KeyValuePair;
namespace System.Speech.Synthesis
{
///
/// TODOC
///
public sealed class SpeechSynthesizer : IDisposable
#if SPEECHSERVER || PROMPT_ENGINE
, ISesSynthesizer
#endif
{
//*******************************************************************
//
// Constructors
//
//*******************************************************************
#region Constructors
///
/// TODOC
///
public SpeechSynthesizer ()
{
}
#if SPEECHSERVER
///
/// TODOC
///
internal SpeechSynthesizer (bool outputAsText) : this ()
{
VoiceSynthesizer._outputAsText = outputAsText;
}
#endif
///
///
///
~SpeechSynthesizer ()
{
Dispose (false);
}
///
/// TODOC
///
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
#endregion
//********************************************************************
//
// Public Methods
//
//*******************************************************************
#region public Methods
///
/// TODOC
///
///
public void SelectVoice (string name)
{
Helpers.ThrowIfEmptyOrNull (name, "name");
TTSVoice ttsVoice = VoiceSynthesizer.GetEngine (name, CultureInfo.CurrentUICulture, VoiceGender.NotSet, VoiceAge.NotSet, 1, true);
if (ttsVoice == null || name != ttsVoice.VoiceInfo.Name)
{
// No match - throw
throw new ArgumentException (SR.Get (SRID.SynthesizerSetVoiceNoMatch));
}
VoiceSynthesizer.Voice = ttsVoice;
}
///
/// TODOC
///
///
public void SelectVoiceByHints (VoiceGender gender)
{
SelectVoiceByHints (gender, VoiceAge.NotSet, 1, CultureInfo.CurrentUICulture);
}
///
/// TODOC
///
///
///
public void SelectVoiceByHints (VoiceGender gender, VoiceAge age)
{
SelectVoiceByHints (gender, age, 1, CultureInfo.CurrentUICulture);
}
///
/// TODOC
///
///
///
///
public void SelectVoiceByHints (VoiceGender gender, VoiceAge age, int voiceAlternate)
{
SelectVoiceByHints (gender, age, voiceAlternate, CultureInfo.CurrentUICulture);
}
///
/// TODOC
///
///
///
///
///
public void SelectVoiceByHints (VoiceGender gender, VoiceAge age, int voiceAlternate, CultureInfo culture)
{
Helpers.ThrowIfNull (culture, "culture");
if (voiceAlternate < 0)
{
throw new ArgumentOutOfRangeException ("voiceAlternate", SR.Get (SRID.PromptBuilderInvalidVariant));
}
if (!VoiceInfo.ValidateGender (gender))
{
throw new ArgumentException (SR.Get (SRID.EnumInvalid, "VoiceGender"), "gender");
}
if (!VoiceInfo.ValidateAge (age))
{
throw new ArgumentException (SR.Get (SRID.EnumInvalid, "VoiceAge"), "age");
}
TTSVoice ttsVoice = VoiceSynthesizer.GetEngine (null, culture, gender, age, voiceAlternate, true);
if (ttsVoice == null)
{
// No match - throw
throw new InvalidOperationException (SR.Get (SRID.SynthesizerSetVoiceNoMatch));
}
VoiceSynthesizer.Voice = ttsVoice;
}
///
/// TODOC
///
///
///
public Prompt SpeakAsync (string textToSpeak)
{
Helpers.ThrowIfNull (textToSpeak, "textToSpeak");
Prompt prompt = new Prompt (textToSpeak, SynthesisTextFormat.Text);
SpeakAsync (prompt);
return prompt;
}
///
/// TODOC
///
///
///
public void SpeakAsync (Prompt prompt)
{
Helpers.ThrowIfNull (prompt, "prompt");
prompt.Synthesizer = this;
VoiceSynthesizer.SpeakAsync (prompt);
}
///
/// TODOC
///
///
///
public Prompt SpeakSsmlAsync (string textToSpeak)
{
Helpers.ThrowIfNull (textToSpeak, "textToSpeak");
Prompt prompt = new Prompt (textToSpeak, SynthesisTextFormat.Ssml);
SpeakAsync (prompt);
return prompt;
}
///
/// TODOC
///
///
///
public Prompt SpeakAsync (PromptBuilder promptBuilder)
{
Helpers.ThrowIfNull (promptBuilder, "promptBuilder");
Prompt prompt = new Prompt (promptBuilder);
SpeakAsync (prompt);
return prompt;
}
///
/// TODOC
///
///
///
public void Speak (string textToSpeak)
{
Speak (new Prompt (textToSpeak, SynthesisTextFormat.Text));
}
///
/// TODOC
///
///
///
public void Speak (Prompt prompt)
{
Helpers.ThrowIfNull (prompt, "prompt");
// Avoid a dead lock if the synthesizer is Paused
if (State == SynthesizerState.Paused)
{
throw new InvalidOperationException (SR.Get (SRID.SynthesizerSyncSpeakWhilePaused));
}
prompt.Synthesizer = this;
prompt._syncSpeak = true;
VoiceSynthesizer.Speak (prompt);
}
///
/// TODOC
///
///
///
public void Speak (PromptBuilder promptBuilder)
{
Speak (new Prompt (promptBuilder));
}
///
/// TODOC
///
///
///
public void SpeakSsml (string textToSpeak)
{
Speak (new Prompt (textToSpeak, SynthesisTextFormat.Ssml));
}
///
/// Pause the playback of all speech in this synthesizer.
///
public void Pause ()
{
// Increment the Paused count
if (!paused)
{
VoiceSynthesizer.Pause ();
paused = true;
}
}
///
/// Resume the playback of all speech in this synthesizer.
///
public void Resume ()
{
if (paused)
{
VoiceSynthesizer.Resume ();
paused = false;
}
}
///
/// Cancel playback of all Prompts currently in the queue.
///
///
public void SpeakAsyncCancel (Prompt prompt)
{
Helpers.ThrowIfNull (prompt, "prompt");
VoiceSynthesizer.Abort (prompt);
}
///
/// Cancel playback of all Prompts currently in the queue.
///
public void SpeakAsyncCancelAll ()
{
VoiceSynthesizer.Abort ();
}
///
/// TODOC
///
///
// The stream is disposed when the speech synthesizer is disposed
public void SetOutputToWaveFile (string path)
{
Helpers.ThrowIfEmptyOrNull (path, "path");
SetOutputToNull ();
SetOutputStream (new FileStream (path, FileMode.Create, FileAccess.Write), null, true, true);
}
#if !SPEECHSERVER
///
/// TODOC
///
///
///
// The stream is disposed when the speech synthesizer is disposed
public void SetOutputToWaveFile (string path, SpeechAudioFormatInfo formatInfo)
{
Helpers.ThrowIfEmptyOrNull (path, "path");
Helpers.ThrowIfNull (formatInfo, "formatInfo");
SetOutputToNull ();
SetOutputStream (new FileStream (path, FileMode.Create, FileAccess.Write), formatInfo, true, true);
}
#endif
///
/// TODOC
///
///
public void SetOutputToWaveStream (Stream audioDestination)
{
Helpers.ThrowIfNull (audioDestination, "audioDestination");
SetOutputStream (audioDestination, null, true, false);
}
///
/// TODOC
///
///
///
#if !SPEECHSERVER
public void SetOutputToAudioStream (Stream audioDestination, SpeechAudioFormatInfo formatInfo)
#else
internal void SetOutputToAudioStream (Stream audioDestination, SpeechAudioFormatInfo formatInfo)
#endif
{
Helpers.ThrowIfNull (audioDestination, "audioDestination");
Helpers.ThrowIfNull (formatInfo, "formatInfo");
SetOutputStream (audioDestination, formatInfo, false, false);
}
///
/// TODOC
///
public void SetOutputToDefaultAudioDevice ()
{
SetOutputStream (null, null, true, false);
}
///
/// TODOC
///
// The stream is disposed when the speech synthesizer is disposed
public void SetOutputToNull ()
{
// Close the existing stream
if (_outputStream != Stream.Null)
{
VoiceSynthesizer.SetOutput (Stream.Null, null, true);
}
if (_outputStream != null)
{
if (_closeStreamOnExit)
{
_outputStream.Close ();
}
}
_outputStream = Stream.Null;
}
///
/// TODOC
///
///
// Dynamic content, use a method instead of a property to denote that fact
public Prompt GetCurrentlySpokenPrompt ()
{
return VoiceSynthesizer.Prompt;
}
///
/// TODOC
///
///
public ReadOnlyCollection GetInstalledVoices ()
{
return VoiceSynthesizer.GetInstalledVoices (null);
}
///
/// TODOC
///
///
///
public ReadOnlyCollection GetInstalledVoices (CultureInfo culture)
{
Helpers.ThrowIfNull (culture, "culture");
if (culture.Equals (CultureInfo.InvariantCulture))
{
throw new ArgumentException (SR.Get (SRID.InvariantCultureInfo), "culture");
}
return VoiceSynthesizer.GetInstalledVoices (culture);
}
///
/// TODOC
///
///
///
public void AddLexicon (Uri uri, string mediaType)
{
Helpers.ThrowIfNull (uri, "uri");
VoiceSynthesizer.AddLexicon (uri, mediaType);
}
///
/// TODOC
///
///
public void RemoveLexicon (Uri uri)
{
Helpers.ThrowIfNull (uri, "uri");
VoiceSynthesizer.RemoveLexicon (uri);
}
#if SPEECHSERVER || PROMPT_ENGINE
///
/// TODOC
///
///
///
void ISesSynthesizer.LoadDatabase (string localName, string alias)
{
VoiceSynthesizer.LoadDatabase (localName, alias);
}
///
/// TODOC
///
///
void ISesSynthesizer.UnloadDatabase (string alias)
{
VoiceSynthesizer.UnloadDatabase (alias);
}
///
/// TODOC
///
///
void ISesSynthesizer.SetResourceLoader (ISpeechResourceLoader resourceLoader)
{
VoiceSynthesizer.SetResourceLoader (resourceLoader);
}
#endif
//********************************************************************
//
// Public Events
//
//********************************************************************
#region public Events
///
/// TODOC
///
public event EventHandler SpeakStarted
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakStarted += value;
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakStarted -= value;
}
}
///
/// TODOC
///
public event EventHandler SpeakCompleted
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakCompleted += value;
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._speakCompleted -= value;
}
}
///
/// TODOC
///
public event EventHandler SpeakProgress
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.WordBoundary, ref VoiceSynthesizer._speakProgress, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.WordBoundary, ref VoiceSynthesizer._speakProgress, value);
}
}
///
/// TODOC
///
public event EventHandler BookmarkReached
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Bookmark, ref VoiceSynthesizer._bookmarkReached, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Bookmark, ref VoiceSynthesizer._bookmarkReached, value);
}
}
///
/// TODOC
///
public event EventHandler VoiceChange
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.VoiceChange, ref VoiceSynthesizer._voiceChange, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.VoiceChange, ref VoiceSynthesizer._voiceChange, value);
}
}
#if !SPEECHSERVER
#region WinFx
///
/// TODOC
///
public event EventHandler PhonemeReached
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Phoneme, ref VoiceSynthesizer._phonemeReached, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Phoneme, ref VoiceSynthesizer._phonemeReached, value);
}
}
///
/// TODOC
///
public event EventHandler VisemeReached
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Viseme, ref VoiceSynthesizer._visemeReached, value);
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Viseme, ref VoiceSynthesizer._visemeReached, value);
}
}
#endregion
#else
///
/// TODOC
///
public event EventHandler ProprietaryEngineEvent
{
[MethodImplAttribute(MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.AddEvent (TtsEventId.Private, ref VoiceSynthesizer._proprietaryEngineEvent, value);
}
[MethodImplAttribute(MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer.RemoveEvent (TtsEventId.Private, ref VoiceSynthesizer._proprietaryEngineEvent, value);
}
}
#endif
///
/// TODOC
///
///
public event EventHandler StateChanged
{
[MethodImplAttribute (MethodImplOptions.Synchronized)]
add
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._stateChanged += value;
}
[MethodImplAttribute (MethodImplOptions.Synchronized)]
remove
{
Helpers.ThrowIfNull (value, "value");
VoiceSynthesizer._stateChanged -= value;
}
}
#endregion
#endregion Events
//*******************************************************************
//
// Public Properties
//
//********************************************************************
#region public Properties
///
/// TODOC
///
///
public SynthesizerState State
{
get
{
return VoiceSynthesizer.State;
}
}
///
/// TODOC
///
public int Rate
{
set
{
if (value < -10 || value > 10)
{
throw new ArgumentOutOfRangeException ("value", SR.Get (SRID.RateOutOfRange));
}
VoiceSynthesizer.Rate = value;
}
get
{
return VoiceSynthesizer.Rate;
}
}
///
/// TODOC
///
public int Volume
{
set
{
if (value < 0 || value > 100)
{
throw new ArgumentOutOfRangeException ("value", SR.Get (SRID.ResourceUsageOutOfRange));
}
VoiceSynthesizer.Volume = value;
}
get
{
return VoiceSynthesizer.Volume;
}
}
///
/// TODOC
///
///
public VoiceInfo Voice
{
get
{
// Get the sapi voice
return VoiceSynthesizer.CurrentVoice (true).VoiceInfo;
}
}
//*******************************************************************
//
// Internal Properties
//
//*******************************************************************
#region Internal Properties
#if SPEECHSERVER || PROMPT_ENGINE
static internal string PromptVoices
{
set
{
_promptVoices = value;
}
get
{
return _promptVoices;
}
}
#endif
#endregion
#endregion
//*******************************************************************
//
// Private Methods
//
//********************************************************************
#region Private Methods
///
/// TODOC
///
///
///
///
///
private void SetOutputStream (Stream stream, SpeechAudioFormatInfo formatInfo, bool headerInfo, bool closeStreamOnExit)
{
SetOutputToNull ();
_outputStream = stream;
_closeStreamOnExit = closeStreamOnExit;
// Need to serialize into a proper wav file before closing the stream
VoiceSynthesizer.SetOutput (stream, formatInfo, headerInfo);
}
///
/// TODOC
///
///
private void Dispose (bool disposing)
{
if (!_isDisposed && disposing)
{
if (_voiceSynthesis != null)
{
// flag it first so asynchronous operation has more time to finish
_isDisposed = true;
SpeakAsyncCancelAll ();
// Flush the Output stream
if (_outputStream != null)
{
if (_closeStreamOnExit)
{
_outputStream.Close ();
}
else
{
_outputStream.Flush ();
}
_outputStream = null;
}
}
}
if (_voiceSynthesis != null)
{
// Terminate the background synthesis object the thread.
_voiceSynthesis.Dispose ();
_voiceSynthesis = null;
}
_isDisposed = true;
}
#endregion
//*******************************************************************
//
// Private Properties
//
//********************************************************************
#region Private Properties
private VoiceSynthesis VoiceSynthesizer
{
get
{
if (_voiceSynthesis == null && _isDisposed)
{
throw new ObjectDisposedException ("SpeechSynthesizer");
}
if (_voiceSynthesis == null)
{
WeakReference wr = new WeakReference (this);
_voiceSynthesis = new VoiceSynthesis (wr);
}
return _voiceSynthesis;
}
}
#endregion
//********************************************************************
//
// Private Fields
//
//*******************************************************************
#region Private Fields
// SpVoice for this synthesizer
private VoiceSynthesis _voiceSynthesis;
// Is the object disposed?
private bool _isDisposed;
// Count of number of consecutive calls to Paused
private bool paused;
// .Net Stream - keep a reference to it to avoid it to be GC
private Stream _outputStream;
// If stream were created in SpeechFx then close it, otherwise it should remain open.
private bool _closeStreamOnExit;
#if SPEECHSERVER || PROMPT_ENGINE
private static string _promptVoices = SAPICategories.PromptVoices;
#endif
#endregion Fields
}
//********************************************************************
//
// Public Enums
//
//*******************************************************************
#region Public Enums
///
/// TODOC
///
public enum SynthesizerState
{
///
/// TODOC
///
Ready,
///
/// TODOC
///
Speaking,
///
/// TODOC
///
Paused
}
#if !SPEECHSERVER
///
/// TODOC
///
[Flags]
public enum SynthesizerEmphasis
{
///
/// TODOC
///
Stressed = 1,
///
/// TODOC
///
Emphasized = 2
}
#endif
#endregion
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SchemaLookupTable.cs
- BasicAsyncResult.cs
- TemplateKey.cs
- CallContext.cs
- TemplatePagerField.cs
- OrderedDictionaryStateHelper.cs
- ManipulationPivot.cs
- GridViewColumn.cs
- KnownBoxes.cs
- ReadWriteObjectLock.cs
- ConfigurationCollectionAttribute.cs
- EntityProviderServices.cs
- ExtentCqlBlock.cs
- SQLInt64Storage.cs
- DetailsViewUpdateEventArgs.cs
- XPathNodePointer.cs
- DiscardableAttribute.cs
- AnnotationComponentManager.cs
- HtmlTableCell.cs
- Transform3DGroup.cs
- StringComparer.cs
- DataGridViewSelectedRowCollection.cs
- ExpressionVisitor.cs
- mactripleDES.cs
- TaskHelper.cs
- DataGridViewCellStateChangedEventArgs.cs
- EntityWithChangeTrackerStrategy.cs
- MeshGeometry3D.cs
- ToolBar.cs
- SHA1CryptoServiceProvider.cs
- StatusBarAutomationPeer.cs
- TriggerBase.cs
- DerivedKeySecurityTokenStub.cs
- WebUtil.cs
- AvTraceDetails.cs
- ActivityPropertyReference.cs
- ClientSponsor.cs
- ToolboxItemAttribute.cs
- PersonalizationStateInfo.cs
- XmlNodeReader.cs
- StylusPointPropertyId.cs
- JsonDeserializer.cs
- FormsAuthenticationTicket.cs
- IPAddress.cs
- ProcessHost.cs
- NamespaceDecl.cs
- TextSelectionHighlightLayer.cs
- SqlDataSource.cs
- RichTextBoxDesigner.cs
- XmlBinaryReaderSession.cs
- ConnectionManagementElementCollection.cs
- ToolStripStatusLabel.cs
- SpellerStatusTable.cs
- DockPanel.cs
- HtmlSelect.cs
- AxHost.cs
- GZipStream.cs
- PopupRootAutomationPeer.cs
- ComboBoxRenderer.cs
- RadioButtonStandardAdapter.cs
- MarkupExtensionReturnTypeAttribute.cs
- ConfigurationStrings.cs
- BinaryUtilClasses.cs
- SortableBindingList.cs
- MetadataItemEmitter.cs
- RSACryptoServiceProvider.cs
- LifetimeServices.cs
- BaseValidator.cs
- safex509handles.cs
- TextDataBindingHandler.cs
- DbProviderFactoriesConfigurationHandler.cs
- OverflowException.cs
- DataContractSerializerSection.cs
- Point3DCollection.cs
- ExtensionDataObject.cs
- IOException.cs
- Group.cs
- DispatcherExceptionEventArgs.cs
- LinkUtilities.cs
- SchemaInfo.cs
- HttpStreams.cs
- AssemblyBuilder.cs
- Pens.cs
- SQlBooleanStorage.cs
- QueryCacheKey.cs
- EDesignUtil.cs
- InternalPolicyElement.cs
- PropertyGridEditorPart.cs
- TableAutomationPeer.cs
- Matrix.cs
- SqlNotificationRequest.cs
- X509CertificateTrustedIssuerElement.cs
- DefaultCommandConverter.cs
- BooleanKeyFrameCollection.cs
- ISFClipboardData.cs
- DataGridCaption.cs
- EmptyCollection.cs
- SafeCryptContextHandle.cs
- DesignerDataStoredProcedure.cs
- InvalidCastException.cs