1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

IBM Watson Text to Speech を使用してテキストから音声ファイルを生成する

Last updated at Posted at 2022-01-12

初めに

この記事では、IBM Watson 「Text to Speech」サービスを使用して、テキストデータから音声ファイルを生成する方法をご紹介します。なお、使用する言語はPythonです。

逆にIBM Watson 「Speech To Text」を使用して、弊社では『こえカラモジ』という音声から文字起こしサービスを現在無償提供しています。
面倒な導入など不要で、Webブラウザから直ぐに使用できますので是非お試し下さい。

手順

手順1. Watson Speech to Text インスタンスの作成

1. IBM Cloud アカウントの作成

IBM Cloudのアカウントが無い場合は、アカウントを作成します。

1-1.https://cloud.ibm.com にブラウザでアクセスします。

1-2.「アカウントの作成」のリンクをクリックします。
image.png

1-3. アカウント作成画面になるので、画面の指示に従い、アカウントを作成してください。
image.png

2. Watson Speech to Text インスタンスの作成

2-1. IBM Cloud の Webコンソールから、「カタログ」をクリックします。
image.png

2-2. 検索欄に、「text to speech」と入力すると、候補がでてくるので、「Text to Speech」を選択します。
image.png

2-3. 料金プランは「ライト」を選択。使用条件の同意にチェックをつけ、「作成」ボタンをクリックします。
image.png

2-4. インスタンスが作成されたら、「サービス資格情報」をクリックし、「apiKey」と「url」の情報を控えてください。コードを記載するときに必要になります。

手順3. Watson SDK の導入

3-1. 以下のpipコマンドで「ibm-watson」インストールします。

pip install ibm-watson

手順4.コーディングと実行

4-1. 以下のようなコードを記載します。

text2speech.py
from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

# インスタンス作成時にできた資格情報のapiKeyの値を指定
apiKey = "xxxxxxxxxxxxxxxxxx"

# インスタンス作成時にできた資格情報のurlの値を指定
serviceUrl = "https://xxxxxxxxxxxxxxx"

# 出力するファイル名を指定
audioFile = "test.wav"

# 音声化したいテキストを指定
text = "このプログラムは、テキストデータから音声ファイルを生成します。"

# 音声モデルを指定。日本語は、女性(Emi)の声のみ。英語は複数選択肢あり。
voice = "ja-JP_EmiV3Voice"

# 出力する音声フォーマットを指定。mp3にしたい場合は「audio/mp3」を指定
audioFormat = "audio/wav"


authenticator = IAMAuthenticator(apiKey)
text_to_speech = TextToSpeechV1(
    authenticator=authenticator
)

text_to_speech.set_service_url(serviceUrl)

with open(audioFile, 'wb') as audio_file:
    audio_file.write(
        text_to_speech.synthesize(
            text,
            voice=voice,
            accept=audioFormat
        ).get_result().content)

4-2. 記載したプログラムを実行します。指定した音声ファイルができます。

python text2speech.py
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?