저번 STT를 이어서 나중에 참고용으로 TTS도 정리. 하지만 공식 API 사이트에서 긁어오고 config 변경한 것밖에 없다. STT보다 훨씬 코드가 간단하다.
from google.cloud import texttospeech
# 클라이언드 생성
client = texttospeech.TextToSpeechClient()
# 텍스트 인풋
TEXT = "안녕하세요!"
synthesis_input = texttospeech.SynthesisInput(text=TEXT)
# 여기서 음성/오디오 config 들을 설정해준다.
# 언어/모델 등도 아래와 같이 여기서 변경
'''
voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
name="en-US-Neural2-C", # Neural2, WaveNet 등 다양한 모델 지원
)
'''
voice_config = texttospeech.VoiceSelectionParams(
language_code="ko-KR",
ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL,
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.LINEAR16
)
# TTS 실행!
response = client.synthesize_speech(
input=synthesis_input, voice=voice_config, audio_config=audio_config
)
# 오디오 아웃풋은 바이트로 생성된다
with open("output.wav", "wb") as out:
out.write(response.audio_content)
print('생성 완료')