我正在尝试创建一个应用程序,您可以在其中书写,然后听到转录。它可以工作,但我正在发送整个文本,然后等待整个音频,SDK 是否可以有一个流输入,它可以实时读取我的输入并作为流发送和输出?所以整个过程不需要等待,可以连续发送音频并读取新的文本。
我知道此过程之间存在延迟,但与等待写入整个文本段落和等待整个音频所需的时间相比,延迟非常小。
const textToSpeech = async (text) => {
// convert callback function to promise
return new Promise((resolve, reject) => {
let ssml = SSML.replace("__TEXT__", text);
const speechConfig = sdk.SpeechConfig.fromSubscription(key, region);
speechConfig.speechSynthesisOutputFormat = 4; // mp3
let audioConfig = null;
// if (filename) {
let randomString = Math.random().toString(36).slice(2, 7);
let filename = `./public/speech-${randomString}.mp3`;
audioConfig = sdk.AudioConfig.fromAudioFileOutput(filename);
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakSsmlAsync(
ssml,
(result) => {
synthesizer.close();
resolve({
filename: `/speech-${randomString}.mp3`,
});
},
(error) => {
synthesizer.close();
reject(error);
}
);
});
};
目前我正在使用一个文件和一个文本输入,但我想创建一个类似的流连接,所以当我开始编写时,它将连续读取并生成一个新的音频,该音频也将作为流接收,所以我可以在它生成时听到它,而不是在完整生成结束时听到它。
最佳答案
我已经修改了给定的 typescript 代码片段并在我的环境中对其进行了测试,我能够听到生成的音频流。
代码:
const textToSpeech = async (text: string, key: string, region: string) => {
return new Promise<{ filename: string }>((resolve, reject) => {
let ssml = `<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='en-US'><voice name='en-US-Jessa24kRUS'>__TEXT__</voice></speak>`.replace("__TEXT__", text);
const speechConfig = sdk.SpeechConfig.fromSubscription(key, region);
speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3;
let audioConfig: sdk.AudioConfig;
let randomString = Math.random().toString(36).slice(2, 7);
let filename = `./public/speech-${randomString}.mp3`;
audioConfig = sdk.AudioConfig.fromAudioFileOutput(filename);
const synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
synthesizer.speakSsmlAsync(
ssml,
(result) => {
synthesizer.close();
resolve({
filename: `/speech-${randomString}.mp3`,
});
},
(error) => {
synthesizer.close();
reject(error);
}
);
});
};
const myText = "Hi kamali. How are you?";
const myKey = "key";
const myRegion = "region";
const myFunction = async () => {
const result = await textToSpeech(myText, myKey, myRegion);
console.log(result);
}
myFunction();
输出:
关于typescript - 具有流输入和输出的 Azure 文本转语音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76081751/