Skip to content

Ai Streaming Asr

Mr.P edited this page Aug 25, 2026 · 2 revisions

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.

API Reference

Framework

AIBudsAI.xcframework

Import

Swift

import AIBudsAI
import AIBudsAIFoundation

Objective-C

#import <AIBudsAI/AIBudsAI-Swift.h>

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.
static func recognizeVoice(
    _ data: Data,
    config: StreamingASRConfig = .default,
    onTranscript: ((
        StreamSpeechASRModel
    ) -> Void)? = nil,
    onFailed: ((
        NSError
    ) -> Void)? = nil,
    onFinish: (() -> Void)? = nil
)

/// 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.
static func recognizeVoice(
    withFile filePath: String,
    config: StreamingASRConfig = .default,
    onTranscript: ((
        StreamSpeechASRModel
    ) -> Void)? = nil,
    onFailed: ((
        NSError
    ) -> Void)? = nil,
    onFinish: (() -> Void)? = nil
)

Objective-C

/// 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.
+ (void)recognizeVoiceData:(NSData *)data
        withConfig:(AIBudsStreamingASRConfig *)config
    onTranscript:
    (void (^ _Nullable)(AIBudsStreamSpeechASRModel *))onTranscript
        onFailed:(void (^ _Nullable)(NSError *))onFailed
        onFinish:(void (^ _Nullable)(void))onFinish;

/// 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.
+ (void)recognizeVoiceFile:(NSString *)filePath
        withConfig:(AIBudsStreamingASRConfig *)config
    onTranscript:
    (void (^ _Nullable)(AIBudsStreamSpeechASRModel *))onTranscript
        onFailed:(void (^ _Nullable)(NSError *))onFailed
        onFinish:(void (^ _Nullable)(void))onFinish;

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

Configuration

Create StreamingASRConfig with:

Value Description
languageForSpeechInput Optional recognition language. When omitted, the provider uses the current app localization language.
audioFormat Input format, such as .pcm, .mp3, or .wav.
enableSpeakerDiarization Whether the provider should identify speaker segments. Defaults to false.

The input format uses AIAudioFormat:

Value Input
.pcm Raw PCM audio matching the selected provider's required sample format.
.opus Opus-encoded audio.
.mp3 MP3 audio.
.wav WAV container audio.
.none / .unknown No 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")
    }
)

Objective-C

AIBudsStreamingASRConfig *config = [[AIBudsStreamingASRConfig alloc]
    initWithLanguageForSpeechInput:@"en-US"
    audioFormat:AIBudsAIAudioFormatWav
    enableSpeakerDiarization:YES];

[AIBudsAISDK recognizeVoiceFile:fileURL.path
    withConfig:config
    onTranscript:^(AIBudsStreamSpeechASRModel *result) {
        NSLog(@"Sequence %ld: %@",
              (long)result.sequence,
              result.transcript ?: @"");
        if (result.isDefinite) {
            NSLog(@"Definite transcript received");
        }
    }
    onFailed:^(NSError *error) {
        NSLog(@"Recognition failed: %@", error.localizedDescription);
    }
    onFinish:^{
        NSLog(@"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.

AIBuds SDK iOS Wiki

Clone this wiki locally