Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
877 stars 574 forks source link

[HELP]: Inconsistent Record Buffer Size #1118

Open MuhammedSaygili opened 2 weeks ago

MuhammedSaygili commented 2 weeks ago

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 commented 2 weeks ago

@Larpoux Do you have any information about this?

Larpoux commented 2 weeks ago

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.

MuhammedSaygili commented 2 weeks ago

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?