Skip to main content
SDK access

CosyVoice Ruby SDK

Use the Ruby SDK with CosyVoice to synthesize speech from text and save it as a WAV file.

Prerequisites

Download dashscope-1.2.0.gem and run gem install --local ./dashscope-1.2.0.gem to install the SDK. Set the DASHSCOPE_API_KEY environment variable to an API key from Singapore.

Quick start

Save the example as example.rb and run ruby example.rb.
require 'dashscope'

client = DashScope::Speech::CosyVoiceSpeechSynthesizer.new(
  model: 'cosyvoice-v3-plus',
# Singapore (ap-southeast-1). For another region, replace ap-southeast-1 in the URL.
# API keys are region-specific. See https://docs.qwencloud.com/developer-guides/getting-started/regions
  url: 'wss://ap-southeast-1.maas.qwencloudapi.com/api-ws/v1/inference',
  voice: 'longanyang',
  format: 'wav',
  sample_rate: 16_000
)
File.binwrite('output.wav', client.synthesize(['Hello, welcome to QwenCloud.']))

SDK reference

OptionDescription
model, url, voiceModel ID, WebSocket endpoint, and a voice supported by the model.
format, sample_rateOutput audio format and sample rate.
See the API reference for model-specific values and limits. synthesize accepts text segments and returns the complete audio. To receive audio as you send text, register on_audio and call start, send_text, and stop. Choose a voice supported by the model. Follow the model limits for format and sample_rate. Read usage or usage_text when synthesis completes.
CosyVoice Ruby SDK - QwenCloud