

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Amazon Polly 合成语音示例
<a name="synthesize-example"></a>

本页中有个简短示例，描述在控制台和 AWS CLI 中执行以及使用 Python 执行语音合成。本示例使用纯文本执行语音合成，而不是使用 SSML。

------
#### [ Console ]

**在控制台上合成语音**

1. 登录到 AWS 管理控制台 并打开 Amazon Polly 控制台，网址：[https://console.aws.amazon.com/polly/](https://console.aws.amazon.com/polly/)。

1. 选择**文本到语音转换**选项卡。文本字段将加载示例文本，以便您可以快速试用 Amazon Polly。

1. 关闭 **SSML**。

1. 在输入框中键入或粘贴此文本。

   ```
   He was caught up in the game. In the middle of the 10/3/2014 W3C meeting he shouted, "Score!" quite loudly.
   ```

1. 在**引擎**下，选择**生成式**、**长篇**、**神经**或**标准**。

1. 选择语言和 AWS 区域，然后选择语音。（如果您为**引擎**选择**神经**，则只有支持 NTTS 的语言和语音才可用。所有标准和长篇语音都会被禁用。）

1. 要立即收听语音，请选择**收听**。

1. 要将语音保存到文件中，请执行以下操作之一：

   1. 选择**下载**。

   1. 要更改为其他文件格式，请展开**其他设置**，打开**语音文件格式设置**，选择所需的文件格式，然后选择**下载**。

------
#### [ AWS CLI ]

在本练习中，将通过传送输入文本来调用 `SynthesizeSpeech` 操作。您可以将生成的音频保存为文件并验证其内容。

1. 运行 `synthesize-speech` AWS CLI 命令以将示例文本合成为音频文件 (`hello.mp3`)。

   以下 AWS CLI 示例针对 Linux、Unix 和 macOS 编排了格式。对于 Windows，请将每行末尾的反斜杠 (\$1) Unix 行继续符替换为脱字号 (^) 并在输入文本周围使用全角引号（“），内部标签使用单引号（’）。

   ```
   aws polly synthesize-speech \
       --output-format mp3 \
       --voice-id Joanna \
       --text 'Hello, my name is Joanna. I learned about the W3C on 10/3 of last year.' \
       hello.mp3
   ```

   在对 `synthesize-speech` 的调用中，您提供样本文本，该文本将与您选择的语音进行合成。您必须提供语音 ID（将在后续步骤中进行说明）和输出格式。该命令会将生成的音频保存为 `hello.mp3` 文件。除了 MP3 文件，此操作还可将以下输出发送至控制台。

   ```
   {
           "ContentType": "audio/mpeg", 
           "RequestCharacters": "71"
   }
   ```

1. 播放生成的 `hello.mp3` 文件以验证合成的语音。

------
#### [ Python ]

要测试 Python 示例代码，您需要AWS SDK for Python (Boto)。有关说明，请参阅 [适用于 Python (Boto3) 的 AWS SDK](https://aws.amazon.com/sdk-for-python/)。

本示例中的 Python 代码将执行以下操作：
+ 调用 AWS SDK for Python (Boto) 以便向 Amazon Polly 发送 `SynthesizeSpeech` 请求（通过提供一些文本作为输入）。
+ 访问在响应中生成的音频流并将音频保存为您本地磁盘上的文件 (`speech.mp3`)。
+ 使用您的本地系统的默认音频播放器播放音频文件。

将代码保存为一个文件 (example.py) 并运行。

```
"""Getting Started Example for Python 2.7+/3.3+"""
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir

# Create a client using the credentials and region defined in the [adminuser]
# section of the AWS credentials file (~/.aws/credentials).
session = Session(profile_name="adminuser")
polly = session.client("polly")

try:
    # Request speech synthesis
    response = polly.synthesize_speech(Text="Hello world!", OutputFormat="mp3",
                                        VoiceId="Joanna")
except (BotoCoreError, ClientError) as error:
    # The service returned an error, exit gracefully
    print(error)
    sys.exit(-1)

# Access the audio stream from the response
if "AudioStream" in response:
    # Note: Closing the stream is important because the service throttles on the
    # number of parallel connections. Here we are using contextlib.closing to
    # ensure the close method of the stream object will be called automatically
    # at the end of the with statement's scope.
        with closing(response["AudioStream"]) as stream:
           output = os.path.join(gettempdir(), "speech.mp3")

           try:
            # Open a file for writing the output as a binary stream
                with open(output, "wb") as file:
                   file.write(stream.read())
           except IOError as error:
              # Could not write to file, exit gracefully
              print(error)
              sys.exit(-1)

else:
    # The response didn't contain audio data, exit gracefully
    print("Could not stream audio")
    sys.exit(-1)

# Play the audio using the platform's default player
if sys.platform == "win32":
    os.startfile(output)
else:
    # The following works on macOS and Linux. (Darwin = mac, xdg-open = linux).
    opener = "open" if sys.platform == "darwin" else "xdg-open"
    subprocess.call([opener, output])
```

------

有关更深入的示例，请参阅以下主题：
+ [在控制台上使用 SSML](ssml-to-speech-console.md) 
+ [应用词典（合成语音）](managing-lexicons-console-synthesize-speech.md) 
+ [Amazon Polly 的示例代码和应用程序](samples-and-examples.md) 