feat: 后端封装tts,可以通过配置文件切换openvoice和vits
This commit is contained in:
1 parent
e2f3decfae
commit
ce033dca2b
6 files changed
+49
-62
No files matched your search
@@ -164,11 +164,14 @@ class TextToSpeech:
|
||||
"""
|
||||
return audio_data.cpu().detach().float().numpy()
|
||||
|
||||
def numpy2bytes(self, audio_data: np.ndarray):
|
||||
"""
|
||||
numpy类型转bytes
|
||||
"""
|
||||
return (audio_data*32768.0).astype(np.int32).tobytes()
|
||||
def numpy2bytes(self, audio_data):
|
||||
if isinstance(audio_data, np.ndarray):
|
||||
if audio_data.dtype == np.dtype('float32'):
|
||||
audio_data = np.int16(audio_data * np.iinfo(np.int16).max)
|
||||
audio_data = audio_data.tobytes()
|
||||
return audio_data
|
||||
else:
|
||||
raise TypeError("audio_data must be a numpy array")
|
||||
|
||||
def _base_tts(self,
|
||||
text: str,
|
||||
@@ -292,14 +295,11 @@ class TextToSpeech:
|
||||
|
||||
def synthesize(self,
|
||||
text: str,
|
||||
sdp_ratio=0.2,
|
||||
noise_scale=0.6,
|
||||
noise_scale_w=0.8,
|
||||
speed=1.0,
|
||||
quite=True,
|
||||
|
||||
tts_info,
|
||||
source_se: Optional[np.ndarray]=None,
|
||||
target_se: Optional[np.ndarray]=None,
|
||||
sdp_ratio=0.2,
|
||||
quite=True,
|
||||
tau :float=0.3,
|
||||
message :str="default"):
|
||||
"""
|
||||
@@ -316,11 +316,11 @@ class TextToSpeech:
|
||||
"""
|
||||
audio, sr = self._base_tts(text,
|
||||
sdp_ratio=sdp_ratio,
|
||||
noise_scale=noise_scale,
|
||||
noise_scale_w=noise_scale_w,
|
||||
speed=speed,
|
||||
noise_scale=tts_info['noise_scale'],
|
||||
noise_scale_w=tts_info['noise_scale_w'],
|
||||
speed=tts_info['speed'],
|
||||
quite=quite)
|
||||
if self.use_tone_convert:
|
||||
if self.use_tone_convert and target_se.size>0:
|
||||
tts_sr = self.base_tts_model.hps.data.sampling_rate
|
||||
converter_sr = self.tone_color_converter.hps.data.sampling_rate
|
||||
audio = F.resample(audio, tts_sr, converter_sr)
|
||||
@@ -342,4 +342,5 @@ class TextToSpeech:
|
||||
save_path: 保存路径
|
||||
"""
|
||||
sf.write(save_path, audio, sample_rate)
|
||||
print(f"Audio saved to {save_path}")
|
||||
print(f"Audio saved to {save_path}")
|
||||
|
||||
Reference in new issue
Block a user