Search code examples
audiopulseaudioalsafilter

Create a real-time audio filter for Alsa/Pulse loopback


I would like to reproduce the effect of active noise-control headphones using regular headphones and a Linux computer, in order to mute any ambiant noise in realtime (not just cleaning a sound input from background noise).

The plan is to loopback the microphone input through Alsa/Pulseaudio, apply a feed-forward narrowband noise cancelling filter (as a LADSPA filter ?), then output the result to the headphones.

The problem is I don't know where to start for the filter part : how should I connect it to Alsa/Pulseaudio ? Is there some sort of C API that can be used to create a custom filter ?


Solution

  • 1) I recommend doing this in Pulseaudio; redirecting ALSA output through your filter will be more difficult.

    2) There are already modules like module-ladspa-sink for Pulseaudio, details e.g. here. The module forwards LADSPA processed sound to master, but as you need both the mic input and the computer sound output, you may have to use some other modules (e.g. combined sink, null sink as a virtual output sink) and/or pulseaudio loopback commands to forward both of them on different sets of channels to the LADSPA module.

    Maybe you can also load two LADSPA modules, use one as virtual output, loopback mic input to the other, and process both from a single LADSPA plugin - I didn't try that, but if it works, it may be simpler.

    3) There are also already noise-cancelling and echo-cancelling modules for Pulseaudio which are used e.g. in Smartphones based on Linux; maybe one of those is good enough for your needs. Google.

    4) If everything else fails, or if LADSPA filters alone are not enough, look at the source of this module (or other Pulseaudio modules), and modify it accordingly.