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 China (Beijing). 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.cn-beijing.maas.aliyuncs.com/api-ws/v1/live',
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
| Option | Description |
|---|---|
model | Model ID for the connection. Use qwen-audio-3.1-live. |
front_model | Model that talks to the user. The example uses qwen-audio-4.0-realtime-preview. |
turn_detection_mode | The example uses duplex so the model can keep listening while it speaks. |
voice | Choose a voice supported by the frontend model. The example uses yunshu. |
delegation | The 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
| Method | Purpose |
|---|---|
connect | Open the connection, wait for session.created, and send session.update. |
append_audio | Send an audio frame. |
mute_input / unmute_input | Mute or unmute input. |
send_user_text / send_item | Add text or a custom item to the session. |
send_function_call_output | Submit the result of a frontend function call. |
send_thinking / send_instruction / send_commentary | Send task progress, instructions, or commentary to the frontend model. |
append_backend_item / create_backend_response | Append an item to the backend or trigger a backend response. |
send_raw_event | Send a Live client event. |
finish | Send session.close, wait for session.closed, and disconnect. |
abort | Disconnect immediately. Final session usage may still be pending. |
| Event or callback | Description |
|---|---|
on_session_ready | Fires when the session ID is available. Wait for session.updated before sending audio. |
on_user_transcript_delta / on_assistant_text_delta | User transcript or assistant subtitle deltas. |
on_assistant_audio_chunk | A chunk of reply audio. |
on_delegation_created | A delegation summary. |
on_backend_event | A backend event in Responses mode. |
on_function_call | A frontend function call. |
on_usage_updated / on_closed | A usage update or session closure. The close event includes final usage. |
on_raw_event / on_error | Other server events or errors. |
- The delegation summary contains
delegation_id,task_id,summary,offset_ms, andcontext. Use the delegation ID when sending task progress. usageexposes the latest usage, includingsecondsand the context window usage ratio when available. On normal closure,session.closedreports final usage.
The SDK sends audio with session.input_audio.append. See WebSocket connection and Server events for the protocol and event fields.