Skip to content

Instantly share code, notes, and snippets.

@BettyJJ
Created January 23, 2024 08:37
Show Gist options
  • Save BettyJJ/14a7f532b4417d257a678066c4b83534 to your computer and use it in GitHub Desktop.
Save BettyJJ/14a7f532b4417d257a678066c4b83534 to your computer and use it in GitHub Desktop.
Example of Asure Speech service TTS in React
// copy to your component to use it
import * as sdk from "microsoft-cognitiveservices-speech-sdk";
function textToSpeech() {
const speechKey = process.env.SPEECH_KEY;
const speechRegion = process.env.SPEECH_REGION;
const speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, speechRegion);
const audioConfig = sdk.AudioConfig.fromDefaultSpeakerOutput();
speechConfig.speechSynthesisVoiceName = "en-US-JennyNeural";
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
const text = "Hello, my name is Jenny. What is your name?";
synthesizer.speakTextAsync(text, (result) => {
if (result) {
synthesizer.close();
return result.audioData;
}
}, (error) => {
console.log(error);
synthesizer.close();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment