I’m new to Azure, and programming in general.
BACKGROUND
I’m building a Google Sheet App Script that sends the content of Sheet2!B1 to Azure Speech API and output the resulting voice to Sheet2!B2. However, despite using the X-Microsoft-OutputFormat
as riff-48khz-16bit-mono-pcm
, the output audio file doesn’t play. I tried changing the output to audio-24khz-160kbitrate-mono-mp3
based on the answer I saw here but it doesn’t work either. I’ve tried changing the file type to .wav too, which didn’t work too.
Here’s the code. Can you suggest what I should do, please? Also, any tip on creating batch api requests is appreciated. Thank you.
function sendTextToAzureTTS() {
// Azure Text to Speech endpoint and API key
var endpoint = "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1";
var apiKey = "hidden";
// Get the plain text from Sheet2 B1
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var text = sheet.getRange("B1").getValue();
// Create the request payload
var requestData = {
method: "POST",
contentType: "application/ssml+xml",
headers: {
"Ocp-Apim-Subscription-Key": apiKey,
"X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3", // Use the supported format
},
payload: '<speak version="1.0" xml:lang="en-US"><voice xml:lang="en-US" xml:gender="Female" name="en-US-EmmaNeural">' + text + '</voice></speak>',
};
// Make the POST request to Azure Text to Speech
var response = UrlFetchApp.fetch(endpoint, requestData);
// Check if the response was successful
if (response.getResponseCode() == 200) {
// Get the audio data from the response
var audioData = response.getBlob().getBytes();
// Write the audio data to a file in Google Drive with the correct MIME type
var audioFile = DriveApp.createFile('otuput_audio.mp3', audioData, "audio/mp3");
// Get the URL of the created audio file
var audioUrl = audioFile.getUrl();
// Write the audio URL to Sheet2 B2
sheet.getRange("B2").setValue(audioUrl);
} else {
Logger.log("Error: " + response.getResponseCode() + " - " + response.getContentText());
}
}
I tried to create azure speech post requests and get the audio file back via Google Sheet app script. However, the audio file is not playing. I expected a working audio file, but the audio file is not playing.
2
Answers
I created speech service in azure portal.
I created a Google Spread Sheet and named it
TextToSpeech
. Add a trigger to the following Spread Sheet.Code:
Then I was able to get the audio file URL in cell B2.
Output:
In your SSML code you use the the attribute xml:gender="Female" in the SSML.
This is an unknow attribute in Azure SSML and gives an error. I tested this
Pavan use in his example not the attibute Gender in his SSML code.
It’s possible it’s just this little thing and the other code is perfect?