109 lines
3.4 KiB
Python
109 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
list prompt example
|
|
"""
|
|
from __future__ import print_function, unicode_literals
|
|
|
|
from pprint import pprint
|
|
|
|
from PyInquirer import prompt, Separator
|
|
from examples import custom_style_2
|
|
|
|
|
|
import os
|
|
from copy import copy
|
|
import time
|
|
import logging as log
|
|
from .translator import llm_translator
|
|
from .text_to_speech import text_to_speech, resample
|
|
from .backend_controller.broadcaster_config import broadcaster_config
|
|
from .backend_controller.broadcaster_play_once import broadcaster_play_file
|
|
from .backend_controller.broadcaster_copy_files import copy_to_broadcaster
|
|
|
|
from .encode import encode_lc3
|
|
|
|
ANNOUNCEMENT_DIR = os.path.join(os.path.dirname(__file__), 'announcements')
|
|
SAMPLING_RATE = int(16e3)
|
|
FRAME_DUR_MS = 10
|
|
BPS = int(32e3) # TODO test 16khz 16kbps
|
|
CONFIG = {
|
|
"de": {
|
|
"file": f"{ANNOUNCEMENT_DIR}/announcement_{SAMPLING_RATE//1000}_{FRAME_DUR_MS}_{BPS//1000}_de",
|
|
"tts": 'de_DE-kerstin-low',
|
|
},
|
|
"en": {
|
|
"file": f"{ANNOUNCEMENT_DIR}/announcement_{SAMPLING_RATE//1000}_{FRAME_DUR_MS}_{BPS//1000}_en",
|
|
"tts": 'en_US-lessac-medium'
|
|
},
|
|
"fr": {
|
|
"file": f"{ANNOUNCEMENT_DIR}/announcement_{SAMPLING_RATE//1000}_{FRAME_DUR_MS}_{BPS//1000}_fr",
|
|
"tts": 'fr_FR-siwis-medium'
|
|
},
|
|
"es": {
|
|
"file": f"{ANNOUNCEMENT_DIR}/announcement_{SAMPLING_RATE//1000}_{FRAME_DUR_MS}_{BPS//1000}_es",
|
|
"tts": 'es_ES-sharvard-medium'
|
|
},
|
|
"it": {
|
|
"file": f"{ANNOUNCEMENT_DIR}/announcement_{SAMPLING_RATE//1000}_{FRAME_DUR_MS}_{BPS//1000}_it",
|
|
"tts": 'it_IT-paola-medium'
|
|
}
|
|
}
|
|
|
|
os.makedirs(ANNOUNCEMENT_DIR, exist_ok=True)
|
|
|
|
def synthesize_resample_encode(text, tts_model, output_file):
|
|
audio_dur = text_to_speech.synthesize(text, tts_model, output_file)
|
|
resample.resample(output_file, output_file, target_rate=SAMPLING_RATE)
|
|
encode_lc3.encode_lc3(output_file, bps=BPS, frame_dur_ms=FRAME_DUR_MS)
|
|
return audio_dur
|
|
|
|
def translate_from_german_and_encode(text_de):
|
|
config = copy(CONFIG)
|
|
base_lang = "de"
|
|
|
|
file = config[base_lang]["file"]
|
|
audio_dur_s [base_lang] = synthesize_resample_encode(text_de, config['de']["tts"], f'{file}.wav')
|
|
|
|
del config[base_lang]
|
|
|
|
audio_dur_s = {}
|
|
for key, val in config.items():
|
|
text = llm_translator.translate_de_to_x(key, text_de)
|
|
file = val['file']
|
|
audio_dur_s[key] = synthesize_resample_encode(text, val['tts'], f'{file}.wav')
|
|
return audio_dur_s
|
|
|
|
def announcement_from_german_text(text_de):
|
|
|
|
audio_durs = translate_from_german_and_encode(text_de)
|
|
# Transfer the files to broadcaster memory
|
|
start = time.time()
|
|
for val in CONFIG.values():
|
|
copy_to_broadcaster(f'{val["file"]}.lc3')
|
|
log.info("Transfering files to broadcaster took %s s", round(time.time() - start, 3))
|
|
|
|
# Instruct the broadcaster to stream the files
|
|
for i, d in enumerate(list(CONFIG.items())):
|
|
key, val = d
|
|
broadcaster_play_file(i, f'{os.path.basename(val["file"])}.lc3')
|
|
time.sleep(audio_durs[key] + 1)
|
|
|
|
log.info("Starting all broadcasts %s s", round(time.time() - start, 3))
|
|
|
|
|
|
# questions = [
|
|
# {
|
|
# 'type': 'list',
|
|
# 'name': 'theme',
|
|
# 'message': 'What type of annoucement would you like to make?',
|
|
# 'choices': [
|
|
# 'predefined',
|
|
# 'custom',
|
|
# 'audio'
|
|
# ]
|
|
# },
|
|
# ]
|
|
|
|
# answers = prompt(questions, style=custom_style_2)
|
|
# pprint(answers)
|