GCP の Text-to-Speech API を使って
テキストから音声を生成する
入力したテキストを
音声を男性か女性か選択して
生成する
音声の文字起こしなら
Speech toText
のほうになる
GCP へログインし
認証情報 > API とサービスの有効化
で
Cloud Text-toSpeech API
をクリック
これには無料枠があるようでhttps://cloud.google.com/text-to-speech/pricing/?hl=JA
に記載されている
1ヶ月あたり
0〜100 万文字
で
音声合成マークアップ言語(SSML)タグも文字数カウントに含まれる
有効化のときに課金設定があるので注意
有効化できたら
認証情報で
認証情報を作成
で
サービスアカウントを作成する
続行
で完了
をクリック
これでサービスアカウントが作成される
このアドレスをクリックし
キーをクリック
鍵を追加
で
新しい鍵を追加
でタイプをJSONにする
これでJSONファイルをダウンロードしたら
環境変数を設定する
GOOGLE_APPLICATION_CREDENTIALS
を設定する
これにパスを設定する
ダウンロードしたJSONファイルを
secret.json に名前を変えておく
ダウンロードしたファイルを新しい順にするなら
lsコマンドでファイルを新しい順番、古い順番にソートする方法
を参考に
1 | ls -lt |
で表示されるので
1 | cp raspberryPi-b787a5abd4d5.json secret.json |
でコピーし
1 | mv secret.json ~ /notebook/ |
で
jupyter notebook で扱えるようにしておく
次に
notebook でos ライブラリをインポートすることで使っていく
1 2 | import os os.environ[ 'GOOGLE_APPLICATION_CREDENTIALS' ] = 'secret.json' |
でOK
次にライブラリのインストール
を参考に
1 | pip install --upgrade google-cloud-texttospeech |
をいれる
notebook でやるなら
1 | !pip install --upgrade google-cloud-texttospeech |
とする
これで準備できたので
音声の再生
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from google.cloud import texttospeech client = texttospeech.TextToSpeechClient() synthesis_input = texttospeech.SynthesisInput(text = "Hello, World!" ) voice = texttospeech.VoiceSelectionParams( language_code = "en-US" , ssml_gender = texttospeech.SsmlVoiceGender.NEUTRAL ) audio_config = texttospeech.AudioConfig( audio_encoding = texttospeech.AudioEncoding.MP3 ) response = client.synthesize_speech( input = synthesis_input, voice = voice, audio_config = audio_config ) with open ( "output.mp3" , "wb" ) as out: out.write(response.audio_content) print ( 'Audio content written to file "output.mp3"' ) |
を
からコピペして
コメント部分を削除
1 | client = texttospeech.TextToSpeechClient() |
の部分で認証をしている
1 | synthesis_input = texttospeech.SynthesisInput(text = "Hello, World!" ) |
で
出力する音声をセット
今回なら
Hello, World
となる
1 2 3 | voice = texttospeech.VoiceSelectionParams( language_code = "en-US" , ssml_gender = texttospeech.SsmlVoiceGender.NEUTRAL ) |
で声の設定
language_code で言語の指定
en-US なら英語
ssml_gender は性別の設定
NEUTRAL だとどっちでもないような音声になる
1 2 3 | audio_config = texttospeech.AudioConfig( audio_encoding = texttospeech.AudioEncoding.MP3 ) |
で音声形式の指定
今回は MP3 にしている
これらのパラメータを
1 2 3 | response = client.synthesize_speech( input = synthesis_input, voice = voice, audio_config = audio_config ) |
でセットして音声を作成する
1 2 3 4 | filename = "output.mp3" with open (filename, "wb" ) as out: out.write(response.audio_content) print (f 'Audio content written to file {filename}' ) |
と最後を変えることで音声ファイル名を簡単に変更できる
wb なので
バイナリ形式で書き込み
しかし Cloud SDK がないと実行できないみたいなので
Google Cloud SDK のインストール
を参考に
1 | echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources .list.d /google-cloud-sdk .list |
で
Cloud SDK の配布 URI をパッケージ ソースとして追加
1 | sudo apt-get install apt-transport-https ca-certificates gnupg |
で
apt-transport-https がインストールされているのを確認
1 | curl https: //packages .cloud.google.com /apt/doc/apt-key .gpg | sudo apt-key --keyring /usr/share/keyrings/cloud .google.gpg add - |
で
公開鍵のインポート
1 2 | sudo apt-get update sudo apt-get install google-cloud-sdk |
で
リポジトリ更新と
google-cloud-sdk のインストール
しかしマニュアルを読んでみたら
全機能は使えないので
Linux 用インストールでないとダメと気づいた
Ubuntu 18.04 (WSL) に、Google Cloud SDK をインストールする
も参考に
一度
1 2 | sudo apt-get remove google-cloud-sdk sudo apt remove python3-crcmod |
で削除
1 | curl -O https: //dl .google.com /dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-337 .0.0-linux-x86_64. tar .gz |
でファイルを取得
1 | tar zxvf google-cloud-sdk-337.0.0-linux-x86_64. tar .gz |
で
圧縮ファイルを展開
1 | /google-cloud-sdk/install .sh |
を実行し
Google Cloud SDK のパスを通すことでコマンド補完ができるようになる
1 | source .bashrc |
を実行して設定反映を忘れずに
1 | . /google-cloud-sdk/bin/gcloud init |
で
SDK初期化と認証開始
1 | You must log in to continue . Would you like to log in (Y /n )? |
となるので
Y
とすると
ブラウザが立ち上がり
認証画面になる
認証成功なら
https://cloud.google.com/sdk/auth_success
の画面がでる
次にどのプロジェクトをデフォルトにするか聞かれる
1 2 3 | Please enter numeric choice or text value (must exactly match list item): Please enter a value between 1 and 12, or a value present in the list: |
となるので
任意のプロジェクト No を入力すると終了
1 | gcloud components update |
でコンポーネントを更新
しかしこれでもダメ
GCPの Text-to-Speech で 文字読み上げ を作る
を参考に
1 | pip install pydub |
で
MP3再生に必要なパッケージをいれる
1 | pip install google-cloud-storage |
をしたけどダメ
もう一回APIの確認をしたら
Cloud Speech Text API が無効になっていたので
再度有効化してから
もう一度鍵ファイルを作成することで有効化された
これで再度実行したら無事に音声が作成された