streamTranscribeAudio method

Stream<TranscriptionMessage> streamTranscribeAudio({
  1. required Stream<List<int>> audioStream,
  2. String? language,
  3. TranscriptionOptions? options,
  4. int sampleRate = 16000,
  5. int bitDepth = 16,
  6. int channels = 1,
  7. String encoding = 'wav/pcm',
})

Performs speech recognition in real time

audioStream - audio data stream language - audio language (optional) options - additional transcription options sampleRate - audio sampling rate in Hz (default 16000) bitDepth - audio bit depth (default 16) channels - audio channel count (default 1) encoding - audio encoding format (default 'wav/pcm')

Returns transcription result stream

Implementation

Stream<TranscriptionMessage> streamTranscribeAudio({
  required Stream<List<int>> audioStream,
  String? language,
  TranscriptionOptions? options,
  int sampleRate = 16000,
  int bitDepth = 16,
  int channels = 1,
  String encoding = 'wav/pcm',
}) async* {
  try {
    // Session initialization
    final sessionResult = await initLiveTranscription(
      sampleRate: sampleRate,
      bitDepth: bitDepth,
      channels: channels,
      encoding: encoding,
      language: language,
      options: options,
    );

    // Create stream controller for result stream
    final streamController = StreamController<TranscriptionMessage>();

    // Create WebSocket connection
    final socket = createLiveTranscriptionSocket(
      sessionUrl: sessionResult.url,
      onMessage: (message) {
        if (message is Map<String, dynamic> &&
            message['type'] == 'transcript') {
          try {
            final transcriptionMessage =
                TranscriptionMessage.fromJson(message);
            streamController.add(transcriptionMessage);
          } catch (e) {
            streamController.addError(GladiaApiException(
              message: 'Error processing message: $e',
              innerException: e,
            ));
          }
        }
      },
      onDone: () {
        if (!streamController.isClosed) {
          streamController.close();
        }
      },
      onError: (error) {
        streamController.addError(GladiaApiException(
          message: 'WebSocket error: $error',
          innerException: error,
        ));
        if (!streamController.isClosed) {
          streamController.close();
        }
      },
    );

    // Subscribe to audio data stream
    final audioSubscription = audioStream.listen(
      (data) {
        if (socket.isConnected) {
          socket.sendAudioData(data);
        }
      },
      onError: (error) {
        streamController.addError(GladiaApiException(
          message: 'Error in audio stream: $error',
          innerException: error,
        ));
      },
      onDone: () {
        // Send signal about recording end
        if (socket.isConnected) {
          socket.sendStopRecording();
        }
      },
    );

    // Return result stream
    yield* streamController.stream;

    // Free resources when stream ends
    await streamController.done.then((_) {
      audioSubscription.cancel();
      socket.close();
    });
  } catch (e) {
    throw GladiaApiException(
      message: 'Error in stream transcription: $e',
      innerException: e,
    );
  }
}