Google Text-toSpeech-在前端获取音频文件

体式

我有一个要求,我需要使用Google Text to Speech将一些文本转换为音频。

我正在使用Nodejs将文本转换为音频文件,并想将音频输出发送到前端。

NodeJS代码:

const client = new textToSpeech.TextToSpeechClient();
const request = {
  input: {text: 'Hello World'},
  // Select the language and SSML voice gender (optional)
  voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
  // select the type of audio encoding
  audioConfig: {audioEncoding: 'MP3'},
};

const [response] = await client.synthesizeSpeech(request);

response.audioContent 包含作为缓冲对象的音频数据,如下所示:

<Buffer ff f3 44 c4 00 00 00 03 48 00 00 00 00 ff 88 89 40 04 06 3d d1 38 20 e1 3b f5 83 f0 7c 1f 0f c1 30 7f 83 ef 28 08 62 00 c6 20 0c 62 03 9f e2 77 d6 0f ... > 

我将此作为api响应发送到前端。但是,我在前端得到的是一个带有数组的普通对象,如下所示:

{ "type": "Buffer", "data": [ 255, 243, 68, 196, 0, 0, 0, 3, 72, 0, 0, 0, 0, 255, 136, 137, 64, 4, 6, 61, 209, 56, 32, 225, 59, 245, 131, 240.......]}

我的问题:

1)由于前端从api接收到的数据不再是缓冲区,因此如何将这些数据转换回Buffer。

2)前端中有合适的缓冲区后,如何使用它播放音频。

在我的情况下,要转换的文本将始终为3-4个字词。因此,我不需要任何流功能。

我的前端是VueJS。

特里·伦诺克斯

您可以下载音频并使用html音频播放器播放。

我们需要两个文件,即index.js(Node.js)代码和index.html(Vue.js /客户端)。

这将合成您输入的文本并播放。

运行节点脚本,然后转到http:// localhost:8000 /观看演示。

您可以省略“ controls”属性以隐藏音频播放器,尽管它仍然可以播放声音!

index.js

const express = require("express");
const port = 8000;
const app = express();
const stream = require("stream");
const textToSpeech = require('@google-cloud/text-to-speech');

app.use(express.static("./"));

app.get('/download-audio', async (req, res) => { 

    let textToSynthesize = req.query.textToSynthesize;
    console.log("textToSynthesize:", textToSynthesize);

    const client = new textToSpeech.TextToSpeechClient();
    const request = {
        input: {text: textToSynthesize || 'Hello World'},
        // Select the language and SSML voice gender (optional)
        voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
        // select the type of audio encoding
        audioConfig: {audioEncoding: 'MP3'},
    };

    const [response] = await client.synthesizeSpeech(request);
    console.log(`Audio synthesized, content-length: ${response.audioContent.length} bytes`)
    const readStream = new stream.PassThrough();

    readStream.end(response.audioContent);
    res.set("Content-disposition", 'attachment; filename=' + 'audio.mp3');
    res.set("Content-Type", "audio/mpeg");

    readStream.pipe(res);
});

app.listen(port);
console.log(`Serving at http://localhost:${port}`);

index.html

<!DOCTYPE html>
<html>
<body>
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

<div class="container m-3" id="app">
<h2>Speech synthesis demo</h2>
<h4>Press synthesize and play to hear</h4>
<audio :src="audio" ref="audio" controls autoplay>
</audio>
<div class="form-group">
    <label for="text">Text to synthesize:</label>
    <input type="text" class="form-control" v-model="synthesisText" placeholder="Enter text" id="text">
</div>
<div>
    <button @click="downloadAudio">Synthesize and play</button>
</div>
</div>

<script>

    new Vue({
        el: "#app",
        data: {
            audio: null,
            synthesisText: "Gatsby believed in the green light, the orgiastic future that year by year recedes before us."
        },
        methods: {
            downloadAudio() {
                this.audio = "/download-audio?textToSynthesize=" + encodeURIComponent(this.synthesisText);
                this.$refs.audio.load();
                this.$refs.audio.play();
            }
        }
    });

</script>
</body>
</html> 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google云端存储:下载部分音频文件

来自分类Dev

流音频文件,以便在Android中使用Google Drive API从Google Drive播放

来自分类Dev

Django 中的 Google TTS:从 base64 字符串在 Javascript 中创建音频文件

来自分类Dev

从音频文件Uri获取专辑封面

来自分类Dev

从音频文件获取字节流?

来自分类Dev

在音频文件中获取期间

来自分类Dev

从音频文件Uri获取专辑封面

来自分类Dev

'NoneType'对象在获取Google结果时没有属性'text'

来自分类Dev

正在前端使用Google Pub / Sub(反应)

来自分类Dev

有没有一种方法可以在Google Assistant上同时说和播放音频文件

来自分类Dev

Android:如何从音频文件获取音频细节

来自分类Dev

如何获取播放音频文件的音频路径?

来自分类Dev

如何获取播放音频文件的音频路径?

来自分类Dev

验证音频文件

来自分类Dev

提供音频文件

来自分类Dev

连接音频文件

来自分类Dev

识别音频文件

来自分类Dev

如何通过获取SDCard的位置从SDCard删除音频文件

来自分类Dev

如何使用MediaStore获取所有音频文件

来自分类Dev

获取.SPH音频文件的整数表示

来自分类Dev

如何通过获取SDCard的位置从SDCard删除音频文件

来自分类Dev

如何获取在NsData中下载的音频文件?

来自分类Dev

从已经映射的数组列表中随机获取音频文件

来自分类Dev

从目录获取多个音频文件的功能循环

来自分类Dev

Express,Node,Angular将音频文件发送到前端

来自分类Dev

如何解决在Google Chrome和Opera浏览器中播放的某些视频和音频文件中没有声音的问题?

来自分类Dev

在前端将PHP $ text转换为可点击的URL链接

来自分类Dev

如何设计执行繁重任务并在前端显示结果的应用(例如Google Search Console)

来自分类Dev

将音频文件拆分

Related 相关文章

  1. 1

    Google云端存储:下载部分音频文件

  2. 2

    流音频文件,以便在Android中使用Google Drive API从Google Drive播放

  3. 3

    Django 中的 Google TTS:从 base64 字符串在 Javascript 中创建音频文件

  4. 4

    从音频文件Uri获取专辑封面

  5. 5

    从音频文件获取字节流?

  6. 6

    在音频文件中获取期间

  7. 7

    从音频文件Uri获取专辑封面

  8. 8

    'NoneType'对象在获取Google结果时没有属性'text'

  9. 9

    正在前端使用Google Pub / Sub(反应)

  10. 10

    有没有一种方法可以在Google Assistant上同时说和播放音频文件

  11. 11

    Android:如何从音频文件获取音频细节

  12. 12

    如何获取播放音频文件的音频路径?

  13. 13

    如何获取播放音频文件的音频路径?

  14. 14

    验证音频文件

  15. 15

    提供音频文件

  16. 16

    连接音频文件

  17. 17

    识别音频文件

  18. 18

    如何通过获取SDCard的位置从SDCard删除音频文件

  19. 19

    如何使用MediaStore获取所有音频文件

  20. 20

    获取.SPH音频文件的整数表示

  21. 21

    如何通过获取SDCard的位置从SDCard删除音频文件

  22. 22

    如何获取在NsData中下载的音频文件?

  23. 23

    从已经映射的数组列表中随机获取音频文件

  24. 24

    从目录获取多个音频文件的功能循环

  25. 25

    Express,Node,Angular将音频文件发送到前端

  26. 26

    如何解决在Google Chrome和Opera浏览器中播放的某些视频和音频文件中没有声音的问题?

  27. 27

    在前端将PHP $ text转换为可点击的URL链接

  28. 28

    如何设计执行繁重任务并在前端显示结果的应用(例如Google Search Console)

  29. 29

    将音频文件拆分

热门标签

归档