{"id":1508,"date":"2008-04-20T02:23:00","date_gmt":"2008-04-20T02:23:00","guid":{"rendered":"http:\/\/gleamynode.net\/articles\/1508\/changing-the-default-sound-card-automatically-in-linux"},"modified":"2022-12-28T01:45:55","modified_gmt":"2022-12-27T16:45:55","slug":"changing-the-default-sound-card-automatically-in-linux","status":"publish","type":"post","link":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/","title":{"rendered":"Changing the default sound card automatically in Linux"},"content":{"rendered":"\n
Many people including me usually use a USB<\/span> sound card or a USB<\/span> speaker to enjoy noise-free high-fidelity sound. I simply don\u2019t understand why all the main board manufacturers ship with a built-in sound chipset which just sucks. It\u2019s not an exception for all laptops.<\/p>\n\n\n\n In a non-portable system such as a desktop PC, you usually don\u2019t need to change your default sound card because your USB<\/span> sound card is always connected. However, it\u2019s a whole different story for a laptop computer. USB<\/span> sound card is often disconnected and connected again. For example, I connected my USB<\/span> speaker to the docking station. The expected behaviour is that the default sound card is chosen automatically \u2013 the sound system should be reconfigured so that my USB<\/span> speaker becomes the default sound card when I dock to the docking station.<\/p>\n\n\n\n Currently, there\u2019s no desktop environment that addresses this problem AFAIK<\/span>, so I wrote a quick and dirty script file that reconfigures the sound system automatically when a new sound card is detected. The script assumes that you are running HAL<\/span> and DBUS<\/span>, which are very common in modern Linux distributions.<\/p>\n\n\n\n Another required script is alsa-reconfigure<\/tt>. The following is what I put into the alsa-reconfigure<\/tt> script. I reset the volume level here:<\/p>\n\n\n\n You could also do something different such as restarting PulseAudio<\/a> daemon:<\/p>\n\n\n\n I execute alsa-watch<\/tt> in my \/etc\/rc.local<\/tt> file and it works perfectly for me. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":" Many people including me usually use a USB sound card or a USB speaker to enjoy noise-free high-fidelity sound. I simply don\u2019t understand why all the main board manufacturers ship with a built-in sound chipset which just sucks. It\u2019s not an exception for all laptops. In a non-portable system such as a desktop PC, you… Continue reading #!\/bin\/sh\n# Path: \/usr\/local\/bin\/alsa-watch\n\n# Exit if running already.\nif [ \"x`pgrep -of 'alsa-watch'`\" != \"x$\" ]; then\n exit 1\nfi\n\n# Configure the sound card to the default.\n\/usr\/local\/bin\/alsa-reconfigure\n\n# Begin monitering.\n{\ndbus-monitor --system --monitor \"type='signal',path='\/org\/freedesktop\/Hal\/Manager',interface='org.freedesktop.Hal.Manager'\" | while read -r EVT; do\n echo \"$EVT\" | egrep -qi \"(DeviceAdded|DeviceRemoved)\"\n if [ \"$?\" = '0' ]; then\n read -r EVT_VAL\n echo \"$EVT_VAL\" | egrep -qi '(alsa_playback|sound_card)_[0-9]+\"'\n if [ \"$?\" = '0' ]; then\n # Reconfigure the sound card if a sound card is plugged in or out.\n \/usr\/local\/bin\/alsa-reconfigure\n fi\n fi\ndone\n} &<\/code><\/pre>\n\n\n\n
#!\/bin\/sh\n# Path: \/usr\/local\/bin\/alsa-reconfigure\n# Exit if there's no sound card.\n[ -f \/proc\/asound\/cards ] || exit 0\ncat \/proc\/asound\/cards | grep -qi 'no soundcard' && exit 0\n\n# Prefer USB audio device to other sound cards.\ncat \/proc\/asound\/cards | grep -qi USB-Audio\nif [ \"$?\" == \"0\" ]; then\n CARD=`cat \/proc\/asound\/cards | grep USB-Audio | head -1 | perl -pi -e \"s\/\\s*([0-9])+.*\/\\1\/\"`\n USB='y'\nelse\n CARD=`cat \/proc\/asound\/cards | head -1 | perl -pi -e \"s\/\\s*([0-9])+.*\/\\1\/\"`\n USB='n'\nfi\n\n# Update ALSA settings.\necho \"pcm.foo {\n type dmix\n slave.pcm \"hw:$CARD\"\n ipc_key 1024\n}\n\npcm.!default {\n type plug\n slave.pcm \"foo\"\n}\n\nctl.!default {\n type hw\n card $CARD\n}\n\" > \/etc\/asound.conf\nchmod 644 \/etc\/asound.conf\n\n# Reset the volume. (optional)\nif [ \"$USB\" == 'y' ]; then\n amixer sset 'PCM' 80% > \/dev\/null 2>&1\nelse\n amixer sset 'Master' 30% > \/dev\/null 2>&1\nfi<\/code><\/pre>\n\n\n\n
#!\/bin\/sh\n# Path: \/usr\/local\/bin\/alsa-reconfigure\n# Exit if there's no sound card.\n[ -f \/proc\/asound\/cards ] || exit 0\ncat \/proc\/asound\/cards | grep -qi 'no soundcard' && exit 0\n\n# Prefer USB audio device to other sound cards.\ncat \/proc\/asound\/cards | grep -qi USB-Audio\nif [ \"$?\" == \"0\" ]; then\n CARD=`cat \/proc\/asound\/cards | grep USB-Audio | head -1 | perl -pi -e \"s\/\\s*([0-9])+.*\/\\1\/\"`\nelse\n CARD=`cat \/proc\/asound\/cards | head -1 | perl -pi -e \"s\/\\s*([0-9])+.*\/\\1\/\"`\nfi\n\n# Update ALSA settings. (optional if your Linux distribution uses PulseAudio by default)\necho \"pcm.!default {\n type pulse\n}\n\nctl.!default {\n type pulse\n}\n\" > \/etc\/asound.conf\n\npkill -f '(^|\/)pulseaudio( |$)' > \/dev\/null 2>&1\nsleep 1\npkill -9 -f '(^|\/)pulseaudio( |$)' > \/dev\/null 2>&1\n\n# Restart PulseAudio daemon\npulseaudio \\\n --system --daemonize --high-priority --realtime --log-target=syslog \\\n --disallow-module-loading --disallow-exit \\\n --resample-method=src-sinc-best-quality --no-cpu-limit -n \\\n -L \"module-native-protocol-unix auth-anonymous=1\" \\\n -L \"module-native-protocol-tcp auth-ip-acl=192.168.0.0\/16;127.0.0.0\/8\" \\\n -L \"module-rescue-streams\" \\\n -L \"module-alsa-sink device=hw:$CARD\" \\<\/code><\/pre>\n\n\n\n