6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
22namespace texttospeech = ::google::cloud::texttospeech_v1;
39 if(m_synthVoices.size() == 0)
41 yCError(
GOOGLESPEECHSYNTH) <<
"You haven't set the language code yet. Voice name cannot be set without a language code";
44 google::protobuf::RepeatedPtrField<google::cloud::texttospeech::v1::Voice>::iterator
it;
45 for(
it = m_synthVoices.begin();
it != m_synthVoices.end();
it++)
57std::shared_ptr<google::cloud::texttospeech_v1::TextToSpeechClient> GoogleSpeechSynthesizer::_getClient(google::cloud::Options
opts)
59 return std::make_shared<texttospeech::TextToSpeechClient>(texttospeech::MakeTextToSpeechConnection(
opts));
65 google::cloud::Options
opts;
68 opts.set<google::cloud::GrpcChannelArgumentsOption>({
69 {
"grpc.initial_reconnect_backoff_ms",
"1000"},
70 {
"grpc.min_reconnect_backoff_ms",
"1000"},
71 {
"grpc.max_reconnect_backoff_ms",
"5000"},
72 {
"grpc.http2.min_ping_interval_without_data_ms",
"30000"},
73 {
"grpc.keepalive_timeout_ms",
"10000"},
76 google::cloud::StatusOr<google::cloud::texttospeech::v1::ListVoicesResponse> response = _getClient(
opts)->ListVoices(
language_code);
78 yCError(
GOOGLESPEECHSYNTH) <<
"Error in getting the list of available voices. Google status:\n\t" << response.status().message() <<
"\n";
88 if(config.
check(
"__offline"))
90 m_offline = config.
find(
"__offline").asInt32() == 1;
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>();
122 if(m_defaultVoicesMap.find(
lang_code) != m_defaultVoicesMap.end())
135 if(config.
check(
"voice_name"))
152 m_synthAudioConfig->set_audio_encoding(google::cloud::texttospeech::v1::MP3);
166 m_synthVoiceSelParams->set_language_code(language);
169 if(language ==
"auto")
175 if(language == m_synthVoiceSelParams->language_code() && m_synthVoices.size() != 0)
182 if(!_getVoicesList(language, m_synthVoices))
187 m_synthVoiceSelParams->set_language_code(language);
188 if(m_defaultVoicesMap.find(language) == m_defaultVoicesMap.end())
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();
195 yCInfo(
GOOGLESPEECHSYNTH) <<
"Setting the voice name to the default voice for the selected language code:" << m_defaultVoicesMap[language];
205 language = m_synthVoiceSelParams->language_code();
214 m_synthVoiceSelParams->set_name(m_synthVoices[0].name());
215 yCInfo(
GOOGLESPEECHSYNTH) <<
"auto option selected. Setting the voice name to:" << m_synthVoiceSelParams->name();
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());
253 for(
const auto& voice : m_synthVoices)
263 m_synthAudioConfig->set_speaking_rate(speed);
275 speed = m_synthAudioConfig->speaking_rate();
282 m_synthAudioConfig->set_pitch(pitch);
294 pitch = m_synthAudioConfig->pitch();
301 m_synthInput->set_text(text);
305 google::cloud::Options
opts;
308 opts.set<google::cloud::GrpcChannelArgumentsOption>({
309 {
"grpc.initial_reconnect_backoff_ms",
"1000"},
310 {
"grpc.min_reconnect_backoff_ms",
"1000"},
311 {
"grpc.max_reconnect_backoff_ms",
"5000"},
312 {
"grpc.http2.min_ping_interval_without_data_ms",
"30000"},
313 {
"grpc.keepalive_timeout_ms",
"10000"},
316 google::cloud::StatusOr<google::cloud::texttospeech::v1::SynthesizeSpeechResponse> response = _getClient(
opts)->SynthesizeSpeech(*m_synthInput,*m_synthVoiceSelParams,*m_synthAudioConfig);
318 yCError(
GOOGLESPEECHSYNTH) <<
"Error synthesizing speech. Google status:\n\t" << response.status().message() <<
"\n";
const yarp::os::LogComponent & GOOGLESPEECHSYNTH()
const std::pair PITCH_RANGE
const std::pair SPEED_RANGE
std::vector< std::string > m_default_voices
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
std::string m_language_code
GoogleSpeechSynthesizer()
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.
A base class for nested structures that can be searched.
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.
size_t getChannels() const
Get the number of channels of the sound.
size_t getSamples() const
Get the number of samples contained in the sound.
#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.
bool read_bytestream(Sound &data, const char *bytestream, size_t streamsize, std::string format)
Read a sound from a byte array.