Python tts google api


google tts 빠른 시작 페이지에 나와있는 코드를 임의로 수정했다.

  1. 한글로 설정
  2. 한글 언어 중 남자 - C 타입으로 음성 변환(WaveNet 사용한 것)
  3. 인증 정보 추가(미리 서비스 계정 credential json 파일이 있어야 한다)
# -*- coding: utf-8 -*-
"""Synthesizes speech from the input string of text or ssml.

Note: ssml must be well-formed according to:
    https://www.w3.org/TR/speech-synthesis/
"""
from google.cloud import texttospeech
from google.oauth2.service_account import Credentials

JSON_FILE_LOCATION = "./    .json"


def tts():
    # Instantiates a client
    creds = Credentials.from_service_account_file(JSON_FILE_LOCATION)
    client = texttospeech.TextToSpeechClient(credentials=creds)

    # Set the text input to be synthesized
    synthesis_input = texttospeech.SynthesisInput(text="하이")

    # Build the voice request, select the language code ("en-US") and the ssml
    # voice gender ("neutral")
    voice = texttospeech.VoiceSelectionParams(
        language_code="ko-KR", 
        name="ko-KR-Wavenet-C",
    )

    # Select the type of audio file you want returned
    audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.MP3
    )

    # Perform the text-to-speech request on the text input with the selected
    # voice parameters and audio file type
    response = client.synthesize_speech(
        input=synthesis_input, voice=voice, audio_config=audio_config
    )

    # The response's audio_content is binary.
    with open("output.mp3", "wb") as out:
        # Write the response to the output file.
        out.write(response.audio_content)
        print('Audio content written to file "output.mp3"')

def list_voices():
    """Lists the available voices."""
    from google.cloud import texttospeech

    creds = Credentials.from_service_account_file(JSON_FILE_LOCATION)
    client = texttospeech.TextToSpeechClient(credentials=creds)

    # Performs the list voices request
    voices = client.list_voices()

    for voice in voices.voices:
        # Display the voice's name. Example: tpc-vocoded
        print(f"Name: {voice.name}")

        # Display the supported language codes for this voice. Example: "en-US"
        for language_code in voice.language_codes:
            print(f"Supported language: {language_code}")

        ssml_gender = texttospeech.SsmlVoiceGender(voice.ssml_gender)

        # Display the SSML Voice Gender
        print(f"SSML Voice Gender: {ssml_gender.name}")

        # Display the natural sample rate hertz for this voice. Example: 24000
        print(f"Natural Sample Rate Hertz: {voice.natural_sample_rate_hertz}\n")


if __name__ == '__main__':
    #list_voices()
    tts()