flutter_sound icon indicating copy to clipboard operation
flutter_sound copied to clipboard

[ENH]: Implement Advanced Noise Suppression Feature

Open Sasha071201 opened this issue 10 months ago • 11 comments

Is your feature request related to a problem? Please describe. I am facing significant audio issues on iOS when configuring the audio session for voice communication. With the current setup using AVAudioSessionMode.voiceChat, the microphone fails to capture my voice. When setting the mode to null, an echo appears after streaming data via _player.feedUint8FromStream. Additionally, not configuring the audio session at all results in an extremely low speaker volume.

Describe the solution you'd like. I propose the implementation of a new noise suppression functionality that intelligently adjusts the audio session parameters for voice communication. This feature should:

  • Ensure clear microphone capture even when using voice chat mode.
  • Prevent echo effects during data streaming.
  • Maintain optimal speaker volume without compromising the microphone’s sensitivity.

Describe alternatives you've considered. Several configurations have been tested:

  • Using AVAudioSessionMode.voiceChat leads to the microphone not picking up my voice.
  • Setting the mode to null avoids the microphone issue but results in echo during playback.
  • Not configuring the audio session at all produces an audio output that is too quiet.
  • These alternatives underscore the need for an improved noise suppression mechanism.

Additional context This issue has been observed exclusively on iOS using Flutter’s audio session, recorder, and player APIs. Below is the relevant code configuration:

Audio Session Configuration

  final session = await AudioSession.instance;
  final configuration = AudioSessionConfiguration(
      avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
      avAudioSessionCategoryOptions:
          AVAudioSessionCategoryOptions.defaultToSpeaker | AVAudioSessionCategoryOptions.allowBluetooth,
      avAudioSessionMode: AVAudioSessionMode.voiceChat,
      avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.notifyOthersOnDeactivation,
      androidAudioAttributes: AndroidAudioAttributes(
        contentType: AndroidAudioContentType.speech,
        flags: AndroidAudioFlags.none,
        usage: AndroidAudioUsage.voiceCommunication,
      ),
      androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
      androidWillPauseWhenDucked: true,
    );
    await session.configure(configuration);

Recorder Configuration

await _recorder.openRecorder();
await _recorder.startRecorder(
  codec: Codec.pcm16,
  sampleRate: 16000,
  numChannels: 1,
  toStream: streamController.sink,
);

Player Configuration

await player.openPlayer();
await player.setVolume(volume);
await player.startPlayerFromStream(
  codec: Codec.pcm16,
  numChannels: 1,
  sampleRate: 16000,
  interleaved: true,
);

Sasha071201 avatar Mar 16 '25 16:03 Sasha071201

I don't remember exactly, but its possible that someone implemented something for noise cancellation. (Or perhaps just another fellow wanting this feature). I have not a good memory. I think that it would be a very big improvement to flutter sound. My problem is that my time is limited and I really want to work on etau/Flutter Sound V10.

It would be great if someone work on this issue. Perhaps you ?

Did you test Android and Web ? can you confirm that there is no necessity to work for this platforms ?

BTW: my work on Audio Streams for Flutter Web is going very well. I must write a test program to verify that everything is OK, and I will release a new version tomorrow or Tuesday.

Larpoux avatar Mar 16 '25 16:03 Larpoux

Hi,

It seems that someone has already implemented a noise cancellation solution. You can check out the details here: https://github.com/Canardoux/flutter_sound/issues/1134.

Unfortunately, I am currently unable to test on Android, and since startPlayerFromStream is not correctly implemented on Flutter Web, I can't test that platform either.

Regrettably, I don't have the time to help with this feature at the moment.

Sasha071201 avatar Mar 16 '25 16:03 Sasha071201

This feature would be very useful. I also needed this and tried looking for alternatives of noise suppression in flutter but could not find any.

Also came across this article which claims that flutter_sound has a noise suppressor module but I couldnot find any. I suppose this article is not correct and maybe a hallucinated GPT generated one. @Larpoux Could you confirm if there is something like this?

Mastermind-sap avatar Mar 21 '25 16:03 Mastermind-sap

I’ve decided to forgo using flutter_sound for now, because I was able to implement everything myself using native code.

Sasha071201 avatar Mar 21 '25 16:03 Sasha071201

@Sasha071201 Ok. I don't have any knowledge about native android. Could you provide some resources about your implementation?

Mastermind-sap avatar Mar 21 '25 18:03 Mastermind-sap

@Mastermind-sap

Here are some concise, authoritative resources that cover exactly what I implemented:

  1. Android AudioRecord & AudioTrack (official docs)
    • AudioRecord: https://developer.android.com/reference/android/media/AudioRecord
    • AudioTrack: https://developer.android.com/reference/android/media/AudioTrack

  2. AcousticEchoCanceler
    • https://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler

  3. AudioManager Modes
    • https://developer.android.com/reference/android/media/AudioManager#MODE_IN_COMMUNICATION

Sasha071201 avatar Mar 21 '25 18:03 Sasha071201

@Sasha071201 Thank you

Mastermind-sap avatar Mar 21 '25 18:03 Mastermind-sap

Is there any hope that you integrate your work inside Flutter Sound ?

Larpoux avatar Mar 21 '25 19:03 Larpoux

I don’t plan to invest the time and effort needed to do that. I’ll wait for this functionality to be supported))

Sasha071201 avatar Mar 21 '25 20:03 Sasha071201

The next days, I am going to work on the WASM support. When it will be done, perhaps I am going to work on the noise suppression. But no idea yet when I will be able to do that. I will let you know how things are going ...

Larpoux avatar Mar 21 '25 20:03 Larpoux

OK, guys and girls. I promised to let you informed. I think I will be able to start working on noise cancellation in a few days. Probably middle of next week. I will start with Android, because @Sasha071201 is an expert, and I hope that he will give me some hints if I have problems.

Larpoux avatar Mar 22 '25 12:03 Larpoux