]> git.0d.be Git - jack_mixer.git/blob - jack_mixer_c.c
Guard Channel_set_volume() against unitialized channel
[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_mute(ChannelObject *self, void *closure)
237 {
238         PyObject *result;
239
240         if (channel_is_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_mute(ChannelObject *self, PyObject *value, void *closure)
251 {
252         if (value == Py_True) {
253                 channel_mute(self->channel);
254         } else {
255                 channel_unmute(self->channel);
256         }
257         return 0;
258 }
259
260 static PyObject*
261 Channel_get_solo(ChannelObject *self, void *closure)
262 {
263         PyObject *result;
264
265         if (channel_is_soloed(self->channel)) {
266                 result = Py_True;
267         } else {
268                 result = Py_False;
269         }
270         Py_INCREF(result);
271         return result;
272 }
273
274 static int
275 Channel_set_solo(ChannelObject *self, PyObject *value, void *closure)
276 {
277         if (value == Py_True) {
278                 channel_solo(self->channel);
279         } else {
280                 channel_unsolo(self->channel);
281         }
282         return 0;
283 }
284
285 static PyObject*
286 Channel_get_meter(ChannelObject *self, void *closure)
287 {
288         PyObject *result;
289         double left, right;
290
291         if (channel_is_stereo(self->channel)) {
292                 result = PyTuple_New(2);
293                 channel_stereo_meter_read(self->channel, &left, &right);
294                 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
295                 PyTuple_SetItem(result, 1, PyFloat_FromDouble(right));
296         } else {
297                 result = PyTuple_New(1);
298                 channel_mono_meter_read(self->channel, &left);
299                 PyTuple_SetItem(result, 0, PyFloat_FromDouble(left));
300         }
301         return result;
302 }
303
304 static PyObject*
305 Channel_get_abspeak(ChannelObject *self, void *closure)
306 {
307         return PyFloat_FromDouble(channel_abspeak_read(self->channel));
308 }
309
310 static int
311 Channel_set_abspeak(ChannelObject *self, PyObject *value, void *closure)
312 {
313         if (value != Py_None) {
314                 fprintf(stderr, "abspeak can only be reset (set to None)\n");
315                 return -1;
316         }
317         channel_abspeak_reset(self->channel);
318         return 0;
319 }
320
321 static int
322 Channel_set_midi_scale(ChannelObject *self, PyObject *value, void *closure)
323 {
324         ScaleObject *scale_object = (ScaleObject*)value; /* XXX: check */
325
326         channel_set_midi_scale(self->channel, scale_object->scale);
327         return 0;
328 }
329
330
331 static PyObject*
332 Channel_get_midi_change_callback(ChannelObject *self, void *closure)
333 {
334         if (self->midi_change_callback) {
335                 Py_INCREF(self->midi_change_callback);
336                 return self->midi_change_callback;
337         } else {
338                 Py_INCREF(Py_None);
339                 return Py_None;
340         }
341 }
342
343 static void
344 channel_midi_callback(void *userdata)
345 {
346         ChannelObject *self = (ChannelObject*)userdata;
347         PyGILState_STATE gstate;
348
349         gstate = PyGILState_Ensure();
350         PyObject_CallObject(self->midi_change_callback, NULL);
351         PyGILState_Release(gstate);
352 }
353
354 static int
355 Channel_set_midi_change_callback(ChannelObject *self, PyObject *value, void *closure)
356 {
357         if (value == Py_None) {
358                 self->midi_change_callback = NULL;
359                 channel_set_midi_change_callback(self->channel, NULL, NULL);
360         } else {
361                 if (!PyCallable_Check(value)) {
362                         PyErr_SetString(PyExc_TypeError, "value must be callable");
363                         return -1;
364                 }
365                 if (self->midi_change_callback) {
366                         Py_XDECREF(self->midi_change_callback);
367                 }
368                 Py_INCREF(value);
369                 self->midi_change_callback = value;
370                 channel_set_midi_change_callback(self->channel,
371                                 channel_midi_callback, self);
372         }
373
374         return 0;
375 }
376
377 static PyObject*
378 Channel_get_name(ChannelObject *self, void *closure)
379 {
380         return PyString_FromString(channel_get_name(self->channel));
381 }
382
383 static int
384 Channel_set_name(ChannelObject *self, PyObject *value, void *closure)
385 {
386         channel_rename(self->channel, PyString_AsString(value));
387         return 0;
388 }
389
390 static PyObject*
391 Channel_get_balance_midi_cc(ChannelObject *self, void *closure)
392 {
393         return PyInt_FromLong(channel_get_balance_midi_cc(self->channel));
394 }
395
396 static int
397 Channel_set_balance_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_balance_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_volume_midi_cc(ChannelObject *self, void *closure)
417 {
418         return PyInt_FromLong(channel_get_volume_midi_cc(self->channel));
419 }
420
421 static int
422 Channel_set_volume_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_volume_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 PyGetSetDef Channel_getseters[] = {
441         {"is_stereo", 
442                 (getter)Channel_get_is_stereo, NULL,
443                 "mono/stereo", NULL},
444         {"volume", 
445                 (getter)Channel_get_volume, (setter)Channel_set_volume,
446                 "volume", NULL},
447         {"balance", 
448                 (getter)Channel_get_balance, (setter)Channel_set_balance,
449                 "balance", NULL},
450         {"mute", 
451                 (getter)Channel_get_mute, (setter)Channel_set_mute,
452                 "mute", NULL},
453         {"solo", 
454                 (getter)Channel_get_solo, (setter)Channel_set_solo,
455                 "solo", NULL},
456         {"meter",
457                 (getter)Channel_get_meter, NULL,
458                 "meter", NULL},
459         {"abspeak", 
460                 (getter)Channel_get_abspeak, (setter)Channel_set_abspeak,
461                 "balance", NULL},
462         {"midi_scale",
463                 NULL, (setter)Channel_set_midi_scale,
464                 "midi scale", NULL},
465         {"midi_change_callback",
466                 (getter)Channel_get_midi_change_callback,
467                 (setter)Channel_set_midi_change_callback,
468                 "midi change callback", NULL},
469         {"name",
470                 (getter)Channel_get_name,
471                 (setter)Channel_set_name,
472                 "name", NULL},
473         {"balance_midi_cc",
474                 (getter)Channel_get_balance_midi_cc,
475                 (setter)Channel_set_balance_midi_cc,
476                 "Balance MIDI CC", NULL},
477         {"volume_midi_cc",
478                 (getter)Channel_get_volume_midi_cc,
479                 (setter)Channel_set_volume_midi_cc,
480                 "Volume MIDI CC", NULL},
481         {NULL}
482 };
483
484 static PyObject*
485 Channel_remove(ChannelObject *self, PyObject *args)
486 {
487         if (! PyArg_ParseTuple(args, "")) return NULL;
488         remove_channel(self->channel);
489         Py_INCREF(Py_None);
490         return Py_None;
491 }
492
493 static PyObject*
494 Channel_autoset_midi_cc(ChannelObject *self, PyObject *args)
495 {
496         if (! PyArg_ParseTuple(args, "")) return NULL;
497         channel_autoset_midi_cc(self->channel);
498         Py_INCREF(Py_None);
499         return Py_None;
500 }
501
502 static PyMethodDef channel_methods[] = {
503         {"remove", (PyCFunction)Channel_remove, METH_VARARGS, "Remove"},
504         {"autoset_midi_cc", (PyCFunction)Channel_autoset_midi_cc, METH_VARARGS, "Autoset MIDI CC"},
505         {NULL}
506 };
507
508 static PyTypeObject ChannelType = {
509         PyObject_HEAD_INIT(NULL)
510         0,       /*ob_size*/
511         "jack_mixer_c.Channel",    /*tp_name*/
512         sizeof(ChannelObject), /*tp_basicsize*/
513         0,       /*tp_itemsize*/
514         (destructor)Channel_dealloc,       /*tp_dealloc*/
515         0,       /*tp_print*/
516         0,       /*tp_getattr*/
517         0,       /*tp_setattr*/
518         0,       /*tp_compare*/
519         0,       /*tp_repr*/
520         0,       /*tp_as_number*/
521         0,       /*tp_as_sequence*/
522         0,       /*tp_as_mapping*/
523         0,       /*tp_hash */
524         0,       /*tp_call*/
525         0,       /*tp_str*/
526         0,       /*tp_getattro*/
527         0,       /*tp_setattro*/
528         0,       /*tp_as_buffer*/
529         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
530         "Channel objects",           /* tp_doc */
531         0,                         /* tp_traverse */
532         0,                         /* tp_clear */
533         0,                         /* tp_richcompare */
534         0,                         /* tp_weaklistoffset */
535         0,                         /* tp_iter */
536         0,                         /* tp_iternext */
537         channel_methods,           /* tp_methods */
538         0,             /* tp_members */
539         Channel_getseters,           /* tp_getset */
540         0,                         /* tp_base */
541         0,                         /* tp_dict */
542         0,                         /* tp_descr_get */
543         0,                         /* tp_descr_set */
544         0,                         /* tp_dictoffset */
545         (initproc)Channel_init,    /* tp_init */
546         0,                         /* tp_alloc */
547         Channel_new,                 /* tp_new */
548 };
549
550 static PyObject*
551 Channel_New(jack_mixer_channel_t channel)
552 {
553         ChannelObject *self;
554         self = (ChannelObject*)PyObject_NEW(ChannelObject, &ChannelType);
555         if (self != NULL) {
556                 self->channel = channel;
557                 self->midi_change_callback = NULL;
558         }
559         return (PyObject*)self;
560 }
561
562 /** Output Channel Type **/
563
564 typedef struct {
565         PyObject_HEAD
566         PyObject *midi_change_callback;
567         jack_mixer_output_channel_t *output_channel;
568 } OutputChannelObject;
569
570 static int
571 OutputChannel_set_prefader(OutputChannelObject *self, PyObject *value, void *closure)
572 {
573         if (value == Py_True) {
574                 output_channel_set_prefader(self->output_channel, true);
575         } else {
576                 output_channel_set_prefader(self->output_channel, false);
577         }
578         return 0;
579 }
580
581 static PyObject*
582 OutputChannel_get_prefader(OutputChannelObject *self, void *closure)
583 {
584         PyObject *result;
585
586         if (output_channel_is_prefader(self->output_channel)) {
587                 result = Py_True;
588         } else {
589                 result = Py_False;
590         }
591         Py_INCREF(result);
592         return result;
593 }
594
595 static PyGetSetDef OutputChannel_getseters[] = {
596         {"prefader", 
597                 (getter)OutputChannel_get_prefader, (setter)OutputChannel_set_prefader,
598                 "prefader", NULL},
599         {NULL}
600 };
601
602 static PyObject*
603 OutputChannel_remove(OutputChannelObject *self, PyObject *args)
604 {
605         if (! PyArg_ParseTuple(args, "")) return NULL;
606         remove_output_channel(self->output_channel);
607         Py_INCREF(Py_None);
608         return Py_None;
609 }
610
611 static PyObject*
612 OutputChannel_set_solo(OutputChannelObject *self, PyObject *args)
613 {
614         PyObject *channel;
615         unsigned char solo;
616
617         if (! PyArg_ParseTuple(args, "Ob", &channel, &solo)) return NULL;
618
619         output_channel_set_solo(self->output_channel,
620                         ((ChannelObject*)channel)->channel,
621                         solo);
622
623         Py_INCREF(Py_None);
624         return Py_None;
625 }
626
627 static PyObject*
628 OutputChannel_set_muted(OutputChannelObject *self, PyObject *args)
629 {
630         PyObject *channel;
631         unsigned char muted;
632
633         if (! PyArg_ParseTuple(args, "Ob", &channel, &muted)) return NULL;
634
635         output_channel_set_muted(self->output_channel,
636                         ((ChannelObject*)channel)->channel,
637                         muted);
638
639         Py_INCREF(Py_None);
640         return Py_None;
641 }
642
643 static PyObject*
644 OutputChannel_is_solo(OutputChannelObject *self, PyObject *args)
645 {
646         PyObject *channel;
647         PyObject *result;
648
649         if (! PyArg_ParseTuple(args, "O", &channel)) return NULL;
650
651         if (output_channel_is_solo(self->output_channel,
652                         ((ChannelObject*)channel)->channel)) {
653                 result = Py_True;
654         } else {
655                 result = Py_False;
656         }
657
658         Py_INCREF(result);
659         return result;
660 }
661
662 static PyObject*
663 OutputChannel_is_muted(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_muted(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 PyMethodDef output_channel_methods[] = {
682         {"remove", (PyCFunction)OutputChannel_remove, METH_VARARGS, "Remove"},
683         {"set_solo", (PyCFunction)OutputChannel_set_solo, METH_VARARGS, "Set a channel as solo"},
684         {"set_muted", (PyCFunction)OutputChannel_set_muted, METH_VARARGS, "Set a channel as muted"},
685         {"is_solo", (PyCFunction)OutputChannel_is_solo, METH_VARARGS, "Is a channel set as solo"},
686         {"is_muted", (PyCFunction)OutputChannel_is_muted, METH_VARARGS, "Is a channel set as muted"},
687         {NULL}
688 };
689
690 static PyTypeObject OutputChannelType = {
691         PyObject_HEAD_INIT(NULL)
692         0,       /*ob_size*/
693         "jack_mixer_c.OutputChannel",    /*tp_name*/
694         sizeof(OutputChannelObject), /*tp_basicsize*/
695         0,       /*tp_itemsize*/
696         0,       /*tp_dealloc*/
697         0,       /*tp_print*/
698         0,       /*tp_getattr*/
699         0,       /*tp_setattr*/
700         0,       /*tp_compare*/
701         0,       /*tp_repr*/
702         0,       /*tp_as_number*/
703         0,       /*tp_as_sequence*/
704         0,       /*tp_as_mapping*/
705         0,       /*tp_hash */
706         0,       /*tp_call*/
707         0,       /*tp_str*/
708         0,       /*tp_getattro*/
709         0,       /*tp_setattro*/
710         0,       /*tp_as_buffer*/
711         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
712         "Output Channel objects",           /* tp_doc */
713         0,                         /* tp_traverse */
714         0,                         /* tp_clear */
715         0,                         /* tp_richcompare */
716         0,                         /* tp_weaklistoffset */
717         0,                         /* tp_iter */
718         0,                         /* tp_iternext */
719         output_channel_methods,    /* tp_methods */
720         0,             /* tp_members */
721         OutputChannel_getseters,   /* tp_getset */
722         &ChannelType,              /* tp_base */
723         0,                         /* tp_dict */
724         0,                         /* tp_descr_get */
725         0,                         /* tp_descr_set */
726         0,                         /* tp_dictoffset */
727         0,                         /* tp_init */
728         0,                         /* tp_alloc */
729         0,                         /* tp_new */
730 };
731
732 static PyObject*
733 OutputChannel_New(jack_mixer_output_channel_t output_channel)
734 {
735         OutputChannelObject *self;
736         self = (OutputChannelObject*)PyObject_NEW(OutputChannelObject, &OutputChannelType);
737         if (self != NULL) {
738                 self->midi_change_callback = NULL;
739                 self->output_channel = output_channel;
740         }
741         return (PyObject*)self;
742 }
743
744
745 /** Mixer Type **/
746
747 typedef struct {
748         PyObject_HEAD
749         PyObject *main_mix_channel;
750         jack_mixer_t mixer;
751 } MixerObject;
752
753 static void
754 Mixer_dealloc(MixerObject *self)
755 {
756         Py_XDECREF(self->main_mix_channel);
757         if (self->mixer)
758                 destroy(self->mixer);
759         self->ob_type->tp_free((PyObject*)self);
760 }
761
762 static PyObject*
763 Mixer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
764 {
765         MixerObject *self;
766
767         self = (MixerObject*)type->tp_alloc(type, 0);
768
769         if (self != NULL) {
770                 self->main_mix_channel = NULL;
771                 self->mixer = NULL;
772         }
773
774         return (PyObject*)self;
775 }
776
777 static int
778 Mixer_init(MixerObject *self, PyObject *args, PyObject *kwds)
779 {
780         static char *kwlist[] = {"name", NULL};
781         char *name;
782
783         if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &name))
784                 return -1;
785         
786         self->mixer = create(name);
787         if (self->mixer == NULL) {
788                 PyErr_SetString(PyExc_RuntimeError,
789                                 "error creating mixer, probably jack is not running");
790                 return -1;
791         }
792
793         self->main_mix_channel = Channel_New(get_main_mix_channel(self->mixer));
794
795         return 0;
796 }
797
798 static PyMemberDef Mixer_members[] = {
799         {"main_mix_channel", T_OBJECT, offsetof(MixerObject, main_mix_channel), 0, "main_mix_channel"},
800         {NULL}
801 };
802
803 static PyObject*
804 Mixer_get_channels_count(MixerObject *self, void *closure)
805 {
806         return PyInt_FromLong(get_channels_count(self->mixer));
807 }
808
809 static PyObject*
810 Mixer_get_last_midi_channel(MixerObject *self, void *closure)
811 {
812         return PyInt_FromLong(get_last_midi_channel(self->mixer));
813 }
814
815
816 static PyGetSetDef Mixer_getseters[] = {
817         {"channels_count", (getter)Mixer_get_channels_count, NULL,
818                 "channels count", NULL},
819         {"last_midi_channel", (getter)Mixer_get_last_midi_channel, NULL,
820                 "last midi channel", NULL},
821         {NULL}
822 };
823
824 static PyObject*
825 Mixer_add_channel(MixerObject *self, PyObject *args)
826 {
827         char *name;
828         int stereo;
829         jack_mixer_channel_t channel;
830
831         if (! PyArg_ParseTuple(args, "sb", &name, &stereo)) return NULL;
832
833         channel = add_channel(self->mixer, name, (bool)stereo);
834
835         if (channel == NULL) {
836                 PyErr_SetString(PyExc_RuntimeError, "error adding channel");
837                 return NULL;
838         }
839
840         return Channel_New(channel);
841 }
842
843 static PyObject*
844 Mixer_add_output_channel(MixerObject *self, PyObject *args)
845 {
846         char *name;
847         int stereo = 1;
848         int system = 0;
849         jack_mixer_output_channel_t channel;
850
851         if (! PyArg_ParseTuple(args, "s|bb", &name, &stereo, &system)) return NULL;
852
853         channel = add_output_channel(self->mixer, name, (bool)stereo, (bool)system);
854
855         return OutputChannel_New(channel);
856 }
857
858
859 static PyMethodDef Mixer_methods[] = {
860         {"add_channel", (PyCFunction)Mixer_add_channel, METH_VARARGS, "Add a new channel"},
861         {"add_output_channel", (PyCFunction)Mixer_add_output_channel, METH_VARARGS, "Add a new output channel"},
862 //      {"remove_channel", (PyCFunction)Mixer_remove_channel, METH_VARARGS, "Remove a channel"},
863         {NULL}
864 };
865
866 static PyTypeObject MixerType = {
867         PyObject_HEAD_INIT(NULL)
868         0,       /*ob_size*/
869         "jack_mixer_c.Mixer",    /*tp_name*/
870         sizeof(MixerObject), /*tp_basicsize*/
871         0,       /*tp_itemsize*/
872         (destructor)Mixer_dealloc,       /*tp_dealloc*/
873         0,       /*tp_print*/
874         0,       /*tp_getattr*/
875         0,       /*tp_setattr*/
876         0,       /*tp_compare*/
877         0,       /*tp_repr*/
878         0,       /*tp_as_number*/
879         0,       /*tp_as_sequence*/
880         0,       /*tp_as_mapping*/
881         0,       /*tp_hash */
882         0,       /*tp_call*/
883         0,       /*tp_str*/
884         0,       /*tp_getattro*/
885         0,       /*tp_setattro*/
886         0,       /*tp_as_buffer*/
887         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
888         "Mixer objects",           /* tp_doc */
889         0,                         /* tp_traverse */
890         0,                         /* tp_clear */
891         0,                         /* tp_richcompare */
892         0,                         /* tp_weaklistoffset */
893         0,                         /* tp_iter */
894         0,                         /* tp_iternext */
895         Mixer_methods,             /* tp_methods */
896         Mixer_members,             /* tp_members */
897         Mixer_getseters,           /* tp_getset */
898         0,                         /* tp_base */
899         0,                         /* tp_dict */
900         0,                         /* tp_descr_get */
901         0,                         /* tp_descr_set */
902         0,                         /* tp_dictoffset */
903         (initproc)Mixer_init,      /* tp_init */
904         0,                         /* tp_alloc */
905         Mixer_new,                 /* tp_new */
906 };
907
908
909 static PyMethodDef jack_mixer_methods[] = {
910         {NULL}  /* Sentinel */
911 };
912
913
914
915 PyMODINIT_FUNC initjack_mixer_c(void)
916 {
917         PyObject *m;
918
919         if (PyType_Ready(&MixerType) < 0)
920                 return;
921         if (PyType_Ready(&ChannelType) < 0)
922                 return;
923         if (PyType_Ready(&OutputChannelType) < 0)
924                 return;
925         if (PyType_Ready(&ScaleType) < 0)
926                 return;
927         
928         m = Py_InitModule3("jack_mixer_c", jack_mixer_methods, "Jack Mixer C Helper Module");
929
930         Py_INCREF(&MixerType);
931         PyModule_AddObject(m, "Mixer", (PyObject*)&MixerType);
932         Py_INCREF(&ChannelType);
933         PyModule_AddObject(m, "Channel", (PyObject*)&ChannelType);
934         Py_INCREF(&OutputChannelType);
935         PyModule_AddObject(m, "OutputChannel", (PyObject*)&OutputChannelType);
936         Py_INCREF(&ScaleType);
937         PyModule_AddObject(m, "Scale", (PyObject*)&ScaleType);
938 }
939