You are not logged in.

#1 2014-05-30 04:52:18

MegaLoler
Member
Registered: 2012-06-15
Posts: 16

Alsa mono-stereo switch

I would like to have a keyboard shortcut that toggles mono/stereo sound output.  (Mainly for when I watch youtube videos where the sound is off to one side and it's a bit annoying.)

I did this in .asoundrc:

pcm.card0 {
type hw
card 0
}
pcm.mono {
type route
slave.pcm card0
slave.channels 2
ttable.0.0 0.5
ttable.0.1 0.5
ttable.1.0 0.5
ttable.1.1 0.5 
}

And I can test it out successfully with this command:

aplay -D mono something.wav

But I don't know how to use "mono" as the output for normal applications, and I don't know how to then toggle the output between "mono" and "default" with a command without reconfiguring things.

Offline

#2 2014-05-30 11:13:30

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: Alsa mono-stereo switch

Look at different plugins available. One method would be to copy the input stream from the application and send it through two softvol plugins (using multi plugin) to the same output device, where softvol plugins have a resolution of 2 (mute switch). Then map a keyshortcut for amixer to switch one softvol plugin on and the other off with capability to toggle between them. In between use your mono ttable. There you could change it at the fly, for the cost of a little bit more processing power.

Another approach would be to use environment variables (look at the wiki) to select a pcm device as output (so different than the example on the wiki), similar to this:

pcm.!default {	type plug;
		slave.pcm {	@func getenv
				vars [ ALSA_DEFAULT_PCM ]
				default "surround41";	}	}

Then launch the application with ALSA_DEFAULT_PCM set:

env ALSA_DEFAULT_PCM=mono application

This could be only set when launching, so not on-the-fly solution. Then there is PulseAudio, which does that on the fly I believe. Write the developers of youtube to make an automatic switch option, so that the player does it itself (the actual problem here). Those are the simplest solutions I can think of right now. Advanced alsa configuration for automatic detection using outside tools and dynamic switching may be possible, I have not tried it yet.

Read the alsa wiki why not to use type hw (read the whole Alternative Method chapter), unless you really want one sound at a time. Instead use:

defaults.pcm.card "CARDNAMEREFERENCE";
defaults.ctl.card "CARDNAMEREFERENCE";
#they already are pointing to card 0 by default, so you can drop it in your case and use pcm.default as slave for pcm.mono

Offline

#3 2014-05-30 11:21:41

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,456
Website

Re: Alsa mono-stereo switch

You can just make to .asoundrc files and toggle a symlink from ~/.asoundrc between them.

Then your keybinding could switch the link to the other file.

This would allow you to change "on the fly" in a sense, but applications that were already started will not change until they are restarted.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB