-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_to_speech_objc.py
executable file
·67 lines (54 loc) · 2.24 KB
/
text_to_speech_objc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import caproto
from caproto.server import pvproperty, PVGroup, ioc_arg_parser, run
import speech
from objc_util import ObjCClass
AVSpeechUtterance = ObjCClass('AVSpeechUtterance')
AVSpeechSynthesizer = ObjCClass('AVSpeechSynthesizer')
AVSpeechSynthesisVoice = ObjCClass('AVSpeechSynthesisVoice')
class SpeechIOC(PVGroup):
language = pvproperty(value=['en-US'],
doc='Language to use',
dtype=caproto.ChannelType.ENUM,
enum_strings=list(speech.get_languages()),
string_encoding='utf-8')
speak = pvproperty(value=['text'],
doc='Text to speak',
string_encoding='utf-8')
rate = pvproperty(value=[0.5],
doc='Normalized speech rate')
speaking = pvproperty(value=[0])
@speak.startup
async def speak(self, instance, async_lib):
self.voices = AVSpeechSynthesisVoice.speechVoices()
self.synthesizer = AVSpeechSynthesizer.new()
@speak.putter
async def speak(self, instance, value):
if isinstance(value, (list, tuple)):
value, = value
language = self.language.value[0]
voice = AVSpeechSynthesisVoice.voiceWithLanguage_(language)
for voice in self.voices:
if (f'Language: {language}' in str(voice) and
'compact' not in str(voice)):
print('Chose voice:', voice)
break
self.voice = voice
utterance = AVSpeechUtterance.speechUtteranceWithString_(value)
rate = self.rate.value[0]
utterance.rate = rate
utterance.voice = self.voice
utterance.useCompactVoice = False
print(f'Saying {value!r} in {language} at rate {rate}')
self.synthesizer.speakUtterance_(utterance)
@speaking.startup
async def speaking(self, instance, async_lib):
while True:
await self.speaking.write(value=[speech.is_speaking()])
await async_lib.library.sleep(0.1)
if __name__ == '__main__':
ioc_options, run_options = ioc_arg_parser(
default_prefix='speech:',
desc="Text-to-speech IOC")
ioc = SpeechIOC(**ioc_options)
run(ioc.pvdb, **run_options)