Skip to content

Latest commit

 

History

History

api

Speech Samples.

These samples demonstrate how to call the Google Cloud Speech API using C++.

We only test these samples on Linux. If you are running Windows and macOS please see the additional notes for your platform.

Build and Run

  1. Create a project in the Google Cloud Platform Console. If you haven't already created a project, create one now. Projects enable you to manage all Google Cloud Platform resources for your app, including deployment, access control, billing, and services.

    1. Open the Cloud Platform Console.
    2. In the drop-down menu at the top, select Create a project.
    3. Give your project a name.
    4. Make a note of the project ID, which might be different from the project name. The project ID is used in commands and in configurations.
  2. Enable billing for your project. If you haven't already enabled billing for your project, enable billing now. Enabling billing allows the application to consume billable resources such as Speech API calls. See Cloud Platform Console Help for more information about billing settings.

  3. Enable APIs for your project. Click here to visit Cloud Platform Console and enable the Speech API via the UI.

    Or use the CLI:

    gcloud services enable speech.googleapis.com
    
  4. If needed, override the Billing Project. If you are using a user account for authentication, you need to set the GOOGLE_CLOUD_CPP_USER_PROJECT environment variable to the project you created in the previous step. Be aware that you must have serviceusage.services.use permission on the project. Alternatively, use a service account as described next.

  1. Download service account credentials. These samples can use service accounts for authentication.

    1. Visit the Cloud Console, and navigate to: API Manager > Credentials > Create credentials > Service account key
    2. Under Service account, select New service account.
    3. Under Service account name, enter a service account name of your choosing. For example, transcriber.
    4. Under Role, select Project > Owner.
    5. Under Key type, leave JSON selected.
    6. Click Create to create a new service account, and download the json credentials file.
    7. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your downloaded service account credentials:
      export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json
      

    See the Cloud Platform Auth Guide for more information.

  2. Install vcpkg. This project uses vcpkg for dependency management. Clone the vcpkg repository to your preferred location. In these instructions we use$HOME:

    git clone -C $HOME https://github.com/microsoft/vcpkg.git
  3. Download or clone this repo with

    git clone https://github.com/GoogleCloudPlatform/cpp-samples
  4. Compile these examples: Use the vcpkg toolchain file to download and compile dependencies. This file would be in the directory you cloned vcpkg into, $HOME/vcpkg if you are following the instructions to the letter. Note that building all the dependencies can take up to an hour, depending on the performance of your workstation. These dependencies are cached, so a second build should be substantially faster.

    cd cpp-samples/speech/api
    cmake -S. -B.build -DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake
    cmake --build .build
  5. Run the examples:

    .build/transcribe --bitrate 16000 resources/audio2.raw
    .build/transcribe resources/audio.flac
    .build/transcribe resources/quit.raw
    .build/streaming_transcribe --bitrate 16000 resources/audio2.raw
    .build/streaming_transcribe resources/audio.flac
    .build/streaming_transcribe resources/quit.raw
    .build/streaming_transcribe_coroutines --bitrate 16000 resources/audio2.raw
    .build/streaming_transcribe_coroutines resources/audio.flac
    .build/streaming_transcribe_coroutines resources/quit.raw
    .build/streaming_transcribe_singlethread --bitrate 16000 resources/audio.raw
    .build/transcribe gs://cloud-samples-tests/speech/brooklyn.flac
    .build/async_transcribe gs://cloud-samples-tests/speech/vr.flac

Platform Specific Notes

macOS

gRPC requires an environment variable to configure the trust store for SSL certificates, you can download and configure this using:

curl -Lo roots.pem https://pki.google.com/roots.pem
export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH="$PWD/roots.pem"

Windows

gRPC requires an environment variable to configure the trust store for SSL certificates, you can download and configure this using:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command ^
    (new-object System.Net.WebClient).Downloadfile( ^
        'https://pki.google.com/roots.pem', 'roots.pem')
set GRPC_DEFAULT_SSL_ROOTS_FILE_PATH=%cd%\roots.pem