Community Blog Buy Now
Blog

Misty Community Forum

Using Google TTS with Misty

Hi Everyone!
I am trying to use google TTS with misty but I am having difficulties with this.
I am trying to use some components from @Cameron’s conversation skills to connect it to the TTS but it does not look like it is reaching to the TTS. not sure why?

Here is the code I am trying to use,
getAcessToken is the same function from the conversation, that I am using.
below is the code I am using

GetAccessToken();

function GetAccessToken() {

    misty.SendExternalRequest("POST",  _params.getAccessTokenUrl, null, null, null, false, false, null, "application/json", "SetAccessToken");

}

function getSessionId(){

    return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);

}

function getRandomInt(max) {

  return Math.floor(Math.random() * Math.floor(max));

}

function SetAccessToken(data) {

    let response = JSON.parse(data.Result.ResponseObject.Data)

    misty.Debug("getAccessToken response: " + JSON.stringify(response));

    misty.Set("googleAccessToken", response.accessToken);

}

function SaySomething()

{

    let say = "How are you doing";

    misty.Debug(say);

    GetSpeech(say);

}

function GetSpeech(){

    

    misty.Debug(data)

    var speechParams = JSON.stringify({

        'input':{

            'text': inputText

          },

          'voice':{

            'languageCode':'en-US',

            "name": "en-US-Wavenet-C",

            "ssmlGender": "FEMALE"

          },

          'audioConfig':{

            'audioEncoding':'LINEAR16',

            "speakingRate": 1,

                "pitch": 5,

                "volumeGainDb": 0,

            "effectsProfileId": []

          }

    });

    let url = "https://texttospeech.googleapis.com/v1beta1/text:synthesize";

    let authorizationType =  "Bearer";

    let accessToken = misty.Get("googleAccessToken");

    misty.SendExternalRequest("POST", url, authorizationType, accessToken, speechParams, false, false, null, "application/json", "ProcessSpeechResults");

}

function ProcessSpeechResults(data){

    let response = JSON.parse(data.Result.ResponseObject.Data)

    misty.Debug("Speech response: " + JSON.stringify(response));

    let audioData = response.audioContent;

    misty.ChangeLED(0, 173, 239); // blue

    // this skill seems to be more reliable when we cycle through 

    // different audio files instead of using the same one each time

    let outputFilename = getRandomInt(50) + "_temp_output_audio.wav";

    misty.SaveAudio(outputFilename, audioData, true, true);

}

function getweather()

{

    misty.SendExternalRequest("GET","http://api.weatherstack.com/current?access_key="+_params.access_key+"&query="+_params.query, "null", "null", 0, false, false, "null", "application/json","_getweather");

}

    function _getweather(data) 

    {

    _data = JSON.parse(data.Result.ResponseObject.Data)

    _temperature = _data.current.temperature

    _weather_descriptions = _data.current.weather_descriptions

    _name = _data.location.name

    misty.Pause(1000);

    misty.ChangeLED(70, 200, 80);

    misty.Debug("Misty here! Just letting you know it's " + _temperature + " and " + _weather_descriptions + " in " + _name);

    misty.Speak("Misty here! Just letting you know it is " + _temperature + " and " + _weather_descriptions + " in " + _name);

    }

One thing to check is to make sure you have the Google text to speech API enabled in your Google cloud project. You should see an “APIs & Services” menu with a “Libraries” option in the left nav of the google cloud console for your project; you can select that option to search for & enable different services and APIs.

@johnathan it is enabled


It is enabled.
Do I need to have any other credentials?

No, I don’t think so.

I noticed the declaration for your GetSpeech function looks for an inputText variable, but this isn’t passed in as an argument – do you need to change function GetSpeech() to function GetSpeech(inputText)?

I tried that. But it did not worked, I will keep looking into it

Previously GetAccessToken() required that you setup a Google Cloud Function to supply the accessToken. That may have been causing the issue, if you had not set that up.

Anyway, I just updated the Misty Conversation Skill so that it does NOT require a Google Cloud Function anymore. You may want to check out the latest update and see if incorporating those changes into your own skill helps.

Cheers!