Skip to main content

CosyVoice Ruby SDK

使用 Ruby SDK 調用 CosyVoice,將文本合成為語音並保存為 WAV 檔案。

前提條件

按 Ruby SDK 安裝說明 安裝 SDK 並配置 API Key。將示例中的 WorkspaceId 替換為新加坡的業務空間 ID,使用同地域的 API Key。

快速開始

將示例保存為 example.rb,運行 ruby example.rb。
require 'dashscope'

client = DashScope::Speech::CosyVoiceSpeechSynthesizer.new(
  model: 'cosyvoice-v3-plus',
  url: 'wss://WorkspaceId.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/inference',
  voice: 'longanyang',
  format: 'wav',
  sample_rate: 16_000
)
File.binwrite('output.wav', client.synthesize(['Hello, welcome to Model Studio.']))

接口說明

參數說明
model, url, voice模型 ID、WebSocket 地址和模型支援的音色。
format, sample_rate傳回音訊的格式和採樣率。
模型的參數取值和限制見接口參考。 synthesize 接收文本分段並傳回完整音訊。需要邊發送邊接收時,通過 on_audio 接收音訊,依次調用 start、send_text 和 stop。 請選擇模型支援的 voice。format 和 sample_rate 的值須符合模型要求。合成完成後,可讀取 usage 或 usage_text。