Azure Cognitive Services – Speech Service (C++)

MSDN Docs

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

Setup

  1. Open a terminal and clone the repo:
    git clone https://github.com/Azure-Samples/cognitive-services-speech-sdk.git
  2. Navigate to the C++ sample directory:
    cd cognitive-services-speech-sdk/samples/cpp/windows/console
  3. Open SpeechRecognizeSample.sln in Visual Studio.
  4. Install the Speech SDK NuGet package:
    Install-Package Microsoft.CognitiveServices.Speech
  5. Replace the placeholder YOUR_SUBSCRIPTION_KEY and YOUR_SERVICE_REGION with your actual values in main.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