Volume and Mute Keys on the Lenovo Carbon X1 under PulseAudio (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Sun, 24 Nov 2019

Volume and Mute Keys on the Lenovo Carbon X1 under PulseAudio

Running Ubuntu Eoan Ermine, I wanted to connect my Mute, Volume +, and Volume - buttons (Fn+F1, F2, F3) in Openbox. So I needed simple command-line ways of accomplishing those functions.

In the past, I've always uninstalled PulseAudio and used simple ALSA commands, because Pulse always seem to cause more problems than it solved; but on my new machine I'm experimenting to see if I can learn to coexist with Pulse.

I Can't Hear You!

My first problem was getting sound at all. It turned out that the problem was that I was testing this while sitting at my desktop, docked to my USB-C hub.

The main program for interacting with PulseAudio is pavucontrol, and running it, I discovered that when you're plugged into a dock that includes a sound chip, Pulse sets "USB PnP Audio Device Analog Stereo" (the sound chip in the dock) as the default output. So it always overrides "Built-in Audio Analog Stereo", even if you have no speakers plugged into the dock. "Built-in Audio Analog Stereo" is always muted. There's some sense to that; if I'd plugged in an actual USB sound device, it's reasonable to assume that I want to use it. A dock that includes sound is a special case; and really, I should probably get some nice speakers for it.

But I don't have any right now, so I need to solve this problem. The Output Devices tab in pavucontrol includes a "Set as fallback" button, and I thought maybe that meant "Set as default" and would let me use the built-in audio instead of the USB one; but no. I don't know what "Set as fallback" actually means, but it isn't that.

However, disabling "USB PnP Audio Device Analog Stereo" in the Configuration tab, leaving only "Built-in Audio", did work, and kept the USB PnP from popping back up like a hydra head every time I un-muted.

There are supposed to be command line ways of doing it. You can list Pulse's available sound sinks:

pacmd list-sinks | grep -e 'index:' -e device.string -e 'name:'
and then you're supposed to be able to set the default like this: pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo but it didn't work for me. Again, Pulse doesn't seem to have the concept of a default; if you want something to be the default, you have to disable all the others.

Hooray, now I could play sound. The next step was mute.

Mute with ALSA, not Pulse

This is supposed to toggle mute on the default output: pactl set-sink-mute @DEFAULT_SINK@ toggle
It worked when I typed it on the command line, but it didn't do anything when I put it in my ~/.config/openbox/rc.xml (and yes, it was running; I added some debugging output to make sure).

After a lot of fiddling, it developed that PulseAudio commands don't work to mute, only ALSA commands:

amixer -q -D pulse sset Master toggle

With that in my rc.xml, the Fn-Mute button worked great.

Volume Up and Down Use PCM

With volume control, too, I had trouble getting Pulse to work. None of the volume sliders in pavucontrol affected volume at all.

On the Arch wiki I had seen commands like: amixer -q -D pulse sset Master 3%+ unmute but those didn't work any better than pavucontrol's volume sliders.

I had to run alsamixer, and then I found that only the PCM slider had any effect on the volume. Maybe that's why pavucontrol doesn't work: it doesn't offer a PCM option, so it's probably only adjusting Master volume. I don't know why Master doesn't work; maybe it's one of the quirks of the only-partially-supported-yet sound chip on the Carbon X1 (we're told future kernels will eventually support it better).

Anyway, at least on this machine, since you have to use ALSA and PCM to set volume, the hands-off command is

amixer sset PCM [volume-level] unmute

Note that those are missing the -D pulse from the commands the Arch wiki suggested. I didn't seem to need that, and Pulse doesn't seem to mind that ALSA is changing the PCM volume, and I'm not sure what the advantage is of using -D pulse; the alsamixer man page gives no clue.

Openbox Configuration

I was now able to set up my ~/.config/openbox/rc.xml with working commands to make my mute and volume keys work:

    <keybind key="XF86AudioRaiseVolume">
      <action name="Execute">
            <command>amixer sset PCM 4%+ unmute</command>
        </action>
    </keybind>
    <keybind key="XF86AudioLowerVolume">
        <action name="Execute">
            <command>amixer sset PCM 4%- unmute</command>
        </action>
    </keybind>
    <keybind key="XF86AudioMute">
        <action name="Execute">
            <command>amixer -q -D pulse sset Master toggle</command>
        </action>
    </keybind>

Tags: , , , ,
[ 15:05 Nov 24, 2019    More linux | permalink to this entry | comments ]