nullEuro / apply-equalizer

python programm that activates or deactivates pulseaudio-equalizer based on output port (headphones or speakers)
MIT License
11 stars 1 forks source link

Settings go back-and-forth many times #6

Closed sgourichon closed 10 years ago

sgourichon commented 10 years ago

Context: plug a jack into a sound output.

Expected: equalizer changes once

Observed: equalizer changes back and forth many times between setup for "nothing plugged in" and "something plugged in". Can be tens of times for a duration of possibly one minute.

Reproducible: sometimes, hardware-dependent. Happens on my main working machine, a laptop less than two years old.

Comments:

A perfect jack sensor would only send one and only one event on jack plug/unplug. A real sensor sometimes "bounces". This indeed is a hardware issue. When the programs reacting to it act quickly, it's not really an issue. For example, xfce4-volumed automatically adjusts volume level depending on active output. When problem occurs one just sees volume slide back and forth quickly and that's all. But apply-equalizer is much longer to appy settings. Events get queued. When bounces trigger tens of events, queue can take a long time to drain and appear like an endless loop.

sgourichon commented 10 years ago

Suggestion to fix the problem: make sure that the event handler is always quick. This can be done by it just updating a variable in the program instead of actually updating the equalizer settings the long way.

But then how to figure out when to actually apply the change ? When the queue had drained. When do know that ? GObject main loop gets idle and calls the idle callback, see g_idle_add.

What do you think ?

nullEuro commented 10 years ago

Can you please clarify what you mean by "go back-and-forth many times" and how to reproduce it?

sgourichon commented 10 years ago

Can you please clarify what you mean by "go back-and-forth many times" and how to reproduce it?

For a test I just ran apply-equalizer, unplugged then plugged the jack once.

The terminal showed the text below.

You can count 7 instances of "Equalizer status" there, while only 3 were expected. It's as if I did a full 3 plug-unplug cycles, and I guess the program indeed has received 6 output port change events.

It took a few seconds for them to scroll before settling. Sometimes there are more "bounces" and it scrolls for a long time, going back and forth between equalizer settings.

Got pa-server address from dbus: unix:path=/run/user/1000/pulse/dbus-socket
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Disabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (30%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Equalizer status: [disabled]
Equalizer configuration status: [disabled]
-------------------------------------
connected to pulseaudio
change detected! new output port is 'analog-output-speaker' on 'alsa_output.pci-0000_00_1b.0.analog-stereo'
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Enabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (45%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Current operation: enabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Loading module-ladspa-sink...
Transferring current mute (0) & volume (45%) to LADSPA sink (ladspa_output.mbeq_1197.mbeq)...
Setting ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo) preamp (1.0x)...
Setting LADSPA sink (ladspa_output.mbeq_1197.mbeq) as default sink...
Moving active PulseAudio clients to LADSPA sink (ladspa_output.mbeq_1197.mbeq)...
-------------------------------------
Equalizer status: [enabled]
Equalizer configuration status: [disabled]
Equalizer plugin: [mbeq_1197/mbeq]
Equalizer control: [0,0,-6.0,-14.9,-25.9,-26.2,-25.1,-31.7,-31.7,-30.8,-26.5,-33.9,-35.0,-15.2,-7.2]
NOTE: Using user-customized settings from '/home/stephane/.config/pulse/equalizerrc'...
-------------------------------------
change detected! new output port is 'analog-output-headphones' on 'alsa_output.pci-0000_00_1b.0.analog-stereo'
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Disabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (30%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Equalizer status: [disabled]
Equalizer configuration status: [disabled]
-------------------------------------
change detected! new output port is 'analog-output-speaker' on 'alsa_output.pci-0000_00_1b.0.analog-stereo'
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Enabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (30%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Current operation: enabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Loading module-ladspa-sink...
Transferring current mute (0) & volume (30%) to LADSPA sink (ladspa_output.mbeq_1197.mbeq)...
Setting ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo) preamp (1.0x)...
Setting LADSPA sink (ladspa_output.mbeq_1197.mbeq) as default sink...
Moving active PulseAudio clients to LADSPA sink (ladspa_output.mbeq_1197.mbeq)...
-------------------------------------
Equalizer status: [enabled]
Equalizer configuration status: [disabled]
Equalizer plugin: [mbeq_1197/mbeq]
Equalizer control: [0,0,-6.0,-14.9,-25.9,-26.2,-25.1,-31.7,-31.7,-30.8,-26.5,-33.9,-35.0,-15.2,-7.2]
NOTE: Using user-customized settings from '/home/stephane/.config/pulse/equalizerrc'...
-------------------------------------
change detected! new output port is 'analog-output-headphones' on 'alsa_output.pci-0000_00_1b.0.analog-stereo'
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Disabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (30%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Equalizer status: [disabled]
Equalizer configuration status: [disabled]
-------------------------------------
change detected! new output port is 'analog-output-speaker' on 'alsa_output.pci-0000_00_1b.0.analog-stereo'
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Enabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (30%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Current operation: enabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Loading module-ladspa-sink...
Transferring current mute (0) & volume (30%) to LADSPA sink (ladspa_output.mbeq_1197.mbeq)...
Setting ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo) preamp (1.0x)...
Setting LADSPA sink (ladspa_output.mbeq_1197.mbeq) as default sink...
Moving active PulseAudio clients to LADSPA sink (ladspa_output.mbeq_1197.mbeq)...
-------------------------------------
Equalizer status: [enabled]
Equalizer configuration status: [disabled]
Equalizer plugin: [mbeq_1197/mbeq]
Equalizer control: [0,0,-6.0,-14.9,-25.9,-26.2,-25.1,-31.7,-31.7,-30.8,-26.5,-33.9,-35.0,-15.2,-7.2]
NOTE: Using user-customized settings from '/home/stephane/.config/pulse/equalizerrc'...
-------------------------------------
change detected! new output port is 'analog-output-headphones' on 'alsa_output.pci-0000_00_1b.0.analog-stereo'
Disabling persistence...
Current operation: saving configuration (disable-config)
-------------------------------------
Equalizer setting saved (disable-config).
-------------------------------------
Disabling...
PulseAudio Equalizer/LADSPA Processor 2.7 (05/02/2010)
-------------------------------------
Current operation: disabling equalizer
-------------------------------------
Unloading & reloading stream-restore module...
Unloading module-ladspa-sink...
Moving active PulseAudio clients to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
Transferring current mute (0) & volume (30%) to ALSA sink (alsa_output.pci-0000_00_1b.0.analog-stereo)...
-------------------------------------
Equalizer status: [disabled]
Equalizer configuration status: [disabled]
-------------------------------------
nullEuro commented 10 years ago

That is indeed not good. Furthermore I can't reproduce it. If I start apply-equalizer manually and did one plug/unplug cycle it just does what you expect. Maybe you accidentally checked "Keep settings" in the equalizer GUI? Maybe there are two instances of apply-equalizer.py running?

sgourichon commented 10 years ago

That is indeed not good. Furthermore I can't reproduce it. If I start apply-equalizer manually and did one plug/unplug cycle it just does what you expect.

This is the correct behavior, which is also what happens to me most of the time.

Maybe you accidentally checked "Keep settings" in the equalizer GUI?

Definitely not.

Maybe there are two instances of apply-equalizer.py running?

Definitely not.

Fundamentally what happens on my side is a hardware glitch.

Still, the software should protect against bounce. Every real-world input like a key or button provides debouncing feature (see http://en.wiktionary.org/wiki/debounce or http://arduino.cc/en/Tutorial/Debounce ).

One could argue that the debouncing should happen upstream, before it even reaches apply-equalizer. I let you judge about it.

Furthermore I can't reproduce it.

You could probably have a similar behavior by plugging/unplugging faster than apply-equalizer processes the change. For example, I can plug and unplug, say, 20 times within 5 seconds and see the terminal scrolling with messages and the sound output being switched between equalized and non-equalized for another 5 seconds after leaving the jack alone.

Whatever the cause (hardware bounce or actually switching several times), seeing the terminal scroll and the sound switch settings back and forth does not make sense.

To summarize, it's expected that plug/unplug events should not be queued then slowly dequeued, but only the most recent one should be handled, because as soon as there is a later event, all previous events are irrelevant and should not be handled at all.

Hope this helps.

nullEuro commented 10 years ago

One could argue that the debouncing should happen upstream, before it even reaches apply-equalizer. I let you judge about it.

Yes I think this should be done in lower layers, but since this a special-purpose program we can add a debouncing feature to make it more useable. I am working on a patch :)