

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# Amazon Polly를 사용한 스피치 합성 예제
<a name="synthesize-example"></a>

이 페이지에는 콘솔, AWS CLI 및 Python을 사용하여 수행된 짧은 스피치 합성 예제가 나와 있습니다. 이 예제에서는 SSML이 아닌 일반 텍스트에서 스피치 합성을 수행합니다.

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

**콘솔에서 스피치 합성**

1. AWS Management Console에 로그인한 후 [https://console.aws.amazon.com/polly/](https://console.aws.amazon.com/polly/)에서 Amazon 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`)에 합성합니다.

   다음은 Unix, Linux, macOS용 형식으로 지정된 AWS CLI 예제입니다. 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)를 간접적으로 호출해 텍스트를 입력으로 제공하여 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) 