alsaaudio

The alsaaudio module defines functions and classes for using ALSA.

alsaaudio.pcms(pcmtype=PCM_PLAYBACK)

List available PCM devices by name.

Arguments are:

  • pcmtype - can be either PCM_CAPTURE or PCM_PLAYBACK (default).

Note:

For PCM_PLAYBACK, the list of device names should be equivalent to the list of device names that aplay -L displays on the commandline:

$ aplay -L

For PCM_CAPTURE, the list of device names should be equivalent to the list of device names that arecord -L displays on the commandline:

$ arecord -L

New in 0.8

alsaaudio.cards()

List the available ALSA cards by name. This function is only moderately useful. If you want to see a list of available PCM devices, use pcms() instead.

alsaaudio.mixers(cardindex=-1, device='default')

List the available mixers. The arguments are:

  • cardindex - the card index. If this argument is given, the device name is constructed as: ‘hw:cardindex’ and the device keyword argument is ignored. 0 is the first hardware sound card.

    Note: This should not be used, as it bypasses most of ALSA’s configuration.

  • device - the name of the device on which the mixer resides. The default is 'default'.

Note: For a list of available controls, you can also use amixer on the commandline:

$ amixer

To elaborate the example, calling mixers() with the argument cardindex=0 should give the same list of Mixer controls as:

$ amixer -c 0

And calling mixers() with the argument device='foo' should give the same list of Mixer controls as:

$ amixer -D foo

Changed in 0.8:

  • The keyword argument device is new and can be used to select virtual devices. As a result, the default behaviour has subtly changed. Since 0.8, this functions returns the mixers for the default device, not the mixers for the first card.

alsaaudio.asoundlib_version()

Return a Python string containing the ALSA version found.

PCM Objects

PCM objects in alsaaudio can play or capture (record) PCM sound through speakers or a microphone. The PCM constructor takes the following arguments:

class alsaaudio.PCM(type=PCM_PLAYBACK, mode=PCM_NORMAL, rate=44100, channels=2, format=PCM_FORMAT_S16_LE, periodsize=32, periods=4, device='default', cardindex=-1)

This class is used to represent a PCM device (either for playback and recording). The arguments are:

  • type - can be either PCM_CAPTURE or PCM_PLAYBACK (default).

  • mode - can be either PCM_NONBLOCK, or PCM_NORMAL (default).

  • rate - the sampling rate in Hz. Typical values are 8000 (mainly used for telephony), 16000, 44100 (default), 48000 and 96000.

  • channels - the number of channels. The default value is 2 (stereo).

  • format - the data format. This controls how the PCM device interprets data for playback, and how data is encoded in captures. The default value is PCM_FORMAT_S16_LE.

Format

Description

PCM_FORMAT_S8

Signed 8 bit samples for each channel

PCM_FORMAT_U8

Unsigned 8 bit samples for each channel

PCM_FORMAT_S16_LE

Signed 16 bit samples for each channel Little Endian byte order)

PCM_FORMAT_S16_BE

Signed 16 bit samples for each channel (Big Endian byte order)

PCM_FORMAT_U16_LE

Unsigned 16 bit samples for each channel (Little Endian byte order)

PCM_FORMAT_U16_BE

Unsigned 16 bit samples for each channel (Big Endian byte order)

PCM_FORMAT_S24_LE

Signed 24 bit samples for each channel (Little Endian byte order in 4 bytes)

PCM_FORMAT_S24_BE

Signed 24 bit samples for each channel (Big Endian byte order in 4 bytes)

PCM_FORMAT_U24_LE

Unsigned 24 bit samples for each channel (Little Endian byte order in 4 bytes)

PCM_FORMAT_U24_BE

Unsigned 24 bit samples for each channel (Big Endian byte order in 4 bytes)

PCM_FORMAT_S32_LE

Signed 32 bit samples for each channel (Little Endian byte order)

PCM_FORMAT_S32_BE

Signed 32 bit samples for each channel (Big Endian byte order)

PCM_FORMAT_U32_LE

Unsigned 32 bit samples for each channel (Little Endian byte order)

PCM_FORMAT_U32_BE

Unsigned 32 bit samples for each channel (Big Endian byte order)

PCM_FORMAT_FLOAT_LE

32 bit samples encoded as float (Little Endian byte order)

PCM_FORMAT_FLOAT_BE

32 bit samples encoded as float (Big Endian byte order)

PCM_FORMAT_FLOAT64_LE

64 bit samples encoded as float (Little Endian byte order)

PCM_FORMAT_FLOAT64_BE

64 bit samples encoded as float (Big Endian byte order)

PCM_FORMAT_MU_LAW

A logarithmic encoding (used by Sun .au files and telephony)

PCM_FORMAT_A_LAW

Another logarithmic encoding

PCM_FORMAT_IMA_ADPCM

A 4:1 compressed format defined by the Interactive Multimedia Association.

PCM_FORMAT_MPEG

MPEG encoded audio?

PCM_FORMAT_GSM

9600 bits/s constant rate encoding for speech

PCM_FORMAT_S24_3LE

Signed 24 bit samples for each channel (Little Endian byte order in 3 bytes)

PCM_FORMAT_S24_3BE

Signed 24 bit samples for each channel (Big Endian byte order in 3 bytes)

PCM_FORMAT_U24_3LE

Unsigned 24 bit samples for each channel (Little Endian byte order in 3 bytes)

PCM_FORMAT_U24_3BE

Unsigned 24 bit samples for each channel (Big Endian byte order in 3 bytes)

  • periodsize - the period size in frames. Make sure you understand the meaning of periods. The default value is 32, which is below the actual minimum of most devices, and will therefore likely be larger in practice.

  • periods - the number of periods in the buffer. The default value is 4.

  • device - the name of the PCM device that should be used (for example a value from the output of pcms()). The default value is 'default'.

  • cardindex - the card index. If this argument is given, the device name is constructed as ‘hw:cardindex’ and the device keyword argument is ignored. 0 is the first hardware sound card.

    Note: This should not be used, as it bypasses most of ALSA’s configuration.

This will construct a PCM object with the given settings.

Changed in 0.10:

  • Added the optional named parameter periods.

Changed in 0.9:

  • Added the optional named parameters rate, channels, format and periodsize.

Changed in 0.8:

  • The card keyword argument is still supported, but deprecated. Please use device instead.

  • The keyword argument cardindex was added.

The card keyword is deprecated because it guesses the real ALSA name of the card. This was always fragile and broke some legitimate usecases.

PCM objects have the following methods:

PCM.info()

The info function returns a dictionary containing the configuration of a PCM device. As ALSA takes into account limitations of the hardware and software devices the configuration achieved might not correspond to the values used during creation. There is therefore a need to check the realised configuration before processing the sound coming from the device or before sending sound to a device. A small subset of parameters can be set, but cannot be queried. These parameters are stored by alsaaudio and returned as they were given by the user, to distinguish them from parameters retrieved from ALSA these parameters have a name prefixed with “ (call value) “. Yet another set of properties derives directly from the hardware and can be obtained through ALSA.

Key

Description (Reference)

Type

name

PCM():device

string

card_no

index of card

integer (negative indicates device not associable with a card)

device_no

index of PCM device

integer

subdevice_no

index of PCM subdevice

integer

state

name of PCM state

string

access_type

name of PCM access type

string

(call value) type

PCM():type

integer

(call value) type_name

PCM():type

string

(call value) mode

PCM():mode

integer

(call value) mode_name

PCM():mode

string

format

PCM():format

integer

format_name

PCM():format

string

format_description

PCM():format

string

subformat_name

name of PCM subformat

string

subformat_description

description of subformat

string

channels

PCM():channels

integer

rate

PCM():rate

integer (Hz)

period_time

period duration

integer (\(\mu s\))

period_size

PCM():period_size

integer (frames)

buffer_time

buffer time

integer (\(\mu s\)) (negative indicates error)

buffer_size

buffer size

integer (frames) (negative indicates error)

get_periods

approx. periods in buffer

integer (negative indicates error)

rate_numden

numerator, denominator

tuple (integer (Hz), integer (Hz))

significant_bits

significant bits in sample

integer (negative indicates error)

is_batch

hw: double buffering

boolean (True: hardware supported)

is_block_transfer

hw: block transfer

boolean (True: hardware supported)

is_double

hw: double buffering

boolean (True: hardware supported)

is_half_duplex

hw: half-duplex

boolean (True: hardware supported)

is_joint_duplex

hw: joint-duplex

boolean (True: hardware supported)

can_overrange

hw: overrange detection

boolean (True: hardware supported)

can_mmap_sample_resolution

hw: sample-resol. mmap

boolean (True: hardware supported)

can_pause

hw: pause

boolean (True: hardware supported)

can_resume

hw: resume

boolean (True: hardware supported)

can_sync_start

hw: synchronized start

boolean (True: hardware supported)

The italicized descriptions give a summary of the “full” description as it can be found in the ALSA documentation. “hw:”: indicates that the property indicated relates to the hardware. Parameters passed to the PCM object during instantation are prefixed with “PCM():”, they are described there for the keyword argument indicated after “PCM():”.

PCM.pcmtype()

Returns the type of PCM object. Either PCM_CAPTURE or PCM_PLAYBACK.

PCM.pcmmode()

Return the mode of the PCM object. One of PCM_NONBLOCK, PCM_ASYNC, or PCM_NORMAL

PCM.cardname()

Return the name of the sound card used by this PCM object.

PCM.setchannels(nchannels)

Deprecated since version 0.9: Use the channels named argument to PCM().

PCM.setrate(rate)

Deprecated since version 0.9: Use the rate named argument to PCM().

PCM.setformat(format)

Deprecated since version 0.9: Use the format named argument to PCM().

PCM.setperiodsize(period)

Deprecated since version 0.9: Use the periodsize named argument to PCM().

PCM.info()

Returns a dictionary with the PCM object’s configured parameters.

Values are retrieved from the ALSA library if they are available; otherwise they represent those stored by pyalsaaudio, and their keys are prefixed with ‘ (call value) ‘.

New in 0.9.1

PCM.dumpinfo()

Dumps the PCM object’s configured parameters to stdout.

PCM.state()

Returs the current state of the stream, which can be one of PCM_STATE_OPEN (this should not actually happen), PCM_STATE_SETUP (after drop() or drain()), PCM_STATE_PREPARED (after construction), PCM_STATE_RUNNING, PCM_STATE_XRUN, PCM_STATE_DRAINING, PCM_STATE_PAUSED, PCM_STATE_SUSPENDED, and PCM_STATE_DISCONNECTED.

New in 0.10

PCM.read()

In PCM_NORMAL mode, this function blocks until a full period is available, and then returns a tuple (length,data) where length is the number of frames of captured data, and data is the captured sound frames as a string. The length of the returned data will be periodsize*framesize bytes.

In PCM_NONBLOCK mode, the call will not block, but will return (0,'') if no new period has become available since the last call to read.

In case of an overrun, this function will return a negative size: -EPIPE. This indicates that data was lost, even if the operation itself succeeded. Try using a larger periodsize.

PCM.write(data)

Writes (plays) the sound in data. The length of data must be a multiple of the frame size, and should be exactly the size of a period. If less than ‘period size’ frames are provided, the actual playout will not happen until more data is written.

If the device is not in PCM_NONBLOCK mode, this call will block if the kernel buffer is full, and until enough sound has been played to allow the sound data to be buffered. The call always returns the size of the data provided.

In PCM_NONBLOCK mode, the call will return immediately, with a return value of zero, if the buffer is full. In this case, the data should be written at a later time.

Note that this call completing means only that the samples were buffered in the kernel, and playout will continue afterwards. Make sure that the stream is drained before discarding the PCM handle.

PCM.pause([enable=True])

If enable is True, playback or capture is paused. Otherwise, playback/capture is resumed.

PCM.drop()

Stop the stream and drop residual buffered frames.

New in 0.9

PCM.drain()

For PCM_PLAYBACK PCM objects, play residual buffered frames and then stop the stream. In PCM_NORMAL mode, this function blocks until all pending playback is drained.

For PCM_CAPTURE PCM objects, this function is not very useful.

New in 0.10

PCM.close()

Closes the PCM device.

For PCM_PLAYBACK PCM objects in PCM_NORMAL mode, this function blocks until all pending playback is drained.

PCM.polldescriptors()

Returns a list of tuples of (file descriptor, eventmask) that can be used to wait for changes on the PCM with select.poll.

The eventmask value is compatible with `poll.register`__ in the Python select module.

PCM.set_tstamp_mode([mode=PCM_TSTAMP_ENABLE])

Set the ALSA timestamp mode on the device. The mode argument can be set to either PCM_TSTAMP_NONE or PCM_TSTAMP_ENABLE.

PCM.get_tstamp_mode()

Return the integer value corresponding to the ALSA timestamp mode. The return value can be either PCM_TSTAMP_NONE or PCM_TSTAMP_ENABLE.

PCM.set_tstamp_type([type=PCM_TSTAMP_TYPE_GETTIMEOFDAY])

Set the ALSA timestamp mode on the device. The type argument can be set to either PCM_TSTAMP_TYPE_GETTIMEOFDAY, PCM_TSTAMP_TYPE_MONOTONIC or PCM_TSTAMP_TYPE_MONOTONIC_RAW.

PCM.get_tstamp_type()

Return the integer value corresponding to the ALSA timestamp type. The return value can be either PCM_TSTAMP_TYPE_GETTIMEOFDAY, PCM_TSTAMP_TYPE_MONOTONIC or PCM_TSTAMP_TYPE_MONOTONIC_RAW.

PCM.htimestamp()

Return a Python tuple (seconds, nanoseconds, frames_available_in_buffer).

The type of output is controlled by the tstamp_type, as described in the table below.

Timestamp Type

Description

PCM_TSTAMP_TYPE_GETTIMEOFDAY

System-wide realtime clock with seconds since epoch.

PCM_TSTAMP_TYPE_MONOTONIC

Monotonic time from an unspecified starting time. Progress is NTP synchronized.

PCM_TSTAMP_TYPE_MONOTONIC_RAW

Monotonic time from an unspecified starting time using only the system clock.

The timestamp mode is controlled by the tstamp_mode, as described in the table below.

Timestamp Mode

Description

PCM_TSTAMP_NONE

No timestamp.

PCM_TSTAMP_ENABLE

Update timestamp at every hardware position update.

A few hints on using PCM devices for playback

The most common reason for problems with playback of PCM audio is that writes to PCM devices must exactly match the data rate of the device.

If too little data is written to the device, it will underrun, and ugly clicking sounds will occur. Conversely, of too much data is written to the device, the write function will either block (PCM_NORMAL mode) or return zero (PCM_NONBLOCK mode).

If your program does nothing but play sound, the best strategy is to put the device in PCM_NORMAL mode, and just write as much data to the device as possible. This strategy can also be achieved by using a separate thread with the sole task of playing out sound.

In GUI programs, however, it may be a better strategy to setup the device, preload the buffer with a few periods by calling write a couple of times, and then use some timer method to write one period size of data to the device every period. The purpose of the preloading is to avoid underrun clicks if the used timer doesn’t expire exactly on time.

Also note, that most timer APIs that you can find for Python will accummulate time delays: If you set the timer to expire after 1/10’th of a second, the actual timeout will happen slightly later, which will accumulate to quite a lot after a few seconds. Hint: use time.time() to check how much time has really passed, and add extra writes as nessecary.

Mixer Objects

Mixer objects provides access to the ALSA mixer API.

class alsaaudio.Mixer(control='Master', id=0, cardindex=-1, device='default')

Arguments are:

  • control - specifies which control to manipulate using this mixer object. The list of available controls can be found with the alsaaudio.mixers() function. The default value is 'Master' - other common controls may be 'Master Mono', 'PCM', 'Line', etc.

  • id - the id of the mixer control. Default is 0.

  • cardindex - specifies which card should be used. If this argument is given, the device name is constructed like this: ‘hw:cardindex’ and the device keyword argument is ignored. 0 is the first sound card.

  • device - the name of the device on which the mixer resides. The default value is 'default'.

Changed in 0.8:

  • The keyword argument device is new and can be used to select virtual devices.

Mixer objects have the following methods:

Mixer.cardname()

Return the name of the sound card used by this Mixer object

Mixer.mixer()

Return the name of the specific mixer controlled by this object, For example 'Master' or 'PCM'

Mixer.mixerid()

Return the ID of the ALSA mixer controlled by this object.

Mixer.switchcap()

Returns a list of the switches which are defined by this specific mixer. Possible values in this list are:

Switch

Description

‘Mute’

This mixer can mute

‘Joined Mute’

This mixer can mute all channels at the same time

‘Playback Mute’

This mixer can mute the playback output

‘Joined Playback Mute’

Mute playback for all channels at the same time}

‘Capture Mute’

Mute sound capture

‘Joined Capture Mute’

Mute sound capture for all channels at a time}

‘Capture Exclusive’

Not quite sure what this is

To manipulate these switches use the setrec() or setmute() methods

Mixer.volumecap()

Returns a list of the volume control capabilities of this mixer. Possible values in the list are:

Capability

Description

‘Volume’

This mixer can control volume

‘Joined Volume’

This mixer can control volume for all channels at the same time

‘Playback Volume’

This mixer can manipulate the playback output

‘Joined Playback Volume’

Manipulate playback volumne for all channels at the same time

‘Capture Volume’

Manipulate sound capture volume

‘Joined Capture Volume’

Manipulate sound capture volume for all channels at a time

Mixer.getenum()

For enumerated controls, return the currently selected item and the list of items available.

Returns a tuple (string, list of strings).

For example, my soundcard has a Mixer called Mono Output Select. Using amixer, I get:

$ amixer get "Mono Output Select"
Simple mixer control 'Mono Output Select',0
  Capabilities: enum
  Items: 'Mix' 'Mic'
  Item0: 'Mix'

Using alsaaudio, one could do:

>>> import alsaaudio
>>> m = alsaaudio.Mixer('Mono Output Select')
>>> m.getenum()
('Mix', ['Mix', 'Mic'])

This method will return an empty tuple if the mixer is not an enumerated control.

Mixer.setenum(index)

For enumerated controls, sets the currently selected item. index is an index into the list of available enumerated items returned by getenum().

Mixer.getrange(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_RAW)

Return the volume range of the ALSA mixer controlled by this object. The value is a tuple of integers whose meaning is determined by the units argument.

The optional pcmtype argument can be either PCM_PLAYBACK or PCM_CAPTURE, which is relevant if the mixer can control both playback and capture volume. The default value is PCM_PLAYBACK if the mixer has playback channels, otherwise it is PCM_CAPTURE.

The optional units argument can be one of VOLUME_UNITS_PERCENTAGE, VOLUME_UNITS_RAW, or VOLUME_UNITS_DB.

Mixer.getvolume(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_PERCENTAGE)

Returns a list with the current volume settings for each channel. The list elements are integers whose meaning is determined by the units argument.

The optional pcmtype argument can be either PCM_PLAYBACK or PCM_CAPTURE, which is relevant if the mixer can control both playback and capture volume. The default value is PCM_PLAYBACK if the mixer has playback channels, otherwise it is PCM_CAPTURE.

The optional units argument can be one of VOLUME_UNITS_PERCENTAGE, VOLUME_UNITS_RAW, or VOLUME_UNITS_DB.

Mixer.setvolume(volume, channel=None, pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_PERCENTAGE)

Change the current volume settings for this mixer. The volume argument is an integer whose meaning is determined by the units argument.

If the optional argument channel is present, the volume is set only for this channel. This assumes that the mixer can control the volume for the channels independently.

The optional pcmtype argument can be either PCM_PLAYBACK or PCM_CAPTURE, which is relevant if the mixer can control both playback and capture volume. The default value is PCM_PLAYBACK if the mixer has playback channels, otherwise it is PCM_CAPTURE.

The optional units argument can be one of VOLUME_UNITS_PERCENTAGE, VOLUME_UNITS_RAW, or VOLUME_UNITS_DB.

Mixer.getmute()

Return a list indicating the current mute setting for each channel. 0 means not muted, 1 means muted.

This method will fail if the mixer has no playback switch capabilities.

Mixer.setmute(mute[, channel])

Sets the mute flag to a new value. The mute argument is either 0 for not muted, or 1 for muted.

The optional channel argument controls which channel is muted. The default is to set the mute flag for all channels.

This method will fail if the mixer has no playback mute capabilities

Mixer.getrec()

Return a list indicating the current record mute setting for each channel. 0 means not recording, 1 means recording.

This method will fail if the mixer has no capture switch capabilities.

Mixer.setrec(capture[, channel])

Sets the capture mute flag to a new value. The capture argument is either 0 for no capture, or 1 for capture.

The optional channel argument controls which channel is changed. The default is to set the capture flag for all channels.

This method will fail if the mixer has no capture switch capabilities.

Mixer.polldescriptors()

Returns a list of tuples of (file descriptor, eventmask) that can be used to wait for changes on the mixer with select.poll.

The eventmask value is compatible with `poll.register`__ in the Python select module.

Mixer.handleevents()

Acknowledge events on the polldescriptors() file descriptors to prevent subsequent polls from returning the same events again. Returns the number of events that were acknowledged.

Mixer.close()

Closes the Mixer device.

A rant on the ALSA Mixer API

The ALSA mixer API is extremely complicated - and hardly documented at all. alsaaudio implements a much simplified way to access this API. In designing the API I’ve had to make some choices which may limit what can and cannot be controlled through the API. However, if I had chosen to implement the full API, I would have reexposed the horrible complexity/documentation ratio of the underlying API. At least the alsaaudio API is easy to understand and use.

If my design choises prevents you from doing something that the underlying API would have allowed, please let me know, so I can incorporate these needs into future versions.

If the current state of affairs annoys you, the best you can do is to write a HOWTO on the API and make this available on the net. Until somebody does this, the availability of ALSA mixer capable devices will stay quite limited.

Unfortunately, I’m not able to create such a HOWTO myself, since I only understand half of the API, and that which I do understand has come from a painful trial and error process.

Examples

The following example are provided:

  • playwav.py

  • recordtest.py

  • playbacktest.py

  • mixertest.py

All examples (except mixertest.py) accept the commandline option -c <cardname>.

To determine a valid card name, use the commandline ALSA player:

$ aplay -L

or:

$ python

>>> import alsaaudio
>>> alsaaudio.pcms()

mixertest.py accepts the commandline options -d <device> and -c <cardindex>.

playwav.py

playwav.py plays a wav file.

To test PCM playback (on your default soundcard), run:

$ python playwav.py <wav file>

recordtest.py and playbacktest.py

recordtest.py and playbacktest.py will record and play a raw sound file in CD quality.

To test PCM recordings (on your default soundcard), run:

$ python recordtest.py <filename>

Speak into the microphone, and interrupt the recording at any time with Ctl-C.

Play back the recording with:

$ python playbacktest.py <filename>

mixertest.py

Without arguments, mixertest.py will list all available controls on the default soundcard.

The output might look like this:

$ ./mixertest.py
Available mixer controls:
   'Master'
   'Master Mono'
   'Headphone'
   'PCM'
   'Line'
   'Line In->Rear Out'
   'CD'
   'Mic'
   'PC Speaker'
   'Aux'
   'Mono Output Select'
   'Capture'
   'Mix'
   'Mix Mono'

With a single argument - the control, it will display the settings of that control; for example:

$ ./mixertest.py Master
Mixer name: 'Master'
Capabilities: Playback Volume Playback Mute
Channel 0 volume: 61%
Channel 1 volume: 61%

With two arguments, the control and a parameter, it will set the parameter on the mixer:

$ ./mixertest.py Master mute

This will mute the Master mixer.

Or:

$ ./mixertest.py Master 40

This sets the volume to 40% on all channels.

To select a different soundcard, use either the device or cardindex argument:

$ ./mixertest.py -c 0 Master
Mixer name: 'Master'
Capabilities: Playback Volume Playback Mute
Channel 0 volume: 61%
Channel 1 volume: 61%