| 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 |
#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 |
{ |
| 30 |
pyo_audio_HEAD |
| 31 |
PyObject *callable; |
| 32 |
PyObject *time; |
| 33 |
Stream *time_stream; |
| 34 |
PyObject *arg; |
| 35 |
int modebuffer[1]; |
| 36 |
MYFLT sampleToSec; |
| 37 |
double currentTime; |
| 38 |
int init; |
| 39 |
} Pattern; |
| 40 |
|
| 41 |
static void |
| 42 |
Pattern_generate_i(Pattern *self) |
| 43 |
{ |
| 44 |
int i; |
| 45 |
MYFLT tm; |
| 46 |
PyObject *tuple, *result; |
| 47 |
|
| 48 |
tm = PyFloat_AS_DOUBLE(self->time); |
| 49 |
|
| 50 |
if (self->init) |
| 51 |
{ |
| 52 |
self->init = 0; |
| 53 |
self->currentTime = tm; |
| 54 |
} |
| 55 |
|
| 56 |
for (i = 0; i < self->bufsize; i++) |
| 57 |
{ |
| 58 |
if (self->currentTime >= tm && PyCallable_Check(self->callable)) |
| 59 |
{ |
| 60 |
self->currentTime = 0.0; |
| 61 |
|
| 62 |
if (self->arg == Py_None) |
| 63 |
{ |
| 64 |
result = PyObject_Call((PyObject *)self->callable, PyTuple_New(0), NULL); |
| 65 |
|
| 66 |
if (result == NULL) |
| 67 |
{ |
| 68 |
PyErr_Print(); |
| 69 |
return; |
| 70 |
} |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
tuple = PyTuple_New(1); |
| 75 |
Py_INCREF(self->arg); |
| 76 |
PyTuple_SET_ITEM(tuple, 0, self->arg); |
| 77 |
result = PyObject_Call((PyObject *)self->callable, tuple, NULL); |
| 78 |
Py_DECREF(tuple); |
| 79 |
|
| 80 |
if (result == NULL) |
| 81 |
{ |
| 82 |
PyErr_Print(); |
| 83 |
return; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
self->currentTime += self->sampleToSec; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
Pattern_generate_a(Pattern *self) |
| 94 |
{ |
| 95 |
int i; |
| 96 |
PyObject *tuple, *result; |
| 97 |
|
| 98 |
MYFLT *tm = Stream_getData((Stream *)self->time_stream); |
| 99 |
|
| 100 |
if (self->init) |
| 101 |
{ |
| 102 |
self->init = 0; |
| 103 |
self->currentTime = tm[0]; |
| 104 |
} |
| 105 |
|
| 106 |
for (i = 0; i < self->bufsize; i++) |
| 107 |
{ |
| 108 |
if (self->currentTime >= tm[i] && PyCallable_Check(self->callable)) |
| 109 |
{ |
| 110 |
self->currentTime = 0.0; |
| 111 |
|
| 112 |
if (self->arg == Py_None) |
| 113 |
{ |
| 114 |
result = PyObject_Call((PyObject *)self->callable, PyTuple_New(0), NULL); |
| 115 |
|
| 116 |
if (result == NULL) |
| 117 |
{ |
| 118 |
PyErr_Print(); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
tuple = PyTuple_New(1); |
| 125 |
Py_INCREF(self->arg); |
| 126 |
PyTuple_SET_ITEM(tuple, 0, self->arg); |
| 127 |
result = PyObject_Call((PyObject *)self->callable, tuple, NULL); |
| 128 |
Py_DECREF(tuple); |
| 129 |
|
| 130 |
if (result == NULL) |
| 131 |
{ |
| 132 |
PyErr_Print(); |
| 133 |
return; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
self->currentTime += self->sampleToSec; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
Pattern_setProcMode(Pattern *self) |
| 144 |
{ |
| 145 |
int procmode = self->modebuffer[0]; |
| 146 |
|
| 147 |
switch (procmode) |
| 148 |
{ |
| 149 |
case 0: |
| 150 |
self->proc_func_ptr = Pattern_generate_i; |
| 151 |
break; |
| 152 |
|
| 153 |
case 1: |
| 154 |
self->proc_func_ptr = Pattern_generate_a; |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
Pattern_compute_next_data_frame(Pattern *self) |
| 161 |
{ |
| 162 |
(*self->proc_func_ptr)(self); |
| 163 |
} |
| 164 |
|
| 165 |
static int |
| 166 |
Pattern_traverse(Pattern *self, visitproc visit, void *arg) |
| 167 |
{ |
| 168 |
pyo_VISIT |
| 169 |
Py_VISIT(self->callable); |
| 170 |
Py_VISIT(self->time); |
| 171 |
Py_VISIT(self->arg); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
static int |
| 176 |
Pattern_clear(Pattern *self) |
| 177 |
{ |
| 178 |
pyo_CLEAR |
| 179 |
Py_CLEAR(self->callable); |
| 180 |
Py_CLEAR(self->time); |
| 181 |
Py_CLEAR(self->arg); |
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
Pattern_dealloc(Pattern* self) |
| 187 |
{ |
| 188 |
pyo_DEALLOC |
| 189 |
Pattern_clear(self); |
| 190 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 191 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 192 |
} |
| 193 |
|
| 194 |
static PyObject * |
| 195 |
Pattern_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 196 |
{ |
| 197 |
int i; |
| 198 |
PyObject *timetmp = NULL, *calltmp = NULL, *argtmp = NULL; |
| 199 |
Pattern *self; |
| 200 |
self = (Pattern *)type->tp_alloc(type, 0); |
| 201 |
|
| 202 |
self->time = PyFloat_FromDouble(1.); |
| 203 |
self->modebuffer[0] = 0; |
| 204 |
self->init = 1; |
| 205 |
self->arg = Py_None; |
| 206 |
|
| 207 |
INIT_OBJECT_COMMON |
| 208 |
Stream_setFunctionPtr(self->stream, Pattern_compute_next_data_frame); |
| 209 |
self->mode_func_ptr = Pattern_setProcMode; |
| 210 |
|
| 211 |
self->sampleToSec = 1. / self->sr; |
| 212 |
self->currentTime = 0.; |
| 213 |
|
| 214 |
static char *kwlist[] = {"callable", "time", "arg", NULL}; |
| 215 |
|
| 216 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|OO", kwlist, &calltmp, &timetmp, &argtmp)) |
| 217 |
Py_RETURN_NONE; |
| 218 |
|
| 219 |
if (calltmp) |
| 220 |
{ |
| 221 |
PyObject_CallMethod((PyObject *)self, "setFunction", "O", calltmp); |
| 222 |
} |
| 223 |
|
| 224 |
if (timetmp) |
| 225 |
{ |
| 226 |
PyObject_CallMethod((PyObject *)self, "setTime", "O", timetmp); |
| 227 |
} |
| 228 |
|
| 229 |
if (argtmp) |
| 230 |
{ |
| 231 |
if (PyTuple_Check(argtmp)) |
| 232 |
{ |
| 233 |
PyObject *argument = PyTuple_New(1); |
| 234 |
PyTuple_SetItem(argument, 0, argtmp); |
| 235 |
PyObject_CallMethod((PyObject *)self, "setArg", "O", argument); |
| 236 |
} |
| 237 |
else |
| 238 |
{ |
| 239 |
PyObject_CallMethod((PyObject *)self, "setArg", "O", argtmp); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 244 |
|
| 245 |
(*self->mode_func_ptr)(self); |
| 246 |
|
| 247 |
return (PyObject *)self; |
| 248 |
} |
| 249 |
|
| 250 |
static PyObject * Pattern_getServer(Pattern* self) { GET_SERVER }; |
| 251 |
static PyObject * Pattern_getStream(Pattern* self) { GET_STREAM }; |
| 252 |
|
| 253 |
static PyObject * |
| 254 |
Pattern_play(Pattern *self, PyObject *args, PyObject *kwds) |
| 255 |
{ |
| 256 |
self->init = 1; |
| 257 |
PLAY |
| 258 |
}; |
| 259 |
|
| 260 |
static PyObject * Pattern_stop(Pattern *self, PyObject *args, PyObject *kwds) { STOP }; |
| 261 |
|
| 262 |
static PyObject * |
| 263 |
Pattern_setFunction(Pattern *self, PyObject *arg) |
| 264 |
{ |
| 265 |
if (! PyCallable_Check(arg)) |
| 266 |
{ |
| 267 |
PyErr_SetString(PyExc_TypeError, "The callable attribute must be a valid Python function."); |
| 268 |
Py_RETURN_NONE; |
| 269 |
} |
| 270 |
|
| 271 |
Py_XDECREF(self->callable); |
| 272 |
self->callable = arg; |
| 273 |
Py_INCREF(self->callable); |
| 274 |
|
| 275 |
Py_RETURN_NONE; |
| 276 |
} |
| 277 |
|
| 278 |
static PyObject * Pattern_setTime(Pattern *self, PyObject *arg) { SET_PARAM(self->time, self->time_stream, 0); } |
| 279 |
|
| 280 |
static PyObject * |
| 281 |
Pattern_setArg(Pattern *self, PyObject *arg) |
| 282 |
{ |
| 283 |
Py_XDECREF(self->arg); |
| 284 |
self->arg = arg; |
| 285 |
Py_INCREF(self->arg); |
| 286 |
|
| 287 |
Py_RETURN_NONE; |
| 288 |
} |
| 289 |
|
| 290 |
static PyMemberDef Pattern_members[] = |
| 291 |
{ |
| 292 |
{"server", T_OBJECT_EX, offsetof(Pattern, server), 0, "Pyo server."}, |
| 293 |
{"stream", T_OBJECT_EX, offsetof(Pattern, stream), 0, "Stream object."}, |
| 294 |
{"time", T_OBJECT_EX, offsetof(Pattern, time), 0, "Pattern time factor."}, |
| 295 |
{NULL} /* Sentinel */ |
| 296 |
}; |
| 297 |
|
| 298 |
static PyMethodDef Pattern_methods[] = |
| 299 |
{ |
| 300 |
{"getServer", (PyCFunction)Pattern_getServer, METH_NOARGS, "Returns server object."}, |
| 301 |
{"_getStream", (PyCFunction)Pattern_getStream, METH_NOARGS, "Returns stream object."}, |
| 302 |
{"play", (PyCFunction)Pattern_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 303 |
{"stop", (PyCFunction)Pattern_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 304 |
{"setTime", (PyCFunction)Pattern_setTime, METH_O, "Sets time factor."}, |
| 305 |
{"setFunction", (PyCFunction)Pattern_setFunction, METH_O, "Sets the function to be called."}, |
| 306 |
{"setArg", (PyCFunction)Pattern_setArg, METH_O, "Sets function's argument."}, |
| 307 |
{NULL} /* Sentinel */ |
| 308 |
}; |
| 309 |
|
| 310 |
PyTypeObject PatternType = |
| 311 |
{ |
| 312 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 313 |
"_pyo.Pattern_base", /*tp_name*/ |
| 314 |
sizeof(Pattern), /*tp_basicsize*/ |
| 315 |
0, /*tp_itemsize*/ |
| 316 |
(destructor)Pattern_dealloc, /*tp_dealloc*/ |
| 317 |
0, /*tp_print*/ |
| 318 |
0, /*tp_getattr*/ |
| 319 |
0, /*tp_setattr*/ |
| 320 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 321 |
0, /*tp_repr*/ |
| 322 |
0, /*tp_as_number*/ |
| 323 |
0, /*tp_as_sequence*/ |
| 324 |
0, /*tp_as_mapping*/ |
| 325 |
0, /*tp_hash */ |
| 326 |
0, /*tp_call*/ |
| 327 |
0, /*tp_str*/ |
| 328 |
0, /*tp_getattro*/ |
| 329 |
0, /*tp_setattro*/ |
| 330 |
0, /*tp_as_buffer*/ |
| 331 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 332 |
"Pattern objects. Create a metronome.", /* tp_doc */ |
| 333 |
(traverseproc)Pattern_traverse, /* tp_traverse */ |
| 334 |
(inquiry)Pattern_clear, /* tp_clear */ |
| 335 |
0, /* tp_richcompare */ |
| 336 |
0, /* tp_weaklistoffset */ |
| 337 |
0, /* tp_iter */ |
| 338 |
0, /* tp_iternext */ |
| 339 |
Pattern_methods, /* tp_methods */ |
| 340 |
Pattern_members, /* tp_members */ |
| 341 |
0, /* tp_getset */ |
| 342 |
0, /* tp_base */ |
| 343 |
0, /* tp_dict */ |
| 344 |
0, /* tp_descr_get */ |
| 345 |
0, /* tp_descr_set */ |
| 346 |
0, /* tp_dictoffset */ |
| 347 |
0, /* tp_init */ |
| 348 |
0, /* tp_alloc */ |
| 349 |
Pattern_new, /* tp_new */ |
| 350 |
}; |
| 351 |
|
| 352 |
/***************/ |
| 353 |
/**** Score ****/ |
| 354 |
/***************/ |
| 355 |
typedef struct |
| 356 |
{ |
| 357 |
pyo_audio_HEAD |
| 358 |
PyObject *input; |
| 359 |
Stream *input_stream; |
| 360 |
char *fname; |
| 361 |
char curfname[100]; |
| 362 |
int last_value; |
| 363 |
} Score; |
| 364 |
|
| 365 |
static void |
| 366 |
Score_selector(Score *self) |
| 367 |
{ |
| 368 |
int i, inval; |
| 369 |
|
| 370 |
MYFLT *in = Stream_getData((Stream *)self->input_stream); |
| 371 |
|
| 372 |
for (i = 0; i < self->bufsize; i++) |
| 373 |
{ |
| 374 |
inval = (int)in[i]; |
| 375 |
|
| 376 |
if (inval != self->last_value) |
| 377 |
{ |
| 378 |
sprintf(self->curfname, "%s%i()\n", self->fname, inval); |
| 379 |
PyRun_SimpleString(self->curfname); |
| 380 |
self->last_value = inval; |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
static void |
| 386 |
Score_setProcMode(Score *self) |
| 387 |
{ |
| 388 |
self->proc_func_ptr = Score_selector; |
| 389 |
} |
| 390 |
|
| 391 |
static void |
| 392 |
Score_compute_next_data_frame(Score *self) |
| 393 |
{ |
| 394 |
(*self->proc_func_ptr)(self); |
| 395 |
} |
| 396 |
|
| 397 |
static int |
| 398 |
Score_traverse(Score *self, visitproc visit, void *arg) |
| 399 |
{ |
| 400 |
pyo_VISIT |
| 401 |
Py_VISIT(self->input); |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
static int |
| 406 |
Score_clear(Score *self) |
| 407 |
{ |
| 408 |
pyo_CLEAR |
| 409 |
Py_CLEAR(self->input); |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
static void |
| 414 |
Score_dealloc(Score* self) |
| 415 |
{ |
| 416 |
pyo_DEALLOC |
| 417 |
Score_clear(self); |
| 418 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 419 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 420 |
} |
| 421 |
|
| 422 |
static PyObject * |
| 423 |
Score_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 424 |
{ |
| 425 |
int i; |
| 426 |
PyObject *inputtmp, *input_streamtmp; |
| 427 |
Score *self; |
| 428 |
self = (Score *)type->tp_alloc(type, 0); |
| 429 |
|
| 430 |
self->last_value = -99; |
| 431 |
|
| 432 |
INIT_OBJECT_COMMON |
| 433 |
Stream_setFunctionPtr(self->stream, Score_compute_next_data_frame); |
| 434 |
self->mode_func_ptr = Score_setProcMode; |
| 435 |
|
| 436 |
static char *kwlist[] = {"input", "fname", NULL}; |
| 437 |
|
| 438 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "O|s", kwlist, &inputtmp, &self->fname)) |
| 439 |
Py_RETURN_NONE; |
| 440 |
|
| 441 |
INIT_INPUT_STREAM |
| 442 |
|
| 443 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 444 |
|
| 445 |
(*self->mode_func_ptr)(self); |
| 446 |
|
| 447 |
return (PyObject *)self; |
| 448 |
} |
| 449 |
|
| 450 |
static PyObject * Score_getServer(Score* self) { GET_SERVER }; |
| 451 |
static PyObject * Score_getStream(Score* self) { GET_STREAM }; |
| 452 |
|
| 453 |
static PyObject * Score_play(Score *self, PyObject *args, PyObject *kwds) { PLAY }; |
| 454 |
static PyObject * Score_stop(Score *self, PyObject *args, PyObject *kwds) { STOP }; |
| 455 |
|
| 456 |
static PyMemberDef Score_members[] = |
| 457 |
{ |
| 458 |
{"server", T_OBJECT_EX, offsetof(Score, server), 0, "Pyo server."}, |
| 459 |
{"stream", T_OBJECT_EX, offsetof(Score, stream), 0, "Stream object."}, |
| 460 |
{NULL} /* Sentinel */ |
| 461 |
}; |
| 462 |
|
| 463 |
static PyMethodDef Score_methods[] = |
| 464 |
{ |
| 465 |
{"getServer", (PyCFunction)Score_getServer, METH_NOARGS, "Returns server object."}, |
| 466 |
{"_getStream", (PyCFunction)Score_getStream, METH_NOARGS, "Returns stream object."}, |
| 467 |
{"play", (PyCFunction)Score_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 468 |
{"stop", (PyCFunction)Score_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 469 |
{NULL} /* Sentinel */ |
| 470 |
}; |
| 471 |
|
| 472 |
PyTypeObject ScoreType = |
| 473 |
{ |
| 474 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 475 |
"_pyo.Score_base", /*tp_name*/ |
| 476 |
sizeof(Score), /*tp_basicsize*/ |
| 477 |
0, /*tp_itemsize*/ |
| 478 |
(destructor)Score_dealloc, /*tp_dealloc*/ |
| 479 |
0, /*tp_print*/ |
| 480 |
0, /*tp_getattr*/ |
| 481 |
0, /*tp_setattr*/ |
| 482 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 483 |
0, /*tp_repr*/ |
| 484 |
0, /*tp_as_number*/ |
| 485 |
0, /*tp_as_sequence*/ |
| 486 |
0, /*tp_as_mapping*/ |
| 487 |
0, /*tp_hash */ |
| 488 |
0, /*tp_call*/ |
| 489 |
0, /*tp_str*/ |
| 490 |
0, /*tp_getattro*/ |
| 491 |
0, /*tp_setattro*/ |
| 492 |
0, /*tp_as_buffer*/ |
| 493 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 494 |
"Score objects. Calls numbered function from an integer count.", /* tp_doc */ |
| 495 |
(traverseproc)Score_traverse, /* tp_traverse */ |
| 496 |
(inquiry)Score_clear, /* tp_clear */ |
| 497 |
0, /* tp_richcompare */ |
| 498 |
0, /* tp_weaklistoffset */ |
| 499 |
0, /* tp_iter */ |
| 500 |
0, /* tp_iternext */ |
| 501 |
Score_methods, /* tp_methods */ |
| 502 |
Score_members, /* tp_members */ |
| 503 |
0, /* tp_getset */ |
| 504 |
0, /* tp_base */ |
| 505 |
0, /* tp_dict */ |
| 506 |
0, /* tp_descr_get */ |
| 507 |
0, /* tp_descr_set */ |
| 508 |
0, /* tp_dictoffset */ |
| 509 |
0, /* tp_init */ |
| 510 |
0, /* tp_alloc */ |
| 511 |
Score_new, /* tp_new */ |
| 512 |
}; |
| 513 |
|
| 514 |
/*****************/ |
| 515 |
/*** CallAfter ***/ |
| 516 |
/*****************/ |
| 517 |
typedef struct |
| 518 |
{ |
| 519 |
pyo_audio_HEAD |
| 520 |
PyObject *callable; |
| 521 |
PyObject *arg; |
| 522 |
MYFLT time; |
| 523 |
MYFLT sampleToSec; |
| 524 |
double currentTime; |
| 525 |
} CallAfter; |
| 526 |
|
| 527 |
static void |
| 528 |
CallAfter_generate(CallAfter *self) |
| 529 |
{ |
| 530 |
int i; |
| 531 |
PyObject *tuple, *result; |
| 532 |
|
| 533 |
for (i = 0; i < self->bufsize; i++) |
| 534 |
{ |
| 535 |
if (self->currentTime >= self->time) |
| 536 |
{ |
| 537 |
if (self->stream != NULL) |
| 538 |
PyObject_CallMethod((PyObject *)self, "stop", NULL); |
| 539 |
|
| 540 |
if (self->arg == Py_None) |
| 541 |
{ |
| 542 |
result = PyObject_Call(self->callable, PyTuple_New(0), NULL); |
| 543 |
} |
| 544 |
else |
| 545 |
{ |
| 546 |
tuple = PyTuple_New(1); |
| 547 |
Py_INCREF(self->arg); |
| 548 |
PyTuple_SET_ITEM(tuple, 0, self->arg); |
| 549 |
result = PyObject_Call(self->callable, tuple, NULL); |
| 550 |
Py_DECREF(tuple); |
| 551 |
|
| 552 |
} |
| 553 |
|
| 554 |
if (result == NULL) |
| 555 |
PyErr_Print(); |
| 556 |
|
| 557 |
break; |
| 558 |
} |
| 559 |
|
| 560 |
self->currentTime += self->sampleToSec; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
static void |
| 565 |
CallAfter_setProcMode(CallAfter *self) |
| 566 |
{ |
| 567 |
self->proc_func_ptr = CallAfter_generate; |
| 568 |
} |
| 569 |
|
| 570 |
static void |
| 571 |
CallAfter_compute_next_data_frame(CallAfter *self) |
| 572 |
{ |
| 573 |
(*self->proc_func_ptr)(self); |
| 574 |
} |
| 575 |
|
| 576 |
static int |
| 577 |
CallAfter_traverse(CallAfter *self, visitproc visit, void *arg) |
| 578 |
{ |
| 579 |
pyo_VISIT |
| 580 |
Py_VISIT(self->callable); |
| 581 |
Py_VISIT(self->arg); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
static int |
| 586 |
CallAfter_clear(CallAfter *self) |
| 587 |
{ |
| 588 |
pyo_CLEAR |
| 589 |
Py_CLEAR(self->callable); |
| 590 |
Py_CLEAR(self->arg); |
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
static void |
| 595 |
CallAfter_dealloc(CallAfter* self) |
| 596 |
{ |
| 597 |
pyo_DEALLOC |
| 598 |
CallAfter_clear(self); |
| 599 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 600 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 601 |
} |
| 602 |
|
| 603 |
static PyObject * |
| 604 |
CallAfter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 605 |
{ |
| 606 |
int i; |
| 607 |
PyObject *calltmp = NULL, *argtmp = NULL; |
| 608 |
CallAfter *self; |
| 609 |
self = (CallAfter *)type->tp_alloc(type, 0); |
| 610 |
|
| 611 |
self->time = 1.; |
| 612 |
self->arg = Py_None; |
| 613 |
|
| 614 |
INIT_OBJECT_COMMON |
| 615 |
Stream_setFunctionPtr(self->stream, CallAfter_compute_next_data_frame); |
| 616 |
self->mode_func_ptr = CallAfter_setProcMode; |
| 617 |
|
| 618 |
self->sampleToSec = 1. / self->sr; |
| 619 |
self->currentTime = 0.; |
| 620 |
|
| 621 |
static char *kwlist[] = {"callable", "time", "arg", NULL}; |
| 622 |
|
| 623 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_O_FO, kwlist, &calltmp, &self->time, &argtmp)) |
| 624 |
Py_RETURN_NONE; |
| 625 |
|
| 626 |
if (! PyCallable_Check(calltmp)) |
| 627 |
Py_RETURN_NONE; |
| 628 |
|
| 629 |
if (argtmp) |
| 630 |
{ |
| 631 |
Py_DECREF(self->arg); |
| 632 |
Py_INCREF(argtmp); |
| 633 |
self->arg = argtmp; |
| 634 |
} |
| 635 |
|
| 636 |
Py_INCREF(calltmp); |
| 637 |
Py_XDECREF(self->callable); |
| 638 |
self->callable = calltmp; |
| 639 |
|
| 640 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 641 |
|
| 642 |
(*self->mode_func_ptr)(self); |
| 643 |
|
| 644 |
return (PyObject *)self; |
| 645 |
} |
| 646 |
|
| 647 |
static PyObject * CallAfter_getServer(CallAfter* self) { GET_SERVER }; |
| 648 |
static PyObject * CallAfter_getStream(CallAfter* self) { GET_STREAM }; |
| 649 |
|
| 650 |
static PyObject * CallAfter_play(CallAfter *self, PyObject *args, PyObject *kwds) |
| 651 |
{ |
| 652 |
self->currentTime = 0.; |
| 653 |
PLAY |
| 654 |
}; |
| 655 |
static PyObject * CallAfter_stop(CallAfter *self, PyObject *args, PyObject *kwds) { STOP }; |
| 656 |
|
| 657 |
static PyObject * |
| 658 |
CallAfter_setTime(CallAfter *self, PyObject *arg) |
| 659 |
{ |
| 660 |
if (PyNumber_Check(arg)) |
| 661 |
{ |
| 662 |
self->time = PyFloat_AsDouble(arg); |
| 663 |
} |
| 664 |
|
| 665 |
Py_RETURN_NONE; |
| 666 |
} |
| 667 |
|
| 668 |
static PyObject * |
| 669 |
CallAfter_setArg(CallAfter *self, PyObject *arg) |
| 670 |
{ |
| 671 |
Py_XDECREF(self->arg); |
| 672 |
self->arg = arg; |
| 673 |
Py_INCREF(self->arg); |
| 674 |
|
| 675 |
Py_RETURN_NONE; |
| 676 |
} |
| 677 |
|
| 678 |
static PyMemberDef CallAfter_members[] = |
| 679 |
{ |
| 680 |
{"server", T_OBJECT_EX, offsetof(CallAfter, server), 0, "Pyo server."}, |
| 681 |
{"stream", T_OBJECT_EX, offsetof(CallAfter, stream), 0, "Stream object."}, |
| 682 |
{NULL} /* Sentinel */ |
| 683 |
}; |
| 684 |
|
| 685 |
static PyMethodDef CallAfter_methods[] = |
| 686 |
{ |
| 687 |
{"getServer", (PyCFunction)CallAfter_getServer, METH_NOARGS, "Returns server object."}, |
| 688 |
{"_getStream", (PyCFunction)CallAfter_getStream, METH_NOARGS, "Returns stream object."}, |
| 689 |
{"play", (PyCFunction)CallAfter_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 690 |
{"stop", (PyCFunction)CallAfter_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 691 |
{"setTime", (PyCFunction)CallAfter_setTime, METH_O, "Sets time argument."}, |
| 692 |
{"setArg", (PyCFunction)CallAfter_setArg, METH_O, "Sets function's argument."}, |
| 693 |
{NULL} /* Sentinel */ |
| 694 |
}; |
| 695 |
|
| 696 |
PyTypeObject CallAfterType = |
| 697 |
{ |
| 698 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 699 |
"_pyo.CallAfter_base", /*tp_name*/ |
| 700 |
sizeof(CallAfter), /*tp_basicsize*/ |
| 701 |
0, /*tp_itemsize*/ |
| 702 |
(destructor)CallAfter_dealloc, /*tp_dealloc*/ |
| 703 |
0, /*tp_print*/ |
| 704 |
0, /*tp_getattr*/ |
| 705 |
0, /*tp_setattr*/ |
| 706 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 707 |
0, /*tp_repr*/ |
| 708 |
0, /*tp_as_number*/ |
| 709 |
0, /*tp_as_sequence*/ |
| 710 |
0, /*tp_as_mapping*/ |
| 711 |
0, /*tp_hash */ |
| 712 |
0, /*tp_call*/ |
| 713 |
0, /*tp_str*/ |
| 714 |
0, /*tp_getattro*/ |
| 715 |
0, /*tp_setattro*/ |
| 716 |
0, /*tp_as_buffer*/ |
| 717 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 718 |
"CallAfter objects. Create a metronome.", /* tp_doc */ |
| 719 |
(traverseproc)CallAfter_traverse, /* tp_traverse */ |
| 720 |
(inquiry)CallAfter_clear, /* tp_clear */ |
| 721 |
0, /* tp_richcompare */ |
| 722 |
0, /* tp_weaklistoffset */ |
| 723 |
0, /* tp_iter */ |
| 724 |
0, /* tp_iternext */ |
| 725 |
CallAfter_methods, /* tp_methods */ |
| 726 |
CallAfter_members, /* tp_members */ |
| 727 |
0, /* tp_getset */ |
| 728 |
0, /* tp_base */ |
| 729 |
0, /* tp_dict */ |
| 730 |
0, /* tp_descr_get */ |
| 731 |
0, /* tp_descr_set */ |
| 732 |
0, /* tp_dictoffset */ |
| 733 |
0, /* tp_init */ |
| 734 |
0, /* tp_alloc */ |
| 735 |
CallAfter_new, /* tp_new */ |
| 736 |
}; |