You are not logged in.

#1 2017-10-12 12:05:55

Asaf K.
Member
Registered: 2011-05-04
Posts: 67

Change ALSA device on the fly

Let me begin by saying that I've seen that this sort of question is not new. But most of the threads and information that I found (through Google or otherwise) is quite old (on average five-six years, and even older), and I'd like to think that the situation is better now. And half the people ended up switching to PulseAudio, which I don't want to do.

I have a Dell XPS 13 and I use ALSA. I recently bought a USB C-to-HDMI cable so I can connect my computer to the TV and watch stuff on a bigger screen with slightly better speakers. I've written a nice script to utilize xrandr in turning on the external HDMI screen in all various ways (secondary monitor, mirrored display, etc.), but now comes the question of sound.

When I run

mplayer -ao alsa:device=plughw=0.3

, I can hear sound just fine through the TV, and life is wonderful, similarly when I use VLC and manually choose the device to be the HDMI output. But I want more from life. I want this to be the default—when I want it to be the default. I want to write a script that enables and disables the HDMI sound output as a default choice.

So far, all I've managed to understand that the documentation of ALSA is godawful and that literally nobody on the internet knows what they are doing, and those who do know, fail to explain it very well.

For example, I've see a lot of places telling me to copy and paste something into my .asoundrc or asound.conf files. But there is literally no explanation of the various values. I have no idea if the .asoundrc file is loaded "on the fly", which means that I can just overwrite it with a new file or remove the existing one in order to change the sound. If that is the case, I have no idea if I should use

type hw

or

type plughw

, if that's even a thing.

I've tried the "use a variable name" from the Arch Wiki, but that didn't work at all, and since I don't understand the configuration file, I can't even begin to understand why it doesn't work either.

In short. I want to be able to change the default PCM device on the fly. The two relevant cards in the aplay -l output are:

card 0: PCH [HDA Intel PCH], device 0: ALC3246 Analog [ALC3246 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

So it's the same card, but a different device. If anyone has any idea how to do that, I'd be very grateful, and if not, then any point of reference which can advance me into understanding how ALSA actually works (without reading the codebase like a deranged lunatic), would also be appreciated.


(Sorry for the despair-filled post. I'm just extra frustrated by the information available online.)

Offline

#2 2017-10-12 12:51:46

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Change ALSA device on the fly

Short-and-sweet answer: "On the fly" changes is a use-case that requires the likes of Pulseaudio. Your best bet these days is to use Pulseaudio.

I have recently switched to Pulseaudio, to get audio less-crackly in some Wine games.

Offline

#3 2017-10-12 13:09:30

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,676

Re: Change ALSA device on the fly

What brebs said. To prevent unwanted suprises, remove any custom ALSA configuration install pulseaudio-alsa/lib32-alsa-plugins and pavucontrol and familiarize yourself with what each knob in pavucontrol does.

Offline

#4 2017-10-13 21:57:50

Asaf K.
Member
Registered: 2011-05-04
Posts: 67

Re: Change ALSA device on the fly

Then how come there is a supposed solution on the Arch wiki? (The one with the environment variables)

Offline

#5 2017-10-13 22:05:20

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: Change ALSA device on the fly

Thats for specifying a certain output for a particular application when you start it. That's not the same thing as changing outputs on-the-fly.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#6 2017-10-14 11:42:24

Asaf K.
Member
Registered: 2011-05-04
Posts: 67

Re: Change ALSA device on the fly

slithery wrote:

Thats for specifying a certain output for a particular application when you start it. That's not the same thing as changing outputs on-the-fly.

I can actually work with that. If I change it for my media player and browser, then I more or less changed all the things I needed to change when it comes to HDMI sound output.

Offline

#7 2017-10-14 14:01:41

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,676

Re: Change ALSA device on the fly

I haven't been able to make it do the right thing with the default variables either, because the default device is hardcoded to 0 by default so changing the predefined environment variables isn't going to change the default pcm. however, minimal solution for your case that should do the right thing:

pcm.default.device {
        @func getenv
        vars [ ALSA_DEVICE ]
        default 0
}

Don't know if this works with specifying different devices and have that play back at the same time, however for switching the outputs to one card respectively, should be good enough.

ALSA_DEVICE=3 $program

to switch

Last edited by V1del (2017-10-14 14:08:53)

Offline

#8 2017-10-15 09:48:37

Asaf K.
Member
Registered: 2011-05-04
Posts: 67

Re: Change ALSA device on the fly

Thanks V1del. But I'm not entirely clear as to the meaning of this.

Okay, the first part should code into the ~/.asoundrc or /etc/asound.conf, but what does the second part mean in this context?

Offline

#9 2017-10-15 09:58:59

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,676

Re: Change ALSA device on the fly

That's the environment you pass to the program you are starting. or you write a script that sets this variable. it will have to be 0 for analog or 3 for hdmi. I just used ALSA_DEVICE here, you can use whatever you want. Or do you mean the sentence after the block? Try with different programs

ALSA_DEVICE=0 $program1
ALSA_DEVICE=3 $program2

and have them both play back at the same time. Does the universe implode? do you hear sound from both? only the one started first?

Offline

#10 2017-10-15 11:03:58

Asaf K.
Member
Registered: 2011-05-04
Posts: 67

Re: Change ALSA device on the fly

Well, running with or without setting the variable, mplayer doesn't recognize a sound card anymore.

[AO_ALSA] alsa-lib: conf.c:3636:(config_file_open) /home/[...]/.asoundrc may be old or corrupted: consider to remove or fix it

Offline

#11 2017-10-15 12:53:29

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,676

Re: Change ALSA device on the fly

Strange that works fine here, alternatively you could just swap out files with

defaults.pcm.device 3 #Or 0 respectively

since the whole file is reevaluated once you start an application.

Edit: Oh wait I copy pasted the wrong one, this should work

defaults.pcm.!device {
        @func igetenv
        vars [ ALSA_DEVICE ]
        default 0
}

sorry about that.

Last edited by V1del (2017-10-15 13:10:32)

Offline

#12 2017-10-15 19:16:10

Asaf K.
Member
Registered: 2011-05-04
Posts: 67

Re: Change ALSA device on the fly

Okay, it works when I do

ALSA_DEVICE=3 mplayer ...

, but not when I try the value 0, or no value. Simultaneous or otherwise.

But I the idea of changing .asoundrc files was the original idea to begin with, so I'm going to go with that. Thanks!

Offline

#13 2017-10-15 19:56:48

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,676

Re: Change ALSA device on the fly

Strange, strange but yeah swapping out the file should work

Offline

Board footer

Powered by FluxBB