Emmanuel Izzy
Emmanuel Izzy

Reputation: 11

Azure Text to Speech API Audio Output is Not Playing

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.

Upvotes: 1

Views: 769

Answers (2)

Peter  Adriaenssens
Peter Adriaenssens

Reputation: 114

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?

Upvotes: 1

Pavan
Pavan

Reputation: 1349

I created speech service in azure portal.

enter image description here

I created a Google Spread Sheet and named it TextToSpeech. Add a trigger to the following Spread Sheet.

Code:

function sendTextToAzureTTS() {
  // Azure Text to Speech region and access key
  var region = "eastus"; // Replace with your Azure region
  var apiKey = "your-api-key"; // Replace with your Azure access key
  var endpoint = "your speech servive endpoint";

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TextToSpeech");
  var text = sheet.getRange("B1").getValue();

  var requestData = {
    method: "POST",
    contentType: "application/ssml+xml",
    headers: {
      "Ocp-Apim-Subscription-Key": apiKey,
      "X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3",
    },
    payload: '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US"><voice name="en-US-AriaNeural">' + text + '</voice></speak>',
  };

  var response = UrlFetchApp.fetch(endpoint, requestData);

  if (response.getResponseCode() == 200) {

    var audioData = response.getBlob().getBytes();
    var audioFile = DriveApp.createFile('output_audio.mp3', audioData, "audio/mpeg");
    var audioUrl = audioFile.getUrl();

    // Write the audio URL to the same sheet in cell B2
    sheet.getRange("B2").setValue(audioUrl);
  } else {
    Logger.log("Error: " + response.getResponseCode() + " - " + response.getContentText());
  }
}

  • The above code is placed in the script editor, and I added my Speech Service API key to the above code.
  • I gave a text in cell B1, which I want to convert into audio.
  • When I ran my script, it executed successfully.
    Then I was able to get the audio file URL in cell B2.

enter image description here

Output: enter image description here

enter image description here

Upvotes: 1

Related Questions