快速入門:使用語音 SDK 在 Windows 上以 C++ 辨識語音Quickstart: Recognize speech in C++ on Windows by using the Speech SDK

另備有文字轉換語音語音翻譯的快速入門。Quickstarts are also available for text-to-speech and speech-translation.

如有需要,請選擇不同的程式設計語言和/或環境:If desired, choose a different programming language and/or environment:

在本文中,您會建立適用於 Windows 的 C++ 主控台應用程式。In this article, you create a C++ console application for Windows. 您將使用認知服務語音 SDK,即時從電腦的麥克風將語音轉譯為文字。You use the Cognitive Services Speech SDK to transcribe speech to text in real time from your PC's microphone. 應用程式是利用語音 SDK NuGet 套件與 Microsoft Visual Studio 2017 (任何版本) 所建置的。The application is built with the Speech SDK NuGet package and Microsoft Visual Studio 2017 (any edition).

必要條件Prerequisites

您需要語音服務訂用帳戶金鑰,才能完成本快速入門。You need a Speech Services subscription key to complete this Quickstart. 您可以免費取得一個金鑰。You can get one for free. 如需詳細資訊,請參閱免費試用語音服務See Try the Speech Services for free for details.

建立 Visual Studio 專案Create a Visual Studio project

  1. 啟動 Visual Studio 2019。Start Visual Studio 2019.

  2. 請確定使用 C++ 進行桌面開發工作負載可供使用。Make sure the Desktop development with C++ workload is available. 從 Visual Studio 功能表列選擇 [工具] > [取得工具和功能] ,以開啟 Visual Studio 安裝程式。Choose Tools > Get Tools and Features from the Visual Studio menu bar to open the Visual Studio installer. 如果已啟用此工作負載,請跳至下一個步驟。If this workload is already enabled, skip to the next step.

    Visual Studio 工作負載索引標籤的螢幕擷取畫面

    否則,請核取 [使用 C++ 進行桌面開發] 旁邊的方塊。Otherwise, check the box next to Desktop development with C++.

  3. 請確定 NuGet 套件管理員元件可用。Make sure the NuGet package manager component is available. 切換至 Visual Studio 安裝程式對話方塊的 [個別元件] 索引標籤,如果 [NuGet 套件管理員] 尚未啟用,則加以選取。Switch to the Individual components tab of the Visual Studio installer dialog box, and select NuGet package manager if it is not already enabled.

    Visual Studio 個別元件索引標籤的螢幕擷取畫面

  4. 如果您需要啟用 C++ 工作負載或 NuGet,請選取 [修改] (在對話方塊的右下角)。If you needed to enable either the C++ workload or NuGet, select Modify (at the lower right corner of the dialog box). 安裝新功能需要一些時間。Installation of the new features takes a moment. 如果這兩項功能都已經啟用,則關閉對話方塊。If both features were already enabled, close the dialog box instead.

  5. 建立新 Visual C++ Windows 桌面的 Windows 主控台應用程式。Create a new Visual C++ Windows Desktop Windows Console Application. 首先,從功能表選擇 [檔案] > [新增] > [專案] 。First, choose File > New > Project from the menu. 在 [新增專案] 對話方塊中,展開左窗格中的 [已安裝] > [Visual C++] > [Windows 桌面] 。In the New Project dialog box, expand Installed > Visual C++ > Windows Desktop in the left pane. 然後選取 [ Windows 主控台應用程式] 。Then select Windows Console Application. 針對專案名稱,請輸入 helloworldFor the project name, enter helloworld.

    [新增專案] 對話方塊的螢幕擷取畫面

  6. 如果您執行 64 位元的 Windows,可以使用 Visual Studio 工具列中的下拉式功能表,將您的建置平台切換為 x64If you're running 64-bit Windows, you may switch your build platform to x64 by using the drop-down menu in the Visual Studio toolbar. (64 位元版本的 Windows 可以執行 32 位元應用程式,因此您不一定需要進行此動作。)(64-bit versions of Windows can run 32-bit applications, so this is not a requirement.)

    Visual Studio 工具列的螢幕擷取畫面,其中 x64 選項已醒目提示

  7. 在 [方案總管] 中,以滑鼠右鍵按一下解決方案,然後選擇 [管理解決方案的 NuGet 套件] 。In Solution Explorer, right-click the solution and choose Manage NuGet Packages for Solution.

    方案總管的螢幕擷取畫面,其中 [管理解決方案的 NuGet 套件] 選項已醒目提示

  8. 在右上角的 [套件來源] 欄位中,選取 [nuget.org] 。搜尋 Microsoft.CognitiveServices.Speech 套件,然後將它安裝到 helloworld 專案。In the upper-right corner, in the Package Source field, select nuget.org. Search for the Microsoft.CognitiveServices.Speech package, and install it into the helloworld project.

    管理解決方案套件對話方塊的螢幕擷取畫面

    注意

    認知服務語音 SDK 目前的版本為 1.5.0The current version of the Cognitive Services Speech SDK is 1.5.0.

  9. 接受顯示的授權,才會開始安裝 NuGet 套件。Accept the displayed license to begin installation of the NuGet package.

    接受授權對話方塊的螢幕擷取畫面

安裝套件之後,[套件管理員] 主控台上會出現確認訊息。After the package is installed, a confirmation appears in the Package Manager console.

新增範例程式碼Add sample code

  1. 開啟來源檔案 helloworld.cpp 。Open the source file helloworld.cpp. 使用下列項目取代開頭 include 陳述式 (#include "stdafx.h"#include "pch.h") 下方的所有程式碼:Replace all the code below the initial include statement (#include "stdafx.h" or #include "pch.h") with the following:

    #include <iostream>
    #include <speechapi_cxx.h>
    
    using namespace std;
    using namespace Microsoft::CognitiveServices::Speech;
    
    void recognizeSpeech()
    {
        // Creates an instance of a speech config with specified subscription key and service region.
        // Replace with your own subscription key and service region (e.g., "westus").
        auto config = SpeechConfig::FromSubscription("YourSubscriptionKey", "YourServiceRegion");
    
        // Creates a speech recognizer.
        auto recognizer = SpeechRecognizer::FromConfig(config);
        cout << "Say something...\n";
    
        // Starts speech recognition, and returns after a single utterance is recognized. The end of a
        // single utterance is determined by listening for silence at the end or until a maximum of 15
        // seconds of audio is processed.  The task returns the recognition text as result. 
        // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
        // shot recognition like command or query. 
        // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
        auto result = recognizer->RecognizeOnceAsync().get();
    
        // Checks result.
        if (result->Reason == ResultReason::RecognizedSpeech)
        {
            cout << "We recognized: " << result->Text << std::endl;
        }
        else if (result->Reason == ResultReason::NoMatch)
        {
            cout << "NOMATCH: Speech could not be recognized." << std::endl;
        }
        else if (result->Reason == ResultReason::Canceled)
        {
            auto cancellation = CancellationDetails::FromResult(result);
            cout << "CANCELED: Reason=" << (int)cancellation->Reason << std::endl;
    
            if (cancellation->Reason == CancellationReason::Error) 
            {
                cout << "CANCELED: ErrorCode= " << (int)cancellation->ErrorCode << std::endl;
                cout << "CANCELED: ErrorDetails=" << cancellation->ErrorDetails << std::endl;
                cout << "CANCELED: Did you update the subscription info?" << std::endl;
            }
        }
    }
    
    int wmain()
    {
        recognizeSpeech();
        cout << "Please press a key to continue.\n";
        cin.get();
        return 0;
    }
    
  2. 在相同檔案中,以您的訂用帳戶金鑰取代 YourSubscriptionKey 字串。In the same file, replace the string YourSubscriptionKey with your subscription key.

  3. 以與您的訂用帳戶 (例如,免費試用訂用帳戶的 westus) 相關聯的區域取代 YourServiceRegion 字串。Replace the string YourServiceRegion with the region associated with your subscription (for example, westus for the free trial subscription).

  4. 儲存專案的變更。Save changes to the project.

建置並執行應用程式Build and run the app

  1. 建置應用程式。Build the application. 從功能表列中,選擇 [建置] > [建置解決方案] 。From the menu bar, choose Build > Build Solution. 這應該會編譯程式碼,而且不會出現任何錯誤。The code should compile without errors.

    Visual Studio 應用程式的螢幕擷取畫面,其中 [建置解決方案] 選項已醒目提示

  2. 啟動應用程式。Start the application. 從功能表列中,選擇 [偵錯] > [開始偵錯] ,或按 F5From the menu bar, choose Debug > Start Debugging, or press F5.

    Visual Studio 應用程式的螢幕擷取畫面,其中 [開始偵錯] 選項已醒目提示

  3. 一個主控台視窗隨即顯示,提示您說話。A console window appears, prompting you to say something. 請說英文片語或句子。Speak an English phrase or sentence. 您的語音會傳送到語音服務,並且轉譯為文字,該文字會出現在相同視窗中。Your speech is transmitted to the Speech Services and transcribed to text, which appears in the same window.

    成功辨識後主控台輸出的螢幕擷取畫面

後續步驟Next steps

在 GitHub 上可取得其他範例,例如如何讀取音訊檔案中的語音。Additional samples, such as how to read speech from an audio file, are available on GitHub.

另請參閱See also