Qwen-Audio-Realtime Ruby SDK
Updated at:
Use the Ruby SDK with Qwen-Audio-Realtime to send a recording and save the spoken response.
Prerequisites
Install the Ruby SDK and configure your API key. Replace WorkspaceId with your workspace ID in China (Beijing) 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::Conversation.new(
model: 'qwen-audio-3.1-realtime-plus',
url: 'wss://WorkspaceId.cn-beijing.maas.aliyuncs.com/api-ws/v1/realtime',
voice: 'longanfengyue_v3.1'
)
done = false
client.on_response_completed = ->(_) { done = true }
File.open('output.pcm', 'wb') do |output|
client.on_assistant_audio_chunk = ->(pcm) { output.write(pcm) }
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
10.times do
send_frame(client, "\0" * 3200, 0.1)
end
client.pump_until(45) { done }
client.finish
end
SDK reference
| Option | Description |
|---|---|
model, url, voice | Model ID, Realtime WebSocket endpoint, and a voice supported by the model. |
turn_detection_mode | Turn detection mode. Defaults to :server_vad. In :push_to_talk mode, the app triggers each response. |
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. After on_response_completed, call finish to close the session.
Use 16 kHz, 16-bit, mono raw PCM input. Choose a voice supported by the model. Trailing silence lets server-side VAD detect the end of the turn.
Is this page helpful?