Skip to main content

Streaming ASR

Send audio data or a local audio file to the selected AI provider and receive incremental speech-recognition results.

Prerequisites

  • AIBudsAISDK is initialized and a registered provider is selected.
  • The provider supports StreamingASRServiceAPI.
  • The input bytes match the audioFormat specified by the configuration.
  • languageForSpeechInput, when supplied, uses a hyphenated language identifier such as en-US or zh-CN.

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Implement AIBuds Streaming ASR skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/implement-aibuds-streaming-asr. Use it to implement Implement AIBuds Streaming ASR in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBudsAI.xcframework

Import

Swift
import AIBudsAI
import AIBudsAIFoundation

Declaration

Swift
/// Recognizes voice data.
/// - Parameters:
///   - data: The audio data to recognize.
///   - config: The recognition configuration.
///   - onTranscript: Called for each incremental transcript result.
///   - onFailed: Called when recognition fails.
///   - onFinish: Called when recognition finishes.
public static func recognizeVoice(_ data: Data,
                                  config: StreamingASRConfig = .default,
                            onTranscript: ((_ transcriptData: StreamSpeechASRModel) -> Void)? = nil,
                                onFailed: ((_ error: NSError) -> Void)? = nil,
                                onFinish: (() -> Void)? = nil) -> Void

/// Recognizes a voice file.
/// - Parameters:
///   - filePath: The local path of the audio file to recognize.
///   - config: The recognition configuration.
///   - onTranscript: Called for each incremental transcript result.
///   - onFailed: Called when recognition fails.
///   - onFinish: Called when recognition finishes.
public static func recognizeVoice(withFile filePath: String,
                                             config: StreamingASRConfig = .default,
                                       onTranscript: ((_ transcriptData: StreamSpeechASRModel) -> Void)? = nil,
                                           onFailed: ((_ error: NSError) -> Void)? = nil,
                                           onFinish: (() -> Void)? = nil) -> Void

Both overloads are exposed by AIBudsAISDK and route to the selected provider's StreamingASRServiceAPI implementation.

Configuration

Create StreamingASRConfig with:

ValueDescription
languageForSpeechInputOptional recognition language. When omitted, the provider uses the current app localization language.
audioFormatInput format, such as .pcm, .mp3, or .wav.
enableSpeakerDiarizationWhether the provider should identify speaker segments. Defaults to false.

The input format uses AIAudioFormat:

ValueInput
.pcmRaw PCM audio matching the selected provider's required sample format.
.opusOpus-encoded audio.
.mp3MP3 audio.
.wavWAV container audio.
.none / .unknownNo usable format; do not start recognition with these values.

Each StreamSpeechASRModel includes a sequence, optional request ID, incremental transcript, definiteness flag, optional transcript sequence, and optional speaker segments.

Usage Examples

Recognize a File

Swift
let config = StreamingASRConfig(
    languageForSpeechInput: "en-US",
    audioFormat: .wav,
    enableSpeakerDiarization: true
)

AIBudsAISDK.recognizeVoice(
    withFile: fileURL.path,
    config: config,
    onTranscript: { result in
        print("Sequence \(result.sequence): \(result.transcript ?? "")")
        if result.isDefinite {
            print("Definite transcript received")
        }
    },
    onFailed: { error in
        print("Recognition failed: \(error.localizedDescription)")
    },
    onFinish: {
        print("Recognition finished")
    }
)

For in-memory audio, call recognizeVoice(_:config:onTranscript:onFailed:onFinish:) with Data / NSData and the same callback handling.

Notes

  • Transcript text is incremental and may repeat as a sentence becomes more complete; use sequence, transcriptSequence, and isDefinite when assembling UI output.
  • The current API accepts a complete Data value or file path. It does not expose start/append/stop streaming controls or a recognition delegate.
  • onFinish has no result value. Store the latest transcript received by onTranscript if the final text is needed after completion.
  • Supported formats, languages, and speaker diarization behavior can vary by provider.