腾讯云提供了Tencent Cloud SDK for nodejs 来简化我们操作腾讯云的一些功能,比如语音合成功能,我们就可以使用sdk来进行操作,简化了签名认证的复杂过程。
下面就是操作腾讯云的api来进行语音合成的过程。
const tencentcloud = require("tencentcloud-sdk-nodejs"); const TtsClient = tencentcloud.tts.v20190823.Client const models = tencentcloud.tts.v20190823.Models; const Credential = tencentcloud.common.Credential; // secreateId和secretKey都可以从腾讯云获取到 let secretId = 'secretId' let secretKey = 'secretKey' let region = 'ap-shanghai' let cred = new Credential(secretId, secretKey); let client = new TtsClient(cred, region); let req = new models.TextToVoiceRequest(); let voiceType = 101001 //智侠,情感男声 req.deserialize({ Text: '前端小兵', SessionId: Date.now(), Volume: 10, Speed: 0, ProjectId: 0, ModelType: 1, VoiceType:voiceType, PrimaryLanguage: 1, SampleRate: 8000, Codec: 'mp3', }) const fs = require('fs') client.TextToVoice(req, function (err, response) { if (err) { console.log(err); return; } console.log(response.Audio); var buf = Buffer.from(response.Audio, 'base64'); // Ta-da fs.writeFileSync(`输出的音频.mp3`,buf) });
其中的secretId和secretKey可以到访问管理-API密钥管理中获取