cagnarrogna
cagnarrogna

Reputation: 105

Change audio card on pyalsaaudio

I need to change the audio device used on a python script. I'm using alsaaudio, and according to this http://pyalsaaudio.sourceforge.net/libalsaaudio.html this is achieved just by entering the card name when creating the PCM device. When I type on Python

import alsaaudio
alsaaudio.cards()

I get

[u'Intel', u'Q9000']

but creating the PCM object as

alsaaudio.PCM(aa.PCM_CAPTURE, aa.PCM_NORMAL, 'Q9000')

it returns

Unknown PCM default:CARD=Q9000

I've tried to modify the .asoundrc for having the configuration I want as default, but while it works with aplay and arecord on Python the default audio device is still the same.

On ~/.asoundrc I put

pcm.quicktimeWebCam
{
    type hw
    card Q9000
}

pcm.internal
{
    type hw
    card Intel
}

pcm.!default
{
    type asym
    playback.pcm
    {
        type plug
        slave.pcm "internal"
    }
    capture.pcm
    {
        type plug
        slave.pcm "quicktimeWebCam"
    }
}

Upvotes: 4

Views: 4633

Answers (3)

Ella Rose
Ella Rose

Reputation: 546

ALSA devices are represented by "hw:x,y", where x is the device and y is the subdevice (if any). It is expecting the card to be specified in the form of "hw:x,y", and not the human readable name.

card_info = {}
for device_number, card_name in enumerate(alsaaudio.cards()):
    card_info[card_name] = "hw:%s,0" % device_number

device = alsaaudio.PCM(card=card_info["Q9000"])

Upvotes: 4

MrAlias
MrAlias

Reputation: 1346

This seems to be a common problem and is a reported bug. The confusion lies in what alsaaudio's PCM object expects for the card argument. As the mentioned wiki would have you believe it is the desired card's name (technically ALSA calls this the ID). This is true as long as you have defined the card name as a control for the default device profile in the .asoundrc you are using.

On the other hand the card argument can accept the whole of a PCM handle as alluded to by CL above. As seen in the mentioned bug there is a patch submitted that would add this PCM handle identification functionality. Until that is added to the module a good way to determine the correct handle to pass to the PCM objects is by looking at the output of arecord -L (or possibly aplay -L if looking for a playback device name).

Upvotes: 0

CL.
CL.

Reputation: 180020

Device names for alsaaudio have some undocumented quirks; when you use a plain card ID (as returned by cards()), it is expected that you have a default device that accepts a card parameter. (The default default supports this parameter.)

To use a specific device, use plug:internal or plug:quicktimeWebCam.

Upvotes: 0

Related Questions