|
| 1 | +--- |
| 2 | +author: eric-urban |
| 3 | +ms.service: cognitive-services |
| 4 | +ms.subservice: speech-service |
| 5 | +ms.date: 02/17/2023 |
| 6 | +ms.topic: include |
| 7 | +ms.author: eur |
| 8 | +--- |
| 9 | + |
| 10 | +[!INCLUDE [Header](../../common/cpp.md)] |
| 11 | + |
| 12 | +[!INCLUDE [Introduction](intro.md)] |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +[!INCLUDE [Prerequisites](../../common/azure-prerequisites-clu.md)] |
| 17 | + |
| 18 | +## Set up the environment |
| 19 | +The Speech SDK is available as a [NuGet package](https://www.nuget.org/packages/Microsoft.CognitiveServices.Speech) and implements .NET Standard 2.0. You install the Speech SDK later in this guide, but first check the [SDK installation guide](../../../quickstarts/setup-platform.md?pivots=programming-language-cpp) for any more requirements. |
| 20 | + |
| 21 | +### Set environment variables |
| 22 | + |
| 23 | +[!INCLUDE [Environment variables](../../common/environment-variables.md)] |
| 24 | + |
| 25 | +## Create a Conversational Language Understanding project |
| 26 | + |
| 27 | +[!INCLUDE [Deploy CLU model](deploy-clu-model.md)] |
| 28 | + |
| 29 | +You'll use the project name and deployment name in the next section. |
| 30 | + |
| 31 | +## Recognize intents from a microphone |
| 32 | + |
| 33 | +Follow these steps to create a new console application and install the Speech SDK. |
| 34 | + |
| 35 | +1. Create a new C++ console project in Visual Studio Community 2022 named `SpeechRecognition`. |
| 36 | +1. Install the Speech SDK in your new project with the NuGet package manager. |
| 37 | + ```powershell |
| 38 | + Install-Package Microsoft.CognitiveServices.Speech |
| 39 | + ``` |
| 40 | +1. Replace the contents of `SpeechRecognition.cpp` with the following code: |
| 41 | + |
| 42 | + ```cpp |
| 43 | + #include <iostream> |
| 44 | + #include <stdlib.h> |
| 45 | + #include <speechapi_cxx.h> |
| 46 | + |
| 47 | + using namespace Microsoft::CognitiveServices::Speech; |
| 48 | + using namespace Microsoft::CognitiveServices::Speech::Audio; |
| 49 | + using namespace Microsoft::CognitiveServices::Speech::Intent; |
| 50 | + |
| 51 | + std::string GetEnvironmentVariable(const char* name); |
| 52 | + |
| 53 | + int main() |
| 54 | + { |
| 55 | + // This example requires environment variables named: |
| 56 | + // "LANGUAGE_KEY", "LANGUAGE_ENDPOINT", "SPEECH_KEY", and "SPEECH_REGION" |
| 57 | + auto languageKey = GetEnvironmentVariable("LANGUAGE_KEY"); |
| 58 | + auto languageEndpoint = GetEnvironmentVariable("LANGUAGE_ENDPOINT"); |
| 59 | + auto speechKey = GetEnvironmentVariable("SPEECH_KEY"); |
| 60 | + auto speechRegion = GetEnvironmentVariable("SPEECH_REGION"); |
| 61 | + |
| 62 | + auto cluProjectName = "YourProjectNameGoesHere"; |
| 63 | + auto cluDeploymentName = "YourDeploymentNameGoesHere"; |
| 64 | + |
| 65 | + if ((size(languageKey) == 0) || (size(languageEndpoint) == 0) || (size(speechKey) == 0) || (size(speechRegion) == 0)) { |
| 66 | + std::cout << "Please set LANGUAGE_KEY, LANGUAGE_ENDPOINT, SPEECH_KEY, and SPEECH_REGION environment variables." << std::endl; |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + auto speechConfig = SpeechConfig::FromSubscription(speechKey, speechRegion); |
| 71 | + |
| 72 | + speechConfig->SetSpeechRecognitionLanguage("en-US"); |
| 73 | + |
| 74 | + auto audioConfig = AudioConfig::FromDefaultMicrophoneInput(); |
| 75 | + auto intentRecognizer = IntentRecognizer::FromConfig(speechConfig, audioConfig); |
| 76 | + |
| 77 | + std::vector<std::shared_ptr<LanguageUnderstandingModel>> models; |
| 78 | + |
| 79 | + auto cluModel = ConversationalLanguageUnderstandingModel::FromResource( |
| 80 | + languageKey, |
| 81 | + languageEndpoint, |
| 82 | + cluProjectName, |
| 83 | + cluDeploymentName); |
| 84 | + |
| 85 | + models.push_back(cluModel); |
| 86 | + intentRecognizer->ApplyLanguageModels(models); |
| 87 | + |
| 88 | + std::cout << "Speak into your microphone.\n"; |
| 89 | + auto result = intentRecognizer->RecognizeOnceAsync().get(); |
| 90 | + |
| 91 | + if (result->Reason == ResultReason::RecognizedIntent) |
| 92 | + { |
| 93 | + std::cout << "RECOGNIZED: Text=" << result->Text << std::endl; |
| 94 | + std::cout << " Intent Id: " << result->IntentId << std::endl; |
| 95 | + |
| 96 | + // There is a known issue with the LanguageUnderstandingServiceResponse_JsonResult |
| 97 | + // property when used with CLU in the Speech SDK version 1.25. |
| 98 | + // The following should return JSON in a future release. |
| 99 | + std::cout << " Intent Service JSON: " << result->Properties.GetProperty(PropertyId::LanguageUnderstandingServiceResponse_JsonResult) << std::endl; |
| 100 | + } |
| 101 | + else if (result->Reason == ResultReason::RecognizedSpeech) |
| 102 | + { |
| 103 | + std::cout << "RECOGNIZED: Text=" << result->Text << " (intent could not be recognized)" << std::endl; |
| 104 | + } |
| 105 | + else if (result->Reason == ResultReason::NoMatch) |
| 106 | + { |
| 107 | + std::cout << "NOMATCH: Speech could not be recognized." << std::endl; |
| 108 | + } |
| 109 | + else if (result->Reason == ResultReason::Canceled) |
| 110 | + { |
| 111 | + auto cancellation = CancellationDetails::FromResult(result); |
| 112 | + std::cout << "CANCELED: Reason=" << (int)cancellation->Reason << std::endl; |
| 113 | + |
| 114 | + if (cancellation->Reason == CancellationReason::Error) |
| 115 | + { |
| 116 | + std::cout << "CANCELED: ErrorCode=" << (int)cancellation->ErrorCode << std::endl; |
| 117 | + std::cout << "CANCELED: ErrorDetails=" << cancellation->ErrorDetails << std::endl; |
| 118 | + std::cout << "CANCELED: Did you update the subscription info?" << std::endl; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + std::string GetEnvironmentVariable(const char* name) |
| 124 | + { |
| 125 | + #if defined(_MSC_VER) |
| 126 | + size_t requiredSize = 0; |
| 127 | + (void)getenv_s(&requiredSize, nullptr, 0, name); |
| 128 | + if (requiredSize == 0) |
| 129 | + { |
| 130 | + return ""; |
| 131 | + } |
| 132 | + auto buffer = std::make_unique<char[]>(requiredSize); |
| 133 | + (void)getenv_s(&requiredSize, buffer.get(), requiredSize, name); |
| 134 | + return buffer.get(); |
| 135 | + #else |
| 136 | + auto value = getenv(name); |
| 137 | + return value ? value : ""; |
| 138 | + #endif |
| 139 | + } |
| 140 | + ``` |
| 141 | +
|
| 142 | +1. In `SpeechRecognition.cpp` set the `cluProjectName` and `cluDeploymentName` variables to the names of your project and deployment. For information about how to create a CLU project and deployment, see [Create a Conversational Language Understanding project](#create-a-conversational-language-understanding-project). |
| 143 | +1. To change the speech recognition language, replace `en-US` with another [supported language](~/articles/cognitive-services/speech-service/supported-languages.md). For example, `es-ES` for Spanish (Spain). The default language is `en-US` if you don't specify a language. For details about how to identify one of multiple languages that might be spoken, see [language identification](~/articles/cognitive-services/speech-service/language-identification.md). |
| 144 | +
|
| 145 | +[Build and run](/cpp/build/vscpp-step-2-build) your new console application to start speech recognition from a microphone. |
| 146 | +
|
| 147 | +> [!IMPORTANT] |
| 148 | +> Make sure that you set the `LANGUAGE_KEY`, `LANGUAGE_ENDPOINT`, `SPEECH__KEY`, and `SPEECH__REGION` environment variables as described [above](#set-environment-variables). If you don't set these variables, the sample will fail with an error message. |
| 149 | +
|
| 150 | +Speak into your microphone when prompted. What you speak should be output as text: |
| 151 | +
|
| 152 | +```console |
| 153 | +Say something ... |
| 154 | +RECOGNIZED: Text=Go ahead and delete the e-mail. |
| 155 | + Intent Id: Delete. |
| 156 | + Language Understanding JSON: |
| 157 | +``` |
| 158 | + |
| 159 | +> [NOTE] |
| 160 | +> There is a known issue with the LanguageUnderstandingServiceResponse_JsonResult property when used with CLU in the Speech SDK version 1.25. You can get detailed JSON output in a future release. Via JSON, the intents are returned in the probability order of most likely to least likely. For example, the `topIntent` might be `Delete` with a confidence score of 0.95413816 (95.41%). The second most likely intent might be `Cancel` with a confidence score of 0.8985081 (89.85%). |
| 161 | +
|
| 162 | +## Remarks |
| 163 | +Now that you've completed the quickstart, here are some additional considerations: |
| 164 | + |
| 165 | +- This example uses the `RecognizeOnceAsync` operation to transcribe utterances of up to 30 seconds, or until silence is detected. For information about continuous recognition for longer audio, including multi-lingual conversations, see [How to recognize speech](~/articles/cognitive-services/speech-service/how-to-recognize-speech.md). |
| 166 | +- To recognize speech from an audio file, use `FromWavFileInput` instead of `FromDefaultMicrophoneInput`: |
| 167 | + ```cpp |
| 168 | + auto audioInput = AudioConfig::FromWavFileInput("YourAudioFile.wav"); |
| 169 | + ``` |
| 170 | +- For compressed audio files such as MP4, install GStreamer and use `PullAudioInputStream` or `PushAudioInputStream`. For more information, see [How to use compressed input audio](~/articles/cognitive-services/speech-service/how-to-use-codec-compressed-audio-input-streams.md). |
| 171 | + |
| 172 | +## Clean up resources |
| 173 | + |
| 174 | +[!INCLUDE [Delete resource](../../common/delete-resource-clu.md)] |
| 175 | + |
0 commit comments