Polly | python3Xのブログ

python3Xのブログ

ここでは40代、50代の方が日々の生活で役に立つ情報や私の趣味であるプログラム、Excelや科学に関する内容で投稿する予定です。

普通音声の合成

import boto3
import contextlib
import os
# Pollyサービスクライアントを作成する
polly = boto3.client('polly')
# 音声合成テキスト
text = "Welcome to Polly's world! Well, let's enjoy the world of speech synthesis together!"
result = polly.synthesize_speech(
    Text=text, OutputFormat='mp3', VoiceId='Joanna')   # テキストから標準音声を合成する
   
path = 'polly_standard.mp3'   # 出力音声ファイルのパス
# 音声のストリームを開く
with contextlib.closing(result['AudioStream']) as stream:
    with open(path, 'wb') as file:   # 書き込み用音声ファイルを開く
        file.write(stream.read())     # 音声を読み込んでファイルに書き込む
       
# 出力の標準音声ファイルを再生する
if os.name == 'nt':
    os.startfile(path)

ニューラル音声の合成

import boto3
import contextlib
import os
# リージョンを指定してPollyサービスクライアントを作成する
polly = boto3.client('polly', 'us-east-1' # us-east-1:米国東部(バージニア北部)のサーバー
text = "Welcome to Polly's world! Well, let's enjoy the world of speech synthesis together!"
result = polly.synthesize_speech(
    Text=text, OutputFormat='mp3', VoiceId='Joanna', # ニューラル音声を指定してテキストから音声を合成する
    Engine='neural')
path = 'polly_neural.mp3'
with contextlib.closing(result['AudioStream']) as stream:
    with open(path, 'wb') as file:
        file.write(stream.read())
if os.name == 'nt':
    os.startfile(path)
 
音が滑らかでソフトに聞こえます口笛
 
以下手抜きです

ニュースキャスター調音声の合成

import boto3
import contextlib
import os
polly = boto3.client('polly', 'us-east-1')
text = '''
<speak>
<amazon:domain name="news">
"Welcome to Polly's world! Well, let's enjoy the world of speech synthesis together!"
</amazon:domain>
</speak>
'''
result = polly.synthesize_speech(
    Text=text, OutputFormat='mp3', VoiceId='Joanna',
    Engine='neural', TextType='ssml')
path = 'polly_news.mp3'
with contextlib.closing(result['AudioStream']) as stream:
    with open(path, 'wb') as file:
        file.write(stream.read())
if os.name == 'nt':
    os.startfile(path)