Skip to main content

Qwen-Audio-Live Ruby SDK

Open a full-duplex voice session over WebSocket and receive subtitles and audio.

Prerequisites

Install Ruby SDK 1.2.0 and configure your API key. Replace WorkspaceId with your workspace ID in Singapore. Qwen-Audio-Live requires a workspace-specific domain. Prepare input.pcm as 16 kHz, 16-bit little-endian, mono raw PCM. You can use FFmpeg to convert a recording.
ffmpeg -i input.wav -ar 16000 -ac 1 -f s16le input.pcm

Quick start

Save the code 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

input_size = File.size('input.pcm')
unless input_size.positive? && input_size.even?
  raise ArgumentError, 'input.pcm must contain 16-bit PCM samples.'
end

live = DashScope::Qwen::Live.new(
  url: 'wss://WorkspaceId.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/realtime',
  model: 'qwen-audio-3.1-live',
  front_model: 'qwen-audio-4.0-realtime-preview',
  turn_detection_mode: :duplex,
  voice: 'yunshu',
  delegation: :client
)

File.open('reply.pcm', 'wb') do |output|
  live.on_assistant_text_delta = ->(text, _, _) { print text }
  live.on_assistant_audio_chunk = ->(pcm, _, _) { output.write(pcm) }
  live.on_error = ->(error) { warn error.inspect }
  session_updated = false
  live.on_raw_event = ->(type, _) { session_updated = true if type == 'session.updated' }
  live.connect
  live.pump_until(10) { session_updated }
  unless session_updated
    live.abort
    raise 'Timed out waiting for session.updated.'
  end

  File.open('input.pcm', 'rb') do |input|
    while (frame = input.read(1280))
    send_frame(live, frame, 0.04)
    end
  end

  25.times do
    send_frame(live, "\0" * 1280, 0.04)
  end

  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 30
  live.pump(0.1) while Process.clock_gettime(Process::CLOCK_MONOTONIC) < deadline
  live.finish
end

puts "\nSession duration: #{live.usage&.seconds} s"
The terminal prints subtitles. reply.pcm contains 24 kHz, 16-bit little-endian, mono audio. The example allows 30 seconds for the reply. In your app, wait for the full reply before closing the session.

Session options

OptionDescription
modelModel ID for the connection. Use qwen-audio-3.1-live.
front_modelModel that talks to the user. The example uses qwen-audio-4.0-realtime-preview.
turn_detection_modeThe example uses duplex so the model can keep listening while it speaks.
voiceChoose a voice supported by the frontend model. The example uses yunshu.
delegationThe example uses Client mode. Set the backend model when you use Responses mode.
See the Qwen-Audio-Live API reference for session configuration.

Methods and events

MethodPurpose
connectOpen the connection, wait for session.created, and send session.update.
append_audioSend an audio frame.
mute_input / unmute_inputMute or unmute input.
send_user_text / send_itemAdd text or a custom item to the session.
send_function_call_outputSubmit the result of a frontend function call.
send_thinking / send_instruction / send_commentarySend task progress, instructions, or commentary to the frontend model.
append_backend_item / create_backend_responseAppend an item to the backend or trigger a backend response.
send_raw_eventSend a Live client event.
finishSend session.close, wait for session.closed, and disconnect.
abortDisconnect immediately. Final session usage may still be pending.
Event or callbackDescription
on_session_readyFires when the session ID is available. Wait for session.updated before sending audio.
on_user_transcript_delta / on_assistant_text_deltaUser transcript or assistant subtitle deltas.
on_assistant_audio_chunkA chunk of reply audio.
on_delegation_createdA delegation summary.
on_backend_eventA backend event in Responses mode.
on_function_callA frontend function call.
on_usage_updated / on_closedA usage update or session closure. The close event includes final usage.
on_raw_event / on_errorOther server events or errors.
  • The delegation summary contains delegation_id, task_id, summary, offset_ms, and context. Use the delegation ID when sending task progress.
  • usage exposes the latest usage, including seconds and the context window usage ratio when available. On normal closure, session.closed reports final usage.
The SDK sends audio with session.input_audio.append. See WebSocket connection and Server events for the protocol and event fields.