Add TTS_SPEED config for speech rate control (atempo filter in ffmpeg)

pull/26/head
Joaquin Madrid Belando 2 weeks ago
parent 159ba43924
commit bdbc97679b

@ -92,12 +92,19 @@ def _generate_tts_audio(text, lang, mp3_path):
return False
def _convert_to_wav(mp3_path, wav_path, volume_db=0):
_cmd = ['ffmpeg', '-y', '-i', mp3_path,
'-ar', '8000', '-ac', '1', '-sample_fmt', 's16']
def _convert_to_wav(mp3_path, wav_path, volume_db=0, speed=1.0):
speed = max(0.5, min(2.0, speed))
_filters = []
if speed != 1.0:
_filters.append('atempo={:.2f}'.format(speed))
logger.info('(TTS) Aplicando velocidad: x%.2f', speed)
if volume_db != 0:
_cmd += ['-af', 'volume={}dB'.format(volume_db)]
_filters.append('volume={}dB'.format(volume_db))
logger.info('(TTS) Aplicando ajuste de volumen: %ddB', volume_db)
_cmd = ['ffmpeg', '-y', '-i', mp3_path,
'-ar', '8000', '-ac', '1', '-sample_fmt', 's16']
if _filters:
_cmd += ['-af', ','.join(_filters)]
_cmd += ['-f', 'wav', wav_path]
try:
result = subprocess.run(_cmd, capture_output=True, timeout=60)
@ -298,7 +305,7 @@ def _encode_ambe_ambeserver(wav_path, ambe_path, host, port):
return True
def text_to_ambe(txt_path, ambe_path, language, vocoder_cmd, ambeserver_host='', ambeserver_port=2460, volume_db=0):
def text_to_ambe(txt_path, ambe_path, language, vocoder_cmd, ambeserver_host='', ambeserver_port=2460, volume_db=0, speed=1.0):
if not os.path.isfile(txt_path):
logger.warning('(TTS) Archivo de texto no encontrado: %s', txt_path)
return False
@ -332,7 +339,7 @@ def text_to_ambe(txt_path, ambe_path, language, vocoder_cmd, ambeserver_host='',
if not _generate_tts_audio(text, _tts_lang, _mp3_path):
return False
if not _convert_to_wav(_mp3_path, _wav_path, volume_db):
if not _convert_to_wav(_mp3_path, _wav_path, volume_db, speed):
_cleanup([_mp3_path])
return False
@ -385,6 +392,7 @@ def ensure_tts_ambe(config, tts_num):
_ambeserver_host = config['GLOBAL'].get('TTS_AMBESERVER_HOST', '')
_ambeserver_port = config['GLOBAL'].get('TTS_AMBESERVER_PORT', 2460)
_volume_db = config['GLOBAL'].get('TTS_VOLUME', -3)
_speed = config['GLOBAL'].get('TTS_SPEED', 1.0)
_txt_path = './Audio/{}/ondemand/{}.txt'.format(_lang, _file)
_ambe_path = './Audio/{}/ondemand/{}.ambe'.format(_lang, _file)
@ -403,7 +411,7 @@ def ensure_tts_ambe(config, tts_num):
logger.warning('(TTS-%d) Archivo de texto no encontrado: %s', tts_num, _txt_path)
return None
if text_to_ambe(_txt_path, _ambe_path, _lang, _vocoder_cmd, _ambeserver_host, _ambeserver_port, _volume_db):
if text_to_ambe(_txt_path, _ambe_path, _lang, _vocoder_cmd, _ambeserver_host, _ambeserver_port, _volume_db, _speed):
return _ambe_path
else:
if os.path.isfile(_ambe_path):

Loading…
Cancel
Save

Powered by TurnKey Linux.