使用 Ruby SDK 调用 Sambert,将文本合成为语音并保存为 MP3 文件。
前提条件
按 Ruby SDK 安装说明 安装 SDK 并配置 API Key。将示例中的 WorkspaceId 替换为华北2(北京)的业务空间 ID,使用同地域的 API Key。
快速开始
将示例保存为 example.rb,运行 ruby example.rb。
require 'dashscope'
client = DashScope::Speech::SambertSpeechSynthesizer.new(
model: 'sambert-zhijia-v1',
url: 'wss://WorkspaceId.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference',
format: 'mp3',
sample_rate: 48_000
)
File.binwrite('output.mp3', client.synthesize('您好,欢迎使用百炼。'))
接口说明
| 参数 | 说明 |
|---|---|
model, url | 模型 ID 和 WebSocket 接口地址。模型 ID 同时决定发音人。 |
format, sample_rate | 所选模型支持的输出格式和采样率。 |
模型的参数取值和限制见接口参考。
Sambert 在启动任务时一次发送完整文本。synthesize 等待音频返回。需要逐块接收时,注册 on_audio 后调用 start(text) 和 wait_finished。
通过 model 选择发音人。音频格式和采样率按模型规格设置。
该文章对您有帮助吗?