Created
September 5, 2017 11:56
-
-
Save jinalshah999/e60fa9153659b2785555a9944729e1bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BingSpeechApp | |
{ | |
using System.Media; | |
using System.Threading; | |
using System.Windows.Threading; | |
using CognitiveServicesTTS; | |
using Microsoft.CognitiveServices.SpeechRecognition; | |
/// <summary> | |
/// Interaction logic for MainWindow.xaml | |
/// </summary> | |
public partial class MainWindow : Window | |
{ | |
private MicrophoneRecognitionClient micClient; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
this.micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient( | |
SpeechRecognitionMode.ShortPhrase, | |
"en-US", | |
"Your_Key_Here"); | |
this.micClient.OnMicrophoneStatus += MicClient_OnMicrophoneStatus; | |
this.micClient.OnResponseReceived += MicClient_OnResponseReceived; | |
} | |
private void button_Click(object sender, RoutedEventArgs e) | |
{ | |
this.MySpeechResponse.Text = string.Empty; | |
this.MySpeechResponseConfidence.Text = string.Empty; | |
this.micClient.StartMicAndRecognition(); | |
} | |
private void MicClient_OnMicrophoneStatus(object sender, MicrophoneEventArgs e) | |
{ | |
Application.Current.Dispatcher.BeginInvoke( | |
DispatcherPriority.Normal, | |
new Action( | |
() => | |
{ | |
if (e.Recording) | |
{ | |
this.status.Text = "Listening"; | |
this.RecordingBar.Visibility = Visibility.Visible; | |
} | |
else | |
{ | |
this.status.Text = "Not Listening"; | |
this.RecordingBar.Visibility = Visibility.Collapsed; | |
} | |
})); | |
} | |
private async void MicClient_OnResponseReceived(object sender, SpeechResponseEventArgs e) | |
{ | |
if (e.PhraseResponse.Results.Length > 0) | |
{ | |
await Application.Current.Dispatcher.BeginInvoke( | |
DispatcherPriority.Normal, new Action(() => | |
{ | |
this.MySpeechResponse.Text = $"'{e.PhraseResponse.Results[0].DisplayText}',"; | |
this.MySpeechResponseConfidence.Text = $"confidence: { e.PhraseResponse.Results[0].Confidence}"; | |
//this.Speak(this.MySpeechResponse.Text); | |
})); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment