What I want is the following:
- When I plug in my headphones, I want the sound to be un-muted and set to a specific volume level.
- When I unplug my headphones, I want the sound to be muted (or set to a specific volume level).
Setting the volume levels isn't the problem, but I somehow need to do this when un-/plugging the headphones, so I'm looking for a way to get notified of those events.
I quickly found /proc/asound/card0/codec#0
to indicate whether headphones are plugged in or not, so I tried to monitor it using inotifywait
and change the volume level based on modified notifications. Unfortunately inotifywait
failed because proc isn't an ordinary filesystem.
Are there other ways to do this (maybe via PulseAudio)?
Audio device: Intel HDA, audio codec: Conexant CX20585.
To summarize what I've tried so far:
- Ear Candy: didn't run at all on my system. Seems to be an abandoned project?
- The code posted by Karl Bielefeldt might work on some hardware, it doesn't on mine.
- The idea from StephenPaulger fails due to the lack of a 'Speaker' channel.
- Motivated by aking1012 I made it work by hacking around in the kernel code (after finding the jack sensing code of the codec), but that's a rather cumbersome solution.
So, I'm still looking for an easy way to do this.
During the UDS for Oneiric there has been a session about jack detection. See the blueprint for details. It sounds like this will exactly do what I'm looking for: different volume levels for different devices - just not on every hardware:
Update for 11.10
This should now be working in Oneiric - unfortunately my ThinkPad T510 seems to be one of the systems that's currently not supported.
Another follow-up from David Henningsson:
More here.
Update for 12.04
It's now working with my ThinkPad T510 (and many other models) in Ubuntu 12.04 (development version).
To set volume level from console use the command amixer. For example:
Will set your level to 0%
I don't know how to change this when you plug in your headphones... but maybe someone can help you with that
Ear Candy has some functionality for dealing with headphone insertion/removal.
I don't know how much coding experience you have, but you mentioned
inotifywait
so I'm going to assume you're at least comfortable with shell scripting. The following C program, based on this article, should be able to detect your headphones being plugged or unplugged, after which you can perform whatever actions you want.The program takes one argument, which should be a path to the input event device for your headphones. This is
/dev/input/event7
for me, but you may have to try a few to find which it is on your system. You will need read permissions on that device. The program will print the name of the device, and the type, code, and value of the first event before exiting.Check dmesg and other log files. Probably plugging in/out fires some particular messages.
Then, you could use a daemon to parse that log file and detect the events. I think you know what to do from this point :)
If you do
the speaker volume will be muted but headphones will carry on working. Unplugging the headphones means no sound. Which is almost what you want...
However...
If you alter the volume control the 'Speaker' volume setting will be set up again. This is because pulseaudio tries to be clever with setting volumes (See http://pulseaudio.org/wiki/PulseAudioStoleMyVolumes for more information). We can stop it from messing with the volumes though.
Open /etc/pulse/default.pa
and find the line
and change it to
Restart pulseaudio.
Now if you mute the speaker volume changing the master volume will not affect it. So headphones will work but the speaker will remain silent.
I think you might achieve what you need using this: https://bbs.archlinux.org/viewtopic.php?pid=1105458#p1105458
Check the ACPID socket for changes, can be done with the help of python or C or shell script, (Python code for controlling the earphone plugin play and pause on rhythmbox). Check for the changes in ACPID voltage to detect earphone plugin and earphone plug out. A seconds delay can be expected though.
Pulseaudio has a module shipped by default that is designed to achieve exactly this:
http://pulseaudio.org/wiki/Modules#module-device-restore