RVC(Retrieval-based-Voice-Conversion)のモデルを使って日本語のテキストから音声データを一括で作成する

準備

コード

from pathlib import Path

from dotenv import load_dotenv
#import soundfile as sf

from infer.modules.vc.modules import VC
import os
from os.path import join, dirname
from configs.config import Config
import numpy as np
import pyopenjtalk
from scipy.io import wavfile
from pydub import AudioSegment
from pydub.playback import play
#import librosa
def generate_wav(text_line, filename):

      x, sr = pyopenjtalk.tts(text_line, speed=1.0, half_tone=0.0)
      wavfile.write("test.wav", sr, x.astype(np.int16))
#      voice = AudioSegment.from_file("test.wav", format="wav")
#      play(voice)


      _,tgt_opt = vc.vc_single(
        1, 
        "test.wav",
        f0_up_key = 0,
        f0_method = "rmvpe",
        f0_file = None,
        file_index = None,
        file_index2 = None,
        index_rate = 0.75,
        filter_radius = 3,
        resample_sr = 44100,
        rms_mix_rate = 0.25,
        protect= 0.33,
    
      )
      tgt_sr, audio_opt = tgt_opt
#      sf = 44100 # サンプリング周波数 Hz

#y, sr = librosa.core.load('test.wav', sr=44100, mono=True) 
#sf.write("new_test.wav", y, sr, subtype="PCM_16") 
#      sf.write("generation.wav",audio_opt , tgt_sr)
#
      wavfile.write(filename, tgt_sr, audio_opt)
      os.remove('test.wav')

      voice = AudioSegment.from_file(filename, format="wav")
      play(voice)
      
def read_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:  # ファイルをUTF-8エンコーディングで読み込み
        return file.readlines()

def main():
    file_path = "LINE.txt"  # 入力ファイル名を固定化
    lines = read_file(file_path)

    for index, line in enumerate(lines):
        line = line.strip()
        if line:

            output_filename = f"dat/"+(str(index).zfill(3))+f".wav"  # 出力ファイル名を固定化
            generate_wav(line,output_filename)
            print(f"wav '{output_filename}' generated successfully!")

if __name__ == "__main__":
    
#      load_dotenv(verbose=True)

      dotenv_path = join(dirname(__file__), '.env')
      print(f"wav '{dotenv_path}' generated successfully!")

      load_dotenv(dotenv_path)

#      load_dotenv(".env")

      config = Config()
      vc = VC(config)
      vc.get_vc("kikiV1.pth")
      main()

Python

Posted by eightban