Skip to content

Commit

Permalink
Merge pull request #34 from robotology/set-language
Browse files Browse the repository at this point in the history
Adding set language at run time
  • Loading branch information
vtikha authored May 20, 2021
2 parents e723c17 + f421b21 commit f753fb6
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 30 deletions.
8 changes: 6 additions & 2 deletions speechInteraction/modules/googleSynthesis/app/conf/config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
name googleSynthesis
language_code en-US
sample_rate_hertz 16000
language en-US
voice en-US-Wavenet-D
pitch 0.0
speed 1.0
languageCodes en-US it-IT pt-PT fr-FR en-GB
voiceCodes en-US-Wavenet-A en-US-Wavenet-B en-US-Wavenet-C en-US-Wavenet-D en-US-Wavenet-E en-US-Wavenet-F en-US-Wavenet-G en-US-Wavenet-H en-US-Wavenet-I en-US-Wavenet-J fr-FR-Wavenet-A fr-FR-Wavenet-B fr-FR-Wavenet-C fr-FR-Wavenet-D en-GB-Wavenet-A en-GB-Wavenet-B en-GB-Wavenet-C en-GB-Wavenet-D pt-PT-Wavenet-A pt-PT-Wavenet-B pt-PT-Wavenet-C pt-PT-Wavenet-D it-IT-Wavenet-A it-IT-Wavenet-B it-IT-Wavenet-C it-IT-Wavenet-D
53 changes: 53 additions & 0 deletions speechInteraction/modules/googleSynthesis/googleSynthesis.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,57 @@ service googleSynthesis_IDL
*/
bool quit();

/**
* Say the phrase.
* @param string containing the phrase to synthesise
* @return true/false on success/failure
*/
bool say(1:string phrase);

/**
* Set the language of the synthesiser.
* example: setLanguage en-US en-US-Wavenet-A
* @param string containing the languageCode
* @param string containing the voiceCode
* @return true/false on success/failure
*/
string setLanguage(1:string languageCode, 2:string voiceCode);

/**
* Set the pitch of the synthesiser.
* @param double containing the desired pitch for the synthesiser
* @return true/false on success/failure
*/
bool setPitch(1:double pitch);

/**
* Set the speed of the synthesiser.
* @param double containing the desired speed for the synthesiser
* @return true/false on success/failure
*/
bool setSpeed(1:double speed);

/**
* Get the language code of the synthesiser
* @return string containing the language code
*/
string getLanguageCode();

/**
* Get the voice code of the synthesiser
* @return string containing the voice code
*/
string getVoiceCode();

/**
* Get the pitch value of the synthesiser
* @return string containing the pitch value
*/
double getPitch();

/**
* Get the speed value of the synthesiser
* @return string containing the speed code
*/
double getSpeed();
}
203 changes: 175 additions & 28 deletions speechInteraction/modules/googleSynthesis/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (C) 2018 iCub Facility - Istituto Italiano di Tecnologia
* Author: Vadim Tikhanoff Laura Cavaliere
* email: [email protected] [email protected]
* Author: Vadim Tikhanoff Laura Cavaliere Ilaria Carlini
* email: [email protected] [email protected] [email protected]
* Permission is granted to copy, distribute, and/or modify this program
* under the terms of the GNU General Public License, version 2 or any
* later version published by the Free Software Foundation.
Expand Down Expand Up @@ -82,6 +82,7 @@ class Processing : public yarp::os::BufferedPort<yarp::os::Bottle>
this->useCallback();
yarp::os::BufferedPort<yarp::os::Bottle >::open( "/" + moduleName + "/text:i" );
syncPort.open( "/" + moduleName + "/sync:o" );

return true;
}

Expand All @@ -93,14 +94,19 @@ class Processing : public yarp::os::BufferedPort<yarp::os::Bottle>
}

/********************************************************/
void onRead( yarp::os::Bottle &bot )
void sendDone()
{
queryGoogleSynthesis(bot);
yarp::os::Bottle syncBot;
syncBot.addString("done");
syncPort.write(syncBot);
yDebug() << "done querying google";
}
/********************************************************/
void onRead( yarp::os::Bottle &bot )
{
queryGoogleSynthesis(bot);
sendDone();
}

/********************************************************/
void queryGoogleSynthesis(yarp::os::Bottle& text)
Expand Down Expand Up @@ -170,7 +176,7 @@ class Processing : public yarp::os::BufferedPort<yarp::os::Bottle>
request.release_voice();
request.release_audio_config();

yInfo() << "\n------dgb1------\n";
yInfo() << "\n------finished google query------\n";
}

/********************************************************/
Expand All @@ -185,9 +191,61 @@ class Processing : public yarp::os::BufferedPort<yarp::os::Bottle>
return true;
}


/********************************************************/
bool setLanguageCode(const std::string &languageCode)
{
language = languageCode;
return true;
}

/********************************************************/
bool setVoiceCode(const std::string &voiceCode)
{
voice = voiceCode;
return true;
}

/********************************************************/
bool setPitch(const double pitchVal)
{
pitch = pitchVal;
return true;
}

/********************************************************/
bool setSpeed(const double speedVal)
{
speed = speedVal;
return true;
}
/********************************************************/
std::string getLanguageCode()
{
return language;
}

/********************************************************/
std::string getVoiceCode()
{
return voice;
}

/********************************************************/
double getPitch()
{
return pitch;
}

/********************************************************/
double getSpeed()
{
return speed;
}

/********************************************************/
bool checkState(std::string new_state)
{
{
if(new_state!=state){
is_changed=true;
state=new_state;
Expand All @@ -212,6 +270,9 @@ class Module : public yarp::os::RFModule, public googleSynthesis_IDL

bool closing;

std::vector<std::string> allLanguageCodes;
std::vector<std::string> allVoiceCodes;

/********************************************************/

public:
Expand All @@ -229,6 +290,24 @@ class Module : public yarp::os::RFModule, public googleSynthesis_IDL
double speed = rf.check("speed", yarp::os::Value(1.0), "speed to use (double)").asDouble();
double pitch = rf.check("pitch", yarp::os::Value(0.0), "pitch to use (double)").asDouble();

if (rf.check("languageCodes", "Getting language codes"))
{
yarp::os::Bottle &grp=rf.findGroup("languageCodes");
int sz=grp.size()-1;

for (int i=0; i<sz; i++)
allLanguageCodes.push_back(grp.get(1+i).asString());
}

if (rf.check("voiceCodes", "Getting voice codes"))
{
yarp::os::Bottle &grp=rf.findGroup("voiceCodes");
int sz=grp.size()-1;

for (int i=0; i<sz; i++)
allVoiceCodes.push_back(grp.get(1+i).asString());
}

setName(moduleName.c_str());

rpcPort.open(("/"+getName("/rpc")).c_str());
Expand All @@ -249,28 +328,15 @@ class Module : public yarp::os::RFModule, public googleSynthesis_IDL
return true;
}

bool respond(const yarp::os::Bottle& command, yarp::os::Bottle& reply) override
/************************************************************************/
bool attach(yarp::os::RpcServer &source)
{
auto cmd0=command.get(0).asString();
if (cmd0!="say")
{
reply.addString("Command not recognized, please specify \"say <sentence>\"");
return false;
}

if (command.size()>1)
{
yarp::os::Bottle sentence_bot;
sentence_bot.addString(command.get(1).asString());
processing->queryGoogleSynthesis(sentence_bot);
reply.addString("ack");
}

return yarp::os::RFModule::respond(command,reply);
return this->yarp().attachAsServer(source);
}

/**********************************************************/
bool close()
{
{
statePort.close();
processing->close();
delete processing;
Expand All @@ -283,6 +349,87 @@ class Module : public yarp::os::RFModule, public googleSynthesis_IDL
return 0.1;
}

/********************************************************/
bool say(const std::string& phrase)
{
yarp::os::Bottle bot;
bot.addString(phrase);
processing->queryGoogleSynthesis(bot);
processing->sendDone();
return true;
}

/********************************************************/
std::string setLanguage(const std::string& languageCode, const std::string& voiceCode)
{
std::string returnVal = "Error, wrong language or voice code (eg: en-US en-US-Wavenet-A)";

std::string language, voice;

for (int i = 0; i < allLanguageCodes.size(); i++)
{
if (languageCode == allLanguageCodes[i])
{
for (int v = 0; v < allVoiceCodes.size(); v++)
{
if (voiceCode == allVoiceCodes[v])
{
language = languageCode;
voice = voiceCode;
returnVal = "[ok]";
break;
}
}
break;
}
}

if(returnVal =="[ok]")
{
processing->setLanguageCode(languageCode);
processing->setVoiceCode(voiceCode);
}
return returnVal;
}

/********************************************************/
bool setPitch(const double pitchVal)
{
processing->setPitch(pitchVal);
return true;
}

/********************************************************/
bool setSpeed(const double speedVal)
{
processing->setSpeed(speedVal);
return true;
}

/********************************************************/
std::string getLanguageCode()
{
return processing->getLanguageCode();
}

/********************************************************/
std::string getVoiceCode()
{
return processing->getVoiceCode();
}

/********************************************************/
double getPitch()
{
return processing->getPitch();
}

/********************************************************/
double getSpeed()
{
return processing->getSpeed();
}

/********************************************************/
bool quit()
{
Expand All @@ -292,15 +439,15 @@ class Module : public yarp::os::RFModule, public googleSynthesis_IDL

/********************************************************/
bool updateModule()
{
{
if(is_changed){
is_changed=false;
yarp::os::Bottle &outTargets = statePort.prepare();
outTargets.clear();
yarp::os::Bottle &outTargets = statePort.prepare();
outTargets.clear();
outTargets.addString(state);
yDebug() << "outTarget:" << outTargets.toString().c_str();
statePort.write();
}
}
return !closing;
}
};
Expand Down

0 comments on commit f753fb6

Please sign in to comment.