Prerequisites
Azure subscription - Create one for free
Create a Speech resource in the Azure portal.
Get the resource key and region. After your Speech resource is deployed, select Go to resource to view and manage keys. For more information about Cognitive Services resources, see Get the keys for your resource.
Azure Text To Speech Function in the Azure Portal
The status of the created resource can be checked by navigating to the overview tab
The keys and the endpoints of the resource can be found by navigating to "Keys and Endpoints" under the Resource Management Tab.
That covers the Azure Text to speech configuration in the Azure Portal.
Coding
Create a file named “SpeechSynthesis.js”
Install the Speech SDK for JavaScript by opening the command prompt and running the code.
npm install microsoft-cognitiveservices-speech-sdk
- Copy the following code into “SpeechSynthesis.js”
(function() {
“use strict”;
//Configure Azure Cognitive service
var sdk = require(“microsoft-cognitiveservices-speech-sdk”);
var readline = require(“readline”);
var key = “YourSubscriptionKey”;
var region = “YourServiceRegion”;
var audioFile = “YourAudioFile.wav”;
const speechConfig = sdk.SpeechConfig.fromSubscription(key, region);
const audioConfig = sdk.AudioConfig.fromAudioFileOutput(audioFile);
// The language of the voice that speaks.
speechConfig.speechSynthesisVoiceName = “en-US-JennyNeural”;
// Create the speech synthesizer.
var synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(“Enter some text that you want to speak >\n> “, function (text) {
rl.close();
// Start the synthesizer and wait for a result.
synthesizer.speakTextAsync(text,
function (result) {
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) {
console.log(“synthesis finished.”);
} else {
console.error(“Speech synthesis canceled, “ + result.errorDetails +
“\nDid you set the speech resource key and region values?”);
}
synthesizer.close();
synthesizer = null;
},
function (err) {
console.trace(“err — “ + err);
synthesizer.close();
synthesizer = null;
});
console.log(“Now synthesizing to: “ + audioFile);
});
}());
- Change “YourSubscriptionKey”, “YourServiceRegion” and “YourAudioFile” with the Speech resource key, Speech resource region, and desired output filename respectively.
In order to run your new console application, execute the code;
node SpeechSynthesis.js
Top comments (0)