快速入門:使用語音 SDK 在 Linux 上以 C++ 合成語音Quickstart: Synthesize speech in C++ on Linux by using the Speech SDK

另備有語音辨識的快速入門。Quickstarts are also available for speech-recognition.

在本文中,您會建立適用於 Linux (Ubuntu 16.04、Ubuntu 18.04、Debian 9) 的 C++ 主控台應用程式。In this article, you create a C++ console application for Linux (Ubuntu 16.04, Ubuntu 18.04, Debian 9). 您將使用認知服務語音 SDK 即時從文字合成語音,並以電腦的喇叭播放語音。You use the Cognitive Services Speech SDK to synthesize speech from text in real time and play the speech on your PC's speaker. 此應用程式是使用適用於 Linux 的語音 SDK 和 Linux 散發套件的 C++ 編譯器 (例如 g++) 所建置。The application is built with the Speech SDK for Linux and your Linux distribution's C++ compiler (for example, g++).

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

安裝語音 SDKInstall Speech SDK

重要

下載此頁面上的任何「Azure 認知服務的語音 SDK」元件,即表示您知悉其授權。By downloading any of the Speech SDK for Azure Cognitive Services components on this page, you acknowledge its license. 請參閱語音 SDK 的 Microsoft 軟體授權條款See the Microsoft Software License Terms for the Speech SDK.

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

適用於 Linux 的語音 SDK 可用來建置 64 位元和 32 位元應用程式。The Speech SDK for Linux can be used to build both 64-bit and 32-bit applications. 您可從 https://aka.ms/csspeech/linuxbinary 以 tar 檔案格式下載必要的程式庫和標頭檔。The required libraries and header files can be downloaded as a tar file from https://aka.ms/csspeech/linuxbinary.

下載並安裝 SDK,如下所示:Download and install the SDK as follows:

  1. 請確定已安裝 SDK 的相依性。Make sure the SDK's dependencies are installed.

    • 在 Ubuntu 上:On Ubuntu:

      sudo apt-get update
      sudo apt-get install build-essential libssl1.0.0 libasound2 wget
      
    • 在 Debian 9 上:On Debian 9:

      sudo apt-get update
      sudo apt-get install build-essential libssl1.0.2 libasound2 wget
      
  2. 選擇語音 SDK 檔案應解壓縮至的目錄,並將 SPEECHSDK_ROOT 環境變數設定為指向該目錄。Choose a directory to which the Speech SDK files should be extracted, and set the SPEECHSDK_ROOT environment variable to point to that directory. 此變數方便在未來命令中參考此目錄。This variable makes it easy to refer to the directory in future commands. 例如,如果您想要在主目錄中使用目錄 speechsdk,請使用類似下列的命令:For example, if you want to use the directory speechsdk in your home directory, use a command like the following:

    export SPEECHSDK_ROOT="$HOME/speechsdk"
    
  3. 在該目錄不存在的情況下建立它。Create the directory if it doesn't exist yet.

    mkdir -p "$SPEECHSDK_ROOT"
    
  4. 下載並解壓縮包含語音 SDK 二進位檔的 .tar.gz 封存:Download and extract the .tar.gz archive containing the Speech SDK binaries:

    wget -O SpeechSDK-Linux.tar.gz https://aka.ms/csspeech/linuxbinary
    tar --strip 1 -xzf SpeechSDK-Linux.tar.gz -C "$SPEECHSDK_ROOT"
    
  5. 驗證已壓縮套件之最上層目錄的內容:Validate the contents of the top-level directory of the extracted package:

    ls -l "$SPEECHSDK_ROOT"
    

    目錄清單應包含第三方聲明和授權檔案,以及包含標頭 (.h) 檔案的 include 目錄和包含程式庫的 lib 目錄。The directory listing should contain the third-party notice and license files, as well as an include directory containing header (.h) files and a lib directory containing libraries.

    PathPath 說明Description
    license.md 授權License
    ThirdPartyNotices.md 第三方聲明。Third-party notices.
    REDIST.txt 轉散發注意事項。Redistribution notice.
    include C 和 C++ 的必要標頭檔The required header files for C and C++
    lib/x64 要連結您的應用程式所需的 x64 原生程式庫Native library for x64 required to link your application
    lib/x86 要連結您的應用程式所需的 x86 原生程式庫Native library for x86 required to link your application

新增範例程式碼Add sample code

  1. 建立名為 helloworld.cpp 的 C++ 原始程式檔,然後在其中貼上下列程式碼。Create a C++ source file named helloworld.cpp, and paste the following code into it.

    #include <iostream> // cin, cout
    #include <speechapi_cxx.h>
    
    using namespace std;
    using namespace Microsoft::CognitiveServices::Speech;
    
    void synthesizeSpeech()
    {
        // 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 synthesizer using the default speaker as audio output. The default spoken language is "en-us".
        auto synthesizer = SpeechSynthesizer::FromConfig(config);
    
        // Receive a text from console input and synthesize it to speaker.
        cout << "Type some text that you want to speak..." << std::endl;
        cout << "> ";
        std::string text;
        getline(cin, text);
    
        auto result = synthesizer->SpeakTextAsync(text).get();
    
        // Checks result.
        if (result->Reason == ResultReason::SynthesizingAudioCompleted)
        {
            cout << "Speech synthesized to speaker for text [" << text << "]" << std::endl;
        }
        else if (result->Reason == ResultReason::Canceled)
        {
            auto cancellation = SpeechSynthesisCancellationDetails::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;
            }
        }
    
        // This is to give some time for the speaker to finish playing back the audio
        cout << "Press enter to exit..." << std::endl;
        cin.get();
    }
    
    int main(int argc, char **argv) {
        setlocale(LC_ALL, "");
        synthesizeSpeech();
        return 0;
    }
    
  2. 在這個新檔案中,以您的語音服務訂用帳戶金鑰取代 YourSubscriptionKey 字串。In this new file, replace the string YourSubscriptionKey with your Speech Services subscription key.

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

建置應用程式Build the app

注意

請務必以「單一命令行」 的形式輸入下面的命令。Make sure to enter the commands below as a single command line. 若要這麼做,最簡單的方式就是使用每個命令旁邊的 [複製] 按鈕來複製命令,然後將它貼在殼層提示字元。The easiest way to do that is to copy the command by using the Copy button next to each command, and then paste it at your shell prompt.

  • x64 (64 位元) 系統上,執行下列命令以建置應用程式。On an x64 (64-bit) system, run the following command to build the application.

    g++ helloworld.cpp -o helloworld -I "$SPEECHSDK_ROOT/include/cxx_api" -I "$SPEECHSDK_ROOT/include/c_api" --std=c++14 -lpthread -lMicrosoft.CognitiveServices.Speech.core -L "$SPEECHSDK_ROOT/lib/x64" -l:libasound.so.2
    
  • x86 (32 位元) 系統上,執行下列命令以建置應用程式。On an x86 (32-bit) system, run the following command to build the application.

    g++ helloworld.cpp -o helloworld -I "$SPEECHSDK_ROOT/include/cxx_api" -I "$SPEECHSDK_ROOT/include/c_api" --std=c++14 -lpthread -lMicrosoft.CognitiveServices.Speech.core -L "$SPEECHSDK_ROOT/lib/x86" -l:libasound.so.2
    

執行應用程式Run the app

  1. 將載入程式的程式庫路徑設定為指向語音 SDK 程式庫。Configure the loader's library path to point to the Speech SDK library.

    • x64 (64 位元) 系統上,輸入下列命令。On an x64 (64-bit) system, enter the following command.

      export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$SPEECHSDK_ROOT/lib/x64"
      
    • x86 (32 位元) 系統上,輸入此命令。On an x86 (32-bit) system, enter this command.

      export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$SPEECHSDK_ROOT/lib/x86"
      
  2. 執行應用程式。Run the application.

    ./helloworld
    
  3. 主控台視窗會出現提示,要求您輸入某些文字。In the console window, a prompt appears, prompting you to type some text. 請輸入幾個字或句子。Type a few words or a sentence. 您輸入的文字會傳送至語音服務並合成為語音,然後以喇叭播放。The text that you typed is transmitted to the Speech Services and synthesized to speech, which plays on your speaker.

    Type some text that you want to speak...
    > hello
    Speech synthesized to speaker for text [hello]
    Press enter to exit...
    

後續步驟Next steps

另請參閱See also