Overview
This quickstart demonstrates how to recognize speech from the microphone using the Azure Speech SDK for C++. It walks you through creating a simple console application that converts spoken words to text.
Prerequisites
- Visual Studio 2019 (or later) with Desktop development with C++ workloads.
- Azure subscription – create a Speech resource and note the Key and Region.
- Git installed (optional, for cloning the sample).
Setup
- Open a terminal and clone the repo:
git clone https://github.com/Azure-Samples/cognitive-services-speech-sdk.git
- Navigate to the C++ sample directory:
cd cognitive-services-speech-sdk/samples/cpp/windows/console
- Open
SpeechRecognizeSample.sln
in Visual Studio. - Install the Speech SDK NuGet package:
Install-Package Microsoft.CognitiveServices.Speech
- Replace the placeholder
YOUR_SUBSCRIPTION_KEY
andYOUR_SERVICE_REGION
with your actual values inmain.cpp
.
Sample Code
#include <speechapi_cxx.h>
#include <iostream>
using namespace Microsoft::CognitiveServices::Speech;
using namespace std;
int main()
{
// Replace with your subscription key and service region (e.g., "westus").
auto config = SpeechConfig::FromSubscription("YOUR_SUBSCRIPTION_KEY", "YOUR_SERVICE_REGION");
config->SetSpeechRecognitionLanguage("en-US");
// Creates a speech recognizer using the default microphone.
auto recognizer = SpeechRecognizer::FromConfig(config);
cout << "Speak into your microphone..." << endl;
// Starts speech recognition, and returns after a single utterance is recognized.
auto result = recognizer->RecognizeOnceAsync().get();
// Checks result.
if (result->Reason == ResultReason::RecognizedSpeech) {
cout << "Recognized: " << result->Text << endl;
} else if (result->Reason == ResultReason::NoMatch) {
cout << "No speech could be recognized." << endl;
} else if (result->Reason == ResultReason::Canceled) {
auto cancellation = CancellationDetails::FromResult(result);
cout << "CANCELED: Reason=" << (int)cancellation->Reason << endl;
cout << "ErrorDetails: " << cancellation->ErrorDetails << endl;
}
return 0;
}
Run it
Press F5 in Visual Studio to build and run the console app. Speak clearly into your microphone; the recognized text will appear in the console window.
Troubleshooting
- Microphone not found: Make sure your system’s default recording device is set correctly.
- Authentication error: Double‑check the subscription key and region; ensure the Speech resource is in the same Azure region.
- Network issues: Verify outbound HTTPS traffic on port 443 is allowed.