Skip to content

Instantly share code, notes, and snippets.

@jinalshah999
Created September 5, 2017 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jinalshah999/e60fa9153659b2785555a9944729e1bb to your computer and use it in GitHub Desktop.
Save jinalshah999/e60fa9153659b2785555a9944729e1bb to your computer and use it in GitHub Desktop.
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