RecognizedAudio.WriteToWaveStream(Stream) Méthode

Définition

Écrit les données audio dans un flux au format d'onde.

public:
 void WriteToWaveStream(System::IO::Stream ^ outputStream);
public void WriteToWaveStream (System.IO.Stream outputStream);
member this.WriteToWaveStream : System.IO.Stream -> unit
Public Sub WriteToWaveStream (outputStream As Stream)

Paramètres

outputStream
Stream

Flux qui recevra les données audio.

Exemples

L’exemple suivant crée une grammaire de reconnaissance vocale pour l’entrée de nom, ajoute un gestionnaire pour l’événement SpeechRecognized et charge la grammaire dans un module de reconnaissance vocale in-process. Ensuite, il écrit les informations audio pour la partie nom de l’entrée dans un fichier audio. Le fichier audio est utilisé comme entrée dans un SpeechSynthesizer objet, qui parle une expression qui inclut l’audio enregistré.

private static void AddNameGrammar(SpeechRecognitionEngine recognizer)  
{  
  GrammarBuilder builder = new GrammarBuilder();  
  builder.Append("My name is");  
  builder.AppendWildcard();  

  Grammar nameGrammar = new Grammar(builder);  
  nameGrammar.Name = "Name Grammar";  
  nameGrammar.SpeechRecognized +=  
    new EventHandler<SpeechRecognizedEventArgs>(  
      NameSpeechRecognized);  

  recognizer.LoadGrammar(nameGrammar);  
}  

// Handle the SpeechRecognized event of the name grammar.  
private static void NameSpeechRecognized(  
  object sender, SpeechRecognizedEventArgs e)  
{  
  Console.WriteLine("Grammar ({0}) recognized speech: {1}",  
    e.Result.Grammar.Name, e.Result.Text);  

  try  
  {  
    // The name phrase starts after the first three words.  
    if (e.Result.Words.Count < 4)  
    {  

      // Add code to check for an alternate that contains the   
wildcard.  
      return;  
    }  

    RecognizedAudio audio = e.Result.Audio;  
    TimeSpan start = e.Result.Words[3].AudioPosition;  
    TimeSpan duration = audio.Duration - start;  

    // Add code to verify and persist the audio.  
    string path = @"C:\temp\nameAudio.wav";  
    using (Stream outputStream = new FileStream(path, FileMode.Create))  
    {  
      RecognizedAudio nameAudio = audio.GetRange(start, duration);  
      nameAudio.WriteToWaveStream(outputStream);  
      outputStream.Close();  
    }  

    Thread testThread =  
      new Thread(new ParameterizedThreadStart(TestAudio));  
    testThread.Start(path);  
  }  
  catch (Exception ex)  
  {  
    Console.WriteLine("Exception thrown while processing audio:");  
    Console.WriteLine(ex.ToString());  
  }  
}  

// Use the speech synthesizer to play back the .wav file  
// that was created in the SpeechRecognized event handler.  

private static void TestAudio(object item)  
{  
  string path = item as string;  
  if (path != null && File.Exists(path))  
  {  
    SpeechSynthesizer synthesizer = new SpeechSynthesizer();  
    PromptBuilder builder = new PromptBuilder();  
    builder.AppendText("Hello");  
    builder.AppendAudio(path);  
    synthesizer.Speak(builder);  
  }  
}  

Remarques

Les données audio sont écrites outputStream au format Wave, qui comprend un en-tête RIFF (Resource Interchange File Format).

La WriteToAudioStream méthode utilise le même format binaire, mais n’inclut pas l’en-tête Wave.

S’applique à

Voir aussi