

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 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. [**Text-to-Speech (テキスト読み上げ機能)**] タブを選択します。テキストフィールドにはサンプルテキストが読み込まれるので、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 の例は、Unix、Linux、および 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) が必要です。手順については、[AWS SDK for Python (Boto3)](https://aws.amazon.com/sdk-for-python/)を参照してください。

次の Python コードの例では、以下のアクションを実行します。
+ AWS SDK for Python (Boto) を呼び出して、`SynthesizeSpeech` リクエストを Amazon Polly に送信します (サンプルテキストを入力として指定します)。
+ レスポンスで生成された音声ストリームにアクセスし、音声をローカルディスク上のファイル (`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) 