mixer simple basic abstraction - added python binding

reasons:
- rapid development
- class-like code structure
- more readable code
features:
- hcontrol binding is managed from python (opportunity to create
  virtual mixer without driver or join multiple cards to behave as one)
This commit is contained in:
Jaroslav Kysela 2007-07-11 10:10:12 +02:00
parent 582cc1f098
commit e0d7bfcea6
9 changed files with 1436 additions and 33 deletions

View File

@ -336,6 +336,23 @@ AC_ARG_ENABLE(instr,
AC_ARG_ENABLE(alisp,
AS_HELP_STRING([--disable-alisp], [disable the alisp component]),
[build_alisp="$enableval"], [build_alisp="yes"])
AC_ARG_ENABLE(python,
AS_HELP_STRING([--disable-python], [disable the python components]),
[build_python="$enableval"], [build_python="yes"])
PYTHON_LIBS=""
if test "$build_python" = "yes"; then
AC_ARG_WITH(pythonlibs,
AS_HELP_STRING([--with-pythonlibs=ldflags],
[specify python libraries (-lpthread -lm -ldl -lpython2.4)]),
pythonlibs="$withval", pythonlibs=`python-config --libs`)
if test -z "$pythonlibs" ; then
echo "Unable to determine python libraries! Probably python-config is not"
echo "available on this system. Please, use --with-pythonlibs options."
exit 1
fi
PYTHON_LIBS="$pythonlibs"
fi
AC_SUBST(PYTHON_LIBS)
if test "$build_seq" != "yes"; then
build_instr="no"
@ -348,6 +365,7 @@ AM_CONDITIONAL(BUILD_HWDEP, test x$build_hwdep = xyes)
AM_CONDITIONAL(BUILD_SEQ, test x$build_seq = xyes)
AM_CONDITIONAL(BUILD_INSTR, test x$build_instr = xyes)
AM_CONDITIONAL(BUILD_ALISP, test x$build_alisp = xyes)
AM_CONDITIONAL(BUILD_PYTHON, test x$build_python = xyes)
if test "$build_mixer" = "yes"; then
AC_DEFINE([BUILD_MIXER], "1", [Build mixer component])

View File

@ -1,4 +1,5 @@
pkglibdir = @ALSA_PLUGIN_DIR@/smixer
pythonlibs = @PYTHON_LIBS@
AM_CFLAGS = -g -O2 -W -Wall
@ -8,6 +9,10 @@ pkglib_LTLIBRARIES = smixer-sbase.la \
smixer-ac97.la \
smixer-hda.la
if BUILD_PYTHON
pkglib_LTLIBRARIES += smixer-python.la
endif
noinst_HEADERS = sbase.h
smixer_sbase_la_SOURCES = sbase.c
@ -21,3 +26,9 @@ smixer_ac97_la_LIBADD = ../../../src/libasound.la
smixer_hda_la_SOURCES = hda.c sbasedl.c
smixer_hda_la_LDFLAGS = -module -avoid-version
smixer_hda_la_LIBADD = ../../../src/libasound.la
if BUILD_PYTHON
smixer_python_la_SOURCES = python.c
smixer_python_la_LDFLAGS = -module -avoid-version $(pythonlibs)
smixer_python_la_LIBADD = ../../../src/libasound.la
endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,228 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- Python -*-
from pyalsa.alsahcontrol import HControl, Element as HElement, \
Info as HInfo, Value as HValue, InterfaceId, \
EventMask, EventMaskRemove
MIXER = InterfaceId['Mixer']
MIXERS = str(MIXER)
class BaseElement(InternalMElement):
def __init__(self, mixer, name, index, weight):
InternalMElement.__init__(self, mixer, name, index, weight)
self.channels = 0
self.min = [0, 0]
self.max = [0, 0]
def opsIsChannel(self, dir, chn):
return chn >= 0 and chn < self.channels
def opsGetRange(self, dir):
return (0, self.min[dir], self.max[dir])
def opsSetRange(self, dir, min, max):
self.min[dir] = min
self.max[dir] = max
def volumeToUser(self, info, dir, value):
min = info.min
max = info.max
if min == max:
return self.min[dir]
n = (value - min) * (self.max[dir] - self.min[dir])
return self.min[dir] + (n + (max - min) / 2) / (max - min)
def volumeFromUser(self, info, dir, value):
min = info.min
max = info.max
if self.max[dir] == self.min[dir]:
return min
n = (value - self.min[dir]) * (max - min)
return min + (n + (self.max[dir] - self.min[dir]) / 2) / (self.max[dir] - self.min[dir])
class StandardElement(BaseElement):
def __init__(self, mixer, name, index, weight):
BaseElement.__init__(self, mixer, name, index, weight)
self.channels = 1
self.volume = [None, None]
self.volumeinfo = [None, None]
self.volumetuple = [None, None]
self.switch = [None, None]
self.switchinfo = [None, None]
self.switchtuple = [None, None]
def decideChannels(self):
max = 0
for i in [0, 1]:
if self.volume[i]:
count = self.volumeinfo[i].count
if count > max:
max = count
if self.switch[i]:
count = self.switchinfo[i].count
if count > max:
max = count
self.channels = max
def attachVolume(self, helem, dir):
self.volume[dir] = helem
self.volumeinfo[dir] = HInfo(helem)
self.min[dir] = self.volumeinfo[dir].min
self.max[dir] = self.volumeinfo[dir].max
self.volumetuple[dir] = HValue(helem).getTuple(self.volumeinfo[dir].type, self.volumeinfo[dir].count)
def attachSwitch(self, helem, dir):
self.switch[dir] = helem
self.switchinfo[dir] = HInfo(helem)
self.switchtuple[dir] = HValue(helem).getTuple(self.switchinfo[dir].type, self.switchinfo[dir].count)
def attach(self, helem):
BaseElement.attach(self, helem)
if helem.name.endswith('Playback Volume'):
self.attachVolume(helem, 0)
self.caps |= self.CAP_PVOLUME
elif helem.name.endswith('Capture Volume'):
self.attachVolume(helem, 1)
self.caps |= self.CAP_CVOLUME
elif helem.name.endswith('Playback Switch'):
self.attachSwitch(helem, 0)
self.caps |= self.CAP_PSWITCH
elif helem.name.endswith('Capture Switch'):
self.attachSwitch(helem, 1)
self.caps |= self.CAP_CSWITCH
self.decideChannels()
self.eventInfo()
def opsGetVolume(self, dir, chn):
return (0, self.volumeToUser(self.volumeinfo[dir], dir, self.volumetuple[dir][chn]))
def opsSetVolume(self, dir, chn, value):
val = self.volumeFromUser(self.volumeinfo[dir], dir, value)
if self.volumetuple[dir][chn] == val:
return
a = list(self.volumetuple[dir])
a[chn] = val
self.volumetuple[dir] = tuple(a)
hv = HValue(self.volume)
hv.setTuple(self.volumeinfo[dir].type, self.volumetuple[dir])
hv.write()
def opsGetSwitch(self, dir, chn):
return (0, self.switchtuple[dir][chn])
def opsSetSwitch(self, dir, chn, value):
if self.switchtuple[dir][chn] and value:
return
if not self.switchtuple[dir][chn] and not value:
return
a = list(self.switchtuple[dir])
a[chn] = int(value)
self.switchtuple[dir] = tuple(a)
hv = HValue(self.switch[dir])
hv.setTuple(self.switchinfo[dir].type, self.switchtuple[dir])
hv.write()
def update(self, helem):
for i in [0, 1]:
if helem == self.volume[i]:
self.volumetuple[i] = HValue(helem).getTuple(self.volumeinfo[i].type, self.volumeinfo[i].count)
elif helem == self.switch[i]:
self.switchtuple[i] = HValue(helem).getTuple(self.switchinfo[i].type, self.switchinfo[i].count)
self.eventValue()
class EnumElement(BaseElement):
def __init__(self, mixer, name, index, weight):
BaseElement.__init__(self, mixer, name, index, weight)
self.mycaps = 0
def attach(self, helem):
BaseElement.attach(self, helem)
self.enum = helem
self.enuminfo = HInfo(helem)
self.enumtuple = HValue(helem).getTuple(self.enuminfo.type, self.enuminfo.count)
self.channels = self.enuminfo.count
self.texts = self.enuminfo.itemNames
self.caps |= self.mycaps
def opsIsEnumerated(self, dir=-1):
if dir < 0:
return 1
if dir == 0 and self.mycaps & self.CAP_PENUM:
return 1
if dir == 1 and self.mycaps & self.CAP_CENUM:
return 1
def opsIsEnumCnt(self, dir):
return self.enuminfo.items
def opsGetEnumItemName(self, item):
return (0, self.texts[item])
def opsGetEnumItem(self, chn):
if chn >= self.channels:
return -1
return (0, self.enumtuple[chn])
def opsSetEnumItem(self, chn, value):
if chn >= self.channels:
return -1
if self.enumtuple[chn] == value:
return
a = list(self.enumtuple)
a[chn] = int(value)
self.enumtuple = tuple(a)
hv = HValue(self.enum)
hv.setTuple(self.enuminfo.type, self.enumtuple)
hv.write()
def update(self, helem):
self.enumtuple = HValue(helem).getTuple(self.enuminfo.type, self.enuminfo.count)
self.eventValue()
class EnumElementPlayback(EnumElement):
def __init__(self, mixer, name, index, weight):
EnumElement.__init__(self, mixer, name, index, weight)
self.mycaps = self.CAP_PENUM
class EnumElementCapture(EnumElement):
def __init__(self, mixer, name, index, weight):
EnumElement.__init__(self, mixer, name, index, weight)
self.mycaps = self.CAP_CENUM
ELEMS = []
def element_add(helem):
key = helem.name+'//'+str(helem.index)+'//'+str(helem.interface)
if not CONTROLS.has_key(key):
return
val = CONTROLS[key]
felem = None
for elem in ELEMS:
if elem.name == val[0] and elem.index == val[1]:
felem = elem
break
if not felem:
felem = mixer.newMElement(val[3], val[0], val[1], val[2])
mixer.addMElement(felem)
ELEMS.append(felem)
felem.attach(helem)
def eventHandler(evmask, helem, melem):
if evmask == EventMaskRemove:
return
if evmask & EventMask['Add']:
element_add(helem)
if evmask & EventMask['Value']:
melem.update(helem)
def init():
hctl = HControl(device, load=False)
mixer.attachHCtl(hctl)
mixer.register()

View File

@ -0,0 +1,42 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- Python -*-
alsacode('common')
CONTROLS = {
'Headphone Playback Switch//0//'+MIXERS:["Headphone", 0, 1, "StandardElement"],
'IEC958 Playback Switch//0//'+MIXERS:["IEC958", 0, 2, "StandardElement"],
'Front Playback Volume//0//'+MIXERS:["Front", 0, 3, "StandardElement"],
'Front Playback Switch//0//'+MIXERS:["Front", 0, 3, "StandardElement"],
'Surround Playback Volume//0//'+MIXERS:["Surround", 0, 4, "StandardElement"],
'Surround Playback Switch//0//'+MIXERS:["Surround", 0, 4, "StandardElement"],
'Center Playback Volume//0//'+MIXERS:["Center", 0, 5, "StandardElement"],
'Center Playback Switch//0//'+MIXERS:["Center", 0, 5, "StandardElement"],
'LFE Playback Volume//0//'+MIXERS:["LFE", 0, 6, "StandardElement"],
'LFE Playback Switch//0//'+MIXERS:["LFE", 0, 6, "StandardElement"],
'Line Playback Volume//0//'+MIXERS:["Line", 0, 7, "StandardElement"],
'Line Playback Switch//0//'+MIXERS:["Line", 0, 7, "StandardElement"],
'CD Playback Volume//0//'+MIXERS:["CD", 0, 8, "StandardElement"],
'CD Playback Switch//0//'+MIXERS:["CD", 0, 8, "StandardElement"],
'Mic Playback Volume//0//'+MIXERS:["Mic", 0, 9, "StandardElement"],
'Mic Playback Switch//0//'+MIXERS:["Mic", 0, 9, "StandardElement"],
'PC Speaker Playback Volume//0//'+MIXERS:["PC Speaker", 0, 10, "StandardElement"],
'PC Speaker Playback Switch//0//'+MIXERS:["PC Speaker", 0, 10, "StandardElement"],
'Front Mic Playback Volume//0//'+MIXERS:["Front Mic", 0, 11, "StandardElement"],
'Front Mic Playback Switch//0//'+MIXERS:["Front Mic", 0, 11, "StandardElement"],
'Capture Switch//0//'+MIXERS:["Capture", 0, 12, "StandardElement"],
'Capture Volume//0//'+MIXERS:["Capture", 0, 12, "StandardElement"],
'Capture Switch//1//'+MIXERS:["Capture", 1, 13, "StandardElement"],
'Capture Volume//1//'+MIXERS:["Capture", 1, 13, "StandardElement"],
'Capture Switch//2//'+MIXERS:["Capture", 2, 14, "StandardElement"],
'Capture Volume//2//'+MIXERS:["Capture", 2, 14, "StandardElement"],
'Input Source//0//'+MIXERS:["Input Source", 0, 15, "EnumElementCapture"],
'Input Source//1//'+MIXERS:["Input Source", 1, 16, "EnumElementCapture"],
'Input Source//2//'+MIXERS:["Input Source", 2, 17, "EnumElementCapture"],
}
def event(evmask, helem, melem):
return eventHandler(evmask, helem, melem)
init()

View File

@ -0,0 +1,24 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# -*- Python -*-
from os.path import dirname
from pyalsa.alsacontrol import Control
from sys import path
path.insert(0, dirname(__file__))
def alsacode(module):
execfile(dirname(__file__)+'/'+module+'.py', globals())
ctl = Control(device)
info = ctl.cardInfo()
#mixername = info['mixername']
components = info['components']
del ctl
if components.find('HDA:') >= 0:
module = 'hda'
else:
raise ValueError, "Mixer for this hardware is not implemented in python"
alsacode(module)

View File

@ -1,3 +1,4 @@
_full smixer-python.so
usb {
searchl "USB"
lib smixer-usb.so

View File

@ -38,7 +38,6 @@
#include <math.h>
#include "mixer_local.h"
#include "mixer_simple.h"
#include "alisp.h"
/**
* \brief Register mixer simple element class

View File

@ -55,6 +55,9 @@ typedef struct _class_priv {
} class_priv_t;
typedef int (*snd_mixer_sbasic_init_t)(snd_mixer_class_t *class);
typedef int (*snd_mixer_sfbasic_init_t)(snd_mixer_class_t *class,
snd_mixer_t *mixer,
const char *device);
#endif /* !DOC_HIDDEN */
@ -62,10 +65,10 @@ static int try_open(snd_mixer_class_t *class, const char *lib)
{
class_priv_t *priv = snd_mixer_class_get_private(class);
snd_mixer_event_t event_func;
snd_mixer_sbasic_init_t init_func;
snd_mixer_sbasic_init_t init_func = NULL;
char *xlib, *path;
void *h;
int err;
int err = 0;
path = getenv("ALSA_MIXER_SIMPLE_MODULES");
if (!path)
@ -82,28 +85,71 @@ static int try_open(snd_mixer_class_t *class, const char *lib)
free(xlib);
return -ENXIO;
}
priv->dlhandle = h;
event_func = snd_dlsym(h, "alsa_mixer_simple_event", NULL);
if (event_func == NULL) {
SNDERR("Symbol 'alsa_mixer_simple_event' was not found in '%s'", xlib);
snd_dlclose(h);
free(xlib);
return -ENXIO;
err = -ENXIO;
}
init_func = snd_dlsym(h, "alsa_mixer_simple_init", NULL);
if (init_func == NULL) {
SNDERR("Symbol 'alsa_mixer_simple_init' was not found in '%s'", xlib);
snd_dlclose(h);
free(xlib);
return -ENXIO;
if (err == 0) {
init_func = snd_dlsym(h, "alsa_mixer_simple_init", NULL);
if (init_func == NULL) {
SNDERR("Symbol 'alsa_mixer_simple_init' was not found in '%s'", xlib);
err = -ENXIO;
}
}
free(xlib);
err = init_func(class);
if (err < 0) {
snd_dlclose(h);
err = err == 0 ? init_func(class) : err;
if (err < 0)
return err;
}
snd_mixer_class_set_event(class, event_func);
return 1;
}
static int try_open_full(snd_mixer_class_t *class, snd_mixer_t *mixer,
const char *lib, const char *device)
{
class_priv_t *priv = snd_mixer_class_get_private(class);
snd_mixer_event_t event_func;
snd_mixer_sfbasic_init_t init_func = NULL;
char *xlib, *path;
void *h;
int err = 0;
path = getenv("ALSA_MIXER_SIMPLE_MODULES");
if (!path)
path = SO_PATH;
xlib = malloc(strlen(lib) + strlen(path) + 1 + 1);
if (xlib == NULL)
return -ENOMEM;
strcpy(xlib, path);
strcat(xlib, "/");
strcat(xlib, lib);
/* note python modules requires RTLD_GLOBAL */
h = snd_dlopen(xlib, RTLD_NOW|RTLD_GLOBAL);
if (h == NULL) {
SNDERR("Unable to open library '%s'", xlib);
free(xlib);
return -ENXIO;
}
priv->dlhandle = h;
event_func = snd_dlsym(h, "alsa_mixer_simple_event", NULL);
if (event_func == NULL) {
SNDERR("Symbol 'alsa_mixer_simple_event' was not found in '%s'", xlib);
err = -ENXIO;
}
if (err == 0) {
init_func = snd_dlsym(h, "alsa_mixer_simple_finit", NULL);
if (init_func == NULL) {
SNDERR("Symbol 'alsa_mixer_simple_finit' was not found in '%s'", xlib);
err = -ENXIO;
}
}
free(xlib);
err = err == 0 ? init_func(class, mixer, device) : err;
if (err < 0)
return err;
snd_mixer_class_set_event(class, event_func);
return 1;
}
@ -126,6 +172,31 @@ static int match(snd_mixer_class_t *class, const char *lib, const char *searchl)
return 0;
}
static int find_full(snd_mixer_class_t *class, snd_mixer_t *mixer,
snd_config_t *top, const char *device)
{
snd_config_iterator_t i, next;
char *lib;
const char *id;
int err;
snd_config_for_each(i, next, top) {
snd_config_t *n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
if (strcmp(id, "_full"))
continue;
err = snd_config_get_string(n, (const char **)&lib);
if (err < 0)
return err;
err = try_open_full(class, mixer, lib, device);
if (err < 0)
return err;
return 0;
}
return -ENOENT;
}
static int find_module(snd_mixer_class_t *class, snd_config_t *top)
{
snd_config_iterator_t i, next;
@ -138,6 +209,8 @@ static int find_module(snd_mixer_class_t *class, snd_config_t *top)
snd_config_t *n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
if (*id == '_')
continue;
searchl = NULL;
lib = NULL;
snd_config_for_each(j, jnext, n) {
@ -223,20 +296,6 @@ int snd_mixer_simple_basic_register(snd_mixer_t *mixer,
snd_mixer_class_set_compare(class, snd_mixer_selem_compare);
snd_mixer_class_set_private(class, priv);
snd_mixer_class_set_private_free(class, private_free);
err = snd_ctl_open(&priv->ctl, priv->device, 0);
if (err < 0) {
SNDERR("unable to open control device '%s': %s", priv->device, snd_strerror(err));
goto __error;
}
err = snd_hctl_open_ctl(&priv->hctl, priv->ctl);
if (err < 0)
goto __error;
err = snd_ctl_card_info_malloc(&priv->info);
if (err < 0)
goto __error;
err = snd_ctl_card_info(priv->ctl, priv->info);
if (err < 0)
goto __error;
file = getenv("ALSA_MIXER_SIMPLE");
if (!file)
file = ALSA_CONFIG_DIR "/smixer.conf";
@ -253,16 +312,35 @@ int snd_mixer_simple_basic_register(snd_mixer_t *mixer,
SNDERR("%s may be old or corrupted: consider to remove or fix it", file);
goto __error;
}
err = find_module(class, top);
snd_config_delete(top);
top = NULL;
err = find_full(class, mixer, top, priv->device);
if (err >= 0)
goto __full;
}
if (err >= 0) {
err = snd_ctl_open(&priv->ctl, priv->device, 0);
if (err < 0) {
SNDERR("unable to open control device '%s': %s", priv->device, snd_strerror(err));
goto __error;
}
err = snd_hctl_open_ctl(&priv->hctl, priv->ctl);
if (err < 0)
goto __error;
err = snd_ctl_card_info_malloc(&priv->info);
if (err < 0)
goto __error;
err = snd_ctl_card_info(priv->ctl, priv->info);
if (err < 0)
goto __error;
}
if (err >= 0)
err = find_module(class, top);
if (err >= 0)
err = snd_mixer_attach_hctl(mixer, priv->hctl);
if (err >= 0) {
priv->attach_flag = 1;
err = snd_mixer_class_register(class, mixer);
}
__full:
if (err < 0) {
__error:
if (top)