2023-04-16 15:56:20 +05:00
|
|
|
|
import time
|
|
|
|
|
|
|
2026-04-22 19:58:42 +03:00
|
|
|
|
import numpy as np
|
2023-04-16 16:59:18 +05:00
|
|
|
|
import sounddevice as sd
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
2026-04-22 19:58:42 +03:00
|
|
|
|
import config
|
|
|
|
|
|
import effects
|
|
|
|
|
|
|
2023-04-16 15:56:20 +05:00
|
|
|
|
language = 'ru'
|
|
|
|
|
|
model_id = 'ru_v3'
|
2023-04-16 16:59:18 +05:00
|
|
|
|
sample_rate = 48000 # 48000
|
|
|
|
|
|
speaker = 'aidar' # aidar, baya, kseniya, xenia, random
|
2023-04-16 15:56:20 +05:00
|
|
|
|
put_accent = True
|
|
|
|
|
|
put_yo = True
|
2023-04-16 16:59:18 +05:00
|
|
|
|
device = torch.device('cpu') # cpu или gpu
|
2023-04-16 15:56:20 +05:00
|
|
|
|
text = "Хауди Хо, друзья!!!"
|
|
|
|
|
|
|
|
|
|
|
|
model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models',
|
|
|
|
|
|
model='silero_tts',
|
|
|
|
|
|
language=language,
|
|
|
|
|
|
speaker=model_id)
|
|
|
|
|
|
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-22 19:58:42 +03:00
|
|
|
|
def _post_process(audio):
|
|
|
|
|
|
if not getattr(config, 'TTS_EFFECTS_ENABLED', False):
|
|
|
|
|
|
return audio
|
|
|
|
|
|
|
|
|
|
|
|
arr = np.asarray(audio, dtype=np.float32)
|
|
|
|
|
|
low = getattr(config, 'TTS_BANDPASS_LOW_HZ', 0)
|
|
|
|
|
|
high = getattr(config, 'TTS_BANDPASS_HIGH_HZ', 0)
|
|
|
|
|
|
bandpass = (low, high) if low and high and high > low else None
|
|
|
|
|
|
|
|
|
|
|
|
wet = float(getattr(config, 'TTS_REVERB_WET', 0.0))
|
|
|
|
|
|
decay = int(getattr(config, 'TTS_REVERB_DECAY_MS', 0))
|
|
|
|
|
|
reverb = (wet, decay) if wet > 0 and decay > 0 else None
|
|
|
|
|
|
|
|
|
|
|
|
pitch = int(getattr(config, 'TTS_PITCH_SEMITONES', 0))
|
|
|
|
|
|
|
|
|
|
|
|
return effects.process(arr, sample_rate,
|
|
|
|
|
|
bandpass=bandpass,
|
|
|
|
|
|
reverb=reverb,
|
|
|
|
|
|
pitch_semitones=pitch)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def synthesize(what: str):
|
2023-04-16 16:59:18 +05:00
|
|
|
|
audio = model.apply_tts(text=what + "..",
|
2023-04-16 15:56:20 +05:00
|
|
|
|
speaker=speaker,
|
|
|
|
|
|
sample_rate=sample_rate,
|
|
|
|
|
|
put_accent=put_accent,
|
|
|
|
|
|
put_yo=put_yo)
|
2026-04-22 19:58:42 +03:00
|
|
|
|
return _post_process(audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def va_speak(what: str):
|
|
|
|
|
|
audio = synthesize(what)
|
2023-04-16 15:56:20 +05:00
|
|
|
|
|
|
|
|
|
|
sd.play(audio, sample_rate * 1.05)
|
|
|
|
|
|
time.sleep((len(audio) / sample_rate) + 0.5)
|
|
|
|
|
|
sd.stop()
|