Skip to content

Commit ebe9cb7

Browse files
authored
Merge pull request #109850 from IEvangelist/sharpBits
Refactor C# speech recognition quickstart to be more concise
2 parents 51ecc1d + 5676061 commit ebe9cb7

File tree

17 files changed

+194
-259
lines changed

17 files changed

+194
-259
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
author: IEvangelist
3+
ms.service: cognitive-services
4+
ms.topic: include
5+
ms.date: 04/02/2020
6+
ms.author: dapine
7+
---
8+
9+
The Speech resource subscription key and region are required to create a speech configuration object. The configuration object is needed to instantiate a speech recognizer object.
10+
11+
The recognizer instance exposes multiple ways to recognize speech. In this example, speech is recognized once. This functionality 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. Once the result is yielded, the code will write the recognition reason to the console.
12+
13+
> [!TIP]
14+
> 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.

articles/cognitive-services/Speech-Service/includes/quickstarts/from-microphone/cpp/footer.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

articles/cognitive-services/Speech-Service/includes/quickstarts/from-microphone/cpp/linux.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
---
2-
title: 'Quickstart: Recognize speech from a microphone, C++ (Linux) - Speech service'
3-
titleSuffix: Azure Cognitive Services
4-
services: cognitive-services
5-
author: wolfma61
6-
manager: nitinme
2+
author: IEvangelist
73
ms.service: cognitive-services
8-
ms.subservice: speech-service
94
ms.topic: include
10-
ms.date: 12/17/2019
11-
ms.author: wolfma
5+
ms.date: 04/03/2020
6+
ms.author: dapine
127
---
138

149
## Prerequisites
1510

1611
Before you get started:
1712

1813
> [!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>
2015
> * [Setup your development environment and create an empty project](../../../../quickstarts/setup-platform.md?tabs=linux)
2116
> * Make sure that you have access to a microphone for audio capture
2217
23-
## Add sample code
18+
## Source code
2419

25-
1. Create a C++ source file named `helloworld.cpp`, and paste the following code into it.
20+
Create a C++ source file named *helloworld.cpp*, and paste the following code into it.
2621

27-
[!code-cpp[Quickstart Code](~/samples-cognitive-services-speech-sdk/quickstart/cpp/linux/from-microphone/helloworld.cpp#code)]
22+
[!code-cpp[Quickstart Code](~/samples-cognitive-services-speech-sdk/quickstart/cpp/linux/from-microphone/helloworld.cpp#code)]
2823

29-
1. In this new file, replace the string `YourSubscriptionKey` with your Speech service subscription key.
24+
[!INCLUDE [replace key and region](../replace-key-and-region.md)]
3025

31-
1. Replace the string `YourServiceRegion` with the **Region identifier** from [region](https://aka.ms/speech/sdkregion) associated with your subscription (for example, `westus` for the free trial subscription).
26+
## Code explanation
3227

33-
> [!NOTE]
34-
> 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.
28+
[!INCLUDE [code explanation](../code-explanation.md)]
3529

3630
## Build the app
3731

@@ -93,4 +87,4 @@ Before you get started:
9387
9488
## Next steps
9589
96-
[!INCLUDE [footer](./footer.md)]
90+
[!INCLUDE [footer](../footer.md)]

articles/cognitive-services/Speech-Service/includes/quickstarts/from-microphone/cpp/macos.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
---
2-
title: 'Quickstart: Recognize speech from a microphone, C++ (macOS) - Speech service'
3-
titleSuffix: Azure Cognitive Services
4-
services: cognitive-services
5-
author: wolfma61
6-
manager: nitinme
2+
author: IEvangelist
73
ms.service: cognitive-services
8-
ms.subservice: speech-service
94
ms.topic: include
10-
ms.date: 12/17/2019
11-
ms.author: wolfma
5+
ms.date: 04/03/2020
6+
ms.author: dapine
127
---
138

149
## Prerequisites
1510

1611
Before you get started:
1712

1813
> [!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>
2015
> * [Setup your development environment and create an empty project](../../../../quickstarts/setup-platform.md?tabs=macos)
2116
> * Make sure that you have access to a microphone for audio capture
2217
23-
## Add sample code
18+
## Source code
2419

25-
1. Create a C++ source file named `helloworld.cpp`, and paste the following code into it.
20+
Create a C++ source file named *helloworld.cpp*, and paste the following code into it.
2621

27-
[!code-cpp[Quickstart Code](~/samples-cognitive-services-speech-sdk/quickstart/cpp/macos/from-microphone/helloworld.cpp#code)]
22+
[!code-cpp[Quickstart Code](~/samples-cognitive-services-speech-sdk/quickstart/cpp/macos/from-microphone/helloworld.cpp#code)]
2823

29-
1. In this new file, replace the string `YourSubscriptionKey` with your Speech service subscription key.
24+
[!INCLUDE [replace key and region](../replace-key-and-region.md)]
3025

31-
1. Replace the string `YourServiceRegion` with the **Region identifier** from [region](https://aka.ms/speech/sdkregion) associated with your subscription (for example, `westus` for the free trial subscription).
26+
## Code explanation
3227

33-
> [!NOTE]
34-
> 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.
28+
[!INCLUDE [code explanation](../code-explanation.md)]
3529

3630
## Build the app
3731

@@ -67,4 +61,4 @@ Before you get started:
6761
6862
## Next steps
6963
70-
[!INCLUDE [footer](./footer.md)]
64+
[!INCLUDE [footer](../footer.md)]
Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,42 @@
11
---
2-
title: 'Quickstart: Recognize speech from a microphone, C++ (Windows) - Speech service'
3-
titleSuffix: Azure Cognitive Services
4-
description: Learn how to recognize speech in C++ on Windows Desktop by using the Speech SDK
5-
services: cognitive-services
6-
author: wolfma61
7-
manager: nitinme
2+
author: IEvangelist
83
ms.service: cognitive-services
9-
ms.subservice: speech-service
104
ms.topic: include
11-
ms.date: 12/17/2019
12-
ms.author: wolfma
5+
ms.date: 04/03/2020
6+
ms.author: dapine
137
---
148

159
## Prerequisites
1610

1711
Before you get started:
1812

1913
> [!div class="checklist"]
20-
> * [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>
2115
> * [Setup your development environment and create an empty project](../../../../quickstarts/setup-platform.md?tabs=windows)
2216
> * Make sure that you have access to a microphone for audio capture
2317
24-
## Add sample code
18+
## Source code
2519

26-
1. Open the source file **helloworld.cpp**.
20+
Create a C++ source file named *helloworld.cpp*, and paste the following code into it.
2721

28-
1. Replace all the code with the following snippet:
22+
[!code-cpp[Quickstart Code](~/samples-cognitive-services-speech-sdk/quickstart/cpp/windows/from-microphone/helloworld/helloworld.cpp#code)]
2923

30-
[!code-cpp[Quickstart Code](~/samples-cognitive-services-speech-sdk/quickstart/cpp/windows/from-microphone/helloworld/helloworld.cpp#code)]
24+
[!INCLUDE [replace key and region](../replace-key-and-region.md)]
3125

32-
1. In the same file, replace the string `YourSubscriptionKey` with your subscription key.
26+
## Code explanation
3327

34-
1. Replace the string `YourServiceRegion` with the **Region identifier** from [region](https://aka.ms/speech/sdkregion) associated with your subscription (for example, `westus` for the free trial subscription).
28+
[!INCLUDE [code explanation](../code-explanation.md)]
3529

36-
1. From the menu bar, choose **File** > **Save All**.
37-
38-
> [!NOTE]
39-
> 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.
40-
41-
## Build and run the application
30+
## Build and run app
4231

4332
1. From the menu bar, select **Build** > **Build Solution** to build the application. The code should compile without errors now.
4433

45-
1. Choose **Debug** > **Start Debugging** (or press **F5**) to start the **helloworld** application.
34+
1. Choose **Debug** > **Start Debugging** (or press <kbd>F5</kbd>) to start the **helloworld** application.
4635

4736
1. Speak an English phrase or sentence. The application transmits your speech to the Speech service, which transcribes to text and sends it back to the application for display.
4837

4938
![Console output after successful recognition](~/articles/cognitive-services/Speech-Service/media/sdk/qs-cpp-windows-08-console-output-release.png)
5039

5140
## Next steps
5241

53-
[!INCLUDE [footer](./footer.md)]
42+
[!INCLUDE [footer](../footer.md)]

articles/cognitive-services/Speech-Service/includes/quickstarts/from-microphone/csharp/csharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ manager: nitinme
77
ms.service: cognitive-services
88
ms.subservice: speech-service
99
ms.topic: include
10-
ms.date: 11/07/2019
10+
ms.date: 04/02/2020
1111
ms.author: erhopf
1212
---
1313

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,97 @@
11
---
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
73
ms.service: cognitive-services
8-
ms.subservice: speech-service
94
ms.topic: include
10-
ms.date: 12/17/2019
11-
ms.author: erhopf
5+
ms.date: 04/03/2020
6+
ms.author: dapine
127
---
138

149
## Prerequisites
1510

1611
Before you get started:
1712

1813
> [!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>
2015
> * [Setup your development environment and create an empty project](../../../../quickstarts/setup-platform.md?tabs=dotnet)
2116
> * Make sure that you have access to a microphone for audio capture
2217
2318
## Open your project in Visual Studio
2419

2520
The first step is to make sure that you have your project open in Visual Studio.
2621

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.
7490

7591
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.
7894

7995
## Next steps
8096

81-
[!INCLUDE [footer](./footer.md)]
97+
[!INCLUDE [footer](../footer.md)]

articles/cognitive-services/Speech-Service/includes/quickstarts/from-microphone/csharp/footer.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)