繁体   English   中英

如何创建使用 azure tts 将音频发送到 web 应用程序的 expressjs 端点?

[英]How do I create an expressjs endpoint that uses azure tts to send audio to a web app?

我想弄清楚如何公开使用 azure tts sdk (microsoft-cognitiveservices-speech-sdk) 生成 some_word 的音频版本(以任何可播放的格式)的快速路由(即:Get api/word/:some_word)通过浏览器)和 res.send() 生成的音频,以便前端 javascript web 应用程序可以使用 api 来播放单词的音频发音。

我有 azure sdk '工作' - 它正在我的 expressjs 代码中创建一个 'ArrayBuffer'。 但是,我不知道如何将这个ArrayBuffer中的数据发送到前端。 我一直按照这里的说明操作: https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-text-to-speech?tabs=import%2Cwindowsinstall&pivots=programming- javascript语言#get-result-as-an-in-memory-stream

表达我的问题的另一种方式是“明确地说,我有一个 ArrayBuffer,其内容是一个 .mp3/.ogg/.wav 文件”。 我如何通过快递发送该文件? 我是否需要将其转换为其他数据类型(如 Base64 编码字符串?缓冲区?)是否需要设置一些特定的响应标头?

在问了这个问题后我终于想通了

我对这个领域还很陌生,所以任何关于如何改进这一点的建议都将不胜感激。

app.get('/api/tts/word/:word', async (req, res) => {
  const word = req.params.word;
  const subscriptionKey = azureKey;
  const serviceRegion = 'australiaeast';

  const speechConfig = sdk.SpeechConfig.fromSubscription(
    subscriptionKey as string,
    serviceRegion
  );
  
  speechConfig.speechSynthesisOutputFormat =
    SpeechSynthesisOutputFormat.Ogg24Khz16BitMonoOpus;

  const synthesizer = new sdk.SpeechSynthesizer(speechConfig);

  synthesizer.speakSsmlAsync(
    `
    <speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
       xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN">
    <voice name="zh-CN-XiaoxiaoNeural">
            ${word}
    </voice>
    </speak>
    `,
    (resp) => {
      const audio = resp.audioData;
      synthesizer.close();
      const buffer = Buffer.from(audio);
      res.set('Content-Type', 'audio/ogg; codecs=opus; rate=24000');
      res.send(buffer);
    }
  );
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM