2 * This file is part of jack_mixer
4 * Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <structmember.h>
28 #include "jack_mixer.h"
35 jack_mixer_scale_t scale;
39 Scale_dealloc(ScaleObject *self)
42 scale_destroy(self->scale);
43 self->ob_type->tp_free((PyObject*)self);
47 Scale_init(ScaleObject *self, PyObject *args, PyObject *kwds)
49 self->scale = scale_create();
54 Scale_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
58 self = (ScaleObject*)type->tp_alloc(type, 0);
60 return (PyObject*)self;
64 Scale_add_threshold(ScaleObject *self, PyObject *args)
66 float db, scale_value;
68 if (! PyArg_ParseTuple(args, "ff", &db, &scale_value)) return NULL;
70 scale_add_threshold(self->scale, db, scale_value);
77 Scale_calculate_coefficients(ScaleObject *self, PyObject *args)
79 if (! PyArg_ParseTuple(args, "")) return NULL;
80 scale_calculate_coefficients(self->scale);
86 Scale_db_to_scale(ScaleObject *self, PyObject *args)
89 if (! PyArg_ParseTuple(args, "d", &db)) return NULL;
90 return PyFloat_FromDouble(scale_db_to_scale(self->scale, db));
94 Scale_scale_to_db(ScaleObject *self, PyObject *args)
97 if (! PyArg_ParseTuple(args, "d", &scale_value)) return NULL;
98 return PyFloat_FromDouble(scale_scale_to_db(self->scale, scale_value));
101 static PyMethodDef Scale_methods[] = {
102 {"add_threshold", (PyCFunction)Scale_add_threshold, METH_VARARGS, "Add threshold"},
103 {"calculate_coefficients", (PyCFunction)Scale_calculate_coefficients,
104 METH_VARARGS, "Calculate coefficients"},
105 {"db_to_scale", (PyCFunction)Scale_db_to_scale, METH_VARARGS, "dB to scale"},
106 {"scale_to_db", (PyCFunction)Scale_scale_to_db, METH_VARARGS, "scale to dB"},
110 static PyTypeObject ScaleType = {
111 PyObject_HEAD_INIT(NULL)
113 "jack_mixer_c.Scale", /*tp_name*/
114 sizeof(ScaleObject), /*tp_basicsize*/
116 (destructor)Scale_dealloc, /*tp_dealloc*/
123 0, /*tp_as_sequence*/
131 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
132 "Scale objects", /* tp_doc */
135 0, /* tp_richcompare */
136 0, /* tp_weaklistoffset */
139 Scale_methods, /* tp_methods */
144 0, /* tp_descr_get */
145 0, /* tp_descr_set */
146 0, /* tp_dictoffset */
147 (initproc)Scale_init, /* tp_init */
149 Scale_new, /* tp_new */
157 PyObject *midi_change_callback;
158 jack_mixer_channel_t channel;
162 Channel_dealloc(ChannelObject *self)
164 Py_XDECREF(self->midi_change_callback);
165 self->ob_type->tp_free((PyObject*)self);
169 Channel_init(ChannelObject *self, PyObject *args, PyObject *kwds)
171 self->midi_change_callback = NULL;
176 Channel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
180 self = (ChannelObject*)type->tp_alloc(type, 0);
183 self->channel = NULL;
184 self->midi_change_callback = NULL;
187 return (PyObject*)self;
191 Channel_get_is_stereo(ChannelObject *self, void *closure)
195 bool is_stereo = channel_is_stereo(self->channel);
206 Channel_get_volume(ChannelObject *self, void *closure)
208 return PyFloat_FromDouble(channel_volume_read(self->channel));
212 Channel_set_volume(ChannelObject *self, PyObject *value, void *closure)
214 if (self->channel == NULL) {
215 PyErr_SetString(PyExc_RuntimeError, "unitialized channel");
218 channel_volume_write(self->channel, PyFloat_AsDouble(value));
223 Channel_get_balance(ChannelObject *self, void *closure)
225 return PyFloat_FromDouble(channel_balance_read(self->channel));
229 Channel_set_balance(ChannelObject *self, PyObject *value, void *closure)
231 channel_balance_write(self->channel, PyFloat_AsDouble(value));
236 Channel_get_out_mute(ChannelObject *self, void *closure)
240 if (channel_is_out_muted(self->channel)) {
250 Channel_set_out_mute(ChannelObject *self, PyObject *value, void *closure)
252 if (value == Py_True) {
253 channel_out_mute(self->channel);
255 channel_out_unmute(self->channel);
261 Channel_get_meter(ChannelObject *self, void *closure)
266 if (channel_is_stereo(self->channel)) {
267 result = PyTuple_New(2);
268 channel_stereo_meter_read(self->channel, &left, &right);
269 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
270 PyTuple_SetItem(result, 1, PyFloat_FromDouble(right));
272 result = PyTuple_New(1);
273 channel_mono_meter_read(self->channel, &left);
274 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
280 Channel_get_abspeak(ChannelObject *self, void *closure)
282 return PyFloat_FromDouble(channel_abspeak_read(self->channel));
286 Channel_set_abspeak(ChannelObject *self, PyObject *value, void *closure)
288 if (value != Py_None) {
289 fprintf(stderr, "abspeak can only be reset (set to None)\n");
292 channel_abspeak_reset(self->channel);
297 Channel_set_midi_scale(ChannelObject *self, PyObject *value, void *closure)
299 ScaleObject *scale_object = (ScaleObject*)value; /* XXX: check */
301 channel_set_midi_scale(self->channel, scale_object->scale);
307 Channel_get_midi_change_callback(ChannelObject *self, void *closure)
309 if (self->midi_change_callback) {
310 Py_INCREF(self->midi_change_callback);
311 return self->midi_change_callback;
319 channel_midi_callback(void *userdata)
321 ChannelObject *self = (ChannelObject*)userdata;
322 PyGILState_STATE gstate;
324 gstate = PyGILState_Ensure();
325 PyObject_CallObject(self->midi_change_callback, NULL);
326 PyGILState_Release(gstate);
330 Channel_set_midi_change_callback(ChannelObject *self, PyObject *value, void *closure)
332 if (value == Py_None) {
333 self->midi_change_callback = NULL;
334 channel_set_midi_change_callback(self->channel, NULL, NULL);
336 if (!PyCallable_Check(value)) {
337 PyErr_SetString(PyExc_TypeError, "value must be callable");
340 if (self->midi_change_callback) {
341 Py_XDECREF(self->midi_change_callback);
344 self->midi_change_callback = value;
345 channel_set_midi_change_callback(self->channel,
346 channel_midi_callback, self);
353 Channel_get_name(ChannelObject *self, void *closure)
355 return PyString_FromString(channel_get_name(self->channel));
359 Channel_set_name(ChannelObject *self, PyObject *value, void *closure)
361 channel_rename(self->channel, PyString_AsString(value));
366 Channel_get_balance_midi_cc(ChannelObject *self, void *closure)
368 return PyInt_FromLong(channel_get_balance_midi_cc(self->channel));
372 Channel_set_balance_midi_cc(ChannelObject *self, PyObject *value, void *closure)
377 new_cc = PyInt_AsLong(value);
378 result = channel_set_balance_midi_cc(self->channel, new_cc);
383 PyErr_SetString(PyExc_RuntimeError, "value already in use");
384 } else if (result == 2) {
385 PyErr_SetString(PyExc_RuntimeError, "value out of range");
391 Channel_get_volume_midi_cc(ChannelObject *self, void *closure)
393 return PyInt_FromLong(channel_get_volume_midi_cc(self->channel));
397 Channel_set_volume_midi_cc(ChannelObject *self, PyObject *value, void *closure)
402 new_cc = PyInt_AsLong(value);
403 result = channel_set_volume_midi_cc(self->channel, new_cc);
408 PyErr_SetString(PyExc_RuntimeError, "value already in use");
409 } else if (result == 2) {
410 PyErr_SetString(PyExc_RuntimeError, "value out of range");
416 Channel_get_midi_in_got_events(ChannelObject *self, void *closure)
420 if (channel_get_midi_in_got_events(self->channel)) {
430 static PyGetSetDef Channel_getseters[] = {
432 (getter)Channel_get_is_stereo, NULL,
433 "mono/stereo", NULL},
435 (getter)Channel_get_volume, (setter)Channel_set_volume,
438 (getter)Channel_get_balance, (setter)Channel_set_balance,
441 (getter)Channel_get_out_mute, (setter)Channel_set_out_mute,
444 (getter)Channel_get_meter, NULL,
447 (getter)Channel_get_abspeak, (setter)Channel_set_abspeak,
450 NULL, (setter)Channel_set_midi_scale,
452 {"midi_change_callback",
453 (getter)Channel_get_midi_change_callback,
454 (setter)Channel_set_midi_change_callback,
455 "midi change callback", NULL},
457 (getter)Channel_get_name,
458 (setter)Channel_set_name,
461 (getter)Channel_get_balance_midi_cc,
462 (setter)Channel_set_balance_midi_cc,
463 "Balance MIDI CC", NULL},
465 (getter)Channel_get_volume_midi_cc,
466 (setter)Channel_set_volume_midi_cc,
467 "Volume MIDI CC", NULL},
468 {"midi_in_got_events",
469 (getter)Channel_get_midi_in_got_events, NULL,
470 "Got new MIDI IN events", NULL},
475 Channel_remove(ChannelObject *self, PyObject *args)
477 if (! PyArg_ParseTuple(args, "")) return NULL;
478 remove_channel(self->channel);
484 Channel_autoset_midi_cc(ChannelObject *self, PyObject *args)
486 if (! PyArg_ParseTuple(args, "")) return NULL;
487 channel_autoset_midi_cc(self->channel);
492 static PyMethodDef channel_methods[] = {
493 {"remove", (PyCFunction)Channel_remove, METH_VARARGS, "Remove"},
494 {"autoset_midi_cc", (PyCFunction)Channel_autoset_midi_cc, METH_VARARGS, "Autoset MIDI CC"},
498 static PyTypeObject ChannelType = {
499 PyObject_HEAD_INIT(NULL)
501 "jack_mixer_c.Channel", /*tp_name*/
502 sizeof(ChannelObject), /*tp_basicsize*/
504 (destructor)Channel_dealloc, /*tp_dealloc*/
511 0, /*tp_as_sequence*/
519 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
520 "Channel objects", /* tp_doc */
523 0, /* tp_richcompare */
524 0, /* tp_weaklistoffset */
527 channel_methods, /* tp_methods */
529 Channel_getseters, /* tp_getset */
532 0, /* tp_descr_get */
533 0, /* tp_descr_set */
534 0, /* tp_dictoffset */
535 (initproc)Channel_init, /* tp_init */
537 Channel_new, /* tp_new */
541 Channel_New(jack_mixer_channel_t channel)
544 self = (ChannelObject*)PyObject_NEW(ChannelObject, &ChannelType);
546 self->channel = channel;
547 self->midi_change_callback = NULL;
549 return (PyObject*)self;
552 /** Output Channel Type **/
556 PyObject *midi_change_callback;
557 jack_mixer_output_channel_t *output_channel;
558 } OutputChannelObject;
561 OutputChannel_set_prefader(OutputChannelObject *self, PyObject *value, void *closure)
563 if (value == Py_True) {
564 output_channel_set_prefader(self->output_channel, true);
566 output_channel_set_prefader(self->output_channel, false);
572 OutputChannel_get_prefader(OutputChannelObject *self, void *closure)
576 if (output_channel_is_prefader(self->output_channel)) {
585 static PyGetSetDef OutputChannel_getseters[] = {
587 (getter)OutputChannel_get_prefader, (setter)OutputChannel_set_prefader,
593 OutputChannel_remove(OutputChannelObject *self, PyObject *args)
595 if (! PyArg_ParseTuple(args, "")) return NULL;
596 remove_output_channel(self->output_channel);
602 OutputChannel_set_solo(OutputChannelObject *self, PyObject *args)
607 if (! PyArg_ParseTuple(args, "Ob", &channel, &solo)) return NULL;
609 output_channel_set_solo(self->output_channel,
610 ((ChannelObject*)channel)->channel,
618 OutputChannel_set_muted(OutputChannelObject *self, PyObject *args)
623 if (! PyArg_ParseTuple(args, "Ob", &channel, &muted)) return NULL;
625 output_channel_set_muted(self->output_channel,
626 ((ChannelObject*)channel)->channel,
634 OutputChannel_is_solo(OutputChannelObject *self, PyObject *args)
639 if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
641 if (output_channel_is_solo(self->output_channel,
642 ((ChannelObject*)channel)->channel)) {
653 OutputChannel_is_muted(OutputChannelObject *self, PyObject *args)
658 if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
660 if (output_channel_is_muted(self->output_channel,
661 ((ChannelObject*)channel)->channel)) {
671 static PyMethodDef output_channel_methods[] = {
672 {"remove", (PyCFunction)OutputChannel_remove, METH_VARARGS, "Remove"},
673 {"set_solo", (PyCFunction)OutputChannel_set_solo, METH_VARARGS, "Set a channel as solo"},
674 {"set_muted", (PyCFunction)OutputChannel_set_muted, METH_VARARGS, "Set a channel as muted"},
675 {"is_solo", (PyCFunction)OutputChannel_is_solo, METH_VARARGS, "Is a channel set as solo"},
676 {"is_muted", (PyCFunction)OutputChannel_is_muted, METH_VARARGS, "Is a channel set as muted"},
680 static PyTypeObject OutputChannelType = {
681 PyObject_HEAD_INIT(NULL)
683 "jack_mixer_c.OutputChannel", /*tp_name*/
684 sizeof(OutputChannelObject), /*tp_basicsize*/
693 0, /*tp_as_sequence*/
701 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
702 "Output Channel objects", /* tp_doc */
705 0, /* tp_richcompare */
706 0, /* tp_weaklistoffset */
709 output_channel_methods, /* tp_methods */
711 OutputChannel_getseters, /* tp_getset */
712 &ChannelType, /* tp_base */
714 0, /* tp_descr_get */
715 0, /* tp_descr_set */
716 0, /* tp_dictoffset */
723 OutputChannel_New(jack_mixer_output_channel_t output_channel)
725 OutputChannelObject *self;
726 self = (OutputChannelObject*)PyObject_NEW(OutputChannelObject, &OutputChannelType);
728 self->midi_change_callback = NULL;
729 self->output_channel = output_channel;
731 return (PyObject*)self;
743 Mixer_dealloc(MixerObject *self)
746 destroy(self->mixer);
747 self->ob_type->tp_free((PyObject*)self);
751 Mixer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
755 self = (MixerObject*)type->tp_alloc(type, 0);
761 return (PyObject*)self;
765 Mixer_init(MixerObject *self, PyObject *args, PyObject *kwds)
767 static char *kwlist[] = {"name", "stereo", NULL};
771 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|b", kwlist, &name, &stereo))
774 self->mixer = create(name, (bool)stereo);
775 if (self->mixer == NULL) {
776 PyErr_SetString(PyExc_RuntimeError,
777 "error creating mixer, probably jack is not running");
784 static PyMemberDef Mixer_members[] = {
789 Mixer_get_channels_count(MixerObject *self, void *closure)
791 return PyInt_FromLong(get_channels_count(self->mixer));
795 Mixer_get_client_name(MixerObject *self, void *closure)
797 return PyString_FromString(get_client_name(self->mixer));
801 Mixer_get_last_midi_channel(MixerObject *self, void *closure)
803 return PyInt_FromLong(get_last_midi_channel(self->mixer));
807 static PyGetSetDef Mixer_getseters[] = {
808 {"channels_count", (getter)Mixer_get_channels_count, NULL,
809 "channels count", NULL},
810 {"last_midi_channel", (getter)Mixer_get_last_midi_channel, NULL,
811 "last midi channel", NULL},
816 Mixer_add_channel(MixerObject *self, PyObject *args)
820 jack_mixer_channel_t channel;
822 if (! PyArg_ParseTuple(args, "si", &name, &stereo)) return NULL;
824 channel = add_channel(self->mixer, name, (bool)stereo);
826 if (channel == NULL) {
827 PyErr_SetString(PyExc_RuntimeError, "error adding channel");
831 return Channel_New(channel);
835 Mixer_add_output_channel(MixerObject *self, PyObject *args)
840 jack_mixer_output_channel_t channel;
842 if (! PyArg_ParseTuple(args, "s|bb", &name, &stereo, &system)) return NULL;
844 channel = add_output_channel(self->mixer, name, (bool)stereo, (bool)system);
846 return OutputChannel_New(channel);
850 Mixer_destroy(MixerObject *self, PyObject *args)
853 destroy(self->mixer);
860 static PyMethodDef Mixer_methods[] = {
861 {"add_channel", (PyCFunction)Mixer_add_channel, METH_VARARGS, "Add a new channel"},
862 {"add_output_channel", (PyCFunction)Mixer_add_output_channel, METH_VARARGS, "Add a new output channel"},
863 {"destroy", (PyCFunction)Mixer_destroy, METH_VARARGS, "Destroy JACK Mixer"},
864 {"client_name", (PyCFunction)Mixer_get_client_name, METH_VARARGS, "Get jack client name"},
865 // {"remove_channel", (PyCFunction)Mixer_remove_channel, METH_VARARGS, "Remove a channel"},
869 static PyTypeObject MixerType = {
870 PyObject_HEAD_INIT(NULL)
872 "jack_mixer_c.Mixer", /*tp_name*/
873 sizeof(MixerObject), /*tp_basicsize*/
875 (destructor)Mixer_dealloc, /*tp_dealloc*/
882 0, /*tp_as_sequence*/
890 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
891 "Mixer objects", /* tp_doc */
894 0, /* tp_richcompare */
895 0, /* tp_weaklistoffset */
898 Mixer_methods, /* tp_methods */
899 Mixer_members, /* tp_members */
900 Mixer_getseters, /* tp_getset */
903 0, /* tp_descr_get */
904 0, /* tp_descr_set */
905 0, /* tp_dictoffset */
906 (initproc)Mixer_init, /* tp_init */
908 Mixer_new, /* tp_new */
912 static PyMethodDef jack_mixer_methods[] = {
913 {NULL} /* Sentinel */
918 PyMODINIT_FUNC initjack_mixer_c(void)
922 if (PyType_Ready(&MixerType) < 0)
924 if (PyType_Ready(&ChannelType) < 0)
926 if (PyType_Ready(&OutputChannelType) < 0)
928 if (PyType_Ready(&ScaleType) < 0)
931 m = Py_InitModule3("jack_mixer_c", jack_mixer_methods, "Jack Mixer C Helper Module");
933 Py_INCREF(&MixerType);
934 PyModule_AddObject(m, "Mixer", (PyObject*)&MixerType);
935 Py_INCREF(&ChannelType);
936 PyModule_AddObject(m, "Channel", (PyObject*)&ChannelType);
937 Py_INCREF(&OutputChannelType);
938 PyModule_AddObject(m, "OutputChannel", (PyObject*)&OutputChannelType);
939 Py_INCREF(&ScaleType);
940 PyModule_AddObject(m, "Scale", (PyObject*)&ScaleType);