17 Commits

Author SHA1 Message Date
Oswald Buddenhagen d0625934bb Merge tag '0.10.0'
the tag was erroneously "orphaned".

fixes #140.

Conflicts:
	CHANGES.md
2024-01-31 20:35:28 +01:00
Lars Immisch 788fcb7c69 Ignore volume events if shairplay-sync is running 2023-11-18 15:41:30 +00:00
Lars Immisch 345260241f loopback.py: bugfixes
Retroactively add to CHANGES.md
2023-10-24 19:17:58 +01:00
Lars Immisch 67adbf0524 Add a (naive) loopback implementation (#132)
* WIP

* Implement alsapcm_polldescriptors_revents and alspcm_avail

* Open/close the playback device when idle.

It takes a long time until it's stopped, though.

* open/close logic of playback device

* Fix opening logic, make period size divisible by 6

* Be less verbose in level info

* Extra argument for output mixer card index

Sometimes, this cannot be deduced from the output device

* Better silence detection

* Run run_after_stop when idle on startup
2023-09-12 21:04:02 +02:00
Lars Immisch cd44517ed5 Improve scope (like in #134) 2023-09-04 13:47:20 +01:00
Lars Immisch 3743cf5bd5 Py_BEGIN_ALLOW_THREADS was in the wrong scope. 2023-09-04 13:45:57 +01:00
Lars Immisch f374adb269 Fix issue #133 2023-09-04 13:43:34 +01:00
Lars Immisch 7cc1dadcb2 Restore previous behaviour of calling snd_pcm_prepare in case of XRUN (#131) 2023-08-31 16:25:42 +02:00
Lars Immisch d3bb44517d Small improvement to VolumeForwarder 2023-06-01 15:29:45 +01:00
Lars Immisch c3dba90ff2 Refactor. SCNR.
The Reactor now takes a callable, and the loopback and volume forwarder are now
implemented as callable instances, which seemed the most Pythonic solution.
2023-06-01 15:16:42 +01:00
Lars Immisch e3ec2d1628 Better error logging and comments 2023-05-31 20:00:16 +01:00
Lars Immisch a66cf02a4c Add volume control forwarding.
This needs the patches from (probably) https://lkml.org/lkml/2021/3/1/419. They are already in the raspberry OS kernel sources and the setup works on a RPi 4
2023-05-30 16:21:53 +01:00
Lars Immisch d5e81f2604 mixertest.py: print capture volume 2023-05-30 14:26:56 +01:00
Lars Immisch 8f7e8fbdfe Add a naive loopback implementation using select.poll()
It does work, though.
2023-05-30 14:07:40 +01:00
Lars Immisch dc3d6c6183 Handle events in alsamixer_getvolume. Closes #126
This issue can be worked around by calling mixer.handleevents() before calling mixer.getvolume(), but it makes more sense to handle all events before returning the volume.
2023-05-12 02:43:51 +01:00
Lars Immisch c6a0c8016d Update changes for release 2023-04-16 00:05:55 +02:00
Oswald Buddenhagen 196ca87a05 assorted improvements (#123)
* fix draining/closing, take 2

commit 8abf06be introduced a pause() prior to draining, in an attempt
to work around clearly broken pulseaudio client behavior for capture
streams (drain() is supposed to imply a stop).

but as the workaround was also applied to playback streams, it would
cause nasty "clicks", as the stream would (obviously) stop before being
resumed for draining.

but draining is actually pointless for capture streams, as we're closing
right afterwards, so the samples are lost anyway.

what's more, destructors are not supposed to wait for anything, so
draining in alsapcm_dealloc() was wrong to start with. so we remove it.
note that this is a minor behavior change, which is reflected by the
adjustment of the playback test to have an explicit close() at the end.

finally, close() was also affected by the pulseaudio bug (which was not
addressed before), so there we make draining exclusive to playback
streams.

* fix memory leaks in *_polldescriptors()

the calloc'd pollfd arrays were not freed.

* fix memory handling in mixer access error paths

in case of error, alsamixer_new() would leak the object, while
alsamixer_list() might crash due to a null pointer.

as a drive-by, make alsamixer_gethandle() `static`.

* fix crashes when accessing already closed devices

PCM.htimestamp() gets the usual exception emission,
Mixer.close() gets a "double invocation" check like PCM.close() has.

* fix deprecation warning about PyEval_InitThreads()

PyEval_InitThreads is a no-op in since python 3.9.

* fix deprecation warning about PyUnicode_AsUnicode()

converting to ascii for the purpose of comparison is inefficient.

* remove redundant snd_pcm_hw_params_any() call

we just called it (and even error-checked it) a few lines above.

* add new high-speed samples rates

closes #89 (but alsa doesn't support 768khz yet).

* drop some pointless comments from the tex => sphinx conversion

amends 5c2a00655.

* remove bogus markup from the documentation

the poll objects are linked properly in a different way, and the
footnote appears outdated.

* unify line spacing in .rst files

one empty line, except for high-level sections, which get two.

while at it, trim whitespace on otherwise empty lines.

* formatting/language fixes in introduction document

* improve terminology document

mention xruns, and rework the definition of periods: concentrate on
relevant information, and remove the misinformation about period size
reduction being not that bad (pedantically, an application could run
somewhat asynchronously to the interrupts by using some timer, and
therefore actually save some of the overhead, but why would one use a
small period size in the first place then?).

also, language and formatting fixes.

* add missing and update incorrect/outdated documentation

for clarity, this includes docs which were previously omitted
(presumably) intentionally, but mark them as comments.

the getrec() and getmute() functions' docs are moved around, so they
appear in pairs with their set*() counterparts, like the *volume() ones
already did.

notably, this also fixes the docu of PCM_FORMAT_U8, which closes #104.

* add some best practices to the docu

addresses #110, among other things.

* purge pydoc from the source

it's been obsolete for a *long* time, and having it redundantly to the
rst sources is bad hygiene. it still contained some useful info, which
has been transplanted to the rst source in the previous commit.

* use data types closer to those of ALSA

this removes lots of casts around snd_pcm_hw_params_get_*() calls

we could go further with that to make the code clean if we enabled all
the warnings, but it doesn't seem worth the effort.

* reduce scope of GIL releases

it's pointless to enclose snd_pcm_close() and snd_pcm_pause(), as these
calls don't sleep.

* reshuffle XRUN recovery somewhat

perform it prior to invoking read()/write() if necessary, not right
after a failure event. this makes things more uniform and predictable.

we don't use snd_pcm_recover() any more, as we used it only for the
EPIPE case anyway, which boils down to snd_pcm_prepare() exactly.
handling ESTRPIPE as well might be desirable, but that's a separate
consideration.

* bump (minor) version

we're about to add new features.

* make period count configurable

the period count is just as important for playback latency as the period
size, so it makes no sense to have only one of them configurable.

as a drive-by, fix up the handling of periods in info() & dumpinfo().

* add PCM.drain()

for playback, this allows making sure that all written frames are
played, without using an external delay.

in principle, it's also usable for capture, but there isn't really a
practical reason to do so, as simply discarding excess captured frames
has no real cost.

* add PCM.state() and associated enum values

in principle, the state is already available from info(), but that's a
rather heavy function for something one might want to query often.

a practical use case might be checking whether a playback stream is done
draining, for example.
2023-04-15 21:45:32 +02:00
6 changed files with 575 additions and 26 deletions
+4
View File
@@ -1,3 +1,7 @@
# Version 0.10.1
- restore previous xrun behaviour, #131
- type hints
# Version 0.10.0
- assorted improvements (#123 from @ossilator)
- support for `periods` in the `PCM` constructor.
+131 -15
View File
@@ -196,8 +196,8 @@ get_pcmtype(PyObject *obj)
static bool is_value_volume_unit(long unit)
{
if (unit == VOLUME_UNITS_PERCENTAGE ||
unit == VOLUME_UNITS_RAW ||
unit == VOLUME_UNITS_DB) {
unit == VOLUME_UNITS_RAW ||
unit == VOLUME_UNITS_DB) {
return true;
}
return false;
@@ -1423,8 +1423,6 @@ alsapcm_read(alsapcm_t *self, PyObject *args)
static PyObject *alsapcm_write(alsapcm_t *self, PyObject *args)
{
snd_pcm_state_t state;
int res;
int datalen;
char *data;
PyObject *rc = NULL;
@@ -1445,6 +1443,11 @@ static PyObject *alsapcm_write(alsapcm_t *self, PyObject *args)
if (!self->handle)
{
PyErr_SetString(ALSAAudioError, "PCM device is closed");
#if PY_MAJOR_VERSION >= 3
PyBuffer_Release(&buf);
#endif
return NULL;
}
@@ -1452,27 +1455,46 @@ static PyObject *alsapcm_write(alsapcm_t *self, PyObject *args)
{
PyErr_SetString(ALSAAudioError,
"Data size must be a multiple of framesize");
#if PY_MAJOR_VERSION >= 3
PyBuffer_Release(&buf);
#endif
return NULL;
}
state = snd_pcm_state(self->handle);
int res;
snd_pcm_state_t state = snd_pcm_state(self->handle);
if ((state != SND_PCM_STATE_XRUN && state != SND_PCM_STATE_SETUP) ||
(res = snd_pcm_prepare(self->handle)) >= 0) {
Py_BEGIN_ALLOW_THREADS
res = snd_pcm_writei(self->handle, data, datalen/self->framesize);
if (res == -EPIPE) {
/* EPIPE means underrun */
res = snd_pcm_recover(self->handle, res, 1);
if (res >= 0) {
res = snd_pcm_writei(self->handle, data, datalen/self->framesize);
}
}
Py_END_ALLOW_THREADS
}
if (res == -EAGAIN) {
rc = PyLong_FromLong(0);
}
else if (res < 0)
res = snd_pcm_prepare(self->handle);
if (res < 0)
{
PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(res),
self->cardname);
}
else {
rc = PyLong_FromLong(res);
#if PY_MAJOR_VERSION >= 3
PyBuffer_Release(&buf);
#endif
return NULL;
}
#if PY_MAJOR_VERSION >= 3
@@ -1482,6 +1504,30 @@ static PyObject *alsapcm_write(alsapcm_t *self, PyObject *args)
return rc;
}
static PyObject *
alsapcm_avail(alsapcm_t *self, PyObject *args)
{
if (!PyArg_ParseTuple(args,":avail"))
return NULL;
if (!self->handle)
{
PyErr_SetString(ALSAAudioError, "PCM device is closed");
return NULL;
}
long avail = snd_pcm_avail(self->handle);
// if (avail < 0)
// {
// PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(avail),
// self->cardname);
// return NULL;
// }
return PyLong_FromLong(avail);
}
static PyObject *alsapcm_pause(alsapcm_t *self, PyObject *args)
{
int enabled=1, res;
@@ -1603,6 +1649,72 @@ alsapcm_polldescriptors(alsapcm_t *self, PyObject *args)
return result;
}
static PyObject *
alsapcm_polldescriptors_revents(alsapcm_t *self, PyObject *args)
{
PyObject *list_obj;
if (!PyArg_ParseTuple(args, "O!:polldescriptors_revents", &PyList_Type, &list_obj))
{
PyErr_SetString(PyExc_TypeError, "parameter must be a list.");
return NULL;
}
Py_ssize_t list_size = PyList_Size(list_obj);
struct pollfd *fds = (struct pollfd*)calloc(list_size, sizeof(struct pollfd));
if (!fds)
{
PyErr_Format(PyExc_MemoryError, "Out of memory [%s]",
self->cardname);
return NULL;
}
for (int i = 0; i < list_size; i++)
{
PyObject *tuple_obj = PyList_GetItem(list_obj, i);
if(!PyTuple_Check(tuple_obj)) {
PyErr_SetString(PyExc_TypeError, "list items must be tuples.");
free(fds);
return NULL;
}
Py_ssize_t tuple_size = PyTuple_Size(tuple_obj);
if (tuple_size != 2) {
PyErr_SetString(PyExc_TypeError, "tuples inside list must be (fd: int, mask: int)");
free(fds);
return NULL;
}
PyObject* t0 = PyTuple_GetItem(tuple_obj, 0);
PyObject* t1 = PyTuple_GetItem(tuple_obj, 1);
if (!PyLong_Check(t0) || !PyLong_Check(t1)) {
PyErr_SetString(PyExc_TypeError, "tuples inside list must be (fd: int, mask: int)");
free(fds);
return NULL;
}
// leave fds[i].event as 0 (from calloc) for now
fds[i].fd = PyLong_AS_LONG(t0);
fds[i].revents = PyLong_AS_LONG(t1);
}
unsigned short revents;
int rc = snd_pcm_poll_descriptors_revents(self->handle, fds, (unsigned short)list_size, &revents);
if (rc < 0)
{
PyErr_Format(ALSAAudioError, "%s [%s]", snd_strerror(rc),
self->cardname);
free(fds);
return NULL;
}
free(fds);
return PyLong_FromLong(revents);
}
/* ALSA PCM Object Bureaucracy */
static PyMethodDef alsapcm_methods[] = {
@@ -1627,11 +1739,13 @@ static PyMethodDef alsapcm_methods[] = {
{"getchannels", (PyCFunction)alsapcm_getchannels, METH_VARARGS},
{"read", (PyCFunction)alsapcm_read, METH_VARARGS},
{"write", (PyCFunction)alsapcm_write, METH_VARARGS},
{"avail", (PyCFunction)alsapcm_avail, METH_VARARGS},
{"pause", (PyCFunction)alsapcm_pause, METH_VARARGS},
{"drop", (PyCFunction)alsapcm_drop, METH_VARARGS},
{"drain", (PyCFunction)alsapcm_drain, METH_VARARGS},
{"close", (PyCFunction)alsapcm_close, METH_VARARGS},
{"polldescriptors", (PyCFunction)alsapcm_polldescriptors, METH_VARARGS},
{"polldescriptors_revents", (PyCFunction)alsapcm_polldescriptors_revents, METH_VARARGS},
{NULL, NULL}
};
@@ -2157,13 +2271,10 @@ alsamixer_getvolume(alsamixer_t *self, PyObject *args, PyObject *kwds)
{
snd_mixer_elem_t *elem;
int channel;
long ival;
PyObject *pcmtypeobj = NULL;
long pcmtype;
int iunits = VOLUME_UNITS_PERCENTAGE;
PyObject *result;
PyObject *item;
PyObject *result = NULL;
char *kw[] = { "pcmtype", "units", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:getvolume", kw, &pcmtypeobj, &iunits)) {
@@ -2187,6 +2298,9 @@ alsamixer_getvolume(alsamixer_t *self, PyObject *args, PyObject *kwds)
}
volume_units_t units = iunits;
// handle updates that may have occurred
snd_mixer_handle_events(self->handle);
elem = alsamixer_find_elem(self->handle,self->controlname,self->controlid);
if (!pcmtypeobj || (pcmtypeobj == Py_None)) {
@@ -2201,6 +2315,8 @@ alsamixer_getvolume(alsamixer_t *self, PyObject *args, PyObject *kwds)
result = PyList_New(0);
for (channel = 0; channel <= SND_MIXER_SCHN_LAST; channel++) {
long ival;
if (pcmtype == SND_PCM_STREAM_PLAYBACK &&
snd_mixer_selem_has_playback_channel(elem, channel))
{
@@ -2218,7 +2334,7 @@ alsamixer_getvolume(alsamixer_t *self, PyObject *args, PyObject *kwds)
break;
}
item = PyLong_FromLong(ival);
PyObject* item = PyLong_FromLong(ival);
PyList_Append(result, item);
Py_DECREF(item);
}
@@ -2239,7 +2355,7 @@ alsamixer_getvolume(alsamixer_t *self, PyObject *args, PyObject *kwds)
break;
}
item = PyLong_FromLong(ival);
PyObject* item = PyLong_FromLong(ival);
PyList_Append(result, item);
Py_DECREF(item);
}
+397
View File
@@ -0,0 +1,397 @@
#!/usr/bin/env python3
# -*- mode: python; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
import sys
import select
import logging
import re
import struct
import subprocess
from datetime import datetime, timedelta
from alsaaudio import (PCM, pcms, PCM_PLAYBACK, PCM_CAPTURE, PCM_NONBLOCK, Mixer,
PCM_STATE_OPEN, PCM_STATE_SETUP, PCM_STATE_PREPARED, PCM_STATE_RUNNING, PCM_STATE_XRUN, PCM_STATE_DRAINING,
PCM_STATE_PAUSED, PCM_STATE_SUSPENDED, ALSAAudioError)
from argparse import ArgumentParser
poll_names = {
select.POLLIN: 'POLLIN',
select.POLLPRI: 'POLLPRI',
select.POLLOUT: 'POLLOUT',
select.POLLERR: 'POLLERR',
select.POLLHUP: 'POLLHUP',
select.POLLRDHUP: 'POLLRDHUP',
select.POLLNVAL: 'POLLNVAL'
}
state_names = {
PCM_STATE_OPEN: 'PCM_STATE_OPEN',
PCM_STATE_SETUP: 'PCM_STATE_SETUP',
PCM_STATE_PREPARED: 'PCM_STATE_PREPARED',
PCM_STATE_RUNNING: 'PCM_STATE_RUNNING',
PCM_STATE_XRUN: 'PCM_STATE_XRUN',
PCM_STATE_DRAINING: 'PCM_STATE_DRAINING',
PCM_STATE_PAUSED: 'PCM_STATE_PAUSED',
PCM_STATE_SUSPENDED: 'PCM_STATE_SUSPENDED'
}
def poll_desc(mask):
return '|'.join([poll_names[bit] for bit, name in poll_names.items() if mask & bit])
class PollDescriptor(object):
'''File Descriptor, event mask and a name for logging'''
def __init__(self, name, fd, mask):
self.name = name
self.fd = fd
self.mask = mask
def as_tuple(self):
return (self.fd, self.mask)
@classmethod
def from_alsa_object(cls, name, alsaobject, mask=None):
# TODO maybe refactor: we ignore objects that have more then one polldescriptor
fd, alsamask = alsaobject.polldescriptors()[0]
if mask is None:
mask = alsamask
return cls(name, fd, mask)
class Loopback(object):
'''Loopback state and event handling'''
def __init__(self, capture, playback_args, volume_handler, run_after_stop=None, run_before_start=None):
self.playback_args = playback_args
self.playback = None
self.volume_handler = volume_handler
self.capture_started = None
self.last_capture_event = None
self.capture = capture
self.capture_pd = PollDescriptor.from_alsa_object('capture', capture)
self.run_after_stop = run_after_stop.split(' ')
self.run_before_start = run_before_start.split(' ')
self.run_after_stop_did_run = False
self.waitBeforeOpen = False
self.queue = []
self.period_size = 0
self.silent_periods = 0
@staticmethod
def compute_energy(data):
values = struct.unpack(f'{len(data)//2}h', data)
e = 0
for v in values:
e = e + v * v
return e
@staticmethod
def run_command(cmd):
if cmd:
rc = subprocess.run(cmd)
if rc.returncode:
logging.warning(f'run {cmd}, return code {rc.returncode}')
else:
logging.info(f'run {cmd}, return code {rc.returncode}')
def register(self, reactor):
reactor.register_timeout_handler(self.timeout_handler)
reactor.register(self.capture_pd, self)
def start(self):
# start reading data
size, data = self.capture.read()
if size:
self.queue.append(data)
def timeout_handler(self):
if self.playback and self.capture_started:
if self.last_capture_event:
if datetime.now() - self.last_capture_event > timedelta(seconds=2):
logging.info('timeout - closing playback device')
self.playback.close()
self.playback = None
self.capture_started = None
if self.volume_handler:
self.volume_handler.stop()
self.run_command(self.run_after_stop)
return
self.waitBeforeOpen = False
if not self.run_after_stop_did_run and not self.playback:
if self.volume_handler:
self.volume_handler.stop()
self.run_command(self.run_after_stop)
self.run_after_stop_did_run = True
def pop(self):
if len(self.queue):
return self.queue.pop()
else:
return None
def handle_capture_event(self, eventmask, name):
'''called when data is available for reading'''
self.last_capture_event = datetime.now()
size, data = self.capture.read()
if not size:
logging.warning(f'capture event but no data')
return False
energy = self.compute_energy(data)
logging.debug(f'energy: {energy}')
# the usecase is a USB capture device where we get perfect silence when it's idle
if energy == 0:
self.silent_periods = self.silent_periods + 1
# turn off playback after two seconds of silence
# 2 channels * 2 seconds * 2 bytes per sample
fps = self.playback_args['rate'] * 8 // (self.playback_args['periodsize'] * self.playback_args['periods'])
logging.debug(f'{self.silent_periods} of {fps} silent periods: {self.playback}')
if self.silent_periods > fps and self.playback:
logging.info(f'closing playback due to silence')
self.playback.close()
self.playback = None
if self.volume_handler:
self.volume_handler.stop()
self.run_command(self.run_after_stop)
self.run_after_stop_did_run = True
if not self.playback:
return
else:
self.silent_periods = 0
if not self.playback:
if self.waitBeforeOpen:
return False
try:
if self.volume_handler:
self.volume_handler.start()
self.run_command(self.run_before_start)
self.playback = PCM(**self.playback_args)
self.period_size = self.playback.info()['period_size']
logging.info(f'opened playback device with period_size {self.period_size}')
except ALSAAudioError as e:
logging.info('opening PCM playback device failed: %s', e)
self.waitBeforeOpen = True
return False
self.capture_started = datetime.now()
logging.info(f'{self.playback} capture started: {self.capture_started}')
self.queue.append(data)
if len(self.queue) <= 2:
logging.info(f'buffering: {len(self.queue)}')
return False
try:
data = self.pop()
if data:
space = self.playback.avail()
written = self.playback.write(data)
logging.debug(f'wrote {written} bytes while space was {space}')
except ALSAAudioError:
logging.error('underrun', exc_info=1)
return True
def __call__(self, fd, eventmask, name):
if fd == self.capture_pd.fd:
real_mask = self.capture.polldescriptors_revents([self.capture_pd.as_tuple()])
if real_mask:
return self.handle_capture_event(real_mask, name)
else:
logging.debug('null capture event')
return False
else:
real_mask = self.playback.polldescriptors_revents([self.playback_pd.as_tuple()])
if real_mask:
return self.handle_playback_event(real_mask, name)
else:
logging.debug('null playback event')
return False
class VolumeForwarder(object):
'''Volume control event handling'''
def __init__(self, capture_control, playback_control):
self.playback_control = playback_control
self.capture_control = capture_control
self.active = True
def start(self):
self.active = True
if self.volume:
self.volume = playback_control.setvolume(self.volume)
def stop(self):
self.active = False
self.volume = self.playback_control.getvolume(pcmtype=PCM_CAPTURE)[0]
def __call__(self, fd, eventmask, name):
if not self.active:
return
volume = self.capture_control.getvolume(pcmtype=PCM_CAPTURE)
# indicate that we've handled the event
self.capture_control.handleevents()
logging.info(f'{name} adjusting volume to {volume}')
if volume:
self.playback_control.setvolume(volume[0])
class Reactor(object):
'''A wrapper around select.poll'''
def __init__(self):
self.poll = select.poll()
self.descriptors = {}
self.timeout_handlers = set()
def register(self, polldescriptor, callable):
logging.debug(f'registered {polldescriptor.name}: {poll_desc(polldescriptor.mask)}')
self.descriptors[polldescriptor.fd] = (polldescriptor, callable)
self.poll.register(polldescriptor.fd, polldescriptor.mask)
def unregister(self, polldescriptor):
self.poll.unregister(polldescriptor.fd)
del self.descriptors[polldescriptor.fd]
def register_timeout_handler(self, callable):
self.timeout_handlers.add(callable)
def unregister_timeout_handler(self, callable):
self.timeout_handlers.remove(callable)
def run(self):
last_timeout_ev = datetime.now()
while True:
# poll for a bit, then send a timeout to registered handlers
events = self.poll.poll(0.25)
for fd, ev in events:
polldescriptor, handler = self.descriptors[fd]
# very chatty - log all events
# logging.debug(f'{polldescriptor.name}: {poll_desc(ev)} ({ev})')
handler(fd, ev, polldescriptor.name)
if datetime.now() - last_timeout_ev > timedelta(seconds=0.25):
for t in self.timeout_handlers:
t()
last_timeout_ev = datetime.now()
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
parser = ArgumentParser(description='ALSA loopback (with volume forwarding)')
playback_pcms = pcms(pcmtype=PCM_PLAYBACK)
capture_pcms = pcms(pcmtype=PCM_CAPTURE)
if not playback_pcms:
logging.error('no playback PCM found')
sys.exit(2)
if not capture_pcms:
logging.error('no capture PCM found')
sys.exit(2)
parser.add_argument('-d', '--debug', action='store_true')
parser.add_argument('-i', '--input', default=capture_pcms[0])
parser.add_argument('-o', '--output', default=playback_pcms[0])
parser.add_argument('-r', '--rate', type=int, default=44100)
parser.add_argument('-c', '--channels', type=int, default=2)
parser.add_argument('-p', '--periodsize', type=int, default=444) # must be divisible by 6 for 44k1
parser.add_argument('-P', '--periods', type=int, default=2)
parser.add_argument('-I', '--input-mixer', help='Control of the input mixer, can contain the card index, e.g. Digital:2')
parser.add_argument('-O', '--output-mixer', help='Control of the output mixer, can contain the card index, e.g. PCM:1')
parser.add_argument('-A', '--run-after-stop', help='command to run when the capture device is idle/silent')
parser.add_argument('-B', '--run-before-start', help='command to run when the capture device becomes active')
parser.add_argument('-V', '--volume', help='Initial volume (default is leave unchanged)')
args = parser.parse_args()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
playback_args = {
'type': PCM_PLAYBACK,
'mode': PCM_NONBLOCK,
'device': args.output,
'rate': args.rate,
'channels': args.channels,
'periodsize': args.periodsize,
'periods': args.periods
}
reactor = Reactor()
# If args.input_mixer and args.output_mixer are set, forward the capture volume to the playback volume.
# The usecase is a capture device that is implemented using g_audio, i.e. the Linux USB gadget driver.
# When a USB device (eg. an iPad) is connected to this machine, its volume events will go to the volume control
# of the output device
capture = None
playback = None
volume_handler = None
if args.input_mixer and args.output_mixer:
re_mixer = re.compile(r'([a-zA-Z0-9]+):?([0-9+])?')
input_mixer_card = None
m = re_mixer.match(args.input_mixer)
if m:
input_mixer = m.group(1)
if m.group(2):
input_mixer_card = int(m.group(2))
else:
parser.print_usage()
sys.exit(1)
output_mixer_card = None
m = re_mixer.match(args.output_mixer)
if m:
output_mixer = m.group(1)
if m.group(2):
output_mixer_card = int(m.group(2))
else:
parser.print_usage()
sys.exit(1)
if input_mixer_card is None:
capture = PCM(type=PCM_CAPTURE, mode=PCM_NONBLOCK, device=args.input, rate=args.rate,
channels=args.channels, periodsize=args.periodsize, periods=args.periods)
input_mixer_card = capture.info()['card_no']
if output_mixer_card is None:
playback = PCM(**playback_args)
output_mixer_card = playback.info()['card_no']
playback.close()
playback_control = Mixer(control=output_mixer, cardindex=int(output_mixer_card))
capture_control = Mixer(control=input_mixer, cardindex=int(input_mixer_card))
volume_handler = VolumeForwarder(capture_control, playback_control)
reactor.register(PollDescriptor.from_alsa_object('capture_control', capture_control, select.POLLIN), volume_handler)
if args.volume and playback_control:
playback_control.setvolume(int(args.volume))
loopback = Loopback(capture, playback_args, volume_handler, args.run_after_stop, args.run_before_start)
loopback.register(reactor)
loopback.start()
reactor.run()
+8 -3
View File
@@ -72,8 +72,13 @@ def show_mixer(name, kwargs):
volumes = mixer.getvolume()
volumes_dB = mixer.getvolume(units=alsaaudio.VOLUME_UNITS_DB)
for i in range(len(volumes)):
print("Channel %i volume: %i%% (%.1f dB)" % (i, volumes[i], volumes_dB[i] / 100.0))
print("Channel %i playback volume: %i%% (%.1f dB)" % (i, volumes[i], volumes_dB[i] / 100.0))
volumes = mixer.getvolume(pcmtype=alsaaudio.PCM_CAPTURE)
volumes_dB = mixer.getvolume(pcmtype=alsaaudio.PCM_CAPTURE, units=alsaaudio.VOLUME_UNITS_DB)
for i in range(len(volumes)):
print("Channel %i capture volume: %i%% (%.1f dB)" % (i, volumes[i], volumes_dB[i] / 100.0))
try:
mutes = mixer.getmute()
for i in range(len(mutes)):
@@ -113,7 +118,7 @@ def set_mixer(name, args, kwargs):
mixer.setmute(1, channel)
else:
mixer.setmute(0, channel)
elif args in ['rec','unrec']:
# Enable/disable recording
if args == 'rec':
+3 -3
View File
@@ -8,7 +8,7 @@ from setuptools import setup
from setuptools.extension import Extension
from sys import version
pyalsa_version = '0.10.0'
pyalsa_version = '0.10.1'
if __name__ == '__main__':
setup(
@@ -29,12 +29,12 @@ if __name__ == '__main__':
'License :: OSI Approved :: Python Software Foundation License',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3',
'Topic :: Multimedia :: Sound/Audio',
'Topic :: Multimedia :: Sound/Audio :: Mixers',
'Topic :: Multimedia :: Sound/Audio :: Players',
'Topic :: Multimedia :: Sound/Audio :: Capture/Recording',
],
ext_modules=[Extension('alsaaudio',['alsaaudio.c'],
ext_modules=[Extension('alsaaudio',['alsaaudio.c'],
libraries=['asound'])]
)
+32 -5
View File
@@ -11,6 +11,7 @@
import unittest
import alsaaudio
import warnings
from contextlib import closing
# we can't test read and write well - these are tested otherwise
PCMMethods = [
@@ -20,7 +21,7 @@ PCMMethods = [
]
PCMDeprecatedMethods = [
('setchannels', (2,)),
('setchannels', (2,)),
('setrate', (44100,)),
('setformat', (alsaaudio.PCM_FORMAT_S8,)),
('setperiodsize', (320,))
@@ -49,10 +50,10 @@ class MixerTest(unittest.TestCase):
def testMixer(self):
"""Open the default Mixers and the Mixers on every card"""
for c in alsaaudio.card_indexes():
mixers = alsaaudio.mixers(cardindex=c)
for m in mixers:
mixer = alsaaudio.Mixer(m, cardindex=c)
mixer.close()
@@ -73,7 +74,7 @@ class MixerTest(unittest.TestCase):
mixer.close()
def testMixerClose(self):
"""Run common Mixer methods on a closed object and verify it raises an
"""Run common Mixer methods on a closed object and verify it raises an
error"""
mixers = alsaaudio.mixers()
@@ -133,7 +134,7 @@ class PCMTest(unittest.TestCase):
pcm = alsaaudio.PCM(card='default')
except alsaaudio.ALSAAudioError:
pass
# Verify we got a DepreciationWarning
self.assertEqual(len(w), 1, "PCM(card='default') expected a warning" )
self.assertTrue(issubclass(w[-1].category, DeprecationWarning), "PCM(card='default') expected a DeprecationWarning")
@@ -156,5 +157,31 @@ class PCMTest(unittest.TestCase):
self.assertEqual(len(w), 1, method + " expected a warning")
self.assertTrue(issubclass(w[-1].category, DeprecationWarning), method + " expected a DeprecationWarning")
class PollDescriptorArgsTest(unittest.TestCase):
'''Test invalid args for polldescriptors_revents (takes a list of tuples of 2 integers)'''
def testArgsNoList(self):
with closing(alsaaudio.PCM()) as pcm:
with self.assertRaises(TypeError):
pcm.polldescriptors_revents('foo')
def testArgsListButNoTuples(self):
with closing(alsaaudio.PCM()) as pcm:
with self.assertRaises(TypeError):
pcm.polldescriptors_revents(['foo', 1])
def testArgsListButInvalidTuples(self):
with closing(alsaaudio.PCM()) as pcm:
with self.assertRaises(TypeError):
pcm.polldescriptors_revents([('foo', 'bar')])
def testArgsListTupleWrongLength(self):
with closing(alsaaudio.PCM()) as pcm:
with self.assertRaises(TypeError):
pcm.polldescriptors_revents([(1, )])
with self.assertRaises(TypeError):
pcm.polldescriptors_revents([(1, 2, 3)])
if __name__ == '__main__':
unittest.main()