google-cloud-speech

How to use the GC Text to Speech API - Studio Voice


UPDATE: The Studio voices are not being recognised, but still no resolution. Error below...

enter image description here

ORIGINAL:

I am trying to use the en-US-Studio-O voice however when I add this voice (among other non-standard voices) it gives error 400. I've also tried it with the non-beta endpoint (v1).

    var untext = response["generations"][0]["text"];
    console.info(text); 
    var text = "<speak>" + untext + "</speak>";
    

    const apiKey = "redacted"; // Replace with your Google Cloud API key

    const apiUrl = `https://texttospeech.googleapis.com/v1beta1/text:synthesize?key=${apiKey}`;
    const requestData = {
        input: { 'ssml': text },
        voice: { languageCode: "en-US", name: "en-US-Standard-G", ssmlGender: "NEUTRAL" },
        audioConfig: { audioEncoding: "MP3" },
    };

    $.ajax({
        url: apiUrl,
        method: "POST",
        data: JSON.stringify(requestData),
        contentType: "application/json",
        success: function (data) {
            const audioContent = data.audioContent;
            const audioUrl = `data:audio/mpeg;base64,${audioContent}`;
            $("#audioElement").attr("src", audioUrl);
            const audio = new Audio(audioUrl);
            audio.play();
        },
        error: function () {
            alert("Error occurred while processing the text.");
        },
    });

The voice should of started speaking like the standard voices but failed to do so.


Solution

  • I think your input object is in the incorrect format, Try removing the string key of the Synthesis Input param input: { ssml: text }, or input: { text: text } or just input: { text}