Text-to-Speech-Clientbibliotheken

Auf dieser Seite werden die ersten Schritte mit den Cloud-Clientbibliotheken für die Text-to-Speech API beschrieben. Clientbibliotheken erleichtern den Zugriff auf Google Cloud APIs mit einer unterstützten Sprache. Sie können Google Cloud APIs direkt verwenden, indem Sie Rohanfragen an den Server senden. Clientbibliotheken bieten jedoch Vereinfachungen, die den zu schreibenden Code erheblich reduzieren.

Weitere Informationen zu den Cloud-Clientbibliotheken und den älteren Google API-Clientbibliotheken finden Sie unter Erläuterung zu Clientbibliotheken.

Clientbibliothek installieren

C++

Weitere Informationen zu den Anforderungen und Abhängigkeiten der Clientbibliothek finden Sie unter C++ Entwicklungsumgebung einrichten.

C#

Wenn Sie Visual Studio 2017 oder höher verwenden, öffnen Sie das Fenster „nuget package manager” und geben Sie Folgendes ein:

Install-Package Google.Apis

Wenn Sie die .NET Core-Befehlszeilentools zum Installieren der Abhängigkeiten verwenden, führen Sie den folgenden Befehl aus:

dotnet add package Google.Apis

Weitere Informationen finden Sie unter .NET-Entwicklungsumgebung einrichten.

Go

go get cloud.google.com/go/texttospeech/apiv1

Weitere Informationen finden Sie unter Go-Entwicklungsumgebung einrichten.

Java

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.39.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-texttospeech</artifactId>
  </dependency>

If you are using Gradle, add the following to your dependencies:

implementation 'com.google.cloud:google-cloud-texttospeech:2.44.0'

If you are using sbt, add the following to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-texttospeech" % "2.44.0"

If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

Weitere Informationen finden Sie unter Java-Entwicklungsumgebung einrichten.

Node.js

npm install --save @google-cloud/text-to-speech

Weitere Informationen finden Sie unter Node.js-Entwicklungsumgebung einrichten.

PHP

composer require google/apiclient

Weitere Informationen finden Sie unter PHP auf Google Cloud verwenden.

Python

pip install --upgrade google-cloud-texttospeech

Weitere Informationen finden Sie unter Python-Entwicklungsumgebung einrichten.

Ruby

gem install google-api-client

Weitere Informationen finden Sie unter Ruby-Entwicklungsumgebung einrichten.

Authentifizierung einrichten

Zur Authentifizierung von Aufrufen an Google Cloud APIs unterstützen Clientbibliotheken Standardanmeldedaten für Anwendungen (Application Default Credentials, ADC). Die Bibliotheken suchen nach Anmeldedaten an einer Reihe von definierten Standorten und verwenden diese Anmeldedaten für Authentifizierungsanfragen an die API. Mit ADC können Sie Anmeldedaten für Ihre Anwendung in verschiedenen Umgebungen bereitstellen, z. B. in der lokalen Entwicklung oder Produktion, ohne den Anwendungscode ändern zu müssen.

In Produktionsumgebungen hängt die Art der Einrichtung von ADC vom Dienst und Kontext ab. Weitere Informationen finden Sie unter Standardanmeldedaten für Anwendungen einrichten.

Für eine lokale Entwicklungsumgebung können Sie ADC mit den Anmeldedaten einrichten, die Ihrem Google-Konto zugeordnet sind:

  1. Installieren und initialisieren Sie die gcloud CLI.

    Achten Sie bei der Initialisierung der gcloud CLI auf ein Google Cloud-Projekt, in dem Sie die Berechtigung für den Zugriff auf die Ressourcen haben, die Ihre Anwendung benötigt.

  2. ADC konfigurieren:

    gcloud auth application-default login

    Ein Anmeldebildschirm wird angezeigt. Nach der Anmeldung werden Ihre Anmeldedaten in der lokalen Anmeldedatendatei für ADC gespeichert.

Clientbibliothek verwenden

Das folgende Beispiel zeigt die Verwendung der Clientbibliothek.

C++


#include "google/cloud/texttospeech/v1/text_to_speech_client.h"
#include <iostream>

auto constexpr kText = R"""(
Four score and seven years ago our fathers brought forth on this
continent, a new nation, conceived in Liberty, and dedicated to
the proposition that all men are created equal.)""";

int main(int argc, char* argv[]) try {
  if (argc != 1) {
    std::cerr << "Usage: " << argv[0] << "\n";
    return 1;
  }

  namespace texttospeech = ::google::cloud::texttospeech_v1;
  auto client = texttospeech::TextToSpeechClient(
      texttospeech::MakeTextToSpeechConnection());

  google::cloud::texttospeech::v1::SynthesisInput input;
  input.set_text(kText);
  google::cloud::texttospeech::v1::VoiceSelectionParams voice;
  voice.set_language_code("en-US");
  google::cloud::texttospeech::v1::AudioConfig audio;
  audio.set_audio_encoding(google::cloud::texttospeech::v1::LINEAR16);

  auto response = client.SynthesizeSpeech(input, voice, audio);
  if (!response) throw std::move(response).status();
  // Normally one would play the results (response->audio_content()) over some
  // audio device. For this quickstart, we just print some information.
  auto constexpr kWavHeaderSize = 48;
  auto constexpr kBytesPerSample = 2;  // we asked for LINEAR16
  auto const sample_count =
      (response->audio_content().size() - kWavHeaderSize) / kBytesPerSample;
  std::cout << "The audio has " << sample_count << " samples\n";

  return 0;
} catch (google::cloud::Status const& status) {
  std::cerr << "google::cloud::Status thrown: " << status << "\n";
  return 1;
}

Go


// Command quickstart generates an audio file with the content "Hello, World!".
package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"

	texttospeech "cloud.google.com/go/texttospeech/apiv1"
	"cloud.google.com/go/texttospeech/apiv1/texttospeechpb"
)

func main() {
	// Instantiates a client.
	ctx := context.Background()

	client, err := texttospeech.NewClient(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// Perform the text-to-speech request on the text input with the selected
	// voice parameters and audio file type.
	req := texttospeechpb.SynthesizeSpeechRequest{
		// Set the text input to be synthesized.
		Input: &texttospeechpb.SynthesisInput{
			InputSource: &texttospeechpb.SynthesisInput_Text{Text: "Hello, World!"},
		},
		// Build the voice request, select the language code ("en-US") and the SSML
		// voice gender ("neutral").
		Voice: &texttospeechpb.VoiceSelectionParams{
			LanguageCode: "en-US",
			SsmlGender:   texttospeechpb.SsmlVoiceGender_NEUTRAL,
		},
		// Select the type of audio file you want returned.
		AudioConfig: &texttospeechpb.AudioConfig{
			AudioEncoding: texttospeechpb.AudioEncoding_MP3,
		},
	}

	resp, err := client.SynthesizeSpeech(ctx, &req)
	if err != nil {
		log.Fatal(err)
	}

	// The resp's AudioContent is binary.
	filename := "output.mp3"
	err = ioutil.WriteFile(filename, resp.AudioContent, 0644)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Audio content written to file: %v\n", filename)
}

Java

// Imports the Google Cloud client library
import com.google.cloud.texttospeech.v1.AudioConfig;
import com.google.cloud.texttospeech.v1.AudioEncoding;
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
import com.google.cloud.texttospeech.v1.SynthesisInput;
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
import com.google.protobuf.ByteString;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * Google Cloud TextToSpeech API sample application. Example usage: mvn package exec:java
 * -Dexec.mainClass='com.example.texttospeech.QuickstartSample'
 */
public class QuickstartSample {

  /** Demonstrates using the Text-to-Speech API. */
  public static void main(String... args) throws Exception {
    // Instantiates a client
    try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
      // Set the text input to be synthesized
      SynthesisInput input = SynthesisInput.newBuilder().setText("Hello, World!").build();

      // Build the voice request, select the language code ("en-US") and the ssml voice gender
      // ("neutral")
      VoiceSelectionParams voice =
          VoiceSelectionParams.newBuilder()
              .setLanguageCode("en-US")
              .setSsmlGender(SsmlVoiceGender.NEUTRAL)
              .build();

      // Select the type of audio file you want returned
      AudioConfig audioConfig =
          AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();

      // Perform the text-to-speech request on the text input with the selected voice parameters and
      // audio file type
      SynthesizeSpeechResponse response =
          textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);

      // Get the audio contents from the response
      ByteString audioContents = response.getAudioContent();

      // Write the response to the output file.
      try (OutputStream out = new FileOutputStream("output.mp3")) {
        out.write(audioContents.toByteArray());
        System.out.println("Audio content written to file \"output.mp3\"");
      }
    }
  }
}

Node.js

// Imports the Google Cloud client library
const textToSpeech = require('@google-cloud/text-to-speech');

// Import other required libraries
const fs = require('fs');
const util = require('util');
// Creates a client
const client = new textToSpeech.TextToSpeechClient();
async function quickStart() {
  // The text to synthesize
  const text = 'hello, world!';

  // Construct the request
  const request = {
    input: {text: text},
    // Select the language and SSML voice gender (optional)
    voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
    // select the type of audio encoding
    audioConfig: {audioEncoding: 'MP3'},
  };

  // Performs the text-to-speech request
  const [response] = await client.synthesizeSpeech(request);
  // Write the binary audio content to a local file
  const writeFile = util.promisify(fs.writeFile);
  await writeFile('output.mp3', response.audioContent, 'binary');
  console.log('Audio content written to file: output.mp3');
}
quickStart();

Python

"""Synthesizes speech from the input string of text or ssml.
Make sure to be working in a virtual environment.

Note: ssml must be well-formed according to:
    https://www.w3.org/TR/speech-synthesis/
"""
from google.cloud import texttospeech

# Instantiates a client
client = texttospeech.TextToSpeechClient()

# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")

# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
    language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)

# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.MP3
)

# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
    input=synthesis_input, voice=voice, audio_config=audio_config
)

# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
    # Write the response to the output file.
    out.write(response.audio_content)
    print('Audio content written to file "output.mp3"')

Weitere Ressourcen

C++

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für C++:

C#

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für C#:

Go

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für Go:

Java

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für Java:

Node.js

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für Node.js:

PHP

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für PHP:

Python

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für Python:

Ruby

Die folgende Liste enthält Links zu weiteren Ressourcen im Zusammenhang mit der Clientbibliothek für Ruby: