File size: 2,658 Bytes
02e90e4 01e655b d2b7e94 01e655b d2b7e94 01e655b 4c19a5a 02e90e4 01e655b d5d0921 01e655b 02e90e4 01e655b |
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 |
from typing import Any, Union
from pydantic import BaseModel
from pydub import AudioSegment
from modules.data import styles_mgr
from modules.speaker import speaker_mgr
from modules.ssml import merge_prompt
class ParamsTypeError(Exception):
pass
class BaseResponse(BaseModel):
message: str
data: Any
def success_response(data: Any, message: str = "ok") -> BaseResponse:
return BaseResponse(message=message, data=data)
def wav_to_mp3(wav_data, bitrate="48k"):
audio: AudioSegment = AudioSegment.from_wav(
wav_data,
)
return audio.export(format="mp3", bitrate=bitrate)
def to_number(value, t, default=0):
try:
number = t(value)
return number
except (ValueError, TypeError) as e:
return default
def calc_spk_style(spk: Union[str, int], style: Union[str, int]):
voice_attrs = {
"spk": None,
"prompt1": None,
"prompt2": None,
"prefix": None,
"temperature": None,
}
params = {}
if type(spk) == int:
voice_attrs["spk"] = spk
elif type(spk) == str:
if spk.isdigit():
voice_attrs["spk"] = int(spk)
else:
spker = speaker_mgr.get_speaker(spk)
if spker:
voice_attrs["spk"] = spker
if type(style) == int or type(style) == float:
raise ParamsTypeError("The style parameter cannot be a number.")
elif type(style) == str and style != "":
if style.isdigit():
raise ParamsTypeError("The style parameter cannot be a number.")
else:
style_params = styles_mgr.find_params_by_name(style)
for k, v in style_params.items():
params[k] = v
voice_attrs = {k: v for k, v in voice_attrs.items() if v is not None}
merge_prompt(voice_attrs, params)
voice_attrs["spk"] = params.get("spk", voice_attrs.get("spk", None))
voice_attrs["temperature"] = params.get(
"temp", voice_attrs.get("temperature", None)
)
voice_attrs["prefix"] = params.get("prefix", voice_attrs.get("prefix", None))
voice_attrs["prompt1"] = params.get("prompt1", voice_attrs.get("prompt1", None))
voice_attrs["prompt2"] = params.get("prompt2", voice_attrs.get("prompt2", None))
if voice_attrs.get("temperature", "") == "min":
# ref: https://github.com/2noise/ChatTTS/issues/123#issue-2326908144
voice_attrs["temperature"] = 0.000000000001
if voice_attrs.get("temperature", "") == "max":
voice_attrs["temperature"] = 1
voice_attrs = {k: v for k, v in voice_attrs.items() if v is not None}
# print(voice_attrs)
return voice_attrs
|