Getting Started With Bing Speech API In WPF Applications

What is cognition?

The mental action or process of acquiring knowledge and understanding through thought, experience, and the senses.

What are Cognitive Services?

Cognitive Services are a set of APIs that are designed to democratize artificial intelligence by enabling systems to see, hear, speak, understand, and interpret our needs using natural methods of communication.

What these services generally do is to bring structured semantic data to human knowledge I/O with a degree of confidence.

Vision Speech Language Knowledge Search
Computer VisionContent ModeratorEmotionFaceVideoBing SpeechCustom Speech ServiceSpeaker RecognitionBing Spell CheckLanguage UnderstandingLinguistic AnalysisText AnalyticsTranslatorWebLMAcademicEntity LinkingKnowledge ExplorationQnA MakerRecommendationsBing AutosuggestBing Image SearchBing News SearchBing Video SearchBing Web Search

Content Credit

Microsoft / Technical Community Content

Prerequisites

In this tutorial, we will cover Bing Speech API for speech to text (STT) using WPF application in Visual Studio 2017. First, we will start with how to get keys for Bing Speech API.

To register for Bing Speech API, perform the following steps.

  1. Navigate here.
  2. Navigate to Speech tab and select "Bing Speech API". Then, hit "Get API Key".

    Cognitive Services

  3. Select "I agree" and your region, as shown in the image.

    Cognitive Services

  4. Login with Microsoft account to get your API Key.

    Cognitive Services

  5. Save your key.

    Cognitive Services

Now, it’s time to create WPF application using Visual Studio 2017. Please follow the steps described below.

  1. Open Visual Studio 2017. In my case, I am using Community Edition.
  2. Go to File >>  New >> Project.
  3. Select WPF App (.NET Framework) from, expand Installed > Templates > Visual C# > Windows Classic Desktop > and select WPF App (.NET Framework).

    Cognitive Services

  4. Then, inside Solution Explorer, right click your project and click "Manage NuGet Packages".

    Cognitive Services

  5. Inside the NuGet Package Manager window, navigate to Browse tab and search for Microsoft.ProjectOxford.Speech. When found, install both x-86 and x-64 versions.

    Cognitive Services

  6. In Solution Explorer, check your references. Speech Client will be added automatically.

    Cognitive Services

  7. Now, almost all the set up is done. Let’s dive into app design. Copy the following code to your MainWindow.xaml file and replace the code between <Grid></Grid>.
    1. <Grid>  
    2.         <Grid.RowDefinitions>  
    3.             <RowDefinition Height="Auto"></RowDefinition>  
    4.             <RowDefinition Height="Auto"></RowDefinition>  
    5.             <RowDefinition Height="Auto"></RowDefinition>  
    6.             <RowDefinition Height="Auto"></RowDefinition>  
    7.             <RowDefinition Height="*"></RowDefinition>  
    8.         </Grid.RowDefinitions>  
    9.         <Grid.ColumnDefinitions>  
    10.             <ColumnDefinition Width="Auto"></ColumnDefinition>  
    11.             <ColumnDefinition Width="*"></ColumnDefinition>  
    12.             <ColumnDefinition Width="3*"></ColumnDefinition>  
    13.         </Grid.ColumnDefinitions>  
    14.         <TextBlock FontSize="24" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" Margin="4">Bing Speech API Demo</TextBlock>  
    15.         <Button x:Name="button" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="button_Click"  
    16.                 Grid.Row="1" Grid.Column="0" Grid.RowSpan="3" Margin="4" Height="75">  
    17.             <Button.Content>  
    18.                 <StackPanel Orientation="Vertical">  
    19.                     <TextBlock FontSize="16">Speak</TextBlock>  
    20.                 </StackPanel>  
    21.             </Button.Content>  
    22.         </Button>  
    23.   
    24.         <TextBlock x:Name="status" TextWrapping="Wrap" Text="Not Listening" VerticalAlignment="Center" FontSize="16" Visibility="Collapsed"  
    25.                    Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Margin="4"/>  
    26.   
    27.         <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2">  
    28.             <TextBlock x:Name="MySpeechResponse" FontSize="20" Margin="4" TextWrapping="Wrap" VerticalAlignment="Center" />  
    29.             <TextBlock x:Name="MySpeechResponseConfidence" FontSize="12" Margin="4" TextWrapping="Wrap" VerticalAlignment="Center" />  
    30.         </StackPanel>  
    31.   
    32.         
    33.         <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="RecordingBar" Visibility="Collapsed"  
    34.                     Grid.Row="0" Grid.Column="0" Grid.RowSpan="5" Grid.ColumnSpan="3">  
    35.               
    36.             <ProgressBar HorizontalAlignment="Left" Width="207" Margin="0,16,0,0" IsIndeterminate="True" />  
    37.         </StackPanel>  
    38.   
    39.   
    40.   
    41.     </Grid>  
  1. Paste the following code to MainWindow.xaml.cs file.
    1. namespace BingSpeechApp  
    2. {  
    3.     using System.Media;  
    4.     using System.Threading;  
    5.     using System.Windows.Threading;  
    6.     using CognitiveServicesTTS;  
    7.       using Microsoft.CognitiveServices.SpeechRecognition;  
    8.     /// <summary>  
    9.     /// Interaction logic for MainWindow.xaml  
    10.     /// </summary>  
    11.     public partial class MainWindow : Window  
    12.     {  
    13.         private MicrophoneRecognitionClient micClient;  
    14.         public MainWindow()  
    15.         {  
    16.             InitializeComponent();  
    17.             this.micClient = SpeechRecognitionServiceFactory.CreateMicrophoneClient(  
    18.                 SpeechRecognitionMode.ShortPhrase,  
    19.                 "en-US",  
    20.                 "Your_Key_Here");  
    21.             this.micClient.OnMicrophoneStatus += MicClient_OnMicrophoneStatus;  
    22.             this.micClient.OnResponseReceived += MicClient_OnResponseReceived;  
    23.         }  
    24.   
    25.         private void button_Click(object sender, RoutedEventArgs e)  
    26.         {           
    27.             this.MySpeechResponse.Text = string.Empty;  
    28.             this.MySpeechResponseConfidence.Text = string.Empty;  
    29.             this.micClient.StartMicAndRecognition();  
    30.         }  
    31.   
    32.         private void MicClient_OnMicrophoneStatus(object sender, MicrophoneEventArgs e)  
    33.         {  
    34.             Application.Current.Dispatcher.BeginInvoke(  
    35.                 DispatcherPriority.Normal,  
    36.                 new Action(  
    37.                     () =>  
    38.                     {  
    39.                         if (e.Recording)  
    40.                         {  
    41.                             this.status.Text = "Listening";  
    42.                             this.RecordingBar.Visibility = Visibility.Visible;  
    43.                         }  
    44.                         else  
    45.                         {  
    46.                             this.status.Text = "Not Listening";  
    47.                             this.RecordingBar.Visibility = Visibility.Collapsed;  
    48.                         }  
    49.                     }));  
    50.         }  
    51.   
    52.         private async void MicClient_OnResponseReceived(object sender, SpeechResponseEventArgs e)  
    53.         {  
    54.             if (e.PhraseResponse.Results.Length > 0)  
    55.             {  
    56.                 await Application.Current.Dispatcher.BeginInvoke(  
    57.                     DispatcherPriority.Normal, new Action(() =>  
    58.                     {  
    59.                         this.MySpeechResponse.Text = $"'{e.PhraseResponse.Results[0].DisplayText}',";  
    60.                         this.MySpeechResponseConfidence.Text = $"confidence: { e.PhraseResponse.Results[0].Confidence}";  
    61.                           
    62.                         //this.Speak(this.MySpeechResponse.Text);  
    63.                     }));  
    64.             }  
    65.         }  
    66. }  
    67. }  

Download Here


Similar Articles