Skip to main content
SDK access

Qwen-LiveTranslate-Realtime Ruby SDK

Use the Ruby SDK with Qwen-LiveTranslate-Realtime to translate a Chinese recording into English and print the translation.

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'

def send_frame(client, frame, interval)
  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + interval
  client.append_audio(frame)
  while (remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)).positive?
    client.pump(remaining)
  end
end

client = DashScope::Qwen::Translator.new(
  model: 'qwen3.5-livetranslate-flash-realtime',
# 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/realtime',
  source_language: 'zh',
  target_language: 'en'
)
client.on_translation_completed = ->(text, _) { puts text }
session_updated = false
client.on_raw_event = ->(type, _) { session_updated = true if type == 'session.updated' }
client.connect
client.pump_until(10) { session_updated }
unless session_updated
  client.abort
  raise 'Timed out waiting for session.updated.'
end
File.open('input.pcm', 'rb') do |input|
  while (frame = input.read(640))
  send_frame(client, frame, 0.02)
  end
end
client.finish

SDK reference

OptionDescription
model, urlTranslation model ID and Realtime WebSocket endpoint.
source_language, target_languageSource and target languages.
voiceVoice for translated audio. Choose a voice supported by the model.
See the API reference for model-specific values and limits. Call connect and use pump to read events. Wait for session.updated before sending audio with append_audio. Read completed translations in on_translation_completed. Call finish to wait for the last segment. Use 16 kHz, 16-bit, mono raw PCM input. Choose source_language and target_language supported by the selected model.