mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Cleanup develop_speech()
This commit is contained in:
parent
a8cc81af9e
commit
6065f1847f
@ -10,6 +10,13 @@ from threading import Thread
|
||||
|
||||
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:
|
||||
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())')
|
||||
|
||||
|
||||
def develop_speech(text='Lucca brazzi sleeps with the fishes'):
|
||||
def develop_speech(text=SSML_SAMPLE):
|
||||
p = start_worker()
|
||||
print('\x1b[32mSpeaking', text, '\x1b[39m]]'[:-2], flush=True)
|
||||
q = Queue()
|
||||
@ -40,17 +47,25 @@ def develop_speech(text='Lucca brazzi sleeps with the fishes'):
|
||||
p.stdin.flush()
|
||||
|
||||
Thread(name='Echo', target=echo_output, args=(p,), daemon=True).start()
|
||||
exit_code = 0
|
||||
with closing(p.stdin), closing(p.stdout):
|
||||
text = text.replace('\n', ' ')
|
||||
st = 'ssml' if '<speak' in text else 'text'
|
||||
try:
|
||||
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:
|
||||
m = q.get()
|
||||
if m['payload_type'] == 'media_state_changed' and m['state'] == 'ended':
|
||||
break
|
||||
send('3 echo Synthesizer exiting')
|
||||
send('exit')
|
||||
p.wait(1)
|
||||
if m['payload_type'] == 'error' and m['related_to'] == 2:
|
||||
exit_code = 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:
|
||||
if p.poll() is None:
|
||||
p.kill()
|
||||
raise SystemExit(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user