flutter_sound icon indicating copy to clipboard operation
flutter_sound copied to clipboard

[HELP]: Inconsistent Record Buffer Size

Open MuhammedSaygili opened this issue 1 year ago • 3 comments

Hello,

Even though I change the buffer size of the audio captured from the microphone, I still receive audio packets in random sizes depending on the sample rate, rather than a fixed buffer size.

Here is the output of this code: samplerate=16000

Future<void> startRecordingAndSendUDP() async {
    if (_mRecorderIsInited && !isRecording) {
      isRecording = true;
      _sendCommand(utf8.encode("SEND"));

      var recordingDataController = StreamController<Uint8List>();
      _mRecordingDataSubscription =
          recordingDataController.stream.listen((soundBuffer) {
        print(soundBuffer.length);
        _sendCommand(soundBuffer);
      });

      await _mRecorder!.startRecorder(
        toStream: recordingDataController.sink,
        codec: Codec.pcm16,
        numChannels: 1,
        sampleRate: sampleRate,
        bufferSize: 1024,
      );
    }
  }

Additionally, even if I set the bufferSize value to 4096, I still receive these variable sizes.

Result:

flutter: 2732
flutter: 2730
flutter: 5546
flutter: 2732
flutter: 2730
flutter: 2730
flutter: 2732
flutter: 2730
flutter: 5546
flutter: 2732
flutter: 2730
flutter: 2730
flutter: 2732
flutter: 2730
flutter: 5546

Additionally, when I increase the sample rate to 44100Hz, the buffer size varies between 8192 and 9600.

If I'm not mistaken, I was previously using an older version of the flutter_sound package due to a version conflict with some other packages, and I don’t recall having this issue then. However, since I can't downgrade the version now, I'm not completely sure about this.

flutter_sound version used: 9.16.3 Test environment: iOS 17

MuhammedSaygili avatar Nov 06 '24 06:11 MuhammedSaygili

@Larpoux Do you have any information about this?

MuhammedSaygili avatar Nov 06 '24 09:11 MuhammedSaygili

The buffer size that you specify in flutter sound is relative to the internal buffers used, and is not tied with the size of packets. On iOS, flutter sound uses 4 buffers with the specified size. The only thing important is that flutter sound has enough buffer so that audio data are not lost.

Larpoux avatar Nov 06 '24 10:11 Larpoux

Got it, thank you for the information. By the way, I think flutter_sound does not support pcm8. Is there any other method you can suggest other than reducing 16 bit pcm to 8bit?

MuhammedSaygili avatar Nov 07 '24 14:11 MuhammedSaygili