阿里云语音合成示例

1.到官网下载SDK并安装

2.测试代码如下:

import nls
import nls.speech_synthesizer

URL = "wss://nls-gateway.cn-shanghai.aliyuncs.com/ws/v1"
TOKEN = "your_token"  # 参考https://help.aliyun.com/document_detail/450255.html获取token
APPKEY = "your_appkey"  # 获取Appkey请前往控制台:https://nls-portal.console.aliyun.com/applist


class MyTTS:

    def on_metainfo(self, message, *args):
        print("on_metainfo message=>{}".format(message))

    def on_error(self, message, *args):
        print("on_error args=>{}".format(args))

    def on_close(self, *args):
        print("on_close: args=>{}".format(args))
        try:
            self.__file.close()
        except Exception as e:
            print("close failed:", e)

    def on_data(self, data, *args):
        try:
            self.__file.write(data)
        except Exception as e:
            print("write data failed:", e)

    def on_completed(self, message, *args):
        print("on_completed:args=>{} message=>{}".format(args, message))

    def run(self, text, voice, file):
        self.__file = open(file, "wb")
        self.__text = text
        self.__voice = voice
        tts = nls.NlsSpeechSynthesizer(
            url=URL,
            token=TOKEN,
            appkey=APPKEY,
            on_metainfo=self.on_metainfo,
            on_data=self.on_data,
            on_completed=self.on_completed,
            on_error=self.on_error,
            on_close=self.on_close
        )
        result = tts.start(self.__text, voice=self.__voice, aformat="mp3")
        print("tts done with result:{}".format(result))


voice_list = ["zhimiao_emo", "zhimi_emo", "zhiyan_emo", "xiaogang", "sicheng", "aicheng", "xiaoyun", "ruoxi", "aida",
              "ninger", "ruilin", "siyue", "aiya", "aixia", "aiyu", "sitong", "xiaobei", "aiwei", "aibao"]

synthesizer = MyTTS()
# 每个声音都测试一下
for voice in voice_list:
    synthesizer.run(text="这是测试用句", voice=voice, file=f"{voice}.mp3")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用阿里云语音合成服务通过Node.js调用SDK来实现。以下是一些简单的步骤: 1. 首先,确保你已经在阿里云上创建了一个账户,并开通了语音合成服务。 2. 安装阿里云SDK的Node.js版本。你可以通过以下命令使用npm进行安装: ```shell npm install @alicloud/pop-core ``` 3. 在你的Node.js代码中,引入SDK并进行配置: ```javascript const Core = require('@alicloud/pop-core'); const client = new Core({ accessKeyId: '<你的Access Key ID>', accessKeySecret: '<你的Access Key Secret>', endpoint: 'https://nls-meta.cn-shanghai.aliyuncs.com', apiVersion: '2018-07-18' }); ``` 确保将`<你的Access Key ID>`和`<你的Access Key Secret>`替换为你自己的阿里云凭证信息。 4. 调用语音合成接口生成语音文件。以下是一个示例代码,生成一个名为`output.mp3`的语音文件: ```javascript const fs = require('fs'); const params = { Action: 'CreateSynthesizeTask', Voice: 'xiaoyun', Text: '你好,欢迎使用阿里云语音合成服务。', Format: 'mp3', Volume: 50 }; const requestOption = { method: 'POST' }; client.request('CreateSynthesizeTask', params, requestOption).then((result) => { const taskId = result.TaskId; const outputFilePath = 'output.mp3'; const downloadParams = { Action: 'GetTaskResult', TaskId: taskId }; setInterval(() => { client.request('GetTaskResult', downloadParams, requestOption).then((result) => { if (result.Status === 'SUCCESS') { const audioUrl = result.Result.OutputFileUrl; const file = fs.createWriteStream(outputFilePath); const request = https.get(audioUrl, (response) => { response.pipe(file); console.log(`语音文件已生成:${outputFilePath}`); }); clearInterval(interval); } }); }, 5000); }); ``` 这段代码将发送一个创建语音合成任务的请求,然后轮询获取任务结果,直到语音文件生成成功。最后,将语音文件下载到本地的`output.mp3`文件。 以上是一个简单的示例,你可以根据自己的需求进行调整和扩展。阿里云SDK提供了更多的功能和接口,你可以查阅官方文档以获取更多信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值