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

語音辨識語音合成也提供快速入門。Quickstarts are also available for speech recognition and speech synthesis.

在此快速入門中,您將會建立 C++ 應用程式以從電腦的麥克風即時擷取使用者語音,翻譯該語音,然後將翻譯的文字轉譯到命令列。In this quickstart, you'll create a C++ application that captures user speech from your computer's microphone, translates the speech, and transcribes the translated text to the command line in real time. 應用程式是利用語音 SDK NuGet 套件 (英文) 與 Microsoft Visual Studio 2019 (任何版本) 所建置的。This application is built with the Speech SDK NuGet package and Microsoft Visual Studio 2019 (any edition).

如需可供進行語音翻譯的完整語言清單,請參閱語言支援For a complete list of languages available for speech translation, see language support.

必要條件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

若要建立 C++ 桌面開發的 Visual Studio 專案,您必須設定 Visual Studio 的開發選項、建立專案、選取目標架構,然後安裝語音 SDK。To create a Visual Studio project for C++ desktop development, you need to set up Visual Studio development options, create the project, select the target architecture, and install the Speech SDK.

設定 Visual Studio 開發選項Set up Visual Studio development options

若要開始,請確定您在 Visual Studio 中已正確地設定,以進行 C++桌面開發:To start, make sure you're set up correctly in Visual Studio for C++ desktop development:

  1. 開啟 Visual Studio 2019 以顯示 [開始] 視窗。Open Visual Studio 2019 to display the Start window.

    [開始] 視窗 - Visual Studio

  2. 選取 [不使用程式碼繼續] ,以移至 Visual Studio IDE。Select Continue without code to go to the Visual Studio IDE.

  3. 從 [Visual Studio] 功能表列中,選取 [工具] > [取得工具和功能] 以開啟 Visual Studio 安裝程式並檢視 [修改] 對話方塊。From the Visual Studio menu bar, select Tools > Get Tools and Features to open Visual Studio Installer and view the Modifying dialog box.

    [工作負載] 索引標籤、[修改] 對話方塊、Visual Studio 安裝程式

  4. 在 [工作負載] 索引標籤的 [Windows] 底下,尋找 [使用 C++ 的桌面開發] 工作負載。In the Workloads tab, under Windows, find the Desktop development with C++ workload. 如果尚未選取該工作負載旁的核取方塊,請加以選取。If the check box next to that workload isn't already selected, select it.

  5. 在 [個別元件] 索引標籤中,尋找 [Nuget 套件管理員] 核取方塊。In the Individual components tab, find the Nuget package manager check box. 如果尚未選取核取方塊,請加以選取。If the check box isn't already selected, select it.

  6. 選取角落標示為 [關閉] 或 [修改] 的按鈕。Select the button in the corner labeled either Close or Modify. (按鈕名稱會根據您是否選取了任何安裝功能而有所不同。)如果您選取 [修改] ,則會開始安裝,這可能需要一些時間。(The button name varies depending on whether you selected any features for installation.) If you select Modify, installation begins, which may take a while.

  7. 關閉 Visual Studio 安裝程式。Close Visual Studio Installer.

建立專案並選取目標架構Create the project and select the target architecture

接下來,建立您的專案:Next, create your project:

  1. 在 [Visual Studio] 功能表列中,選擇 [檔案] > [新增] > [專案] 以顯示 [建立新專案] 視窗。In the Visual Studio menu bar, choose File > New > Project to display the Create a new project window.

    建立新專案,C++ - Visual Studio

  2. 尋找並選取 [容器應用程式] 。Find and select Console App. 請確定您選取的是此專案類型的 C++ 版本 (而不是 C# 或 Visual Basic)。Make sure that you select the C++ version of this project type (as opposed to C# or Visual Basic).

  3. 選取 [下一步] 以顯示 [設定您的新專案] 畫面。Select Next to display the Configure your new project screen.

    設定您的新專案,C++ - Visual Studio

  4. 在 [專案名稱] 中,輸入 helloworldIn Project name, enter helloworld.

  5. 在 [位置] 中,瀏覽至用來儲存專案的資料夾,然後選取或建立。In Location, navigate to and select or create the folder to save your project in.

現在,請選取您的目標平台架構。Now select your target platform architecture. 在 [Visual Studio] 工具列中, 尋找 [方案平台] 下拉式方塊。In the Visual Studio toolbar, find the Solution Platforms drop-down box. (如果您沒有看到它,請選擇 [檢視] > [工具列] > [標準] 以顯示包含 [方案平台] 的工具列。)如果您執行的是 64 位元 Windows,請在下拉式方塊中選擇 [x64] 。(If you don't see it, choose View > Toolbars > Standard to display the toolbar containing Solution Platforms.) If you're running 64-bit Windows, choose x64 in the drop-down box. 64 位元 Windows 也可執行 32 位元應用程式,因此您可以依需求選擇 [x86] 。64-bit Windows can also run 32-bit applications, so you can choose x86 if you prefer.

安裝語音 SDKInstall the Speech SDK

最後,安裝語音 SDK NuGet 套件 (英文),並在您的專案中參考語音 SDK:Finally, install the Speech SDK NuGet package, and reference the Speech SDK in your project:

  1. 在 [方案總管] 中,以滑鼠右鍵按一下解決方案,然後選擇 [管理解決方案的 NuGet 套件] 以移至 [NuGet - 解決方案] 視窗。In Solution Explorer, right-click your solution, and choose Manage NuGet Packages for Solution to go to the Nuget - Solution window.

  2. 選取 [瀏覽] 。Select Browse.

    [NuGet - 解決方案] 索引標籤,Visual Studio

  3. 在 [套件來源] 中,選擇 [nuget.org] 。In Package source, choose nuget.org.

  4. 在 [搜尋] 方塊中,輸入 Microsoft.CognitiveServices.Speech,然後在搜尋結果中出現該套件後加以選擇。In the Search box, enter Microsoft.CognitiveServices.Speech, and then choose that package after it appears in the search results.

    Microsoft.CognitiveServices.Speech C++ 套件安裝 - Visual Studio

  5. 在搜尋結果旁的 [套件狀態] 窗格中,選取您的 helloworld 專案。In the package status pane next to the search results, select your helloworld project.

  6. 選取 [安裝] 。Select Install.

  7. 在 [預覽變更] 對話方塊中,選取 [確定] 。In the Preview Changes dialog box, select OK.

  8. 在 [接受授權] 對話方塊中,檢視授權,然後選取 [我接受] 。In the License Acceptance dialog box, view the license, and then select I Accept. 套件安裝隨即開始,而且當安裝完成時,[輸出] 窗格會顯示類似下列文字的訊息:Successfully installed 'Microsoft.CognitiveServices.Speech 1.6.0' to helloworldThe package installation begins, and when installation is complete, the Output pane displays a message similar to the following text: Successfully installed 'Microsoft.CognitiveServices.Speech 1.6.0' to helloworld.

新增範例程式碼Add sample code

  1. 開啟來源檔案 helloworld.cpp 。Open the source file helloworld.cpp.

  2. 使用下列程式碼片段取代所有程式碼:Replace all the code with the following snippet:

    #include "pch.h"
    #include <iostream>
    #include <vector>
    #include <speechapi_cxx.h>
    
    using namespace std;
    using namespace Microsoft::CognitiveServices::Speech;
    using namespace Microsoft::CognitiveServices::Speech::Translation;
    
    // Translation with microphone input.
    void TranslationWithMicrophone()
    {
        // Creates an instance of a speech translation config with specified subscription key and service region.
        // Replace with your own subscription key and service region (e.g., "westus").
        auto config = SpeechTranslationConfig::FromSubscription("YourSubscriptionKey", "YourServiceRegion");
    
        // Sets source and target languages
        // Replace with the languages of your choice.
        auto fromLanguage = "en-US";
        config->SetSpeechRecognitionLanguage(fromLanguage);
        config->AddTargetLanguage("de");
        config->AddTargetLanguage("fr");
    
        // Creates a translation recognizer using microphone as audio input.
        auto recognizer = TranslationRecognizer::FromConfig(config);
        cout << "Say something...\n";
    
        // Starts translation, 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 recognized text as well as the translation.
        // 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::TranslatedSpeech)
        {
            cout << "RECOGNIZED: Text=" << result->Text << std::endl
            << "    Language=" << fromLanguage << std::endl;
    
            for (const auto& it : result->Translations)
            {
                cout << "TRANSLATED into '" << it.first.c_str() << "': " << it.second.c_str() << std::endl;
            }
        }
        else if (result->Reason == ResultReason::RecognizedSpeech)
        {
            cout << "RECOGNIZED: Text=" << result->Text << " (text could not be translated)" << 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()
    {
        TranslationWithMicrophone();
        cout << "Please press a key to continue.\n";
        cin.get();
        return 0;
    }
    
  3. 在相同檔案中,以您的訂用帳戶金鑰取代 YourSubscriptionKey 字串。In the same file, replace the string YourSubscriptionKey with your subscription key.

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

  5. 從功能表列中,選擇 [檔案] > [全部儲存] 。From the menu bar, choose File > Save All.

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

  1. 從功能表列中,選取 [建置] > [建置方案] 來建置應用程式。From the menu bar, select Build > Build Solution to build the application. 現在會編譯程式碼,而且不會出現任何錯誤。The code should compile without errors now.

  2. 選擇 [偵錯] > [開始偵錯] (或按 F5) 以啟動 helloworld 應用程式。Choose Debug > Start Debugging (or press F5) to start the helloworld application.

  3. 請說英文片語或句子。Speak an English phrase or sentence. 應用程式會將您的語音傳送到語音服務,語音服務會進行翻譯並轉換為文字 (在此案例中,為法文轉換為德文)。The application transmits your speech to the Speech Services, which translates and transcribes to text (in this case, to French and German). 語音服務接著會將文字傳送回應用程式以供顯示。The Speech Services then sends the text back to the application for display.

    語音翻譯成功後的主控台輸出

後續步驟Next steps

在 GitHub 上可取得其他範例,例如如何讀取音訊檔案中的語音,或者將翻譯的文字轉換為合成語音。Additional samples, such as how to read speech from an audio file or turn translated text into synthesized speech, are available on GitHub.

另請參閱See also