Skip to content

Instantly share code, notes, and snippets.

@jamesbulpin
Last active November 13, 2019 23:04
Show Gist options
  • Save jamesbulpin/9db6338b94c2c9a75ce5fe0aafba2399 to your computer and use it in GitHub Desktop.
Save jamesbulpin/9db6338b94c2c9a75ce5fe0aafba2399 to your computer and use it in GitHub Desktop.
var azureCognitiveServicesMainEndpoint = 'https://northeurope.tts.speech.microsoft.com/cognitiveservices/v1';
function azureCallApi(text, callback) {
var ssml = "<speak version='1.0' xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang='en-US'>" +
"<voice name='Microsoft Server Speech Text to Speech Voice (en-GB, George, Apollo)'>" +
"<prosody rate=\"slow\">" +
text +
"</prosody>" +
"</voice>" +
"</speak>";
console.log(ssml);
azureGetAuthToken(function (err, token) {
if (err) {
callback(err, token);
return;
}
var mp3file = tempfile(".mp3");
var x = fs.createWriteStream(mp3file);
var r = request({
url: azureCognitiveServicesMainEndpoint,
method: "POST",
body: ssml,
headers: {
'Content-Type' : 'application/ssml+xml',
'X-Microsoft-OutputFormat': 'audio-24khz-48kbitrate-mono-mp3',
'User-Agent': 'Talking fish',
'Authorization': 'Bearer ' + token
}
})
.on('error', function(err) {
callback(err, null);
})
.on('response', function(response) {
if (response.statusCode != 200) {
callback(response, null);
return
}
})
.pipe(x)
.on('finish', function() {
x.close();
callback(null, mp3file);
});
});
}
// TODO - fix up error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment