forked from auracaster/pyalsaaudio
Whitespace cleanup.
I ended up using Visual Studio Code and did a global regex replace ` +\n` -> `\n` (StackOverflow)
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
For documentation, see http://larsimmisch.github.io/pyalsaaudio/
|
For documentation, see http://larsimmisch.github.io/pyalsaaudio/
|
||||||
|
|
||||||
> Author: Casper Wilstrup (cwi@aves.dk)
|
> Author: Casper Wilstrup (cwi@aves.dk)
|
||||||
> Maintainer: Lars Immisch (lars@ibp.de)
|
> Maintainer: Lars Immisch (lars@ibp.de)
|
||||||
|
|
||||||
This package contains wrappers for accessing the
|
This package contains wrappers for accessing the
|
||||||
@@ -45,12 +45,12 @@ First, get the sources and change to the source directory:
|
|||||||
$ git clone https://github.com/larsimmisch/pyalsaaudio.git
|
$ git clone https://github.com/larsimmisch/pyalsaaudio.git
|
||||||
$ cd pyalsaaudio
|
$ cd pyalsaaudio
|
||||||
```
|
```
|
||||||
|
|
||||||
Then, build:
|
Then, build:
|
||||||
```
|
```
|
||||||
$ python setup.py build
|
$ python setup.py build
|
||||||
```
|
```
|
||||||
|
|
||||||
And install:
|
And install:
|
||||||
```
|
```
|
||||||
$ sudo python setup.py install
|
$ sudo python setup.py install
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ Don't forget to update the documentation.
|
|||||||
The documentation is published through the `gh-pages` branch.
|
The documentation is published through the `gh-pages` branch.
|
||||||
|
|
||||||
To publish the documentation, you need to clone the `gh-pages` branch of this repository into
|
To publish the documentation, you need to clone the `gh-pages` branch of this repository into
|
||||||
`doc/gh-pages`. In `doc`, do:
|
`doc/gh-pages`. In `doc`, do:
|
||||||
|
|
||||||
git clone -b gh-pages git@github.com:larsimmisch/pyalsaaudio.git gh-pages
|
git clone -b gh-pages git@github.com:larsimmisch/pyalsaaudio.git gh-pages
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ The :mod:`alsaaudio` module defines functions and classes for using ALSA.
|
|||||||
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:**
|
||||||
|
|
||||||
@@ -102,12 +102,12 @@ following arguments:
|
|||||||
recording). The constructor's arguments are:
|
recording). The constructor's 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`.
|
||||||
|
|
||||||
========================= ===============
|
========================= ===============
|
||||||
@@ -206,19 +206,19 @@ PCM objects have the following methods:
|
|||||||
descriptions are prefixed with "hw:" below.
|
descriptions are prefixed with "hw:" below.
|
||||||
|
|
||||||
=========================== ============================= ==================================================================
|
=========================== ============================= ==================================================================
|
||||||
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
|
||||||
@@ -241,7 +241,7 @@ 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
|
The italicized descriptions give a summary of the "full" description
|
||||||
@@ -346,7 +346,7 @@ PCM objects have the following methods:
|
|||||||
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
|
||||||
@@ -422,7 +422,7 @@ PCM objects have the following methods:
|
|||||||
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.polldescriptors_revents(descriptors)
|
.. method:: PCM.polldescriptors_revents(descriptors)
|
||||||
@@ -485,7 +485,7 @@ PCM objects have the following methods:
|
|||||||
|
|
||||||
**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
|
||||||
@@ -523,7 +523,7 @@ Mixer objects provides access to the ALSA mixer API.
|
|||||||
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.
|
||||||
@@ -533,7 +533,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'``.
|
||||||
@@ -570,7 +570,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
|
||||||
====================== ================
|
====================== ================
|
||||||
@@ -709,7 +709,7 @@ Mixer objects have the following methods:
|
|||||||
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()
|
||||||
@@ -757,7 +757,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::
|
||||||
@@ -772,12 +772,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::
|
||||||
|
|
||||||
@@ -825,7 +825,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
|
||||||
@@ -834,7 +834,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
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ and need the ALSA headers for compilation. Verify that you have
|
|||||||
|
|
||||||
Naturally you also need to use a kernel with proper ALSA support. This is the
|
Naturally you also need to use a kernel with proper ALSA support. This is the
|
||||||
default in Linux kernel 2.6 and later. If you are using kernel version 2.4 you
|
default in Linux kernel 2.6 and later. If you are using kernel version 2.4 you
|
||||||
may need to install the ALSA patches yourself - although most distributions
|
may need to install the ALSA patches yourself - although most distributions
|
||||||
ship with ALSA kernels.
|
ship with ALSA kernels.
|
||||||
|
|
||||||
To install, execute the following: --- ::
|
To install, execute the following: --- ::
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ In order to use PCM devices it is useful to be familiar with some concepts and
|
|||||||
terminology.
|
terminology.
|
||||||
|
|
||||||
Sample
|
Sample
|
||||||
PCM audio, whether it is input or output, consists of *samples*.
|
PCM audio, whether it is input or output, consists of *samples*.
|
||||||
A single sample represents the amplitude of one channel of sound
|
A single sample represents the amplitude of one channel of sound
|
||||||
at a certain point in time. A lot of individual samples are
|
at a certain point in time. A lot of individual samples are
|
||||||
necessary to represent actual sound; for CD audio, 44100 samples
|
necessary to represent actual sound; for CD audio, 44100 samples
|
||||||
@@ -22,8 +22,8 @@ Sample
|
|||||||
loudest signal that can be reproduced.
|
loudest signal that can be reproduced.
|
||||||
|
|
||||||
Frame
|
Frame
|
||||||
A frame consists of exactly one sample per channel. If there is only one
|
A frame consists of exactly one sample per channel. If there is only one
|
||||||
channel (Mono sound) a frame is simply a single sample. If the sound is
|
channel (Mono sound) a frame is simply a single sample. If the sound is
|
||||||
stereo, each frame consists of two samples, etc.
|
stereo, each frame consists of two samples, etc.
|
||||||
|
|
||||||
Frame size
|
Frame size
|
||||||
@@ -33,7 +33,7 @@ Frame size
|
|||||||
is 48 bytes.
|
is 48 bytes.
|
||||||
|
|
||||||
Rate
|
Rate
|
||||||
PCM sound consists of a flow of sound frames. The sound rate controls how
|
PCM sound consists of a flow of sound frames. The sound rate controls how
|
||||||
often the current frame is replaced. For example, a rate of 8000 Hz
|
often the current frame is replaced. For example, a rate of 8000 Hz
|
||||||
means that a new frame is played or captured 8000 times per second.
|
means that a new frame is played or captured 8000 times per second.
|
||||||
|
|
||||||
|
|||||||
12
isine.py
12
isine.py
@@ -44,18 +44,18 @@ def generate(frequency, duration = 0.125):
|
|||||||
|
|
||||||
# total number of frames
|
# total number of frames
|
||||||
size = cycle_size * factor
|
size = cycle_size * factor
|
||||||
|
|
||||||
sine = [ int(32767 * sin(2 * pi * frequency * i / sampling_rate)) \
|
sine = [ int(32767 * sin(2 * pi * frequency * i / sampling_rate)) \
|
||||||
for i in range(size)]
|
for i in range(size)]
|
||||||
|
|
||||||
if channels > 1:
|
if channels > 1:
|
||||||
sine = list(itertools.chain.from_iterable(itertools.repeat(x, channels) for x in sine))
|
sine = list(itertools.chain.from_iterable(itertools.repeat(x, channels) for x in sine))
|
||||||
|
|
||||||
return struct.pack(str(size * channels) + pack_format, *sine)
|
return struct.pack(str(size * channels) + pack_format, *sine)
|
||||||
|
|
||||||
|
|
||||||
class SinePlayer(Thread):
|
class SinePlayer(Thread):
|
||||||
|
|
||||||
def __init__(self, frequency = 440.0):
|
def __init__(self, frequency = 440.0):
|
||||||
Thread.__init__(self, daemon=True)
|
Thread.__init__(self, daemon=True)
|
||||||
self.device = alsaaudio.PCM(channels=channels, format=format, rate=sampling_rate)
|
self.device = alsaaudio.PCM(channels=channels, format=format, rate=sampling_rate)
|
||||||
@@ -75,7 +75,7 @@ class SinePlayer(Thread):
|
|||||||
|
|
||||||
buf = generate(f)
|
buf = generate(f)
|
||||||
self.queue.put(buf)
|
self.queue.put(buf)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
buffer = None
|
buffer = None
|
||||||
while True:
|
while True:
|
||||||
@@ -86,7 +86,7 @@ class SinePlayer(Thread):
|
|||||||
if buffer:
|
if buffer:
|
||||||
if self.device.write(buffer) < 0:
|
if self.device.write(buffer) < 0:
|
||||||
print("Playback buffer underrun! Continuing nonetheless ...")
|
print("Playback buffer underrun! Continuing nonetheless ...")
|
||||||
|
|
||||||
|
|
||||||
isine = SinePlayer()
|
isine = SinePlayer()
|
||||||
isine.start()
|
isine.start()
|
||||||
|
|||||||
@@ -51,4 +51,4 @@ if __name__ == '__main__':
|
|||||||
print("Playback buffer underrun! Continuing nonetheless ...")
|
print("Playback buffer underrun! Continuing nonetheless ...")
|
||||||
data = f.read(320)
|
data = f.read(320)
|
||||||
out.close()
|
out.close()
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
f = open(args[0], 'wb')
|
f = open(args[0], 'wb')
|
||||||
|
|
||||||
# Open the device in nonblocking capture mode in mono, with a sampling rate of 44100 Hz
|
# Open the device in nonblocking capture mode in mono, with a sampling rate of 44100 Hz
|
||||||
# and 16 bit little endian samples
|
# and 16 bit little endian samples
|
||||||
# The period size controls the internal number of frames per period.
|
# The period size controls the internal number of frames per period.
|
||||||
# The significance of this parameter is documented in the ALSA api.
|
# The significance of this parameter is documented in the ALSA api.
|
||||||
@@ -49,8 +49,8 @@ if __name__ == '__main__':
|
|||||||
# This means that the reads below will return either 320 bytes of data
|
# This means that the reads below will return either 320 bytes of data
|
||||||
# or 0 bytes of data. The latter is possible because we are in nonblocking
|
# or 0 bytes of data. The latter is possible because we are in nonblocking
|
||||||
# mode.
|
# mode.
|
||||||
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK,
|
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK,
|
||||||
channels=1, rate=44100, format=alsaaudio.PCM_FORMAT_S16_LE,
|
channels=1, rate=44100, format=alsaaudio.PCM_FORMAT_S16_LE,
|
||||||
periodsize=160, device=device)
|
periodsize=160, device=device)
|
||||||
|
|
||||||
loops = 1000000
|
loops = 1000000
|
||||||
|
|||||||
Reference in New Issue
Block a user