How can my audio input always be the webcam microphone?

Changing default settings for PulseAudio

When running pulseaudio all settings can be defined by putting custom configuration files in the hidden .pulse directory in your HOME. If these file do not yet exist there you can use the standard files from /etc/pulse/ as a template. Most entries have verbous explanations in these files.

  • ~/.pulse/default.pa to load modules and define defaults
  • ~/.pulse/client.conf to configure a client for the sound server
  • ~/.pulse/daemon.conf to define sample rates and buffers

To make your webcam the default source we need to know this source's name and number by looking through the list produced by the following command in a terminal:

pacmd list-sources

We then edit the following line in ~/.pulse/default.pa:

set-default-source [name]

and replace [name] by the name or the source number for the desired source. We then need to make sure that the source is unmuted by adding this line:

set-source-mute [name]|[#n]> 0

Give the name or the index of your source. You can add a variety of other settings that are referenced in the PulseAudio Wiki (now on man page: man pulse-cli-syntax).

It is recommended to first test all commands through the command line interface before changing the configuration files. This can be opened with pacmd in a terminal


I agree that the problem is that Pulseaudio does not set a USB device as default because it is not present when pulse starts.

Soo my solution to this is to hook into the system that does notice when a USB device is connected... UDEV!

BTW, these instructions are slightly funky, and I am assuming that you are comfortable doing stuff at the command line, and becoming the root user to update udev files, and making scripts executable.

First, find out stuff

USB Id

First step is to plug in your webcam and find the magic vendor and product numbers by typing

lsusb

I'm using a Microsoft Lifecam, and it show as

Bus 001 Device 013: ID 045e:072d Microsoft Corp.

the important part is 045e:072d

Pulseaudio device

Next, while the webcam is connected, find out the pulseaudio device name. Type

pacmd list-sources

Again, I'm looking for the line that starts name: then has something about a Microsoft Lifecam... here it is

name: <alsa_input.usb-Microsoft_Microsoft___LifeCam_VX-5500-02-VX5500.analog-mono>

so my pulseaudio device name will be alsa_input.usb-Microsoft_Microsoft___LifeCam_VX-5500-02-VX5500.analog-mono

UDEV rules

Then I made a new udev rule which is fired when a USB device with vendor=045e and product=072d is inserted. To make a rule, create a new file called /etc/udev/rules.d/SOMETHING.rules (I called my file /etc/udev/rules.d/95-webcam-mic.rules).

Put this line into the file

SUBSYSTEMS=="usb", ATTR{idVendor}=="045e", ATTR{idProduct}=="072d", ACTION=="add", GROUP="audio", RUN+="/usr/local/bin/webcam-mic"

Remember to replace the ATTR{idVendor} and ATTR{idProduct} with the values for your webcam.

What this rule does is run a script to set the usb device for input, that is the RUN+="/usr/local/bin/webcam-mic" part.

Finally, set the webcam for input

Quick overview time. Each time I connect my webcam I want to run the command

pacmd set-default-source alsa_input.usb-Microsoft_Microsoft___LifeCam_VX-5500-02-VX5500.analog-mono

But the command needs to run as me (the user running the desktop), but udev runs as the root user - so when udev runs my command I need it to change to my user then run the command.

So we have 2 options.

  1. Quick and dirty. Make the udev rule run su james -c pacmd set-default-source... this is sooo wrong I can't even finish typing the command. What if you change webcams? What if you install this for another user?

  2. Slightly cleaner. Run a script that reads a defaults file which has the pulseaudio device name, and your username in it. If you want the files they are on GitHub. First the script /usr/local/bin/webcam-mic. The defaults file /etc/default/webcam-mic just needs to define the 2 shell variables, as per the comment in the gist. Put the script into /usr/local/bin/ and make it executable. Then put the defaults file into /etc/defaults/ and edit it to use your username and the pulseaudio device from earlier.

Good luck!


The solution by Takkat was not working for me, here is my solution for anyone still having a problem with this.

There were two issues for me with the solution by Takkat. First, I realised that there needs to be a delay after startup before the switch to the microphone can take place (I use the command 'sleep' in my solution below). Also, the command 'set-default-source' was working to switch the USB microphone on, but somehow Skype was not recognising this and continued to use the internal microphone.

For this to work, first it is useful to install pavucontrol. In pavucontrol, open the 'Configuration' tab, and observe what happens with the commands below to check things are working as intended.

Then, from a terminal enter:

pacmd

From there, enter:

list-cards

In the output, find the index number of the USB audio you want (in my case, this was '1'). Then have a look at the lines listed under 'profiles' for both the default card and USB card. These should be the same as the ones listed in pavucontrol, and can be something like 'ouput:input+output', 'off', or 'input:analog-mono' etc. Still in the pacmd command line, the command set-card-profile <index number> <profile name> should change the settings in pavucontrol. What we want to do here is to switch the internal audio from duplex to output only, and the USB to input.

Once found the appropriate profiles, we want to create a startup command to change a few settings just after startup. To create a startup command, open the dash and search for 'startup applications', then click 'add', then enter a name of your choice and the appropriate command below.

After trial and error, my start up command ended up looking like this:

bash -c "sleep 30 && pacmd set-card-profile 0 output:analog-stereo && pacmd set-card-profile 1 off && pacmd set-card-profile 1 input:analog-mono"

Explanation: I found that the command 'sleep' was necessary, this means that the command is executed after a pause, that I set to 30 seconds (I think this can be less, play around with this). The next command (after the '&&') is to get the default (internal) card profile to switch from input+output (duplex, which is the default) to output only. The next command is for the USB sound card (in my case, the number is '1') to switch off. The last command is for this same card to switch back on in the mode 'input:analog-mono'

You might have to play around with these settings, to find the one that works.

Now when I startup, my USB microphone is ready to use after 30 seconds, and Skype uses the correct microphone.