{"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

#!\/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

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

#!\/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

You could also do something different such as restarting PulseAudio<\/a> daemon:<\/p>\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

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 →<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"Changing the default sound card automatically in Linux — T's message of the day<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Changing the default sound card automatically in Linux — T's message of the day\" \/>\n<meta property=\"og:description\" content=\"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 →\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"T's message of the day\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/trustin\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/trustin\" \/>\n<meta property=\"article:published_time\" content=\"2008-04-20T02:23:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-27T16:45:55+00:00\" \/>\n<meta name=\"author\" content=\"Trustin Lee\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/trustin\" \/>\n<meta name=\"twitter:site\" content=\"@trustin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Trustin Lee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\"},\"author\":{\"name\":\"Trustin Lee\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\"},\"headline\":\"Changing the default sound card automatically in Linux\",\"datePublished\":\"2008-04-20T02:23:00+00:00\",\"dateModified\":\"2022-12-27T16:45:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\"},\"wordCount\":247,\"publisher\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\",\"url\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\",\"name\":\"Changing the default sound card automatically in Linux — T's message of the day\",\"isPartOf\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#website\"},\"datePublished\":\"2008-04-20T02:23:00+00:00\",\"dateModified\":\"2022-12-27T16:45:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vault.motd.kr\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Changing the default sound card automatically in Linux\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#website\",\"url\":\"https:\/\/vault.motd.kr\/wordpress\/\",\"name\":\"T's message of the day\",\"description\":\"the best is yet to come\",\"publisher\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vault.motd.kr\/wordpress\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d\",\"name\":\"Trustin Lee\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png\",\"contentUrl\":\"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png\",\"width\":966,\"height\":966,\"caption\":\"Trustin Lee\"},\"logo\":{\"@id\":\"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/t.motd.kr\/\",\"https:\/\/www.facebook.com\/trustin\",\"https:\/\/www.instagram.com\/trustinlee\/\",\"https:\/\/www.linkedin.com\/in\/trustin\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/trustin\",\"https:\/\/www.youtube.com\/@trustinlee\"]}]}<\/script>","yoast_head_json":{"title":"Changing the default sound card automatically in Linux — T's message of the day","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"Changing the default sound card automatically in Linux — T's message of the day","og_description":"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 →","og_url":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/","og_site_name":"T's message of the day","article_publisher":"https:\/\/www.facebook.com\/trustin","article_author":"https:\/\/www.facebook.com\/trustin","article_published_time":"2008-04-20T02:23:00+00:00","article_modified_time":"2022-12-27T16:45:55+00:00","author":"Trustin Lee","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/trustin","twitter_site":"@trustin","twitter_misc":{"Written by":"Trustin Lee","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/#article","isPartOf":{"@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/"},"author":{"name":"Trustin Lee","@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d"},"headline":"Changing the default sound card automatically in Linux","datePublished":"2008-04-20T02:23:00+00:00","dateModified":"2022-12-27T16:45:55+00:00","mainEntityOfPage":{"@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/"},"wordCount":247,"publisher":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/","url":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/","name":"Changing the default sound card automatically in Linux — T's message of the day","isPartOf":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#website"},"datePublished":"2008-04-20T02:23:00+00:00","dateModified":"2022-12-27T16:45:55+00:00","breadcrumb":{"@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vault.motd.kr\/wordpress\/posts\/1508\/changing-the-default-sound-card-automatically-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vault.motd.kr\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Changing the default sound card automatically in Linux"}]},{"@type":"WebSite","@id":"https:\/\/vault.motd.kr\/wordpress\/#website","url":"https:\/\/vault.motd.kr\/wordpress\/","name":"T's message of the day","description":"the best is yet to come","publisher":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vault.motd.kr\/wordpress\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/4430ad90fc2ddeef051565701d85db9d","name":"Trustin Lee","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png","contentUrl":"https:\/\/vault.motd.kr\/wordpress\/wp-content\/uploads\/2020\/09\/avatar-2019-966px.png","width":966,"height":966,"caption":"Trustin Lee"},"logo":{"@id":"https:\/\/vault.motd.kr\/wordpress\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/t.motd.kr\/","https:\/\/www.facebook.com\/trustin","https:\/\/www.instagram.com\/trustinlee\/","https:\/\/www.linkedin.com\/in\/trustin","https:\/\/twitter.com\/https:\/\/www.twitter.com\/trustin","https:\/\/www.youtube.com\/@trustinlee"]}]}},"_links":{"self":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts\/1508"}],"collection":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=1508"}],"version-history":[{"count":2,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"predecessor-version":[{"id":5967,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/posts\/1508\/revisions\/5967"}],"wp:attachment":[{"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vault.motd.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}