Cleanup develop_speech()

This commit is contained in:
Kovid Goyal 2023-01-25 06:45:06 +05:30
parent a8cc81af9e
commit 6065f1847f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -10,6 +10,13 @@ from threading import Thread
from calibre.utils.ipc.simple_worker import start_pipe_worker from calibre.utils.ipc.simple_worker import start_pipe_worker
SSML_SAMPLE = '''
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="en-US">
<voice name="en-US-AriaNeural">
We are selling <bookmark mark='flower_1'/>roses and <bookmark mark='flower_2'/>daisies.
</voice>
</speak>
'''
def decode_msg(line: bytes) -> dict: def decode_msg(line: bytes) -> dict:
parts = line.strip().split(b' ', 2) parts = line.strip().split(b' ', 2)
@ -23,7 +30,7 @@ def start_worker():
return start_pipe_worker('from calibre_extensions.winspeech import run_main_loop; raise SystemExit(run_main_loop())') return start_pipe_worker('from calibre_extensions.winspeech import run_main_loop; raise SystemExit(run_main_loop())')
def develop_speech(text='Lucca brazzi sleeps with the fishes'): def develop_speech(text=SSML_SAMPLE):
p = start_worker() p = start_worker()
print('\x1b[32mSpeaking', text, '\x1b[39m]]'[:-2], flush=True) print('\x1b[32mSpeaking', text, '\x1b[39m]]'[:-2], flush=True)
q = Queue() q = Queue()
@ -40,17 +47,25 @@ def develop_speech(text='Lucca brazzi sleeps with the fishes'):
p.stdin.flush() p.stdin.flush()
Thread(name='Echo', target=echo_output, args=(p,), daemon=True).start() Thread(name='Echo', target=echo_output, args=(p,), daemon=True).start()
exit_code = 0
with closing(p.stdin), closing(p.stdout): with closing(p.stdin), closing(p.stdout):
text = text.replace('\n', ' ')
st = 'ssml' if '<speak' in text else 'text'
try: try:
send('1 echo Synthesizer started') send('1 echo Synthesizer started')
send('2 speak text inline', text) send('1 volume 0.1')
send(f'2 speak {st} inline', text)
while True: while True:
m = q.get() m = q.get()
if m['payload_type'] == 'media_state_changed' and m['state'] == 'ended': if m['payload_type'] == 'media_state_changed' and m['state'] == 'ended':
break break
send('3 echo Synthesizer exiting') if m['payload_type'] == 'error' and m['related_to'] == 2:
send('exit') exit_code = 1
p.wait(1) break
send(f'3 echo Synthesizer exiting with exit code: {exit_code}')
send(f'4 exit {exit_code}')
raise SystemExit(p.wait(1))
finally: finally:
if p.poll() is None: if p.poll() is None:
p.kill() p.kill()
raise SystemExit(1)