The officially official Devuan Forum!

You are not logged in.

#1 2025-11-17 19:50:38

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Simple card-switcher for ALSA

So this is another tiny utility to be added to the toolbox for AlsaTune or other interface, for use in a pure alsa system. This is just the rough prototype, I need to do more testing, and more thinking about possible errors and edge-cases and dealing with them.

It would be great if someone with multiple cards could test, I only have one machine that has two cards and I have tested on it and all the single-card machines and this works fine so far. Curious about systems with more cards and those with sub-devices, and also other possible areas to edit that aren't in my config (you can see in the script it changes for hw, plughw, and card).

This works by merely editing ~/.asoundrc, it uses aplay to get the card list, then seds your choice into the config file. It doesn't require re-booting or even re-loading alsa, you just need to close and re-open any running sound instances like your music player after editing is complete. It also creates a backup of your old config, but for testing purposes you should definitely backup whatever you have now for an .asoundrc.

#!/bin/bash
# soundswitch - simple card switcher for AlsaTune that mods ~/.asoundrc
# TO-DO: deal with subdevices? Any other prefixes common in larger asound files 
# (like card, hw, or plughw below) that need added?

CARDS=$(aplay -l | grep "^card [0-9]")

CHOICE=$(echo "$CARDS" | yad --list --title="Select Sound Card" \
  --column="Card" --width=550 --height=200 --window-icon=info --button="Apply:0" --button="Cancel:1")

[ $? -ne 0 ] && exit 1

CARD_NUM=$(echo "$CHOICE" | cut -d'|' -f1 | grep -o -E '[0-9]' | head -n 1)

if [ -z "$CARD_NUM" ]; then
  yad --title="Error" --width=300 --center --text-align=center --window-icon=error \
--text="\nNo card was selected. Exiting..." --no-buttons --timeout=4
  exit 1
fi

# Backup
cp ~/.asoundrc ~/.asoundrc.bak

# Replace all card #'s with new card #'s
sed -i "s/hw:[0-9],/hw:$CARD_NUM,/g; s/plughw:[0-9],/plughw:$CARD_NUM,/g; s/card [0-9]/card $CARD_NUM/g" ~/.asoundrc

yad --window-icon=info --text-align=center --width=300 --center --title="Sound" \
--text="\nSound card switched to Card $CARD_NUM\n\nConfig backed up to ~/.asoundrc.bak" --button=gtk-ok:0

https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#2 2025-11-17 22:12:10

g4sra
Member
Registered: 2018-12-12
Posts: 105  

Re: Simple card-switcher for ALSA

@greenjeans, like'ish your script...wont work for me, running a corner-case here smile

Suggestions...
Have a default when 'sed' fails/ .asoundrc is empty ore doesn't exist.
Have the window size itself to the number of entries in the list (its annoying to have to keep resizing to make a selection).

Online

#3 2025-11-17 22:30:46

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

^^Much appreciated!

No .asoundrc is a case, at the start of this endeavour I am writing this to be included in AlsaTune/Vuu-do which will already have an .asoundrc in the system, but like other things I want to also make a generic/portable version that can be used anywhere so that's the next target for functionality.

Window-size: How many cards are in your list? I had envisioned entries for just a couple, can you elaborate? Do I need a scroll function?


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#4 2025-11-17 23:40:20

g4sra
Member
Registered: 2018-12-12
Posts: 105  

Re: Simple card-switcher for ALSA

Window-size: 11 Lines (four of those are for the GPU alone)
So for a Twin GPU system that could be eight just to start with.
I suppose a scrollbar would cover all bases, don't want to push you to loose the KISS aspect though.

Online

#5 2025-11-18 01:04:54

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

Ummm, GPU? Ya lost me there somewhere...


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#6 2025-11-18 13:06:23

g4sra
Member
Registered: 2018-12-12
Posts: 105  

Re: Simple card-switcher for ALSA

Ummm, GPU?

Outputs are capable of carrying digital sound as well as video, so consequently show up in ALSA as individual sink devices.

ls -ld /proc/asound/card1/pcm*
dr-xr-xr-x 4 root root 0 Nov 18 13:07 /proc/asound/card1/pcm3p
dr-xr-xr-x 4 root root 0 Nov 18 13:07 /proc/asound/card1/pcm7p
dr-xr-xr-x 4 root root 0 Nov 18 13:07 /proc/asound/card1/pcm8p
dr-xr-xr-x 4 root root 0 Nov 18 13:07 /proc/asound/card1/pcm9p

Last edited by g4sra (2025-11-18 13:10:27)

Online

#7 2025-11-18 22:20:08

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

Ahh I gotcha, and that's the kind of thing I need to know to proceed, problem is I don't have a machine around here to test, I get one line on my single-card lappy, two on the library machine...without having a complex machine with multiple outputs around to test on it's gonna make it really hard for me to extend this script. I kinda need to see what things are doing, my methods involve a lot of trial-and-error.

Basic philosophy/use-case is for Vuu-do as it already has a working alsa system, and is meant for simple everyday garden-variety use, just a quick switch for those machines that have an analog and an HDMI output as most of the time the dual-setups default to HDMI as card 0.

At least at this stage...baby steps...but i'd like to pursue it and figure out all the edge-cases and sub-devices and such so I can package it with AT for general use at some point.


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#8 2025-11-18 22:45:55

g4sra
Member
Registered: 2018-12-12
Posts: 105  

Re: Simple card-switcher for ALSA

a lot of trial-and-error

With the work you put in for the benefit of others I judge that you are entitled to use the computer science term for that, stepwise refinement smile

i'd like to pursue it and figure out all the edge-cases

I don't mind throwing it at my workstation on occasion and reporting back, I guarantee it is an edge case, my /etc/asound.conf contains 724 lines.

Online

#9 2025-11-20 18:02:43

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

Hah, I love it...yep...Vuu-do Linux, handcrafted using stepwise refinement and multi-solutional iterations (hit-or-miss). lol

Thanks for the help, I really appreciate it and at some point i'll take you up on the offer to test the next version.

724 lines in asound.conf? Holy crap, that's epic! Yeah I can see where my little script probably wouldn't be good for that system, but it's a dang fine one for testing the limits of it for sure.


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#10 2025-11-21 22:50:19

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

Yeah...so it seems there's no scroll function or auto-sizing for the yad list dialog, at least not in the old versions being used in the repo. For the moment I added a little height. arrgh.

I added an error check to look for ~/.asoundrc right at the start and exit if there isn't one. But now i'm thinking in the error message to also add the ability to copy a simple .asoundrc (to be shipped with the mxeq/AT package) to the users home if there isn't one, and then continuing (or hit cancel to back out of course).

I'll be modding AT a fair bit to add a button for the card-switcher and some other functions including a button to test sound, using aplay to play a .wav file I made, just 3 seconds of a 500hz note, the idea being you can test whatever changes you made with the card-switcher right there in the gui after doing it, without having to cycle audio/video apps on and off.


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#11 Today 17:05:06

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

After several days of work I have a new version of AlsaTune packaged as of last night, working great for me, but as usual when I wake up the next morning there's always some little detail buzzing in my head, something that could be done better....

So I did some work on this card-switcher script as I mentioned above, enlarged the list window some, and added a check at the start of the script to check for ~/.asoundrc, and if absent it offers to install the generic one included with the package, or you can hit cancel to abort.

The script works great and fast for me and should for the vast majority of simple laptops and desktops. But going forward I want to improve detection and expand a bit, first thing I want to do is deal with sub-devices, that pesky second number, currently this script only swaps the card number.

And thus my dilemma today, should I upload the new AlsaTune as-is so hopefully it can get tested some more, or should I work on the next phase of this script and wait until I have it working before uploading a new AT....*sigh*....


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#12 Today 17:23:12

g4sra
Member
Registered: 2018-12-12
Posts: 105  

Re: Simple card-switcher for ALSA

@greenjeans

or should I work on the next phase of this script and wait until I have it working before uploading

I have seen someone (fsmithred ?, ralph.ronquist ?) offer someone project space on git.devuan.org.
Your project is quite small (no offence, small is beautiful smile) and therefore would not be a resource gobbler, and I am sure of interest to Devuan users even if unlikely to be qualified for packaging.
Uploading to git.devuan.org. you could concentrate on developing and the interested could follow your progress as (in)frequently as they choose.
Testing and bug reports could also be more forthcoming (at least in theory).

Online

#13 Today 17:34:12

greenjeans
Member
Registered: 2017-04-07
Posts: 1,315  
Website

Re: Simple card-switcher for ALSA

You're right of course, and I did register an account on Devuan's git a while back, I just haven't messed with it. I have never used git before so I don't know how bad the learning curve is...the thing is I already have multiple learning curves i'm going through and git looks kind of like a pain in the butt, lol.

Packaging locally here using dpkg-deb is fast and uncomplicated. But again you're right that it might get some more testing if I used git.

This is the first year i've branched out to more complex things than simple scripts, so i'm still trying to get a feel for things.


https://sourceforge.net/projects/vuu-do/ New Vuu-do isos uploaded October 2025!
Vuu-do GNU/Linux, minimal Devuan-based Openbox and Mate systems to build on. Also a max version for OB.
Devuan 5 mate-mini iso, pure Devuan, 100% no-vuu-do. wink Devuan 6 version also available for testing.
Please donate to support Devuan and init freedom! https://devuan.org/os/donate

Online

#14 Today 17:43:24

g4sra
Member
Registered: 2018-12-12
Posts: 105  

Re: Simple card-switcher for ALSA

Git is worth it. I have been tweaking the kernel for decades, having git track my changes and re-apply them is a godsend.
I even now use it for full (small) system backups.

Last edited by g4sra (Today 17:44:55)

Online

Board footer