YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
GoogleSpeechSynthesizer.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
8#endif
9
11
12#include <yarp/sig/SoundFile.h>
13
15#include <yarp/os/LogStream.h>
16#include <fstream>
17
18#include <cmath>
19
20using namespace yarp::os;
21using namespace yarp::dev;
22namespace texttospeech = ::google::cloud::texttospeech_v1;
23
24
26
27
32
34{
35 if(m_offline)
36 {
37 return true;
38 }
39 if(m_synthVoices.size() == 0)
40 {
41 yCError(GOOGLESPEECHSYNTH) << "You haven't set the language code yet. Voice name cannot be set without a language code";
42 }
43
44 google::protobuf::RepeatedPtrField<google::cloud::texttospeech::v1::Voice>::iterator it;
45 for(it = m_synthVoices.begin(); it != m_synthVoices.end(); it++)
46 {
47 if(it->name() == voice_name)
48 {
49 return true;
50 }
51 }
52
53 yCError(GOOGLESPEECHSYNTH) << "Unsupported voice name";
54 return false;
55}
56
57std::shared_ptr<google::cloud::texttospeech_v1::TextToSpeechClient> GoogleSpeechSynthesizer::_getClient(google::cloud::Options opts)
58{
59 return std::make_shared<texttospeech::TextToSpeechClient>(texttospeech::MakeTextToSpeechConnection(opts));
60}
61
62 yarp::dev::ReturnValue GoogleSpeechSynthesizer::_getVoicesList(std::string language_code, google::protobuf::RepeatedPtrField<google::cloud::texttospeech::v1::Voice>& voices_list)
63{
64 // Configure client with timeout options
65 google::cloud::Options opts;
66 // Connection timeout (initial connection establishment)
67 // This is what you want to be short for detecting no internet
68 opts.set<google::cloud::GrpcChannelArgumentsOption>({
69 {"grpc.initial_reconnect_backoff_ms", "1000"}, // 1 second
70 {"grpc.min_reconnect_backoff_ms", "1000"},
71 {"grpc.max_reconnect_backoff_ms", "5000"}, // 5 seconds max
72 {"grpc.http2.min_ping_interval_without_data_ms", "30000"},
73 {"grpc.keepalive_timeout_ms", "10000"}, // 10 seconds
74 });
75
76 google::cloud::StatusOr<google::cloud::texttospeech::v1::ListVoicesResponse> response = _getClient(opts)->ListVoices(language_code);
77 if (!response) {
78 yCError(GOOGLESPEECHSYNTH) << "Error in getting the list of available voices. Google status:\n\t" << response.status().message() << "\n";
80 }
81
82 voices_list = response->voices();
84}
85
87{
88 if(config.check("__offline"))
89 {
90 m_offline = config.find("__offline").asInt32() == 1;
91 }
92
93 if (!parseParams(config))
94 {
95 yCError(GOOGLESPEECHSYNTH) << "Failed to parse parameters";
96 return false;
97 }
98
99 // m_synthClient = std::make_shared<texttospeech::TextToSpeechClient>(texttospeech::MakeTextToSpeechConnection());
100 m_synthVoiceSelParams = std::make_shared<google::cloud::texttospeech::v1::VoiceSelectionParams>();
101 m_synthInput = std::make_shared<google::cloud::texttospeech::v1::SynthesisInput>();
102 m_synthAudioConfig = std::make_shared<google::cloud::texttospeech::v1::AudioConfig>();
103
104 // Populate default voices map
105 for(const auto& voice : m_default_voices)
106 {
107 std::string lang_code;
108 size_t first_dash = voice.find('-');
109 if (first_dash == std::string::npos) {
110 // No dash found: fallback to using the whole name
111 lang_code = voice;
112 } else {
113 size_t second_dash = voice.find('-', first_dash + 1);
114 if (second_dash == std::string::npos) {
115 // Only one dash: language code is the part before it
116 lang_code = voice.substr(0, first_dash);
117 } else {
118 // Two or more dashes: language code is up to the second dash
119 lang_code = voice.substr(0, second_dash);
120 }
121 }
122 if(m_defaultVoicesMap.find(lang_code) != m_defaultVoicesMap.end())
123 {
124 yCWarning(GOOGLESPEECHSYNTH) << "The default voice for language code" << lang_code << "was already set to" << m_defaultVoicesMap[lang_code] << ". Overriding it with the new value:" << voice;
125 continue;
126 }
127 m_defaultVoicesMap[lang_code] = voice;
128 }
129
130 if(!setLanguage(m_language_code) && !m_offline)
131 {
132 return false;
133 }
134
135 if(config.check("voice_name"))
136 {
137 if(!setVoice(config.find("voice_name").asString()))
138 {
139 return false;
140 }
141 };
142
144 {
145 return false;
146 }
148 {
149 return false;
150 }
151
152 m_synthAudioConfig->set_audio_encoding(google::cloud::texttospeech::v1::MP3);
153
154 return true;
155}
156
158{
159 return true;
160}
161
163{
164 if(m_offline)
165 {
166 m_synthVoiceSelParams->set_language_code(language);
168 }
169 if(language == "auto")
170 {
171 yCError(GOOGLESPEECHSYNTH) << "The \"auto\" option is not supported by this device";
172
174 }
175 if(language == m_synthVoiceSelParams->language_code() && m_synthVoices.size() != 0)
176 {
177 yCWarning(GOOGLESPEECHSYNTH) << "The language code is already set to:" << language;
179 }
180 std::string start_voice;
181
182 if(!_getVoicesList(language, m_synthVoices))
183 {
184 yCError(GOOGLESPEECHSYNTH) << "Error while getting the list of available voices for language code:" << language;
186 }
187 m_synthVoiceSelParams->set_language_code(language);
188 if(m_defaultVoicesMap.find(language) == m_defaultVoicesMap.end())
189 {
190 yCInfo(GOOGLESPEECHSYNTH) << "The provided language code is not supported. Setting the voice name to the first available voice for the selected language code:" << m_synthVoices[0].name();
191 start_voice = m_synthVoices[0].name();
192 }
193 else
194 {
195 yCInfo(GOOGLESPEECHSYNTH) << "Setting the voice name to the default voice for the selected language code:" << m_defaultVoicesMap[language];
196 start_voice = m_defaultVoicesMap[language];
197 }
198
199 return setVoice(start_voice);
200
201}
202
204{
205 language = m_synthVoiceSelParams->language_code();
206
208}
209
211{
212 if(voice_name == "auto")
213 {
214 m_synthVoiceSelParams->set_name(m_synthVoices[0].name());
215 yCInfo(GOOGLESPEECHSYNTH) << "auto option selected. Setting the voice name to:" << m_synthVoiceSelParams->name();
216
218 }
219
220 if(m_offline)
221 {
222 m_synthVoiceSelParams->set_name(voice_name);
224 }
225
227 {
228 yCInfo(GOOGLESPEECHSYNTH) << "Unsupported voice name:" << voice_name;
230 {
231 yCInfo(GOOGLESPEECHSYNTH) << "Setting the voice name to the first available one for the selected language code:" << m_synthVoices[0].name();
232 m_synthVoiceSelParams->set_name(m_synthVoices[0].name());
234 }
236 }
237
238 m_synthVoiceSelParams->set_name(voice_name);
239
241}
242
249
251{
252 voices_list.clear();
253 for(const auto& voice : m_synthVoices)
254 {
255 voices_list.push_back(voice.name());
256 }
257
259}
260
262{
263 m_synthAudioConfig->set_speaking_rate(speed);
265 {
266 yCError(GOOGLESPEECHSYNTH) << "Error while setting the speach rate (speed). The value is outside the allowed range [" << SPEED_RANGE.first << SPEED_RANGE.second << "]";
268 }
269
271}
272
274{
275 speed = m_synthAudioConfig->speaking_rate();
276
278}
279
281{
282 m_synthAudioConfig->set_pitch(pitch);
284 {
285 yCError(GOOGLESPEECHSYNTH) << "Error while setting the speach pitch. The value is outside the allowed range [" << PITCH_RANGE.first << PITCH_RANGE.second << "]";
287 }
288
290}
291
293{
294 pitch = m_synthAudioConfig->pitch();
295
297}
298
300{
301 m_synthInput->set_text(text);
302 yCDebug(GOOGLESPEECHSYNTH) << "Synthesizing text:" << text;
303
304 // Configure client with timeout options
305 google::cloud::Options opts;
306 // Connection timeout (initial connection establishment)
307 // This is what you want to be short for detecting no internet
308 opts.set<google::cloud::GrpcChannelArgumentsOption>({
309 {"grpc.initial_reconnect_backoff_ms", "1000"}, // 1 second
310 {"grpc.min_reconnect_backoff_ms", "1000"},
311 {"grpc.max_reconnect_backoff_ms", "5000"}, // 5 seconds max
312 {"grpc.http2.min_ping_interval_without_data_ms", "30000"},
313 {"grpc.keepalive_timeout_ms", "10000"}, // 10 seconds
314 });
315
316 google::cloud::StatusOr<google::cloud::texttospeech::v1::SynthesizeSpeechResponse> response = _getClient(opts)->SynthesizeSpeech(*m_synthInput,*m_synthVoiceSelParams,*m_synthAudioConfig);
317 if (!response) {
318 yCError(GOOGLESPEECHSYNTH) << "Error synthesizing speech. Google status:\n\t" << response.status().message() << "\n";
320 }
321
322 sound.clear();
323 if(!yarp::sig::file::read_bytestream(sound, response->audio_content().data(), response->audio_content().size(), ".mp3"))
324 {
325 yCError(GOOGLESPEECHSYNTH) << "Error while transfering data from google response to yarp::sigSound sound object";
327 }
328 yCDebug(GOOGLESPEECHSYNTH) << "Synthesized sound channels:" << sound.getChannels() << "channels";
329 yCDebug(GOOGLESPEECHSYNTH) << "Synthesized sound length:" << sound.getSamples() << "samples";
330
332}
const yarp::os::LogComponent & GOOGLESPEECHSYNTH()
const std::pair PITCH_RANGE
const std::pair SPEED_RANGE
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool _voiceSupported(const std::string &voice_name)
Checks whether or not a particular voice name is supported by the currently selected language code.
yarp::dev::ReturnValue getPitch(double &pitch) override
Gets the current pitch set for speech synthesis.
yarp::dev::ReturnValue getLanguage(std::string &language) override
Gets the current language set for speech synthesis.
yarp::dev::ReturnValue setSpeed(const double speed=0) override
Sets the voice speed for speech synthesis.
yarp::dev::ReturnValue getAvailableVoices(yarp::sig::VectorOfString &voices_list) override
yarp::dev::ReturnValue getSpeed(double &speed) override
Gets the current voice speed.
yarp::dev::ReturnValue setPitch(const double pitch) override
Sets the pitch for speech synthesis.
yarp::dev::ReturnValue setVoice(const std::string &voice_name="auto") override
Sets the voice set for speech synthesis.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue setLanguage(const std::string &language="auto") override
Sets the language for speech synthesis.
yarp::dev::ReturnValue synthesize(const std::string &text, yarp::sig::Sound &sound) override
Performs the speech synthesis.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue getVoice(std::string &voice_name) override
Gets the current voice set for speech synthesis.
@ return_value_error_generic
Method was successfully executed.
A mini-server for performing network communication in the background.
@ TraceType
Definition Log.h:92
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:42
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:669
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:664
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
bool read_bytestream(Sound &data, const char *bytestream, size_t streamsize, std::string format)
Read a sound from a byte array.
Definition SoundFile.cpp:56