speech
Loading...
Searching...
No Matches
speechSynthesizerClientExample.py
1#!/usr/bin/env python3
2
3
7
8import argparse
9import time
10import yarp
11
12parser = argparse.ArgumentParser(description='speech synthesizer example')
13parser.add_argument('--remote', default='/speechSynthesizer_nws', type=str, help='remote port')
14
15args = parser.parse_args()
16
17yarp.Network.init()
18
19if not yarp.Network.checkNetwork():
20 print('Please start a YARP name server first')
21 raise SystemExit
22
23options = yarp.Property()
24options.put('device', 'speechSynthesizer_nwc_yarp')
25options.put('local', '/speechSynthesizerExample/client')
26options.put('remote', args.remote)
27
28device = yarp.PolyDriver(options)
29
30if not device.isValid():
31 print('Remote device not available')
32 raise SystemExit
33
34synthesizer = device.viewISpeechSynthesizer()
35
36if not synthesizer:
37 print('Unable to get ISpeechSynthesizer interface')
38 raise SystemExit
39
40language = yarp.SVector(1)
41
42if not synthesizer.getLanguage(language):
43 print("Failed to get current language")
44 raise SystemExit
45
46print(f"Current language: {language[0]}") # espeak: en-uk
47
48voice = yarp.SVector(1)
49
50if not synthesizer.getVoice(voice):
51 print("Failed to get current voice")
52 raise SystemExit
53
54print(f"Current voice: {voice[0]}") # espeak: english-mb-en1
55
56speed = yarp.DVector(1)
57
58if not synthesizer.getSpeed(speed):
59 print("Failed to get current speed")
60 raise SystemExit
61
62print(f"Current speed: {speed[0]}")
63
64pitch = yarp.DVector(1)
65
66if not synthesizer.getPitch(pitch):
67 print("Failed to get current pitch")
68 # not implemented in PiperSynthesizer
69
70print(f"Current pitch: {pitch[0]}")
71
72if not synthesizer.setLanguage("english"):
73 print("Failed to set language to English")
74 raise SystemExit
75
76print("Language set to English")
77
78if not synthesizer.setSpeed(1.0):
79 print("Failed to set speed to 1.0")
80 raise SystemExit
81
82print("Speed set to 1.0")
83
84text = "Hello, world! This is a test of the speech synthesizer."
85sound = yarp.Sound()
86
87if not synthesizer.synthesize(text, sound):
88 print("Failed to synthesize speech")
89 raise SystemExit
90
91time.sleep(4.0) # Wait for synthesis to complete
92
93if not synthesizer.setSpeed(0.5):
94 print("Failed to set speed to 0.5")
95 raise SystemExit
96
97print("Speed set to 0.5")
98
99if not synthesizer.synthesize(text, sound):
100 print("Failed to synthesize speech after setting speed to 0.5")
101 raise SystemExit
102
103time.sleep(6.0) # Wait for synthesis to complete
104
105if not synthesizer.setSpeed(2.0):
106 print("Failed to set speed to 2.0")
107 raise SystemExit
108
109print("Speed set to 2.0")
110
111if not synthesizer.synthesize(text, sound):
112 print("Failed to synthesize speech after setting speed to 2.0")
113 raise SystemExit
114
115time.sleep(2.0) # Wait for synthesis to complete
116
117if not synthesizer.setLanguage("spanish"):
118 print("Failed to set language to Spanish")
119 raise SystemExit
120
121print("Language set to Spanish")
122
123if not synthesizer.synthesize(text, sound):
124 print("Failed to synthesize speech in Spanish")
125 raise SystemExit
126
127time.sleep(4.0) # Wait for synthesis to complete
128
129if not synthesizer.setVoice("lessac"):
130 print("Failed to set voice to lessac")
131 raise SystemExit
132
133print("Voice set to lessac")
134
135if not synthesizer.synthesize("Oh hello there!", sound):
136 print("Failed to synthesize speech with lessac voice")
137 raise SystemExit
138
139time.sleep(2.0) # Wait for synthesis to complete
140
141device.close()