Monday, November 18, 2013

.asoundrc for Chrome and Hangouts with separate microphone

The default settings for a computer with a single sound device - the built-in chipset - tend to work pretty well for videoconferencing. Even though the camera may have a microphone, the system tends to like getting audio from the same place it sends it to, the default sound device. Both ALSA and PulseAudio will do this out of the box.

But what if you wanted to use the camera's microphone? If you use PulseAudio, you simply have to select that device in the Input tab of Sound Setting.

But what if you didn't want to use PulseAudio? What if you like a simple system with a minimal of latency? Then you would want to use ALSA directly.

The trouble is, Chrome tries to re-open the sound devices when it runs a Hangout. If you use PulseAudio or JACK, you will see Chrome open the sound device 2-3 times at once. Unfortunately, ALSA can't handle multiple processes opening a sound device all at once. ALSA will block the second and later users. If you only use ALSA, Chrome audio will then block as it tries opening a device that it already has open.

The way to solve this is to simply not use the default ALSA config file. If you make your own, you can use a device called "dmix" that will allow multiple sound producers to simultaneously open a device.

Additionally, we want to change the default microphone from the built-in one to a USB device, perhaps the camera perched atop your monitor.

For the benefit of the following discussion, here are my devices, as reported by a "arecord -l":

**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC262 Analog [ALC262 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 2: ALC262 Analog [ALC262 Analog]
  Subdevices: 2/2
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
card 2: AK5370 [AK5370], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 3: U0x46d0x821 [USB Device 0x46d:0x821], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

In my .asoundrc file, we're going to use the AK5370 device (even though the U0x46d0x821 is the USB camera. Feel free to switch it.)

Here's the .asoundrc file:

# Here we define the default sound device, the built-in Intel chipset,
# which we'll use for output.
pcm.PCH {
        type hw
        card PCH
}

# Here we define the device that will allow multiple processes - Chrome,
# vlc, etc. - to open the device at the same time. The secret is the "dmix"
# device.
pcm.multiPCH {
        # Chrome needs to open this multiple times.
        type dmix
        ipc_key 1024 # must be unique!
        slave {
                # Never use numbers ie. hw:0
                # Linux re-numbers them, depending on how it boots,
                # or how you plug them in.
                pcm PCH   # you cannot use a "plug" device here, darn.
                period_time 0
                period_size 1024 # must be power of 2
                buffer_size 4096 # ditto
                rate 48000
        }
}

# Here we define the raw microphone. By the way, it only supports
# 32kbs. ALSA does the conversion.
pcm.logitech32k {
        type hw
        # Never use numbers ie. hw:0
        # Linux re-numbers them.
        card AK5370
#       rate 32000
}

# Here's the last part, to put the output and input devices above together.
# A built-in sound chip doesn't need to be told that the output and input
# go together. But when you pull together 2 random devices, you need to tell
# ALSA how to put them together.
#
# We do this with the "asym" device, as in "asymmetric."
pcm.chrome {
        type asym
        playback.pcm "multiPCH"
        capture.pcm "logitech32k"
}

# This just defines the default device, which is our "asym" device above.
# Make a direct alias, no intermediate "plug" pcm
pcm.!default pcm.chrome

No comments:

Post a Comment