Quickstart: Recognize speech with the Speech SDK for .NET Framework (Windows)

Quickstarts are also available for text-to-speech and speech-translation.

If desired, choose a different programming language and/or environment:

Use this guide to create a speech-to-text console application using the .NET framework for Windows and the Speech SDK. When finished, you can use your computer's microphone to transcribe speech to text in real time.

For a quick demonstration (without building the Visual Studio project yourself as shown below):

Get the latest Cognitive Services Speech SDK Samples from GitHub.

Prerequisites

To complete this project, you'll need:

Create a Visual Studio project

  1. Open Visual Studio 2019.

  2. In the Start window, select Create a new project.

  3. Select Console App (.NET Framework), and then select Next.

  4. In Project name, enter helloworld, and then select Create.

  5. From the menu bar in Visual Studio, select Tools > Get Tools and Features, and check whether the .NET desktop development workload is available. If the workload hasn't been installed, mark the checkbox, then select Modify to start the installation. It may take a few minutes to download and install.

    If the checkbox next to .NET desktop development is selected, you can close the dialog box now.

    Enable .NET desktop development

The next step is to install the Speech SDK NuGet package, so you can reference it in the code.

  1. In the Solution Explorer, right-click helloworld, and then select Manage NuGet Packages to show the NuGet Package Manager.

    NuGet Package Manager

  2. In the upper-right corner, find the Package Source drop-down box, and make sure that nuget.org is selected.

  3. In the upper-left corner, select Browse.

  4. In the search box, type Microsoft.CognitiveServices.Speech package and press Enter.

  5. Select Microsoft.CognitiveServices.Speech, and then select Install to install the latest stable version.

    Install Microsoft.CognitiveServices.Speech NuGet package

  6. Accept all agreements and licenses to start the installation.

    After the package is installed, a confirmation appears in the Package Manager Console window.

Now, to build and run the console application, create a platform configuration matching your computer's architecture.

  1. From the menu bar, select Build > Configuration Manager. The Configuration Manager dialog box appears.

    Configuration Manager dialog box

  2. In the Active solution platform drop-down box, select New. The New Solution Platform dialog box appears.

  3. In the Type or select the new platform drop-down box:

    • If you're running 64-bit Windows, select x64.
    • If you're running 32-bit Windows, select x86.
  4. Select OK and then Close.

Add sample code

  1. Open Program.cs and replace the automatically generated code with this sample:

    using System;
    using System.Threading.Tasks;
    using Microsoft.CognitiveServices.Speech;
    
    namespace helloworld
    {
        class Program
        {
            public static async Task RecognizeSpeechAsync()
            {
                // Creates an instance of a speech config with specified subscription key and service region.
                // Replace with your own subscription key and service region (e.g., "westus").
                var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
    
                // Creates a speech recognizer.
                using (var recognizer = new SpeechRecognizer(config))
                {
                    Console.WriteLine("Say something...");
    
                    // Starts speech recognition, and returns after a single utterance is recognized. The end of a
                    // single utterance is determined by listening for silence at the end or until a maximum of 15
                    // seconds of audio is processed.  The task returns the recognition text as result. 
                    // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
                    // shot recognition like command or query. 
                    // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
                    var result = await recognizer.RecognizeOnceAsync();
    
                    // Checks result.
                    if (result.Reason == ResultReason.RecognizedSpeech)
                    {
                        Console.WriteLine($"We recognized: {result.Text}");
                    }
                    else if (result.Reason == ResultReason.NoMatch)
                    {
                        Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                    }
                    else if (result.Reason == ResultReason.Canceled)
                    {
                        var cancellation = CancellationDetails.FromResult(result);
                        Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");
    
                        if (cancellation.Reason == CancellationReason.Error)
                        {
                            Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                            Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                            Console.WriteLine($"CANCELED: Did you update the subscription info?");
                        }
                    }
                }
            }
    
            static void Main()
            {
                RecognizeSpeechAsync().Wait();
                Console.WriteLine("Please press a key to continue.");
                Console.ReadLine();
            }
        }
    }
    
  2. Locate and replace the string YourSubscriptionKey with your Speech Services subscription key.

  3. Locate and replace the string YourServiceRegion with the region associated with your subscription. For example, if you're using the free trial, the region is westus.

  4. Save the changes to the project.

Build and run the app

  1. From the menu bar, select Build > Build Solution. The code should compile without errors now.

    Screenshot of Visual Studio application, with Build Solution option highlighted

  2. From the menu bar, select Debug > Start Debugging, or press F5 to start the application.

    Screenshot of Visual Studio application, with Start Debugging option highlighted

  3. A console window will appear, prompting you to speak. Now, say something in English. Your speech is transmitted to the Speech Services and transcribed to text in real time. The result is printed to the console.

    Screenshot of console output after successful recognition

Next steps

See also