~ubuntu-branches/debian/sid/python-pyo/sid

« back to all changes in this revision

Viewing changes to src/objects/matrixprocessmodule.c

  • Committer: Package Import Robot
  • Author(s): Tiago Bortoletto Vaz
  • Date: 2012-06-08 20:35:45 UTC
  • Revision ID: package-import@ubuntu.com-20120608203545-4z7kcf2lgvpsk18y
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
 * Copyright 2010 Olivier Belanger                                        *                  
 
3
 *                                                                        * 
 
4
 * This file is part of pyo, a python module to help digital signal       *
 
5
 * processing script creation.                                            *  
 
6
 *                                                                        * 
 
7
 * pyo is free software: you can redistribute it and/or modify            *
 
8
 * it under the terms of the GNU General Public License as published by   *
 
9
 * the Free Software Foundation, either version 3 of the License, or      *
 
10
 * (at your option) any later version.                                    * 
 
11
 *                                                                        *
 
12
 * pyo is distributed in the hope that it will be useful,                 *
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         *    
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
15
 * GNU General Public License for more details.                           *
 
16
 *                                                                        *
 
17
 * You should have received a copy of the GNU General Public License      *
 
18
 * along with pyo.  If not, see <http://www.gnu.org/licenses/>.           *
 
19
 *************************************************************************/
 
20
 
 
21
#include <Python.h>
 
22
#include "structmember.h"
 
23
#include "pyomodule.h"
 
24
#include "streammodule.h"
 
25
#include "servermodule.h"
 
26
#include "dummymodule.h"
 
27
#include "matrixmodule.h"
 
28
 
 
29
/**************/
 
30
/* MatrixPointer object */
 
31
/**************/
 
32
typedef struct {
 
33
    pyo_audio_HEAD
 
34
    PyObject *matrix;
 
35
    PyObject *x;
 
36
    Stream *x_stream;
 
37
    PyObject *y;
 
38
    Stream *y_stream;
 
39
    int modebuffer[2];
 
40
} MatrixPointer;
 
41
 
 
42
static void
 
43
MatrixPointer_readframes(MatrixPointer *self) {
 
44
    int i;
 
45
 
 
46
    MYFLT *x = Stream_getData((Stream *)self->x_stream);
 
47
    MYFLT *y = Stream_getData((Stream *)self->y_stream);
 
48
 
 
49
    for (i=0; i<self->bufsize; i++) {
 
50
        self->data[i] = MatrixStream_getInterpPointFromPos(self->matrix, x[i], y[i]);
 
51
    }
 
52
}
 
53
 
 
54
static void MatrixPointer_postprocessing_ii(MatrixPointer *self) { POST_PROCESSING_II };
 
55
static void MatrixPointer_postprocessing_ai(MatrixPointer *self) { POST_PROCESSING_AI };
 
56
static void MatrixPointer_postprocessing_ia(MatrixPointer *self) { POST_PROCESSING_IA };
 
57
static void MatrixPointer_postprocessing_aa(MatrixPointer *self) { POST_PROCESSING_AA };
 
58
static void MatrixPointer_postprocessing_ireva(MatrixPointer *self) { POST_PROCESSING_IREVA };
 
59
static void MatrixPointer_postprocessing_areva(MatrixPointer *self) { POST_PROCESSING_AREVA };
 
60
static void MatrixPointer_postprocessing_revai(MatrixPointer *self) { POST_PROCESSING_REVAI };
 
61
static void MatrixPointer_postprocessing_revaa(MatrixPointer *self) { POST_PROCESSING_REVAA };
 
62
static void MatrixPointer_postprocessing_revareva(MatrixPointer *self) { POST_PROCESSING_REVAREVA };
 
63
 
 
64
static void
 
65
MatrixPointer_setProcMode(MatrixPointer *self)
 
66
{
 
67
    int muladdmode;
 
68
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
69
 
 
70
    self->proc_func_ptr = MatrixPointer_readframes;
 
71
 
 
72
        switch (muladdmode) {
 
73
        case 0:        
 
74
            self->muladd_func_ptr = MatrixPointer_postprocessing_ii;
 
75
            break;
 
76
        case 1:    
 
77
            self->muladd_func_ptr = MatrixPointer_postprocessing_ai;
 
78
            break;
 
79
        case 2:    
 
80
            self->muladd_func_ptr = MatrixPointer_postprocessing_revai;
 
81
            break;
 
82
        case 10:        
 
83
            self->muladd_func_ptr = MatrixPointer_postprocessing_ia;
 
84
            break;
 
85
        case 11:    
 
86
            self->muladd_func_ptr = MatrixPointer_postprocessing_aa;
 
87
            break;
 
88
        case 12:    
 
89
            self->muladd_func_ptr = MatrixPointer_postprocessing_revaa;
 
90
            break;
 
91
        case 20:        
 
92
            self->muladd_func_ptr = MatrixPointer_postprocessing_ireva;
 
93
            break;
 
94
        case 21:    
 
95
            self->muladd_func_ptr = MatrixPointer_postprocessing_areva;
 
96
            break;
 
97
        case 22:    
 
98
            self->muladd_func_ptr = MatrixPointer_postprocessing_revareva;
 
99
            break;
 
100
    } 
 
101
}
 
102
 
 
103
static void
 
104
MatrixPointer_compute_next_data_frame(MatrixPointer *self)
 
105
{
 
106
    (*self->proc_func_ptr)(self); 
 
107
    (*self->muladd_func_ptr)(self);
 
108
}
 
109
 
 
110
static int
 
111
MatrixPointer_traverse(MatrixPointer *self, visitproc visit, void *arg)
 
112
{
 
113
    pyo_VISIT
 
114
    Py_VISIT(self->matrix);
 
115
    Py_VISIT(self->x);    
 
116
    Py_VISIT(self->x_stream);    
 
117
    Py_VISIT(self->y);    
 
118
    Py_VISIT(self->y_stream);    
 
119
    return 0;
 
120
}
 
121
 
 
122
static int 
 
123
MatrixPointer_clear(MatrixPointer *self)
 
124
{
 
125
    pyo_CLEAR
 
126
    Py_CLEAR(self->matrix);
 
127
    Py_CLEAR(self->x);    
 
128
    Py_CLEAR(self->x_stream);    
 
129
    Py_CLEAR(self->y);    
 
130
    Py_CLEAR(self->y_stream);    
 
131
    return 0;
 
132
}
 
133
 
 
134
static void
 
135
MatrixPointer_dealloc(MatrixPointer* self)
 
136
{
 
137
    free(self->data);
 
138
    MatrixPointer_clear(self);
 
139
    self->ob_type->tp_free((PyObject*)self);
 
140
}
 
141
 
 
142
static PyObject * MatrixPointer_deleteStream(MatrixPointer *self) { DELETE_STREAM };
 
143
 
 
144
static PyObject *
 
145
MatrixPointer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
146
{
 
147
    int i;
 
148
    MatrixPointer *self;
 
149
    self = (MatrixPointer *)type->tp_alloc(type, 0);
 
150
    
 
151
        self->modebuffer[0] = 0;
 
152
        self->modebuffer[1] = 0;
 
153
    
 
154
    INIT_OBJECT_COMMON
 
155
    Stream_setFunctionPtr(self->stream, MatrixPointer_compute_next_data_frame);
 
156
    self->mode_func_ptr = MatrixPointer_setProcMode;
 
157
    
 
158
    return (PyObject *)self;
 
159
}
 
160
 
 
161
static int
 
162
MatrixPointer_init(MatrixPointer *self, PyObject *args, PyObject *kwds)
 
163
{
 
164
    PyObject *matrixtmp, *xtmp, *ytmp, *multmp=NULL, *addtmp=NULL;
 
165
    
 
166
    static char *kwlist[] = {"matrix", "x", "y", "mul", "add", NULL};
 
167
    
 
168
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOO|OO", kwlist, &matrixtmp, &xtmp, &ytmp, &multmp, &addtmp))
 
169
        return -1; 
 
170
    
 
171
    Py_XDECREF(self->matrix);
 
172
    self->matrix = PyObject_CallMethod((PyObject *)matrixtmp, "getMatrixStream", "");
 
173
    
 
174
    if (xtmp) {
 
175
        PyObject_CallMethod((PyObject *)self, "setX", "O", xtmp);
 
176
    }
 
177
 
 
178
    if (ytmp) {
 
179
        PyObject_CallMethod((PyObject *)self, "setY", "O", ytmp);
 
180
    }
 
181
    
 
182
    if (multmp) {
 
183
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
184
    }
 
185
    
 
186
    if (addtmp) {
 
187
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
188
    }
 
189
    
 
190
    Py_INCREF(self->stream);
 
191
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
192
    
 
193
    (*self->mode_func_ptr)(self);
 
194
        
 
195
    Py_INCREF(self);
 
196
    return 0;
 
197
}
 
198
 
 
199
static PyObject * MatrixPointer_getServer(MatrixPointer* self) { GET_SERVER };
 
200
static PyObject * MatrixPointer_getStream(MatrixPointer* self) { GET_STREAM };
 
201
static PyObject * MatrixPointer_setMul(MatrixPointer *self, PyObject *arg) { SET_MUL }; 
 
202
static PyObject * MatrixPointer_setAdd(MatrixPointer *self, PyObject *arg) { SET_ADD }; 
 
203
static PyObject * MatrixPointer_setSub(MatrixPointer *self, PyObject *arg) { SET_SUB }; 
 
204
static PyObject * MatrixPointer_setDiv(MatrixPointer *self, PyObject *arg) { SET_DIV }; 
 
205
 
 
206
static PyObject * MatrixPointer_play(MatrixPointer *self, PyObject *args, PyObject *kwds) { PLAY };
 
207
static PyObject * MatrixPointer_out(MatrixPointer *self, PyObject *args, PyObject *kwds) { OUT };
 
208
static PyObject * MatrixPointer_stop(MatrixPointer *self) { STOP };
 
209
 
 
210
static PyObject * MatrixPointer_multiply(MatrixPointer *self, PyObject *arg) { MULTIPLY };
 
211
static PyObject * MatrixPointer_inplace_multiply(MatrixPointer *self, PyObject *arg) { INPLACE_MULTIPLY };
 
212
static PyObject * MatrixPointer_add(MatrixPointer *self, PyObject *arg) { ADD };
 
213
static PyObject * MatrixPointer_inplace_add(MatrixPointer *self, PyObject *arg) { INPLACE_ADD };
 
214
static PyObject * MatrixPointer_sub(MatrixPointer *self, PyObject *arg) { SUB };
 
215
static PyObject * MatrixPointer_inplace_sub(MatrixPointer *self, PyObject *arg) { INPLACE_SUB };
 
216
static PyObject * MatrixPointer_div(MatrixPointer *self, PyObject *arg) { DIV };
 
217
static PyObject * MatrixPointer_inplace_div(MatrixPointer *self, PyObject *arg) { INPLACE_DIV };
 
218
 
 
219
static PyObject *
 
220
MatrixPointer_getMatrix(MatrixPointer* self)
 
221
{
 
222
    Py_INCREF(self->matrix);
 
223
    return self->matrix;
 
224
};
 
225
 
 
226
static PyObject *
 
227
MatrixPointer_setMatrix(MatrixPointer *self, PyObject *arg)
 
228
{
 
229
        PyObject *tmp;
 
230
        
 
231
        if (arg == NULL) {
 
232
                Py_INCREF(Py_None);
 
233
                return Py_None;
 
234
        }
 
235
    
 
236
        tmp = arg;
 
237
        Py_DECREF(self->matrix);
 
238
    self->matrix = PyObject_CallMethod((PyObject *)tmp, "getMatrixStream", "");
 
239
    
 
240
        Py_INCREF(Py_None);
 
241
        return Py_None;
 
242
}       
 
243
 
 
244
static PyObject *
 
245
MatrixPointer_setX(MatrixPointer *self, PyObject *arg)
 
246
{
 
247
        PyObject *tmp, *streamtmp;
 
248
        
 
249
        if (arg == NULL) {
 
250
                Py_INCREF(Py_None);
 
251
                return Py_None;
 
252
        }
 
253
    
 
254
        int isNumber = PyNumber_Check(arg);
 
255
        if (isNumber == 1) {
 
256
                printf("MatrixPointer x attributes must be a PyoObject.\n");
 
257
        Py_INCREF(Py_None);
 
258
        return Py_None;
 
259
        }
 
260
        
 
261
        tmp = arg;
 
262
        Py_INCREF(tmp);
 
263
        Py_XDECREF(self->x);
 
264
 
 
265
    self->x = tmp;
 
266
    streamtmp = PyObject_CallMethod((PyObject *)self->x, "_getStream", NULL);
 
267
    Py_INCREF(streamtmp);
 
268
    Py_XDECREF(self->x_stream);
 
269
    self->x_stream = (Stream *)streamtmp;
 
270
    
 
271
        Py_INCREF(Py_None);
 
272
        return Py_None;
 
273
}       
 
274
 
 
275
static PyObject *
 
276
MatrixPointer_setY(MatrixPointer *self, PyObject *arg)
 
277
{
 
278
        PyObject *tmp, *streamtmp;
 
279
        
 
280
        if (arg == NULL) {
 
281
                Py_INCREF(Py_None);
 
282
                return Py_None;
 
283
        }
 
284
    
 
285
        int isNumber = PyNumber_Check(arg);
 
286
        if (isNumber == 1) {
 
287
                printf("MatrixPointer y attributes must be a PyoObject.\n");
 
288
        Py_INCREF(Py_None);
 
289
        return Py_None;
 
290
        }
 
291
        
 
292
        tmp = arg;
 
293
        Py_INCREF(tmp);
 
294
        Py_XDECREF(self->y);
 
295
    
 
296
    self->y = tmp;
 
297
    streamtmp = PyObject_CallMethod((PyObject *)self->y, "_getStream", NULL);
 
298
    Py_INCREF(streamtmp);
 
299
    Py_XDECREF(self->y_stream);
 
300
    self->y_stream = (Stream *)streamtmp;
 
301
    
 
302
        Py_INCREF(Py_None);
 
303
        return Py_None;
 
304
}       
 
305
 
 
306
static PyMemberDef MatrixPointer_members[] = {
 
307
{"server", T_OBJECT_EX, offsetof(MatrixPointer, server), 0, "Pyo server."},
 
308
{"stream", T_OBJECT_EX, offsetof(MatrixPointer, stream), 0, "Stream object."},
 
309
{"matrix", T_OBJECT_EX, offsetof(MatrixPointer, matrix), 0, "Waveform matrix."},
 
310
{"x", T_OBJECT_EX, offsetof(MatrixPointer, x), 0, "Reader x."},
 
311
{"y", T_OBJECT_EX, offsetof(MatrixPointer, y), 0, "Reader y."},
 
312
{"mul", T_OBJECT_EX, offsetof(MatrixPointer, mul), 0, "Mul factor."},
 
313
{"add", T_OBJECT_EX, offsetof(MatrixPointer, add), 0, "Add factor."},
 
314
{NULL}  /* Sentinel */
 
315
};
 
316
 
 
317
static PyMethodDef MatrixPointer_methods[] = {
 
318
{"getMatrix", (PyCFunction)MatrixPointer_getMatrix, METH_NOARGS, "Returns waveform matrix object."},
 
319
{"getServer", (PyCFunction)MatrixPointer_getServer, METH_NOARGS, "Returns server object."},
 
320
{"_getStream", (PyCFunction)MatrixPointer_getStream, METH_NOARGS, "Returns stream object."},
 
321
{"deleteStream", (PyCFunction)MatrixPointer_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
 
322
{"play", (PyCFunction)MatrixPointer_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
 
323
{"out", (PyCFunction)MatrixPointer_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
 
324
{"stop", (PyCFunction)MatrixPointer_stop, METH_NOARGS, "Stops computing."},
 
325
{"setMatrix", (PyCFunction)MatrixPointer_setMatrix, METH_O, "Sets oscillator matrix."},
 
326
{"setX", (PyCFunction)MatrixPointer_setX, METH_O, "Sets reader x."},
 
327
{"setY", (PyCFunction)MatrixPointer_setY, METH_O, "Sets reader y."},
 
328
{"setMul", (PyCFunction)MatrixPointer_setMul, METH_O, "Sets oscillator mul factor."},
 
329
{"setAdd", (PyCFunction)MatrixPointer_setAdd, METH_O, "Sets oscillator add factor."},
 
330
{"setSub", (PyCFunction)MatrixPointer_setSub, METH_O, "Sets oscillator inverse add factor."},
 
331
{"setDiv", (PyCFunction)MatrixPointer_setDiv, METH_O, "Sets inverse mul factor."},
 
332
{NULL}  /* Sentinel */
 
333
};
 
334
 
 
335
static PyNumberMethods MatrixPointer_as_number = {
 
336
(binaryfunc)MatrixPointer_add,                      /*nb_add*/
 
337
(binaryfunc)MatrixPointer_sub,                 /*nb_subtract*/
 
338
(binaryfunc)MatrixPointer_multiply,                 /*nb_multiply*/
 
339
(binaryfunc)MatrixPointer_div,                   /*nb_divide*/
 
340
0,                /*nb_remainder*/
 
341
0,                   /*nb_divmod*/
 
342
0,                   /*nb_power*/
 
343
0,                  /*nb_neg*/
 
344
0,                /*nb_pos*/
 
345
0,                  /*(unaryfunc)array_abs,*/
 
346
0,                    /*nb_nonzero*/
 
347
0,                    /*nb_invert*/
 
348
0,               /*nb_lshift*/
 
349
0,              /*nb_rshift*/
 
350
0,              /*nb_and*/
 
351
0,              /*nb_xor*/
 
352
0,               /*nb_or*/
 
353
0,                                          /*nb_coerce*/
 
354
0,                       /*nb_int*/
 
355
0,                      /*nb_long*/
 
356
0,                     /*nb_float*/
 
357
0,                       /*nb_oct*/
 
358
0,                       /*nb_hex*/
 
359
(binaryfunc)MatrixPointer_inplace_add,              /*inplace_add*/
 
360
(binaryfunc)MatrixPointer_inplace_sub,         /*inplace_subtract*/
 
361
(binaryfunc)MatrixPointer_inplace_multiply,         /*inplace_multiply*/
 
362
(binaryfunc)MatrixPointer_inplace_div,           /*inplace_divide*/
 
363
0,        /*inplace_remainder*/
 
364
0,           /*inplace_power*/
 
365
0,       /*inplace_lshift*/
 
366
0,      /*inplace_rshift*/
 
367
0,      /*inplace_and*/
 
368
0,      /*inplace_xor*/
 
369
0,       /*inplace_or*/
 
370
0,             /*nb_floor_divide*/
 
371
0,              /*nb_true_divide*/
 
372
0,     /*nb_inplace_floor_divide*/
 
373
0,      /*nb_inplace_true_divide*/
 
374
0,                     /* nb_x */
 
375
};
 
376
 
 
377
PyTypeObject MatrixPointerType = {
 
378
PyObject_HEAD_INIT(NULL)
 
379
0,                         /*ob_size*/
 
380
"_pyo.MatrixPointer_base",         /*tp_name*/
 
381
sizeof(MatrixPointer),         /*tp_basicsize*/
 
382
0,                         /*tp_itemsize*/
 
383
(destructor)MatrixPointer_dealloc, /*tp_dealloc*/
 
384
0,                         /*tp_print*/
 
385
0,                         /*tp_getattr*/
 
386
0,                         /*tp_setattr*/
 
387
0,                         /*tp_compare*/
 
388
0,                         /*tp_repr*/
 
389
&MatrixPointer_as_number,             /*tp_as_number*/
 
390
0,                         /*tp_as_sequence*/
 
391
0,                         /*tp_as_mapping*/
 
392
0,                         /*tp_hash */
 
393
0,                         /*tp_call*/
 
394
0,                         /*tp_str*/
 
395
0,                         /*tp_getattro*/
 
396
0,                         /*tp_setattro*/
 
397
0,                         /*tp_as_buffer*/
 
398
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
 
399
"MatrixPointer objects. Read a waveform matrix with a pointer x.",           /* tp_doc */
 
400
(traverseproc)MatrixPointer_traverse,   /* tp_traverse */
 
401
(inquiry)MatrixPointer_clear,           /* tp_clear */
 
402
0,                             /* tp_richcompare */
 
403
0,                             /* tp_weaklistoffset */
 
404
0,                             /* tp_iter */
 
405
0,                             /* tp_iternext */
 
406
MatrixPointer_methods,             /* tp_methods */
 
407
MatrixPointer_members,             /* tp_members */
 
408
0,                      /* tp_getset */
 
409
0,                         /* tp_base */
 
410
0,                         /* tp_dict */
 
411
0,                         /* tp_descr_get */
 
412
0,                         /* tp_descr_set */
 
413
0,                         /* tp_dictoffset */
 
414
(initproc)MatrixPointer_init,      /* tp_init */
 
415
0,                         /* tp_alloc */
 
416
MatrixPointer_new,                 /* tp_new */
 
417
};