SSML로 주소 말하기

이 튜토리얼에서는 음성 합성 마크업 언어(SSML)를 사용하여 주소 텍스트 파일을 말하는 방법을 보여줍니다. SSML 태그로 텍스트의 문자열을 마크업하여 Cloud Text-to-Speech에서 합성 오디오를 맞춤설정할 수 있습니다.

일반 텍스트 일반 텍스트의 SSML 렌더링
123 Street Ln
<speak>123 Street Ln</speak>
1 Number St
<speak>1 Number St</speak>
1 Piazza del Fibonacci
<speak>1 Piazza del Fibonacci</speak>

목표

SSML 및 Cloud Text-to-Speech 클라이언트 라이브러리를 사용하여 Cloud Text-to-Speech에 합성 음성 요청을 전송합니다.

비용

비용 정보는 Cloud TTS 가격 책정 페이지를 참조하세요.

시작하기 전에

코드 샘플 다운로드

코드 샘플을 다운로드하려면 사용하려는 프로그래밍 언어에 대해 Google Cloud GitHub 샘플을 클론합니다.

Java

이 튜토리얼에서는 Google Cloud Platform 자바 샘플 저장소texttospeech/cloud-client/src/main/java/com/example/texttospeech/ 디렉터리에 있는 코드가 사용됩니다.

이 튜토리얼용 코드로 이동하여 다운로드하려면 터미널에서 다음 명령어를 실행합니다.

git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd java-docs-samples/texttospeech/cloud-client/src/main/java/com/example/texttospeech/

Node.js

이 튜토리얼에서는 Google Cloud Platform Node.js 샘플 저장소texttospeech 디렉터리에 있는 코드를 사용합니다.

이 튜토리얼용 코드로 이동하여 다운로드하려면 터미널에서 다음 명령어를 실행합니다.

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
cd texttospeech/

Python

이 튜토리얼에서는 Google Cloud Platform Python 샘플 저장소texttospeech/snippets 디렉터리에 있는 코드가 사용됩니다.

이 튜토리얼용 코드로 이동하여 다운로드하려면 터미널에서 다음 명령어를 실행합니다.

git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
cd samples/snippets

클라이언트 라이브러리 설치

이 튜토리얼에서는 텍스트 음성 변환 클라이언트 라이브러리가 사용됩니다.

Java

이 튜토리얼에서는 다음 종속 항목이 사용됩니다.

<!--  Using libraries-bom to manage versions.
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.32.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

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

Node.js

터미널에서 다음 명령어를 실행합니다.

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

Python

터미널에서 다음 명령어를 실행합니다.

pip install --upgrade google-cloud-texttospeech

Google Cloud Platform 사용자 인증 정보 설정

Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable applies only to your current shell session. If you want the variable to apply to future shell sessions, set the variable in your shell startup file, for example in the ~/.bashrc or ~/.profile file.

Linux 또는 macOS

export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

Replace KEY_PATH with the path of the JSON file that contains your credentials.

For example:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

Windows

For PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

Replace KEY_PATH with the path of the JSON file that contains your credentials.

For example:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

For command prompt:

set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH

Replace KEY_PATH with the path of the JSON file that contains your credentials.

라이브러리 가져오기

이 튜토리얼에서는 다음 시스템 및 클라이언트 라이브러리가 사용됩니다.

Java

Cloud TTS용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud TTS 클라이언트 라이브러리를 참조하세요. 자세한 내용은 Cloud TTS Java API 참고 문서를 확인하세요.

Cloud TTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

// 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.common.html.HtmlEscapers;
import com.google.protobuf.ByteString;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

Node.js

Cloud TTS용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud TTS 클라이언트 라이브러리를 참조하세요. 자세한 내용은 Cloud TTS Node.js API 참고 문서를 확인하세요.

Cloud TTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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

// Import other required libraries
const fs = require('fs');
//const escape = require('escape-html');
const util = require('util');

Python

Cloud TTS용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud TTS 클라이언트 라이브러리를 참조하세요. 자세한 내용은 Cloud TTS Python API 참고 문서를 확인하세요.

Cloud TTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

import html

from google.cloud import texttospeech

Cloud Text-to-Speech API 사용

다음 함수는 SSML로 태그가 지정된 텍스트 문자열과 MP3 파일 이름을 사용합니다. 다음 함수는 SSML로 태그가 지정된 텍스트를 사용해서 합성 오디오를 생성합니다. 이 함수는 합성 오디오를 매개변수로 지정된 MP3 파일 이름으로 저장합니다.

전체 SSML 입력은 단일 음성으로만 읽혀질 수 있습니다. VoiceSelectionParams 객체에 음성을 설정할 수 있습니다.

Java

Cloud TTS용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Cloud TTS 클라이언트 라이브러리를 참조하세요. 자세한 내용은 Cloud TTS Java API 참고 문서를 확인하세요.

Cloud TTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

/**
 * Generates synthetic audio from a String of SSML text.
 *
 * <p>Given a string of SSML text and an output file name, this function calls the Text-to-Speech
 * API. The API returns a synthetic audio version of the text, formatted according to the SSML
 * commands. This function saves the synthetic audio to the designated output file.
 *
 * @param ssmlText String of tagged SSML text
 * @param outFile String name of file under which to save audio output
 * @throws Exception on errors while closing the client
 */
public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
  // Instantiates a client
  try (TextToSpeechClient textToSpeechClient = TextToSpeechClient