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_mute(ChannelObject *self, void *closure)
240 if (channel_is_muted(self->channel)) {
250 Channel_get_out_mute(ChannelObject *self, void *closure)
254 if (channel_is_out_muted(self->channel)) {
264 Channel_set_mute(ChannelObject *self, PyObject *value, void *closure)
266 if (value == Py_True) {
267 channel_mute(self->channel);
269 channel_unmute(self->channel);
275 Channel_set_out_mute(ChannelObject *self, PyObject *value, void *closure)
277 if (value == Py_True) {
278 channel_out_mute(self->channel);
280 channel_out_unmute(self->channel);
286 Channel_get_solo(ChannelObject *self, void *closure)
290 if (channel_is_soloed(self->channel)) {
300 Channel_set_solo(ChannelObject *self, PyObject *value, void *closure)
302 if (value == Py_True) {
303 channel_solo(self->channel);
305 channel_unsolo(self->channel);
311 Channel_get_meter(ChannelObject *self, void *closure)
316 if (channel_is_stereo(self->channel)) {
317 result = PyTuple_New(2);
318 channel_stereo_meter_read(self->channel, &left, &right);
319 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
320 PyTuple_SetItem(result, 1, PyFloat_FromDouble(right));
322 result = PyTuple_New(1);
323 channel_mono_meter_read(self->channel, &left);
324 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
330 Channel_get_abspeak(ChannelObject *self, void *closure)
332 return PyFloat_FromDouble(channel_abspeak_read(self->channel));
336 Channel_set_abspeak(ChannelObject *self, PyObject *value, void *closure)
338 if (value != Py_None) {
339 fprintf(stderr, "abspeak can only be reset (set to None)\n");
342 channel_abspeak_reset(self->channel);
347 Channel_set_midi_scale(ChannelObject *self, PyObject *value, void *closure)
349 ScaleObject *scale_object = (ScaleObject*)value; /* XXX: check */
351 channel_set_midi_scale(self->channel, scale_object->scale);
357 Channel_get_midi_change_callback(ChannelObject *self, void *closure)
359 if (self->midi_change_callback) {
360 Py_INCREF(self->midi_change_callback);
361 return self->midi_change_callback;
369 channel_midi_callback(void *userdata)
371 ChannelObject *self = (ChannelObject*)userdata;
372 PyGILState_STATE gstate;
374 gstate = PyGILState_Ensure();
375 PyObject_CallObject(self->midi_change_callback, NULL);
376 PyGILState_Release(gstate);
380 Channel_set_midi_change_callback(ChannelObject *self, PyObject *value, void *closure)
382 if (value == Py_None) {
383 self->midi_change_callback = NULL;
384 channel_set_midi_change_callback(self->channel, NULL, NULL);
386 if (!PyCallable_Check(value)) {
387 PyErr_SetString(PyExc_TypeError, "value must be callable");
390 if (self->midi_change_callback) {
391 Py_XDECREF(self->midi_change_callback);
394 self->midi_change_callback = value;
395 channel_set_midi_change_callback(self->channel,
396 channel_midi_callback, self);
403 Channel_get_name(ChannelObject *self, void *closure)
405 return PyString_FromString(channel_get_name(self->channel));
409 Channel_set_name(ChannelObject *self, PyObject *value, void *closure)
411 channel_rename(self->channel, PyString_AsString(value));
416 Channel_get_balance_midi_cc(ChannelObject *self, void *closure)
418 return PyInt_FromLong(channel_get_balance_midi_cc(self->channel));
422 Channel_set_balance_midi_cc(ChannelObject *self, PyObject *value, void *closure)
427 new_cc = PyInt_AsLong(value);
428 result = channel_set_balance_midi_cc(self->channel, new_cc);
433 PyErr_SetString(PyExc_RuntimeError, "value already in use");
434 } else if (result == 2) {
435 PyErr_SetString(PyExc_RuntimeError, "value out of range");
441 Channel_get_volume_midi_cc(ChannelObject *self, void *closure)
443 return PyInt_FromLong(channel_get_volume_midi_cc(self->channel));
447 Channel_set_volume_midi_cc(ChannelObject *self, PyObject *value, void *closure)
452 new_cc = PyInt_AsLong(value);
453 result = channel_set_volume_midi_cc(self->channel, new_cc);
458 PyErr_SetString(PyExc_RuntimeError, "value already in use");
459 } else if (result == 2) {
460 PyErr_SetString(PyExc_RuntimeError, "value out of range");
466 Channel_get_midi_in_got_events(ChannelObject *self, void *closure)
470 if (channel_get_midi_in_got_events(self->channel)) {
480 static PyGetSetDef Channel_getseters[] = {
482 (getter)Channel_get_is_stereo, NULL,
483 "mono/stereo", NULL},
485 (getter)Channel_get_volume, (setter)Channel_set_volume,
488 (getter)Channel_get_balance, (setter)Channel_set_balance,
491 (getter)Channel_get_mute, (setter)Channel_set_mute,
494 (getter)Channel_get_out_mute, (setter)Channel_set_out_mute,
497 (getter)Channel_get_solo, (setter)Channel_set_solo,
500 (getter)Channel_get_meter, NULL,
503 (getter)Channel_get_abspeak, (setter)Channel_set_abspeak,
506 NULL, (setter)Channel_set_midi_scale,
508 {"midi_change_callback",
509 (getter)Channel_get_midi_change_callback,
510 (setter)Channel_set_midi_change_callback,
511 "midi change callback", NULL},
513 (getter)Channel_get_name,
514 (setter)Channel_set_name,
517 (getter)Channel_get_balance_midi_cc,
518 (setter)Channel_set_balance_midi_cc,
519 "Balance MIDI CC", NULL},
521 (getter)Channel_get_volume_midi_cc,
522 (setter)Channel_set_volume_midi_cc,
523 "Volume MIDI CC", NULL},
524 {"midi_in_got_events",
525 (getter)Channel_get_midi_in_got_events, NULL,
526 "Got new MIDI IN events", NULL},
531 Channel_remove(ChannelObject *self, PyObject *args)
533 if (! PyArg_ParseTuple(args, "")) return NULL;
534 remove_channel(self->channel);
540 Channel_autoset_midi_cc(ChannelObject *self, PyObject *args)
542 if (! PyArg_ParseTuple(args, "")) return NULL;
543 channel_autoset_midi_cc(self->channel);
548 static PyMethodDef channel_methods[] = {
549 {"remove", (PyCFunction)Channel_remove, METH_VARARGS, "Remove"},
550 {"autoset_midi_cc", (PyCFunction)Channel_autoset_midi_cc, METH_VARARGS, "Autoset MIDI CC"},
554 static PyTypeObject ChannelType = {
555 PyObject_HEAD_INIT(NULL)
557 "jack_mixer_c.Channel", /*tp_name*/
558 sizeof(ChannelObject), /*tp_basicsize*/
560 (destructor)Channel_dealloc, /*tp_dealloc*/
567 0, /*tp_as_sequence*/
575 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
576 "Channel objects", /* tp_doc */
579 0, /* tp_richcompare */
580 0, /* tp_weaklistoffset */
583 channel_methods, /* tp_methods */
585 Channel_getseters, /* tp_getset */
588 0, /* tp_descr_get */
589 0, /* tp_descr_set */
590 0, /* tp_dictoffset */
591 (initproc)Channel_init, /* tp_init */
593 Channel_new, /* tp_new */
597 Channel_New(jack_mixer_channel_t channel)
600 self = (ChannelObject*)PyObject_NEW(ChannelObject, &ChannelType);
602 self->channel = channel;
603 self->midi_change_callback = NULL;
605 return (PyObject*)self;
608 /** Output Channel Type **/
612 PyObject *midi_change_callback;
613 jack_mixer_output_channel_t *output_channel;
614 } OutputChannelObject;
617 OutputChannel_set_prefader(OutputChannelObject *self, PyObject *value, void *closure)
619 if (value == Py_True) {
620 output_channel_set_prefader(self->output_channel, true);
622 output_channel_set_prefader(self->output_channel, false);
628 OutputChannel_get_prefader(OutputChannelObject *self, void *closure)
632 if (output_channel_is_prefader(self->output_channel)) {
641 static PyGetSetDef OutputChannel_getseters[] = {
643 (getter)OutputChannel_get_prefader, (setter)OutputChannel_set_prefader,
649 OutputChannel_remove(OutputChannelObject *self, PyObject *args)
651 if (! PyArg_ParseTuple(args, "")) return NULL;
652 remove_output_channel(self->output_channel);
658 OutputChannel_set_solo(OutputChannelObject *self, PyObject *args)
663 if (! PyArg_ParseTuple(args, "Ob", &channel, &solo)) return NULL;
665 output_channel_set_solo(self->output_channel,
666 ((ChannelObject*)channel)->channel,
674 OutputChannel_set_muted(OutputChannelObject *self, PyObject *args)
679 if (! PyArg_ParseTuple(args, "Ob", &channel, &muted)) return NULL;
681 output_channel_set_muted(self->output_channel,
682 ((ChannelObject*)channel)->channel,
690 OutputChannel_is_solo(OutputChannelObject *self, PyObject *args)
695 if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
697 if (output_channel_is_solo(self->output_channel,
698 ((ChannelObject*)channel)->channel)) {
709 OutputChannel_is_muted(OutputChannelObject *self, PyObject *args)
714 if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
716 if (output_channel_is_muted(self->output_channel,
717 ((ChannelObject*)channel)->channel)) {
727 static PyMethodDef output_channel_methods[] = {
728 {"remove", (PyCFunction)OutputChannel_remove, METH_VARARGS, "Remove"},
729 {"set_solo", (PyCFunction)OutputChannel_set_solo, METH_VARARGS, "Set a channel as solo"},
730 {"set_muted", (PyCFunction)OutputChannel_set_muted, METH_VARARGS, "Set a channel as muted"},
731 {"is_solo", (PyCFunction)OutputChannel_is_solo, METH_VARARGS, "Is a channel set as solo"},
732 {"is_muted", (PyCFunction)OutputChannel_is_muted, METH_VARARGS, "Is a channel set as muted"},
736 static PyTypeObject OutputChannelType = {
737 PyObject_HEAD_INIT(NULL)
739 "jack_mixer_c.OutputChannel", /*tp_name*/
740 sizeof(OutputChannelObject), /*tp_basicsize*/
749 0, /*tp_as_sequence*/
757 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
758 "Output Channel objects", /* tp_doc */
761 0, /* tp_richcompare */
762 0, /* tp_weaklistoffset */
765 output_channel_methods, /* tp_methods */
767 OutputChannel_getseters, /* tp_getset */
768 &ChannelType, /* tp_base */
770 0, /* tp_descr_get */
771 0, /* tp_descr_set */
772 0, /* tp_dictoffset */
779 OutputChannel_New(jack_mixer_output_channel_t output_channel)
781 OutputChannelObject *self;
782 self = (OutputChannelObject*)PyObject_NEW(OutputChannelObject, &OutputChannelType);
784 self->midi_change_callback = NULL;
785 self->output_channel = output_channel;
787 return (PyObject*)self;
795 PyObject *main_mix_channel;
800 Mixer_dealloc(MixerObject *self)
802 Py_XDECREF(self->main_mix_channel);
804 destroy(self->mixer);
805 self->ob_type->tp_free((PyObject*)self);
809 Mixer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
813 self = (MixerObject*)type->tp_alloc(type, 0);
816 self->main_mix_channel = NULL;
820 return (PyObject*)self;
824 Mixer_init(MixerObject *self, PyObject *args, PyObject *kwds)
826 static char *kwlist[] = {"name", "stereo", NULL};
830 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|b", kwlist, &name, &stereo))
833 self->mixer = create(name, (bool)stereo);
834 if (self->mixer == NULL) {
835 PyErr_SetString(PyExc_RuntimeError,
836 "error creating mixer, probably jack is not running");
840 self->main_mix_channel = Channel_New(get_main_mix_channel(self->mixer));
845 static PyMemberDef Mixer_members[] = {
846 {"main_mix_channel", T_OBJECT, offsetof(MixerObject, main_mix_channel), 0, "main_mix_channel"},
851 Mixer_get_channels_count(MixerObject *self, void *closure)
853 return PyInt_FromLong(get_channels_count(self->mixer));
857 Mixer_get_client_name(MixerObject *self, void *closure)
859 return PyString_FromString(get_client_name(self->mixer));
863 Mixer_get_last_midi_channel(MixerObject *self, void *closure)
865 return PyInt_FromLong(get_last_midi_channel(self->mixer));
869 static PyGetSetDef Mixer_getseters[] = {
870 {"channels_count", (getter)Mixer_get_channels_count, NULL,
871 "channels count", NULL},
872 {"last_midi_channel", (getter)Mixer_get_last_midi_channel, NULL,
873 "last midi channel", NULL},
878 Mixer_add_channel(MixerObject *self, PyObject *args)
882 jack_mixer_channel_t channel;
884 if (! PyArg_ParseTuple(args, "si", &name, &stereo)) return NULL;
886 channel = add_channel(self->mixer, name, (bool)stereo);
888 if (channel == NULL) {
889 PyErr_SetString(PyExc_RuntimeError, "error adding channel");
893 return Channel_New(channel);
897 Mixer_add_output_channel(MixerObject *self, PyObject *args)
902 jack_mixer_output_channel_t channel;
904 if (! PyArg_ParseTuple(args, "s|bb", &name, &stereo, &system)) return NULL;
906 channel = add_output_channel(self->mixer, name, (bool)stereo, (bool)system);
908 return OutputChannel_New(channel);
912 Mixer_destroy(MixerObject *self, PyObject *args)
915 destroy(self->mixer);
922 static PyMethodDef Mixer_methods[] = {
923 {"add_channel", (PyCFunction)Mixer_add_channel, METH_VARARGS, "Add a new channel"},
924 {"add_output_channel", (PyCFunction)Mixer_add_output_channel, METH_VARARGS, "Add a new output channel"},
925 {"destroy", (PyCFunction)Mixer_destroy, METH_VARARGS, "Destroy JACK Mixer"},
926 {"client_name", (PyCFunction)Mixer_get_client_name, METH_VARARGS, "Get jack client name"},
927 // {"remove_channel", (PyCFunction)Mixer_remove_channel, METH_VARARGS, "Remove a channel"},
931 static PyTypeObject MixerType = {
932 PyObject_HEAD_INIT(NULL)
934 "jack_mixer_c.Mixer", /*tp_name*/
935 sizeof(MixerObject), /*tp_basicsize*/
937 (destructor)Mixer_dealloc, /*tp_dealloc*/
944 0, /*tp_as_sequence*/
952 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
953 "Mixer objects", /* tp_doc */
956 0, /* tp_richcompare */
957 0, /* tp_weaklistoffset */
960 Mixer_methods, /* tp_methods */
961 Mixer_members, /* tp_members */
962 Mixer_getseters, /* tp_getset */
965 0, /* tp_descr_get */
966 0, /* tp_descr_set */
967 0, /* tp_dictoffset */
968 (initproc)Mixer_init, /* tp_init */
970 Mixer_new, /* tp_new */
974 static PyMethodDef jack_mixer_methods[] = {
975 {NULL} /* Sentinel */
980 PyMODINIT_FUNC initjack_mixer_c(void)
984 if (PyType_Ready(&MixerType) < 0)
986 if (PyType_Ready(&ChannelType) < 0)
988 if (PyType_Ready(&OutputChannelType) < 0)
990 if (PyType_Ready(&ScaleType) < 0)
993 m = Py_InitModule3("jack_mixer_c", jack_mixer_methods, "Jack Mixer C Helper Module");
995 Py_INCREF(&MixerType);
996 PyModule_AddObject(m, "Mixer", (PyObject*)&MixerType);
997 Py_INCREF(&ChannelType);
998 PyModule_AddObject(m, "Channel", (PyObject*)&ChannelType);
999 Py_INCREF(&OutputChannelType);
1000 PyModule_AddObject(m, "OutputChannel", (PyObject*)&OutputChannelType);
1001 Py_INCREF(&ScaleType);
1002 PyModule_AddObject(m, "Scale", (PyObject*)&ScaleType);