| 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 "servermodule.h" |
| 25 |
#include "streammodule.h" |
| 26 |
#include "dummymodule.h" |
| 27 |
#include "matrixmodule.h" |
| 28 |
|
| 29 |
/*************************/ |
| 30 |
/* MatrixStream structure */ |
| 31 |
/*************************/ |
| 32 |
static void |
| 33 |
MatrixStream_dealloc(MatrixStream* self) |
| 34 |
{ |
| 35 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 36 |
} |
| 37 |
|
| 38 |
static PyObject * |
| 39 |
MatrixStream_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 40 |
{ |
| 41 |
MatrixStream *self; |
| 42 |
MAKE_NEW_MATRIXSTREAM(self, type, NULL); |
| 43 |
return (PyObject *)self; |
| 44 |
} |
| 45 |
|
| 46 |
int |
| 47 |
MatrixStream_getWidth(MatrixStream *self) |
| 48 |
{ |
| 49 |
return self->width; |
| 50 |
} |
| 51 |
|
| 52 |
int |
| 53 |
MatrixStream_getHeight(MatrixStream *self) |
| 54 |
{ |
| 55 |
return self->height; |
| 56 |
} |
| 57 |
|
| 58 |
/* width and height position normalized between 0 and 1 */ |
| 59 |
MYFLT |
| 60 |
MatrixStream_getInterpPointFromPos(MatrixStream *self, MYFLT x, MYFLT y) |
| 61 |
{ |
| 62 |
MYFLT xpos, ypos, xfpart, yfpart, x1, x2, x3, x4; |
| 63 |
int xipart, yipart; |
| 64 |
|
| 65 |
xpos = x * self->width; |
| 66 |
|
| 67 |
if (xpos < 0) |
| 68 |
xpos += self->width; |
| 69 |
else if (xpos >= self->width) |
| 70 |
{ |
| 71 |
while (xpos >= self->width) |
| 72 |
{ |
| 73 |
xpos -= self->width; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
ypos = y * self->height; |
| 78 |
|
| 79 |
if (ypos < 0) |
| 80 |
ypos += self->height; |
| 81 |
else if (ypos >= self->height) |
| 82 |
{ |
| 83 |
while (ypos >= self->height) |
| 84 |
{ |
| 85 |
ypos -= self->height; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
xipart = (int)xpos; |
| 90 |
xfpart = xpos - xipart; |
| 91 |
|
| 92 |
yipart = (int)ypos; |
| 93 |
yfpart = ypos - yipart; |
| 94 |
|
| 95 |
x1 = self->data[yipart][xipart]; // (0, 0) |
| 96 |
x2 = self->data[yipart + 1][xipart]; // (0, 1) |
| 97 |
x3 = self->data[yipart][xipart + 1]; // (1, 0) |
| 98 |
x4 = self->data[yipart + 1][xipart + 1]; // (1, 1) |
| 99 |
|
| 100 |
return (x1 * (1 - yfpart) * (1 - xfpart) + x2 * yfpart * (1 - xfpart) + x3 * (1 - yfpart) * xfpart + x4 * yfpart * xfpart); |
| 101 |
} |
| 102 |
|
| 103 |
MYFLT |
| 104 |
MatrixStream_getPointFromPos(MatrixStream *self, long x, long y) |
| 105 |
{ |
| 106 |
return self->data[y][x]; |
| 107 |
} |
| 108 |
|
| 109 |
void |
| 110 |
MatrixStream_setData(MatrixStream *self, MYFLT **data) |
| 111 |
{ |
| 112 |
self->data = data; |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
MatrixStream_setWidth(MatrixStream *self, int size) |
| 117 |
{ |
| 118 |
self->width = size; |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
MatrixStream_setHeight(MatrixStream *self, int size) |
| 123 |
{ |
| 124 |
self->height = size; |
| 125 |
} |
| 126 |
|
| 127 |
PyTypeObject MatrixStreamType = |
| 128 |
{ |
| 129 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 130 |
"_pyo.MatrixStream", /*tp_name*/ |
| 131 |
sizeof(MatrixStream), /*tp_basicsize*/ |
| 132 |
0, /*tp_itemsize*/ |
| 133 |
(destructor)MatrixStream_dealloc, /*tp_dealloc*/ |
| 134 |
0, /*tp_print*/ |
| 135 |
0, /*tp_getattr*/ |
| 136 |
0, /*tp_setattr*/ |
| 137 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 138 |
0, /*tp_repr*/ |
| 139 |
0, /*tp_as_number*/ |
| 140 |
0, /*tp_as_sequence*/ |
| 141 |
0, /*tp_as_mapping*/ |
| 142 |
0, /*tp_hash */ |
| 143 |
0, /*tp_call*/ |
| 144 |
0, /*tp_str*/ |
| 145 |
0, /*tp_getattro*/ |
| 146 |
0, /*tp_setattro*/ |
| 147 |
0, /*tp_as_buffer*/ |
| 148 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 149 |
"MatrixStream objects. For internal use only. Must never be instantiated by the user.", /* tp_doc */ |
| 150 |
0, /* tp_traverse */ |
| 151 |
0, /* tp_clear */ |
| 152 |
0, /* tp_richcompare */ |
| 153 |
0, /* tp_weaklistoffset */ |
| 154 |
0, /* tp_iter */ |
| 155 |
0, /* tp_iternext */ |
| 156 |
0, /* tp_methods */ |
| 157 |
0, /* tp_members */ |
| 158 |
0, /* tp_getset */ |
| 159 |
0, /* tp_base */ |
| 160 |
0, /* tp_dict */ |
| 161 |
0, /* tp_descr_get */ |
| 162 |
0, /* tp_descr_set */ |
| 163 |
0, /* tp_dictoffset */ |
| 164 |
0, /* tp_init */ |
| 165 |
0, /* tp_alloc */ |
| 166 |
MatrixStream_new, /* tp_new */ |
| 167 |
}; |
| 168 |
|
| 169 |
/***********************/ |
| 170 |
/* NewMatrix structure */ |
| 171 |
/***********************/ |
| 172 |
typedef struct |
| 173 |
{ |
| 174 |
pyo_matrix_HEAD |
| 175 |
int x_pointer; |
| 176 |
int y_pointer; |
| 177 |
} NewMatrix; |
| 178 |
|
| 179 |
MYFLT |
| 180 |
NewMatrix_clip(MYFLT val, MYFLT min, MYFLT max) |
| 181 |
{ |
| 182 |
if (val < min) return min; |
| 183 |
else if (val > max) return max; |
| 184 |
else return val; |
| 185 |
} |
| 186 |
|
| 187 |
static PyObject * |
| 188 |
NewMatrix_recordChunkAllRow(NewMatrix *self, MYFLT *data, long datasize) |
| 189 |
{ |
| 190 |
long i; |
| 191 |
|
| 192 |
for (i = 0; i < datasize; i++) |
| 193 |
{ |
| 194 |
self->data[self->y_pointer][self->x_pointer++] = data[i]; |
| 195 |
|
| 196 |
// Guardpoints. |
| 197 |
if (self->x_pointer == 0 && self->y_pointer == 0) |
| 198 |
self->data[self->height][self->width] = self->data[self->y_pointer][self->x_pointer]; |
| 199 |
else if (self->x_pointer == 0) |
| 200 |
self->data[self->y_pointer][self->width] = self->data[self->y_pointer][self->x_pointer]; |
| 201 |
else if (self->y_pointer == 0) |
| 202 |
self->data[self->height][self->x_pointer] = self->data[self->y_pointer][self->x_pointer]; |
| 203 |
|
| 204 |
if (self->x_pointer >= self->width) |
| 205 |
{ |
| 206 |
self->x_pointer = 0; |
| 207 |
self->y_pointer++; |
| 208 |
|
| 209 |
if (self->y_pointer >= self->height) |
| 210 |
self->y_pointer = 0; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
Py_RETURN_NONE; |
| 215 |
} |
| 216 |
|
| 217 |
static int |
| 218 |
NewMatrix_traverse(NewMatrix *self, visitproc visit, void *arg) |
| 219 |
{ |
| 220 |
pyo_matrix_VISIT |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
static int |
| 225 |
NewMatrix_clear(NewMatrix *self) |
| 226 |
{ |
| 227 |
pyo_matrix_CLEAR |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
static void |
| 232 |
NewMatrix_dealloc(NewMatrix* self) |
| 233 |
{ |
| 234 |
int i; |
| 235 |
|
| 236 |
for (i = 0; i < (self->height + 1); i++) |
| 237 |
{ |
| 238 |
PyMem_RawFree(self->data[i]); |
| 239 |
} |
| 240 |
|
| 241 |
PyMem_RawFree(self->data); |
| 242 |
NewMatrix_clear(self); |
| 243 |
Py_TYPE(self->matrixstream)->tp_free((PyObject*)self->matrixstream); |
| 244 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 245 |
} |
| 246 |
|
| 247 |
static PyObject * |
| 248 |
NewMatrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 249 |
{ |
| 250 |
int i, j; |
| 251 |
PyObject *inittmp = NULL; |
| 252 |
NewMatrix *self; |
| 253 |
|
| 254 |
self = (NewMatrix *)type->tp_alloc(type, 0); |
| 255 |
|
| 256 |
self->server = PyServer_get_server(); |
| 257 |
Py_INCREF(self->server); |
| 258 |
|
| 259 |
self->x_pointer = self->y_pointer = 0; |
| 260 |
|
| 261 |
MAKE_NEW_MATRIXSTREAM(self->matrixstream, &MatrixStreamType, NULL); |
| 262 |
|
| 263 |
static char *kwlist[] = {"width", "height", "init", NULL}; |
| 264 |
|
| 265 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "ii|O", kwlist, &self->width, &self->height, &inittmp)) |
| 266 |
Py_RETURN_NONE; |
| 267 |
|
| 268 |
self->data = (MYFLT **)PyMem_RawRealloc(self->data, (self->height + 1) * sizeof(MYFLT *)); |
| 269 |
|
| 270 |
for (i = 0; i < (self->height + 1); i++) |
| 271 |
{ |
| 272 |
self->data[i] = (MYFLT *)PyMem_RawMalloc((self->width + 1) * sizeof(MYFLT)); |
| 273 |
} |
| 274 |
|
| 275 |
for (i = 0; i < (self->height + 1); i++) |
| 276 |
{ |
| 277 |
for (j = 0; j < (self->width + 1); j++) |
| 278 |
{ |
| 279 |
self->data[i][j] = 0.0; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
MatrixStream_setWidth(self->matrixstream, self->width); |
| 284 |
MatrixStream_setHeight(self->matrixstream, self->height); |
| 285 |
|
| 286 |
if (inittmp) |
| 287 |
{ |
| 288 |
PyObject_CallMethod((PyObject *)self, "setMatrix", "O", inittmp); |
| 289 |
} |
| 290 |
|
| 291 |
MatrixStream_setData(self->matrixstream, self->data); |
| 292 |
|
| 293 |
return (PyObject *)self; |
| 294 |
} |
| 295 |
|
| 296 |
static PyObject * NewMatrix_getServer(NewMatrix* self) { GET_SERVER }; |
| 297 |
static PyObject * NewMatrix_getMatrixStream(NewMatrix* self) { GET_MATRIX_STREAM }; |
| 298 |
static PyObject * NewMatrix_normalize(NewMatrix *self, PyObject *args, PyObject *kwds) { NORMALIZE_MATRIX }; |
| 299 |
static PyObject * NewMatrix_setData(NewMatrix *self, PyObject *arg) { SET_MATRIX_DATA }; |
| 300 |
static PyObject * NewMatrix_blur(NewMatrix *self) { MATRIX_BLUR }; |
| 301 |
static PyObject * NewMatrix_boost(NewMatrix *self, PyObject *args, PyObject *kwds) { MATRIX_BOOST }; |
| 302 |
static PyObject * NewMatrix_put(NewMatrix *self, PyObject *args, PyObject *kwds) { MATRIX_PUT }; |
| 303 |
static PyObject * NewMatrix_get(NewMatrix *self, PyObject *args, PyObject *kwds) { MATRIX_GET }; |
| 304 |
static PyObject * NewMatrix_getInterpolated(NewMatrix *self, PyObject *args, PyObject *kwds) { MATRIX_GET_INTERPOLATED }; |
| 305 |
|
| 306 |
static PyObject * |
| 307 |
NewMatrix_getSize(NewMatrix *self) |
| 308 |
{ |
| 309 |
return Py_BuildValue("(ii)", self->width, self->height); |
| 310 |
}; |
| 311 |
|
| 312 |
static int |
| 313 |
NewMatrix_getWidth(NewMatrix *self) |
| 314 |
{ |
| 315 |
return self->width; |
| 316 |
}; |
| 317 |
|
| 318 |
static int |
| 319 |
NewMatrix_getHeight(NewMatrix *self) |
| 320 |
{ |
| 321 |
return self->height; |
| 322 |
}; |
| 323 |
|
| 324 |
static PyObject * |
| 325 |
NewMatrix_getRate(NewMatrix *self) |
| 326 |
{ |
| 327 |
MYFLT sr = PyFloat_AsDouble(PyObject_CallMethod(self->server, "getSamplingRate", NULL)); |
| 328 |
\ |
| 329 |
return PyFloat_FromDouble(sr / self->width); |
| 330 |
}; |
| 331 |
|
| 332 |
static PyObject * |
| 333 |
NewMatrix_getData(NewMatrix *self) |
| 334 |
{ |
| 335 |
int i, j; |
| 336 |
PyObject *matrix, *samples; |
| 337 |
|
| 338 |
matrix = PyList_New(self->height); |
| 339 |
|
| 340 |
for (i = 0; i < self->height; i++) |
| 341 |
{ |
| 342 |
samples = PyList_New(self->width); |
| 343 |
|
| 344 |
for (j = 0; j < self->width; j++) |
| 345 |
{ |
| 346 |
PyList_SetItem(samples, j, PyFloat_FromDouble(self->data[i][j])); |
| 347 |
} |
| 348 |
|
| 349 |
PyList_SetItem(matrix, i, samples); |
| 350 |
} |
| 351 |
|
| 352 |
return matrix; |
| 353 |
}; |
| 354 |
|
| 355 |
static PyObject * |
| 356 |
NewMatrix_getViewData(NewMatrix *self) |
| 357 |
{ |
| 358 |
int i, j; |
| 359 |
PyObject *matrix; |
| 360 |
|
| 361 |
matrix = PyList_New(self->width * self->height); |
| 362 |
|
| 363 |
for (i = 0; i < self->height; i++) |
| 364 |
{ |
| 365 |
for (j = 0; j < self->width; j++) |
| 366 |
{ |
| 367 |
PyList_SET_ITEM(matrix, i * self->width + j, PyFloat_FromDouble(self->data[i][j] * 128 + 128)); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
return matrix; |
| 372 |
}; |
| 373 |
|
| 374 |
static PyObject * |
| 375 |
NewMatrix_getImageData(NewMatrix *self) |
| 376 |
{ |
| 377 |
int i, j, w3, index; |
| 378 |
char value; |
| 379 |
char matrix[self->width * self->height * 3]; |
| 380 |
|
| 381 |
w3 = self->width * 3; |
| 382 |
|
| 383 |
for (i = 0; i < self->height; i++) |
| 384 |
{ |
| 385 |
for (j = 0; j < self->width; j++) |
| 386 |
{ |
| 387 |
value = (char)(self->data[i][j] * 128 + 128); |
| 388 |
index = i * w3 + j * 3; |
| 389 |
matrix[index] = matrix[index + 1] = matrix[index + 2] = value; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
return PyByteArray_FromStringAndSize(matrix, self->width * self->height * 3); |
| 394 |
}; |
| 395 |
|
| 396 |
static PyObject * |
| 397 |
NewMatrix_setMatrix(NewMatrix *self, PyObject *value) |
| 398 |
{ |
| 399 |
int i, j; |
| 400 |
PyObject *innerlist; |
| 401 |
|
| 402 |
if (value == NULL) |
| 403 |
{ |
| 404 |
PyErr_SetString(PyExc_TypeError, "Cannot delete the list attribute."); |
| 405 |
return PyLong_FromLong(-1); |
| 406 |
} |
| 407 |
|
| 408 |
if (! PyList_Check(value)) |
| 409 |
{ |
| 410 |
PyErr_SetString(PyExc_TypeError, "The matrix value value must be a list."); |
| 411 |
return PyLong_FromLong(-1); |
| 412 |
} |
| 413 |
|
| 414 |
int height = PyList_Size(value); |
| 415 |
int width = PyList_Size(PyList_GetItem(value, 0)); |
| 416 |
|
| 417 |
if (height != self->height || width != self->width) |
| 418 |
{ |
| 419 |
PyErr_SetString(PyExc_TypeError, "New matrix must be of the same size as actual matrix."); |
| 420 |
return PyLong_FromLong(-1); |
| 421 |
} |
| 422 |
|
| 423 |
for (i = 0; i < self->height; i++) |
| 424 |
{ |
| 425 |
innerlist = PyList_GetItem(value, i); |
| 426 |
|
| 427 |
for (j = 0; j < self->width; j++) |
| 428 |
{ |
| 429 |
self->data[i][j] = PyFloat_AsDouble(PyList_GET_ITEM(innerlist, j)); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
Py_RETURN_NONE; |
| 434 |
} |
| 435 |
|
| 436 |
static PyObject * |
| 437 |
NewMatrix_genSineTerrain(NewMatrix *self, PyObject *args, PyObject *kwds) |
| 438 |
{ |
| 439 |
int i, j; |
| 440 |
MYFLT xfreq, xphase, xsize; |
| 441 |
MYFLT freq = 1; |
| 442 |
MYFLT phase = 0.0625; |
| 443 |
static char *kwlist[] = {"freq", "phase", NULL}; |
| 444 |
|
| 445 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE__FF, kwlist, &freq, &phase)) |
| 446 |
return PyLong_FromLong(-1); |
| 447 |
|
| 448 |
xfreq = TWOPI * freq; |
| 449 |
xsize = 1.0 / self->width; |
| 450 |
|
| 451 |
for (i = 0; i < self->height; i++) |
| 452 |
{ |
| 453 |
xphase = MYSIN(i * phase); |
| 454 |
|
| 455 |
for (j = 0; j < self->width; j++) |
| 456 |
{ |
| 457 |
self->data[i][j] = MYSIN(xfreq * j * xsize + xphase); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
Py_RETURN_NONE; |
| 462 |
} |
| 463 |
|
| 464 |
static PyMemberDef NewMatrix_members[] = |
| 465 |
{ |
| 466 |
{"server", T_OBJECT_EX, offsetof(NewMatrix, server), 0, "Pyo server."}, |
| 467 |
{"matrixstream", T_OBJECT_EX, offsetof(NewMatrix, matrixstream), 0, "Matrix stream object."}, |
| 468 |
{NULL} /* Sentinel */ |
| 469 |
}; |
| 470 |
|
| 471 |
static PyMethodDef NewMatrix_methods[] = |
| 472 |
{ |
| 473 |
{"getServer", (PyCFunction)NewMatrix_getServer, METH_NOARGS, "Returns server object."}, |
| 474 |
{"getData", (PyCFunction)NewMatrix_getData, METH_NOARGS, "Returns a list of matrix samples."}, |
| 475 |
{"getViewData", (PyCFunction)NewMatrix_getViewData, METH_NOARGS, "Returns a list of matrix samples normalized between 0 and 256 ."}, |
| 476 |
{"getImageData", (PyCFunction)NewMatrix_getImageData, METH_NOARGS, "Returns a list of matrix samples in tuple of 3 ints normalized between 0 and 256 ."}, |
| 477 |
{"getMatrixStream", (PyCFunction)NewMatrix_getMatrixStream, METH_NOARGS, "Returns matrixstream object created by this matrix."}, |
| 478 |
{"setMatrix", (PyCFunction)NewMatrix_setMatrix, METH_O, "Sets the matrix from a list of list of floats (must be the same size as the object size)."}, |
| 479 |
{"setData", (PyCFunction)NewMatrix_setData, METH_O, "Sets the matrix from a list of list of floats (resizes the matrix)."}, |
| 480 |
{"normalize", (PyCFunction)NewMatrix_normalize, METH_VARARGS | METH_KEYWORDS, "Normalize matrix samples between -1 and 1"}, |
| 481 |
{"blur", (PyCFunction)NewMatrix_blur, METH_NOARGS, "Blur the matrix."}, |
| 482 |
{"genSineTerrain", (PyCFunction)NewMatrix_genSineTerrain, METH_VARARGS | METH_KEYWORDS, "Generate a modulated sinusoidal terrain."}, |
| 483 |
{"boost", (PyCFunction)NewMatrix_boost, METH_VARARGS | METH_KEYWORDS, "Boost the contrast of the matrix."}, |
| 484 |
{"put", (PyCFunction)NewMatrix_put, METH_VARARGS | METH_KEYWORDS, "Puts a value at specified position in the matrix."}, |
| 485 |
{"get", (PyCFunction)NewMatrix_get, METH_VARARGS | METH_KEYWORDS, "Gets the value at specified position in the matrix."}, |
| 486 |
{"getInterpolated", (PyCFunction)NewMatrix_getInterpolated, METH_VARARGS | METH_KEYWORDS, "Gets the value at normalized position in the matrix."}, |
| 487 |
{"getSize", (PyCFunction)NewMatrix_getSize, METH_NOARGS, "Return the size of the matrix in samples."}, |
| 488 |
{"getRate", (PyCFunction)NewMatrix_getRate, METH_NOARGS, "Return the frequency (in cps) that reads the sound without pitch transposition."}, |
| 489 |
{NULL} /* Sentinel */ |
| 490 |
}; |
| 491 |
|
| 492 |
PyTypeObject NewMatrixType = |
| 493 |
{ |
| 494 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 495 |
"_pyo.NewMatrix_base", /*tp_name*/ |
| 496 |
sizeof(NewMatrix), /*tp_basicsize*/ |
| 497 |
0, /*tp_itemsize*/ |
| 498 |
(destructor)NewMatrix_dealloc, /*tp_dealloc*/ |
| 499 |
0, /*tp_print*/ |
| 500 |
0, /*tp_getattr*/ |
| 501 |
0, /*tp_setattr*/ |
| 502 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 503 |
0, /*tp_repr*/ |
| 504 |
0, /*tp_as_number*/ |
| 505 |
0, /*tp_as_sequence*/ |
| 506 |
0, /*tp_as_mapping*/ |
| 507 |
0, /*tp_hash */ |
| 508 |
0, /*tp_call*/ |
| 509 |
0, /*tp_str*/ |
| 510 |
0, /*tp_getattro*/ |
| 511 |
0, /*tp_setattro*/ |
| 512 |
0, /*tp_as_buffer*/ |
| 513 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 514 |
"NewMatrix objects. Generates an empty matrix.", /* tp_doc */ |
| 515 |
(traverseproc)NewMatrix_traverse, /* tp_traverse */ |
| 516 |
(inquiry)NewMatrix_clear, /* tp_clear */ |
| 517 |
0, /* tp_richcompare */ |
| 518 |
0, /* tp_weaklistoffset */ |
| 519 |
0, /* tp_iter */ |
| 520 |
0, /* tp_iternext */ |
| 521 |
NewMatrix_methods, /* tp_methods */ |
| 522 |
NewMatrix_members, /* tp_members */ |
| 523 |
0, /* tp_getset */ |
| 524 |
0, /* tp_base */ |
| 525 |
0, /* tp_dict */ |
| 526 |
0, /* tp_descr_get */ |
| 527 |
0, /* tp_descr_set */ |
| 528 |
0, /* tp_dictoffset */ |
| 529 |
0, /* tp_init */ |
| 530 |
0, /* tp_alloc */ |
| 531 |
NewMatrix_new, /* tp_new */ |
| 532 |
}; |
| 533 |
|
| 534 |
/******************************/ |
| 535 |
/* MatrixRec object definition */ |
| 536 |
/******************************/ |
| 537 |
typedef struct |
| 538 |
{ |
| 539 |
pyo_audio_HEAD |
| 540 |
PyObject *input; |
| 541 |
Stream *input_stream; |
| 542 |
NewMatrix *matrix; |
| 543 |
int pointer; |
| 544 |
int active; |
| 545 |
int delay; |
| 546 |
int delayCount; |
| 547 |
MYFLT fadetime; |
| 548 |
MYFLT fadeInSample; |
| 549 |
MYFLT *trigsBuffer; |
| 550 |
TriggerStream *trig_stream; |
| 551 |
} MatrixRec; |
| 552 |
|
| 553 |
static void |
| 554 |
MatrixRec_compute_next_data_frame(MatrixRec *self) |
| 555 |
{ |
| 556 |
int i, num, num2, upBound; |
| 557 |
MYFLT val; |
| 558 |
int width = NewMatrix_getWidth((NewMatrix *)self->matrix); |
| 559 |
int height = NewMatrix_getHeight((NewMatrix *)self->matrix); |
| 560 |
int size = width * height; |
| 561 |
|
| 562 |
int off = self->delay - self->delayCount; |
| 563 |
|
| 564 |
for (i = 0; i < self->bufsize; i++) |
| 565 |
{ |
| 566 |
self->trigsBuffer[i] = 0.0; |
| 567 |
} |
| 568 |
|
| 569 |
if ((size - self->pointer) >= self->bufsize) |
| 570 |
num = self->bufsize; |
| 571 |
else |
| 572 |
{ |
| 573 |
num = size - self->pointer; |
| 574 |
|
| 575 |
if (self->active == 1) |
| 576 |
{ |
| 577 |
if (num <= 0) |
| 578 |
self->trigsBuffer[0] = 1.0; |
| 579 |
else |
| 580 |
self->trigsBuffer[num - 1] = 1.0; |
| 581 |
|
| 582 |
self->active = 0; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if (self->pointer < size) |
| 587 |
{ |
| 588 |
upBound = size - self->fadeInSample; |
| 589 |
|
| 590 |
if (off == 0) |
| 591 |
num2 = num; |
| 592 |
else if ((num - off) <= 0) |
| 593 |
num2 = 0; |
| 594 |
else |
| 595 |
num2 = num - off; |
| 596 |
|
| 597 |
MYFLT buffer[num2]; |
| 598 |
memset(&buffer, 0, sizeof(buffer)); |
| 599 |
MYFLT *in = Stream_getData((Stream *)self->input_stream); |
| 600 |
|
| 601 |
for (i = 0; i < num; i++) |
| 602 |
{ |
| 603 |
if (self->delayCount < self->delay) |
| 604 |
{ |
| 605 |
self->delayCount++; |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
if (self->pointer < self->fadeInSample) |
| 610 |
val = self->pointer / self->fadeInSample; |
| 611 |
else if (self->pointer > upBound) |
| 612 |
val = (size - self->pointer) / self->fadeInSample; |
| 613 |
else |
| 614 |
val = 1.; |
| 615 |
|
| 616 |
buffer[i - off] = in[i] * val; |
| 617 |
self->pointer++; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
NewMatrix_recordChunkAllRow((NewMatrix *)self->matrix, buffer, num2); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
static int |
| 626 |
MatrixRec_traverse(MatrixRec *self, visitproc visit, void *arg) |
| 627 |
{ |
| 628 |
pyo_VISIT |
| 629 |
Py_VISIT(self->input); |
| 630 |
Py_VISIT(self->matrix); |
| 631 |
return 0; |
| 632 |
} |
| 633 |
|
| 634 |
static int |
| 635 |
MatrixRec_clear(MatrixRec *self) |
| 636 |
{ |
| 637 |
pyo_CLEAR |
| 638 |
Py_CLEAR(self->input); |
| 639 |
Py_CLEAR(self->matrix); |
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
static void |
| 644 |
MatrixRec_dealloc(MatrixRec* self) |
| 645 |
{ |
| 646 |
pyo_DEALLOC |
| 647 |
PyMem_RawFree(self->trigsBuffer); |
| 648 |
MatrixRec_clear(self); |
| 649 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 650 |
Py_TYPE(self->trig_stream)->tp_free((PyObject*)self->trig_stream); |
| 651 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 652 |
} |
| 653 |
|
| 654 |
static PyObject * |
| 655 |
MatrixRec_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 656 |
{ |
| 657 |
int i; |
| 658 |
PyObject *inputtmp, *input_streamtmp, *matrixtmp; |
| 659 |
MatrixRec *self; |
| 660 |
self = (MatrixRec *)type->tp_alloc(type, 0); |
| 661 |
|
| 662 |
self->pointer = 0; |
| 663 |
self->active = 1; |
| 664 |
self->fadetime = 0.; |
| 665 |
self->delay = self->delayCount = 0; |
| 666 |
|
| 667 |
INIT_OBJECT_COMMON |
| 668 |
|
| 669 |
Stream_setFunctionPtr(self->stream, MatrixRec_compute_next_data_frame); |
| 670 |
|
| 671 |
static char *kwlist[] = {"input", "matrix", "fadetime", "delay", NULL}; |
| 672 |
|
| 673 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, TYPE_OO_FI, kwlist, &inputtmp, &matrixtmp, &self->fadetime, &self->delay)) |
| 674 |
Py_RETURN_NONE; |
| 675 |
|
| 676 |
INIT_INPUT_STREAM |
| 677 |
|
| 678 |
self->matrix = (NewMatrix *)matrixtmp; |
| 679 |
Py_INCREF(self->matrix); |
| 680 |
|
| 681 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 682 |
|
| 683 |
self->trigsBuffer = (MYFLT *)PyMem_RawRealloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT)); |
| 684 |
|
| 685 |
for (i = 0; i < self->bufsize; i++) |
| 686 |
{ |
| 687 |
self->trigsBuffer[i] = 0.0; |
| 688 |
} |
| 689 |
|
| 690 |
MAKE_NEW_TRIGGER_STREAM(self->trig_stream, &TriggerStreamType, NULL); |
| 691 |
TriggerStream_setData(self->trig_stream, self->trigsBuffer); |
| 692 |
|
| 693 |
int width = NewMatrix_getWidth((NewMatrix *)self->matrix); |
| 694 |
int height = NewMatrix_getHeight((NewMatrix *)self->matrix); |
| 695 |
int size = width * height; |
| 696 |
|
| 697 |
if ((self->fadetime * self->sr) > (size * 0.5)) |
| 698 |
self->fadetime = size * 0.5 / self->sr; |
| 699 |
|
| 700 |
self->fadeInSample = roundf(self->fadetime * self->sr + 0.5); |
| 701 |
|
| 702 |
return (PyObject *)self; |
| 703 |
} |
| 704 |
|
| 705 |
static PyObject * MatrixRec_getServer(MatrixRec* self) { GET_SERVER }; |
| 706 |
static PyObject * MatrixRec_getStream(MatrixRec* self) { GET_STREAM }; |
| 707 |
static PyObject * MatrixRec_getTriggerStream(MatrixRec* self) { GET_TRIGGER_STREAM }; |
| 708 |
|
| 709 |
static PyObject * MatrixRec_play(MatrixRec *self, PyObject *args, PyObject *kwds) |
| 710 |
{ |
| 711 |
self->pointer = 0; |
| 712 |
self->active = 1; |
| 713 |
self->delayCount = 0; |
| 714 |
PLAY |
| 715 |
}; |
| 716 |
|
| 717 |
static PyObject * MatrixRec_stop(MatrixRec *self, PyObject *args, PyObject *kwds) { STOP }; |
| 718 |
|
| 719 |
static PyObject * |
| 720 |
MatrixRec_setMatrix(MatrixRec *self, PyObject *arg) |
| 721 |
{ |
| 722 |
ASSERT_ARG_NOT_NULL |
| 723 |
|
| 724 |
Py_DECREF(self->matrix); |
| 725 |
self->matrix = (NewMatrix *)arg; |
| 726 |
Py_INCREF(self->matrix); |
| 727 |
|
| 728 |
Py_RETURN_NONE; |
| 729 |
} |
| 730 |
|
| 731 |
static PyMemberDef MatrixRec_members[] = |
| 732 |
{ |
| 733 |
{"server", T_OBJECT_EX, offsetof(MatrixRec, server), 0, "Pyo server."}, |
| 734 |
{"stream", T_OBJECT_EX, offsetof(MatrixRec, stream), 0, "Stream object."}, |
| 735 |
{"trig_stream", T_OBJECT_EX, offsetof(MatrixRec, trig_stream), 0, "Trigger Stream object."}, |
| 736 |
{"input", T_OBJECT_EX, offsetof(MatrixRec, input), 0, "Input sound object."}, |
| 737 |
{"matrix", T_OBJECT_EX, offsetof(MatrixRec, matrix), 0, "matrix to record in."}, |
| 738 |
{NULL} /* Sentinel */ |
| 739 |
}; |
| 740 |
|
| 741 |
static PyMethodDef MatrixRec_methods[] = |
| 742 |
{ |
| 743 |
{"getServer", (PyCFunction)MatrixRec_getServer, METH_NOARGS, "Returns server object."}, |
| 744 |
{"_getStream", (PyCFunction)MatrixRec_getStream, METH_NOARGS, "Returns stream object."}, |
| 745 |
{"_getTriggerStream", (PyCFunction)MatrixRec_getTriggerStream, METH_NOARGS, "Returns trigger stream object."}, |
| 746 |
{"setMatrix", (PyCFunction)MatrixRec_setMatrix, METH_O, "Sets a new Matrix."}, |
| 747 |
{"play", (PyCFunction)MatrixRec_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 748 |
{"stop", (PyCFunction)MatrixRec_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 749 |
{NULL} /* Sentinel */ |
| 750 |
}; |
| 751 |
|
| 752 |
PyTypeObject MatrixRecType = |
| 753 |
{ |
| 754 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 755 |
"_pyo.MatrixRec_base", /*tp_name*/ |
| 756 |
sizeof(MatrixRec), /*tp_basicsize*/ |
| 757 |
0, /*tp_itemsize*/ |
| 758 |
(destructor)MatrixRec_dealloc, /*tp_dealloc*/ |
| 759 |
0, /*tp_print*/ |
| 760 |
0, /*tp_getattr*/ |
| 761 |
0, /*tp_setattr*/ |
| 762 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 763 |
0, /*tp_repr*/ |
| 764 |
0, /*tp_as_number*/ |
| 765 |
0, /*tp_as_sequence*/ |
| 766 |
0, /*tp_as_mapping*/ |
| 767 |
0, /*tp_hash */ |
| 768 |
0, /*tp_call*/ |
| 769 |
0, /*tp_str*/ |
| 770 |
0, /*tp_getattro*/ |
| 771 |
0, /*tp_setattro*/ |
| 772 |
0, /*tp_as_buffer*/ |
| 773 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 774 |
"MatrixRec objects. Record audio input in a Matrix object.", /* tp_doc */ |
| 775 |
(traverseproc)MatrixRec_traverse, /* tp_traverse */ |
| 776 |
(inquiry)MatrixRec_clear, /* tp_clear */ |
| 777 |
0, /* tp_richcompare */ |
| 778 |
0, /* tp_weaklistoffset */ |
| 779 |
0, /* tp_iter */ |
| 780 |
0, /* tp_iternext */ |
| 781 |
MatrixRec_methods, /* tp_methods */ |
| 782 |
MatrixRec_members, /* tp_members */ |
| 783 |
0, /* tp_getset */ |
| 784 |
0, /* tp_base */ |
| 785 |
0, /* tp_dict */ |
| 786 |
0, /* tp_descr_get */ |
| 787 |
0, /* tp_descr_set */ |
| 788 |
0, /* tp_dictoffset */ |
| 789 |
0, /* tp_init */ |
| 790 |
0, /* tp_alloc */ |
| 791 |
MatrixRec_new, /* tp_new */ |
| 792 |
}; |
| 793 |
|
| 794 |
/******************************/ |
| 795 |
/* MatrixRecLoop object definition */ |
| 796 |
/******************************/ |
| 797 |
typedef struct |
| 798 |
{ |
| 799 |
pyo_audio_HEAD |
| 800 |
PyObject *input; |
| 801 |
Stream *input_stream; |
| 802 |
NewMatrix *matrix; |
| 803 |
int pointer; |
| 804 |
MYFLT *trigsBuffer; |
| 805 |
TriggerStream *trig_stream; |
| 806 |
} MatrixRecLoop; |
| 807 |
|
| 808 |
static void |
| 809 |
MatrixRecLoop_compute_next_data_frame(MatrixRecLoop *self) |
| 810 |
{ |
| 811 |
int i; |
| 812 |
int width = NewMatrix_getWidth((NewMatrix *)self->matrix); |
| 813 |
int height = NewMatrix_getHeight((NewMatrix *)self->matrix); |
| 814 |
int size = width * height; |
| 815 |
|
| 816 |
MYFLT buffer[self->bufsize]; |
| 817 |
memset(&buffer, 0, sizeof(buffer)); |
| 818 |
MYFLT *in = Stream_getData((Stream *)self->input_stream); |
| 819 |
|
| 820 |
for (i = 0; i < self->bufsize; i++) |
| 821 |
{ |
| 822 |
self->trigsBuffer[i] = 0.0; |
| 823 |
buffer[i] = in[i]; |
| 824 |
|
| 825 |
if (self->pointer++ >= size) |
| 826 |
{ |
| 827 |
self->pointer = 0; |
| 828 |
self->trigsBuffer[i] = 1.0; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
NewMatrix_recordChunkAllRow((NewMatrix *)self->matrix, buffer, self->bufsize); |
| 833 |
} |
| 834 |
|
| 835 |
static int |
| 836 |
MatrixRecLoop_traverse(MatrixRecLoop *self, visitproc visit, void *arg) |
| 837 |
{ |
| 838 |
pyo_VISIT |
| 839 |
Py_VISIT(self->input); |
| 840 |
Py_VISIT(self->matrix); |
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
static int |
| 845 |
MatrixRecLoop_clear(MatrixRecLoop *self) |
| 846 |
{ |
| 847 |
pyo_CLEAR |
| 848 |
Py_CLEAR(self->input); |
| 849 |
Py_CLEAR(self->matrix); |
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
static void |
| 854 |
MatrixRecLoop_dealloc(MatrixRecLoop* self) |
| 855 |
{ |
| 856 |
pyo_DEALLOC |
| 857 |
PyMem_RawFree(self->trigsBuffer); |
| 858 |
MatrixRecLoop_clear(self); |
| 859 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 860 |
Py_TYPE(self->trig_stream)->tp_free((PyObject*)self->trig_stream); |
| 861 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 862 |
} |
| 863 |
|
| 864 |
static PyObject * |
| 865 |
MatrixRecLoop_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 866 |
{ |
| 867 |
int i; |
| 868 |
PyObject *inputtmp, *input_streamtmp, *matrixtmp; |
| 869 |
MatrixRecLoop *self; |
| 870 |
self = (MatrixRecLoop *)type->tp_alloc(type, 0); |
| 871 |
|
| 872 |
self->pointer = 0; |
| 873 |
|
| 874 |
INIT_OBJECT_COMMON |
| 875 |
|
| 876 |
Stream_setFunctionPtr(self->stream, MatrixRecLoop_compute_next_data_frame); |
| 877 |
|
| 878 |
static char *kwlist[] = {"input", "matrix", NULL}; |
| 879 |
|
| 880 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &inputtmp, &matrixtmp)) |
| 881 |
Py_RETURN_NONE; |
| 882 |
|
| 883 |
INIT_INPUT_STREAM |
| 884 |
|
| 885 |
self->matrix = (NewMatrix *)matrixtmp; |
| 886 |
Py_INCREF(self->matrix); |
| 887 |
|
| 888 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 889 |
|
| 890 |
self->trigsBuffer = (MYFLT *)PyMem_RawRealloc(self->trigsBuffer, self->bufsize * sizeof(MYFLT)); |
| 891 |
|
| 892 |
for (i = 0; i < self->bufsize; i++) |
| 893 |
{ |
| 894 |
self->trigsBuffer[i] = 0.0; |
| 895 |
} |
| 896 |
|
| 897 |
MAKE_NEW_TRIGGER_STREAM(self->trig_stream, &TriggerStreamType, NULL); |
| 898 |
TriggerStream_setData(self->trig_stream, self->trigsBuffer); |
| 899 |
|
| 900 |
return (PyObject *)self; |
| 901 |
} |
| 902 |
|
| 903 |
static PyObject * MatrixRecLoop_getServer(MatrixRecLoop* self) { GET_SERVER }; |
| 904 |
static PyObject * MatrixRecLoop_getStream(MatrixRecLoop* self) { GET_STREAM }; |
| 905 |
static PyObject * MatrixRecLoop_getTriggerStream(MatrixRecLoop* self) { GET_TRIGGER_STREAM }; |
| 906 |
|
| 907 |
static PyObject * MatrixRecLoop_play(MatrixRecLoop *self, PyObject *args, PyObject *kwds) { PLAY }; |
| 908 |
static PyObject * MatrixRecLoop_stop(MatrixRecLoop *self, PyObject *args, PyObject *kwds) { STOP }; |
| 909 |
|
| 910 |
static PyObject * |
| 911 |
MatrixRecLoop_setMatrix(MatrixRecLoop *self, PyObject *arg) |
| 912 |
{ |
| 913 |
ASSERT_ARG_NOT_NULL |
| 914 |
|
| 915 |
Py_DECREF(self->matrix); |
| 916 |
self->matrix = (NewMatrix *)arg; |
| 917 |
Py_INCREF(self->matrix); |
| 918 |
|
| 919 |
Py_RETURN_NONE; |
| 920 |
} |
| 921 |
|
| 922 |
static PyMemberDef MatrixRecLoop_members[] = |
| 923 |
{ |
| 924 |
{"server", T_OBJECT_EX, offsetof(MatrixRecLoop, server), 0, "Pyo server."}, |
| 925 |
{"stream", T_OBJECT_EX, offsetof(MatrixRecLoop, stream), 0, "Stream object."}, |
| 926 |
{"trig_stream", T_OBJECT_EX, offsetof(MatrixRecLoop, trig_stream), 0, "Trigger Stream object."}, |
| 927 |
{"input", T_OBJECT_EX, offsetof(MatrixRecLoop, input), 0, "Input sound object."}, |
| 928 |
{"matrix", T_OBJECT_EX, offsetof(MatrixRecLoop, matrix), 0, "matrix to record in."}, |
| 929 |
{NULL} /* Sentinel */ |
| 930 |
}; |
| 931 |
|
| 932 |
static PyMethodDef MatrixRecLoop_methods[] = |
| 933 |
{ |
| 934 |
{"getServer", (PyCFunction)MatrixRecLoop_getServer, METH_NOARGS, "Returns server object."}, |
| 935 |
{"_getStream", (PyCFunction)MatrixRecLoop_getStream, METH_NOARGS, "Returns stream object."}, |
| 936 |
{"_getTriggerStream", (PyCFunction)MatrixRecLoop_getTriggerStream, METH_NOARGS, "Returns trigger stream object."}, |
| 937 |
{"setMatrix", (PyCFunction)MatrixRecLoop_setMatrix, METH_O, "Sets a new Matrix."}, |
| 938 |
{"play", (PyCFunction)MatrixRecLoop_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 939 |
{"stop", (PyCFunction)MatrixRecLoop_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 940 |
{NULL} /* Sentinel */ |
| 941 |
}; |
| 942 |
|
| 943 |
PyTypeObject MatrixRecLoopType = |
| 944 |
{ |
| 945 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 946 |
"_pyo.MatrixRecLoop_base", /*tp_name*/ |
| 947 |
sizeof(MatrixRecLoop), /*tp_basicsize*/ |
| 948 |
0, /*tp_itemsize*/ |
| 949 |
(destructor)MatrixRecLoop_dealloc, /*tp_dealloc*/ |
| 950 |
0, /*tp_print*/ |
| 951 |
0, /*tp_getattr*/ |
| 952 |
0, /*tp_setattr*/ |
| 953 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 954 |
0, /*tp_repr*/ |
| 955 |
0, /*tp_as_number*/ |
| 956 |
0, /*tp_as_sequence*/ |
| 957 |
0, /*tp_as_mapping*/ |
| 958 |
0, /*tp_hash */ |
| 959 |
0, /*tp_call*/ |
| 960 |
0, /*tp_str*/ |
| 961 |
0, /*tp_getattro*/ |
| 962 |
0, /*tp_setattro*/ |
| 963 |
0, /*tp_as_buffer*/ |
| 964 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 965 |
"MatrixRecLoop objects. Record circular audio input in a Matrix object.", /* tp_doc */ |
| 966 |
(traverseproc)MatrixRecLoop_traverse, /* tp_traverse */ |
| 967 |
(inquiry)MatrixRecLoop_clear, /* tp_clear */ |
| 968 |
0, /* tp_richcompare */ |
| 969 |
0, /* tp_weaklistoffset */ |
| 970 |
0, /* tp_iter */ |
| 971 |
0, /* tp_iternext */ |
| 972 |
MatrixRecLoop_methods, /* tp_methods */ |
| 973 |
MatrixRecLoop_members, /* tp_members */ |
| 974 |
0, /* tp_getset */ |
| 975 |
0, /* tp_base */ |
| 976 |
0, /* tp_dict */ |
| 977 |
0, /* tp_descr_get */ |
| 978 |
0, /* tp_descr_set */ |
| 979 |
0, /* tp_dictoffset */ |
| 980 |
0, /* tp_init */ |
| 981 |
0, /* tp_alloc */ |
| 982 |
MatrixRecLoop_new, /* tp_new */ |
| 983 |
}; |
| 984 |
|
| 985 |
/******************************/ |
| 986 |
/* MatrixMorph object definition */ |
| 987 |
/******************************/ |
| 988 |
typedef struct |
| 989 |
{ |
| 990 |
pyo_audio_HEAD |
| 991 |
PyObject *input; |
| 992 |
Stream *input_stream; |
| 993 |
PyObject *matrix; |
| 994 |
PyObject *sources; |
| 995 |
MYFLT *buffer; |
| 996 |
} MatrixMorph; |
| 997 |
|
| 998 |
static MYFLT |
| 999 |
MatrixMorph_clip(MYFLT x) |
| 1000 |
{ |
| 1001 |
if (x < 0.0) |
| 1002 |
return 0.0; |
| 1003 |
else if (x >= 0.999999) |
| 1004 |
return 0.999999; |
| 1005 |
else |
| 1006 |
return x; |
| 1007 |
} |
| 1008 |
|
| 1009 |
static void |
| 1010 |
MatrixMorph_compute_next_data_frame(MatrixMorph *self) |
| 1011 |
{ |
| 1012 |
int x, y; |
| 1013 |
long i, j, width, height, numsamps, index; |
| 1014 |
MYFLT input, interp, interp1; |
| 1015 |
|
| 1016 |
MYFLT *in = Stream_getData((Stream *)self->input_stream); |
| 1017 |
width = NewMatrix_getWidth((NewMatrix *)self->matrix); |
| 1018 |
height = NewMatrix_getHeight((NewMatrix *)self->matrix); |
| 1019 |
numsamps = width * height; |
| 1020 |
int len = PyList_Size(self->sources); |
| 1021 |
|
| 1022 |
input = MatrixMorph_clip(in[0]); |
| 1023 |
|
| 1024 |
interp = input * (len - 1); |
| 1025 |
x = (int)(interp); |
| 1026 |
y = x + 1; |
| 1027 |
|
| 1028 |
MatrixStream *tab1 = (MatrixStream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->sources, x), "getMatrixStream", ""); |
| 1029 |
MatrixStream *tab2 = (MatrixStream *)PyObject_CallMethod((PyObject *)PyList_GET_ITEM(self->sources, y), "getMatrixStream", ""); |
| 1030 |
|
| 1031 |
interp = MYFMOD(interp, 1.0); |
| 1032 |
interp1 = 1. - interp; |
| 1033 |
|
| 1034 |
for (i = 0; i < height; i++) |
| 1035 |
{ |
| 1036 |
for (j = 0; j < width; j++) |
| 1037 |
{ |
| 1038 |
index = i * width + j; |
| 1039 |
self->buffer[index] = MatrixStream_getPointFromPos(tab1, j, i) * interp1 + MatrixStream_getPointFromPos(tab2, j, i) * interp; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
NewMatrix_recordChunkAllRow((NewMatrix *)self->matrix, self->buffer, numsamps); |
| 1044 |
} |
| 1045 |
|
| 1046 |
static int |
| 1047 |
MatrixMorph_traverse(MatrixMorph *self, visitproc visit, void *arg) |
| 1048 |
{ |
| 1049 |
pyo_VISIT |
| 1050 |
Py_VISIT(self->input); |
| 1051 |
Py_VISIT(self->matrix); |
| 1052 |
Py_VISIT(self->sources); |
| 1053 |
return 0; |
| 1054 |
} |
| 1055 |
|
| 1056 |
static int |
| 1057 |
MatrixMorph_clear(MatrixMorph *self) |
| 1058 |
{ |
| 1059 |
pyo_CLEAR |
| 1060 |
Py_CLEAR(self->input); |
| 1061 |
Py_CLEAR(self->matrix); |
| 1062 |
Py_CLEAR(self->sources); |
| 1063 |
return 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static void |
| 1067 |
MatrixMorph_dealloc(MatrixMorph* self) |
| 1068 |
{ |
| 1069 |
pyo_DEALLOC |
| 1070 |
PyMem_RawFree(self->buffer); |
| 1071 |
MatrixMorph_clear(self); |
| 1072 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 1073 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 1074 |
} |
| 1075 |
|
| 1076 |
static PyObject * |
| 1077 |
MatrixMorph_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1078 |
{ |
| 1079 |
int i; |
| 1080 |
long width, height, numsamps; |
| 1081 |
PyObject *inputtmp, *input_streamtmp, *matrixtmp, *sourcestmp; |
| 1082 |
MatrixMorph *self; |
| 1083 |
self = (MatrixMorph *)type->tp_alloc(type, 0); |
| 1084 |
|
| 1085 |
INIT_OBJECT_COMMON |
| 1086 |
|
| 1087 |
Stream_setFunctionPtr(self->stream, MatrixMorph_compute_next_data_frame); |
| 1088 |
|
| 1089 |
static char *kwlist[] = {"input", "matrix", "sources", NULL}; |
| 1090 |
|
| 1091 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOO", kwlist, &inputtmp, &matrixtmp, &sourcestmp)) |
| 1092 |
Py_RETURN_NONE; |
| 1093 |
|
| 1094 |
INIT_INPUT_STREAM |
| 1095 |
|
| 1096 |
self->matrix = (PyObject *)matrixtmp; |
| 1097 |
Py_INCREF(self->matrix); |
| 1098 |
|
| 1099 |
width = NewMatrix_getWidth((NewMatrix *)self->matrix); |
| 1100 |
height = NewMatrix_getHeight((NewMatrix *)self->matrix); |
| 1101 |
numsamps = width * height; |
| 1102 |
self->buffer = (MYFLT *)PyMem_RawRealloc(self->buffer, (numsamps) * sizeof(MYFLT)); |
| 1103 |
|
| 1104 |
self->sources = (PyObject *)sourcestmp; |
| 1105 |
Py_INCREF(self->sources); |
| 1106 |
|
| 1107 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 1108 |
|
| 1109 |
return (PyObject *)self; |
| 1110 |
} |
| 1111 |
|
| 1112 |
static PyObject * MatrixMorph_getServer(MatrixMorph* self) { GET_SERVER }; |
| 1113 |
static PyObject * MatrixMorph_getStream(MatrixMorph* self) { GET_STREAM }; |
| 1114 |
|
| 1115 |
static PyObject * MatrixMorph_play(MatrixMorph *self, PyObject *args, PyObject *kwds) { PLAY }; |
| 1116 |
static PyObject * MatrixMorph_stop(MatrixMorph *self, PyObject *args, PyObject *kwds) { STOP }; |
| 1117 |
|
| 1118 |
static PyObject * |
| 1119 |
MatrixMorph_setMatrix(MatrixMorph *self, PyObject *arg) |
| 1120 |
{ |
| 1121 |
ASSERT_ARG_NOT_NULL |
| 1122 |
|
| 1123 |
Py_DECREF(self->matrix); |
| 1124 |
self->matrix = (PyObject *)arg; |
| 1125 |
Py_INCREF(self->matrix); |
| 1126 |
|
| 1127 |
Py_RETURN_NONE; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static PyObject * |
| 1131 |
MatrixMorph_setSources(MatrixMorph *self, PyObject *arg) |
| 1132 |
{ |
| 1133 |
ASSERT_ARG_NOT_NULL |
| 1134 |
|
| 1135 |
if (! PyList_Check(arg)) |
| 1136 |
{ |
| 1137 |
PyErr_SetString(PyExc_TypeError, "The amplitude list attribute value must be a list."); |
| 1138 |
return PyLong_FromLong(-1); |
| 1139 |
} |
| 1140 |
|
| 1141 |
Py_INCREF(arg); |
| 1142 |
Py_DECREF(self->sources); |
| 1143 |
self->sources = arg; |
| 1144 |
|
| 1145 |
Py_RETURN_NONE; |
| 1146 |
} |
| 1147 |
|
| 1148 |
static PyMemberDef MatrixMorph_members[] = |
| 1149 |
{ |
| 1150 |
{"server", T_OBJECT_EX, offsetof(MatrixMorph, server), 0, "Pyo server."}, |
| 1151 |
{"stream", T_OBJECT_EX, offsetof(MatrixMorph, stream), 0, "Stream object."}, |
| 1152 |
{"input", T_OBJECT_EX, offsetof(MatrixMorph, input), 0, "Input sound object."}, |
| 1153 |
{"matrix", T_OBJECT_EX, offsetof(MatrixMorph, matrix), 0, "Matrix to record in."}, |
| 1154 |
{"sources", T_OBJECT_EX, offsetof(MatrixMorph, sources), 0, "list of matrixes to interpolate from."}, |
| 1155 |
{NULL} /* Sentinel */ |
| 1156 |
}; |
| 1157 |
|
| 1158 |
static PyMethodDef MatrixMorph_methods[] = |
| 1159 |
{ |
| 1160 |
{"getServer", (PyCFunction)MatrixMorph_getServer, METH_NOARGS, "Returns server object."}, |
| 1161 |
{"_getStream", (PyCFunction)MatrixMorph_getStream, METH_NOARGS, "Returns stream object."}, |
| 1162 |
{"setMatrix", (PyCFunction)MatrixMorph_setMatrix, METH_O, "Sets a new matrix."}, |
| 1163 |
{"setSources", (PyCFunction)MatrixMorph_setSources, METH_O, "Changes the sources matrixs."}, |
| 1164 |
{"play", (PyCFunction)MatrixMorph_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 1165 |
{"stop", (PyCFunction)MatrixMorph_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 1166 |
{NULL} /* Sentinel */ |
| 1167 |
}; |
| 1168 |
|
| 1169 |
PyTypeObject MatrixMorphType = |
| 1170 |
{ |
| 1171 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1172 |
"_pyo.MatrixMorph_base", /*tp_name*/ |
| 1173 |
sizeof(MatrixMorph), /*tp_basicsize*/ |
| 1174 |
0, /*tp_itemsize*/ |
| 1175 |
(destructor)MatrixMorph_dealloc, /*tp_dealloc*/ |
| 1176 |
0, /*tp_print*/ |
| 1177 |
0, /*tp_getattr*/ |
| 1178 |
0, /*tp_setattr*/ |
| 1179 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 1180 |
0, /*tp_repr*/ |
| 1181 |
0, /*tp_as_number*/ |
| 1182 |
0, /*tp_as_sequence*/ |
| 1183 |
0, /*tp_as_mapping*/ |
| 1184 |
0, /*tp_hash */ |
| 1185 |
0, /*tp_call*/ |
| 1186 |
0, /*tp_str*/ |
| 1187 |
0, /*tp_getattro*/ |
| 1188 |
0, /*tp_setattro*/ |
| 1189 |
0, /*tp_as_buffer*/ |
| 1190 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 1191 |
"MatrixMorph objects. Interpolation contents of different matrix objects.", /* tp_doc */ |
| 1192 |
(traverseproc)MatrixMorph_traverse, /* tp_traverse */ |
| 1193 |
(inquiry)MatrixMorph_clear, /* tp_clear */ |
| 1194 |
0, /* tp_richcompare */ |
| 1195 |
0, /* tp_weaklistoffset */ |
| 1196 |
0, /* tp_iter */ |
| 1197 |
0, /* tp_iternext */ |
| 1198 |
MatrixMorph_methods, /* tp_methods */ |
| 1199 |
MatrixMorph_members, /* tp_members */ |
| 1200 |
0, /* tp_getset */ |
| 1201 |
0, /* tp_base */ |
| 1202 |
0, /* tp_dict */ |
| 1203 |
0, /* tp_descr_get */ |
| 1204 |
0, /* tp_descr_set */ |
| 1205 |
0, /* tp_dictoffset */ |
| 1206 |
0, /* tp_init */ |
| 1207 |
0, /* tp_alloc */ |
| 1208 |
MatrixMorph_new, /* tp_new */ |
| 1209 |
}; |