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

« back to all changes in this revision

Viewing changes to src/objects/noisemodule.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
 
 
28
typedef struct {
 
29
    pyo_audio_HEAD
 
30
    int modebuffer[2];
 
31
    int seed;
 
32
    int type;
 
33
} Noise;
 
34
 
 
35
static void
 
36
Noise_generate(Noise *self) {
 
37
    int i;
 
38
 
 
39
    for (i=0; i<self->bufsize; i++) {
 
40
        self->data[i] = rand()/((MYFLT)(RAND_MAX)+1)*1.98-0.99;
 
41
    }
 
42
}
 
43
 
 
44
static void
 
45
Noise_generate_cheap(Noise *self) {
 
46
    int i;
 
47
    
 
48
    for (i=0; i<self->bufsize; i++) {
 
49
        self->seed = (self->seed * 15625 + 1) & 0xFFFF;
 
50
        self->data[i] = (self->seed - 0x8000) * 3.0517578125e-05;
 
51
    }
 
52
}
 
53
 
 
54
static void Noise_postprocessing_ii(Noise *self) { POST_PROCESSING_II };
 
55
static void Noise_postprocessing_ai(Noise *self) { POST_PROCESSING_AI };
 
56
static void Noise_postprocessing_ia(Noise *self) { POST_PROCESSING_IA };
 
57
static void Noise_postprocessing_aa(Noise *self) { POST_PROCESSING_AA };
 
58
static void Noise_postprocessing_ireva(Noise *self) { POST_PROCESSING_IREVA };
 
59
static void Noise_postprocessing_areva(Noise *self) { POST_PROCESSING_AREVA };
 
60
static void Noise_postprocessing_revai(Noise *self) { POST_PROCESSING_REVAI };
 
61
static void Noise_postprocessing_revaa(Noise *self) { POST_PROCESSING_REVAA };
 
62
static void Noise_postprocessing_revareva(Noise *self) { POST_PROCESSING_REVAREVA };
 
63
 
 
64
static void
 
65
Noise_setProcMode(Noise *self)
 
66
{
 
67
    int muladdmode;
 
68
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
69
    
 
70
    switch (self->type) {
 
71
        case 0:
 
72
            self->proc_func_ptr = Noise_generate;
 
73
            break;
 
74
        case 1:
 
75
            self->proc_func_ptr = Noise_generate_cheap;
 
76
            break;
 
77
    }
 
78
        switch (muladdmode) {
 
79
        case 0:        
 
80
            self->muladd_func_ptr = Noise_postprocessing_ii;
 
81
            break;
 
82
        case 1:    
 
83
            self->muladd_func_ptr = Noise_postprocessing_ai;
 
84
            break;
 
85
        case 2:    
 
86
            self->muladd_func_ptr = Noise_postprocessing_revai;
 
87
            break;
 
88
        case 10:        
 
89
            self->muladd_func_ptr = Noise_postprocessing_ia;
 
90
            break;
 
91
        case 11:    
 
92
            self->muladd_func_ptr = Noise_postprocessing_aa;
 
93
            break;
 
94
        case 12:    
 
95
            self->muladd_func_ptr = Noise_postprocessing_revaa;
 
96
            break;
 
97
        case 20:        
 
98
            self->muladd_func_ptr = Noise_postprocessing_ireva;
 
99
            break;
 
100
        case 21:    
 
101
            self->muladd_func_ptr = Noise_postprocessing_areva;
 
102
            break;
 
103
        case 22:    
 
104
            self->muladd_func_ptr = Noise_postprocessing_revareva;
 
105
            break;
 
106
    }
 
107
}
 
108
 
 
109
static void
 
110
Noise_compute_next_data_frame(Noise *self)
 
111
{
 
112
    (*self->proc_func_ptr)(self); 
 
113
    (*self->muladd_func_ptr)(self);
 
114
}
 
115
 
 
116
static int
 
117
Noise_traverse(Noise *self, visitproc visit, void *arg)
 
118
{
 
119
    pyo_VISIT
 
120
    return 0;
 
121
}
 
122
 
 
123
static int 
 
124
Noise_clear(Noise *self)
 
125
{
 
126
    pyo_CLEAR
 
127
    return 0;
 
128
}
 
129
 
 
130
static void
 
131
Noise_dealloc(Noise* self)
 
132
{
 
133
    free(self->data);
 
134
    Noise_clear(self);
 
135
    self->ob_type->tp_free((PyObject*)self);
 
136
}
 
137
 
 
138
static PyObject * Noise_deleteStream(Noise *self) { DELETE_STREAM };
 
139
 
 
140
static PyObject *
 
141
Noise_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
142
{
 
143
    int i;
 
144
    Noise *self;
 
145
    self = (Noise *)type->tp_alloc(type, 0);
 
146
    
 
147
    self->type = 0;
 
148
        self->modebuffer[0] = 0;
 
149
        self->modebuffer[1] = 0;
 
150
 
 
151
    INIT_OBJECT_COMMON
 
152
    Stream_setFunctionPtr(self->stream, Noise_compute_next_data_frame);
 
153
    self->mode_func_ptr = Noise_setProcMode;
 
154
    
 
155
    return (PyObject *)self;
 
156
}
 
157
 
 
158
static int
 
159
Noise_init(Noise *self, PyObject *args, PyObject *kwds)
 
160
{
 
161
    PyObject *multmp=NULL, *addtmp=NULL;
 
162
    
 
163
    static char *kwlist[] = {"mul", "add", NULL};
 
164
    
 
165
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &multmp, &addtmp))
 
166
        return -1; 
 
167
 
 
168
    if (multmp) {
 
169
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
170
    }
 
171
    
 
172
    if (addtmp) {
 
173
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
174
    }
 
175
    
 
176
    Py_INCREF(self->stream);
 
177
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
178
 
 
179
    Server_generateSeed((Server *)self->server, NOISE_ID);
 
180
    
 
181
    self->seed = rand();
 
182
 
 
183
    (*self->mode_func_ptr)(self);
 
184
 
 
185
    Py_INCREF(self);
 
186
    return 0;
 
187
}
 
188
 
 
189
static PyObject * Noise_getServer(Noise* self) { GET_SERVER };
 
190
static PyObject * Noise_getStream(Noise* self) { GET_STREAM };
 
191
static PyObject * Noise_setMul(Noise *self, PyObject *arg) { SET_MUL }; 
 
192
static PyObject * Noise_setAdd(Noise *self, PyObject *arg) { SET_ADD }; 
 
193
static PyObject * Noise_setSub(Noise *self, PyObject *arg) { SET_SUB }; 
 
194
static PyObject * Noise_setDiv(Noise *self, PyObject *arg) { SET_DIV }; 
 
195
 
 
196
static PyObject * Noise_play(Noise *self, PyObject *args, PyObject *kwds) { PLAY };
 
197
static PyObject * Noise_out(Noise *self, PyObject *args, PyObject *kwds) { OUT };
 
198
static PyObject * Noise_stop(Noise *self) { STOP };
 
199
 
 
200
static PyObject * Noise_multiply(Noise *self, PyObject *arg) { MULTIPLY };
 
201
static PyObject * Noise_inplace_multiply(Noise *self, PyObject *arg) { INPLACE_MULTIPLY };
 
202
static PyObject * Noise_add(Noise *self, PyObject *arg) { ADD };
 
203
static PyObject * Noise_inplace_add(Noise *self, PyObject *arg) { INPLACE_ADD };
 
204
static PyObject * Noise_sub(Noise *self, PyObject *arg) { SUB };
 
205
static PyObject * Noise_inplace_sub(Noise *self, PyObject *arg) { INPLACE_SUB };
 
206
static PyObject * Noise_div(Noise *self, PyObject *arg) { DIV };
 
207
static PyObject * Noise_inplace_div(Noise *self, PyObject *arg) { INPLACE_DIV };
 
208
 
 
209
static PyObject *
 
210
Noise_setType(Noise *self, PyObject *arg)
 
211
{       
 
212
    if (arg == NULL) {
 
213
        Py_INCREF(Py_None);
 
214
        return Py_None;
 
215
    }
 
216
    
 
217
    if (PyInt_AS_LONG(arg) == 0)
 
218
        self->type = 0;
 
219
    else if (PyInt_AS_LONG(arg) == 1)
 
220
        self->type = 1;
 
221
 
 
222
    (*self->mode_func_ptr)(self);
 
223
 
 
224
    Py_INCREF(Py_None);
 
225
    return Py_None;
 
226
}       
 
227
 
 
228
static PyMemberDef Noise_members[] = {
 
229
{"server", T_OBJECT_EX, offsetof(Noise, server), 0, "Pyo server."},
 
230
{"stream", T_OBJECT_EX, offsetof(Noise, stream), 0, "Stream object."},
 
231
{"mul", T_OBJECT_EX, offsetof(Noise, mul), 0, "Mul factor."},
 
232
{"add", T_OBJECT_EX, offsetof(Noise, add), 0, "Add factor."},
 
233
{NULL}  /* Sentinel */
 
234
};
 
235
 
 
236
static PyMethodDef Noise_methods[] = {
 
237
{"getServer", (PyCFunction)Noise_getServer, METH_NOARGS, "Returns server object."},
 
238
{"_getStream", (PyCFunction)Noise_getStream, METH_NOARGS, "Returns stream object."},
 
239
{"deleteStream", (PyCFunction)Noise_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
 
240
{"play", (PyCFunction)Noise_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
 
241
{"out", (PyCFunction)Noise_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
 
242
{"stop", (PyCFunction)Noise_stop, METH_NOARGS, "Stops computing."},
 
243
{"setType", (PyCFunction)Noise_setType, METH_O, "Sets Noise generation algorithm."},
 
244
{"setMul", (PyCFunction)Noise_setMul, METH_O, "Sets Noise mul factor."},
 
245
{"setAdd", (PyCFunction)Noise_setAdd, METH_O, "Sets Noise add factor."},
 
246
{"setSub", (PyCFunction)Noise_setSub, METH_O, "Sets inverse add factor."},
 
247
{"setDiv", (PyCFunction)Noise_setDiv, METH_O, "Sets inverse mul factor."},
 
248
{NULL}  /* Sentinel */
 
249
};
 
250
 
 
251
static PyNumberMethods Noise_as_number = {
 
252
(binaryfunc)Noise_add,                      /*nb_add*/
 
253
(binaryfunc)Noise_sub,                 /*nb_subtract*/
 
254
(binaryfunc)Noise_multiply,                 /*nb_multiply*/
 
255
(binaryfunc)Noise_div,                   /*nb_divide*/
 
256
0,                /*nb_remainder*/
 
257
0,                   /*nb_divmod*/
 
258
0,                   /*nb_power*/
 
259
0,                  /*nb_neg*/
 
260
0,                /*nb_pos*/
 
261
0,                  /*(unaryfunc)array_abs,*/
 
262
0,                    /*nb_nonzero*/
 
263
0,                    /*nb_invert*/
 
264
0,               /*nb_lshift*/
 
265
0,              /*nb_rshift*/
 
266
0,              /*nb_and*/
 
267
0,              /*nb_xor*/
 
268
0,               /*nb_or*/
 
269
0,                                          /*nb_coerce*/
 
270
0,                       /*nb_int*/
 
271
0,                      /*nb_long*/
 
272
0,                     /*nb_float*/
 
273
0,                       /*nb_oct*/
 
274
0,                       /*nb_hex*/
 
275
(binaryfunc)Noise_inplace_add,              /*inplace_add*/
 
276
(binaryfunc)Noise_inplace_sub,         /*inplace_subtract*/
 
277
(binaryfunc)Noise_inplace_multiply,         /*inplace_multiply*/
 
278
(binaryfunc)Noise_inplace_div,           /*inplace_divide*/
 
279
0,        /*inplace_remainder*/
 
280
0,           /*inplace_power*/
 
281
0,       /*inplace_lshift*/
 
282
0,      /*inplace_rshift*/
 
283
0,      /*inplace_and*/
 
284
0,      /*inplace_xor*/
 
285
0,       /*inplace_or*/
 
286
0,             /*nb_floor_divide*/
 
287
0,              /*nb_true_divide*/
 
288
0,     /*nb_inplace_floor_divide*/
 
289
0,      /*nb_inplace_true_divide*/
 
290
0,                     /* nb_index */
 
291
};
 
292
 
 
293
PyTypeObject NoiseType = {
 
294
PyObject_HEAD_INIT(NULL)
 
295
0,                         /*ob_size*/
 
296
"_pyo.Noise_base",         /*tp_name*/
 
297
sizeof(Noise),         /*tp_basicsize*/
 
298
0,                         /*tp_itemsize*/
 
299
(destructor)Noise_dealloc, /*tp_dealloc*/
 
300
0,                         /*tp_print*/
 
301
0,                         /*tp_getattr*/
 
302
0,                         /*tp_setattr*/
 
303
0,                         /*tp_compare*/
 
304
0,                         /*tp_repr*/
 
305
&Noise_as_number,             /*tp_as_number*/
 
306
0,                         /*tp_as_sequence*/
 
307
0,                         /*tp_as_mapping*/
 
308
0,                         /*tp_hash */
 
309
0,                         /*tp_call*/
 
310
0,                         /*tp_str*/
 
311
0,                         /*tp_getattro*/
 
312
0,                         /*tp_setattro*/
 
313
0,                         /*tp_as_buffer*/
 
314
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
 
315
"Noise objects. White noise generator.",           /* tp_doc */
 
316
(traverseproc)Noise_traverse,   /* tp_traverse */
 
317
(inquiry)Noise_clear,           /* tp_clear */
 
318
0,                             /* tp_richcompare */
 
319
0,                             /* tp_weaklistoffset */
 
320
0,                             /* tp_iter */
 
321
0,                             /* tp_iternext */
 
322
Noise_methods,             /* tp_methods */
 
323
Noise_members,             /* tp_members */
 
324
0,                      /* tp_getset */
 
325
0,                         /* tp_base */
 
326
0,                         /* tp_dict */
 
327
0,                         /* tp_descr_get */
 
328
0,                         /* tp_descr_set */
 
329
0,                         /* tp_dictoffset */
 
330
(initproc)Noise_init,      /* tp_init */
 
331
0,                         /* tp_alloc */
 
332
Noise_new,                 /* tp_new */
 
333
};
 
334
 
 
335
typedef struct {
 
336
    pyo_audio_HEAD
 
337
    int modebuffer[2];
 
338
    MYFLT c0;
 
339
    MYFLT c1;
 
340
    MYFLT c2;
 
341
    MYFLT c3;
 
342
    MYFLT c4;
 
343
    MYFLT c5;
 
344
    MYFLT c6;
 
345
} PinkNoise;
 
346
 
 
347
static void
 
348
PinkNoise_generate(PinkNoise *self) {
 
349
    MYFLT in, val;
 
350
    int i;
 
351
    
 
352
    for (i=0; i<self->bufsize; i++) {
 
353
        in = rand()/((MYFLT)(RAND_MAX)+1)*1.98-0.99;
 
354
        self->c0 = self->c0 * 0.99886 + in * 0.0555179;
 
355
        self->c1 = self->c1 * 0.99332 + in * 0.0750759;
 
356
        self->c2 = self->c2 * 0.96900 + in * 0.1538520;
 
357
        self->c3 = self->c3 * 0.86650 + in * 0.3104856;
 
358
        self->c4 = self->c4 * 0.55000 + in * 0.5329522;
 
359
        self->c5 = self->c5 * -0.7616 - in * 0.0168980;
 
360
        val = self->c0 + self->c1 + self->c2 + self->c3 + self->c4 + self->c5 + self->c6 + in * 0.5362;
 
361
        self->data[i] = val * 0.2;
 
362
        self->c6 = in * 0.115926;
 
363
    }
 
364
}
 
365
 
 
366
static void PinkNoise_postprocessing_ii(PinkNoise *self) { POST_PROCESSING_II };
 
367
static void PinkNoise_postprocessing_ai(PinkNoise *self) { POST_PROCESSING_AI };
 
368
static void PinkNoise_postprocessing_ia(PinkNoise *self) { POST_PROCESSING_IA };
 
369
static void PinkNoise_postprocessing_aa(PinkNoise *self) { POST_PROCESSING_AA };
 
370
static void PinkNoise_postprocessing_ireva(PinkNoise *self) { POST_PROCESSING_IREVA };
 
371
static void PinkNoise_postprocessing_areva(PinkNoise *self) { POST_PROCESSING_AREVA };
 
372
static void PinkNoise_postprocessing_revai(PinkNoise *self) { POST_PROCESSING_REVAI };
 
373
static void PinkNoise_postprocessing_revaa(PinkNoise *self) { POST_PROCESSING_REVAA };
 
374
static void PinkNoise_postprocessing_revareva(PinkNoise *self) { POST_PROCESSING_REVAREVA };
 
375
 
 
376
static void
 
377
PinkNoise_setProcMode(PinkNoise *self)
 
378
{
 
379
    int muladdmode;
 
380
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
381
    
 
382
        switch (muladdmode) {
 
383
        case 0:        
 
384
            self->muladd_func_ptr = PinkNoise_postprocessing_ii;
 
385
            break;
 
386
        case 1:    
 
387
            self->muladd_func_ptr = PinkNoise_postprocessing_ai;
 
388
            break;
 
389
        case 2:    
 
390
            self->muladd_func_ptr = PinkNoise_postprocessing_revai;
 
391
            break;
 
392
        case 10:        
 
393
            self->muladd_func_ptr = PinkNoise_postprocessing_ia;
 
394
            break;
 
395
        case 11:    
 
396
            self->muladd_func_ptr = PinkNoise_postprocessing_aa;
 
397
            break;
 
398
        case 12:    
 
399
            self->muladd_func_ptr = PinkNoise_postprocessing_revaa;
 
400
            break;
 
401
        case 20:        
 
402
            self->muladd_func_ptr = PinkNoise_postprocessing_ireva;
 
403
            break;
 
404
        case 21:    
 
405
            self->muladd_func_ptr = PinkNoise_postprocessing_areva;
 
406
            break;
 
407
        case 22:    
 
408
            self->muladd_func_ptr = PinkNoise_postprocessing_revareva;
 
409
            break;
 
410
    }
 
411
}
 
412
 
 
413
static void
 
414
PinkNoise_compute_next_data_frame(PinkNoise *self)
 
415
{
 
416
    PinkNoise_generate(self); 
 
417
    (*self->muladd_func_ptr)(self);
 
418
}
 
419
 
 
420
static int
 
421
PinkNoise_traverse(PinkNoise *self, visitproc visit, void *arg)
 
422
{
 
423
    pyo_VISIT
 
424
    return 0;
 
425
}
 
426
 
 
427
static int 
 
428
PinkNoise_clear(PinkNoise *self)
 
429
{
 
430
    pyo_CLEAR
 
431
    return 0;
 
432
}
 
433
 
 
434
static void
 
435
PinkNoise_dealloc(PinkNoise* self)
 
436
{
 
437
    free(self->data);
 
438
    PinkNoise_clear(self);
 
439
    self->ob_type->tp_free((PyObject*)self);
 
440
}
 
441
 
 
442
static PyObject * PinkNoise_deleteStream(PinkNoise *self) { DELETE_STREAM };
 
443
 
 
444
static PyObject *
 
445
PinkNoise_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
446
{
 
447
    int i;
 
448
    PinkNoise *self;
 
449
    self = (PinkNoise *)type->tp_alloc(type, 0);
 
450
    
 
451
    self->c0 = self->c1 = self->c2 = self->c3 = self->c4 = self->c5 = self->c6 = 0.0;
 
452
        self->modebuffer[0] = 0;
 
453
        self->modebuffer[1] = 0;
 
454
    
 
455
    INIT_OBJECT_COMMON
 
456
    Stream_setFunctionPtr(self->stream, PinkNoise_compute_next_data_frame);
 
457
    self->mode_func_ptr = PinkNoise_setProcMode;
 
458
    
 
459
    return (PyObject *)self;
 
460
}
 
461
 
 
462
static int
 
463
PinkNoise_init(PinkNoise *self, PyObject *args, PyObject *kwds)
 
464
{
 
465
    PyObject *multmp=NULL, *addtmp=NULL;
 
466
    
 
467
    static char *kwlist[] = {"mul", "add", NULL};
 
468
    
 
469
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &multmp, &addtmp))
 
470
        return -1; 
 
471
    
 
472
    if (multmp) {
 
473
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
474
    }
 
475
    
 
476
    if (addtmp) {
 
477
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
478
    }
 
479
    
 
480
    Py_INCREF(self->stream);
 
481
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
482
    
 
483
    (*self->mode_func_ptr)(self);
 
484
    
 
485
    Server_generateSeed((Server *)self->server, PINKNOISE_ID);
 
486
    
 
487
    Py_INCREF(self);
 
488
    return 0;
 
489
}
 
490
 
 
491
static PyObject * PinkNoise_getServer(PinkNoise* self) { GET_SERVER };
 
492
static PyObject * PinkNoise_getStream(PinkNoise* self) { GET_STREAM };
 
493
static PyObject * PinkNoise_setMul(PinkNoise *self, PyObject *arg) { SET_MUL }; 
 
494
static PyObject * PinkNoise_setAdd(PinkNoise *self, PyObject *arg) { SET_ADD }; 
 
495
static PyObject * PinkNoise_setSub(PinkNoise *self, PyObject *arg) { SET_SUB }; 
 
496
static PyObject * PinkNoise_setDiv(PinkNoise *self, PyObject *arg) { SET_DIV }; 
 
497
 
 
498
static PyObject * PinkNoise_play(PinkNoise *self, PyObject *args, PyObject *kwds) { PLAY };
 
499
static PyObject * PinkNoise_out(PinkNoise *self, PyObject *args, PyObject *kwds) { OUT };
 
500
static PyObject * PinkNoise_stop(PinkNoise *self) { STOP };
 
501
 
 
502
static PyObject * PinkNoise_multiply(PinkNoise *self, PyObject *arg) { MULTIPLY };
 
503
static PyObject * PinkNoise_inplace_multiply(PinkNoise *self, PyObject *arg) { INPLACE_MULTIPLY };
 
504
static PyObject * PinkNoise_add(PinkNoise *self, PyObject *arg) { ADD };
 
505
static PyObject * PinkNoise_inplace_add(PinkNoise *self, PyObject *arg) { INPLACE_ADD };
 
506
static PyObject * PinkNoise_sub(PinkNoise *self, PyObject *arg) { SUB };
 
507
static PyObject * PinkNoise_inplace_sub(PinkNoise *self, PyObject *arg) { INPLACE_SUB };
 
508
static PyObject * PinkNoise_div(PinkNoise *self, PyObject *arg) { DIV };
 
509
static PyObject * PinkNoise_inplace_div(PinkNoise *self, PyObject *arg) { INPLACE_DIV };
 
510
 
 
511
static PyMemberDef PinkNoise_members[] = {
 
512
    {"server", T_OBJECT_EX, offsetof(PinkNoise, server), 0, "Pyo server."},
 
513
    {"stream", T_OBJECT_EX, offsetof(PinkNoise, stream), 0, "Stream object."},
 
514
    {"mul", T_OBJECT_EX, offsetof(PinkNoise, mul), 0, "Mul factor."},
 
515
    {"add", T_OBJECT_EX, offsetof(PinkNoise, add), 0, "Add factor."},
 
516
    {NULL}  /* Sentinel */
 
517
};
 
518
 
 
519
static PyMethodDef PinkNoise_methods[] = {
 
520
    {"getServer", (PyCFunction)PinkNoise_getServer, METH_NOARGS, "Returns server object."},
 
521
    {"_getStream", (PyCFunction)PinkNoise_getStream, METH_NOARGS, "Returns stream object."},
 
522
    {"deleteStream", (PyCFunction)PinkNoise_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
 
523
    {"play", (PyCFunction)PinkNoise_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
 
524
    {"out", (PyCFunction)PinkNoise_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
 
525
    {"stop", (PyCFunction)PinkNoise_stop, METH_NOARGS, "Stops computing."},
 
526
    {"setMul", (PyCFunction)PinkNoise_setMul, METH_O, "Sets PinkNoise mul factor."},
 
527
    {"setAdd", (PyCFunction)PinkNoise_setAdd, METH_O, "Sets PinkNoise add factor."},
 
528
    {"setSub", (PyCFunction)PinkNoise_setSub, METH_O, "Sets inverse add factor."},
 
529
    {"setDiv", (PyCFunction)PinkNoise_setDiv, METH_O, "Sets inverse mul factor."},
 
530
    {NULL}  /* Sentinel */
 
531
};
 
532
 
 
533
static PyNumberMethods PinkNoise_as_number = {
 
534
    (binaryfunc)PinkNoise_add,                      /*nb_add*/
 
535
    (binaryfunc)PinkNoise_sub,                 /*nb_subtract*/
 
536
    (binaryfunc)PinkNoise_multiply,                 /*nb_multiply*/
 
537
    (binaryfunc)PinkNoise_div,                   /*nb_divide*/
 
538
    0,                /*nb_remainder*/
 
539
    0,                   /*nb_divmod*/
 
540
    0,                   /*nb_power*/
 
541
    0,                  /*nb_neg*/
 
542
    0,                /*nb_pos*/
 
543
    0,                  /*(unaryfunc)array_abs,*/
 
544
    0,                    /*nb_nonzero*/
 
545
    0,                    /*nb_invert*/
 
546
    0,               /*nb_lshift*/
 
547
    0,              /*nb_rshift*/
 
548
    0,              /*nb_and*/
 
549
    0,              /*nb_xor*/
 
550
    0,               /*nb_or*/
 
551
    0,                                          /*nb_coerce*/
 
552
    0,                       /*nb_int*/
 
553
    0,                      /*nb_long*/
 
554
    0,                     /*nb_float*/
 
555
    0,                       /*nb_oct*/
 
556
    0,                       /*nb_hex*/
 
557
    (binaryfunc)PinkNoise_inplace_add,              /*inplace_add*/
 
558
    (binaryfunc)PinkNoise_inplace_sub,         /*inplace_subtract*/
 
559
    (binaryfunc)PinkNoise_inplace_multiply,         /*inplace_multiply*/
 
560
    (binaryfunc)PinkNoise_inplace_div,           /*inplace_divide*/
 
561
    0,        /*inplace_remainder*/
 
562
    0,           /*inplace_power*/
 
563
    0,       /*inplace_lshift*/
 
564
    0,      /*inplace_rshift*/
 
565
    0,      /*inplace_and*/
 
566
    0,      /*inplace_xor*/
 
567
    0,       /*inplace_or*/
 
568
    0,             /*nb_floor_divide*/
 
569
    0,              /*nb_true_divide*/
 
570
    0,     /*nb_inplace_floor_divide*/
 
571
    0,      /*nb_inplace_true_divide*/
 
572
    0,                     /* nb_index */
 
573
};
 
574
 
 
575
PyTypeObject PinkNoiseType = {
 
576
    PyObject_HEAD_INIT(NULL)
 
577
    0,                         /*ob_size*/
 
578
    "_pyo.PinkNoise_base",         /*tp_name*/
 
579
    sizeof(PinkNoise),         /*tp_basicsize*/
 
580
    0,                         /*tp_itemsize*/
 
581
    (destructor)PinkNoise_dealloc, /*tp_dealloc*/
 
582
    0,                         /*tp_print*/
 
583
    0,                         /*tp_getattr*/
 
584
    0,                         /*tp_setattr*/
 
585
    0,                         /*tp_compare*/
 
586
    0,                         /*tp_repr*/
 
587
    &PinkNoise_as_number,             /*tp_as_number*/
 
588
    0,                         /*tp_as_sequence*/
 
589
    0,                         /*tp_as_mapping*/
 
590
    0,                         /*tp_hash */
 
591
    0,                         /*tp_call*/
 
592
    0,                         /*tp_str*/
 
593
    0,                         /*tp_getattro*/
 
594
    0,                         /*tp_setattro*/
 
595
    0,                         /*tp_as_buffer*/
 
596
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
 
597
    "PinkNoise objects. Pink noise generator.",           /* tp_doc */
 
598
    (traverseproc)PinkNoise_traverse,   /* tp_traverse */
 
599
    (inquiry)PinkNoise_clear,           /* tp_clear */
 
600
    0,                         /* tp_richcompare */
 
601
    0,                         /* tp_weaklistoffset */
 
602
    0,                         /* tp_iter */
 
603
    0,                         /* tp_iternext */
 
604
    PinkNoise_methods,             /* tp_methods */
 
605
    PinkNoise_members,             /* tp_members */
 
606
    0,                      /* tp_getset */
 
607
    0,                         /* tp_base */
 
608
    0,                         /* tp_dict */
 
609
    0,                         /* tp_descr_get */
 
610
    0,                         /* tp_descr_set */
 
611
    0,                         /* tp_dictoffset */
 
612
    (initproc)PinkNoise_init,      /* tp_init */
 
613
    0,                         /* tp_alloc */
 
614
    PinkNoise_new,                 /* tp_new */
 
615
};
 
616
 
 
617
typedef struct {
 
618
    pyo_audio_HEAD
 
619
    int modebuffer[2];
 
620
    MYFLT y1;
 
621
    MYFLT c1;
 
622
    MYFLT c2;
 
623
} BrownNoise;
 
624
 
 
625
static void
 
626
BrownNoise_generate(BrownNoise *self) {
 
627
    MYFLT rnd, val;
 
628
    int i;
 
629
    
 
630
    for (i=0; i<self->bufsize; i++) {
 
631
        rnd = rand()/((MYFLT)(RAND_MAX)+1)*1.98-0.99;
 
632
        val = self->c1 * rnd + self->c2 * self->y1;
 
633
        self->y1 = val;
 
634
        self->data[i] = val * 20.0; /* gain compensation */
 
635
    }
 
636
}
 
637
 
 
638
static void BrownNoise_postprocessing_ii(BrownNoise *self) { POST_PROCESSING_II };
 
639
static void BrownNoise_postprocessing_ai(BrownNoise *self) { POST_PROCESSING_AI };
 
640
static void BrownNoise_postprocessing_ia(BrownNoise *self) { POST_PROCESSING_IA };
 
641
static void BrownNoise_postprocessing_aa(BrownNoise *self) { POST_PROCESSING_AA };
 
642
static void BrownNoise_postprocessing_ireva(BrownNoise *self) { POST_PROCESSING_IREVA };
 
643
static void BrownNoise_postprocessing_areva(BrownNoise *self) { POST_PROCESSING_AREVA };
 
644
static void BrownNoise_postprocessing_revai(BrownNoise *self) { POST_PROCESSING_REVAI };
 
645
static void BrownNoise_postprocessing_revaa(BrownNoise *self) { POST_PROCESSING_REVAA };
 
646
static void BrownNoise_postprocessing_revareva(BrownNoise *self) { POST_PROCESSING_REVAREVA };
 
647
 
 
648
static void
 
649
BrownNoise_setProcMode(BrownNoise *self)
 
650
{
 
651
    int muladdmode;
 
652
    muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
 
653
    
 
654
        switch (muladdmode) {
 
655
        case 0:        
 
656
            self->muladd_func_ptr = BrownNoise_postprocessing_ii;
 
657
            break;
 
658
        case 1:    
 
659
            self->muladd_func_ptr = BrownNoise_postprocessing_ai;
 
660
            break;
 
661
        case 2:    
 
662
            self->muladd_func_ptr = BrownNoise_postprocessing_revai;
 
663
            break;
 
664
        case 10:        
 
665
            self->muladd_func_ptr = BrownNoise_postprocessing_ia;
 
666
            break;
 
667
        case 11:    
 
668
            self->muladd_func_ptr = BrownNoise_postprocessing_aa;
 
669
            break;
 
670
        case 12:    
 
671
            self->muladd_func_ptr = BrownNoise_postprocessing_revaa;
 
672
            break;
 
673
        case 20:        
 
674
            self->muladd_func_ptr = BrownNoise_postprocessing_ireva;
 
675
            break;
 
676
        case 21:    
 
677
            self->muladd_func_ptr = BrownNoise_postprocessing_areva;
 
678
            break;
 
679
        case 22:    
 
680
            self->muladd_func_ptr = BrownNoise_postprocessing_revareva;
 
681
            break;
 
682
    }
 
683
}
 
684
 
 
685
static void
 
686
BrownNoise_compute_next_data_frame(BrownNoise *self)
 
687
{
 
688
    BrownNoise_generate(self); 
 
689
    (*self->muladd_func_ptr)(self);
 
690
}
 
691
 
 
692
static int
 
693
BrownNoise_traverse(BrownNoise *self, visitproc visit, void *arg)
 
694
{
 
695
    pyo_VISIT
 
696
    return 0;
 
697
}
 
698
 
 
699
static int 
 
700
BrownNoise_clear(BrownNoise *self)
 
701
{
 
702
    pyo_CLEAR
 
703
    return 0;
 
704
}
 
705
 
 
706
static void
 
707
BrownNoise_dealloc(BrownNoise* self)
 
708
{
 
709
    free(self->data);
 
710
    BrownNoise_clear(self);
 
711
    self->ob_type->tp_free((PyObject*)self);
 
712
}
 
713
 
 
714
static PyObject * BrownNoise_deleteStream(BrownNoise *self) { DELETE_STREAM };
 
715
 
 
716
static PyObject *
 
717
BrownNoise_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
718
{
 
719
    int i;
 
720
    BrownNoise *self;
 
721
    self = (BrownNoise *)type->tp_alloc(type, 0);
 
722
    
 
723
        self->modebuffer[0] = 0;
 
724
        self->modebuffer[1] = 0;
 
725
    self->y1 = self->c1 = self->c2 = 0.0;
 
726
    
 
727
    INIT_OBJECT_COMMON
 
728
    Stream_setFunctionPtr(self->stream, BrownNoise_compute_next_data_frame);
 
729
    self->mode_func_ptr = BrownNoise_setProcMode;
 
730
    
 
731
    return (PyObject *)self;
 
732
}
 
733
 
 
734
static int
 
735
BrownNoise_init(BrownNoise *self, PyObject *args, PyObject *kwds)
 
736
{
 
737
    MYFLT b;
 
738
    PyObject *multmp=NULL, *addtmp=NULL;
 
739
    
 
740
    static char *kwlist[] = {"mul", "add", NULL};
 
741
    
 
742
    if (! PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist, &multmp, &addtmp))
 
743
        return -1; 
 
744
    
 
745
    if (multmp) {
 
746
        PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
 
747
    }
 
748
    
 
749
    if (addtmp) {
 
750
        PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
 
751
    }
 
752
    
 
753
    Py_INCREF(self->stream);
 
754
    PyObject_CallMethod(self->server, "addStream", "O", self->stream);
 
755
 
 
756
    b = 2.0 - MYCOS(TWOPI * 20.0 / self->sr);
 
757
    self->c2 = (b - MYSQRT(b * b - 1.0));
 
758
    self->c1 = 1.0 - self->c2;
 
759
    
 
760
    (*self->mode_func_ptr)(self);
 
761
    
 
762
    Server_generateSeed((Server *)self->server, BROWNNOISE_ID);
 
763
    
 
764
    Py_INCREF(self);
 
765
    return 0;
 
766
}
 
767
 
 
768
static PyObject * BrownNoise_getServer(BrownNoise* self) { GET_SERVER };
 
769
static PyObject * BrownNoise_getStream(BrownNoise* self) { GET_STREAM };
 
770
static PyObject * BrownNoise_setMul(BrownNoise *self, PyObject *arg) { SET_MUL };       
 
771
static PyObject * BrownNoise_setAdd(BrownNoise *self, PyObject *arg) { SET_ADD };       
 
772
static PyObject * BrownNoise_setSub(BrownNoise *self, PyObject *arg) { SET_SUB };       
 
773
static PyObject * BrownNoise_setDiv(BrownNoise *self, PyObject *arg) { SET_DIV };       
 
774
 
 
775
static PyObject * BrownNoise_play(BrownNoise *self, PyObject *args, PyObject *kwds) { PLAY };
 
776
static PyObject * BrownNoise_out(BrownNoise *self, PyObject *args, PyObject *kwds) { OUT };
 
777
static PyObject * BrownNoise_stop(BrownNoise *self) { STOP };
 
778
 
 
779
static PyObject * BrownNoise_multiply(BrownNoise *self, PyObject *arg) { MULTIPLY };
 
780
static PyObject * BrownNoise_inplace_multiply(BrownNoise *self, PyObject *arg) { INPLACE_MULTIPLY };
 
781
static PyObject * BrownNoise_add(BrownNoise *self, PyObject *arg) { ADD };
 
782
static PyObject * BrownNoise_inplace_add(BrownNoise *self, PyObject *arg) { INPLACE_ADD };
 
783
static PyObject * BrownNoise_sub(BrownNoise *self, PyObject *arg) { SUB };
 
784
static PyObject * BrownNoise_inplace_sub(BrownNoise *self, PyObject *arg) { INPLACE_SUB };
 
785
static PyObject * BrownNoise_div(BrownNoise *self, PyObject *arg) { DIV };
 
786
static PyObject * BrownNoise_inplace_div(BrownNoise *self, PyObject *arg) { INPLACE_DIV };
 
787
 
 
788
static PyMemberDef BrownNoise_members[] = {
 
789
    {"server", T_OBJECT_EX, offsetof(BrownNoise, server), 0, "Pyo server."},
 
790
    {"stream", T_OBJECT_EX, offsetof(BrownNoise, stream), 0, "Stream object."},
 
791
    {"mul", T_OBJECT_EX, offsetof(BrownNoise, mul), 0, "Mul factor."},
 
792
    {"add", T_OBJECT_EX, offsetof(BrownNoise, add), 0, "Add factor."},
 
793
    {NULL}  /* Sentinel */
 
794
};
 
795
 
 
796
static PyMethodDef BrownNoise_methods[] = {
 
797
    {"getServer", (PyCFunction)BrownNoise_getServer, METH_NOARGS, "Returns server object."},
 
798
    {"_getStream", (PyCFunction)BrownNoise_getStream, METH_NOARGS, "Returns stream object."},
 
799
    {"deleteStream", (PyCFunction)BrownNoise_deleteStream, METH_NOARGS, "Remove stream from server and delete the object."},
 
800
    {"play", (PyCFunction)BrownNoise_play, METH_VARARGS|METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
 
801
    {"out", (PyCFunction)BrownNoise_out, METH_VARARGS|METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
 
802
    {"stop", (PyCFunction)BrownNoise_stop, METH_NOARGS, "Stops computing."},
 
803
    {"setMul", (PyCFunction)BrownNoise_setMul, METH_O, "Sets BrownNoise mul factor."},
 
804
    {"setAdd", (PyCFunction)BrownNoise_setAdd, METH_O, "Sets BrownNoise add factor."},
 
805
    {"setSub", (PyCFunction)BrownNoise_setSub, METH_O, "Sets inverse add factor."},
 
806
    {"setDiv", (PyCFunction)BrownNoise_setDiv, METH_O, "Sets inverse mul factor."},
 
807
    {NULL}  /* Sentinel */
 
808
};
 
809
 
 
810
static PyNumberMethods BrownNoise_as_number = {
 
811
    (binaryfunc)BrownNoise_add,                      /*nb_add*/
 
812
    (binaryfunc)BrownNoise_sub,                 /*nb_subtract*/
 
813
    (binaryfunc)BrownNoise_multiply,                 /*nb_multiply*/
 
814
    (binaryfunc)BrownNoise_div,                   /*nb_divide*/
 
815
    0,                /*nb_remainder*/
 
816
    0,                   /*nb_divmod*/
 
817
    0,                   /*nb_power*/
 
818
    0,                  /*nb_neg*/
 
819
    0,                /*nb_pos*/
 
820
    0,                  /*(unaryfunc)array_abs,*/
 
821
    0,                    /*nb_nonzero*/
 
822
    0,                    /*nb_invert*/
 
823
    0,               /*nb_lshift*/
 
824
    0,              /*nb_rshift*/
 
825
    0,              /*nb_and*/
 
826
    0,              /*nb_xor*/
 
827
    0,               /*nb_or*/
 
828
    0,                                          /*nb_coerce*/
 
829
    0,                       /*nb_int*/
 
830
    0,                      /*nb_long*/
 
831
    0,                     /*nb_float*/
 
832
    0,                       /*nb_oct*/
 
833
    0,                       /*nb_hex*/
 
834
    (binaryfunc)BrownNoise_inplace_add,              /*inplace_add*/
 
835
    (binaryfunc)BrownNoise_inplace_sub,         /*inplace_subtract*/
 
836
    (binaryfunc)BrownNoise_inplace_multiply,         /*inplace_multiply*/
 
837
    (binaryfunc)BrownNoise_inplace_div,           /*inplace_divide*/
 
838
    0,        /*inplace_remainder*/
 
839
    0,           /*inplace_power*/
 
840
    0,       /*inplace_lshift*/
 
841
    0,      /*inplace_rshift*/
 
842
    0,      /*inplace_and*/
 
843
    0,      /*inplace_xor*/
 
844
    0,       /*inplace_or*/
 
845
    0,             /*nb_floor_divide*/
 
846
    0,              /*nb_true_divide*/
 
847
    0,     /*nb_inplace_floor_divide*/
 
848
    0,      /*nb_inplace_true_divide*/
 
849
    0,                     /* nb_index */
 
850
};
 
851
 
 
852
PyTypeObject BrownNoiseType = {
 
853
    PyObject_HEAD_INIT(NULL)
 
854
    0,                         /*ob_size*/
 
855
    "_pyo.BrownNoise_base",         /*tp_name*/
 
856
    sizeof(BrownNoise),         /*tp_basicsize*/
 
857
    0,                         /*tp_itemsize*/
 
858
    (destructor)BrownNoise_dealloc, /*tp_dealloc*/
 
859
    0,                         /*tp_print*/
 
860
    0,                         /*tp_getattr*/
 
861
    0,                         /*tp_setattr*/
 
862
    0,                         /*tp_compare*/
 
863
    0,                         /*tp_repr*/
 
864
    &BrownNoise_as_number,             /*tp_as_number*/
 
865
    0,                         /*tp_as_sequence*/
 
866
    0,                         /*tp_as_mapping*/
 
867
    0,                         /*tp_hash */
 
868
    0,                         /*tp_call*/
 
869
    0,                         /*tp_str*/
 
870
    0,                         /*tp_getattro*/
 
871
    0,                         /*tp_setattro*/
 
872
    0,                         /*tp_as_buffer*/
 
873
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
 
874
    "BrownNoise objects. Brown noise generator (-6dB/octave rolloff).",           /* tp_doc */
 
875
    (traverseproc)BrownNoise_traverse,   /* tp_traverse */
 
876
    (inquiry)BrownNoise_clear,           /* tp_clear */
 
877
    0,                         /* tp_richcompare */
 
878
    0,                         /* tp_weaklistoffset */
 
879
    0,                         /* tp_iter */
 
880
    0,                         /* tp_iternext */
 
881
    BrownNoise_methods,             /* tp_methods */
 
882
    BrownNoise_members,             /* tp_members */
 
883
    0,                      /* tp_getset */
 
884
    0,                         /* tp_base */
 
885
    0,                         /* tp_dict */
 
886
    0,                         /* tp_descr_get */
 
887
    0,                         /* tp_descr_set */
 
888
    0,                         /* tp_dictoffset */
 
889
    (initproc)BrownNoise_init,      /* tp_init */
 
890
    0,                         /* tp_alloc */
 
891
    BrownNoise_new,                 /* tp_new */
 
892
};
 
893