| 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 |
int chnl; |
| 32 |
int modebuffer[2]; |
| 33 |
} Input; |
| 34 |
|
| 35 |
static void Input_postprocessing_ii(Input *self) { POST_PROCESSING_II }; |
| 36 |
static void Input_postprocessing_ai(Input *self) { POST_PROCESSING_AI }; |
| 37 |
static void Input_postprocessing_ia(Input *self) { POST_PROCESSING_IA }; |
| 38 |
static void Input_postprocessing_aa(Input *self) { POST_PROCESSING_AA }; |
| 39 |
static void Input_postprocessing_ireva(Input *self) { POST_PROCESSING_IREVA }; |
| 40 |
static void Input_postprocessing_areva(Input *self) { POST_PROCESSING_AREVA }; |
| 41 |
static void Input_postprocessing_revai(Input *self) { POST_PROCESSING_REVAI }; |
| 42 |
static void Input_postprocessing_revaa(Input *self) { POST_PROCESSING_REVAA }; |
| 43 |
static void Input_postprocessing_revareva(Input *self) { POST_PROCESSING_REVAREVA }; |
| 44 |
|
| 45 |
static void |
| 46 |
Input_setProcMode(Input *self) |
| 47 |
{ |
| 48 |
int muladdmode; |
| 49 |
muladdmode = self->modebuffer[0] + self->modebuffer[1] * 10; |
| 50 |
|
| 51 |
switch (muladdmode) |
| 52 |
{ |
| 53 |
case 0: |
| 54 |
self->muladd_func_ptr = Input_postprocessing_ii; |
| 55 |
break; |
| 56 |
|
| 57 |
case 1: |
| 58 |
self->muladd_func_ptr = Input_postprocessing_ai; |
| 59 |
break; |
| 60 |
|
| 61 |
case 2: |
| 62 |
self->muladd_func_ptr = Input_postprocessing_revai; |
| 63 |
break; |
| 64 |
|
| 65 |
case 10: |
| 66 |
self->muladd_func_ptr = Input_postprocessing_ia; |
| 67 |
break; |
| 68 |
|
| 69 |
case 11: |
| 70 |
self->muladd_func_ptr = Input_postprocessing_aa; |
| 71 |
break; |
| 72 |
|
| 73 |
case 12: |
| 74 |
self->muladd_func_ptr = Input_postprocessing_revaa; |
| 75 |
break; |
| 76 |
|
| 77 |
case 20: |
| 78 |
self->muladd_func_ptr = Input_postprocessing_ireva; |
| 79 |
break; |
| 80 |
|
| 81 |
case 21: |
| 82 |
self->muladd_func_ptr = Input_postprocessing_areva; |
| 83 |
break; |
| 84 |
|
| 85 |
case 22: |
| 86 |
self->muladd_func_ptr = Input_postprocessing_revareva; |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
Input_compute_next_data_frame(Input *self) |
| 93 |
{ |
| 94 |
int i; |
| 95 |
MYFLT *tmp; |
| 96 |
tmp = Server_getInputBuffer((Server *)self->server); |
| 97 |
|
| 98 |
for (i = 0; i < self->bufsize * self->ichnls; i++) |
| 99 |
{ |
| 100 |
if ((i % self->ichnls) == self->chnl) |
| 101 |
self->data[(int)(i / self->ichnls)] = tmp[i]; |
| 102 |
} |
| 103 |
|
| 104 |
(*self->muladd_func_ptr)(self); |
| 105 |
} |
| 106 |
|
| 107 |
static int |
| 108 |
Input_traverse(Input *self, visitproc visit, void *arg) |
| 109 |
{ |
| 110 |
pyo_VISIT |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
static int |
| 115 |
Input_clear(Input *self) |
| 116 |
{ |
| 117 |
pyo_CLEAR |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
Input_dealloc(Input* self) |
| 123 |
{ |
| 124 |
pyo_DEALLOC |
| 125 |
Input_clear(self); |
| 126 |
Py_TYPE(self->stream)->tp_free((PyObject*)self->stream); |
| 127 |
Py_TYPE(self)->tp_free((PyObject*)self); |
| 128 |
} |
| 129 |
|
| 130 |
static PyObject * |
| 131 |
Input_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 132 |
{ |
| 133 |
int i; |
| 134 |
PyObject *multmp = NULL, *addtmp = NULL; |
| 135 |
Input *self; |
| 136 |
self = (Input *)type->tp_alloc(type, 0); |
| 137 |
|
| 138 |
self->chnl = 0; |
| 139 |
self->modebuffer[0] = 0; |
| 140 |
self->modebuffer[1] = 0; |
| 141 |
|
| 142 |
INIT_OBJECT_COMMON |
| 143 |
Stream_setFunctionPtr(self->stream, Input_compute_next_data_frame); |
| 144 |
self->mode_func_ptr = Input_setProcMode; |
| 145 |
|
| 146 |
static char *kwlist[] = {"chnl", "mul", "add", NULL}; |
| 147 |
|
| 148 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iOO", kwlist, &self->chnl, &multmp, &addtmp)) |
| 149 |
Py_RETURN_NONE; |
| 150 |
|
| 151 |
if (multmp) |
| 152 |
{ |
| 153 |
PyObject_CallMethod((PyObject *)self, "setMul", "O", multmp); |
| 154 |
} |
| 155 |
|
| 156 |
if (addtmp) |
| 157 |
{ |
| 158 |
PyObject_CallMethod((PyObject *)self, "setAdd", "O", addtmp); |
| 159 |
} |
| 160 |
|
| 161 |
PyObject_CallMethod(self->server, "addStream", "O", self->stream); |
| 162 |
|
| 163 |
(*self->mode_func_ptr)(self); |
| 164 |
|
| 165 |
return (PyObject *)self; |
| 166 |
} |
| 167 |
|
| 168 |
static PyObject * Input_getServer(Input* self) { GET_SERVER }; |
| 169 |
static PyObject * Input_getStream(Input* self) { GET_STREAM }; |
| 170 |
static PyObject * Input_setMul(Input *self, PyObject *arg) { SET_MUL }; |
| 171 |
static PyObject * Input_setAdd(Input *self, PyObject *arg) { SET_ADD }; |
| 172 |
static PyObject * Input_setSub(Input *self, PyObject *arg) { SET_SUB }; |
| 173 |
static PyObject * Input_setDiv(Input *self, PyObject *arg) { SET_DIV }; |
| 174 |
|
| 175 |
static PyObject * Input_play(Input *self, PyObject *args, PyObject *kwds) { PLAY }; |
| 176 |
static PyObject * Input_out(Input *self, PyObject *args, PyObject *kwds) { OUT }; |
| 177 |
static PyObject * Input_stop(Input *self, PyObject *args, PyObject *kwds) { STOP }; |
| 178 |
|
| 179 |
static PyObject * Input_multiply(Input *self, PyObject *arg) { MULTIPLY }; |
| 180 |
static PyObject * Input_inplace_multiply(Input *self, PyObject *arg) { INPLACE_MULTIPLY }; |
| 181 |
static PyObject * Input_add(Input *self, PyObject *arg) { ADD }; |
| 182 |
static PyObject * Input_inplace_add(Input *self, PyObject *arg) { INPLACE_ADD }; |
| 183 |
static PyObject * Input_sub(Input *self, PyObject *arg) { SUB }; |
| 184 |
static PyObject * Input_inplace_sub(Input *self, PyObject *arg) { INPLACE_SUB }; |
| 185 |
static PyObject * Input_div(Input *self, PyObject *arg) { DIV }; |
| 186 |
static PyObject * Input_inplace_div(Input *self, PyObject *arg) { INPLACE_DIV }; |
| 187 |
|
| 188 |
static PyMemberDef Input_members[] = |
| 189 |
{ |
| 190 |
{"server", T_OBJECT_EX, offsetof(Input, server), 0, "Pyo server."}, |
| 191 |
{"stream", T_OBJECT_EX, offsetof(Input, stream), 0, "Stream object."}, |
| 192 |
{"mul", T_OBJECT_EX, offsetof(Input, mul), 0, "Mul factor."}, |
| 193 |
{"add", T_OBJECT_EX, offsetof(Input, add), 0, "Add factor."}, |
| 194 |
{NULL} /* Sentinel */ |
| 195 |
}; |
| 196 |
|
| 197 |
static PyMethodDef Input_methods[] = |
| 198 |
{ |
| 199 |
{"getServer", (PyCFunction)Input_getServer, METH_NOARGS, "Returns server object."}, |
| 200 |
{"_getStream", (PyCFunction)Input_getStream, METH_NOARGS, "Returns stream object."}, |
| 201 |
{"play", (PyCFunction)Input_play, METH_VARARGS | METH_KEYWORDS, "Starts computing without sending sound to soundcard."}, |
| 202 |
{"out", (PyCFunction)Input_out, METH_VARARGS | METH_KEYWORDS, "Starts computing and sends sound to soundcard channel speficied by argument."}, |
| 203 |
{"stop", (PyCFunction)Input_stop, METH_VARARGS | METH_KEYWORDS, "Stops computing."}, |
| 204 |
{"setMul", (PyCFunction)Input_setMul, METH_O, "Sets oscillator mul factor."}, |
| 205 |
{"setAdd", (PyCFunction)Input_setAdd, METH_O, "Sets oscillator add factor."}, |
| 206 |
{"setSub", (PyCFunction)Input_setSub, METH_O, "Sets inverse add factor."}, |
| 207 |
{"setDiv", (PyCFunction)Input_setDiv, METH_O, "Sets inverse mul factor."}, |
| 208 |
{NULL} /* Sentinel */ |
| 209 |
}; |
| 210 |
|
| 211 |
static PyNumberMethods Input_as_number = |
| 212 |
{ |
| 213 |
(binaryfunc)Input_add, /*nb_add*/ |
| 214 |
(binaryfunc)Input_sub, /*nb_subtract*/ |
| 215 |
(binaryfunc)Input_multiply, /*nb_multiply*/ |
| 216 |
0, /*nb_remainder*/ |
| 217 |
0, /*nb_divmod*/ |
| 218 |
0, /*nb_power*/ |
| 219 |
0, /*nb_neg*/ |
| 220 |
0, /*nb_pos*/ |
| 221 |
0, /*(unaryfunc)array_abs,*/ |
| 222 |
0, /*nb_nonzero*/ |
| 223 |
0, /*nb_invert*/ |
| 224 |
0, /*nb_lshift*/ |
| 225 |
0, /*nb_rshift*/ |
| 226 |
0, /*nb_and*/ |
| 227 |
0, /*nb_xor*/ |
| 228 |
0, /*nb_or*/ |
| 229 |
0, /*nb_int*/ |
| 230 |
0, /*nb_long*/ |
| 231 |
0, /*nb_float*/ |
| 232 |
(binaryfunc)Input_inplace_add, /*inplace_add*/ |
| 233 |
(binaryfunc)Input_inplace_sub, /*inplace_subtract*/ |
| 234 |
(binaryfunc)Input_inplace_multiply, /*inplace_multiply*/ |
| 235 |
0, /*inplace_remainder*/ |
| 236 |
0, /*inplace_power*/ |
| 237 |
0, /*inplace_lshift*/ |
| 238 |
0, /*inplace_rshift*/ |
| 239 |
0, /*inplace_and*/ |
| 240 |
0, /*inplace_xor*/ |
| 241 |
0, /*inplace_or*/ |
| 242 |
0, /*nb_floor_divide*/ |
| 243 |
(binaryfunc)Input_div, /*nb_true_divide*/ |
| 244 |
0, /*nb_inplace_floor_divide*/ |
| 245 |
(binaryfunc)Input_inplace_div, /*nb_inplace_true_divide*/ |
| 246 |
0, /* nb_index */ |
| 247 |
}; |
| 248 |
|
| 249 |
PyTypeObject InputType = |
| 250 |
{ |
| 251 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 252 |
"_pyo.Input_base", /*tp_name*/ |
| 253 |
sizeof(Input), /*tp_basicsize*/ |
| 254 |
0, /*tp_itemsize*/ |
| 255 |
(destructor)Input_dealloc, /*tp_dealloc*/ |
| 256 |
0, /*tp_print*/ |
| 257 |
0, /*tp_getattr*/ |
| 258 |
0, /*tp_setattr*/ |
| 259 |
0, /*tp_as_async (tp_compare in Python 2)*/ |
| 260 |
0, /*tp_repr*/ |
| 261 |
&Input_as_number, /*tp_as_number*/ |
| 262 |
0, /*tp_as_sequence*/ |
| 263 |
0, /*tp_as_mapping*/ |
| 264 |
0, /*tp_hash */ |
| 265 |
0, /*tp_call*/ |
| 266 |
0, /*tp_str*/ |
| 267 |
0, /*tp_getattro*/ |
| 268 |
0, /*tp_setattro*/ |
| 269 |
0, /*tp_as_buffer*/ |
| 270 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/ |
| 271 |
"Input objects. Retreive audio from an input channel.", /* tp_doc */ |
| 272 |
(traverseproc)Input_traverse, /* tp_traverse */ |
| 273 |
(inquiry)Input_clear, /* tp_clear */ |
| 274 |
0, /* tp_richcompare */ |
| 275 |
0, /* tp_weaklistoffset */ |
| 276 |
0, /* tp_iter */ |
| 277 |
0, /* tp_iternext */ |
| 278 |
Input_methods, /* tp_methods */ |
| 279 |
Input_members, /* tp_members */ |
| 280 |
0, /* tp_getset */ |
| 281 |
0, /* tp_base */ |
| 282 |
0, /* tp_dict */ |
| 283 |
0, /* tp_descr_get */ |
| 284 |
0, /* tp_descr_set */ |
| 285 |
0, /* tp_dictoffset */ |
| 286 |
0, /* tp_init */ |
| 287 |
0, /* tp_alloc */ |
| 288 |
Input_new, /* tp_new */ |
| 289 |
}; |