Skip to main content

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

Install the Ruby SDK and configure your API key. Replace WorkspaceId with your workspace ID in Singapore and use an API key from the same region.

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',
  url: 'wss://WorkspaceId.ap-southeast-1.maas.aliyuncs.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.