Debian Codegraph @

src/objects/sfplayermodule.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /**************************************************************************
2 * Copyright 2009-2015 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 Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (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 Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with pyo. If not, see <http://www.gnu.org/licenses/>. *
19 *************************************************************************/
20
21 #define PY_SSIZE_T_CLEAN
22
23 #include <Python.h>
24 #include "structmember.h"
25 #include <math.h>
26 #include "pyomodule.h"
27 #include "streammodule.h"
28 #include "servermodule.h"
29 #include "dummymodule.h"
30 #include "sndfile.h"
31 #include "interpolation.h"
32
33 /* SfPlayer object */
34 typedef struct
35 {
36 pyo_audio_HEAD
37 PyObject *speed;
38 Stream *speed_stream;
39 int modebuffer[1];
40 SNDFILE *sf;
41 SF_INFO info;
42 char *path;
43 int loop;
44 int interp; /* 0 = default to 2, 1 = nointerp, 2 = linear, 3 = cos, 4 = cubic */
45 sf_count_t sndSize; /* number of frames */
46 int sndChnls;
47 int sndSr;
48 MYFLT srScale;
49 MYFLT startPos;
50 double pointerPos;
51 MYFLT *samplesBuffer;
52 MYFLT *trigsBuffer;
53 TriggerStream *trig_stream;
54 int init;
55 MYFLT (*interp_func_ptr)(MYFLT *, T_SIZE_T, MYFLT, T_SIZE_T);
56 } SfPlayer;
57
58 static void
59 SfPlayer_readframes_i(SfPlayer *self)
60 {
61 MYFLT sp, frac, bufpos, delta, startPos;
62 int i, j, shortbuflen, pad;
63 T_SIZE_T totlen, buflen, bufindex;
64 sf_count_t index;
65
66 if (self->modebuffer[0] == 0)
67 sp = PyFloat_AS_DOUBLE(self->speed);
68 else
69 sp = Stream_getData((Stream *)self->speed_stream)[0];
70
71 delta = MYFABS(sp) * self->srScale;
72
73 buflen = (T_SIZE_T)(self->bufsize * delta + 0.5) + 64;
74 totlen = self->sndChnls * buflen;
75 MYFLT buffer[totlen];
76 MYFLT buffer2[self->sndChnls][buflen];
77
78 if (sp > 0) /* forward reading */
79 {
80 if (self->pointerPos >= self->sndSize)
81 {
82 self->pointerPos -= self->sndSize - self->startPos;
83
84 if (self->loop == 0)
85 {
86 PyObject_CallMethod((PyObject *)self, "stop", NULL);
87
88 for (i = 0; i < (self->bufsize * self->sndChnls); i++)
89 {
90 self->samplesBuffer[i] = 0.0;
91 }
92
93 for (i = 0; i < self->bufsize; i++)
94 {
95 self->trigsBuffer[i] = 0.0;
96 }
97
98 return;
99 }
100 }
101
102 index = (sf_count_t)self->pointerPos;
103 sf_seek(self->sf, index, SEEK_SET); /* sets position pointer in the file */
104
105 /* fill a buffer with enough samples to satisfy speed reading */
106 /* if not enough samples left in the file */
107 if ((index + buflen) > self->sndSize)
108 {
109 shortbuflen = self->sndSize - index;
110 pad = (buflen - shortbuflen) * self->sndChnls;
111 SF_READ(self->sf, buffer, shortbuflen * self->sndChnls);
112
113 if (self->loop == 0) /* with zero padding if noloop */
114 {
115 for (i = 0; i < pad; i++)
116 {
117 buffer[i + shortbuflen * self->sndChnls] = 0.;
118 }
119 }
120 else /* wrap around and read new samples if loop */
121 {
122 MYFLT buftemp[pad];
123 sf_seek(self->sf, (sf_count_t)self->startPos, SEEK_SET);
124 SF_READ(self->sf, buftemp, pad);
125
126 for (i = 0; i < pad; i++)
127 {
128 buffer[i + shortbuflen * self->sndChnls] = buftemp[i];
129 }
130 }
131 }
132 else /* without zero padding */
133 SF_READ(self->sf, buffer, totlen);
134
135 /* de-interleave samples */
136 for (i = 0; i < totlen; i++)
137 {
138 buffer2[i % self->sndChnls][(T_SIZE_T)(i / self->sndChnls)] = buffer[i];
139 }
140
141 /* fill samplesBuffer with samples */
142 for (i = 0; i < self->bufsize; i++)
143 {
144 self->trigsBuffer[i] = 0.0;
145 bufpos = self->pointerPos - index;
146 bufindex = (T_SIZE_T)bufpos;
147 frac = bufpos - bufindex;
148
149 for (j = 0; j < self->sndChnls; j++)
150 {
151 self->samplesBuffer[i + (j * self->bufsize)] = (*self->interp_func_ptr)(buffer2[j], bufindex, frac, buflen);
152 }
153
154 self->pointerPos += delta;
155 }
156
157 if (self->pointerPos >= self->sndSize)
158 self->trigsBuffer[0] = 1.0;
159 }
160 else if (sp < 0) /* backward reading */
161 {
162 startPos = self->startPos;
163
164 if (startPos == 0.)
165 startPos = self->sndSize - 1;
166
167 if (self->pointerPos == 0.0)
168 self->pointerPos = self->sndSize - 1;
169
170 if (self->pointerPos <= 0)
171 {
172 self->pointerPos += startPos;
173
174 if (self->loop == 0)
175 {
176 PyObject_CallMethod((PyObject *)self, "stop", NULL);
177
178 for (i = 0; i < (self->bufsize * self->sndChnls); i++)
179 {
180 self->samplesBuffer[i] = 0.0;
181 }
182
183 for (i = 0; i < self->bufsize; i++)
184 {
185 self->trigsBuffer[i] = 0.0;
186 }
187
188 return;
189 }
190 }
191
192 index = (sf_count_t)self->pointerPos + 1;
193
194 /* fill a buffer with enough samples to satisfy speed reading */
195 /* if not enough samples to read in the file */
196 if ((index - buflen) < 0)
197 {
198 shortbuflen = index;
199 int pad = buflen - shortbuflen;
200 int padlen = pad * self->sndChnls;
201
202 if (self->loop == 0) /* with zero padding if noloop */
203 {
204 for (i = 0; i < padlen; i++)
205 {
206 buffer[i] = 0.;
207 }
208 }
209 else /* wrap around and read new samples if loop */
210 {
211 MYFLT buftemp[padlen];
212 sf_seek(self->sf, (sf_count_t)startPos - pad, SEEK_SET);
213 SF_READ(self->sf, buftemp, padlen);
214
215 for (i = 0; i < padlen; i++)
216 {
217 buffer[i] = buftemp[i];
218 }
219 }
220
221 MYFLT buftemp2[shortbuflen * self->sndChnls];
222 sf_seek(self->sf, 0, SEEK_SET); /* sets position pointer in the file */
223 SF_READ(self->sf, buftemp2, shortbuflen * self->sndChnls);
224
225 for (i = 0; i < (shortbuflen * self->sndChnls); i++)
226 {
227 buffer[i + padlen] = buftemp2[i];
228 }
229 }
230 else /* without zero padding */
231 sf_seek(self->sf, index - buflen, SEEK_SET); /* sets position pointer in the file */
232
233 SF_READ(self->sf, buffer, totlen);
234
235 /* de-interleave samples */
236 for (i = 0; i < totlen; i++)
237 {
238 buffer2[i % self->sndChnls][(T_SIZE_T)(i / self->sndChnls)] = buffer[i];
239 }
240
241 /* reverse arrays */
242 MYFLT swap;
243
244 for (i = 0; i < self->sndChnls; i++)
245 {
246 int a;
247 int b = buflen;
248
249 for (a = 0; a < --b; a++) //increment a and decrement b until they meet eachother
250 {
251 swap = buffer2[i][a]; //put what's in a into swap space
252 buffer2[i][a] = buffer2[i][b]; //put what's in b into a
253 buffer2[i][b] = swap; //put what's in the swap (a) into b
254 }
255 }
256
257 /* fill stream buffer with samples */
258 for (i = 0; i < self->bufsize; i++)
259 {
260 self->trigsBuffer[i] = 0.0;
261 bufpos = index - self->pointerPos;
262 bufindex = (T_SIZE_T)bufpos;
263 frac = bufpos - bufindex;
264
265 for (j = 0; j < self->sndChnls; j++)
266 {
267 self->samplesBuffer[i + (j * self->bufsize)] = (*self->interp_func_ptr)(buffer2[j], bufindex, frac, buflen);
268 }
269
270 self->pointerPos -= delta;
271 }
272
273 if (self->pointerPos <= 0)
274 {
275 if (self->init == 0)
276 self->trigsBuffer[0] = 1.0;
277 else
278 self->init = 0;
279 }
280 }
281 else /* speed == 0.0 */
282 {
283 for (i = 0; i < (self->bufsize * self->sndChnls); i++)
284 {
285 self->samplesBuffer[i] = 0.0;
286 }
287 }
288 }
289
290 static void
291 SfPlayer_setProcMode(SfPlayer *self)
292 {
293 self->proc_func_ptr = SfPlayer_readframes_i;
294 }
295
296 static void
297 SfPlayer_compute_next_data_frame(SfPlayer *self)
298 {
299 (*self->proc_func_ptr)(self);
300 }
301
302 static int
303 SfPlayer_traverse(SfPlayer *self, visitproc visit, void *arg)
304 {
305 pyo_VISIT
306 Py_VISIT(self->speed);
307 return 0;
308 }
309
310 static int
311 SfPlayer_clear(SfPlayer *self)
312 {
313 pyo_CLEAR
314 Py_CLEAR(self->speed);
315 return 0;
316 }
317
318 static void
319 SfPlayer_dealloc(SfPlayer* self)
320 {
321 pyo_DEALLOC
322
323 if (self->sf != NULL)
324 sf_close(self->sf);
325
326 PyMem_RawFree(self->trigsBuffer);
327 PyMem_RawFree(self->samplesBuffer);
328 SfPlayer_clear(self);
329 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
330 Py_TYPE(self->trig_stream)->tp_free((PyObject*)self->trig_stream);
331 Py_TYPE(self)->tp_free((PyObject*)self);
332 }
333
334 static PyObject *
335 SfPlayer_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
336 {
337 int i;
338 Py_ssize_t psize;
339 MYFLT offset = 0.;
340 PyObject *speedtmp = NULL;
341 SfPlayer *self;
342 self = (SfPlayer *)type->tp_alloc(type, 0);
343
344 self->speed = PyFloat_FromDouble(1);
345 self->loop = 0;
346 self->interp = 2;
347 self->init = 1;
348 self->modebuffer[0] = 0;
349
350 INIT_OBJECT_COMMON
351 Stream_setFunctionPtr(self->stream, SfPlayer_compute_next_data_frame);
352 self->mode_func_ptr = SfPlayer_setProcMode;
353
354 static char *kwlist[] = {"path", "speed", "loop", "offset", "interp", NULL};
355
356 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_P__OIFI, kwlist, &self->path, &psize, &speedtmp, &self->loop, &offset, &self->interp))
357 Py_RETURN_NONE;
358
359 if (speedtmp)
360 {
361 PyObject_CallMethod((PyObject *)self, "setSpeed", "O", speedtmp);
362 }
363
364 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
365
366 (*self->mode_func_ptr)(self);
367
368 SET_INTERP_POINTER
369
370 /* Open the sound file. */
371 self->info.format = 0;
372 self->sf = sf_open(self->path, SFM_READ, &self->info);
373
374 if (self->sf == NULL)
375 {
376 PySys_WriteStdout("SfPlayer: failed to open the file.\n");
377 }
378
379 self->sndSize = self->info.frames;
380 self->sndSr = self->info.samplerate;
381 self->sndChnls = self->info.channels;
382 self->srScale = self->sndSr / self->sr;
383
384 self->samplesBuffer = (MYFLT *)PyMem_RawRealloc(self->samplesBuffer, self->bufsize * self->sndChnls * sizeof(MYFLT));
385 self->trigsBuffer = (MYFLT *)PyMem_RawRealloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT));
386
387 for (i = 0; i < self->bufsize; i++)
388 {
389 self->trigsBuffer[i] = 0.0;
390 }
391
392 MAKE_NEW_TRIGGER_STREAM(self->trig_stream, &TriggerStreamType, NULL);
393 TriggerStream_setData(self->trig_stream, self->trigsBuffer);
394
395 self->startPos = offset * self->sr * self->srScale;
396
397 if (self->startPos < 0.0 || self->startPos >= self->sndSize)
398 self->startPos = 0.0;
399
400 self->pointerPos = self->startPos;
401
402 return (PyObject *)self;
403 }
404
405 static PyObject * SfPlayer_getServer(SfPlayer* self) { GET_SERVER };
406 static PyObject * SfPlayer_getStream(SfPlayer* self) { GET_STREAM };
407 static PyObject * SfPlayer_getTriggerStream(SfPlayer* self) { GET_TRIGGER_STREAM };
408
409 static PyObject * SfPlayer_play(SfPlayer *self, PyObject *args, PyObject *kwds)
410 {
411 self->init = 1;
412 self->pointerPos = self->startPos;
413 PLAY
414 };
415
416 static PyObject * SfPlayer_out(SfPlayer *self, PyObject *args, PyObject *kwds)
417 {
418 self->init = 1;
419 self->pointerPos = self->startPos;
420 OUT
421 };
422
423 static PyObject * SfPlayer_stop(SfPlayer *self, PyObject *args, PyObject *kwds) { STOP };
424
425 static PyObject * SfPlayer_setSpeed(SfPlayer *self, PyObject *arg) { SET_PARAM(self->speed, self->speed_stream, 0); }
426
427 static PyObject *
428 SfPlayer_setSound(SfPlayer *self, PyObject *args)
429 {
430 /* Need to perform a check to be sure that the new
431 sound is of the same number of channels. */
432 Py_ssize_t psize;
433
434 //ASSERT_ARG_NOT_NULL
435
436 if (! PyArg_ParseTuple(args, "s#", &self->path, &psize))
437 Py_RETURN_NONE;
438
439 //self->path = PyUnicode_AsUTF8(arg);
440
441 sf_close(self->sf);
442
443 /* Open the sound file. */
444 self->info.format = 0;
445 self->sf = sf_open(self->path, SFM_READ, &self->info);
446
447 if (self->sf == NULL)
448 {
449 PySys_WriteStdout("SfPlayer: failed to open the file.\n");
450 }
451
452 self->sndSize = self->info.frames;
453 self->sndSr = self->info.samplerate;
454 //self->sndChnls = self->info.channels;
455 self->srScale = self->sndSr / self->sr;
456
457 //self->samplesBuffer = (MYFLT *)PyMem_RawRealloc(self->samplesBuffer, self->bufsize * self->sndChnls * sizeof(MYFLT));
458
459 self->startPos = 0.0;
460 self->pointerPos = self->startPos;
461
462 Py_RETURN_NONE;
463 }
464
465 static PyObject *
466 SfPlayer_setLoop(SfPlayer *self, PyObject *arg)
467 {
468 ASSERT_ARG_NOT_NULL
469
470 self->loop = PyLong_AsLong(arg);
471
472 Py_RETURN_NONE;
473 }
474
475 static PyObject *
476 SfPlayer_setOffset(SfPlayer *self, PyObject *arg)
477 {
478 ASSERT_ARG_NOT_NULL
479
480 if (PyNumber_Check(arg))
481 {
482 self->startPos = PyFloat_AsDouble(arg) * self->sr * self->srScale;
483
484 if (self->startPos < 0.0 || self->startPos >= self->sndSize)
485 self->startPos = 0.0;
486 }
487
488 Py_RETURN_NONE;
489 }
490
491 static PyObject *
492 SfPlayer_setInterp(SfPlayer *self, PyObject *arg)
493 {
494 ASSERT_ARG_NOT_NULL
495
496 if (PyNumber_Check(arg))
497 {
498 self->interp = PyLong_AsLong(PyNumber_Long(arg));
499 }
500
501 SET_INTERP_POINTER
502
503 Py_RETURN_NONE;
504 }
505
506 MYFLT *
507 SfPlayer_getSamplesBuffer(SfPlayer *self)
508 {
509 return (MYFLT *)self->samplesBuffer;
510 }
511
512 static PyMemberDef SfPlayer_members[] =
513 {
514 {"server", T_OBJECT_EX, offsetof(SfPlayer, server), 0, "Pyo server."},
515 {"stream", T_OBJECT_EX, offsetof(SfPlayer, stream), 0, "Stream object."},
516 {"trig_stream", T_OBJECT_EX, offsetof(SfPlayer, trig_stream), 0, "Trigger Stream object."},
517 {"speed", T_OBJECT_EX, offsetof(SfPlayer, speed), 0, "Frequency in cycle per second."},
518 {NULL} /* Sentinel */
519 };
520
521 static PyMethodDef SfPlayer_methods[] =
522 {
523 {"getServer", (PyCFunction)SfPlayer_getServer, METH_NOARGS, "Returns server object."},
524 {"_getStream", (PyCFunction)SfPlayer_getStream, METH_NOARGS, "Returns stream object."},
525 {"_getTriggerStream", (PyCFunction)SfPlayer_getTriggerStream, METH_NOARGS, "Returns trigger stream object."},
526 {"play", (PyCFunction)SfPlayer_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
527 {"out", (PyCFunction)SfPlayer_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
528 {"stop", (PyCFunction)SfPlayer_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
529 {"setSound", (PyCFunction)SfPlayer_setSound, METH_VARARGS, "Sets sfplayer sound path."},
530 {"setSpeed", (PyCFunction)SfPlayer_setSpeed, METH_O, "Sets sfplayer reading speed."},
531 {"setLoop", (PyCFunction)SfPlayer_setLoop, METH_O, "Sets sfplayer loop mode (0 = no loop, 1 = loop)."},
532 {"setOffset", (PyCFunction)SfPlayer_setOffset, METH_O, "Sets sfplayer start position."},
533 {"setInterp", (PyCFunction)SfPlayer_setInterp, METH_O, "Sets sfplayer interpolation mode."},
534 {NULL} /* Sentinel */
535 };
536
537 PyTypeObject SfPlayerType =
538 {
539 PyVarObject_HEAD_INIT(NULL, 0)
540 "_pyo.SfPlayer_base", /*tp_name*/
541 sizeof(SfPlayer), /*tp_basicsize*/
542 0, /*tp_itemsize*/
543 (destructor)SfPlayer_dealloc, /*tp_dealloc*/
544 0, /*tp_print*/
545 0, /*tp_getattr*/
546 0, /*tp_setattr*/
547 0, /*tp_as_async (tp_compare in Python 2)*/
548 0, /*tp_repr*/
549 0, /*tp_as_number*/
550 0, /*tp_as_sequence*/
551 0, /*tp_as_mapping*/
552 0, /*tp_hash */
553 0, /*tp_call*/
554 0, /*tp_str*/
555 0, /*tp_getattro*/
556 0, /*tp_setattro*/
557 0, /*tp_as_buffer*/
558 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
559 "SfPlayer objects. Reads a soundfile directly from disk.", /* tp_doc */
560 (traverseproc)SfPlayer_traverse, /* tp_traverse */
561 (inquiry)SfPlayer_clear, /* tp_clear */
562 0, /* tp_richcompare */
563 0, /* tp_weaklistoffset */
564 0, /* tp_iter */
565 0, /* tp_iternext */
566 SfPlayer_methods, /* tp_methods */
567 SfPlayer_members, /* tp_members */
568 0, /* tp_getset */
569 0, /* tp_base */
570 0, /* tp_dict */
571 0, /* tp_descr_get */
572 0, /* tp_descr_set */
573 0, /* tp_dictoffset */
574 0, /* tp_init */
575 0, /* tp_alloc */
576 SfPlayer_new, /* tp_new */
577 };
578
579 /************************************************************************************************/
580 /* Sfplay streamer object per channel */
581 /************************************************************************************************/
582 typedef struct
583 {
584 pyo_audio_HEAD
585 SfPlayer *mainPlayer;
586 int modebuffer[2];
587 int chnl;
588 } SfPlay;
589
590 static void SfPlay_postprocessing_ii(SfPlay *self) { POST_PROCESSING_II };
591 static void SfPlay_postprocessing_ai(SfPlay *self) { POST_PROCESSING_AI };
592 static void SfPlay_postprocessing_ia(SfPlay *self) { POST_PROCESSING_IA };
593 static void SfPlay_postprocessing_aa(SfPlay *self) { POST_PROCESSING_AA };
594 static void SfPlay_postprocessing_ireva(SfPlay *self) { POST_PROCESSING_IREVA };
595 static void SfPlay_postprocessing_areva(SfPlay *self) { POST_PROCESSING_AREVA };
596 static void SfPlay_postprocessing_revai(SfPlay *self) { POST_PROCESSING_REVAI };
597 static void SfPlay_postprocessing_revaa(SfPlay *self) { POST_PROCESSING_REVAA };
598 static void SfPlay_postprocessing_revareva(SfPlay *self) { POST_PROCESSING_REVAREVA };
599
600 static void
601 SfPlay_setProcMode(SfPlay *self)
602 {
603 int muladdmode;
604 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
605
606 switch (muladdmode)
607 {
608 case 0:
609 self->muladd_func_ptr = SfPlay_postprocessing_ii;
610 break;
611
612 case 1:
613 self->muladd_func_ptr = SfPlay_postprocessing_ai;
614 break;
615
616 case 2:
617 self->muladd_func_ptr = SfPlay_postprocessing_revai;
618 break;
619
620 case 10:
621 self->muladd_func_ptr = SfPlay_postprocessing_ia;
622 break;
623
624 case 11:
625 self->muladd_func_ptr = SfPlay_postprocessing_aa;
626 break;
627
628 case 12:
629 self->muladd_func_ptr = SfPlay_postprocessing_revaa;
630 break;
631
632 case 20:
633 self->muladd_func_ptr = SfPlay_postprocessing_ireva;
634 break;
635
636 case 21:
637 self->muladd_func_ptr = SfPlay_postprocessing_areva;
638 break;
639
640 case 22:
641 self->muladd_func_ptr = SfPlay_postprocessing_revareva;
642 break;
643 }
644 }
645
646 static void
647 SfPlay_compute_next_data_frame(SfPlay *self)
648 {
649 int i;
650 MYFLT *tmp;
651 int offset = self->chnl * self->bufsize;
652 tmp = SfPlayer_getSamplesBuffer((SfPlayer *)self->mainPlayer);
653
654 for (i = 0; i < self->bufsize; i++)
655 {
656 self->data[i] = tmp[i + offset];
657 }
658
659 (*self->muladd_func_ptr)(self);
660 }
661
662 static int
663 SfPlay_traverse(SfPlay *self, visitproc visit, void *arg)
664 {
665 pyo_VISIT
666 Py_VISIT(self->mainPlayer);
667 return 0;
668 }
669
670 static int
671 SfPlay_clear(SfPlay *self)
672 {
673 pyo_CLEAR
674 Py_CLEAR(self->mainPlayer);
675 return 0;
676 }
677
678 static void
679 SfPlay_dealloc(SfPlay* self)
680 {
681 pyo_DEALLOC
682 SfPlay_clear(self);
683 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
684 Py_TYPE(self)->tp_free((PyObject*)self);
685 }
686
687 static PyObject *
688 SfPlay_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
689 {
690 int i;
691 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
692 SfPlay *self;
693 self = (SfPlay *)type->tp_alloc(type, 0);
694
695 self->chnl = 0;
696 self->modebuffer[0] = 0;
697 self->modebuffer[1] = 0;
698
699 INIT_OBJECT_COMMON
700 Stream_setFunctionPtr(self->stream, SfPlay_compute_next_data_frame);
701 self->mode_func_ptr = SfPlay_setProcMode;
702
703 static char *kwlist[] = {"mainPlayer", "chnl", "mul", "add", NULL};
704
705 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
706 Py_RETURN_NONE;
707
708 self->mainPlayer = (SfPlayer *)maintmp;
709 Py_INCREF(self->mainPlayer);
710
711 if (multmp)
712 {
713 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
714 }
715
716 if (addtmp)
717 {
718 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
719 }
720
721 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
722
723 (*self->mode_func_ptr)(self);
724
725 return (PyObject *)self;
726 }
727
728 static PyObject * SfPlay_getServer(SfPlay* self) { GET_SERVER };
729 static PyObject * SfPlay_getStream(SfPlay* self) { GET_STREAM };
730 static PyObject * SfPlay_setMul(SfPlay *self, PyObject *arg) { SET_MUL };
731 static PyObject * SfPlay_setAdd(SfPlay *self, PyObject *arg) { SET_ADD };
732 static PyObject * SfPlay_setSub(SfPlay *self, PyObject *arg) { SET_SUB };
733 static PyObject * SfPlay_setDiv(SfPlay *self, PyObject *arg) { SET_DIV };
734
735 static PyObject * SfPlay_play(SfPlay *self, PyObject *args, PyObject *kwds) { PLAY };
736 static PyObject * SfPlay_out(SfPlay *self, PyObject *args, PyObject *kwds) { OUT };
737 static PyObject * SfPlay_stop(SfPlay *self, PyObject *args, PyObject *kwds) { STOP };
738
739 static PyObject * SfPlay_multiply(SfPlay *self, PyObject *arg) { MULTIPLY };
740 static PyObject * SfPlay_inplace_multiply(SfPlay *self, PyObject *arg) { INPLACE_MULTIPLY };
741 static PyObject * SfPlay_add(SfPlay *self, PyObject *arg) { ADD };
742 static PyObject * SfPlay_inplace_add(SfPlay *self, PyObject *arg) { INPLACE_ADD };
743 static PyObject * SfPlay_sub(SfPlay *self, PyObject *arg) { SUB };
744 static PyObject * SfPlay_inplace_sub(SfPlay *self, PyObject *arg) { INPLACE_SUB };
745 static PyObject * SfPlay_div(SfPlay *self, PyObject *arg) { DIV };
746 static PyObject * SfPlay_inplace_div(SfPlay *self, PyObject *arg) { INPLACE_DIV };
747
748 static PyMemberDef SfPlay_members[] =
749 {
750 {"server", T_OBJECT_EX, offsetof(SfPlay, server), 0, "Pyo server."},
751 {"stream", T_OBJECT_EX, offsetof(SfPlay, stream), 0, "Stream object."},
752 {"mul", T_OBJECT_EX, offsetof(SfPlay, mul), 0, "Mul factor."},
753 {"add", T_OBJECT_EX, offsetof(SfPlay, add), 0, "Add factor."},
754 {NULL} /* Sentinel */
755 };
756
757 static PyMethodDef SfPlay_methods[] =
758 {
759 {"getServer", (PyCFunction)SfPlay_getServer, METH_NOARGS, "Returns server object."},
760 {"_getStream", (PyCFunction)SfPlay_getStream, METH_NOARGS, "Returns stream object."},
761 {"play", (PyCFunction)SfPlay_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
762 {"out", (PyCFunction)SfPlay_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
763 {"stop", (PyCFunction)SfPlay_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
764 {"setMul", (PyCFunction)SfPlay_setMul, METH_O, "Sets SfPlay mul factor."},
765 {"setAdd", (PyCFunction)SfPlay_setAdd, METH_O, "Sets SfPlay add factor."},
766 {"setSub", (PyCFunction)SfPlay_setSub, METH_O, "Sets inverse add factor."},
767 {"setDiv", (PyCFunction)SfPlay_setDiv, METH_O, "Sets inverse mul factor."},
768 {NULL} /* Sentinel */
769 };
770
771 static PyNumberMethods SfPlay_as_number =
772 {
773 (binaryfunc)SfPlay_add, /*nb_add*/
774 (binaryfunc)SfPlay_sub, /*nb_subtract*/
775 (binaryfunc)SfPlay_multiply, /*nb_multiply*/
776 0, /*nb_remainder*/
777 0, /*nb_divmod*/
778 0, /*nb_power*/
779 0, /*nb_neg*/
780 0, /*nb_pos*/
781 0, /*(unaryfunc)array_abs,*/
782 0, /*nb_nonzero*/
783 0, /*nb_invert*/
784 0, /*nb_lshift*/
785 0, /*nb_rshift*/
786 0, /*nb_and*/
787 0, /*nb_xor*/
788 0, /*nb_or*/
789 0, /*nb_int*/
790 0, /*nb_long*/
791 0, /*nb_float*/
792 (binaryfunc)SfPlay_inplace_add, /*inplace_add*/
793 (binaryfunc)SfPlay_inplace_sub, /*inplace_subtract*/
794 (binaryfunc)SfPlay_inplace_multiply, /*inplace_multiply*/
795 0, /*inplace_remainder*/
796 0, /*inplace_power*/
797 0, /*inplace_lshift*/
798 0, /*inplace_rshift*/
799 0, /*inplace_and*/
800 0, /*inplace_xor*/
801 0, /*inplace_or*/
802 0, /*nb_floor_divide*/
803 (binaryfunc)SfPlay_div, /*nb_true_divide*/
804 0, /*nb_inplace_floor_divide*/
805 (binaryfunc)SfPlay_inplace_div, /*nb_inplace_true_divide*/
806 0, /* nb_index */
807 };
808
809 PyTypeObject SfPlayType =
810 {
811 PyVarObject_HEAD_INIT(NULL, 0)
812 "_pyo.SfPlay_base", /*tp_name*/
813 sizeof(SfPlay), /*tp_basicsize*/
814 0, /*tp_itemsize*/
815 (destructor)SfPlay_dealloc, /*tp_dealloc*/
816 0, /*tp_print*/
817 0, /*tp_getattr*/
818 0, /*tp_setattr*/
819 0, /*tp_as_async (tp_compare in Python 2)*/
820 0, /*tp_repr*/
821 &SfPlay_as_number, /*tp_as_number*/
822 0, /*tp_as_sequence*/
823 0, /*tp_as_mapping*/
824 0, /*tp_hash */
825 0, /*tp_call*/
826 0, /*tp_str*/
827 0, /*tp_getattro*/
828 0, /*tp_setattro*/
829 0, /*tp_as_buffer*/
830 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
831 "SfPlay objects. Reads a channel from a soundfile directly from disk.", /* tp_doc */
832 (traverseproc)SfPlay_traverse, /* tp_traverse */
833 (inquiry)SfPlay_clear, /* tp_clear */
834 0, /* tp_richcompare */
835 0, /* tp_weaklistoffset */
836 0, /* tp_iter */
837 0, /* tp_iternext */
838 SfPlay_methods, /* tp_methods */
839 SfPlay_members, /* tp_members */
840 0, /* tp_getset */
841 0, /* tp_base */
842 0, /* tp_dict */
843 0, /* tp_descr_get */
844 0, /* tp_descr_set */
845 0, /* tp_dictoffset */
846 0, /* tp_init */
847 0, /* tp_alloc */
848 SfPlay_new, /* tp_new */
849 };
850
851 /************************************************************************************************/
852 /* SfMarkerShuffler object */
853 /************************************************************************************************/
854 typedef struct
855 {
856 pyo_audio_HEAD
857 PyObject *speed;
858 Stream *speed_stream;
859 int modebuffer[1];
860 SNDFILE *sf;
861 SF_INFO info;
862 char *path;
863 int interp; /* 0 = default to 2, 1 = nointerp, 2 = linear, 3 = cos, 4 = cubic */
864 sf_count_t sndSize; /* number of frames */
865 int sndChnls;
866 int sndSr;
867 int lastDir;
868 MYFLT srScale;
869 MYFLT startPos;
870 MYFLT endPos;
871 MYFLT nextStartPos;
872 MYFLT nextEndPos;
873 double pointerPos;
874 MYFLT *samplesBuffer;
875 MYFLT *markers;
876 int markers_size;
877 MYFLT x;
878 MYFLT (*type_func_ptr)();
879 MYFLT (*interp_func_ptr)(MYFLT *, T_SIZE_T, MYFLT, T_SIZE_T);
880 } SfMarkerShuffler;
881
882 /*** PROTOTYPES ***/
883 static void SfMarkerShuffler_chooseNewMark(SfMarkerShuffler *self, int dir);
884 /******************/
885
886 // no parameter
887 static MYFLT
888 SfMarkerShuffler_uniform(SfMarkerShuffler *self)
889 {
890 return RANDOM_UNIFORM;
891 }
892
893 static MYFLT
894 SfMarkerShuffler_linear_min(SfMarkerShuffler *self)
895 {
896 MYFLT a = RANDOM_UNIFORM;
897 MYFLT b = RANDOM_UNIFORM;
898
899 if (a < b) return a;
900 else return b;
901 }
902
903 static MYFLT
904 SfMarkerShuffler_linear_max(SfMarkerShuffler *self)
905 {
906 MYFLT a = RANDOM_UNIFORM;
907 MYFLT b = RANDOM_UNIFORM;
908
909 if (a > b) return a;
910 else return b;
911 }
912
913 static MYFLT
914 SfMarkerShuffler_triangle(SfMarkerShuffler *self)
915 {
916 MYFLT a = RANDOM_UNIFORM;
917 MYFLT b = RANDOM_UNIFORM;
918 return ((a + b) * 0.5);
919 }
920
921 // x1 = slope
922 static MYFLT
923 SfMarkerShuffler_expon_min(SfMarkerShuffler *self)
924 {
925 if (self->x <= 0.0) self->x = 0.00001;
926
927 MYFLT val = -MYLOG(RANDOM_UNIFORM) / self->x;
928
929 if (val < 0.0) return 0.0;
930 else if (val > 1.0) return 1.0;
931 else return val;
932 }
933
934 static MYFLT
935 SfMarkerShuffler_expon_max(SfMarkerShuffler *self)
936 {
937 if (self->x <= 0.0) self->x = 0.00001;
938
939 MYFLT val = 1.0 - (-MYLOG(RANDOM_UNIFORM) / self->x);
940
941 if (val < 0.0) return 0.0;
942 else if (val > 1.0) return 1.0;
943 else return val;
944 }
945
946 // x1 = bandwidth
947 static MYFLT
948 SfMarkerShuffler_biexpon(SfMarkerShuffler *self)
949 {
950 MYFLT polar, val;
951
952 if (self->x <= 0.0) self->x = 0.00001;
953
954 MYFLT sum = RANDOM_UNIFORM * 2.0;
955
956 if (sum > 1.0)
957 {
958 polar = -1;
959 sum = 2.0 - sum;
960 }
961 else
962 polar = 1;
963
964 val = 0.5 * (polar * MYLOG(sum) / self->x) + 0.5;
965
966 if (val < 0.0) return 0.0;
967 else if (val > 1.0) return 1.0;
968 else return val;
969 }
970
971 static MYFLT
972 SfMarkerShuffler_cauchy(SfMarkerShuffler *self)
973 {
974 MYFLT rnd, val, dir;
975
976 do
977 {
978 rnd = RANDOM_UNIFORM;
979 }
980 while (rnd == 0.5);
981
982 if (pyorand() < (PYO_RAND_MAX / 2))
983 dir = -1;
984 else
985 dir = 1;
986
987 val = 0.5 * (MYTAN(rnd) * self->x * dir) + 0.5;
988
989 if (val < 0.0) return 0.0;
990 else if (val > 1.0) return 1.0;
991 else return val;
992 }
993
994 // x = shape
995 static MYFLT
996 SfMarkerShuffler_weibull(SfMarkerShuffler *self)
997 {
998 MYFLT rnd, val;
999
1000 if (self->x <= 0.0) self->x = 0.00001;
1001
1002 rnd = 1.0 / (1.0 - RANDOM_UNIFORM);
1003 val = 0.5 * MYPOW(MYLOG(rnd), (1.0 / self->x));
1004
1005 if (val < 0.0) return 0.0;
1006 else if (val > 1.0) return 1.0;
1007 else return val;
1008 }
1009
1010 // x = bandwidth
1011 static MYFLT
1012 SfMarkerShuffler_gaussian(SfMarkerShuffler *self)
1013 {
1014 MYFLT rnd, val;
1015
1016 rnd = (RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM + RANDOM_UNIFORM);
1017 val = (self->x * (rnd - 3.0) * 0.33 + 0.5);
1018
1019 if (val < 0.0) return 0.0;
1020 else if (val > 1.0) return 1.0;
1021 else return val;
1022 }
1023
1024 static void
1025 SfMarkerShuffler_readframes_i(SfMarkerShuffler *self)
1026 {
1027 MYFLT sp, frac, bufpos, delta, tmp;
1028 int i, j, shortbuflen;
1029 T_SIZE_T totlen, buflen, bufindex;
1030 sf_count_t index;
1031
1032 if (self->modebuffer[0] == 0)
1033 sp = PyFloat_AS_DOUBLE(self->speed);
1034 else
1035 sp = Stream_getData((Stream *)self->speed_stream)[0];
1036
1037 delta = MYFABS(sp) * self->srScale;
1038
1039 buflen = (T_SIZE_T)(self->bufsize * delta + 0.5) + 64;
1040 totlen = self->sndChnls * buflen;
1041 MYFLT buffer[totlen];
1042 MYFLT buffer2[self->sndChnls][buflen];
1043
1044 if (sp > 0) /* reading forward */
1045 {
1046 if (self->startPos == -1 || self->lastDir == 0)
1047 {
1048 self->lastDir = 1;
1049 SfMarkerShuffler_chooseNewMark((SfMarkerShuffler *)self, 1);
1050 self->pointerPos = self->startPos;
1051 }
1052
1053 if (self->lastDir != 1)
1054 {
1055 tmp = self->startPos;
1056 self->startPos = self->endPos;
1057 self->endPos = tmp;
1058 self->lastDir = 1;
1059 }
1060
1061 index = (sf_count_t)self->pointerPos;
1062 sf_seek(self->sf, index, SEEK_SET); /* sets position pointer in the file */
1063
1064 /* fill a buffer with enough samples to satisfy speed reading */
1065 /* if not enough samples to read in the file */
1066 if ((index + buflen) > self->endPos)
1067 {
1068 shortbuflen = self->endPos - index;
1069 SF_READ(self->sf, buffer, shortbuflen * self->sndChnls);
1070
1071 /* wrap around and read new samples from new marker */
1072 int pad = buflen - shortbuflen;
1073 int padlen = pad * self->sndChnls;
1074 MYFLT buftemp[padlen];
1075 sf_seek(self->sf, (sf_count_t)self->nextStartPos, SEEK_SET);
1076 SF_READ(self->sf, buftemp, padlen);
1077
1078 for (i = 0; i < padlen; i++)
1079 {
1080 buffer[i + shortbuflen * self->sndChnls] = buftemp[i];
1081 }
1082 }
1083 else /* without wraparound */
1084 SF_READ(self->sf, buffer, totlen);
1085
1086 /* de-interleave samples */
1087 for (i = 0; i < totlen; i++)
1088 {
1089 buffer2[i % self->sndChnls][(T_SIZE_T)(i / self->sndChnls)] = buffer[i];
1090 }
1091
1092 /* fill data with samples */
1093 for (i = 0; i < self->bufsize; i++)
1094 {
1095 bufpos = self->pointerPos - index;
1096 bufindex = (T_SIZE_T)bufpos;
1097 frac = bufpos - bufindex;
1098
1099 for (j = 0; j < self->sndChnls; j++)
1100 {
1101 self->samplesBuffer[i + (j * self->bufsize)] = (*self->interp_func_ptr)(buffer2[j], bufindex, frac, buflen);
1102 }
1103
1104 self->pointerPos += delta;
1105 }
1106
1107 if (self->pointerPos >= self->endPos)
1108 {
1109 MYFLT off = self->pointerPos - self->endPos;
1110 SfMarkerShuffler_chooseNewMark((SfMarkerShuffler *)self, 1);
1111 self->pointerPos = self->startPos + off;
1112 }
1113 }
1114 else if (sp < 0) /* reading backward */
1115 {
1116 if (self->startPos == -1 || self->lastDir != -1)
1117 {
1118 self->lastDir = self->startPos = -1;
1119 SfMarkerShuffler_chooseNewMark((SfMarkerShuffler *)self, 0);
1120 self->pointerPos = self->startPos;
1121 }
1122
1123 index = (sf_count_t)self->pointerPos + 1;
1124
1125 /* fill a buffer with enough samples to satisfy speed reading */
1126 /* if not enough samples to read in the file */
1127 if ((index - buflen) < self->endPos)
1128 {
1129 shortbuflen = index - self->endPos;
1130 int pad = buflen - shortbuflen;
1131 int padlen = pad * self->sndChnls;
1132
1133 /* wrap around and read new samples from new marker */
1134 MYFLT buftemp[padlen];
1135 sf_seek(self->sf, (sf_count_t)self->nextStartPos - pad, SEEK_SET);
1136 SF_READ(self->sf, buftemp, padlen);
1137
1138 for (i = 0; i < padlen; i++)
1139 {
1140 buffer[i] = buftemp[i];
1141 }
1142
1143 MYFLT buftemp2[shortbuflen * self->sndChnls];
1144 sf_seek(self->sf, (sf_count_t)self->endPos, SEEK_SET); /* sets position pointer in the file */
1145 SF_READ(self->sf, buftemp2, shortbuflen * self->sndChnls);
1146
1147 for (i = 0; i < (shortbuflen * self->sndChnls); i++)
1148 {
1149 buffer[i + padlen] = buftemp2[i];
1150 }
1151 }
1152 else /* without wraparound */
1153 {
1154 sf_seek(self->sf, index - buflen, SEEK_SET); /* sets position pointer in the file */
1155 SF_READ(self->sf, buffer, totlen);
1156 }
1157
1158 /* de-interleave samples */
1159 for (i = 0; i < totlen; i++)
1160 {
1161 buffer2[i % self->sndChnls][(T_SIZE_T)(i / self->sndChnls)] = buffer[i];
1162 }
1163
1164 /* reverse arrays */
1165 MYFLT swap;
1166
1167 for (i = 0; i < self->sndChnls; i++)
1168 {
1169 int a;
1170 int b = buflen;
1171
1172 for (a = 0; a < --b; a++) //increment a and decrement b until they meet eachother
1173 {
1174 swap = buffer2[i][a]; //put what's in a into swap space
1175 buffer2[i][a] = buffer2[i][b]; //put what's in b into a
1176 buffer2[i][b] = swap; //put what's in the swap (a) into b
1177 }
1178 }
1179
1180 /* fill stream buffer with samples */
1181 for (i = 0; i < self->bufsize; i++)
1182 {
1183 bufpos = index - self->pointerPos;
1184 bufindex = (T_SIZE_T)bufpos;
1185 frac = bufpos - bufindex;
1186
1187 for (j = 0; j < self->sndChnls; j++)
1188 {
1189 self->samplesBuffer[i + (j * self->bufsize)] = (*self->interp_func_ptr)(buffer2[j], bufindex, frac, buflen);
1190 }
1191
1192 self->pointerPos -= delta;
1193 }
1194
1195 if (self->pointerPos <= self->endPos)
1196 {
1197 MYFLT off = self->endPos - self->pointerPos;
1198 SfMarkerShuffler_chooseNewMark((SfMarkerShuffler *)self, 0);
1199 self->pointerPos = self->startPos - off;
1200 }
1201 }
1202 else /* speed == 0 */
1203 {
1204 self->lastDir = 0;
1205
1206 for (i = 0; i < (self->bufsize * self->sndChnls); i++)
1207 {
1208 self->samplesBuffer[i] = 0.0;
1209 }
1210 }
1211 }
1212
1213 static void
1214 SfMarkerShuffler_setProcMode(SfMarkerShuffler *self)
1215 {
1216 self->proc_func_ptr = SfMarkerShuffler_readframes_i;
1217 }
1218
1219 static void
1220 SfMarkerShuffler_compute_next_data_frame(SfMarkerShuffler *self)
1221 {
1222 (*self->proc_func_ptr)(self);
1223 }
1224
1225 static void
1226 SfMarkerShuffler_chooseNewMark(SfMarkerShuffler *self, int dir)
1227 {
1228 int mark;
1229
1230 if (dir == 1)
1231 {
1232 if (self->startPos == -1)
1233 {
1234 mark = (int)(self->markers_size * (*self->type_func_ptr)(self) * 0.99);
1235 self->startPos = self->markers[mark];
1236 self->endPos = self->markers[mark + 1];
1237 }
1238 else
1239 {
1240 self->startPos = self->nextStartPos;
1241 self->endPos = self->nextEndPos;
1242 }
1243
1244 mark = (int)(self->markers_size * (*self->type_func_ptr)(self) * 0.99);
1245 self->nextStartPos = self->markers[mark];
1246 self->nextEndPos = self->markers[mark + 1];
1247 }
1248 else
1249 {
1250 if (self->startPos == -1)
1251 {
1252 mark = self->markers_size - (int)(self->markers_size * (*self->type_func_ptr)(self) * 0.99);
1253 self->startPos = self->markers[mark];
1254 self->endPos = self->markers[mark - 1];
1255 }
1256 else
1257 {
1258 self->startPos = self->nextStartPos;
1259 self->endPos = self->nextEndPos;
1260 }
1261
1262 mark = self->markers_size - (int)(self->markers_size * (*self->type_func_ptr)(self) * 0.99);
1263 self->nextStartPos = self->markers[mark];
1264 self->nextEndPos = self->markers[mark - 1];
1265 }
1266 }
1267
1268 static void
1269 SfMarkerShuffler_setMarkers(SfMarkerShuffler *self)
1270 {
1271 Py_ssize_t i;
1272 SF_CUES cues;
1273 if (sf_command(self->sf, SFC_GET_CUE, &cues, sizeof(cues)))
1274 {
1275 self->markers = (MYFLT *)PyMem_RawRealloc(self->markers, (cues.cue_count + 2) * sizeof(MYFLT));
1276 self->markers[0] = 0.;
1277
1278 for (i = 0; i < cues.cue_count; i++)
1279 {
1280 self->markers[i + 1] = (MYFLT)(cues.cue_points[i].sample_offset);
1281 }
1282
1283 self->markers[cues.cue_count + 1] = self->sndSize;
1284 self->markers_size = (int)cues.cue_count + 1;
1285 }
1286 else
1287 {
1288 self->markers = NULL;
1289 self->markers_size = 0;
1290 }
1291 }
1292
1293 static int
1294 SfMarkerShuffler_traverse(SfMarkerShuffler *self, visitproc visit, void *arg)
1295 {
1296 pyo_VISIT
1297 Py_VISIT(self->speed);
1298 return 0;
1299 }
1300
1301 static int
1302 SfMarkerShuffler_clear(SfMarkerShuffler *self)
1303 {
1304 pyo_CLEAR
1305 Py_CLEAR(self->speed);
1306 return 0;
1307 }
1308
1309 static void
1310 SfMarkerShuffler_dealloc(SfMarkerShuffler* self)
1311 {
1312 pyo_DEALLOC
1313
1314 if (self->sf != NULL)
1315 sf_close(self->sf);
1316
1317 PyMem_RawFree(self->samplesBuffer);
1318 if (self->markers)
1319 PyMem_RawFree(self->markers);
1320 SfMarkerShuffler_clear(self);
1321 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1322 Py_TYPE(self)->tp_free((PyObject*)self);
1323 }
1324
1325 static PyObject *
1326 SfMarkerShuffler_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1327 {
1328 int i;
1329 Py_ssize_t psize;
1330 PyObject *speedtmp = NULL;
1331 SfMarkerShuffler *self;
1332 self = (SfMarkerShuffler *)type->tp_alloc(type, 0);
1333
1334 self->speed = PyFloat_FromDouble(1);
1335 self->interp = 2;
1336 self->startPos = -1;
1337 self->endPos = -1;
1338 self->modebuffer[0] = 0;
1339 self->lastDir = 1;
1340 self->x = 0.5;
1341 INIT_OBJECT_COMMON
1342 Stream_setFunctionPtr(self->stream, SfMarkerShuffler_compute_next_data_frame);
1343 self->mode_func_ptr = SfMarkerShuffler_setProcMode;
1344
1345 static char *kwlist[] = {"path", "speed", "interp", NULL};
1346
1347 if (! PyArg_ParseTupleAndKeywords(args, kwds, "s#|Oi", kwlist, &self->path, &psize, &speedtmp, &self->interp))
1348 Py_RETURN_NONE;
1349
1350 if (speedtmp)
1351 {
1352 PyObject_CallMethod((PyObject *)self, "setSpeed", "O", speedtmp);
1353 }
1354
1355 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1356
1357 (*self->mode_func_ptr)(self);
1358
1359 self->type_func_ptr = SfMarkerShuffler_uniform;
1360
1361 if (self->interp == 0)
1362 self->interp = 2;
1363
1364 if (self->interp == 1)
1365 self->interp_func_ptr = nointerp;
1366 else if (self->interp == 2)
1367 self->interp_func_ptr = linear;
1368 else if (self->interp == 3)
1369 self->interp_func_ptr = cosine;
1370 else if (self->interp == 4)
1371 self->interp_func_ptr = cubic;
1372
1373 /* Open the sound file. */
1374 self->info.format = 0;
1375 self->sf = sf_open(self->path, SFM_READ, &self->info);
1376
1377 if (self->sf == NULL)
1378 {
1379 PySys_WriteStdout("SfMarkerShuffler: failed to open the file.\n");
1380 Py_RETURN_NONE;
1381 }
1382
1383 self->sndSize = self->info.frames;
1384 self->sndSr = self->info.samplerate;
1385 self->sndChnls = self->info.channels;
1386 self->srScale = self->sndSr / self->sr;
1387
1388 SfMarkerShuffler_setMarkers((SfMarkerShuffler *)self);
1389
1390 self->samplesBuffer = (MYFLT *)PyMem_RawRealloc(self->samplesBuffer, self->bufsize * self->sndChnls * sizeof(MYFLT));
1391
1392 Server_generateSeed((Server *)self->server, SFMARKERSHUFFLER_ID);
1393
1394 return (PyObject *)self;
1395 }
1396
1397 static PyObject * SfMarkerShuffler_getServer(SfMarkerShuffler* self) { GET_SERVER };
1398 static PyObject * SfMarkerShuffler_getStream(SfMarkerShuffler* self) { GET_STREAM };
1399
1400 static PyObject * SfMarkerShuffler_play(SfMarkerShuffler *self, PyObject *args, PyObject *kwds) { PLAY };
1401 static PyObject * SfMarkerShuffler_out(SfMarkerShuffler *self, PyObject *args, PyObject *kwds) { OUT };
1402 static PyObject * SfMarkerShuffler_stop(SfMarkerShuffler *self, PyObject *args, PyObject *kwds) { STOP };
1403
1404 static PyObject * SfMarkerShuffler_setSpeed(SfMarkerShuffler *self, PyObject *arg) { SET_PARAM(self->speed, self->speed_stream, 0); }
1405
1406 static PyObject *
1407 SfMarkerShuffler_setInterp(SfMarkerShuffler *self, PyObject *arg)
1408 {
1409 ASSERT_ARG_NOT_NULL
1410
1411 if (PyNumber_Check(arg))
1412 {
1413 self->interp = PyLong_AsLong(PyNumber_Long(arg));
1414 }
1415
1416 if (self->interp == 0)
1417 self->interp = 2;
1418
1419 if (self->interp == 1)
1420 self->interp_func_ptr = nointerp;
1421 else if (self->interp == 2)
1422 self->interp_func_ptr = linear;
1423 else if (self->interp == 3)
1424 self->interp_func_ptr = cosine;
1425 else if (self->interp == 4)
1426 self->interp_func_ptr = cubic;
1427
1428 Py_RETURN_NONE;
1429 }
1430
1431 static PyObject *
1432 SfMarkerShuffler_setRandomType(SfMarkerShuffler *self, PyObject *args, PyObject *kwds)
1433 {
1434 int dist = 0;
1435 MYFLT x = 0.5;
1436
1437 static char *kwlist[] = {"dist", "x", NULL};
1438
1439 if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE__IF, kwlist, &dist, &x))
1440 Py_RETURN_NONE;
1441
1442 if (dist < 0 || dist > 9)
1443 {
1444 Py_RETURN_NONE;
1445 }
1446
1447 if (x < 0.0)
1448 self->x = 0.0;
1449 else if (x >= 1.0)
1450 self->x = 1.0;
1451 else
1452 self->x = x;
1453
1454 switch (dist)
1455 {
1456 case 0:
1457 self->type_func_ptr = SfMarkerShuffler_uniform;
1458 break;
1459
1460 case 1:
1461 self->type_func_ptr = SfMarkerShuffler_linear_min;
1462 break;
1463
1464 case 2:
1465 self->type_func_ptr = SfMarkerShuffler_linear_max;
1466 break;
1467
1468 case 3:
1469 self->type_func_ptr = SfMarkerShuffler_triangle;
1470 break;
1471
1472 case 4:
1473 self->type_func_ptr = SfMarkerShuffler_expon_min;
1474 self->x *= 10.0;
1475 break;
1476
1477 case 5:
1478 self->type_func_ptr = SfMarkerShuffler_expon_max;
1479 self->x *= 10.0;
1480 break;
1481
1482 case 6:
1483 self->type_func_ptr = SfMarkerShuffler_biexpon;
1484 self->x *= 10.0;
1485 break;
1486
1487 case 7:
1488 self->type_func_ptr = SfMarkerShuffler_cauchy;
1489 self->x *= 10.0;
1490 self->x = 10.0 - self->x;
1491 break;
1492
1493 case 8:
1494 self->type_func_ptr = SfMarkerShuffler_weibull;
1495 self->x = self->x * 5.0 + 0.1;
1496 break;
1497
1498 case 9:
1499 self->type_func_ptr = SfMarkerShuffler_gaussian;
1500 self->x *= 10.0;
1501 self->x = 10.0 - self->x;
1502 break;
1503 }
1504
1505 Py_RETURN_NONE;
1506 }
1507
1508 MYFLT *
1509 SfMarkerShuffler_getSamplesBuffer(SfMarkerShuffler *self)
1510 {
1511 return (MYFLT *)self->samplesBuffer;
1512 }
1513
1514 PyObject *
1515 SfMarkerShuffler_getMarkers(SfMarkerShuffler *self)
1516 {
1517 PyObject* l = PyList_New(0);
1518 if (self->markers_size > 0)
1519 {
1520 Py_ssize_t i;
1521 for (i=0; i < self->markers_size; i++)
1522 {
1523 PyList_Append(l, PyLong_FromLong((long)self->markers[i]));
1524 }
1525 }
1526 Py_INCREF(l);
1527 return l;
1528 }
1529
1530 static PyMemberDef SfMarkerShuffler_members[] =
1531 {
1532 {"server", T_OBJECT_EX, offsetof(SfMarkerShuffler, server), 0, "Pyo server."},
1533 {"stream", T_OBJECT_EX, offsetof(SfMarkerShuffler, stream), 0, "Stream object."},
1534 {"speed", T_OBJECT_EX, offsetof(SfMarkerShuffler, speed), 0, "Frequency in cycle per second."},
1535 {NULL} /* Sentinel */
1536 };
1537
1538 static PyMethodDef SfMarkerShuffler_methods[] =
1539 {
1540 {"getServer", (PyCFunction)SfMarkerShuffler_getServer, METH_NOARGS, "Returns server object."},
1541 {"_getStream", (PyCFunction)SfMarkerShuffler_getStream, METH_NOARGS, "Returns stream object."},
1542 {"play", (PyCFunction)SfMarkerShuffler_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1543 {"out", (PyCFunction)SfMarkerShuffler_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1544 {"stop", (PyCFunction)SfMarkerShuffler_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1545 {"setSpeed", (PyCFunction)SfMarkerShuffler_setSpeed, METH_O, "Sets sfplayer reading speed."},
1546 {"setInterp", (PyCFunction)SfMarkerShuffler_setInterp, METH_O, "Sets sfplayer interpolation mode."},
1547 {"setRandomType", (PyCFunction)SfMarkerShuffler_setRandomType, METH_VARARGS | METH_KEYWORDS, "Sets sfplayer random type."},
1548 {"getMarkers", (PyCFunction)SfMarkerShuffler_getMarkers, METH_NOARGS, "Returns all marker positions as a list."},
1549 {NULL} /* Sentinel */
1550 };
1551
1552 PyTypeObject SfMarkerShufflerType =
1553 {
1554 PyVarObject_HEAD_INIT(NULL, 0)
1555 "_pyo.SfMarkerShuffler_base", /*tp_name*/
1556 sizeof(SfMarkerShuffler), /*tp_basicsize*/
1557 0, /*tp_itemsize*/
1558 (destructor)SfMarkerShuffler_dealloc, /*tp_dealloc*/
1559 0, /*tp_print*/
1560 0, /*tp_getattr*/
1561 0, /*tp_setattr*/
1562 0, /*tp_as_async (tp_compare in Python 2)*/
1563 0, /*tp_repr*/
1564 0, /*tp_as_number*/
1565 0, /*tp_as_sequence*/
1566 0, /*tp_as_mapping*/
1567 0, /*tp_hash */
1568 0, /*tp_call*/
1569 0, /*tp_str*/
1570 0, /*tp_getattro*/
1571 0, /*tp_setattro*/
1572 0, /*tp_as_buffer*/
1573 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1574 "SfMarkerShuffler objects. Shuffle an AIFF soundfile from markers points.", /* tp_doc */
1575 (traverseproc)SfMarkerShuffler_traverse, /* tp_traverse */
1576 (inquiry)SfMarkerShuffler_clear, /* tp_clear */
1577 0, /* tp_richcompare */
1578 0, /* tp_weaklistoffset */
1579 0, /* tp_iter */
1580 0, /* tp_iternext */
1581 SfMarkerShuffler_methods, /* tp_methods */
1582 SfMarkerShuffler_members, /* tp_members */
1583 0, /* tp_getset */
1584 0, /* tp_base */
1585 0, /* tp_dict */
1586 0, /* tp_descr_get */
1587 0, /* tp_descr_set */
1588 0, /* tp_dictoffset */
1589 0, /* tp_init */
1590 0, /* tp_alloc */
1591 SfMarkerShuffler_new, /* tp_new */
1592 };
1593
1594 /************************************************************************************************/
1595 /* SfMarkerShuffle streamer object per channel */
1596 /************************************************************************************************/
1597 typedef struct
1598 {
1599 pyo_audio_HEAD
1600 SfMarkerShuffler *mainPlayer;
1601 int modebuffer[2];
1602 int chnl;
1603 } SfMarkerShuffle;
1604
1605 static void SfMarkerShuffle_postprocessing_ii(SfMarkerShuffle *self) { POST_PROCESSING_II };
1606 static void SfMarkerShuffle_postprocessing_ai(SfMarkerShuffle *self) { POST_PROCESSING_AI };
1607 static void SfMarkerShuffle_postprocessing_ia(SfMarkerShuffle *self) { POST_PROCESSING_IA };
1608 static void SfMarkerShuffle_postprocessing_aa(SfMarkerShuffle *self) { POST_PROCESSING_AA };
1609 static void SfMarkerShuffle_postprocessing_ireva(SfMarkerShuffle *self) { POST_PROCESSING_IREVA };
1610 static void SfMarkerShuffle_postprocessing_areva(SfMarkerShuffle *self) { POST_PROCESSING_AREVA };
1611 static void SfMarkerShuffle_postprocessing_revai(SfMarkerShuffle *self) { POST_PROCESSING_REVAI };
1612 static void SfMarkerShuffle_postprocessing_revaa(SfMarkerShuffle *self) { POST_PROCESSING_REVAA };
1613 static void SfMarkerShuffle_postprocessing_revareva(SfMarkerShuffle *self) { POST_PROCESSING_REVAREVA };
1614
1615 static void
1616 SfMarkerShuffle_setProcMode(SfMarkerShuffle *self)
1617 {
1618 int muladdmode;
1619 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
1620
1621 switch (muladdmode)
1622 {
1623 case 0:
1624 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_ii;
1625 break;
1626
1627 case 1:
1628 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_ai;
1629 break;
1630
1631 case 2:
1632 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_revai;
1633 break;
1634
1635 case 10:
1636 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_ia;
1637 break;
1638
1639 case 11:
1640 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_aa;
1641 break;
1642
1643 case 12:
1644 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_revaa;
1645 break;
1646
1647 case 20:
1648 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_ireva;
1649 break;
1650
1651 case 21:
1652 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_areva;
1653 break;
1654
1655 case 22:
1656 self->muladd_func_ptr = SfMarkerShuffle_postprocessing_revareva;
1657 break;
1658 }
1659 }
1660
1661 static void
1662 SfMarkerShuffle_compute_next_data_frame(SfMarkerShuffle *self)
1663 {
1664 int i;
1665 MYFLT *tmp;
1666 int offset = self->chnl * self->bufsize;
1667 tmp = SfMarkerShuffler_getSamplesBuffer((SfMarkerShuffler *)self->mainPlayer);
1668
1669 for (i = 0; i < self->bufsize; i++)
1670 {
1671 self->data[i] = tmp[i + offset];
1672 }
1673
1674 (*self->muladd_func_ptr)(self);
1675 }
1676
1677 static int
1678 SfMarkerShuffle_traverse(SfMarkerShuffle *self, visitproc visit, void *arg)
1679 {
1680 pyo_VISIT
1681 Py_VISIT(self->mainPlayer);
1682 return 0;
1683 }
1684
1685 static int
1686 SfMarkerShuffle_clear(SfMarkerShuffle *self)
1687 {
1688 pyo_CLEAR
1689 Py_CLEAR(self->mainPlayer);
1690 return 0;
1691 }
1692
1693 static void
1694 SfMarkerShuffle_dealloc(SfMarkerShuffle* self)
1695 {
1696 pyo_DEALLOC
1697 SfMarkerShuffle_clear(self);
1698 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
1699 Py_TYPE(self)->tp_free((PyObject*)self);
1700 }
1701
1702 static PyObject *
1703 SfMarkerShuffle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1704 {
1705 int i;
1706 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
1707 SfMarkerShuffle *self;
1708 self = (SfMarkerShuffle *)type->tp_alloc(type, 0);
1709
1710 self->chnl = 0;
1711 self->modebuffer[0] = 0;
1712 self->modebuffer[1] = 0;
1713
1714 INIT_OBJECT_COMMON
1715 Stream_setFunctionPtr(self->stream, SfMarkerShuffle_compute_next_data_frame);
1716 self->mode_func_ptr = SfMarkerShuffle_setProcMode;
1717
1718 static char *kwlist[] = {"mainPlayer", "chnl", "mul", "add", NULL};
1719
1720 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
1721 Py_RETURN_NONE;
1722
1723 self->mainPlayer = (SfMarkerShuffler *)maintmp;
1724 Py_INCREF(self->mainPlayer);
1725
1726 if (multmp)
1727 {
1728 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
1729 }
1730
1731 if (addtmp)
1732 {
1733 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
1734 }
1735
1736 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
1737
1738 (*self->mode_func_ptr)(self);
1739
1740 return (PyObject *)self;
1741 }
1742
1743 static PyObject * SfMarkerShuffle_getServer(SfMarkerShuffle* self) { GET_SERVER };
1744 static PyObject * SfMarkerShuffle_getStream(SfMarkerShuffle* self) { GET_STREAM };
1745 static PyObject * SfMarkerShuffle_setMul(SfMarkerShuffle *self, PyObject *arg) { SET_MUL };
1746 static PyObject * SfMarkerShuffle_setAdd(SfMarkerShuffle *self, PyObject *arg) { SET_ADD };
1747 static PyObject * SfMarkerShuffle_setSub(SfMarkerShuffle *self, PyObject *arg) { SET_SUB };
1748 static PyObject * SfMarkerShuffle_setDiv(SfMarkerShuffle *self, PyObject *arg) { SET_DIV };
1749
1750 static PyObject * SfMarkerShuffle_play(SfMarkerShuffle *self, PyObject *args, PyObject *kwds) { PLAY };
1751 static PyObject * SfMarkerShuffle_out(SfMarkerShuffle *self, PyObject *args, PyObject *kwds) { OUT };
1752 static PyObject * SfMarkerShuffle_stop(SfMarkerShuffle *self, PyObject *args, PyObject *kwds) { STOP };
1753
1754 static PyObject * SfMarkerShuffle_multiply(SfMarkerShuffle *self, PyObject *arg) { MULTIPLY };
1755 static PyObject * SfMarkerShuffle_inplace_multiply(SfMarkerShuffle *self, PyObject *arg) { INPLACE_MULTIPLY };
1756 static PyObject * SfMarkerShuffle_add(SfMarkerShuffle *self, PyObject *arg) { ADD };
1757 static PyObject * SfMarkerShuffle_inplace_add(SfMarkerShuffle *self, PyObject *arg) { INPLACE_ADD };
1758 static PyObject * SfMarkerShuffle_sub(SfMarkerShuffle *self, PyObject *arg) { SUB };
1759 static PyObject * SfMarkerShuffle_inplace_sub(SfMarkerShuffle *self, PyObject *arg) { INPLACE_SUB };
1760 static PyObject * SfMarkerShuffle_div(SfMarkerShuffle *self, PyObject *arg) { DIV };
1761 static PyObject * SfMarkerShuffle_inplace_div(SfMarkerShuffle *self, PyObject *arg) { INPLACE_DIV };
1762
1763 static PyMemberDef SfMarkerShuffle_members[] =
1764 {
1765 {"server", T_OBJECT_EX, offsetof(SfMarkerShuffle, server), 0, "Pyo server."},
1766 {"stream", T_OBJECT_EX, offsetof(SfMarkerShuffle, stream), 0, "Stream object."},
1767 {"mul", T_OBJECT_EX, offsetof(SfMarkerShuffle, mul), 0, "Mul factor."},
1768 {"add", T_OBJECT_EX, offsetof(SfMarkerShuffle, add), 0, "Add factor."},
1769 {NULL} /* Sentinel */
1770 };
1771
1772 static PyMethodDef SfMarkerShuffle_methods[] =
1773 {
1774 {"getServer", (PyCFunction)SfMarkerShuffle_getServer, METH_NOARGS, "Returns server object."},
1775 {"_getStream", (PyCFunction)SfMarkerShuffle_getStream, METH_NOARGS, "Returns stream object."},
1776 {"play", (PyCFunction)SfMarkerShuffle_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
1777 {"out", (PyCFunction)SfMarkerShuffle_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
1778 {"stop", (PyCFunction)SfMarkerShuffle_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
1779 {"setMul", (PyCFunction)SfMarkerShuffle_setMul, METH_O, "Sets SfMarkerShuffle mul factor."},
1780 {"setAdd", (PyCFunction)SfMarkerShuffle_setAdd, METH_O, "Sets SfMarkerShuffle add factor."},
1781 {"setSub", (PyCFunction)SfMarkerShuffle_setSub, METH_O, "Sets inverse add factor."},
1782 {"setDiv", (PyCFunction)SfMarkerShuffle_setDiv, METH_O, "Sets inverse mul factor."},
1783 {NULL} /* Sentinel */
1784 };
1785
1786 static PyNumberMethods SfMarkerShuffle_as_number =
1787 {
1788 (binaryfunc)SfMarkerShuffle_add, /*nb_add*/
1789 (binaryfunc)SfMarkerShuffle_sub, /*nb_subtract*/
1790 (binaryfunc)SfMarkerShuffle_multiply, /*nb_multiply*/
1791 0, /*nb_remainder*/
1792 0, /*nb_divmod*/
1793 0, /*nb_power*/
1794 0, /*nb_neg*/
1795 0, /*nb_pos*/
1796 0, /*(unaryfunc)array_abs,*/
1797 0, /*nb_nonzero*/
1798 0, /*nb_invert*/
1799 0, /*nb_lshift*/
1800 0, /*nb_rshift*/
1801 0, /*nb_and*/
1802 0, /*nb_xor*/
1803 0, /*nb_or*/
1804 0, /*nb_int*/
1805 0, /*nb_long*/
1806 0, /*nb_float*/
1807 (binaryfunc)SfMarkerShuffle_inplace_add, /*inplace_add*/
1808 (binaryfunc)SfMarkerShuffle_inplace_sub, /*inplace_subtract*/
1809 (binaryfunc)SfMarkerShuffle_inplace_multiply, /*inplace_multiply*/
1810 0, /*inplace_remainder*/
1811 0, /*inplace_power*/
1812 0, /*inplace_lshift*/
1813 0, /*inplace_rshift*/
1814 0, /*inplace_and*/
1815 0, /*inplace_xor*/
1816 0, /*inplace_or*/
1817 0, /*nb_floor_divide*/
1818 (binaryfunc)SfMarkerShuffle_div, /*nb_true_divide*/
1819 0, /*nb_inplace_floor_divide*/
1820 (binaryfunc)SfMarkerShuffle_inplace_div, /*nb_inplace_true_divide*/
1821 0, /* nb_index */
1822 };
1823
1824 PyTypeObject SfMarkerShuffleType =
1825 {
1826 PyVarObject_HEAD_INIT(NULL, 0)
1827 "_pyo.SfMarkerShuffle_base", /*tp_name*/
1828 sizeof(SfMarkerShuffle), /*tp_basicsize*/
1829 0, /*tp_itemsize*/
1830 (destructor)SfMarkerShuffle_dealloc, /*tp_dealloc*/
1831 0, /*tp_print*/
1832 0, /*tp_getattr*/
1833 0, /*tp_setattr*/
1834 0, /*tp_as_async (tp_compare in Python 2)*/
1835 0, /*tp_repr*/
1836 &SfMarkerShuffle_as_number, /*tp_as_number*/
1837 0, /*tp_as_sequence*/
1838 0, /*tp_as_mapping*/
1839 0, /*tp_hash */
1840 0, /*tp_call*/
1841 0, /*tp_str*/
1842 0, /*tp_getattro*/
1843 0, /*tp_setattro*/
1844 0, /*tp_as_buffer*/
1845 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
1846 "SfMarkerShuffle objects. Reads a channel from a soundfile directly from disk.", /* tp_doc */
1847 (traverseproc)SfMarkerShuffle_traverse, /* tp_traverse */
1848 (inquiry)SfMarkerShuffle_clear, /* tp_clear */
1849 0, /* tp_richcompare */
1850 0, /* tp_weaklistoffset */
1851 0, /* tp_iter */
1852 0, /* tp_iternext */
1853 SfMarkerShuffle_methods, /* tp_methods */
1854 SfMarkerShuffle_members, /* tp_members */
1855 0, /* tp_getset */
1856 0, /* tp_base */
1857 0, /* tp_dict */
1858 0, /* tp_descr_get */
1859 0, /* tp_descr_set */
1860 0, /* tp_dictoffset */
1861 0, /* tp_init */
1862 0, /* tp_alloc */
1863 SfMarkerShuffle_new, /* tp_new */
1864 };
1865
1866 /************************************************************************************************/
1867 /* SfMarkerLooper object */
1868 /************************************************************************************************/
1869 typedef struct
1870 {
1871 pyo_audio_HEAD
1872 PyObject *speed;
1873 Stream *speed_stream;
1874 PyObject *mark;
1875 Stream *mark_stream;
1876 int modebuffer[2];
1877 SNDFILE *sf;
1878 SF_INFO info;
1879 char *path;
1880 int interp; /* 0 = default to 2, 1 = nointerp, 2 = linear, 3 = cos, 4 = cubic */
1881 sf_count_t sndSize; /* number of frames */
1882 int sndChnls;
1883 int sndSr;
1884 MYFLT srScale;
1885 MYFLT startPos;
1886 MYFLT endPos;
1887 MYFLT nextStartPos;
1888 MYFLT nextEndPos;
1889 double pointerPos;
1890 MYFLT *samplesBuffer;
1891 MYFLT *markers;
1892 int markers_size;
1893 int old_mark;
1894 int lastDir;
1895 MYFLT (*interp_func_ptr)(MYFLT *, T_SIZE_T, MYFLT, T_SIZE_T);
1896 } SfMarkerLooper;
1897
1898 /*** PROTOTYPES ***/
1899 static void SfMarkerLooper_chooseNewMark(SfMarkerLooper *self, int dir);
1900 /******************/
1901
1902 static void
1903 SfMarkerLooper_readframes_i(SfMarkerLooper *self)
1904 {
1905 MYFLT sp, frac, bufpos, delta, tmp;
1906 int i, j, shortbuflen;
1907 T_SIZE_T totlen, buflen, bufindex;
1908 sf_count_t index;
1909
1910 if (self->modebuffer[0] == 0)
1911 sp = PyFloat_AS_DOUBLE(self->speed);
1912 else
1913 sp = Stream_getData((Stream *)self->speed_stream)[0];
1914
1915 delta = MYFABS(sp) * self->srScale;
1916
1917 buflen = (T_SIZE_T)(self->bufsize * delta + 0.5) + 64;
1918 totlen = self->sndChnls * buflen;
1919 MYFLT buffer[totlen];
1920 MYFLT buffer2[self->sndChnls][buflen];
1921
1922 if (sp > 0) /* reading forward */
1923 {
1924 if (self->startPos == -1 || self->lastDir == 0)
1925 {
1926 self->lastDir = 1;
1927 SfMarkerLooper_chooseNewMark((SfMarkerLooper *)self, 1);
1928 self->pointerPos = self->startPos;
1929 }
1930
1931 if (self->lastDir != 1)
1932 {
1933 tmp = self->startPos;
1934 self->startPos = self->endPos;
1935 self->endPos = tmp;
1936 self->lastDir = 1;
1937 }
1938
1939 index = (sf_count_t)self->pointerPos;
1940 sf_seek(self->sf, index, SEEK_SET); /* sets position pointer in the file */
1941
1942 /* fill a buffer with enough samples to satisfy speed reading */
1943 /* if not enough samples to read in the file */
1944 if ((index + buflen) > self->endPos)
1945 {
1946 shortbuflen = self->endPos - index;
1947 SF_READ(self->sf, buffer, shortbuflen * self->sndChnls);
1948
1949 /* wrap around and read new samples if loop */
1950 int pad = buflen - shortbuflen;
1951 int padlen = pad * self->sndChnls;
1952 MYFLT buftemp[padlen];
1953 sf_seek(self->sf, (sf_count_t)self->nextStartPos, SEEK_SET);
1954 SF_READ(self->sf, buftemp, padlen);
1955
1956 for (i = 0; i < (padlen); i++)
1957 {
1958 buffer[i + shortbuflen * self->sndChnls] = buftemp[i];
1959 }
1960 }
1961 else /* without zero padding */
1962 SF_READ(self->sf, buffer, totlen);
1963
1964 /* de-interleave samples */
1965 for (i = 0; i < totlen; i++)
1966 {
1967 buffer2[i % self->sndChnls][(T_SIZE_T)(i / self->sndChnls)] = buffer[i];
1968 }
1969
1970 /* fill data with samples */
1971 for (i = 0; i < self->bufsize; i++)
1972 {
1973 bufpos = self->pointerPos - index;
1974 bufindex = (T_SIZE_T)bufpos;
1975 frac = bufpos - bufindex;
1976
1977 for (j = 0; j < self->sndChnls; j++)
1978 {
1979 self->samplesBuffer[i + (j * self->bufsize)] = (*self->interp_func_ptr)(buffer2[j], bufindex, frac, buflen);
1980 }
1981
1982 self->pointerPos += delta;
1983 }
1984
1985 if (self->pointerPos >= self->endPos)
1986 {
1987 MYFLT off = self->pointerPos - self->endPos;
1988 SfMarkerLooper_chooseNewMark((SfMarkerLooper *)self, 1);
1989 self->pointerPos = self->startPos + off;
1990 }
1991 }
1992 else if (sp < 0) /* reading backward */
1993 {
1994 if (self->startPos == -1 || self->lastDir != -1)
1995 {
1996 self->lastDir = self->startPos = -1;
1997 SfMarkerLooper_chooseNewMark((SfMarkerLooper *)self, 0);
1998 self->pointerPos = self->startPos;
1999 }
2000
2001 index = (sf_count_t)self->pointerPos + 1;
2002
2003 /* fill a buffer with enough samples to satisfy speed reading */
2004 /* if not enough samples to read in the file */
2005 if ((index - buflen) < self->endPos)
2006 {
2007 shortbuflen = index - self->endPos;
2008 int pad = buflen - shortbuflen;
2009 int padlen = pad * self->sndChnls;
2010
2011 /* wrap around and read new samples if loop */
2012 MYFLT buftemp[padlen];
2013 sf_seek(self->sf, (sf_count_t)self->nextStartPos - pad, SEEK_SET);
2014 SF_READ(self->sf, buftemp, padlen);
2015
2016 for (i = 0; i < padlen; i++)
2017 {
2018 buffer[i] = buftemp[i];
2019 }
2020
2021 MYFLT buftemp2[shortbuflen * self->sndChnls];
2022 sf_seek(self->sf, (sf_count_t)self->endPos, SEEK_SET); /* sets position pointer in the file */
2023 SF_READ(self->sf, buftemp2, shortbuflen * self->sndChnls);
2024
2025 for (i = 0; i < (shortbuflen * self->sndChnls); i++)
2026 {
2027 buffer[i + padlen] = buftemp2[i];
2028 }
2029 }
2030 else /* without zero padding */
2031 {
2032 sf_seek(self->sf, index - buflen, SEEK_SET); /* sets position pointer in the file */
2033 SF_READ(self->sf, buffer, totlen);
2034 }
2035
2036 /* de-interleave samples */
2037 for (i = 0; i < totlen; i++)
2038 {
2039 buffer2[i % self->sndChnls][(T_SIZE_T)(i / self->sndChnls)] = buffer[i];
2040 }
2041
2042 /* reverse arrays */
2043 MYFLT swap;
2044
2045 for (i = 0; i < self->sndChnls; i++)
2046 {
2047 int a;
2048 int b = buflen;
2049
2050 for (a = 0; a < --b; a++) //increment a and decrement b until they meet eachother
2051 {
2052 swap = buffer2[i][a]; //put what's in a into swap space
2053 buffer2[i][a] = buffer2[i][b]; //put what's in b into a
2054 buffer2[i][b] = swap; //put what's in the swap (a) into b
2055 }
2056 }
2057
2058 /* fill stream buffer with samples */
2059 for (i = 0; i < self->bufsize; i++)
2060 {
2061 bufpos = index - self->pointerPos;
2062 bufindex = (T_SIZE_T)bufpos;
2063 frac = bufpos - bufindex;
2064
2065 for (j = 0; j < self->sndChnls; j++)
2066 {
2067 self->samplesBuffer[i + (j * self->bufsize)] = (*self->interp_func_ptr)(buffer2[j], bufindex, frac, buflen);
2068 }
2069
2070 self->pointerPos -= delta;
2071 }
2072
2073 if (self->pointerPos <= self->endPos)
2074 {
2075 MYFLT off = self->endPos - self->pointerPos;
2076 SfMarkerLooper_chooseNewMark((SfMarkerLooper *)self, 0);
2077 self->pointerPos = self->startPos - off;
2078 }
2079 }
2080 else /* speed == 0 */
2081 {
2082 self->lastDir = 0;
2083
2084 for (i = 0; i < (self->bufsize * self->sndChnls); i++)
2085 {
2086 self->samplesBuffer[i] = 0.0;
2087 }
2088 }
2089 }
2090
2091 static void
2092 SfMarkerLooper_setProcMode(SfMarkerLooper *self)
2093 {
2094 self->proc_func_ptr = SfMarkerLooper_readframes_i;
2095 }
2096
2097 static void
2098 SfMarkerLooper_compute_next_data_frame(SfMarkerLooper *self)
2099 {
2100 (*self->proc_func_ptr)(self);
2101 }
2102
2103 static void
2104 SfMarkerLooper_chooseNewMark(SfMarkerLooper *self, int dir)
2105 {
2106 int mark;
2107
2108 if (self->modebuffer[1] == 0)
2109 mark = (int)(PyFloat_AS_DOUBLE(self->mark));
2110 else
2111 mark = (int)(Stream_getData((Stream *)self->mark_stream)[0]);
2112
2113 if (mark < 0 || mark >= self->markers_size)
2114 {
2115 mark = self->markers_size / 2;
2116 }
2117
2118 self->old_mark = mark;
2119
2120 if (dir == 1)
2121 {
2122 if (self->startPos == -1)
2123 {
2124 self->startPos = self->markers[mark];
2125 self->endPos = self->markers[mark + 1];
2126 }
2127 else
2128 {
2129 self->startPos = self->nextStartPos;
2130 self->endPos = self->nextEndPos;
2131 }
2132
2133 self->nextStartPos = self->markers[mark];
2134 self->nextEndPos = self->markers[mark + 1];
2135 }
2136 else
2137 {
2138 mark = self->markers_size - mark;
2139
2140 if (self->startPos == -1)
2141 {
2142 self->startPos = self->markers[mark];
2143 self->endPos = self->markers[mark - 1];
2144 }
2145 else
2146 {
2147 self->startPos = self->nextStartPos;
2148 self->endPos = self->nextEndPos;
2149 }
2150
2151 self->nextStartPos = self->markers[mark];
2152 self->nextEndPos = self->markers[mark - 1];
2153 }
2154 }
2155
2156 static void
2157 SfMarkerLooper_setMarkers(SfMarkerLooper *self)
2158 {
2159 Py_ssize_t i;
2160 SF_CUES cues;
2161 if (sf_command(self->sf, SFC_GET_CUE, &cues, sizeof(cues)))
2162 {
2163 self->markers = (MYFLT *)PyMem_RawRealloc(self->markers, (cues.cue_count + 2) * sizeof(MYFLT));
2164 self->markers[0] = 0.;
2165
2166 for (i = 0; i < cues.cue_count; i++)
2167 {
2168 self->markers[i + 1] = (MYFLT)(cues.cue_points[i].sample_offset);
2169 }
2170
2171 self->markers[cues.cue_count + 1] = self->sndSize;
2172 self->markers_size = (int)cues.cue_count + 1;
2173 }
2174 else
2175 {
2176 self->markers = NULL;
2177 self->markers_size = 0;
2178 }
2179 }
2180
2181 static int
2182 SfMarkerLooper_traverse(SfMarkerLooper *self, visitproc visit, void *arg)
2183 {
2184 pyo_VISIT
2185 Py_VISIT(self->speed);
2186 Py_VISIT(self->mark);
2187 return 0;
2188 }
2189
2190 static int
2191 SfMarkerLooper_clear(SfMarkerLooper *self)
2192 {
2193 pyo_CLEAR
2194 Py_CLEAR(self->speed);
2195 Py_CLEAR(self->mark);
2196 return 0;
2197 }
2198
2199 static void
2200 SfMarkerLooper_dealloc(SfMarkerLooper* self)
2201 {
2202 pyo_DEALLOC
2203
2204 if (self->sf != NULL)
2205 sf_close(self->sf);
2206
2207 PyMem_RawFree(self->samplesBuffer);
2208 if (self->markers)
2209 PyMem_RawFree(self->markers);
2210 SfMarkerLooper_clear(self);
2211 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2212 Py_TYPE(self)->tp_free((PyObject*)self);
2213 }
2214
2215 static PyObject *
2216 SfMarkerLooper_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2217 {
2218 int i;
2219 Py_ssize_t psize;
2220 PyObject *speedtmp = NULL, *marktmp = NULL;
2221 SfMarkerLooper *self;
2222 self = (SfMarkerLooper *)type->tp_alloc(type, 0);
2223
2224 self->speed = PyFloat_FromDouble(1);
2225 self->mark = PyFloat_FromDouble(0);
2226 self->interp = 2;
2227 self->startPos = -1;
2228 self->endPos = -1;
2229 self->old_mark = -1;
2230 self->lastDir = 1;
2231 self->modebuffer[0] = 0;
2232 self->modebuffer[1] = 0;
2233
2234 INIT_OBJECT_COMMON
2235 Stream_setFunctionPtr(self->stream, SfMarkerLooper_compute_next_data_frame);
2236 self->mode_func_ptr = SfMarkerLooper_setProcMode;
2237
2238 static char *kwlist[] = {"path", "speed", "mark", "interp", NULL};
2239
2240 if (! PyArg_ParseTupleAndKeywords(args, kwds, "s#|OOi", kwlist, &self->path, &psize, &speedtmp, &marktmp, &self->interp))
2241 Py_RETURN_NONE;
2242
2243 if (speedtmp)
2244 {
2245 PyObject_CallMethod((PyObject *)self, "setSpeed", "O", speedtmp);
2246 }
2247
2248 if (marktmp)
2249 {
2250 PyObject_CallMethod((PyObject *)self, "setMark", "O", marktmp);
2251 }
2252
2253 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2254
2255 (*self->mode_func_ptr)(self);
2256
2257 if (self->interp == 0)
2258 self->interp = 2;
2259
2260 if (self->interp == 1)
2261 self->interp_func_ptr = nointerp;
2262 else if (self->interp == 2)
2263 self->interp_func_ptr = linear;
2264 else if (self->interp == 3)
2265 self->interp_func_ptr = cosine;
2266 else if (self->interp == 4)
2267 self->interp_func_ptr = cubic;
2268
2269 /* Open the sound file. */
2270 self->info.format = 0;
2271 self->sf = sf_open(self->path, SFM_READ, &self->info);
2272
2273 if (self->sf == NULL)
2274 {
2275 PySys_WriteStdout("SfMarkerLooper: failed to open the file.\n");
2276 Py_RETURN_NONE;
2277 }
2278
2279 self->sndSize = self->info.frames;
2280 self->sndSr = self->info.samplerate;
2281 self->sndChnls = self->info.channels;
2282 self->srScale = self->sndSr / self->sr;
2283
2284 SfMarkerLooper_setMarkers((SfMarkerLooper *)self);
2285
2286 self->samplesBuffer = (MYFLT *)PyMem_RawRealloc(self->samplesBuffer, self->bufsize * self->sndChnls * sizeof(MYFLT));
2287
2288 Server_generateSeed((Server *)self->server, SFMARKERLOOPER_ID);
2289
2290 return (PyObject *)self;
2291 }
2292
2293 static PyObject * SfMarkerLooper_getServer(SfMarkerLooper* self) { GET_SERVER };
2294 static PyObject * SfMarkerLooper_getStream(SfMarkerLooper* self) { GET_STREAM };
2295
2296 static PyObject * SfMarkerLooper_play(SfMarkerLooper *self, PyObject *args, PyObject *kwds) { PLAY };
2297 static PyObject * SfMarkerLooper_out(SfMarkerLooper *self, PyObject *args, PyObject *kwds) { OUT };
2298 static PyObject * SfMarkerLooper_stop(SfMarkerLooper *self, PyObject *args, PyObject *kwds) { STOP };
2299
2300 static PyObject * SfMarkerLooper_setSpeed(SfMarkerLooper *self, PyObject *arg) { SET_PARAM(self->speed, self->speed_stream, 0); }
2301 static PyObject * SfMarkerLooper_setMark(SfMarkerLooper *self, PyObject *arg) { SET_PARAM(self->mark, self->mark_stream, 1); }
2302
2303 static PyObject *
2304 SfMarkerLooper_setInterp(SfMarkerLooper *self, PyObject *arg)
2305 {
2306 ASSERT_ARG_NOT_NULL
2307
2308 if (PyNumber_Check(arg))
2309 {
2310 self->interp = PyLong_AsLong(PyNumber_Long(arg));
2311 }
2312
2313 if (self->interp == 0)
2314 self->interp = 2;
2315
2316 if (self->interp == 1)
2317 self->interp_func_ptr = nointerp;
2318 else if (self->interp == 2)
2319 self->interp_func_ptr = linear;
2320 else if (self->interp == 3)
2321 self->interp_func_ptr = cosine;
2322 else if (self->interp == 4)
2323 self->interp_func_ptr = cubic;
2324
2325 Py_RETURN_NONE;
2326 }
2327
2328 MYFLT *
2329 SfMarkerLooper_getSamplesBuffer(SfMarkerLooper *self)
2330 {
2331 return (MYFLT *)self->samplesBuffer;
2332 }
2333
2334 PyObject *
2335 SfMarkerLooper_getMarkers(SfMarkerLooper *self)
2336 {
2337 PyObject* l = PyList_New(0);
2338 if (self->markers_size > 0)
2339 {
2340 Py_ssize_t i;
2341 for (i=0; i < self->markers_size; i++)
2342 {
2343 PyList_Append(l, PyLong_FromLong((long)self->markers[i]));
2344 }
2345 }
2346 Py_INCREF(l);
2347 return l;
2348 }
2349
2350 static PyMemberDef SfMarkerLooper_members[] =
2351 {
2352 {"server", T_OBJECT_EX, offsetof(SfMarkerLooper, server), 0, "Pyo server."},
2353 {"stream", T_OBJECT_EX, offsetof(SfMarkerLooper, stream), 0, "Stream object."},
2354 {"speed", T_OBJECT_EX, offsetof(SfMarkerLooper, speed), 0, "Frequency in cycle per second."},
2355 {"mark", T_OBJECT_EX, offsetof(SfMarkerLooper, mark), 0, "Marker to loop."},
2356 {NULL} /* Sentinel */
2357 };
2358
2359 static PyMethodDef SfMarkerLooper_methods[] =
2360 {
2361 {"getServer", (PyCFunction)SfMarkerLooper_getServer, METH_NOARGS, "Returns server object."},
2362 {"_getStream", (PyCFunction)SfMarkerLooper_getStream, METH_NOARGS, "Returns stream object."},
2363 {"play", (PyCFunction)SfMarkerLooper_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2364 {"out", (PyCFunction)SfMarkerLooper_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2365 {"stop", (PyCFunction)SfMarkerLooper_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2366 {"setSpeed", (PyCFunction)SfMarkerLooper_setSpeed, METH_O, "Sets sfplayer reading speed."},
2367 {"setMark", (PyCFunction)SfMarkerLooper_setMark, METH_O, "Sets marker to loop."},
2368 {"setInterp", (PyCFunction)SfMarkerLooper_setInterp, METH_O, "Sets sfplayer interpolation mode."},
2369 {"getMarkers", (PyCFunction)SfMarkerLooper_getMarkers, METH_NOARGS, "Returns all marker positions as a list."},
2370 {NULL} /* Sentinel */
2371 };
2372
2373 PyTypeObject SfMarkerLooperType =
2374 {
2375 PyVarObject_HEAD_INIT(NULL, 0)
2376 "_pyo.SfMarkerLooper_base", /*tp_name*/
2377 sizeof(SfMarkerLooper), /*tp_basicsize*/
2378 0, /*tp_itemsize*/
2379 (destructor)SfMarkerLooper_dealloc, /*tp_dealloc*/
2380 0, /*tp_print*/
2381 0, /*tp_getattr*/
2382 0, /*tp_setattr*/
2383 0, /*tp_as_async (tp_compare in Python 2)*/
2384 0, /*tp_repr*/
2385 0, /*tp_as_number*/
2386 0, /*tp_as_sequence*/
2387 0, /*tp_as_mapping*/
2388 0, /*tp_hash */
2389 0, /*tp_call*/
2390 0, /*tp_str*/
2391 0, /*tp_getattro*/
2392 0, /*tp_setattro*/
2393 0, /*tp_as_buffer*/
2394 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2395 "SfMarkerLooper objects. Shuffle an AIFF soundfile from markers points.", /* tp_doc */
2396 (traverseproc)SfMarkerLooper_traverse, /* tp_traverse */
2397 (inquiry)SfMarkerLooper_clear, /* tp_clear */
2398 0, /* tp_richcompare */
2399 0, /* tp_weaklistoffset */
2400 0, /* tp_iter */
2401 0, /* tp_iternext */
2402 SfMarkerLooper_methods, /* tp_methods */
2403 SfMarkerLooper_members, /* tp_members */
2404 0, /* tp_getset */
2405 0, /* tp_base */
2406 0, /* tp_dict */
2407 0, /* tp_descr_get */
2408 0, /* tp_descr_set */
2409 0, /* tp_dictoffset */
2410 0, /* tp_init */
2411 0, /* tp_alloc */
2412 SfMarkerLooper_new, /* tp_new */
2413 };
2414
2415 /************************************************************************************************/
2416 /* SfMarkerLoop streamer object per channel */
2417 /************************************************************************************************/
2418 typedef struct
2419 {
2420 pyo_audio_HEAD
2421 SfMarkerLooper *mainPlayer;
2422 int modebuffer[2];
2423 int chnl;
2424 } SfMarkerLoop;
2425
2426 static void SfMarkerLoop_postprocessing_ii(SfMarkerLoop *self) { POST_PROCESSING_II };
2427 static void SfMarkerLoop_postprocessing_ai(SfMarkerLoop *self) { POST_PROCESSING_AI };
2428 static void SfMarkerLoop_postprocessing_ia(SfMarkerLoop *self) { POST_PROCESSING_IA };
2429 static void SfMarkerLoop_postprocessing_aa(SfMarkerLoop *self) { POST_PROCESSING_AA };
2430 static void SfMarkerLoop_postprocessing_ireva(SfMarkerLoop *self) { POST_PROCESSING_IREVA };
2431 static void SfMarkerLoop_postprocessing_areva(SfMarkerLoop *self) { POST_PROCESSING_AREVA };
2432 static void SfMarkerLoop_postprocessing_revai(SfMarkerLoop *self) { POST_PROCESSING_REVAI };
2433 static void SfMarkerLoop_postprocessing_revaa(SfMarkerLoop *self) { POST_PROCESSING_REVAA };
2434 static void SfMarkerLoop_postprocessing_revareva(SfMarkerLoop *self) { POST_PROCESSING_REVAREVA };
2435
2436 static void
2437 SfMarkerLoop_setProcMode(SfMarkerLoop *self)
2438 {
2439 int muladdmode;
2440 muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10;
2441
2442 switch (muladdmode)
2443 {
2444 case 0:
2445 self->muladd_func_ptr = SfMarkerLoop_postprocessing_ii;
2446 break;
2447
2448 case 1:
2449 self->muladd_func_ptr = SfMarkerLoop_postprocessing_ai;
2450 break;
2451
2452 case 2:
2453 self->muladd_func_ptr = SfMarkerLoop_postprocessing_revai;
2454 break;
2455
2456 case 10:
2457 self->muladd_func_ptr = SfMarkerLoop_postprocessing_ia;
2458 break;
2459
2460 case 11:
2461 self->muladd_func_ptr = SfMarkerLoop_postprocessing_aa;
2462 break;
2463
2464 case 12:
2465 self->muladd_func_ptr = SfMarkerLoop_postprocessing_revaa;
2466 break;
2467
2468 case 20:
2469 self->muladd_func_ptr = SfMarkerLoop_postprocessing_ireva;
2470 break;
2471
2472 case 21:
2473 self->muladd_func_ptr = SfMarkerLoop_postprocessing_areva;
2474 break;
2475
2476 case 22:
2477 self->muladd_func_ptr = SfMarkerLoop_postprocessing_revareva;
2478 break;
2479 }
2480 }
2481
2482 static void
2483 SfMarkerLoop_compute_next_data_frame(SfMarkerLoop *self)
2484 {
2485 int i;
2486 MYFLT *tmp;
2487 int offset = self->chnl * self->bufsize;
2488 tmp = SfMarkerLooper_getSamplesBuffer((SfMarkerLooper *)self->mainPlayer);
2489
2490 for (i = 0; i < self->bufsize; i++)
2491 {
2492 self->data[i] = tmp[i + offset];
2493 }
2494
2495 (*self->muladd_func_ptr)(self);
2496 }
2497
2498 static int
2499 SfMarkerLoop_traverse(SfMarkerLoop *self, visitproc visit, void *arg)
2500 {
2501 pyo_VISIT
2502 Py_VISIT(self->mainPlayer);
2503 return 0;
2504 }
2505
2506 static int
2507 SfMarkerLoop_clear(SfMarkerLoop *self)
2508 {
2509 pyo_CLEAR
2510 Py_CLEAR(self->mainPlayer);
2511 return 0;
2512 }
2513
2514 static void
2515 SfMarkerLoop_dealloc(SfMarkerLoop* self)
2516 {
2517 pyo_DEALLOC
2518 SfMarkerLoop_clear(self);
2519 Py_TYPE(self->stream)->tp_free((PyObject*)self->stream);
2520 Py_TYPE(self)->tp_free((PyObject*)self);
2521 }
2522
2523 static PyObject *
2524 SfMarkerLoop_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2525 {
2526 int i;
2527 PyObject *maintmp = NULL, *multmp = NULL, *addtmp = NULL;
2528 SfMarkerLoop *self;
2529 self = (SfMarkerLoop *)type->tp_alloc(type, 0);
2530
2531 self->chnl = 0;
2532 self->modebuffer[0] = 0;
2533 self->modebuffer[1] = 0;
2534
2535 INIT_OBJECT_COMMON
2536 Stream_setFunctionPtr(self->stream, SfMarkerLoop_compute_next_data_frame);
2537 self->mode_func_ptr = SfMarkerLoop_setProcMode;
2538
2539 static char *kwlist[] = {"mainPlayer", "chnl", "mul", "add", NULL};
2540
2541 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwlist, &maintmp, &self->chnl, &multmp, &addtmp))
2542 Py_RETURN_NONE;
2543
2544 self->mainPlayer = (SfMarkerLooper *)maintmp;
2545 Py_INCREF(self->mainPlayer);
2546
2547 if (multmp)
2548 {
2549 PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp);
2550 }
2551
2552 if (addtmp)
2553 {
2554 PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp);
2555 }
2556
2557 PyObject_CallMethod(self->server, "addStream", "O", self->stream);
2558
2559 (*self->mode_func_ptr)(self);
2560
2561 return (PyObject *)self;
2562 }
2563
2564 static PyObject * SfMarkerLoop_getServer(SfMarkerLoop* self) { GET_SERVER };
2565 static PyObject * SfMarkerLoop_getStream(SfMarkerLoop* self) { GET_STREAM };
2566 static PyObject * SfMarkerLoop_setMul(SfMarkerLoop *self, PyObject *arg) { SET_MUL };
2567 static PyObject * SfMarkerLoop_setAdd(SfMarkerLoop *self, PyObject *arg) { SET_ADD };
2568 static PyObject * SfMarkerLoop_setSub(SfMarkerLoop *self, PyObject *arg) { SET_SUB };
2569 static PyObject * SfMarkerLoop_setDiv(SfMarkerLoop *self, PyObject *arg) { SET_DIV };
2570
2571 static PyObject * SfMarkerLoop_play(SfMarkerLoop *self, PyObject *args, PyObject *kwds) { PLAY };
2572 static PyObject * SfMarkerLoop_out(SfMarkerLoop *self, PyObject *args, PyObject *kwds) { OUT };
2573 static PyObject * SfMarkerLoop_stop(SfMarkerLoop *self, PyObject *args, PyObject *kwds) { STOP };
2574
2575 static PyObject * SfMarkerLoop_multiply(SfMarkerLoop *self, PyObject *arg) { MULTIPLY };
2576 static PyObject * SfMarkerLoop_inplace_multiply(SfMarkerLoop *self, PyObject *arg) { INPLACE_MULTIPLY };
2577 static PyObject * SfMarkerLoop_add(SfMarkerLoop *self, PyObject *arg) { ADD };
2578 static PyObject * SfMarkerLoop_inplace_add(SfMarkerLoop *self, PyObject *arg) { INPLACE_ADD };
2579 static PyObject * SfMarkerLoop_sub(SfMarkerLoop *self, PyObject *arg) { SUB };
2580 static PyObject * SfMarkerLoop_inplace_sub(SfMarkerLoop *self, PyObject *arg) { INPLACE_SUB };
2581 static PyObject * SfMarkerLoop_div(SfMarkerLoop *self, PyObject *arg) { DIV };
2582 static PyObject * SfMarkerLoop_inplace_div(SfMarkerLoop *self, PyObject *arg) { INPLACE_DIV };
2583
2584 static PyMemberDef SfMarkerLoop_members[] =
2585 {
2586 {"server", T_OBJECT_EX, offsetof(SfMarkerLoop, server), 0, "Pyo server."},
2587 {"stream", T_OBJECT_EX, offsetof(SfMarkerLoop, stream), 0, "Stream object."},
2588 {"mul", T_OBJECT_EX, offsetof(SfMarkerLoop, mul), 0, "Mul factor."},
2589 {"add", T_OBJECT_EX, offsetof(SfMarkerLoop, add), 0, "Add factor."},
2590 {NULL} /* Sentinel */
2591 };
2592
2593 static PyMethodDef SfMarkerLoop_methods[] =
2594 {
2595 {"getServer", (PyCFunction)SfMarkerLoop_getServer, METH_NOARGS, "Returns server object."},
2596 {"_getStream", (PyCFunction)SfMarkerLoop_getStream, METH_NOARGS, "Returns stream object."},
2597 {"play", (PyCFunction)SfMarkerLoop_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."},
2598 {"out", (PyCFunction)SfMarkerLoop_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."},
2599 {"stop", (PyCFunction)SfMarkerLoop_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."},
2600 {"setMul", (PyCFunction)SfMarkerLoop_setMul, METH_O, "Sets SfMarkerLoop mul factor."},
2601 {"setAdd", (PyCFunction)SfMarkerLoop_setAdd, METH_O, "Sets SfMarkerLoop add factor."},
2602 {"setSub", (PyCFunction)SfMarkerLoop_setSub, METH_O, "Sets inverse add factor."},
2603 {"setDiv", (PyCFunction)SfMarkerLoop_setDiv, METH_O, "Sets inverse mul factor."},
2604 {NULL} /* Sentinel */
2605 };
2606
2607 static PyNumberMethods SfMarkerLoop_as_number =
2608 {
2609 (binaryfunc)SfMarkerLoop_add, /*nb_add*/
2610 (binaryfunc)SfMarkerLoop_sub, /*nb_subtract*/
2611 (binaryfunc)SfMarkerLoop_multiply, /*nb_multiply*/
2612 0, /*nb_remainder*/
2613 0, /*nb_divmod*/
2614 0, /*nb_power*/
2615 0, /*nb_neg*/
2616 0, /*nb_pos*/
2617 0, /*(unaryfunc)array_abs,*/
2618 0, /*nb_nonzero*/
2619 0, /*nb_invert*/
2620 0, /*nb_lshift*/
2621 0, /*nb_rshift*/
2622 0, /*nb_and*/
2623 0, /*nb_xor*/
2624 0, /*nb_or*/
2625 0, /*nb_int*/
2626 0, /*nb_long*/
2627 0, /*nb_float*/
2628 (binaryfunc)SfMarkerLoop_inplace_add, /*inplace_add*/
2629 (binaryfunc)SfMarkerLoop_inplace_sub, /*inplace_subtract*/
2630 (binaryfunc)SfMarkerLoop_inplace_multiply, /*inplace_multiply*/
2631 0, /*inplace_remainder*/
2632 0, /*inplace_power*/
2633 0, /*inplace_lshift*/
2634 0, /*inplace_rshift*/
2635 0, /*inplace_and*/
2636 0, /*inplace_xor*/
2637 0, /*inplace_or*/
2638 0, /*nb_floor_divide*/
2639 (binaryfunc)SfMarkerLoop_div, /*nb_true_divide*/
2640 0, /*nb_inplace_floor_divide*/
2641 (binaryfunc)SfMarkerLoop_inplace_div, /*nb_inplace_true_divide*/
2642 0, /* nb_index */
2643 };
2644
2645 PyTypeObject SfMarkerLoopType =
2646 {
2647 PyVarObject_HEAD_INIT(NULL, 0)
2648 "_pyo.SfMarkerLoop_base", /*tp_name*/
2649 sizeof(SfMarkerLoop), /*tp_basicsize*/
2650 0, /*tp_itemsize*/
2651 (destructor)SfMarkerLoop_dealloc, /*tp_dealloc*/
2652 0, /*tp_print*/
2653 0, /*tp_getattr*/
2654 0, /*tp_setattr*/
2655 0, /*tp_as_async (tp_compare in Python 2)*/
2656 0, /*tp_repr*/
2657 &SfMarkerLoop_as_number, /*tp_as_number*/
2658 0, /*tp_as_sequence*/
2659 0, /*tp_as_mapping*/
2660 0, /*tp_hash */
2661 0, /*tp_call*/
2662 0, /*tp_str*/
2663 0, /*tp_getattro*/
2664 0, /*tp_setattro*/
2665 0, /*tp_as_buffer*/
2666 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
2667 "SfMarkerLoop objects. Reads a channel from a soundfile directly from disk.", /* tp_doc */
2668 (traverseproc)SfMarkerLoop_traverse, /* tp_traverse */
2669 (inquiry)SfMarkerLoop_clear, /* tp_clear */
2670 0, /* tp_richcompare */
2671 0, /* tp_weaklistoffset */
2672 0, /* tp_iter */
2673 0, /* tp_iternext */
2674 SfMarkerLoop_methods, /* tp_methods */
2675 SfMarkerLoop_members, /* tp_members */
2676 0, /* tp_getset */
2677 0, /* tp_base */
2678 0, /* tp_dict */
2679 0, /* tp_descr_get */
2680 0, /* tp_descr_set */
2681 0, /* tp_dictoffset */
2682 0, /* tp_init */
2683 0, /* tp_alloc */
2684 SfMarkerLoop_new, /* tp_new */
2685 };