-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzure_API.js
68 lines (54 loc) · 2.25 KB
/
Azure_API.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { SpeechConfig, AudioConfig, SpeechSynthesizer } from 'microsoft-cognitiveservices-speech-sdk';
import { Readable } from 'stream';
import {readFileSync} from 'fs';
async function synthesizeSpeech(textToVoice) {
try {
const speechConfig = SpeechConfig.fromSubscription(process.env?.SubscriptionKey, process.env?.ServiceRegion);
const audioConfig = AudioConfig.fromAudioFileOutput(process.env?.PathApiVoice || './audio/stock/apivoice.mp3');
const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakTextAsync(
textToVoice,
result => {
const { audioData } = result;
synthesizer.close();
const stream = Readable.from(audioData.toString());
return stream;
},
error => {
console.log(error);
synthesizer.close();
});
}
catch {};
}
function xmlToString(filePath) {
const xml = readFileSync(filePath, "utf8");
return xml;
}
async function synthesizeSpeechXML(textToVoice,voiceName) {
return new Promise(function(resolve, reject) {
const speechConfig = SpeechConfig.fromSubscription(process.env?.SubscriptionKey, process.env?.ServiceRegion);
const audioConfig = AudioConfig.fromAudioFileOutput(process.env?.PathApiVoiceXml || './audio/apivoice.mp3');
const ssml=`<speak version="1.0" xmlns="https://www.w3.org/2001/10/synthesis" xml:lang="en-US"> <voice name="${voiceName == "default_male" ? "en-GB-RyanNeural" : voiceName == "default_female" ? "en-US-JennyNeural" : "en-US-GuyNeural"}">${textToVoice}</voice> </speak>`;
//const ssml = xmlToString("ssml.xml");
const synthesizer = new SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakSsmlAsync(
ssml,
result => {
if (result.errorDetails) {
console.error(result.errorDetails);
} else {
console.log(JSON.stringify(result));
}
const { audioData } = result;
synthesizer.close();
resolve();
},
error => {
console.log(error);
synthesizer.close();
reject();
});
});
}
export {synthesizeSpeech, synthesizeSpeechXML };