Compare commits

...

3 Commits

Author SHA1 Message Date
Lars Immisch
9693a932a3 Add echo (seed for loopback.py) 2024-02-16 14:14:37 +01:00
Lars Immisch
ee1c3a546b Documentation for type hints 2024-02-05 23:34:41 +00:00
Lars Immisch
e4ec455ffa Add type hints 2024-02-05 23:12:44 +00:00
4 changed files with 238 additions and 77 deletions

View File

@@ -1,4 +1,5 @@
include *.py include *.py
include alsaaudio.pyi
include CHANGES include CHANGES
include TODO include TODO
include LICENSE include LICENSE

128
alsaaudio.pyi Normal file
View File

@@ -0,0 +1,128 @@
from typing import list
PCM_PLAYBACK: int
PCM_CAPTURE: int
PCM_NORMAL: int
PCM_NONBLOCK: int
PCM_ASYNC: int
PCM_FORMAT_S8: int
PCM_FORMAT_U8: int
PCM_FORMAT_S16_LE: int
PCM_FORMAT_S16_BE: int
PCM_FORMAT_U16_LE: int
PCM_FORMAT_U16_BE: int
PCM_FORMAT_S24_LE: int
PCM_FORMAT_S24_BE: int
PCM_FORMAT_U24_LE: int
PCM_FORMAT_U24_BE: int
PCM_FORMAT_S32_LE: int
PCM_FORMAT_S32_BE: int
PCM_FORMAT_U32_LE: int
PCM_FORMAT_U32_BE: int
PCM_FORMAT_FLOAT_LE: int
PCM_FORMAT_FLOAT_BE: int
PCM_FORMAT_FLOAT64_LE: int
PCM_FORMAT_FLOAT64_BE: int
PCM_FORMAT_MU_LAW: int
PCM_FORMAT_A_LAW: int
PCM_FORMAT_IMA_ADPCM: int
PCM_FORMAT_MPEG: int
PCM_FORMAT_GSM: int
PCM_FORMAT_S24_3LE: int
PCM_FORMAT_S24_3BE: int
PCM_FORMAT_U24_3LE: int
PCM_FORMAT_U24_3BE: int
PCM_TSTAMP_NONE: int
PCM_TSTAMP_ENABLE: int
PCM_TSTAMP_TYPE_GETTIMEOFDAY: int
PCM_TSTAMP_TYPE_MONOTONIC: int
PCM_TSTAMP_TYPE_MONOTONIC_RAW: int
PCM_FORMAT_DSD_U8: int
PCM_FORMAT_DSD_U16_LE: int
PCM_FORMAT_DSD_U32_LE: int
PCM_FORMAT_DSD_U32_BE: int
PCM_STATE_OPEN: int
PCM_STATE_SETUP: int
PCM_STATE_PREPARED: int
PCM_STATE_RUNNING: int
PCM_STATE_XRUN: int
PCM_STATE_DRAINING: int
PCM_STATE_PAUSED: int
PCM_STATE_SUSPENDED: int
PCM_STATE_DISCONNECTED: int
MIXER_CHANNEL_ALL: int
MIXER_SCHN_UNKNOWN: int
MIXER_SCHN_FRONT_LEFT: int
MIXER_SCHN_FRONT_RIGHT: int
MIXER_SCHN_REAR_LEFT: int
MIXER_SCHN_REAR_RIGHT: int
MIXER_SCHN_FRONT_CENTER: int
MIXER_SCHN_WOOFER: int
MIXER_SCHN_SIDE_LEFT: int
MIXER_SCHN_SIDE_RIGHT: int
MIXER_SCHN_REAR_CENTER: int
MIXER_SCHN_MONO: int
VOLUME_UNITS_PERCENTAGE: int
VOLUME_UNITS_RAW: int
VOLUME_UNITS_DB: int
def pcms(pcmtype:int) -> list[str]: ...
def cards() -> list[str]: ...
def mixers(cardindex:int=-1, device:str='default') -> list[str]: ...
def asoundlib_version() -> str: ...
class PCM:
def __init__(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: ...
def info() -> dict: ...
def pcmtype() -> int: ...
def pcmmode() -> int: ...
def cardname() -> str: ...
def setchannels(nchannels: int) -> None: ...
def setrate(rate: int) -> None: ...
def setformat(format: int) -> int: ...
def setperiodsize(period: int) -> int: ...
def dumpinfo() -> None: ...
def state() -> int: ...
def read() -> tuple[int, bytes]: ...
def write(data:bytes) -> int: ...
def pause(enable:bool=True) -> int: ...
def drop() -> int: ...
def drain() -> int: ...
def polldescriptors() -> list[tuple[int, int]]: ...
def set_tstamp_mode(mode:int=PCM_TSTAMP_ENABLE) -> None: ...
def get_tstamp_mode() -> int: ...
def set_tstamp_type(type:int=PCM_TSTAMP_TYPE_GETTIMEOFDAY) -> None: ...
def get_tstamp_type() -> int: ...
def htimestamp() -> tuple[int, int, int]: ...
class Mixer:
def __init__(control:str='Master', id:int=0, cardindex:int=-1, device:str='default') -> Mixer: ...
def cardname() -> str: ...
def mixer() -> str: ...
def mixerid() -> int: ...
def switchcap() -> int: ...
def volumecap() -> int: ...
def getenum() -> tuple[ str, list[str]]: ...
def setenum(index:int) -> None: ...
def getrange(pcmtype:int=PCM_PLAYBACK, units:int=VOLUME_UNITS_RAW) -> tuple[int, int]: ...
def getvolume(pcmtype:int=PCM_PLAYBACK, units:int=VOLUME_UNITS_PERCENTAGE) -> int: ...
def setvolume(volume:int, pcmtype:int=PCM_PLAYBACK, units:int=VOLUME_UNITS_PERCENTAGE, channel:int|None=None) -> None: ...
def getmute() -> list[int]: ...
def setmute(mute:bool, channel:int|None=None) -> None: ...
def getrec() -> list[int]: ...
def setrec(capture:int, channel:int|None=None) -> None: ...
def polldescriptors() -> list[tuple[int, int]]: ...
def close() -> None: ...

View File

@@ -10,14 +10,14 @@
The :mod:`alsaaudio` module defines functions and classes for using ALSA. The :mod:`alsaaudio` module defines functions and classes for using ALSA.
.. function:: pcms(pcmtype=PCM_PLAYBACK) .. function:: pcms(pcmtype:int=PCM_PLAYBACK) ->list[str]
List available PCM devices by name. List available PCM devices by name.
Arguments are: Arguments are:
* *pcmtype* - can be either :const:`PCM_CAPTURE` or :const:`PCM_PLAYBACK` * *pcmtype* - can be either :const:`PCM_CAPTURE` or :const:`PCM_PLAYBACK`
(default). (default).
**Note:** **Note:**
@@ -34,7 +34,7 @@ The :mod:`alsaaudio` module defines functions and classes for using ALSA.
*New in 0.8* *New in 0.8*
.. function:: cards() .. function:: cards() -> list[str]
List the available ALSA cards by name. This function is only moderately 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 :func:`pcms` useful. If you want to see a list of available PCM devices, use :func:`pcms`
@@ -96,18 +96,18 @@ PCM objects in :mod:`alsaaudio` can play or capture (record) PCM
sound through speakers or a microphone. The PCM constructor takes the sound through speakers or a microphone. The PCM constructor takes the
following arguments: following arguments:
.. class:: PCM(type=PCM_PLAYBACK, mode=PCM_NORMAL, rate=44100, channels=2, format=PCM_FORMAT_S16_LE, periodsize=32, periods=4, device='default', cardindex=-1) .. class:: PCM(type=PCM_PLAYBACK, mode=PCM_NORMAL, rate=44100, channels=2, format=PCM_FORMAT_S16_LE, periodsize=32, periods=4, device='default', cardindex=-1) -> PCM
This class is used to represent a PCM device (either for playback and This class is used to represent a PCM device (either for playback and
recording). The arguments are: recording). The arguments are:
* *type* - can be either :const:`PCM_CAPTURE` or :const:`PCM_PLAYBACK` * *type* - can be either :const:`PCM_CAPTURE` or :const:`PCM_PLAYBACK`
(default). (default).
* *mode* - can be either :const:`PCM_NONBLOCK`, or :const:`PCM_NORMAL` * *mode* - can be either :const:`PCM_NONBLOCK`, or :const:`PCM_NORMAL`
(default). (default).
* *rate* - the sampling rate in Hz. Typical values are ``8000`` (mainly used for telephony), ``16000``, ``44100`` (default), ``48000`` and ``96000``. * *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). * *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. * *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 :const:`PCM_FORMAT_S16_LE`. The default value is :const:`PCM_FORMAT_S16_LE`.
========================= =============== ========================= ===============
@@ -179,24 +179,24 @@ following arguments:
PCM objects have the following methods: PCM objects have the following methods:
.. method:: PCM.info() .. method:: PCM.info() -> dict
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. 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 Key Description (Reference) Type
=========================== ============================= ================================================================== =========================== ============================= ==================================================================
name PCM():device string name PCM():device string
card_no *index of card* integer (negative indicates device not associable with a card) card_no *index of card* integer (negative indicates device not associable with a card)
device_no *index of PCM device* integer device_no *index of PCM device* integer
subdevice_no *index of PCM subdevice* integer subdevice_no *index of PCM subdevice* integer
state *name of PCM state* string state *name of PCM state* string
access_type *name of PCM access type* string access_type *name of PCM access type* string
(call value) type PCM():type integer (call value) type PCM():type integer
(call value) type_name PCM():type string (call value) type_name PCM():type string
(call value) mode PCM():mode integer (call value) mode PCM():mode integer
(call value) mode_name PCM():mode string (call value) mode_name PCM():mode string
format PCM():format integer format PCM():format integer
format_name PCM():format string format_name PCM():format string
format_description PCM():format string format_description PCM():format string
subformat_name *name of PCM subformat* string subformat_name *name of PCM subformat* string
@@ -219,13 +219,13 @@ PCM objects have the following methods:
can_mmap_sample_resolution *hw: sample-resol. mmap* 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_pause *hw: pause* boolean (True: hardware supported)
can_resume *hw: resume* boolean (True: hardware supported) can_resume *hw: resume* boolean (True: hardware supported)
can_sync_start *hw: synchronized start* 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 <https://www.alsa-project.org/alsa-doc>`_. "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():". The italicized descriptions give a summary of the "full" description as it can be found in the `ALSA documentation <https://www.alsa-project.org/alsa-doc>`_. "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():".
.. method:: PCM.pcmtype() .. method:: PCM.pcmtype() -> int
Returns the type of PCM object. Either :const:`PCM_CAPTURE` or Returns the type of PCM object. Either :const:`PCM_CAPTURE` or
:const:`PCM_PLAYBACK`. :const:`PCM_PLAYBACK`.
@@ -267,37 +267,29 @@ PCM objects have the following methods:
Returns a dictionary of supported format codes (integers) keyed by Returns a dictionary of supported format codes (integers) keyed by
their standard ALSA names (strings). their standard ALSA names (strings).
.. method:: PCM.setchannels(nchannels) .. method:: PCM.setchannels(nchannels: int) -> int
.. deprecated:: 0.9 Use the `channels` named argument to :func:`PCM`. .. deprecated:: 0.9 Use the `channels` named argument to :func:`PCM`.
.. method:: PCM.setrate(rate) .. method:: PCM.setrate(rate: int) -> int
.. deprecated:: 0.9 Use the `rate` named argument to :func:`PCM`. .. deprecated:: 0.9 Use the `rate` named argument to :func:`PCM`.
.. method:: PCM.setformat(format) .. method:: PCM.setformat(format: int) -> int
.. deprecated:: 0.9 Use the `format` named argument to :func:`PCM`. .. deprecated:: 0.9 Use the `format` named argument to :func:`PCM`.
.. method:: PCM.setperiodsize(period) .. method:: PCM.setperiodsize(period: int) -> int
.. deprecated:: 0.9 Use the `periodsize` named argument to :func:`PCM`. .. deprecated:: 0.9 Use the `periodsize` named argument to :func:`PCM`.
.. method:: 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* *New in 0.9.1*
.. method:: PCM.dumpinfo() .. method:: PCM.dumpinfo() -> None
Dumps the PCM object's configured parameters to stdout. Dumps the PCM object's configured parameters to stdout.
.. method:: PCM.state() .. method:: PCM.state() -> int
Returs the current state of the stream, which can be one of Returs the current state of the stream, which can be one of
:const:`PCM_STATE_OPEN` (this should not actually happen), :const:`PCM_STATE_OPEN` (this should not actually happen),
@@ -312,12 +304,12 @@ PCM objects have the following methods:
*New in 0.10* *New in 0.10*
.. method:: PCM.read() .. method:: PCM.read() -> tuple[int, bytes]
In :const:`PCM_NORMAL` mode, this function blocks until a full period is In :const:`PCM_NORMAL` mode, this function blocks until a full period is
available, and then returns a tuple (length,data) where *length* is available, and then returns a tuple (length,data) where *length* is
the number of frames of captured data, and *data* is the captured 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 sound frames as a string. The length of the returned data will be
periodsize\*framesize bytes. periodsize\*framesize bytes.
In :const:`PCM_NONBLOCK` mode, the call will not block, but will return In :const:`PCM_NONBLOCK` mode, the call will not block, but will return
@@ -331,7 +323,7 @@ PCM objects have the following methods:
To avoid the problem in the future, try using a larger period size To avoid the problem in the future, try using a larger period size
and/or more periods, at the cost of higher latency. and/or more periods, at the cost of higher latency.
.. method:: PCM.write(data) .. method:: PCM.write(data: bytes) -> int
Writes (plays) the sound in data. The length of data *must* be a 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 multiple of the frame size, and *should* be exactly the size of a
@@ -360,18 +352,18 @@ PCM objects have the following methods:
in the kernel, and playout will continue afterwards. Make sure that the in the kernel, and playout will continue afterwards. Make sure that the
stream is drained before discarding the PCM handle. stream is drained before discarding the PCM handle.
.. method:: PCM.pause([enable=True]) .. method:: PCM.pause([enable=True]) -> int
If *enable* is :const:`True`, playback or capture is paused. If *enable* is :const:`True`, playback or capture is paused.
Otherwise, playback/capture is resumed. Otherwise, playback/capture is resumed.
.. method:: PCM.drop() .. method:: PCM.drop() -> int
Stop the stream and drop residual buffered frames. Stop the stream and drop residual buffered frames.
*New in 0.9* *New in 0.9*
.. method:: PCM.drain() .. method:: PCM.drain() -> int
For :const:`PCM_PLAYBACK` PCM objects, play residual buffered frames For :const:`PCM_PLAYBACK` PCM objects, play residual buffered frames
and then stop the stream. In :const:`PCM_NORMAL` mode, and then stop the stream. In :const:`PCM_NORMAL` mode,
@@ -381,44 +373,37 @@ PCM objects have the following methods:
*New in 0.10* *New in 0.10*
.. method:: PCM.close() .. method:: PCM.polldescriptors() -> list[tuple[int, int]]
Closes the PCM device.
For :const:`PCM_PLAYBACK` PCM objects in :const:`PCM_NORMAL` mode,
this function blocks until all pending playback is drained.
.. method:: PCM.polldescriptors()
Returns a list of tuples of *(file descriptor, eventmask)* that can be Returns a list of tuples of *(file descriptor, eventmask)* that can be
used to wait for changes on the PCM with *select.poll*. 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
:const:`select` module. :const:`select` module.
.. method:: PCM.set_tstamp_mode([mode=PCM_TSTAMP_ENABLE]) .. method:: PCM.set_tstamp_mode([mode=PCM_TSTAMP_ENABLE]) -> None
Set the ALSA timestamp mode on the device. The mode argument can be set to Set the ALSA timestamp mode on the device. The mode argument can be set to
either :const:`PCM_TSTAMP_NONE` or :const:`PCM_TSTAMP_ENABLE`. either :const:`PCM_TSTAMP_NONE` or :const:`PCM_TSTAMP_ENABLE`.
.. method:: PCM.get_tstamp_mode() .. method:: PCM.get_tstamp_mode() -> int
Return the integer value corresponding to the ALSA timestamp mode. The Return the integer value corresponding to the ALSA timestamp mode. The
return value can be either :const:`PCM_TSTAMP_NONE` or :const:`PCM_TSTAMP_ENABLE`. return value can be either :const:`PCM_TSTAMP_NONE` or :const:`PCM_TSTAMP_ENABLE`.
.. method:: PCM.set_tstamp_type([type=PCM_TSTAMP_TYPE_GETTIMEOFDAY]) .. method:: PCM.set_tstamp_type([type=PCM_TSTAMP_TYPE_GETTIMEOFDAY]) -> None
Set the ALSA timestamp mode on the device. The type argument Set the ALSA timestamp mode on the device. The type argument
can be set to either :const:`PCM_TSTAMP_TYPE_GETTIMEOFDAY`, can be set to either :const:`PCM_TSTAMP_TYPE_GETTIMEOFDAY`,
:const:`PCM_TSTAMP_TYPE_MONOTONIC` or :const:`PCM_TSTAMP_TYPE_MONOTONIC_RAW`. :const:`PCM_TSTAMP_TYPE_MONOTONIC` or :const:`PCM_TSTAMP_TYPE_MONOTONIC_RAW`.
.. method:: PCM.get_tstamp_type() .. method:: PCM.get_tstamp_type() -> int
Return the integer value corresponding to the ALSA timestamp type. The Return the integer value corresponding to the ALSA timestamp type. The
return value can be either :const:`PCM_TSTAMP_TYPE_GETTIMEOFDAY`, return value can be either :const:`PCM_TSTAMP_TYPE_GETTIMEOFDAY`,
:const:`PCM_TSTAMP_TYPE_MONOTONIC` or :const:`PCM_TSTAMP_TYPE_MONOTONIC_RAW`. :const:`PCM_TSTAMP_TYPE_MONOTONIC` or :const:`PCM_TSTAMP_TYPE_MONOTONIC_RAW`.
.. method:: PCM.htimestamp() .. method:: PCM.htimestamp() -> tuple[int, int, int]
Return a Python tuple *(seconds, nanoseconds, frames_available_in_buffer)*. Return a Python tuple *(seconds, nanoseconds, frames_available_in_buffer)*.
@@ -445,9 +430,16 @@ PCM objects have the following methods:
update. update.
================================= =========================================== ================================= ===========================================
.. method:: PCM.close() -> None
Closes the PCM device.
For :const:`PCM_PLAYBACK` PCM objects in :const:`PCM_NORMAL` mode,
this function blocks until all pending playback is drained.
**A few hints on using PCM devices for playback** **A few hints on using PCM devices for playback**
The most common reason for problems with playback of PCM audio is that writes 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. 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 If too little data is written to the device, it will underrun, and
@@ -480,12 +472,12 @@ Mixer Objects
Mixer objects provides access to the ALSA mixer API. Mixer objects provides access to the ALSA mixer API.
.. class:: Mixer(control='Master', id=0, cardindex=-1, device='default') .. class:: Mixer(control='Master', id=0, cardindex=-1, device='default') -> Mixer
Arguments are: Arguments are:
* *control* - specifies which control to manipulate using this mixer * *control* - specifies which control to manipulate using this mixer
object. The list of available controls can be found with the object. The list of available controls can be found with the
:mod:`alsaaudio`.\ :func:`mixers` function. The default value is :mod:`alsaaudio`.\ :func:`mixers` function. The default value is
``'Master'`` - other common controls may be ``'Master Mono'``, ``'PCM'``, ``'Master'`` - other common controls may be ``'Master Mono'``, ``'PCM'``,
``'Line'``, etc. ``'Line'``, etc.
@@ -495,7 +487,7 @@ Mixer objects provides access to the ALSA mixer API.
* *cardindex* - specifies which card should be used. If this argument * *cardindex* - specifies which card should be used. If this argument
is given, the device name is constructed like this: 'hw:*cardindex*' and is given, the device name is constructed like this: 'hw:*cardindex*' and
the `device` keyword argument is ignored. ``0`` is the the `device` keyword argument is ignored. ``0`` is the
first sound card. first sound card.
* *device* - the name of the device on which the mixer resides. The default * *device* - the name of the device on which the mixer resides. The default
value is ``'default'``. value is ``'default'``.
@@ -507,20 +499,20 @@ Mixer objects provides access to the ALSA mixer API.
Mixer objects have the following methods: Mixer objects have the following methods:
.. method:: Mixer.cardname() .. method:: Mixer.cardname() -> str
Return the name of the sound card used by this Mixer object Return the name of the sound card used by this Mixer object
.. method:: Mixer.mixer() .. method:: Mixer.mixer() -> str
Return the name of the specific mixer controlled by this object, For example Return the name of the specific mixer controlled by this object, For example
``'Master'`` or ``'PCM'`` ``'Master'`` or ``'PCM'``
.. method:: Mixer.mixerid() .. method:: Mixer.mixerid() -> int
Return the ID of the ALSA mixer controlled by this object. Return the ID of the ALSA mixer controlled by this object.
.. method:: Mixer.switchcap() .. method:: Mixer.switchcap() -> int
Returns a list of the switches which are defined by this specific mixer. Returns a list of the switches which are defined by this specific mixer.
Possible values in this list are: Possible values in this list are:
@@ -532,7 +524,7 @@ Mixer objects have the following methods:
'Joined Mute' This mixer can mute all channels at the same time 'Joined Mute' This mixer can mute all channels at the same time
'Playback Mute' This mixer can mute the playback output 'Playback Mute' This mixer can mute the playback output
'Joined Playback Mute' Mute playback for all channels at the same time} 'Joined Playback Mute' Mute playback for all channels at the same time}
'Capture Mute' Mute sound capture 'Capture Mute' Mute sound capture
'Joined Capture Mute' Mute sound capture for all channels at a time} 'Joined Capture Mute' Mute sound capture for all channels at a time}
'Capture Exclusive' Not quite sure what this is 'Capture Exclusive' Not quite sure what this is
====================== ================ ====================== ================
@@ -540,7 +532,7 @@ Mixer objects have the following methods:
To manipulate these switches use the :meth:`setrec` or To manipulate these switches use the :meth:`setrec` or
:meth:`setmute` methods :meth:`setmute` methods
.. method:: Mixer.volumecap() .. method:: Mixer.volumecap() -> int
Returns a list of the volume control capabilities of this Returns a list of the volume control capabilities of this
mixer. Possible values in the list are: mixer. Possible values in the list are:
@@ -556,7 +548,7 @@ Mixer objects have the following methods:
'Joined Capture Volume' Manipulate sound capture volume for all channels at a time 'Joined Capture Volume' Manipulate sound capture volume for all channels at a time
======================== ================ ======================== ================
.. method:: Mixer.getenum() .. method:: Mixer.getenum() -> tuple[ str, list[str]]
For enumerated controls, return the currently selected item and the list of For enumerated controls, return the currently selected item and the list of
items available. items available.
@@ -582,13 +574,13 @@ Mixer objects have the following methods:
This method will return an empty tuple if the mixer is not an enumerated This method will return an empty tuple if the mixer is not an enumerated
control. control.
.. method:: Mixer.setenum(index) .. method:: Mixer.setenum(index:int) -> None
For enumerated controls, sets the currently selected item. For enumerated controls, sets the currently selected item.
*index* is an index into the list of available enumerated items returned *index* is an index into the list of available enumerated items returned
by :func:`getenum`. by :func:`getenum`.
.. method:: Mixer.getrange(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_RAW) .. method:: Mixer.getrange(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_RAW) -> tuple[int, int]
Return the volume range of the ALSA mixer controlled by this object. 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 The value is a tuple of integers whose meaning is determined by the
@@ -602,7 +594,7 @@ Mixer objects have the following methods:
The optional *units* argument can be one of :const:`VOLUME_UNITS_PERCENTAGE`, The optional *units* argument can be one of :const:`VOLUME_UNITS_PERCENTAGE`,
:const:`VOLUME_UNITS_RAW`, or :const:`VOLUME_UNITS_DB`. :const:`VOLUME_UNITS_RAW`, or :const:`VOLUME_UNITS_DB`.
.. method:: Mixer.getvolume(pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_PERCENTAGE) .. method:: 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 Returns a list with the current volume settings for each channel. The list
elements are integers whose meaning is determined by the *units* argument. elements are integers whose meaning is determined by the *units* argument.
@@ -615,7 +607,7 @@ Mixer objects have the following methods:
The optional *units* argument can be one of :const:`VOLUME_UNITS_PERCENTAGE`, The optional *units* argument can be one of :const:`VOLUME_UNITS_PERCENTAGE`,
:const:`VOLUME_UNITS_RAW`, or :const:`VOLUME_UNITS_DB`. :const:`VOLUME_UNITS_RAW`, or :const:`VOLUME_UNITS_DB`.
.. method:: Mixer.setvolume(volume, channel=None, pcmtype=PCM_PLAYBACK, units=VOLUME_UNITS_PERCENTAGE) .. method:: Mixer.setvolume(volume:int, pcmtype:int=PCM_PLAYBACK, units:int=VOLUME_UNITS_PERCENTAGE, channel:int|None) -> None
Change the current volume settings for this mixer. The *volume* argument Change the current volume settings for this mixer. The *volume* argument
is an integer whose meaning is determined by the *units* argument. is an integer whose meaning is determined by the *units* argument.
@@ -632,14 +624,14 @@ Mixer objects have the following methods:
The optional *units* argument can be one of :const:`VOLUME_UNITS_PERCENTAGE`, The optional *units* argument can be one of :const:`VOLUME_UNITS_PERCENTAGE`,
:const:`VOLUME_UNITS_RAW`, or :const:`VOLUME_UNITS_DB`. :const:`VOLUME_UNITS_RAW`, or :const:`VOLUME_UNITS_DB`.
.. method:: Mixer.getmute() .. method:: Mixer.getmute() -> list[int]
Return a list indicating the current mute setting for each channel. Return a list indicating the current mute setting for each channel.
0 means not muted, 1 means muted. 0 means not muted, 1 means muted.
This method will fail if the mixer has no playback switch capabilities. This method will fail if the mixer has no playback switch capabilities.
.. method:: Mixer.setmute(mute, [channel]) .. method:: 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 Sets the mute flag to a new value. The *mute* argument is either 0 for not
muted, or 1 for muted. muted, or 1 for muted.
@@ -649,14 +641,14 @@ Mixer objects have the following methods:
This method will fail if the mixer has no playback mute capabilities This method will fail if the mixer has no playback mute capabilities
.. method:: Mixer.getrec() .. method:: Mixer.getrec() -> list[int]
Return a list indicating the current record mute setting for each channel. Return a list indicating the current record mute setting for each channel.
0 means not recording, 1 means recording. 0 means not recording, 1 means recording.
This method will fail if the mixer has no capture switch capabilities. This method will fail if the mixer has no capture switch capabilities.
.. method:: Mixer.setrec(capture, [channel]) .. method:: Mixer.setrec(capture:int, channel:int|None=None) -> None
Sets the capture mute flag to a new value. The *capture* argument Sets the capture mute flag to a new value. The *capture* argument
is either 0 for no capture, or 1 for capture. is either 0 for no capture, or 1 for capture.
@@ -666,21 +658,21 @@ Mixer objects have the following methods:
This method will fail if the mixer has no capture switch capabilities. This method will fail if the mixer has no capture switch capabilities.
.. method:: Mixer.polldescriptors() .. method:: Mixer.polldescriptors() -> list[tuple[int, int]]
Returns a list of tuples of *(file descriptor, eventmask)* that can be Returns a list of tuples of *(file descriptor, eventmask)* that can be
used to wait for changes on the mixer with *select.poll*. 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
:const:`select` module. :const:`select` module.
.. method:: Mixer.handleevents() .. method:: Mixer.handleevents() -> int
Acknowledge events on the :func:`polldescriptors` file descriptors Acknowledge events on the :func:`polldescriptors` file descriptors
to prevent subsequent polls from returning the same events again. to prevent subsequent polls from returning the same events again.
Returns the number of events that were acknowledged. Returns the number of events that were acknowledged.
.. method:: Mixer.close() .. method:: Mixer.close() -> None
Closes the Mixer device. Closes the Mixer device.
@@ -719,7 +711,7 @@ The following example are provided:
* `playbacktest.py` * `playbacktest.py`
* `mixertest.py` * `mixertest.py`
All examples (except `mixertest.py`) accept the commandline option All examples (except `mixertest.py`) accept the commandline option
*-c <cardname>*. *-c <cardname>*.
To determine a valid card name, use the commandline ALSA player:: To determine a valid card name, use the commandline ALSA player::
@@ -734,12 +726,12 @@ or::
>>> alsaaudio.pcms() >>> alsaaudio.pcms()
mixertest.py accepts the commandline options *-d <device>* and mixertest.py accepts the commandline options *-d <device>* and
*-c <cardindex>*. *-c <cardindex>*.
playwav.py playwav.py
~~~~~~~~~~ ~~~~~~~~~~
**playwav.py** plays a wav file. **playwav.py** plays a wav file.
To test PCM playback (on your default soundcard), run:: To test PCM playback (on your default soundcard), run::
@@ -787,7 +779,7 @@ The output might look like this::
'Mix' 'Mix'
'Mix Mono' 'Mix Mono'
With a single argument - the *control*, it will display the settings of With a single argument - the *control*, it will display the settings of
that control; for example:: that control; for example::
$ ./mixertest.py Master $ ./mixertest.py Master
@@ -796,7 +788,7 @@ that control; for example::
Channel 0 volume: 61% Channel 0 volume: 61%
Channel 1 volume: 61% Channel 1 volume: 61%
With two arguments, the *control* and a *parameter*, it will set the With two arguments, the *control* and a *parameter*, it will set the
parameter on the mixer:: parameter on the mixer::
$ ./mixertest.py Master mute $ ./mixertest.py Master mute

40
echo.py Normal file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env python3
# -*- mode: python; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
import alsaaudio
import select
def echo(inpcm, outpcm):
q = list()
# setup the synchronous event loop
# See https://docs.python.org/3/library/select.html#poll-objects for background
reactor = select.poll()
infd, inmask = inpcm.polldecriptors()
outfd, outmask = outpcm.polldescriptors()
write_started = False
def write():
data = q.pop()
written = outpcm.write(data)
if written < len(data):
q.insert(0, data[written:])
reactor.register(infd, inmask)
reactor.register(outfd, outmask)
while True:
events = reactor.poll()
for fd, event in events:
if event == select.POLLIN and fd == infd:
data = inpcm.read()
q.append(data)
if not write_started:
write()
write_started = True
elif event == select.POLLOUT and fd == outfd:
if not q:
return
write()