Skip to content

Instantly share code, notes, and snippets.

@jamesbulpin
Last active Nov 13, 2019
Embed
What would you like to do?
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