llfbandit / record

Audio recorder from microphone to a given file path. No external dependencies, MediaRecorder is used for Android an AVAudioRecorder for iOS.
https://pub.dev/packages/record
232 stars 193 forks source link

Problems with Audio Streams on Mac OS #336

Closed ihdream72 closed 3 months ago

ihdream72 commented 3 months ago

Package version 5.1.1

Environment

I am developing a service where I want to record stream audio from multiple platforms and use Google's Speech to Text service.

const RecordConfig config = RecordConfig(
  encoder: AudioEncoder.pcm16bits,
  sampleRate: 16000,
  numChannels: 1,
);

Stream<Uint8List>? stream = await recorder?.startStream(config);

streamSubscription = stream?.listen((chunk) async {
  Log.d('MACOS PCM => [${DateTime.now()}] ${chunk.length}');
  onStream(chunk);
});

Recording as above works fine on Android, but not on MacOS. If you look at the logs left behind on MacOS, you'll see the following

flutter: [⭐️] MACOS PCM => [2024-06-04 14:23:10.071288] 1600 flutter: [⭐️] MACOS PCM => [2024-06-04 14:23:10.178055] 1600 flutter: [⭐️] MACOS PCM => [2024-06-04 14:23:10.275184] 1600 flutter: [⭐️] MACOS PCM => [2024-06-04 14:23:10.381623] 1600

Can you confirm this issue?

I appreciates the your effort.

ihdream72 commented 3 months ago

I made the following modifications to the 'record_darwin/darwin/Classes/delegate/RecorderStreamDelegate.swift' file I did that and it works fine on MacOS. Please check it out and let me know if you can update it.

private func stream(
    buffer: AVAudioPCMBuffer,
    dstFormat: AVAudioFormat,
    converter: AVAudioConverter,
    recordEventHandler: RecordStreamHandler
  ) -> Void {

   ...
   ... 
    // Determine frame capacity
    // let capacity = (UInt32(dstFormat.sampleRate) * dstFormat.channelCount * buffer.frameLength) / (UInt32(buffer.format.sampleRate) * buffer.format.channelCount)

       let capacity = (UInt32(dstFormat.sampleRate) * dstFormat.channelCount * buffer.frameLength * 2) / (UInt32(buffer.format.sampleRate) * buffer.format.channelCount)

     ...
     ...

  }
llfbandit commented 3 months ago

Thanks, can you fill a PR for this? Also, it should be tested on iOS since this code is shared between the platforms.