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 Py_TYPE(self
)->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 PyVarObject_HEAD_INIT(NULL
, 0)
112 "jack_mixer_c.Scale", /*tp_name*/
113 sizeof(ScaleObject
), /*tp_basicsize*/
115 (destructor
)Scale_dealloc
, /*tp_dealloc*/
122 0, /*tp_as_sequence*/
130 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
131 "Scale objects", /* tp_doc */
134 0, /* tp_richcompare */
135 0, /* tp_weaklistoffset */
138 Scale_methods
, /* tp_methods */
143 0, /* tp_descr_get */
144 0, /* tp_descr_set */
145 0, /* tp_dictoffset */
146 (initproc
)Scale_init
, /* tp_init */
148 Scale_new
, /* tp_new */
156 PyObject
*midi_change_callback
;
157 jack_mixer_channel_t channel
;
161 Channel_dealloc(ChannelObject
*self
)
163 Py_XDECREF(self
->midi_change_callback
);
164 Py_TYPE(self
)->tp_free((PyObject
*)self
);
168 Channel_init(ChannelObject
*self
, PyObject
*args
, PyObject
*kwds
)
170 self
->midi_change_callback
= NULL
;
175 Channel_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
179 self
= (ChannelObject
*)type
->tp_alloc(type
, 0);
182 self
->channel
= NULL
;
183 self
->midi_change_callback
= NULL
;
186 return (PyObject
*)self
;
190 Channel_get_is_stereo(ChannelObject
*self
, void *closure
)
194 bool is_stereo
= channel_is_stereo(self
->channel
);
205 Channel_get_volume(ChannelObject
*self
, void *closure
)
207 return PyFloat_FromDouble(channel_volume_read(self
->channel
));
211 Channel_set_volume(ChannelObject
*self
, PyObject
*value
, void *closure
)
213 if (self
->channel
== NULL
) {
214 PyErr_SetString(PyExc_RuntimeError
, "unitialized channel");
217 channel_volume_write(self
->channel
, PyFloat_AsDouble(value
));
222 Channel_get_balance(ChannelObject
*self
, void *closure
)
224 return PyFloat_FromDouble(channel_balance_read(self
->channel
));
228 Channel_set_balance(ChannelObject
*self
, PyObject
*value
, void *closure
)
230 channel_balance_write(self
->channel
, PyFloat_AsDouble(value
));
235 Channel_get_out_mute(ChannelObject
*self
, void *closure
)
239 if (channel_is_out_muted(self
->channel
)) {
249 Channel_set_out_mute(ChannelObject
*self
, PyObject
*value
, void *closure
)
251 if (value
== Py_True
) {
252 channel_out_mute(self
->channel
);
254 channel_out_unmute(self
->channel
);
260 Channel_get_solo(ChannelObject
*self
, void *closure
)
264 if (channel_is_soloed(self
->channel
)) {
274 Channel_set_solo(ChannelObject
*self
, PyObject
*value
, void *closure
)
276 if (value
== Py_True
) {
277 channel_solo(self
->channel
);
279 channel_unsolo(self
->channel
);
285 Channel_get_meter(ChannelObject
*self
, void *closure
)
290 if (channel_is_stereo(self
->channel
)) {
291 result
= PyTuple_New(2);
292 channel_stereo_meter_read(self
->channel
, &left
, &right
);
293 PyTuple_SetItem(result
, 0, PyFloat_FromDouble(left
));
294 PyTuple_SetItem(result
, 1, PyFloat_FromDouble(right
));
296 result
= PyTuple_New(1);
297 channel_mono_meter_read(self
->channel
, &left
);
298 PyTuple_SetItem(result
, 0, PyFloat_FromDouble(left
));
304 Channel_get_abspeak(ChannelObject
*self
, void *closure
)
306 return PyFloat_FromDouble(channel_abspeak_read(self
->channel
));
310 Channel_set_abspeak(ChannelObject
*self
, PyObject
*value
, void *closure
)
312 if (value
!= Py_None
) {
313 fprintf(stderr
, "abspeak can only be reset (set to None)\n");
316 channel_abspeak_reset(self
->channel
);
321 Channel_set_midi_scale(ChannelObject
*self
, PyObject
*value
, void *closure
)
323 ScaleObject
*scale_object
= (ScaleObject
*)value
; /* XXX: check */
325 channel_set_midi_scale(self
->channel
, scale_object
->scale
);
331 Channel_get_midi_change_callback(ChannelObject
*self
, void *closure
)
333 if (self
->midi_change_callback
) {
334 Py_INCREF(self
->midi_change_callback
);
335 return self
->midi_change_callback
;
343 channel_midi_callback(void *userdata
)
345 ChannelObject
*self
= (ChannelObject
*)userdata
;
346 PyGILState_STATE gstate
;
348 gstate
= PyGILState_Ensure();
349 PyObject_CallObject(self
->midi_change_callback
, NULL
);
350 PyGILState_Release(gstate
);
354 Channel_set_midi_change_callback(ChannelObject
*self
, PyObject
*value
, void *closure
)
356 if (value
== Py_None
) {
357 self
->midi_change_callback
= NULL
;
358 channel_set_midi_change_callback(self
->channel
, NULL
, NULL
);
360 if (!PyCallable_Check(value
)) {
361 PyErr_SetString(PyExc_TypeError
, "value must be callable");
364 if (self
->midi_change_callback
) {
365 Py_XDECREF(self
->midi_change_callback
);
368 self
->midi_change_callback
= value
;
369 channel_set_midi_change_callback(self
->channel
,
370 channel_midi_callback
, self
);
377 Channel_get_name(ChannelObject
*self
, void *closure
)
379 return PyUnicode_FromString(channel_get_name(self
->channel
));
383 Channel_set_name(ChannelObject
*self
, PyObject
*value
, void *closure
)
385 channel_rename(self
->channel
, PyUnicode_AsUTF8(value
));
390 Channel_get_balance_midi_cc(ChannelObject
*self
, void *closure
)
392 return PyLong_FromLong(channel_get_balance_midi_cc(self
->channel
));
396 Channel_set_balance_midi_cc(ChannelObject
*self
, PyObject
*value
, void *closure
)
401 new_cc
= PyLong_AsLong(value
);
402 result
= channel_set_balance_midi_cc(self
->channel
, new_cc
);
407 PyErr_SetString(PyExc_RuntimeError
, "value out of range");
413 Channel_get_volume_midi_cc(ChannelObject
*self
, void *closure
)
415 return PyLong_FromLong(channel_get_volume_midi_cc(self
->channel
));
419 Channel_set_volume_midi_cc(ChannelObject
*self
, PyObject
*value
, void *closure
)
424 new_cc
= PyLong_AsLong(value
);
425 result
= channel_set_volume_midi_cc(self
->channel
, new_cc
);
430 PyErr_SetString(PyExc_RuntimeError
, "value out of range");
436 Channel_get_mute_midi_cc(ChannelObject
*self
, void *closure
)
438 return PyLong_FromLong(channel_get_mute_midi_cc(self
->channel
));
442 Channel_set_mute_midi_cc(ChannelObject
*self
, PyObject
*value
, void *closure
)
447 new_cc
= PyLong_AsLong(value
);
448 result
= channel_set_mute_midi_cc(self
->channel
, new_cc
);
453 PyErr_SetString(PyExc_RuntimeError
, "value out of range");
459 Channel_get_solo_midi_cc(ChannelObject
*self
, void *closure
)
461 return PyLong_FromLong(channel_get_solo_midi_cc(self
->channel
));
465 Channel_set_solo_midi_cc(ChannelObject
*self
, PyObject
*value
, void *closure
)
470 new_cc
= PyLong_AsLong(value
);
471 result
= channel_set_solo_midi_cc(self
->channel
, new_cc
);
476 PyErr_SetString(PyExc_RuntimeError
, "value out of range");
482 Channel_get_midi_in_got_events(ChannelObject
*self
, void *closure
)
486 if (channel_get_midi_in_got_events(self
->channel
)) {
495 static PyGetSetDef Channel_getseters
[] = {
497 (getter
)Channel_get_is_stereo
, NULL
,
498 "mono/stereo", NULL
},
500 (getter
)Channel_get_volume
, (setter
)Channel_set_volume
,
503 (getter
)Channel_get_balance
, (setter
)Channel_set_balance
,
506 (getter
)Channel_get_out_mute
, (setter
)Channel_set_out_mute
,
509 (getter
)Channel_get_solo
, (setter
)Channel_set_solo
,
512 (getter
)Channel_get_meter
, NULL
,
515 (getter
)Channel_get_abspeak
, (setter
)Channel_set_abspeak
,
518 NULL
, (setter
)Channel_set_midi_scale
,
520 {"midi_change_callback",
521 (getter
)Channel_get_midi_change_callback
,
522 (setter
)Channel_set_midi_change_callback
,
523 "midi change callback", NULL
},
525 (getter
)Channel_get_name
,
526 (setter
)Channel_set_name
,
529 (getter
)Channel_get_balance_midi_cc
,
530 (setter
)Channel_set_balance_midi_cc
,
531 "Balance MIDI CC", NULL
},
533 (getter
)Channel_get_volume_midi_cc
,
534 (setter
)Channel_set_volume_midi_cc
,
535 "Volume MIDI CC", NULL
},
537 (getter
)Channel_get_mute_midi_cc
,
538 (setter
)Channel_set_mute_midi_cc
,
539 "Mute MIDI CC", NULL
},
541 (getter
)Channel_get_solo_midi_cc
,
542 (setter
)Channel_set_solo_midi_cc
,
543 "Mute MIDI CC", NULL
},
544 {"midi_in_got_events",
545 (getter
)Channel_get_midi_in_got_events
, NULL
,
546 "Got new MIDI IN events", NULL
},
551 Channel_remove(ChannelObject
*self
, PyObject
*args
)
553 if (! PyArg_ParseTuple(args
, "")) return NULL
;
554 remove_channel(self
->channel
);
560 Channel_autoset_midi_cc(ChannelObject
*self
, PyObject
*args
)
562 if (! PyArg_ParseTuple(args
, "")) return NULL
;
563 channel_autoset_midi_cc(self
->channel
);
568 static PyMethodDef channel_methods
[] = {
569 {"remove", (PyCFunction
)Channel_remove
, METH_VARARGS
, "Remove"},
570 {"autoset_midi_cc", (PyCFunction
)Channel_autoset_midi_cc
, METH_VARARGS
, "Autoset MIDI CC"},
574 static PyTypeObject ChannelType
= {
575 PyVarObject_HEAD_INIT(NULL
, 0)
576 "jack_mixer_c.Channel", /*tp_name*/
577 sizeof(ChannelObject
), /*tp_basicsize*/
579 (destructor
)Channel_dealloc
, /*tp_dealloc*/
586 0, /*tp_as_sequence*/
594 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
595 "Channel objects", /* tp_doc */
598 0, /* tp_richcompare */
599 0, /* tp_weaklistoffset */
602 channel_methods
, /* tp_methods */
604 Channel_getseters
, /* tp_getset */
607 0, /* tp_descr_get */
608 0, /* tp_descr_set */
609 0, /* tp_dictoffset */
610 (initproc
)Channel_init
, /* tp_init */
612 Channel_new
, /* tp_new */
616 Channel_New(jack_mixer_channel_t channel
)
619 self
= (ChannelObject
*)PyObject_NEW(ChannelObject
, &ChannelType
);
621 self
->channel
= channel
;
622 self
->midi_change_callback
= NULL
;
624 return (PyObject
*)self
;
627 /** Output Channel Type **/
631 PyObject
*midi_change_callback
;
632 jack_mixer_output_channel_t
*output_channel
;
633 } OutputChannelObject
;
636 OutputChannel_set_prefader(OutputChannelObject
*self
, PyObject
*value
, void *closure
)
638 if (value
== Py_True
) {
639 output_channel_set_prefader(self
->output_channel
, true);
641 output_channel_set_prefader(self
->output_channel
, false);
647 OutputChannel_get_prefader(OutputChannelObject
*self
, void *closure
)
651 if (output_channel_is_prefader(self
->output_channel
)) {
660 static PyGetSetDef OutputChannel_getseters
[] = {
662 (getter
)OutputChannel_get_prefader
, (setter
)OutputChannel_set_prefader
,
668 OutputChannel_remove(OutputChannelObject
*self
, PyObject
*args
)
670 if (! PyArg_ParseTuple(args
, "")) return NULL
;
671 remove_output_channel(self
->output_channel
);
677 OutputChannel_set_solo(OutputChannelObject
*self
, PyObject
*args
)
682 if (! PyArg_ParseTuple(args
, "Ob", &channel
, &solo
)) return NULL
;
684 output_channel_set_solo(self
->output_channel
,
685 ((ChannelObject
*)channel
)->channel
,
693 OutputChannel_set_muted(OutputChannelObject
*self
, PyObject
*args
)
698 if (! PyArg_ParseTuple(args
, "Ob", &channel
, &muted
)) return NULL
;
700 output_channel_set_muted(self
->output_channel
,
701 ((ChannelObject
*)channel
)->channel
,
709 OutputChannel_is_solo(OutputChannelObject
*self
, PyObject
*args
)
714 if (! PyArg_ParseTuple(args
, "O", &channel
)) return NULL
;
716 if (output_channel_is_solo(self
->output_channel
,
717 ((ChannelObject
*)channel
)->channel
)) {
728 OutputChannel_is_muted(OutputChannelObject
*self
, PyObject
*args
)
733 if (! PyArg_ParseTuple(args
, "O", &channel
)) return NULL
;
735 if (output_channel_is_muted(self
->output_channel
,
736 ((ChannelObject
*)channel
)->channel
)) {
746 static PyMethodDef output_channel_methods
[] = {
747 {"remove", (PyCFunction
)OutputChannel_remove
, METH_VARARGS
, "Remove"},
748 {"set_solo", (PyCFunction
)OutputChannel_set_solo
, METH_VARARGS
, "Set a channel as solo"},
749 {"set_muted", (PyCFunction
)OutputChannel_set_muted
, METH_VARARGS
, "Set a channel as muted"},
750 {"is_solo", (PyCFunction
)OutputChannel_is_solo
, METH_VARARGS
, "Is a channel set as solo"},
751 {"is_muted", (PyCFunction
)OutputChannel_is_muted
, METH_VARARGS
, "Is a channel set as muted"},
755 static PyTypeObject OutputChannelType
= {
756 PyVarObject_HEAD_INIT(NULL
, 0)
757 "jack_mixer_c.OutputChannel", /*tp_name*/
758 sizeof(OutputChannelObject
), /*tp_basicsize*/
767 0, /*tp_as_sequence*/
775 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
776 "Output Channel objects", /* tp_doc */
779 0, /* tp_richcompare */
780 0, /* tp_weaklistoffset */
783 output_channel_methods
, /* tp_methods */
785 OutputChannel_getseters
, /* tp_getset */
786 &ChannelType
, /* tp_base */
788 0, /* tp_descr_get */
789 0, /* tp_descr_set */
790 0, /* tp_dictoffset */
797 OutputChannel_New(jack_mixer_output_channel_t output_channel
)
799 OutputChannelObject
*self
;
800 self
= (OutputChannelObject
*)PyObject_NEW(OutputChannelObject
, &OutputChannelType
);
802 self
->midi_change_callback
= NULL
;
803 self
->output_channel
= output_channel
;
805 return (PyObject
*)self
;
817 Mixer_dealloc(MixerObject
*self
)
820 destroy(self
->mixer
);
821 Py_TYPE(self
)->tp_free((PyObject
*)self
);
825 Mixer_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
829 self
= (MixerObject
*)type
->tp_alloc(type
, 0);
835 return (PyObject
*)self
;
839 Mixer_init(MixerObject
*self
, PyObject
*args
, PyObject
*kwds
)
841 static char *kwlist
[] = {"name", "stereo", NULL
};
845 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "s|b", kwlist
, &name
, &stereo
))
848 self
->mixer
= create(name
, (bool)stereo
);
849 if (self
->mixer
== NULL
) {
850 PyErr_SetString(PyExc_RuntimeError
,
851 "error creating mixer, probably jack is not running");
858 static PyMemberDef Mixer_members
[] = {
863 Mixer_get_channels_count(MixerObject
*self
, void *closure
)
865 return PyLong_FromLong(get_channels_count(self
->mixer
));
869 Mixer_get_client_name(MixerObject
*self
, void *closure
)
871 return PyUnicode_FromString(get_client_name(self
->mixer
));
875 Mixer_get_last_midi_channel(MixerObject
*self
, void *closure
)
877 return PyLong_FromLong(get_last_midi_channel(self
->mixer
));
881 Mixer_set_last_midi_channel(MixerObject
*self
, PyObject
*value
, void *closure
)
886 new_channel
= PyLong_AsLong(value
);
887 result
= set_last_midi_channel(self
->mixer
, new_channel
);
894 static PyGetSetDef Mixer_getseters
[] = {
895 {"channels_count", (getter
)Mixer_get_channels_count
, NULL
,
896 "channels count", NULL
},
897 {"last_midi_channel", (getter
)Mixer_get_last_midi_channel
, (setter
)Mixer_set_last_midi_channel
,
898 "last midi channel", NULL
},
903 Mixer_add_channel(MixerObject
*self
, PyObject
*args
)
907 jack_mixer_channel_t channel
;
909 if (! PyArg_ParseTuple(args
, "si", &name
, &stereo
)) return NULL
;
911 channel
= add_channel(self
->mixer
, name
, (bool)stereo
);
913 if (channel
== NULL
) {
914 PyErr_SetString(PyExc_RuntimeError
, "error adding channel");
918 return Channel_New(channel
);
922 Mixer_add_output_channel(MixerObject
*self
, PyObject
*args
)
927 jack_mixer_output_channel_t channel
;
929 if (! PyArg_ParseTuple(args
, "s|bb", &name
, &stereo
, &system
)) return NULL
;
931 channel
= add_output_channel(self
->mixer
, name
, (bool)stereo
, (bool)system
);
933 return OutputChannel_New(channel
);
937 Mixer_destroy(MixerObject
*self
, PyObject
*args
)
940 destroy(self
->mixer
);
947 static PyMethodDef Mixer_methods
[] = {
948 {"add_channel", (PyCFunction
)Mixer_add_channel
, METH_VARARGS
, "Add a new channel"},
949 {"add_output_channel", (PyCFunction
)Mixer_add_output_channel
, METH_VARARGS
, "Add a new output channel"},
950 {"destroy", (PyCFunction
)Mixer_destroy
, METH_VARARGS
, "Destroy JACK Mixer"},
951 {"client_name", (PyCFunction
)Mixer_get_client_name
, METH_VARARGS
, "Get jack client name"},
952 // {"remove_channel", (PyCFunction)Mixer_remove_channel, METH_VARARGS, "Remove a channel"},
956 static PyTypeObject MixerType
= {
957 PyVarObject_HEAD_INIT(NULL
, 0)
958 "jack_mixer_c.Mixer", /*tp_name*/
959 sizeof(MixerObject
), /*tp_basicsize*/
961 (destructor
)Mixer_dealloc
, /*tp_dealloc*/
968 0, /*tp_as_sequence*/
976 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
977 "Mixer objects", /* tp_doc */
980 0, /* tp_richcompare */
981 0, /* tp_weaklistoffset */
984 Mixer_methods
, /* tp_methods */
985 Mixer_members
, /* tp_members */
986 Mixer_getseters
, /* tp_getset */
989 0, /* tp_descr_get */
990 0, /* tp_descr_set */
991 0, /* tp_dictoffset */
992 (initproc
)Mixer_init
, /* tp_init */
994 Mixer_new
, /* tp_new */
998 static PyMethodDef jack_mixer_methods
[] = {
999 {NULL
, NULL
, 0, NULL
} /* Sentinel */
1003 static struct PyModuleDef moduledef
= {
1004 PyModuleDef_HEAD_INIT
,
1005 "jack_mixer_c", /* m_name */
1006 "Jack Mixer C Helper Module", /* m_doc */
1008 jack_mixer_methods
, /* m_methods */
1009 NULL
, /* m_reload */
1010 NULL
, /* m_traverse */
1015 PyMODINIT_FUNC
PyInit_jack_mixer_c(void)
1019 if (PyType_Ready(&MixerType
) < 0)
1021 if (PyType_Ready(&ChannelType
) < 0)
1023 if (PyType_Ready(&OutputChannelType
) < 0)
1025 if (PyType_Ready(&ScaleType
) < 0)
1028 m
= PyModule_Create(&moduledef
);
1029 //m = Py_InitModule3("jack_mixer_c", jack_mixer_methods, "module doc");
1031 Py_INCREF(&MixerType
);
1032 PyModule_AddObject(m
, "Mixer", (PyObject
*)&MixerType
);
1033 Py_INCREF(&ChannelType
);
1034 PyModule_AddObject(m
, "Channel", (PyObject
*)&ChannelType
);
1035 Py_INCREF(&OutputChannelType
);
1036 PyModule_AddObject(m
, "OutputChannel", (PyObject
*)&OutputChannelType
);
1037 Py_INCREF(&ScaleType
);
1038 PyModule_AddObject(m
, "Scale", (PyObject
*)&ScaleType
);