forked from myshell-ai/OpenVoice
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodes.py
230 lines (183 loc) · 9.2 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import torch
import random
import folder_paths
import soundfile as sf
from melo.api import TTS
from .openvoice import se_extractor
from .openvoice.api import BaseSpeakerTTS, ToneColorConverter
class AnyType(str):
def __eq__(self, _) -> bool:
return True
def __ne__(self, __value: object) -> bool:
return False
any = AnyType("*")
class OpenVoiceTTS:
@classmethod
def INPUT_TYPES(s):
audio_extensions = ["wav", "mp3", "flac"]
input_dir = folder_paths.get_input_directory()
files = []
for f in os.listdir(input_dir):
if os.path.isfile(os.path.join(input_dir, f)):
file_parts = f.lower().split('.')
if len(file_parts) > 1 and (file_parts[-1] in audio_extensions):
files.append(f)
return {
"required": {
"text": ("STRING", {"default": '', "multiline": True}),
"lang": (["English","Chinese"],),
"style": (["default","whispering","cheerful","terrified","angry","sad","friendly"],),
"speed": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.1}),
"ref_voice": (sorted(files),),
},
}
CATEGORY = "OpenVoice"
RETURN_TYPES = (any, "INT",)
RETURN_NAMES = ("AUDIO", "SAMPLE_RATE",)
FUNCTION = "inference"
def inference(self, text, lang, style, speed, ref_voice):
local_dir = os.path.join(folder_paths.models_dir, 'openovice')
if not os.path.exists(local_dir) or not os.path.isdir(local_dir):
from huggingface_hub import snapshot_download
snapshot_download(repo_id="myshell-ai/OpenVoice", local_dir=local_dir, local_dir_use_symlinks=False)
mark = BaseSpeakerTTS.language_marks.get(lang.lower(), None)
assert mark is not None, f"language {lang} is not supported"
ckpt_base = os.path.join(local_dir, f'checkpoints/base_speakers/{mark}')
ckpt_converter = os.path.join(local_dir, 'checkpoints/converter')
device="cuda:0" if torch.cuda.is_available() else "cpu"
base_speaker_tts = BaseSpeakerTTS(f'{ckpt_base}/config.json', device=device)
base_speaker_tts.load_ckpt(f'{ckpt_base}/checkpoint.pth')
tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)
tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
style_name = 'default' if style == 'default' else 'style'
source_se = torch.load(f'{ckpt_base}/{mark.lower()}_{style_name}_se.pth').to(device)
reference_speaker = os.path.join(folder_paths.get_input_directory(), ref_voice)
temp_dir = folder_paths.get_temp_directory()
file_prefix = ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for _ in range(5))
target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir=temp_dir, vad=True)
save_path = f'{temp_dir}/{file_prefix}_output_{mark.lower()}_{style}.wav'
# Run the base speaker tts
src_path = f'{temp_dir}/{file_prefix}_base_{mark.lower()}_{style}.wav'
base_speaker_tts.tts(text, src_path, speaker=style, language=lang, speed=speed)
# Run the tone color converter
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)
audio_samples, sample_rate =sf.read(save_path)
return (list(audio_samples), sample_rate)
class OpenVoiceTTSV2:
@classmethod
def INPUT_TYPES(s):
audio_extensions = ["wav", "mp3", "flac"]
input_dir = folder_paths.get_input_directory()
files = []
for f in os.listdir(input_dir):
if os.path.isfile(os.path.join(input_dir, f)):
file_parts = f.lower().split('.')
if len(file_parts) > 1 and (file_parts[-1] in audio_extensions):
files.append(f)
return {
"required": {
"text": ("STRING", {"default": '', "multiline": True}),
"lang": (["EN","EN_NEWEST","FR","JP","ES","ZH","KR"],),
"speed": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.1}),
"ref_voice": (sorted(files),),
},
}
CATEGORY = "OpenVoice"
RETURN_TYPES = (any, "INT",)
RETURN_NAMES = ("AUDIO", "SAMPLE_RATE",)
FUNCTION = "inference"
def inference(self, text, lang, speed, ref_voice):
local_dir = os.path.join(folder_paths.models_dir, 'openovice', 'checkpoints_v2')
if not os.path.exists(local_dir) or not os.path.isdir(local_dir):
from huggingface_hub import snapshot_download
snapshot_download(repo_id="myshell-ai/OpenVoiceV2", local_dir=local_dir, local_dir_use_symlinks=False)
device="cuda:0" if torch.cuda.is_available() else "cpu"
model = TTS(language=lang, device=device)
speaker_key, speaker_id = list(model.hps.data.spk2id.items())[0]
ckpt_base = os.path.join(local_dir, 'base_speakers')
ckpt_converter = os.path.join(local_dir, 'converter')
tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)
tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
source_se = torch.load(f'{ckpt_base}/ses/{speaker_key.lower()}.pth', map_location=device)
reference_speaker = os.path.join(folder_paths.get_input_directory(), ref_voice)
temp_dir = folder_paths.get_temp_directory()
file_prefix = ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for _ in range(5))
target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir=temp_dir, vad=False)
save_path = f'{temp_dir}/{file_prefix}_output_v2.wav'
# Run the base speaker tts
src_path = f'{temp_dir}/{file_prefix}_base_v2.wav'
model.tts_to_file(text, speaker_id, src_path, speed=speed)
# Run the tone color converter
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)
audio_samples, sample_rate =sf.read(save_path)
return (list(audio_samples), sample_rate)
class OpenVoiceSTS:
@classmethod
def INPUT_TYPES(s):
audio_extensions = ["wav", "mp3", "flac"]
input_dir = folder_paths.get_input_directory()
files = []
for f in os.listdir(input_dir):
if os.path.isfile(os.path.join(input_dir, f)):
file_parts = f.lower().split('.')
if len(file_parts) > 1 and (file_parts[-1] in audio_extensions):
files.append(f)
return {
"required": {
"src_voice": (sorted(files),),
"ref_voice": (sorted(files),),
},
}
CATEGORY = "OpenVoice"
RETURN_TYPES = (any, "INT",)
RETURN_NAMES = ("AUDIO", "SAMPLE_RATE",)
FUNCTION = "inference"
def inference(self, src_voice, ref_voice):
local_dir = os.path.join(folder_paths.models_dir, 'openovice')
if not os.path.exists(local_dir) or not os.path.isdir(local_dir):
from huggingface_hub import snapshot_download
snapshot_download(repo_id="myshell-ai/OpenVoice", local_dir=local_dir, local_dir_use_symlinks=False)
ckpt_converter = os.path.join(local_dir, 'checkpoints/converter')
device="cuda:0" if torch.cuda.is_available() else "cpu"
tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)
tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
temp_dir = folder_paths.get_temp_directory()
file_prefix = ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for _ in range(5))
source_speaker = os.path.join(folder_paths.get_input_directory(), src_voice)
source_se, audio_name = se_extractor.get_se(source_speaker, tone_color_converter, target_dir=temp_dir, vad=True)
reference_speaker = os.path.join(folder_paths.get_input_directory(), ref_voice)
target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir=temp_dir, vad=True)
save_path = f'{temp_dir}/{file_prefix}_output_crosslingual.wav'
# Run the tone color converter
encode_message = "@MyShell"
tone_color_converter.convert(
audio_src_path=source_speaker,
src_se=source_se,
tgt_se=target_se,
output_path=save_path,
message=encode_message)
audio_samples, sample_rate =sf.read(save_path)
return (list(audio_samples), sample_rate)
NODE_CLASS_MAPPINGS = {
"D_OpenVoice_TTS" : OpenVoiceTTS,
"D_OpenVoice_TTS_V2" : OpenVoiceTTSV2,
"D_OpenVoice_STS" : OpenVoiceSTS,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"D_OpenVoice_TTS" : "Open Voice TTS",
"D_OpenVoice_TTS_V2" : "Open Voice TTS V2",
"D_OpenVoice_STS" : "Open Voice STS",
}