]> git.0d.be Git - jack_mixer.git/blob - jack_mixer_c.c
Add midi control for mute-all button
[jack_mixer.git] / jack_mixer_c.c
1 /*
2  * This file is part of jack_mixer
3  *
4  * Copyright (C) 2009 Frederic Peters <fpeters@0d.be>
5  *
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
9  *
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.
14  *
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.
18  */
19
20 #include <Python.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
25
26 #include <structmember.h>
27
28 #include "jack_mixer.h"
29
30
31 /** Scale Type **/
32
33 typedef struct {
34         PyObject_HEAD
35         jack_mixer_scale_t scale;
36 } ScaleObject;
37
38 static void
39 Scale_dealloc(ScaleObject *self)
40 {
41         if (self->scale)
42                 scale_destroy(self->scale);
43         self->ob_type->tp_free((PyObject*)self);
44 }
45
46 static int
47 Scale_init(ScaleObject *self, PyObject *args, PyObject *kwds)
48 {
49         self->scale = scale_create();
50         return 0;
51 }
52
53 static PyObject*
54 Scale_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
55 {
56         ScaleObject *self;
57
58         self = (ScaleObject*)type->tp_alloc(type, 0);
59
60         return (PyObject*)self;
61 }
62
63 static PyObject*
64 Scale_add_threshold(ScaleObject *self, PyObject *args)
65 {
66         float db, scale_value;
67
68         if (! PyArg_ParseTuple(args, "ff", &db, &scale_value)) return NULL;
69
70         scale_add_threshold(self->scale, db, scale_value);
71
72         Py_INCREF(Py_None);
73         return Py_None;
74 }
75
76 static PyObject*
77 Scale_calculate_coefficients(ScaleObject *self, PyObject *args)
78 {
79         if (! PyArg_ParseTuple(args, "")) return NULL;
80         scale_calculate_coefficients(self->scale);
81         Py_INCREF(Py_None);
82         return Py_None;
83 }
84
85 static PyObject*
86 Scale_db_to_scale(ScaleObject *self, PyObject *args)
87 {
88         double db;
89         if (! PyArg_ParseTuple(args, "d", &db)) return NULL;
90         return PyFloat_FromDouble(scale_db_to_scale(self->scale, db));
91 }
92
93 static PyObject*
94 Scale_scale_to_db(ScaleObject *self, PyObject *args)
95 {
96         double scale_value;
97         if (! PyArg_ParseTuple(args, "d", &scale_value)) return NULL;
98         return PyFloat_FromDouble(scale_scale_to_db(self->scale, scale_value));
99 }
100
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"},
107         {NULL}
108 };
109
110 static PyTypeObject ScaleType = {
111         PyObject_HEAD_INIT(NULL)
112         0,       /*ob_size*/
113         "jack_mixer_c.Scale",    /*tp_name*/
114         sizeof(ScaleObject), /*tp_basicsize*/
115         0,       /*tp_itemsize*/
116         (destructor)Scale_dealloc,       /*tp_dealloc*/
117         0,       /*tp_print*/
118         0,       /*tp_getattr*/
119         0,       /*tp_setattr*/
120         0,       /*tp_compare*/
121         0,       /*tp_repr*/
122         0,       /*tp_as_number*/
123         0,       /*tp_as_sequence*/
124         0,       /*tp_as_mapping*/
125         0,       /*tp_hash */
126         0,       /*tp_call*/
127         0,       /*tp_str*/
128         0,       /*tp_getattro*/
129         0,       /*tp_setattro*/
130         0,       /*tp_as_buffer*/
131         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
132         "Scale objects",           /* tp_doc */
133         0,                         /* tp_traverse */
134         0,                         /* tp_clear */
135         0,                         /* tp_richcompare */
136         0,                         /* tp_weaklistoffset */
137         0,                         /* tp_iter */
138         0,                         /* tp_iternext */
139         Scale_methods,             /* tp_methods */
140         0,             /* tp_members */
141         0,           /* tp_getset */
142         0,                         /* tp_base */
143         0,                         /* tp_dict */
144         0,                         /* tp_descr_get */
145         0,                         /* tp_descr_set */
146         0,                         /* tp_dictoffset */
147         (initproc)Scale_init,      /* tp_init */
148         0,                         /* tp_alloc */
149         Scale_new,                 /* tp_new */
150 };
151
152
153 /** Channel Type **/
154
155 typedef struct {
156         PyObject_HEAD
157         PyObject *midi_change_callback;
158         jack_mixer_channel_t channel;
159 } ChannelObject;
160
161 static void
162 Channel_dealloc(ChannelObject *self)
163 {
164         Py_XDECREF(self->midi_change_callback);
165         self->ob_type->tp_free((PyObject*)self);
166 }
167
168 static int
169 Channel_init(ChannelObject *self, PyObject *args, PyObject *kwds)
170 {
171         self->midi_change_callback = NULL;
172         return 0;
173 }
174
175 static PyObject*
176 Channel_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
177 {
178         ChannelObject *self;
179
180         self = (ChannelObject*)type->tp_alloc(type, 0);
181
182         if (self != NULL) {
183                 self->channel = NULL;
184                 self->midi_change_callback = NULL;
185         }
186
187         return (PyObject*)self;
188 }
189
190 static PyObject*
191 Channel_get_is_stereo(ChannelObject *self, void *closure)
192 {
193         PyObject *result;
194
195         bool is_stereo = channel_is_stereo(self->channel);
196         if (is_stereo) {
197                 result = Py_True;
198         } else {
199                 result = Py_False;
200         }
201         Py_INCREF(result);
202         return result;
203 }
204
205 static PyObject*
206 Channel_get_volume(ChannelObject *self, void *closure)
207 {
208         return PyFloat_FromDouble(channel_volume_read(self->channel));
209 }
210
211 static int
212 Channel_set_volume(ChannelObject *self, PyObject *value, void *closure)
213 {
214         if (self->channel == NULL) {
215                 PyErr_SetString(PyExc_RuntimeError, "unitialized channel");
216                 return -1;
217         }
218         channel_volume_write(self->channel, PyFloat_AsDouble(value));
219         return 0;
220 }
221
222 static PyObject*
223 Channel_get_balance(ChannelObject *self, void *closure)
224 {
225         return PyFloat_FromDouble(channel_balance_read(self->channel));
226 }
227
228 static int
229 Channel_set_balance(ChannelObject *self, PyObject *value, void *closure)
230 {
231         channel_balance_write(self->channel, PyFloat_AsDouble(value));
232         return 0;
233 }
234
235 static PyObject*
236 Channel_get_out_mute(ChannelObject *self, void *closure)
237 {
238         PyObject *result;
239
240     if (channel_is_out_muted(self->channel)) {
241                 result = Py_True;
242         } else {
243                 result = Py_False;
244         }
245         Py_INCREF(result);
246         return result;
247 }
248
249 static int
250 Channel_set_out_mute(ChannelObject *self, PyObject *value, void *closure)
251 {
252         if (value == Py_True) {
253                 channel_out_mute(self->channel);
254         } else {
255                 channel_out_unmute(self->channel);
256         }
257         return 0;
258 }
259
260 static PyObject*
261 Channel_get_meter(ChannelObject *self, void *closure)
262 {
263         PyObject *result;
264         double left, right;
265
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));
271         } else {
272                 result = PyTuple_New(1);
273                 channel_mono_meter_read(self->channel, &left);
274                 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
275         }
276         return result;
277 }
278
279 static PyObject*
280 Channel_get_abspeak(ChannelObject *self, void *closure)
281 {
282         return PyFloat_FromDouble(channel_abspeak_read(self->channel));
283 }
284
285 static int
286 Channel_set_abspeak(ChannelObject *self, PyObject *value, void *closure)
287 {
288         if (value != Py_None) {
289                 fprintf(stderr, "abspeak can only be reset (set to None)\n");
290                 return -1;
291         }
292         channel_abspeak_reset(self->channel);
293         return 0;
294 }
295
296 static int
297 Channel_set_midi_scale(ChannelObject *self, PyObject *value, void *closure)
298 {
299         ScaleObject *scale_object = (ScaleObject*)value; /* XXX: check */
300
301         channel_set_midi_scale(self->channel, scale_object->scale);
302         return 0;
303 }
304
305
306 static PyObject*
307 Channel_get_midi_change_callback(ChannelObject *self, void *closure)
308 {
309         if (self->midi_change_callback) {
310                 Py_INCREF(self->midi_change_callback);
311                 return self->midi_change_callback;
312         } else {
313                 Py_INCREF(Py_None);
314                 return Py_None;
315         }
316 }
317
318 static void
319 channel_midi_callback(void *userdata)
320 {
321         ChannelObject *self = (ChannelObject*)userdata;
322         PyGILState_STATE gstate;
323
324         gstate = PyGILState_Ensure();
325         PyObject_CallObject(self->midi_change_callback, NULL);
326         PyGILState_Release(gstate);
327 }
328
329 static int
330 Channel_set_midi_change_callback(ChannelObject *self, PyObject *value, void *closure)
331 {
332         if (value == Py_None) {
333                 self->midi_change_callback = NULL;
334                 channel_set_midi_change_callback(self->channel, NULL, NULL);
335         } else {
336                 if (!PyCallable_Check(value)) {
337                         PyErr_SetString(PyExc_TypeError, "value must be callable");
338                         return -1;
339                 }
340                 if (self->midi_change_callback) {
341                         Py_XDECREF(self->midi_change_callback);
342                 }
343                 Py_INCREF(value);
344                 self->midi_change_callback = value;
345                 channel_set_midi_change_callback(self->channel,
346                                 channel_midi_callback, self);
347         }
348
349         return 0;
350 }
351
352 static PyObject*
353 Channel_get_name(ChannelObject *self, void *closure)
354 {
355         return PyString_FromString(channel_get_name(self->channel));
356 }
357
358 static int
359 Channel_set_name(ChannelObject *self, PyObject *value, void *closure)
360 {
361         channel_rename(self->channel, PyString_AsString(value));
362         return 0;
363 }
364
365 static PyObject*
366 Channel_get_balance_midi_cc(ChannelObject *self, void *closure)
367 {
368         return PyInt_FromLong(channel_get_balance_midi_cc(self->channel));
369 }
370
371 static int
372 Channel_set_balance_midi_cc(ChannelObject *self, PyObject *value, void *closure)
373 {
374         unsigned int new_cc;
375         unsigned int result;
376
377         new_cc = PyInt_AsLong(value);
378         result = channel_set_balance_midi_cc(self->channel, new_cc);
379         if (result == 0) {
380                 return 0;
381         }
382         if (result == 1) {
383                 PyErr_SetString(PyExc_RuntimeError, "value already in use");
384         } else if (result == 2) {
385                 PyErr_SetString(PyExc_RuntimeError, "value out of range");
386         }
387         return -1;
388 }
389
390 static PyObject*
391 Channel_get_volume_midi_cc(ChannelObject *self, void *closure)
392 {
393         return PyInt_FromLong(channel_get_volume_midi_cc(self->channel));
394 }
395
396 static int
397 Channel_set_volume_midi_cc(ChannelObject *self, PyObject *value, void *closure)
398 {
399         unsigned int new_cc;
400         unsigned int result;
401
402         new_cc = PyInt_AsLong(value);
403         result = channel_set_volume_midi_cc(self->channel, new_cc);
404         if (result == 0) {
405                 return 0;
406         }
407         if (result == 1) {
408                 PyErr_SetString(PyExc_RuntimeError, "value already in use");
409         } else if (result == 2) {
410                 PyErr_SetString(PyExc_RuntimeError, "value out of range");
411         }
412         return -1;
413 }
414
415 static PyObject*
416 Channel_get_mute_midi_cc(ChannelObject *self, void *closure)
417 {
418         return PyInt_FromLong(channel_get_mute_midi_cc(self->channel));
419 }
420
421 static int
422 Channel_set_mute_midi_cc(ChannelObject *self, PyObject *value, void *closure)
423 {
424         unsigned int new_cc;
425         unsigned int result;
426
427         new_cc = PyInt_AsLong(value);
428         result = channel_set_mute_midi_cc(self->channel, new_cc);
429         if (result == 0) {
430                 return 0;
431         }
432         if (result == 1) {
433                 PyErr_SetString(PyExc_RuntimeError, "value already in use");
434         } else if (result == 2) {
435                 PyErr_SetString(PyExc_RuntimeError, "value out of range");
436         }
437         return -1;
438 }
439
440 static PyObject*
441 Channel_get_midi_in_got_events(ChannelObject *self, void *closure)
442 {
443         PyObject *result;
444
445         if (channel_get_midi_in_got_events(self->channel)) {
446                 result = Py_True;
447         } else {
448                 result = Py_False;
449         }
450         Py_INCREF(result);
451         return result;
452 }
453
454
455 static PyGetSetDef Channel_getseters[] = {
456         {"is_stereo",
457                 (getter)Channel_get_is_stereo, NULL,
458                 "mono/stereo", NULL},
459         {"volume",
460                 (getter)Channel_get_volume, (setter)Channel_set_volume,
461                 "volume", NULL},
462         {"balance",
463                 (getter)Channel_get_balance, (setter)Channel_set_balance,
464                 "balance", NULL},
465         {"out_mute",
466                 (getter)Channel_get_out_mute, (setter)Channel_set_out_mute,
467                 "out_mute", NULL},
468         {"meter",
469                 (getter)Channel_get_meter, NULL,
470                 "meter", NULL},
471         {"abspeak",
472                 (getter)Channel_get_abspeak, (setter)Channel_set_abspeak,
473                 "balance", NULL},
474         {"midi_scale",
475                 NULL, (setter)Channel_set_midi_scale,
476                 "midi scale", NULL},
477         {"midi_change_callback",
478                 (getter)Channel_get_midi_change_callback,
479                 (setter)Channel_set_midi_change_callback,
480                 "midi change callback", NULL},
481         {"name",
482                 (getter)Channel_get_name,
483                 (setter)Channel_set_name,
484                 "name", NULL},
485         {"balance_midi_cc",
486                 (getter)Channel_get_balance_midi_cc,
487                 (setter)Channel_set_balance_midi_cc,
488                 "Balance MIDI CC", NULL},
489         {"volume_midi_cc",
490                 (getter)Channel_get_volume_midi_cc,
491                 (setter)Channel_set_volume_midi_cc,
492                 "Volume MIDI CC", NULL},
493         {"mute_midi_cc",
494                 (getter)Channel_get_mute_midi_cc,
495                 (setter)Channel_set_mute_midi_cc,
496                 "Mute MIDI CC", NULL},
497         {"midi_in_got_events",
498                 (getter)Channel_get_midi_in_got_events, NULL,
499                 "Got new MIDI IN events", NULL},
500         {NULL}
501 };
502
503 static PyObject*
504 Channel_remove(ChannelObject *self, PyObject *args)
505 {
506         if (! PyArg_ParseTuple(args, "")) return NULL;
507         remove_channel(self->channel);
508         Py_INCREF(Py_None);
509         return Py_None;
510 }
511
512 static PyObject*
513 Channel_autoset_midi_cc(ChannelObject *self, PyObject *args)
514 {
515         if (! PyArg_ParseTuple(args, "")) return NULL;
516         channel_autoset_midi_cc(self->channel);
517         Py_INCREF(Py_None);
518         return Py_None;
519 }
520
521 static PyMethodDef channel_methods[] = {
522         {"remove", (PyCFunction)Channel_remove, METH_VARARGS, "Remove"},
523         {"autoset_midi_cc", (PyCFunction)Channel_autoset_midi_cc, METH_VARARGS, "Autoset MIDI CC"},
524         {NULL}
525 };
526
527 static PyTypeObject ChannelType = {
528         PyObject_HEAD_INIT(NULL)
529         0,       /*ob_size*/
530         "jack_mixer_c.Channel",    /*tp_name*/
531         sizeof(ChannelObject), /*tp_basicsize*/
532         0,       /*tp_itemsize*/
533         (destructor)Channel_dealloc,       /*tp_dealloc*/
534         0,       /*tp_print*/
535         0,       /*tp_getattr*/
536         0,       /*tp_setattr*/
537         0,       /*tp_compare*/
538         0,       /*tp_repr*/
539         0,       /*tp_as_number*/
540         0,       /*tp_as_sequence*/
541         0,       /*tp_as_mapping*/
542         0,       /*tp_hash */
543         0,       /*tp_call*/
544         0,       /*tp_str*/
545         0,       /*tp_getattro*/
546         0,       /*tp_setattro*/
547         0,       /*tp_as_buffer*/
548         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
549         "Channel objects",           /* tp_doc */
550         0,                         /* tp_traverse */
551         0,                         /* tp_clear */
552         0,                         /* tp_richcompare */
553         0,                         /* tp_weaklistoffset */
554         0,                         /* tp_iter */
555         0,                         /* tp_iternext */
556         channel_methods,           /* tp_methods */
557         0,             /* tp_members */
558         Channel_getseters,           /* tp_getset */
559         0,                         /* tp_base */
560         0,                         /* tp_dict */
561         0,                         /* tp_descr_get */
562         0,                         /* tp_descr_set */
563         0,                         /* tp_dictoffset */
564         (initproc)Channel_init,    /* tp_init */
565         0,                         /* tp_alloc */
566         Channel_new,                 /* tp_new */
567 };
568
569 static PyObject*
570 Channel_New(jack_mixer_channel_t channel)
571 {
572         ChannelObject *self;
573         self = (ChannelObject*)PyObject_NEW(ChannelObject, &ChannelType);
574         if (self != NULL) {
575                 self->channel = channel;
576                 self->midi_change_callback = NULL;
577         }
578         return (PyObject*)self;
579 }
580
581 /** Output Channel Type **/
582
583 typedef struct {
584         PyObject_HEAD
585         PyObject *midi_change_callback;
586         jack_mixer_output_channel_t *output_channel;
587 } OutputChannelObject;
588
589 static int
590 OutputChannel_set_prefader(OutputChannelObject *self, PyObject *value, void *closure)
591 {
592         if (value == Py_True) {
593                 output_channel_set_prefader(self->output_channel, true);
594         } else {
595                 output_channel_set_prefader(self->output_channel, false);
596         }
597         return 0;
598 }
599
600 static PyObject*
601 OutputChannel_get_prefader(OutputChannelObject *self, void *closure)
602 {
603         PyObject *result;
604
605         if (output_channel_is_prefader(self->output_channel)) {
606                 result = Py_True;
607         } else {
608                 result = Py_False;
609         }
610         Py_INCREF(result);
611         return result;
612 }
613
614 static PyGetSetDef OutputChannel_getseters[] = {
615         {"prefader",
616                 (getter)OutputChannel_get_prefader, (setter)OutputChannel_set_prefader,
617                 "prefader", NULL},
618         {NULL}
619 };
620
621 static PyObject*
622 OutputChannel_remove(OutputChannelObject *self, PyObject *args)
623 {
624         if (! PyArg_ParseTuple(args, "")) return NULL;
625         remove_output_channel(self->output_channel);
626         Py_INCREF(Py_None);
627         return Py_None;
628 }
629
630 static PyObject*
631 OutputChannel_set_solo(OutputChannelObject *self, PyObject *args)
632 {
633         PyObject *channel;
634         unsigned char solo;
635
636         if (! PyArg_ParseTuple(args, "Ob", &channel, &solo)) return NULL;
637
638         output_channel_set_solo(self->output_channel,
639                         ((ChannelObject*)channel)->channel,
640                         solo);
641
642         Py_INCREF(Py_None);
643         return Py_None;
644 }
645
646 static PyObject*
647 OutputChannel_set_muted(OutputChannelObject *self, PyObject *args)
648 {
649         PyObject *channel;
650         unsigned char muted;
651
652         if (! PyArg_ParseTuple(args, "Ob", &channel, &muted)) return NULL;
653
654         output_channel_set_muted(self->output_channel,
655                         ((ChannelObject*)channel)->channel,
656                         muted);
657
658         Py_INCREF(Py_None);
659         return Py_None;
660 }
661
662 static PyObject*
663 OutputChannel_is_solo(OutputChannelObject *self, PyObject *args)
664 {
665         PyObject *channel;
666         PyObject *result;
667
668         if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
669
670         if (output_channel_is_solo(self->output_channel,
671                         ((ChannelObject*)channel)->channel)) {
672                 result = Py_True;
673         } else {
674                 result = Py_False;
675         }
676
677         Py_INCREF(result);
678         return result;
679 }
680
681 static PyObject*
682 OutputChannel_is_muted(OutputChannelObject *self, PyObject *args)
683 {
684         PyObject *channel;
685         PyObject *result;
686
687         if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
688
689         if (output_channel_is_muted(self->output_channel,
690                         ((ChannelObject*)channel)->channel)) {
691                 result = Py_True;
692         } else {
693                 result = Py_False;
694         }
695
696         Py_INCREF(result);
697         return result;
698 }
699
700 static PyMethodDef output_channel_methods[] = {
701         {"remove", (PyCFunction)OutputChannel_remove, METH_VARARGS, "Remove"},
702         {"set_solo", (PyCFunction)OutputChannel_set_solo, METH_VARARGS, "Set a channel as solo"},
703         {"set_muted", (PyCFunction)OutputChannel_set_muted, METH_VARARGS, "Set a channel as muted"},
704         {"is_solo", (PyCFunction)OutputChannel_is_solo, METH_VARARGS, "Is a channel set as solo"},
705         {"is_muted", (PyCFunction)OutputChannel_is_muted, METH_VARARGS, "Is a channel set as muted"},
706         {NULL}
707 };
708
709 static PyTypeObject OutputChannelType = {
710         PyObject_HEAD_INIT(NULL)
711         0,       /*ob_size*/
712         "jack_mixer_c.OutputChannel",    /*tp_name*/
713         sizeof(OutputChannelObject), /*tp_basicsize*/
714         0,       /*tp_itemsize*/
715         0,       /*tp_dealloc*/
716         0,       /*tp_print*/
717         0,       /*tp_getattr*/
718         0,       /*tp_setattr*/
719         0,       /*tp_compare*/
720         0,       /*tp_repr*/
721         0,       /*tp_as_number*/
722         0,       /*tp_as_sequence*/
723         0,       /*tp_as_mapping*/
724         0,       /*tp_hash */
725         0,       /*tp_call*/
726         0,       /*tp_str*/
727         0,       /*tp_getattro*/
728         0,       /*tp_setattro*/
729         0,       /*tp_as_buffer*/
730         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
731         "Output Channel objects",           /* tp_doc */
732         0,                         /* tp_traverse */
733         0,                         /* tp_clear */
734         0,                         /* tp_richcompare */
735         0,                         /* tp_weaklistoffset */
736         0,                         /* tp_iter */
737         0,                         /* tp_iternext */
738         output_channel_methods,    /* tp_methods */
739         0,             /* tp_members */
740         OutputChannel_getseters,   /* tp_getset */
741         &ChannelType,              /* tp_base */
742         0,                         /* tp_dict */
743         0,                         /* tp_descr_get */
744         0,                         /* tp_descr_set */
745         0,                         /* tp_dictoffset */
746         0,                         /* tp_init */
747         0,                         /* tp_alloc */
748         0,                         /* tp_new */
749 };
750
751 static PyObject*
752 OutputChannel_New(jack_mixer_output_channel_t output_channel)
753 {
754         OutputChannelObject *self;
755         self = (OutputChannelObject*)PyObject_NEW(OutputChannelObject, &OutputChannelType);
756         if (self != NULL) {
757                 self->midi_change_callback = NULL;
758                 self->output_channel = output_channel;
759         }
760         return (PyObject*)self;
761 }
762
763
764 /** Mixer Type **/
765
766 typedef struct {
767         PyObject_HEAD
768         jack_mixer_t mixer;
769 } MixerObject;
770
771 static void
772 Mixer_dealloc(MixerObject *self)
773 {
774         if (self->mixer)
775                 destroy(self->mixer);
776         self->ob_type->tp_free((PyObject*)self);
777 }
778
779 static PyObject*
780 Mixer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
781 {
782         MixerObject *self;
783
784         self = (MixerObject*)type->tp_alloc(type, 0);
785
786         if (self != NULL) {
787                 self->mixer = NULL;
788         }
789
790         return (PyObject*)self;
791 }
792
793 static int
794 Mixer_init(MixerObject *self, PyObject *args, PyObject *kwds)
795 {
796         static char *kwlist[] = {"name", "stereo", NULL};
797         char *name;
798         int stereo = 1;
799
800         if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|b", kwlist, &name, &stereo))
801                 return -1;
802
803         self->mixer = create(name, (bool)stereo);
804         if (self->mixer == NULL) {
805                 PyErr_SetString(PyExc_RuntimeError,
806                                 "error creating mixer, probably jack is not running");
807                 return -1;
808         }
809
810         return 0;
811 }
812
813 static PyMemberDef Mixer_members[] = {
814         {NULL}
815 };
816
817 static PyObject*
818 Mixer_get_channels_count(MixerObject *self, void *closure)
819 {
820         return PyInt_FromLong(get_channels_count(self->mixer));
821 }
822
823 static PyObject*
824 Mixer_get_client_name(MixerObject *self, void *closure)
825 {
826         return PyString_FromString(get_client_name(self->mixer));
827 }
828
829 static PyObject*
830 Mixer_get_last_midi_channel(MixerObject *self, void *closure)
831 {
832         return PyInt_FromLong(get_last_midi_channel(self->mixer));
833 }
834
835
836 static PyGetSetDef Mixer_getseters[] = {
837         {"channels_count", (getter)Mixer_get_channels_count, NULL,
838                 "channels count", NULL},
839         {"last_midi_channel", (getter)Mixer_get_last_midi_channel, NULL,
840                 "last midi channel", NULL},
841         {NULL}
842 };
843
844 static PyObject*
845 Mixer_add_channel(MixerObject *self, PyObject *args)
846 {
847         char *name;
848         int stereo;
849         jack_mixer_channel_t channel;
850
851         if (! PyArg_ParseTuple(args, "si", &name, &stereo)) return NULL;
852
853         channel = add_channel(self->mixer, name, (bool)stereo);
854
855         if (channel == NULL) {
856                 PyErr_SetString(PyExc_RuntimeError, "error adding channel");
857                 return NULL;
858         }
859
860         return Channel_New(channel);
861 }
862
863 static PyObject*
864 Mixer_add_output_channel(MixerObject *self, PyObject *args)
865 {
866         char *name;
867         int stereo = 1;
868         int system = 0;
869         jack_mixer_output_channel_t channel;
870
871         if (! PyArg_ParseTuple(args, "s|bb", &name, &stereo, &system)) return NULL;
872
873         channel = add_output_channel(self->mixer, name, (bool)stereo, (bool)system);
874
875         return OutputChannel_New(channel);
876 }
877
878 static PyObject*
879 Mixer_destroy(MixerObject *self, PyObject *args)
880 {
881         if (self->mixer) {
882                 destroy(self->mixer);
883                 self->mixer = NULL;
884         }
885         Py_INCREF(Py_None);
886         return Py_None;
887 }
888
889 static PyMethodDef Mixer_methods[] = {
890         {"add_channel", (PyCFunction)Mixer_add_channel, METH_VARARGS, "Add a new channel"},
891         {"add_output_channel", (PyCFunction)Mixer_add_output_channel, METH_VARARGS, "Add a new output channel"},
892         {"destroy", (PyCFunction)Mixer_destroy, METH_VARARGS, "Destroy JACK Mixer"},
893         {"client_name", (PyCFunction)Mixer_get_client_name, METH_VARARGS, "Get jack client name"},
894 //      {"remove_channel", (PyCFunction)Mixer_remove_channel, METH_VARARGS, "Remove a channel"},
895         {NULL}
896 };
897
898 static PyTypeObject MixerType = {
899         PyObject_HEAD_INIT(NULL)
900         0,       /*ob_size*/
901         "jack_mixer_c.Mixer",    /*tp_name*/
902         sizeof(MixerObject), /*tp_basicsize*/
903         0,       /*tp_itemsize*/
904         (destructor)Mixer_dealloc,       /*tp_dealloc*/
905         0,       /*tp_print*/
906         0,       /*tp_getattr*/
907         0,       /*tp_setattr*/
908         0,       /*tp_compare*/
909         0,       /*tp_repr*/
910         0,       /*tp_as_number*/
911         0,       /*tp_as_sequence*/
912         0,       /*tp_as_mapping*/
913         0,       /*tp_hash */
914         0,       /*tp_call*/
915         0,       /*tp_str*/
916         0,       /*tp_getattro*/
917         0,       /*tp_setattro*/
918         0,       /*tp_as_buffer*/
919         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
920         "Mixer objects",           /* tp_doc */
921         0,                         /* tp_traverse */
922         0,                         /* tp_clear */
923         0,                         /* tp_richcompare */
924         0,                         /* tp_weaklistoffset */
925         0,                         /* tp_iter */
926         0,                         /* tp_iternext */
927         Mixer_methods,             /* tp_methods */
928         Mixer_members,             /* tp_members */
929         Mixer_getseters,           /* tp_getset */
930         0,                         /* tp_base */
931         0,                         /* tp_dict */
932         0,                         /* tp_descr_get */
933         0,                         /* tp_descr_set */
934         0,                         /* tp_dictoffset */
935         (initproc)Mixer_init,      /* tp_init */
936         0,                         /* tp_alloc */
937         Mixer_new,                 /* tp_new */
938 };
939
940
941 static PyMethodDef jack_mixer_methods[] = {
942         {NULL}  /* Sentinel */
943 };
944
945
946
947 PyMODINIT_FUNC initjack_mixer_c(void)
948 {
949         PyObject *m;
950
951         if (PyType_Ready(&MixerType) < 0)
952                 return;
953         if (PyType_Ready(&ChannelType) < 0)
954                 return;
955         if (PyType_Ready(&OutputChannelType) < 0)
956                 return;
957         if (PyType_Ready(&ScaleType) < 0)
958                 return;
959
960         m = Py_InitModule3("jack_mixer_c", jack_mixer_methods, "Jack Mixer C Helper Module");
961
962         Py_INCREF(&MixerType);
963         PyModule_AddObject(m, "Mixer", (PyObject*)&MixerType);
964         Py_INCREF(&ChannelType);
965         PyModule_AddObject(m, "Channel", (PyObject*)&ChannelType);
966         Py_INCREF(&OutputChannelType);
967         PyModule_AddObject(m, "OutputChannel", (PyObject*)&OutputChannelType);
968         Py_INCREF(&ScaleType);
969         PyModule_AddObject(m, "Scale", (PyObject*)&ScaleType);
970 }
971