Skip to content

Instantly share code, notes, and snippets.

@weedge
Created May 10, 2024 11:25
Show Gist options
  • Save weedge/7b8393990adfa205afbede4d1c8615ec to your computer and use it in GitHub Desktop.
Save weedge/7b8393990adfa205afbede4d1c8615ec to your computer and use it in GitHub Desktop.
from melo.api import TTS
import os
import torch
from openvoice import se_extractor
from openvoice.api import ToneColorConverter
ckpt_converter = 'checkpoints_v2/converter'
device = "cuda:0" if torch.cuda.is_available() else "cpu"
output_dir = 'outputs_v2'
print(device)
# load tone_color_converter ckpt
tone_color_converter = ToneColorConverter(
f'{ckpt_converter}/config.json', device=device)
tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
#load target se
target_dir = "processed"
audio_name = "me_reference_v2_2oULQ_^lIO3MSGW77"
se_path = os.path.join(target_dir, audio_name, 'se.pth')
target_se = torch.load(f'{se_path}', map_location=device)
#print(target_se)
src_path = f'{output_dir}/tmp.wav'
# Speed is adjustable
speed = 1.0
texts = {
# The newest English base speaker model
'EN_NEWEST': "Did you ever hear a folk tale about a giant turtle?",
# 'EN': "Did you ever hear a folk tale about a giant turtle?",
# 'ES': "El resplandor del sol acaricia las olas, pintando el cielo con una paleta deslumbrante.",
# 'FR': "La lueur dorée du soleil caresse les vagues, peignant le ciel d'une palette éblouissante.",
'ZH': "在这次vacation中,我们计划去Paris欣赏埃菲尔铁塔和卢浮宫的美景。",
# 'JP': "彼は毎朝ジョギングをして体を健康に保っています。",
# 'KR': "안녕하세요! 오늘은 날씨가 정말 좋네요.",
}
for language, text in texts.items():
model = TTS(language=language, device=device)
speaker_ids = model.hps.data.spk2id
for speaker_key in speaker_ids.keys():
speaker_id = speaker_ids[speaker_key]
speaker_key = speaker_key.lower().replace('_', '-')
print(speaker_id, speaker_key)
# 使用meloTTS作为基础原始模型将文本转换成语音,将音频文件保存在src_path中
source_se = torch.load(
f'checkpoints_v2/base_speakers/ses/{speaker_key}.pth', map_location=device)
model.tts_to_file(text, speaker_id, src_path, speed=speed)
save_path = f'{output_dir}/output_v2_{speaker_key}.wav'
# Run the tone color converter
# 将源音频和目标音频进行音色转换, 并将转换后的音频文件保存到save_path中
encode_message = "@MyShell"
tone_color_converter.convert(
audio_src_path=src_path,
src_se=source_se,
tgt_se=target_se,
output_path=save_path,
message=encode_message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment