flutter_sound icon indicating copy to clipboard operation
flutter_sound copied to clipboard

[HELP]: toStreamFloat32 not working

Open wernerpaulin opened this issue 1 year ago • 10 comments

Flutter Sound Version : 9.18.0

Severity

  • Result is not what expected

Platforms you faced the error

  • iOS
  • Android

Describe the bug When using ToStream I get into the upper and lower limits of the microphone: +/- 32767. I read in your change log I should use toStreamFloat32 as toStream is depreciated and somewhere I read it is better for the gain of the signal. However I can't get toStreamFloat32 working. Most of the the coders won't on iOS and when I use aacADTS I never get a onData callback. I don't need the accuracy. I need to avoid that the signal goes into the limitations.

My questions would be:

  1. Would Float32 help and how do I get it working on iOS?
  2. Is there a possibility for gain: VS Code kind of suggests but it is not compiling.

Thank you!

Logs!!!!

No specific errors. It is just not working using Float32.

wernerpaulin avatar Jan 11 '25 22:01 wernerpaulin

Hi @wernerpaulin , Float32 has not been implemented in Flutter Sound. I am currently working on Player to Stream and Recorder from Stream for Flutter Web. Hopefully it will support PCM Float32. Then I will try to support PCM Float32 on iOS, but I don’t promise anything. And then I will look to Android.

Actually I can’t answer for gain, because I don’t remember what I did. I must look to the code and I will tell you.

I will post something later.

Larpoux avatar Jan 12 '25 10:01 Larpoux

I also need this function to work with it

hungtranauthentia avatar Jan 18 '25 17:01 hungtranauthentia

I will release a new Flutter Sound later next week with Float32 and Float32Wav implemented on iOS. This task is actually worked on. Be patient ...

Larpoux avatar Jan 18 '25 17:01 Larpoux

iOS Float32 will be supported next week. Android and Web will be supported later.

Larpoux avatar Jan 18 '25 17:01 Larpoux

Here some news about the dev : I am working hard on 9.20.0. As it is always the case, dev are always harder and longer than expected. Float32 works correctly, but I have still problems to support interleaving pcm when there are several channels. I want this point be correctly working before releasing 9.20.0 Then I will work on 9.21.0 for streaming implementation on web

Larpoux avatar Jan 22 '25 18:01 Larpoux

@wernerpaulin , @hungtranauthentia and others ...

Flutter Sound 9.20.5 is released. It implements support for Float32 PCM on iOS. I wrote some doc here and here. I am not very good for documentation, so if there are points not clear, tell me. I will improve this doc. There is a new example showing how to record to a Float32 Stream and how to play from a Float32 Stream on iOS.

Please, note that the interleaving state is important, even if you have just one channel.

  • When interleaved, you will handle UInt8List Raw buffers. Useful if you only want to send or receive raw data for/from a server.
  • When not interleaved, you will handle List of Float32List. Useful if you need to access the Float32 samples.

Of course tell me if any problem. This dev was important and I could have introduced issues.


CHANGELOG

9.20.5

Work on iOS to support Float32 and non interleaved Streams. See this guide and this guide.

  • On IOS : Support of numChannels and sampleRate parameters for PCM codecs

    • On iOS : codec==Codec.pcm16 and codec==Codec.pcm16WAV -- startRecorder() -- Parameter numChannels is correctely handled (if >1 then the audio samples are interleaved)
    • On iOS : codec==Codec.pcm16 and codec==Codec.pcm16WAV -- startPlayer() -- Parameter numChannels is correctely handled (if >1 then the audio samples are interleaved)
    • On iOS : codec==Codec.pcm16 and codec==Codec.pcm16WAV -- startRecorder() -- Parameter sampleRate is correctely handled
    • On iOS : codec==Codec.pcm16 and codec==Codec.pcm16WAV -- startPlayer() -- Parameter sampleRate is correctely handled
  • On iOS : implementation of Codec pcmFloat32 and pcmFloat32WAV

    • On iOS : codec==Codec.pcmFloat32 and codec==Codec.pcmFloat32WAV -- startRecorder() -- Float32 is implemented
    • On iOS : codec==Codec.pcmFloat32 and codec==Codec.pcmFloat32WAV -- startPlayer() -- Float32 is implemented
    • FlutterSoundHelper::pcmToWaveBuffer() : Add parameter Codec and implement codec==Codec.pcmFloat32
  • On iOS : The peak level during recording pcm16 was unstable.

  • TODO

    • On iOS : codec==Codec.pcm16WAV -- startRecorder() -- The frames are not correctely coded with int16 but float32.
    • On iOS : codec==Codec.pcm32WAV -- The peak level is not computed correctly
    • On iOS : Codec.pcmINT16 not interleaved
    • Implement Float32 on Web and Android
    • Implement Streams on Web

Larpoux avatar Jan 28 '25 17:01 Larpoux

Nice! Thank you @Larpoux :-)!!!

wernerpaulin avatar Jan 28 '25 19:01 wernerpaulin

@Larpoux Thanks for your work. I'm currently using pcmFloat32 for FFT processing, but I'm having issues when saving this buffer to a PCM file and converting it to WAV. The audio file after conversion has choppy or discontinuous sound. Am I doing something wrong in the processing part?"

Here is code to save buffer to pcm file

    _fileSink = await createFile();

    var recordingDataController = StreamController<List<Float32List>>();
    _mRecordingDataSubscription =
        recordingDataController.stream.listen((buffer) {
      final leftChannel = buffer[0];
      final rightChannel = buffer[1];
      final interleavedData = Float32List(leftChannel.length * 2);
      
      for (var i = 0; i < leftChannel.length; i++) {
        interleavedData[i * 2] = leftChannel[i];     
        interleavedData[i * 2 + 1] = rightChannel[i];
      }
      
      final bytes = interleavedData.buffer.asUint8List();
      _fileSink?.add(bytes);

      ....
      await _myRecorder.startRecorder(
        toStreamFloat32: recordingDataController.sink,
        codec: Codec.pcmFloat32,
        sampleRate: 44000,
        bufferSize: 256,
        numChannels: 2,  // Changed to 2 channels
    );

Here is code to convert raw pcm to wav

await flutterSoundHelper.pcmToWave(
        inputFile: pcmPath,
        outputFile: wavPath,
        numChannels: 2,  // Changed to 2 channels
        codec: Codec.pcmFloat32,
        sampleRate: 44000);

hungtrn75 avatar Feb 19 '25 17:02 hungtrn75

Thank you @hungtrn75 for your information. I am surprised because I thought that all the modern cpu are little endian. I know that my work on float32 and plan mode is not finished. Not many users tried this new feature but they had issues with that. Actually I am cleaning the documentation. My plan is to work again on pcm float 32 when the doc will be better.

Thank you again.

Larpoux avatar Feb 19 '25 17:02 Larpoux

Nice! Thank for your works @Larpoux

hungtrn75 avatar Feb 19 '25 17:02 hungtrn75