|Changing Output Device with PulseAudio|
Changing Output Device with PulseAudio
Posted on August 31st, 2013 23:07
Y'know, when I first started this section, I just called it "code" and figured most of the things I did would actually involve code. Now, most of it just seems to involve non-coded Linux usage. Well then, from now on, Linux it is.
I hate PulseAudio. I know it should be a good thing, in theory, but in practice, I always ended up removing it from my Linux desktop distro. It seems like too much software I use doesn't play nice with it, the command line interface has too much if you only want to do a little, it is hard to switch output devices with it from the command line, and -- worst of all -- I can never find good answers to PulseAudio problems on the interwebs.
However, when I finally updated to Debian Wheezy, Pulse was all up in my GNOME3. I did decide to give GNOME3 a shot, so I decided to give Pulse, too, another shot, which means finding the right bullet to put in this beast.
My desktop sits pretty close to my TV in my small apartment -- close enough to run a six-foot HDMI cable. I would hope the reason would be obvious, but in today's world, watching your tee-vees on a screen more than a few inches big may seem weird and scary to people.
My setup uses integrated graphics (no graphics card for power saving) with one VGA and one HDMI output. I can have one or both (!) active at any time. That good guy xrandr soundly handles switching my display. Switching sound from analog stereo to HDMI in a script with PulseAudio is a different matter.
The ALSA Solution
If you're not using PulseAudio, then you are probably using simple ALSA (unless you time-traveled back to OSS days). Fortunately, there is a wealth of information out there on how to setup ALSA and sound-switching properly. Why, all I had to do is:
echo 'defaults.pcm.device 3' >> $HOME/.asoundrc service alsa-utils restart
Put it in a script, and all the sudden, my sound is comin' from my TV. The only ponderous thing about it is that magic number "3". It comes from /proc/asound/pcm as the device number for HDMI playback. No more magic! It is fairly straightforward, and sed-ing out that line I added makes the sound return to the default -- or analog -- output. I guess I could pick device 0, since that is what the asound/pcm file says, but this isn't terribly important. The script is working and happy, so I'm happy.
At least, everyone is happy with ALSA.
The Pulse Problem
The first thing I needed to check is that PulseAudio actually recognizes my audio devices. Fortunately, it does.
That is what my Sound Settings show. I can switch the output device, and sound starts coming out of the new device, as it should. That's good, but I don't want to have to open up a GUI interface every time I switch back and forth between my monitor and my TV; I want to script this.
So what does Google say? pacmd and particularly the commands set-*sink.
And this recommendation doesn't work. And this is part of why I hate PulseAudio.
I am trying list-sinks and passing in every manner of index and device name, but nothing works. At least I am getting some errors out of it.
[jaret@ragnarok ~]$ pacmd set-default-sink 2 >>> Sink 2 does not exist.
Hooray! The list-sinks command spits out a lot of garbage, and I try everything that is suggested -- and then some -- but nothing gets the sound in the right place.
The problem is that, like my displays, all of my audio is also coming from my motherboard. I don't have any sound cards, and the internets seems to like to assume that I do. I don't have any video cards, either, but the video stuff wasn't a problem at all. I think xrandr's comparatively terse output when compared to pacmd list-sinks is a good indication of why I was able to figure that out so much more quickly.
So, my HDMI and analog stereo sound are actually one sink. That means that none of the sink commands will do me any good. What I actually want is to use is set-card-profile. It was actually staring me in the face in my GNOME control center: "Profile".
Oh, yeah, now I see it.
Upon running a pacmd list-cards, I can get the index and possible profiles for my PC, all of which match up with what I saw in the Sound Settings window:
index: 2 ... profiles: output:analog-stereo: Analog Stereo Output (priority 6000) output:analog-stereo+input:analog-stereo: Analog Stereo Duplex (priority 6060) output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400) output:hdmi-stereo+input:analog-stereo: Digital Stereo (HDMI) Output + Analog Stereo Input (priority 5460) output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300) output:hdmi-surround+input:analog-stereo: Digital Surround 5.1 (HDMI) Output + Analog Stereo Input (priority 360) input:analog-stereo: Analog Stereo Input (priority 60) off: Off (priority 0) ...
Armed with this new knowledge, I can use the correct command instead of the incorect set-*sink* command -- which would be set-card-profile:
pacmd set-card-profile 2 output:hdmi-stereo pacmd set-card-profile 2 output:analog-stereo
Of course, I am pulling all these values with an awk in my script, but these are what the executed commands should look like, given the output of list-cards.
When you start figuring out PulseAudio stuff, you can start hating it a little less.
No discussions exist yet for this. You can start one if you would like.
|Copyright © 1999-2021|