alsa

You are currently browsing articles tagged alsa.

Linux usb audio problems.

I’ve had some linux usb audio trouble recently. I’ve noticed this is a common problem, so I figure I should detail my experience somewhere to add to the global self help desk.

Recently I bought an Edirol/Roland UA-1EX external usb sound card.
My usb sound card

It comes more from the music world then the computer world, so it far better audio then most sound cards. This also means that it has no internal mixer, as that’s not it’s job. This isn’t a problem in windows, as all the mixing is automatically done in software, but in linux sound is one of those areas that don’t just work (yet).

The fact that people are looking at these issues seriously is a really good sign. Like so many desktop linux issues in the past I’m sure this will improve in the future.

USB sound cards basically confuse the hell out of linux. Or rather it just doesn’t do things automatically yet. I plugged it in optimistically expecting to get a nice prompt on whether I’d like to configure it or something. Hah! Some of the most common problems people had that I googled across, was that their card was detected, but they still had no sound, or for some applications sound would come out the usb card and for others apps sound would come out their onboard card.

So after many googles, I found a solution. For my problem anyway. Which I chronicle below for sentimental reasons.

The first thing is, in Ubuntu 7.04 the UA-1EX is only detected automatically by ALSA (advanced linux sound architecture) when the advanced driver switch on the card is off. It took a while for me to figure that out. I can live without whatever it is as long as I have pretty sounds, so I switched it to off.

Basically if it shows up in the file /proc/asound/cards it is working.
Also, use aplay -l to display a list of currently available cards.

My /proc/asound/cards file looks like this:

0 [nForce2 ]: NFORCE - NVidia nForce2
NVidia nForce2 with ALC650F at 0xeb002000, irq 17
1 [UART ]: MPU-401 UART - MPU-401 UART
MPU-401 UART at 0x330, irq 10
2 [UA1EX ]: USB-Audio - UA-1EX
EDIROL UA-1EX at usb-0000:00:02.1-2, full speed

It’s important to note that since the UA-1EX is a USB card, it wont always be at number 2. USB things get loaded in all sorts of unpredictable orders at boot (or after boot if you plug it in late), sometimes I boot up and it’s 0. Since the standard way of referring to cards is by their number (which obviously wont always work), it’s best to use it’s name UA1EX instead.

So I have the card there, but there’s still no sound. What the fuck now? I noticed in gnome under system->preferences->sound, I can select USB audio for everything (except the mixer). Testing this, I have sound in esd (enlightenment sound daemon) applications, ie, the usual gnome applications for things, like rhythmbox, movie player etc, but not amarok (which I use because it shits over rhythmbox) or any other gnome unfriendly apps. Also the volume control in the system tray doesn’t do a thing. This is because I can only select the onboard sound card as the mixer.

The biggest problem though is that the sound will only work with one application at a time, second applications can’t open it because it’s busy. I did manage to get Amarok to work eventually, but I wont go into it since the way I fixed all this solved the Amarok problem too.

The problems with the gnome apps and the sys tray volume control was because like many usb sound cards the UA-1EX doesn’t have an internal hardware mixer. This isn’t bad, on the contrary it just means it doesn’t care about those fiddly things. It’s got more important things to do, like spitting out good audio. It means though that only one application can use the device at a time (until I add a software mixer that is). Windows is smart enough to do the software mixing automatically, these sort of desktop conveniences are one of the rare of things windows is actually good at.

The problems with the other apps, was because they are just talking directly to the ALSA or OSS (predecessor of ALSA) system rather then the ESD. As it turns out the ALSA system was just sending the sound to the onboard sound card because it was the first one in the cards list. So I had some apps with sound coming out the usb card, and others coming out of the onboard card. You’d think that gnome would tell ALSA where I wanted all the sound, but as we know, sound in linux doesn’t just work (yet).

The solution to all these problems is just make ALSA tell gnome what to do instead, and then tell ALSA what to do.

Under System->Preferences->Sound->Devices set all the outputs and inputs to ALSA. The mixer I just left as the onboard card, so once again system tray volume control is now useless, but I couldn’t be bothered working around that since I have an amp sitting on my real desktop.

So all aware desktop linux apps will use ALSA. Now all we need to do is tell ALSA what device to use. I think it defaults to the first one in the /proc/asound/cards list. So we have to correct it with an .asoundrc file. It’s a file that lets you tell ALSA to do fancy things. You can mix together multiple sound cards, all sort of things. I only wanted to replace my useless on board sound card completely so I just needed a really simple file.

You can create a file called .asoundrc in your home directory which ALSA will look at, but I preferred, to use a system wide one as /etc/asound.conf. Otherwise I wouldn’t have sound till I logged in, thus missing out on the login prompt sound (oh no!), also of course then multiple users will get sound that works as long as they tell gnome to use ALSA in their preferences.

My asound.conf file started out like this:


#/etc/asound.conf

pcm.!default {
type hw
card "UA1EX"
}
ctl.!default {
type hw
card "UA1EX"
}

This tells ALSA the default device is UA1EX, and the default control (ie the mixer) is the “UA1EX”. Since the UA-1EX doesn’t have a mixer, this seems a bit silly. Still it sort of worked, the default device was now my usb audio card and I had sound with everything, everyone was happy. Unless of course two things tried to use it at once. This can be fixed though :)

It might be worth pointing out, if this sort of setup works for someone with a different usb sound card and the mixer works then there is nothing more to do. You can test to see if your card has a mixer with this setup, by using the command, alsamixer. If it can’t find a control thingi (mixer) on the card specified it will tell you, other wise you will get a nice display that lets you adjust different volume controls.

So since I don’t have an internal hardware mixer, I want it to be done in software. ALSA can do this with dmix.
See version two of my asound.conf


#/etc/asound.conf

pcm.!default {
type plug
slave.pcm "dmixer"
}

pcm.dmixer {
type dmix
ipc_key 1024
slave {
pcm "hw:UA1EX"
period_time 0
period_size 1024
buffer_size 4096
rate 44100
}
bindings {
0 0
1 1
}
}

ctl.dmixer {
type hw
card "UA1EX"
}

I got this off the ALSA website. The only thing I changed is the type to be plug, since it’s a usb card, and obviously the card name to be UA1EX instead of a number. I honestly have no idea what the details of this really means. But basically it sets dmix to be the main device thus giving us a software mixer. The default device is set to be a slave of the alias dmixer.

Anyway the important thing is, ALSA can set up software mixing. All apps using ALSA will just spit out sound, ALSA will mix them together and send it onto the usb audio device.

So now it all works! Starting the command alsamixer brings up the volume controls. The system tray gnome volume control doesn’t work but that doesn’t bother me. All the application volume controls talk to the software mixer in ALSA, so it’s really no big deal, Amarok even gets that pretty fade out when you close it.

Hurrah! right? Well not quite, as with all things linux there’s always exceptions. Some applications just aren’t smart enough to work intelligently with ALSA or whatever library they use (here’s looking at you Neverwinter Nights). Often they’ll try to use the old predecessor known as OSS (open sound system), yes I mean you Java sound library! (this has implications for Puzzle Pirates). These are pretty rare though, and there are often workarounds. I might make another post about this since this is kind of long. Specifically about Puzzle Pirates and NWN.

If anyone has any suggestions or improvements on this setup please let me know.
Bleh that was long!

Tags: , , , , , ,