Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got two sound cards: a PCI and an USB one.

I'd like to have that, if the USB card is plugged in, that is used as default device on the whole system, otherwise (if the USB card isn't plugged) the PCI card gets used.

How can I do that?

share|improve this question

PulseAudio will handle switching audio outputs in this manner transparently. Just plug in the USB device, choose it either in the global sound preferences for the system or in pavucontrol for the applications you want, and PulseAudio will do as required.

share|improve this answer

Here's the Python script that I use:

#!/usr/bin/env python
# This script monitors USB connections and when the audio device is connected
# makes it the default output device.

import dbus, gobject, os
from dbus.mainloop.glib import DBusGMainLoop
from subprocess import Popen, PIPE

PCI_DEVICE = 'alsa_output.pci-0000_00_1b.0.analog-stereo'
USB_DEVICE = 'alsa_output.usb-Logitech_Logitech_Z205-00-default.analog-stereo'

def set_default_sink(sink):
    popen = Popen('pacmd', stdin = PIPE, stdout = PIPE)
    popen.communicate('set-default-sink ' + sink)

def device_added(udi):
    if udi.find('alsa') >= 0:

def device_removed(udi):
    if udi.find('alsa') >= 0:

DBusGMainLoop(set_as_default = True)
bus = dbus.SystemBus()
hal_manager = bus.get_object('org.freedesktop.Hal',
bus.add_signal_receiver(device_added, 'DeviceAdded',
                        'org.freedesktop.Hal.Manager', 'org.freedesktop.Hal',
bus.add_signal_receiver(device_removed, 'DeviceRemoved',
                        'org.freedesktop.Hal.Manager', 'org.freedesktop.Hal',


The sink names (PCI_DEVICE for the internal speakers and USB_DEVICE for the USB speakers) are specific to my machine, so you'll need to replace them. You can get these names from pacmd which is a part of pulseaudio-utils - just enter list-sinks in the pacmd prompt and find the names of your devices (USB speakers must be connected).

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .