Last active
September 4, 2021 17:37
-
-
Save tam17aki/0f3d75930b43a4496f10a5208e76cb85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """PySimpleGUI上でテキスト音声合成するサンプルスクリプト.""" | |
| import numpy as np | |
| import pyopenjtalk # テキスト音声合成のライブラリ | |
| import PySimpleGUI as sg # GUI構築のライブラリ | |
| import sounddevice as sd # 録音・再生系のライブラリ | |
| # レイアウトの設定;テキストボックスとボタンを配置 | |
| LAYOUT = [ | |
| [ | |
| sg.InputText(default_text="おはようございます!", size=(35, 1), key="-TEXT-", font=32), | |
| sg.Button("合成", key="-SYNTH-", font=24), | |
| ] | |
| ] | |
| # レイアウトをもとにウィンドウを作成 | |
| WINDOW = sg.Window("TTS-sample", LAYOUT) | |
| def mainloop() -> None: | |
| """メインのループ.""" | |
| while True: # 無限ループにすることでGUIは起動しつづける | |
| event, values = WINDOW.read() # イベントと「値」を取得 | |
| # windowを閉じる | |
| if event is sg.WIN_CLOSED: | |
| break # 無限ループを脱出 | |
| # 入力されたテキストから音声合成する | |
| # →テキストから音声への変換処理 | |
| if event == "-SYNTH-": | |
| text = values["-TEXT-"] | |
| # 音声合成(テキストデータ→音声データ) | |
| speech, sr = pyopenjtalk.tts(text) # numpy配列とサンプリング周波数 | |
| # 音割れ防止 | |
| speech = (speech / np.abs(speech).max()) * (np.iinfo(np.int16).max / 2 - 1) | |
| # 再生 | |
| sd.play(speech.astype(np.int16), sr) | |
| sd.sleep(int(1000 * len(speech) / sr)) | |
| # 終了処理:ウィンドウを閉じる | |
| WINDOW.close() | |
| if __name__ == "__main__": | |
| mainloop() # GUI起動 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment