|
1 | 1 | ---
|
2 |
| -title: "Quickstart: Recognize speech from a microphone, C# (.NET) - Speech service" |
3 |
| -titleSuffix: Azure Cognitive Services |
4 |
| -services: cognitive-services |
5 |
| -author: erhopf |
6 |
| -manager: nitinme |
| 2 | +author: IEvangelist |
7 | 3 | ms.service: cognitive-services
|
8 |
| -ms.subservice: speech-service |
9 | 4 | ms.topic: include
|
10 |
| -ms.date: 12/17/2019 |
11 |
| -ms.author: erhopf |
| 5 | +ms.date: 04/03/2020 |
| 6 | +ms.author: dapine |
12 | 7 | ---
|
13 | 8 |
|
14 | 9 | ## Prerequisites
|
15 | 10 |
|
16 | 11 | Before you get started:
|
17 | 12 |
|
18 | 13 | > [!div class="checklist"]
|
19 |
| -> * [Create an Azure Speech Resource](../../../../get-started.md) |
| 14 | +> * <a href="https://ms.portal.azure.com/#create/Microsoft.CognitiveServicesSpeechServices" target="_blank">Create an Azure Speech resource <span class="docon docon-navigate-external x-hidden-focus"></span></a> |
20 | 15 | > * [Setup your development environment and create an empty project](../../../../quickstarts/setup-platform.md?tabs=dotnet)
|
21 | 16 | > * Make sure that you have access to a microphone for audio capture
|
22 | 17 |
|
23 | 18 | ## Open your project in Visual Studio
|
24 | 19 |
|
25 | 20 | The first step is to make sure that you have your project open in Visual Studio.
|
26 | 21 |
|
27 |
| -1. Launch Visual Studio 2019. |
28 |
| -2. Load your project and open `Program.cs`. |
29 |
| - |
30 |
| -## Start with some boilerplate code |
31 |
| - |
32 |
| -Let's add some code that works as a skeleton for our project. Make note that you've created an async method called `RecognizeSpeechAsync()`. |
33 |
| -[!code-csharp[](~/samples-cognitive-services-speech-sdk/quickstart/csharp/dotnet/from-microphone/helloworld/Program.cs?range=5-15,43-52)] |
34 |
| - |
35 |
| -## Create a Speech configuration |
36 |
| - |
37 |
| -Before you can initialize a `SpeechRecognizer` object, you need to create a configuration that uses your subscription key and subscription region (choose the **Region identifier** from [region](https://aka.ms/speech/sdkregion). Insert this code in the `RecognizeSpeechAsync()` method. |
38 |
| - |
39 |
| -> [!NOTE] |
40 |
| -> This sample uses the `FromSubscription()` method to build the `SpeechConfig`. For a full list of available methods, see [SpeechConfig Class](https://docs.microsoft.com/dotnet/api/microsoft.cognitiveservices.speech.speechconfig?view=azure-dotnet). |
41 |
| -[!code-csharp[](~/samples-cognitive-services-speech-sdk/quickstart/csharp/dotnet/from-microphone/helloworld/Program.cs?range=16)] |
42 |
| -> The Speech SDK will default to recognizing using en-us for the language, see [Specify source language for speech to text](../../../../how-to-specify-source-language.md) for information on choosing the source language. |
43 |
| -
|
44 |
| -## Initialize a SpeechRecognizer |
45 |
| - |
46 |
| -Now, let's create a `SpeechRecognizer`. This object is created inside of a using statement to ensure the proper release of unmanaged resources. Insert this code in the `RecognizeSpeechAsync()` method, right below your Speech configuration. |
47 |
| -[!code-csharp[](~/samples-cognitive-services-speech-sdk/quickstart/csharp/dotnet/from-microphone/helloworld/Program.cs?range=17-19,42)] |
48 |
| - |
49 |
| -## Recognize a phrase |
50 |
| - |
51 |
| -From the `SpeechRecognizer` object, you're going to call the `RecognizeOnceAsync()` method. This method lets the Speech service know that you're sending a single phrase for recognition, and that once the phrase is identified to stop recognizing speech. |
52 |
| - |
53 |
| -Inside the using statement, add this code. |
54 |
| - |
55 |
| -[!code-csharp[](~/samples-cognitive-services-speech-sdk/quickstart/csharp/dotnet/from-microphone/helloworld/Program.cs?range=20)] |
56 |
| - |
57 |
| -## Display the recognition results (or errors) |
58 |
| - |
59 |
| -When the recognition result is returned by the Speech service, you'll want to do something with it. We're going to keep it simple and print the result to console. |
60 |
| - |
61 |
| -Inside the using statement, below `RecognizeOnceAsync()`, add this code. |
62 |
| - |
63 |
| -[!code-csharp[](~/samples-cognitive-services-speech-sdk/quickstart/csharp/dotnet/from-microphone/helloworld/Program.cs?range=22-41)] |
64 |
| - |
65 |
| -## Check your code |
66 |
| - |
67 |
| -At this point, your code should look like this. |
68 |
| - |
69 |
| -[!code-csharp[](~/samples-cognitive-services-speech-sdk/quickstart/csharp/dotnet/from-microphone/helloworld/Program.cs)] |
70 |
| - |
71 |
| -## Build and run your app |
72 |
| - |
73 |
| -Now you're ready to build your app and test our speech recognition using the Speech service. |
| 22 | +1. Launch **Visual Studio 2019**. |
| 23 | +2. Load your project and open *Program.cs*. |
| 24 | + |
| 25 | +## Source code |
| 26 | + |
| 27 | +Replace the contents of the *Program.cs* file with the following C# code. |
| 28 | + |
| 29 | +```csharp |
| 30 | +using System; |
| 31 | +using System.Threading.Tasks; |
| 32 | +using Microsoft.CognitiveServices.Speech; |
| 33 | + |
| 34 | +namespace Speech.Recognition |
| 35 | +{ |
| 36 | + class Program |
| 37 | + { |
| 38 | + static async Task Main() |
| 39 | + { |
| 40 | + await RecognizeSpeechAsync(); |
| 41 | + |
| 42 | + Console.WriteLine("Please press any key to continue..."); |
| 43 | + Console.ReadLine(); |
| 44 | + } |
| 45 | + |
| 46 | + static async Task RecognizeSpeechAsync() |
| 47 | + { |
| 48 | + var config = |
| 49 | + SpeechConfig.FromSubscription( |
| 50 | + "YourSubscriptionKey", |
| 51 | + "YourServiceRegion"); |
| 52 | + |
| 53 | + using var recognizer = new SpeechRecognizer(config); |
| 54 | + |
| 55 | + var result = await recognizer.RecognizeOnceAsync(); |
| 56 | + switch (result.Reason) |
| 57 | + { |
| 58 | + case ResultReason.RecognizedSpeech: |
| 59 | + Console.WriteLine($"We recognized: {result.Text}"); |
| 60 | + break; |
| 61 | + case ResultReason.NoMatch: |
| 62 | + Console.WriteLine($"NOMATCH: Speech could not be recognized."); |
| 63 | + break; |
| 64 | + case ResultReason.Canceled: |
| 65 | + var cancellation = CancellationDetails.FromResult(result); |
| 66 | + Console.WriteLine($"CANCELED: Reason={cancellation.Reason}"); |
| 67 | + |
| 68 | + if (cancellation.Reason == CancellationReason.Error) |
| 69 | + { |
| 70 | + Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}"); |
| 71 | + Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}"); |
| 72 | + Console.WriteLine($"CANCELED: Did you update the subscription info?"); |
| 73 | + } |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +[!INCLUDE [replace key and region](../replace-key-and-region.md)] |
| 82 | + |
| 83 | +## Code explanation |
| 84 | + |
| 85 | +[!INCLUDE [code explanation](../code-explanation.md)] |
| 86 | + |
| 87 | +## Build and run app |
| 88 | + |
| 89 | +Now you're ready to rebuild your app and test the speech recognition functionality using the Speech service. |
74 | 90 |
|
75 | 91 | 1. **Compile the code** - From the menu bar of Visual Studio, choose **Build** > **Build Solution**.
|
76 |
| -2. **Start your app** - From the menu bar, choose **Debug** > **Start Debugging** or press **F5**. |
77 |
| -3. **Start recognition** - It'll prompt you to speak a phrase in English. Your speech is sent to the Speech service, transcribed as text, and rendered in the console. |
| 92 | +2. **Start your app** - From the menu bar, choose **Debug** > **Start Debugging** or press <kbd>F5</kbd>. |
| 93 | +3. **Start recognition** - It will prompt you to speak a phrase in English. Your speech is sent to the Speech service, transcribed as text, and rendered in the console. |
78 | 94 |
|
79 | 95 | ## Next steps
|
80 | 96 |
|
81 |
| -[!INCLUDE [footer](./footer.md)] |
| 97 | +[!INCLUDE [footer](../footer.md)] |
0 commit comments