script for quick switch between audio interfaces via commandline

Talk about your MIDI interfaces, microphones, keyboards...

Moderators: MattKingUSA, khz

Post Reply
alignwaivers
Established Member
Posts: 5
Joined: Thu Sep 26, 2019 11:38 pm

script for quick switch between audio interfaces via commandline

Post by alignwaivers »

So I switch so much back and forth between my usb audio interface and my internal soundcard (when I take my laptop elsewhere), it gets a little tedious managing all the settings manually (in cadence) whenever I switch.

A slightly better step would be to create two separate rooms in claudia and switch between loading those. But I'm wondering, is there a way of switching between audio devices in the command line? Haven't had success with this but have put much effort into doing so. Any insights would be greatly appreciated, thanks

p.s. would be fine to use claudia to switch 'rooms' if I could do so from the terminal, but as far as I'm aware this is not a feature and claudia takes no arguments when run in the command line
User avatar
bluebell
Established Member
Posts: 1909
Joined: Sat Sep 15, 2012 11:44 am
Location: Saarland, Germany
Has thanked: 111 times
Been thanked: 116 times

Re: script for quick switch between audio interfaces via commandline

Post by bluebell »

alignwaivers wrote:So I switch so much back and forth between my usb audio interface and my internal soundcard (when I take my laptop elsewhere), it gets a little tedious managing all the settings manually (in cadence) whenever I switch.

A slightly better step would be to create two separate rooms in claudia and switch between loading those. But I'm wondering, is there a way of switching between audio devices in the command line? Haven't had success with this but have put much effort into doing so. Any insights would be greatly appreciated, thanks

p.s. would be fine to use claudia to switch 'rooms' if I could do so from the terminal, but as far as I'm aware this is not a feature and claudia takes no arguments when run in the command line
AFAIK jackd's audio interface can't be changed on the fly. So you have to restart jack, e.g. with a shell script.

Linux – MOTU UltraLite AVB – Qtractor – http://suedwestlicht.saar.de/

User avatar
Linuxmusician01
Established Member
Posts: 1524
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 756 times
Been thanked: 135 times

Re: script for quick switch between audio interfaces via commandline

Post by Linuxmusician01 »

I made a shell script for personal use on my various Linux machines. Maybe it's almost what you mean. You can find it here and it looks like this:

Code: Select all

┌─────────────────────────────────────┐
│              Audio menu             │
└─────────────────────────────────────┘
Use ↑ and ↓ keys to select and Enter
to execute.

 Jack audio daemon: start               
                  : kill                
 Check Midi       : port numbers        
                  : output              
 Check Alsa       : device/card numbers 
 Pulseaudio       : start               
                  : stop                
                  : PAVUControl         
 Config file      : create/edit         
                  : delete              
                  : view                
 Help                                  
 Exit                                  
alignwaivers
Established Member
Posts: 5
Joined: Thu Sep 26, 2019 11:38 pm

Re: script for quick switch between audio interfaces via commandline

Post by alignwaivers »

That's an awesome script! Thanks for sharing that

Unfortunately I am primarily interested in switching the master instead of restarting jack completely.

Do you know if that might be possible if you change the device based on the config file?





I found a useable solution (thanks to Falktx for pointing me in the right direction)

jack_control dps device "hw:USB,0" #(my audio interface)
jack_control sm

HERE's the new problem. I switch between 2 different USB audio interfaces that BOTH show up as "hw:USB,0"

I hope to find a way to clear / reset jack's memory of the devices so when I plug in the NEW device my script tries to connect to that "hw:USB,0" and ignores the old one.

The only way I know how to do that now is by stopping jack :(
User avatar
Linuxmusician01
Established Member
Posts: 1524
Joined: Mon Feb 23, 2015 2:38 pm
Location: Holland
Has thanked: 756 times
Been thanked: 135 times

Re: script for quick switch between audio interfaces via commandline

Post by Linuxmusician01 »

alignwaivers wrote:That's an awesome script! Thanks for sharing that

Unfortunately I am primarily interested in switching the master instead of restarting jack completely.

Do you know if that might be possible if you change the device based on the config file?





I found a useable solution (thanks to Falktx for pointing me in the right direction)

jack_control dps device "hw:USB,0" #(my audio interface)
jack_control sm

HERE's the new problem. I switch between 2 different USB audio interfaces that BOTH show up as "hw:USB,0"

I hope to find a way to clear / reset jack's memory of the devices so when I plug in the NEW device my script tries to connect to that "hw:USB,0" and ignores the old one.

The only way I know how to do that now is by stopping jack :(
I don't think that I know exactly what you want but I think that it's not possible. One cannot change between audio devices without restarting Jack I think.

You might also try the commands that you already know w/ sound card names instead of sound card numbers. Like for instance:

Code: Select all

cat /proc/asound/cards
 0 [CMI8738        ]: CMI8738-MC6 - C-Media CMI8738
                      C-Media CMI8738 (model 55) at 0xd000, irq 20
 1 [PCH            ]: HDA-Intel - HDA Intel PCH
                      HDA Intel PCH at 0xf7c10000 irq 45
And then:

Code: Select all

alsamixer -c "CMI8738"
Instead of:

Code: Select all

alsamixer -c "0"
User avatar
nikgnomicradio
Established Member
Posts: 109
Joined: Wed Feb 07, 2018 9:31 pm
Has thanked: 1 time
Been thanked: 7 times
Contact:

Re: script for quick switch between audio interfaces via commandline

Post by nikgnomicradio »

Code: Select all

I am primarily interested in switching the master instead of restarting jack completely.
do not see how you can switch out system device that is part of the command to start JACK without restarting

maybe if started JACK with dummy device, then alsa_in and alsa_out commands to load ALSA device
would that allow ALSA device to be removed and replaced later without disturbing JACK ?
I switch between 2 different USB audio interfaces that BOTH show up as "hw:USB,0"
if they are non-identical devices, the Vendor ID (vid) and Product ID (pid) numbers can be assigned to ALSA index numbers
https://alsa.opensrc.org/MultipleUSBAudioDevices
tavasti
Established Member
Posts: 2047
Joined: Tue Feb 16, 2016 6:56 am
Location: Kangasala, Finland
Has thanked: 369 times
Been thanked: 208 times
Contact:

Re: script for quick switch between audio interfaces via commandline

Post by tavasti »

nikgnomicradio wrote: maybe if started JACK with dummy device, then alsa_in and alsa_out commands to load ALSA device
would that allow ALSA device to be removed and replaced later without disturbing JACK ?
Instead of alsa_in and alsa_out, jack_load could be used.

Linux veteran & Novice musician

Latest track: https://www.youtube.com/watch?v=ycVrgGtrBmM

merlyn
Established Member
Posts: 1392
Joined: Thu Oct 11, 2018 4:13 pm
Has thanked: 168 times
Been thanked: 247 times

Re: script for quick switch between audio interfaces via commandline

Post by merlyn »

You can switch device while JACK is running using

Code: Select all

jack_control dps device hw:<x>
jack_control sm
You can if you're using jackdbus. Does jack_control do anything if you're using jackd?

@alignwaivers there must be a unique identifier for each card. If you do

Code: Select all

aplay -l
you'll get the card indexes which you can use as hw:<x> rather than the name of the card.
alignwaivers
Established Member
Posts: 5
Joined: Thu Sep 26, 2019 11:38 pm

Re: script for quick switch between audio interfaces via commandline

Post by alignwaivers »

thanks for the insights, the one issue is going by the hw:#, means you always have to plug audio devices in the same order.

Fortunately I found some good options for renaming the devices https://jackaudio.org/faq/device_naming.html


and also a completely separate solution: https://github.com/SpotlightKid/
Shadow_7
Established Member
Posts: 175
Joined: Tue Jun 08, 2010 3:35 pm

Re: script for quick switch between audio interfaces via commandline

Post by Shadow_7 »

With jack2 (jackdbus) you can change many things on the fly. Via jack_control as others have mentioned. The device, the sample rate, the bit width, and such. You can do similar things with pulseaudio via pacmd / pactl.

Another option would be the set everything up with alsa's loopback device (snd_aloop) and have two alsaloop's in the .bash_history to route that to the real devices. Although each layer adds a bit of latency. And alsaloop can be a bit unstable. To the point that I run it inside an infinite loop.

$ while true; do alsaloop -C hw:Loopback,1,0 -P pulse -t 50000; sleep 1; done

And kind of a lazy way to use jack on snd_aloop and route that to pulseaudio, if you're pulse is over a network, or you trust your jack configuration skills more than your pulseaudio skillset. The loopback device output is it's input, which would make it work for playback, not so much for capture. Although many apps that capture lets you explicitly select your input device(s).
Post Reply