Skip to main content

Qwen-Audio-ASR-Streaming Ruby SDK

使用 Ruby SDK 調用 Qwen-Audio-ASR-Streaming,識別 16 kHz、16 位、單聲道原始 PCM 音訊。

前提條件

按 Ruby SDK 安裝說明 安裝 SDK 並配置 API Key。將示例中的 WorkspaceId 替換為新加坡的業務空間 ID,使用同地域的 API Key。

快速開始

將示例保存為 example.rb,運行 ruby example.rb。
require 'dashscope'

client = DashScope::Speech::FunAsrSpeechTranscriber.new(
  model: 'qwen-audio-3.1-asr-flash-streaming',
  url: 'wss://WorkspaceId.ap-southeast-1.maas.aliyuncs.com/api-ws/v1/inference',
  format: 'pcm',
  sample_rate: 16_000
)
client.on_sentence_end = ->(sentence, _) { puts sentence.text }
client.on_failed = ->(error) { warn error.inspect }
client.transcribe_pcm_file('input.pcm')

接口說明

參數說明
model, url模型 ID 和 WebSocket 接口地址。
format, sample_rate音訊格式和採樣率。與實際輸入保持一致。
模型的參數取值和限制見接口參考。 transcribe_pcm_file 按實時節奏發送音訊並等待任務完成。識別定稿通過 on_sentence_end 傳回。需要控制發送時,依次調用 start、send_audio 和 stop。通過 on_failed 接收任務錯誤。 連線超時、任務啓動和收尾分別由 connect_timeout、start_timeout、stop_timeout 控制。音訊的 format 和 sample_rate 必須與實際輸入一致。