Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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:
        set_default_sink(USB_DEVICE)

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

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

gobject.MainLoop().run()

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.