본문 바로가기
Python

TTS 사용하기

by holy season 2023. 8. 8.
반응형

TTS

윈도우에는 기본적으로 내장되어있는 tts 엔진이 있다.

파이썬에서 이것을 사용하려면 pywin32를 설치해야한다.

라이브러리 설치

pip install pywin32

사용 가능한 목소리

사용가능한 목소리로는 Heami, Zira, David가 있다.

Heami는 한국어를 말할 수 있는 TTS 엔진이고 Zira, David는 영어를 말할 수 있는 TTS 엔진이다.

0. Microsoft Heami Desktop - Korean
1. Microsoft Zira Desktop - English (United States)
2. Microsoft David Desktop - English (United States)

코드

이 코드는 사용가능한 목소리 종류를 보여주고 selected_voice_index에 설정한 값의 목소리로 음성을 출력해주는 예제이다.

text_to_speech함수의 2번째 인자로 음성 출력할 텍스트를 넣어주면 된다.

import win32com.client

def change_voice(sp_voice, voice_index):
    # Get available voices
    voices = sp_voice.GetVoices()

    if 0 <= voice_index < len(voices):
        # Set the desired voice
        sp_voice.Voice = voices.Item(voice_index)
        print(f"Changed voice to: {sp_voice.Voice.GetDescription()}")

def text_to_speech(sp_voice, text):
    # Set the text to be spoken
    sp_voice.Speak(text)

def main():
    # Create a SpVoice COM object
    sp_voice = win32com.client.Dispatch("SAPI.SpVoice")

    # Display available voices
    print("Available voices:")
    voices = sp_voice.GetVoices()
    for i, voice in enumerate(voices):
        print(f"{i}. {voice.GetDescription()}")

    # Change the voice (voice index may vary)
    selected_voice_index = 0  # Adjust the index as needed
    change_voice(sp_voice, selected_voice_index)

    # Convert text to speech and play
    text_to_speech(sp_voice, "Hello, this is a test using win32com.client.")

if __name__ == "__main__":
    main()
반응형

'Python' 카테고리의 다른 글

Pyinstaller ffmpeg 포함 시키기  (0) 2023.08.12
아나콘다(Anaconda)  (0) 2023.08.09
PySide6 Qtdesigner로 만든 UI를 파이썬 파일로 만들기  (0) 2023.07.12
eval 함수  (0) 2023.07.06
json 형태로 파일 저장하기  (0) 2023.07.05