alsaaudio documentation¶
+alsaaudio documentation¶
-
@@ -58,20 +58,20 @@
- @@ -86,27 +86,16 @@
- -alsaaudio.pcms(pcmtype=PCM_PLAYBACK)¶ +alsaaudio.pcms(pcmtype: int = PCM_PLAYBACK) list[str] ¶
List available PCM devices by name.
Arguments are:
-
@@ -61,7 +60,7 @@
- -alsaaudio.cards()¶ +alsaaudio.cards() list[str] ¶
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
@@ -69,7 +68,7 @@pcms()
instead.- -alsaaudio.mixers(cardindex=-1, device='default')¶ +alsaaudio.mixers(cardindex: int = -1, device: str = 'default') list[str] ¶
List the available mixers. The arguments are:
cardindex - the card index. If this argument is given, the device name @@ -107,20 +106,24 @@
- -alsaaudio.asoundlib_version()¶ +alsaaudio.asoundlib_version() str ¶
Return a Python string containing the ALSA version found.
- PCM Objects¶
+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:
+- +PCM(type: int = PCM_PLAYBACK, mode: int = PCM_NORMAL, rate: int = 44100, channels: int = 2, +
- +format: int = PCM_FORMAT_S16_LE, periodsize: int = 32, periods: int = 4, +
- +device: str = 'default', cardindex: int = -1) -> PCM +
This class is used to represent a PCM device (either for playback or +recording). The constructor’s arguments are:
type - can be either
PCM_CAPTURE
orPCM_PLAYBACK
(default).
@@ -237,7 +240,14 @@
Note: This should not be used, as it bypasses most of ALSA’s configuration.
This will construct a PCM object with the given settings.
+The defaults mentioned above are values passed by :mod:alsaaudio +to ALSA, not anything internal to ALSA.
+Note: For default and non-default values alike, there is no +guarantee that a PCM device supports the requested configuration, +and ALSA may pick realizable values which it believes to be closest +to the request. Therefore, after creating a PCM object, it is +necessary to verify whether its realized configuration is acceptable. +The :func:info method can be used to query it.
Changed in 0.10:
Added the optional named parameter periods.
@@ -259,8 +269,19 @@
- -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.
+PCM.info() dict ¶ +Returns a dictionary containing the configuration of a PCM device.
+A small subset of properties reflects fixed parameters given by the +user, stored within alsaaudio. To distinguish them from properties +retrieved from ALSA when the call is made, they have their name +prefixed with “ (call value) “.
+Descriptions of properties which can be directly set during PCM object +instantiation carry the prefix “PCM():”, followed by the respective +constructor parameter. Note that due to device limitations, the values +may deviate from those originally requested.
+Yet another set of properties cannot be set, and derives directly from +the hardware, possibly depending on other properties. Those properties’ +descriptions are prefixed with “hw:” below.
@@ -362,7 +383,15 @@Key
tuple (integer (Hz), integer (Hz))
+ -significant_bits
+significant bits in sample
+significant bits in sample [1]
+integer (negative indicates error)
+ +nominal_bits
+nominal bits in sample [1]
+integer (negative indicates error)
+physical_bits
sample width in bits [1]
integer (negative indicates error)
@@ -407,80 +436,82 @@is_batch
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.dumpinfo()¶ +
Dumps the PCM object’s configured parameters to stdout.
- -PCM.pcmtype()¶ +PCM.pcmtype() int ¶
Returns the type of PCM object. Either
PCM_CAPTURE
orPCM_PLAYBACK
.- -PCM.pcmmode()¶ +PCM.pcmmode() int ¶
Return the mode of the PCM object. One of
PCM_NONBLOCK
,PCM_ASYNC
, orPCM_NORMAL
- -PCM.cardname()¶ +PCM.cardname() string ¶
Return the name of the sound card used by this PCM object.
- -PCM.setchannels(nchannels)¶ +PCM.setchannels(nchannels: int) int ¶
- -
Deprecated since version 0.9: Use the channels named argument to
+PCM()
.Deprecated since version 0.9: Use the channels named argument to
PCM()
. - -PCM.setrate(rate)¶ +PCM.setrate(rate: int) int ¶
- -
Deprecated since version 0.9: Use the rate named argument to
+PCM()
.Deprecated since version 0.9: Use the rate named argument to
PCM()
. - -PCM.setformat(format)¶ +PCM.setformat(format: int) int ¶
- -
Deprecated since version 0.9: Use the format named argument to
+PCM()
.Deprecated since version 0.9: Use the format named argument to
PCM()
. - -PCM.setperiodsize(period)¶ +PCM.setperiodsize(period: int) int ¶
- -
Deprecated since version 0.9: Use the periodsize named argument to
+PCM()
.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()¶ +PCM.state() int ¶
Returs the current state of the stream, which can be one of
PCM_STATE_OPEN
(this should not actually happen),PCM_STATE_SETUP
(afterdrop()
ordrain()
), @@ -494,9 +525,22 @@New in 0.10
- +PCM.avail() int ¶ +
For
+PCM_PLAYBACK
PCM objects, returns the number of writable +(that is, free) frames in the buffer.For
+PCM_CAPTURE
PCM objects, returns the number of readable +(that is, filled) frames in the buffer.An attempt to read/write more frames than indicated will block (in +
+PCM_NORMAL
mode) or fail and return zero (in +PCM_NONBLOCK
mode).New in 0.11
+- -PCM.read()¶ +PCM.read() tuple[int, bytes] ¶
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 @@ -505,25 +549,35 @@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.In case of a buffer overrun, this function will return the negative +size
-EPIPE
, and no data is read. +This indicates that data was lost. To resume capturing, just call read +again, but note that the stream was already corrupted. +To avoid the problem in the future, try using a larger period size +and/or more periods, at the cost of higher latency.- -PCM.write(data)¶ +PCM.write(data: bytes) int ¶
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.If the data was successfully written, the call returns the size of the +data in frames.
+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.In
+should be written again at a later time. +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.In case of a buffer underrun, this function will return the negative +size
-EPIPE
, and no data is written. +At this point, the playback was already corrupted. If you want to play +the data nonetheless, call write again with the same data. +To avoid the problem in the future, try using a larger period size +and/or more periods, at the cost of higher latency.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.
@@ -531,21 +585,21 @@- -PCM.pause([enable=True])¶ +PCM.pause([enable: int = True]) int ¶
If enable is
True
, playback or capture is paused. Otherwise, playback/capture is resumed.
- -PCM.drain()¶ +PCM.drain() int ¶
For
@@ -555,7 +609,7 @@PCM_PLAYBACK
PCM objects, play residual buffered frames and then stop the stream. InPCM_NORMAL
mode, this function blocks until all pending playback is drained.- -PCM.close()¶ +PCM.close() None ¶
Closes the PCM device.
For
@@ -563,30 +617,40 @@PCM_PLAYBACK
PCM objects inPCM_NORMAL
mode, this function blocks until all pending playback is drained.- -PCM.polldescriptors()¶ +PCM.polldescriptors() list[tuple[int, int]] ¶
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 +
The eventmask value is compatible with `poll.register`__ in the Python
select
module.
-
+
- +PCM.polldescriptors_revents(descriptors: list[tuple[int, int]]) int ¶ +
Processes the descriptor list returned by
+polldescriptors()
after +using it with select.poll, and returns a single eventmask value that +is meaningful for deciding whetherread()
orwrite()
should +be called.New in 0.11
+
- -PCM.set_tstamp_mode([mode=PCM_TSTAMP_ENABLE])¶ +PCM.set_tstamp_mode([mode: int = PCM_TSTAMP_ENABLE])¶
Set the ALSA timestamp mode on the device. The mode argument can be set to either
PCM_TSTAMP_NONE
orPCM_TSTAMP_ENABLE
.
- -PCM.get_tstamp_mode()¶ +PCM.get_tstamp_mode() int ¶
Return the integer value corresponding to the ALSA timestamp mode. The return value can be either
PCM_TSTAMP_NONE
orPCM_TSTAMP_ENABLE
.
- -PCM.set_tstamp_type([type=PCM_TSTAMP_TYPE_GETTIMEOFDAY])¶ +PCM.set_tstamp_type([type: int = PCM_TSTAMP_TYPE_GETTIMEOFDAY]) None ¶
Set the ALSA timestamp mode on the device. The type argument can be set to either
@@ -594,7 +658,7 @@PCM_TSTAMP_TYPE_GETTIMEOFDAY
,PCM_TSTAMP_TYPE_MONOTONIC
orPCM_TSTAMP_TYPE_MONOTONIC_RAW
.- -PCM.get_tstamp_type()¶ +PCM.get_tstamp_type() int ¶
Return the integer value corresponding to the ALSA timestamp type. The return value can be either
@@ -602,7 +666,7 @@PCM_TSTAMP_TYPE_GETTIMEOFDAY
,PCM_TSTAMP_TYPE_MONOTONIC
orPCM_TSTAMP_TYPE_MONOTONIC_RAW
.- -PCM.htimestamp()¶ +PCM.htimestamp() tuple[int, int, int] ¶
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.
@@ -649,7 +713,7 @@
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 +ugly clicking sounds will occur. Conversely, if 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 @@ -668,11 +732,11 @@ to check how much time has really passed, and add extra writes as nessecary.
- Mixer Objects¶
+Mixer Objects¶
Mixer objects provides access to the ALSA mixer API.
- -class alsaaudio.Mixer(control='Master', id=0, cardindex=-1, device='default')¶ +class alsaaudio.Mixer(control: str = 'Master', id: int = 0, cardindex: int = -1, device: str = 'default') Mixer ¶
Arguments are:
control - specifies which control to manipulate using this mixer @@ -698,26 +762,26 @@
Mixer objects have the following methods:
- -Mixer.cardname()¶ +Mixer.cardname() str ¶
Return the name of the sound card used by this Mixer object
- -Mixer.mixer()¶ +Mixer.mixer() str ¶
Return the name of the specific mixer controlled by this object, For example
'Master'
or'PCM'
- -Mixer.switchcap()¶ +Mixer.switchcap() int ¶
Returns a list of the switches which are defined by this specific mixer. Possible values in this list are:
@@ -756,7 +820,7 @@
- -Mixer.volumecap()¶ +Mixer.volumecap() int ¶
Returns a list of the volume control capabilities of this mixer. Possible values in the list are:
@@ -790,7 +854,7 @@
- -Mixer.getenum()¶ +Mixer.getenum() tuple[str, list[str]] ¶
For enumerated controls, return the currently selected item and the list of items available.
Returns a tuple (string, list of strings).
@@ -816,7 +880,7 @@- -Mixer.setenum(index)¶ +Mixer.setenum(index: int) None ¶
For enumerated controls, sets the currently selected item. index is an index into the list of available enumerated items returned by
@@ -824,7 +888,7 @@getenum()
.- -Mixer.getrange(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_RAW)¶ +Mixer.getrange(pcmtype: int = PCM_PLAYBACK, units: int = VOLUME_UNITS_RAW) tuple[int, int] ¶
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.
@@ -838,7 +902,7 @@- -Mixer.getvolume(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_PERCENTAGE)¶ +Mixer.getvolume(pcmtype: int = PCM_PLAYBACK, units: int = VOLUME_UNITS_PERCENTAGE) int ¶
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 @@ -851,7 +915,7 @@- -Mixer.setvolume(volume, channel=None, pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_PERCENTAGE)¶ +Mixer.setvolume(volume: int, pcmtype: int = PCM_PLAYBACK, units: int = VOLUME_UNITS_PERCENTAGE, channel: int | None = None) None ¶
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 @@ -867,7 +931,7 @@
- -Mixer.getmute()¶ +Mixer.getmute() list[int] ¶
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.
@@ -875,7 +939,7 @@- -Mixer.setmute(mute[, channel])¶ +Mixer.setmute(mute: bool, channel: int | None = None) None ¶
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 @@ -885,7 +949,7 @@
- -Mixer.getrec()¶ +Mixer.getrec() list[int] ¶
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.
@@ -893,7 +957,7 @@- -Mixer.setrec(capture[, channel])¶ +Mixer.setrec(capture: int, channel: int | None = None) None ¶
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 @@ -903,16 +967,16 @@
- -Mixer.polldescriptors()¶ +Mixer.polldescriptors() list[tuple[int, int]] ¶
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 +
The eventmask value is compatible with `poll.register`__ in the Python
select
module.
- -Mixer.handleevents()¶ +Mixer.handleevents() int ¶
Acknowledge events on the
@@ -920,7 +984,7 @@ @@ -943,7 +1007,7 @@ painful trial and error process.polldescriptors()
file descriptors to prevent subsequent polls from returning the same events again. Returns the number of events that were acknowledged.- Examples¶
+Examples¶
The following example are provided:
playwav.py
@@ -967,7 +1031,7 @@
mixertest.py accepts the commandline options -d <device> and -c <cardindex>.
- playwav.py¶
+playwav.py¶
playwav.py plays a wav file.
To test PCM playback (on your default soundcard), run:
$ python playwav.py <wav file> @@ -975,7 +1039,7 @@
playwav.py -
recordtest.py and playbacktest.py¶
+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:
@@ -990,7 +1054,7 @@recordtest.py and playbacktest.py -
mixertest.py¶
+mixertest.py¶
Without arguments, mixertest.py will list all available controls on the default soundcard.
The output might look like this:
@@ -1052,36 +1116,75 @@mixertest.py
Quick search
©2017, Lars Immisch & Casper Wilstrup. | - Powered by @@ -1115,8 +1217,8 @@ Sphinx 6.1.3 - & Alabaster 0.7.13 + Powered by Sphinx 7.3.7 + & Alabaster 0.7.12 | - + -Python Module Index — alsaaudio documentation 0.10.0 documentation - - - - - +Python Module Index — alsaaudio documentation 0.11.0 documentation + + + + + @@ -60,35 +59,14 @@Python Module Index
PCM objects have the following methods:
+
+The italicized descriptions give a summary of the “full” description +as can be found in the +ALSA documentation.
+New in 0.9.1
+-
+
-
-
-
-
-
+
alsaaudio documentation
-Download¶
+Download¶
-Github¶
+Github¶
-Indices and tables¶
+Indices and tables¶
Indices and tables
Quick search
©2017, Lars Immisch & Casper Wilstrup.
|
- Powered by
@@ -140,8 +128,8 @@ Sphinx 6.1.3
- & Alabaster 0.7.13
+ Powered by Sphinx 7.3.7
+ & Alabaster 0.7.12
|
-
+
-
-
- alsaaudio — alsaaudio documentation 0.10.0 documentation
-
-
-
-
-
+
+
+ alsaaudio — alsaaudio documentation 0.11.0 documentation
+
+
+
+
+
@@ -33,11 +32,11 @@
-alsaaudio
¶
+alsaaudio
¶
The alsaaudio
module defines functions and classes for using ALSA.
Download¶
+Download¶
Github¶
+Github¶
Indices and tables¶
+Indices and tables¶
Indices and tables
Quick search
©2017, Lars Immisch & Casper Wilstrup.
|
- Powered by
@@ -140,8 +128,8 @@ Sphinx 6.1.3
- & Alabaster 0.7.13
+ Powered by Sphinx 7.3.7
+ & Alabaster 0.7.12
|
-
+
-
-
- alsaaudio — alsaaudio documentation 0.10.0 documentation
-
-
-
-
-
+
+
+ alsaaudio — alsaaudio documentation 0.11.0 documentation
+
+
+
+
+
@@ -33,11 +32,11 @@
-alsaaudio
¶
+alsaaudio
¶
The alsaaudio
module defines functions and classes for using ALSA.
alsaaudio
¶
+alsaaudio
¶
The alsaaudio
module defines functions and classes for using ALSA.