-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
haruhi
committed
Sep 30, 2024
1 parent
7e7cb07
commit 9971668
Showing
14 changed files
with
258 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"files.exclude": { | ||
"**/*.rpyc": true, | ||
"**/*.rpa": true, | ||
"**/*.rpymc": true, | ||
"**/cache/": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
'voice': 27, | ||
'menu': 28, | ||
'side_character': 29, | ||
'voice_cmd':30, | ||
} | ||
|
||
# 元素映射 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
EXCEL_PARSE_START_ROW = 7 | ||
|
||
# excel解析列数 | ||
EXCEL_PARSE_START_COL = 30 | ||
EXCEL_PARSE_START_COL = 31 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
role_model_mapping = { | ||
"长门": { | ||
"gpt": "GPT_weights_v2/nagato_yuki-e15.ckpt", | ||
"sovits": "SoVITS_weights_v2/nagato_yuki_e15_s2160.pth" | ||
}, | ||
"角色2": { | ||
"gpt": "gpt_model_2_path", | ||
"sovits": "sovits_model_2_path" | ||
}, | ||
# 添加更多角色... | ||
} | ||
|
||
voice_cmd_mapping = { | ||
"voice_cmd_1": { | ||
"ref_audio_path": "path_to_reference_1.wav", | ||
"prompt_text": "Prompt text for voice_cmd_1" | ||
}, | ||
"voice_cmd_2": { | ||
"ref_audio_path": "path_to_reference_2.wav", | ||
"prompt_text": "Prompt text for voice_cmd_2" | ||
}, | ||
# 添加更多映射... | ||
} | ||
|
||
default_prompt_audio = "D:/GPT-SoVITS-v2-240821/predef_ref/正常有希/01_有希_平静.wav" | ||
default_prompt_text = "私が再び異常動作を起こさないという確証はない。" | ||
|
||
API_BASE_URL = { | ||
'base': 'http://127.0.0.1:9880/' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from collections import namedtuple | ||
|
||
from const.converter_setting import ElementColNumMapping, PositionMapping, ImageCmdMapping, TransitionMapping, \ | ||
ReplaceCharacterMapping | ||
|
||
from const.tts_setting import role_model_mapping, API_BASE_URL, voice_cmd_mapping, default_prompt_audio, default_prompt_text | ||
|
||
import requests, os | ||
|
||
class TTS(object): | ||
def __init__(self,conveter): | ||
self.conveter = conveter | ||
self.parser = conveter.parser | ||
self.last_role_name = None | ||
|
||
def filter_parsed_sheets_tts(self): | ||
parsed_sheets = self.parser.get_parsed_sheets() | ||
parsed_sheets_tts = [] | ||
|
||
current_role_name = None # 用于跟踪最近的有效 role_name | ||
|
||
for parsed_sheet in parsed_sheets: | ||
filtered_rows = [] | ||
for row in parsed_sheet.row_values: | ||
# 检查当前行的 role_name | ||
role_name = row[ElementColNumMapping.get('role_name')] | ||
if role_name.strip(): | ||
current_role_name = role_name # 更新最近的有效 role_name | ||
else: | ||
role_name = current_role_name # 如果当前 role_name 为空,使用最近的有效值 | ||
|
||
if row[ElementColNumMapping.get('voice')].strip().lower() == 'tts': | ||
# 只保留 role_name, text, 和 voice_cmd 列 | ||
filtered_row = { | ||
'role_name': role_name, | ||
'text': row[ElementColNumMapping.get('text')], | ||
'voice_cmd': row[ElementColNumMapping.get('voice_cmd')] | ||
} | ||
filtered_rows.append(filtered_row) | ||
|
||
filtered_rows.sort(key=lambda x: x['role_name']) | ||
|
||
if filtered_rows: | ||
parsed_sheets_tts.append({ | ||
'name': parsed_sheet.name, | ||
'rows': filtered_rows | ||
}) | ||
|
||
return parsed_sheets_tts | ||
|
||
|
||
def switch_models(self, role_name): | ||
|
||
# 切换到对应的GPT和SoVITS模型 | ||
|
||
if role_name == self.last_role_name: | ||
return # 如果角色名相同,则无需切换 | ||
|
||
models = role_model_mapping.get(role_name) | ||
|
||
if models: | ||
gpt_model = models['gpt'] | ||
sovits_model = models['sovits'] | ||
|
||
# 切换到对应的GPT模型 | ||
requests.get(f"{API_BASE_URL['base']}set_gpt_weights?weights_path={gpt_model}") | ||
|
||
# 切换到对应的SoVITS模型 | ||
requests.get(f"{API_BASE_URL['base']}set_sovits_weights?weights_path={sovits_model}") | ||
|
||
self.last_role_name = role_name # 更新上一个角色名 | ||
else: | ||
print(f"No model found for role: {role_name}") | ||
|
||
|
||
def synthesize_voice(self,voice_tts_sheets): | ||
for sheet_index, sheet in enumerate(voice_tts_sheets): | ||
for row_index, row in enumerate(sheet['rows']): | ||
role_name = row['role_name'] # 获取角色名 | ||
text = row['text'] # 获取文本 | ||
voice_cmd = row['voice_cmd'] # 获取语音指令 | ||
|
||
# 获取对应的 ref_audio_path 和 prompt_text | ||
audio_params = voice_cmd_mapping.get(voice_cmd, {}) | ||
ref_audio_path = audio_params.get("ref_audio_path", f"{default_prompt_audio}") # 默认值 | ||
prompt_text = audio_params.get("prompt_text", f"{default_prompt_text}") # 默认值 | ||
|
||
self.switch_models(role_name) | ||
|
||
# 发送合成请求 | ||
response = requests.post( | ||
f"{API_BASE_URL['base']}tts", | ||
json={ | ||
"text": text, | ||
"text_lang": "auto", | ||
"ref_audio_path": ref_audio_path, # 参考音频路径 | ||
"prompt_text": prompt_text, # 参考音频文本 | ||
"prompt_lang": "auto", | ||
"text_split_method": "cut0", # 可选的文本分割方法 | ||
"batch_size": 1, # 每次请求一行 | ||
} | ||
) | ||
# 确保audio文件夹存在 | ||
audio_folder = "audio" | ||
os.makedirs(audio_folder, exist_ok=True) | ||
# 处理响应 | ||
if response.status_code == 200: | ||
# 处理成功的音频流 | ||
audio_stream = response.content | ||
audio_file_path = os.path.join(audio_folder, f"{role_name}_sheet{sheet_index+1}_row{row_index+8}_synthesized.wav") | ||
with open(audio_file_path, "wb") as f: | ||
f.write(audio_stream) | ||
else: | ||
print(f"Error for {role_name}: {response.json()}") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.