0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VOICEVOX(zip版)を使って読み上げwavを作る際のメモ #2

Posted at

はじめに

の続きです。

Web APIを叩くスクリプトの改造

前回の記事で

あとは、params_dict.textを外部ファイル化などすれば、より便利になるだろうと感じた。
と書いたので、そこを変更しました。

ファイルの読み込みは with を使うと便利だそうです。

これを踏まえて、スクリプトを変更しました。

vvt2.py
import json
import requests
import wave
from datetime import datetime

# 基本情報
base_hash = {
    'host' : "localhost",
    'port': 50021,
    'in_txt' : "./input.txt",
    'out_wav': "./audio.wav"
}
# パラメータ
params_dict = {
    'text' : "これはテストなのだ。",
    'speaker': 1,    # ずんだもんあまあま
#    'speaker': 29,   # No.7ノーマル
}
# 追加のパラメータ
data_dict = {
    'speedScale': 1.3,
#    'speedScale': 1.1,
}

#----------

# wavファイルを出力
def generate_out_wav():
    host = base_hash['host']
    port = base_hash['port']

    # audi_queryを作る
    response1 = requests.post(
        f'http://{host}:{port}/audio_query',
        params=params_dict
    )
    headers = {'Content-Type': 'application/json',}

    # 追加のパラメータはここでセット
    my_audio_query = response1.json()
    for key, value in data_dict.items():
        my_audio_query[key] = value

    # synthesis
    response2 = requests.post(
        f'http://{host}:{port}/synthesis',
        headers=headers,
        params=params_dict,
        data=json.dumps(my_audio_query)
    )

    # ファイルに保存
    base_hash['out_wav'] = './' + get_nowymdhms_14str() + '.wav'
    wf = wave.open(base_hash['out_wav'], 'wb')
    wf.setnchannels(1)
    wf.setsampwidth(2)
    wf.setframerate(24000)
    wf.writeframes(response2.content)
    wf.close()

# txtファイルを読み込む
def read_in_txt():
    with open(base_hash['in_txt'], 'r', encoding='utf-8') as f:
        str = f.read()
        params_dict['text'] =str

# 現在の日時を14桁の文字列にフォーマット
def get_nowymdhms_14str():
    now = datetime.now()
    formatted_now = now.strftime('%Y%m%d%H%M%S')
    return formatted_now

# main()
if __name__ == '__main__':
    read_in_txt()
    generate_out_wav()

出力ファイル名が毎回同じだと管理が面倒なので、yyyymmddHHMMDDで出力されるように変更。(この関数自体はCopilotに書いてもらいました。便利。)

今後の課題

wavを外部ファイル化できたものの、wavファイルの冒頭に「プツッ」のようなノイズが乗るようになってしまったので、そのうち見直します。気になる。

(この項おわり)

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?