

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Mensintesis pidato dengan contoh Amazon Polly
<a name="synthesize-example"></a>

Halaman ini menyajikan contoh sintesis ucapan singkat yang dilakukan di konsol, the AWS CLI, dan dengan Python. Contoh ini melakukan sintesis ucapan dari teks biasa, bukan SSML.

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

**Sintesis ucapan di konsol**

1. Masuk ke Konsol Manajemen AWS dan buka konsol Amazon Polly di. [https://console.aws.amazon.com/polly/](https://console.aws.amazon.com/polly/)

1. Pilih tab **Text-to-Speech**. Bidang teks akan dimuat dengan contoh teks sehingga Anda dapat dengan cepat mencoba Amazon Polly.

1. Matikan **SSML**.

1. Ketik atau tempel teks ini ke dalam kotak input.

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

1. Di bawah **Engine**, pilih **Generative**, **Long Form**, **Neural**, atau **Standard**.

1. Pilih bahasa dan AWS Wilayah, lalu pilih suara. (Jika Anda memilih **Neural** for **Engine**, hanya bahasa dan suara yang mendukung NTTS yang tersedia. Semua suara Standar dan Formulir Panjang dinonaktifkan.)

1. Untuk segera mendengarkan pidato, pilih **Dengarkan**.

1. Untuk menyimpan pidato ke file, lakukan salah satu hal berikut:

   1. Pilih **Unduh**.

   1. **Untuk mengubah ke format file yang berbeda, perluas **Pengaturan tambahan**, aktifkan **Pengaturan format file ucapan**, pilih format file yang Anda inginkan (seperti MP3, OGG, PCM, MU-law, atau A-law), lalu pilih Unduh.** 

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

Dalam latihan ini, Anda memanggil `SynthesizeSpeech` operasi dengan melewatkan teks input. Anda dapat menyimpan audio yang dihasilkan sebagai file dan memverifikasi kontennya.

1. Jalankan `synthesize-speech` AWS CLI perintah untuk mensintesis teks sampel ke file audio (`hello.mp3`). 

    AWS CLI Contoh berikut diformat untuk Unix, Linux, dan macOS. Untuk Windows, ganti karakter kelanjutan backslash (\$1) Unix di akhir setiap baris dengan tanda sisipan (^) dan gunakan tanda kutip penuh (“) di sekitar teks input dengan tanda kutip tunggal (') untuk tag interior.

   ```
   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
   ```

   Dalam panggilan ke`synthesize-speech`, Anda memberikan contoh teks untuk disintesis dengan suara pilihan Anda. Anda harus memberikan ID suara (dijelaskan pada langkah berikut) dan format output. Perintah menyimpan audio yang dihasilkan ke `hello.mp3` file. Selain MP3 file, operasi mengirimkan output berikut ke konsol. 

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

1. Putar `hello.mp3` file yang dihasilkan untuk memverifikasi ucapan yang disintesis. 

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

Untuk menguji kode contoh Python, Anda memerlukan kode. AWS SDK for Python (Boto) Untuk instruksi, lihat [AWS SDK untuk Python (Boto3)](https://aws.amazon.com/sdk-for-python/). 

Kode Python dalam contoh ini melakukan tindakan berikut:
+ Memanggil AWS SDK for Python (Boto) untuk mengirim `SynthesizeSpeech` permintaan ke Amazon Polly (dengan memberikan beberapa teks sebagai input). 
+ Mengakses aliran audio yang dihasilkan dalam respons dan menyimpan audio ke file (`speech.mp3`) di disk lokal Anda. 
+ Memutar file audio dengan pemutar audio default untuk sistem lokal Anda. 

Simpan kode ke file (example.py) dan jalankan.

```
"""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])
```

------

Untuk contoh lebih mendalam, lihat topik berikut:
+ [Menggunakan SSML di konsol](ssml-to-speech-console.md) 
+ [Menerapkan leksikon (Synthesizing Speech)](managing-lexicons-console-synthesize-speech.md) 
+ [Contoh kode dan aplikasi untuk Amazon Polly](samples-and-examples.md) 