/// <summary>Speaks the specified text to the specified file.</summary>
		/// <param name="text">The text to be spoken.</param>
		/// <param name="audioPath">The file to which the spoken text should be written.</param>
		/// <remarks>Uses the Microsoft Speech libraries.</remarks>
		private void SpeakToFile(string text, FileInfo audioPath)
		{
			SpFileStream spFileStream = new SpFileStream();
			try
			{
				// Create the speech engine and set it to a random installed voice
				SpVoice speech = new SpVoice();
				ISpeechObjectTokens voices = speech.GetVoices(string.Empty, string.Empty);
				speech.Voice = voices.Item(NextRandom(voices.Count));

				// Set the format type to be heavily compressed.  This both decreases download
				// size and increases distortion.
				SpAudioFormatClass format = new SpAudioFormatClass();
				format.Type = SpeechAudioFormatType.SAFTGSM610_11kHzMono;
				spFileStream.Format = format;

				// Open the output stream for the file, speak to it, and wait until it's complete
				spFileStream.Open(audioPath.FullName, SpeechStreamFileMode.SSFMCreateForWrite, false);
				speech.AudioOutputStream = spFileStream;
				speech.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
				speech.Rate = -5;
				speech.WaitUntilDone(System.Threading.Timeout.Infinite);
			}
			finally
			{
				// Close the output file
				spFileStream.Close();
			}
		}
Example #2
0
 ///<summary>SpVoiceSpeak</summary>
 public static void SpVoiceSpeak
 (
  ref UtilitySpeechArgument  utilitySpeechArgument,
  ref string                 exceptionMessage
 )
 {
  object                 voice                  =  null;
  object[]               voiceArgv              =  null;                  
  SpeechVoiceSpeakFlags  speechVoiceSpeakFlags  =  SpeechVoiceSpeakFlagsDefault;
  SpAudioFormatClass     spAudioFormatClass     =  null;
  SpFileStream           spFileStream           =  null;
  SpVoice                spVoice                =  null;
  Type                   typeSAPISpVoice        =  null;
  try
  {
   spVoice                =  new SpVoice();
   typeSAPISpVoice        =  Type.GetTypeFromProgID("SAPI.SpVoice");
   voice                  =  Activator.CreateInstance( typeSAPISpVoice );
   voiceArgv              =  new object[2];
   voiceArgv[1]           =  0;
   speechVoiceSpeakFlags  =  SpeechVoiceSpeakFlagsEnum( false, utilitySpeechArgument.xml );
   if ( string.IsNullOrEmpty( utilitySpeechArgument.pathAudio ) == false )
   {
    spAudioFormatClass         =  new SpAudioFormatClass();
    spAudioFormatClass.Type    =  SpeechAudioFormatType.SAFTGSM610_11kHzMono; //Heavily compressed
    spFileStream               =  new SpFileStream();
    spFileStream.Format        =  spAudioFormatClass;
    spFileStream.Open( utilitySpeechArgument.pathAudio, SpeechStreamFileMode.SSFMCreateForWrite, false );
    spVoice.AudioOutputStream  =  spFileStream;
    spVoice.Rate = -5; //Ranges from -10 to 10 
   }
   foreach( string text in utilitySpeechArgument.text )
   {
    /*
    spVoice.Speak( text, speechVoiceSpeakFlags );
    */
    voiceArgv[0]  =  text;
    typeSAPISpVoice.InvokeMember("Speak", BindingFlags.InvokeMethod, null, voice, voiceArgv);
   }
   speechVoiceSpeakFlags = SpeechVoiceSpeakFlagsEnum( true, utilitySpeechArgument.xml );
   foreach( string pathSource in utilitySpeechArgument.pathSource )
   {
    if ( string.IsNullOrEmpty( pathSource ) ) { continue; }
    if ( File.Exists( pathSource ) == false ) { continue; }     
    spVoice.Speak( pathSource, speechVoiceSpeakFlags );
   }//foreach( string pathSource in utilitySpeechArgument.pathSource )
  }//try
  catch ( Exception exception ) { UtilityException.ExceptionLog( exception, exception.GetType().Name, ref exceptionMessage ); }
  if ( spFileStream != null ) { spFileStream.Close(); }
 }//public static void SpVoiceSpeak()