Change default sound input/output device on USB plug-in

Questions about hardware and drivers
Forum rules
Before you post please read this

Change default sound input/output device on USB plug-in

Postby AndyNZ on Wed Dec 25, 2013 11:21 pm

Kia ora,

I'm tired to change the default sound input device every time manually when I'm plugging in my webcam for Skype.
I made a custom set up script that get called if a video4linux device is plugged in.
(Target group: advanced user/admin - if you not understand what this is about, please ask an advanced user/admin not me)

/etc/udev/rules.d/99-persistent-cam.rules
# set uvcvideo device as default input
#
SUBSYSTEM=="video4linux", ACTION=="add", RUN+="/bin/su user -c /usr/local/bin/set_default_snd_input_device.sh"

Variables:
your user name

/usr/local/bin/set_default_snd_input_device.sh
Code: Select all
#!/bin/bash
#
# search for the first snd_usb_audio input device and set it as input default device
/usr/bin/pacmd set-default-source $(/usr/bin/pacmd list | grep -B 40 -e "alsa.driver_name.=..snd_usb_audio." \
| grep -m1 -e 'name:..alsa_input' | awk '{print $2}' | sed -e 's/<//;s/>//') >/dev/null 2>&1


Activating:
udevadm control --reload-rules

Debugging:
udevadm monitor --property --subsystem-match=video4linux
Right event and subsystem ?
or/and
/usr/bin/pacmd list | grep -B 40 -e "alsa.driver_name.=..snd_usb_audio." | grep -e 'name:..alsa_input'
Right line picked ?

----------------------------------------------------------------------------------------------------------------------------

I also own an external USB radio transmitter that shows up as a USB sound card.
This device should be the default output device if plugged-in.

/etc/udev/rules.d/88-persistent-usbsnd.rules
# set usb audio device as default output
#
SUBSYSTEM=="sound", ACTION=="add", RUN+="/bin/su user -c /usr/local/bin/set_default_snd_output_device.sh"

Variables:
your user name

/usr/local/bin/set_default_snd_output_device.sh
Code: Select all
#!/bin/bash
#
# search for the first snd_usb_audio output device and set it as output default device
/usr/bin/pacmd set-default-sink $(/usr/bin/pacmd list | grep -B 40 -e "alsa.driver_name.=..snd_usb_audio." \
| grep -m1 -e 'name:..alsa_output' | awk '{print $2}' | sed -e 's/<//;s/>//') >/dev/null 2>&1


Activating:
udevadm control --reload-rules

Debugging:
udevadm monitor --property --subsystem-match=sound
Right event and subsystem ?
or/and
/usr/bin/pacmd list | grep -B 40 -e "alsa.driver_name.=..snd_usb_audio." | grep -e 'name:..alsa_output'
Right line picked ?

Feel free to play with it and give me some feedback.
cheers
/\ndy
AndyNZ
Level 1
Level 1
 
Posts: 3
Joined: Wed Sep 18, 2013 5:42 pm

Linux Mint is funded by ads and donations.
 

Return to Hardware Support

Who is online

Users browsing this forum: No registered users and 3 guests