[f1100a4] | 1 | #include "aubiowraphell.h" |
---|
| 2 | |
---|
| 3 | typedef struct |
---|
| 4 | { |
---|
| 5 | PyObject_HEAD |
---|
| 6 | aubio_sink_t * o; |
---|
| 7 | char_t* uri; |
---|
| 8 | uint_t samplerate; |
---|
[1a121ca] | 9 | uint_t channels; |
---|
[f1100a4] | 10 | } Py_sink; |
---|
| 11 | |
---|
[a79ec76] | 12 | static char Py_sink_doc[] = "" |
---|
| 13 | " __new__(path, samplerate = 44100, channels = 1)\n" |
---|
| 14 | "\n" |
---|
| 15 | " Create a new sink, opening the given path for writing.\n" |
---|
| 16 | "\n" |
---|
| 17 | " Examples\n" |
---|
| 18 | " --------\n" |
---|
| 19 | "\n" |
---|
| 20 | " Create a new sink at 44100Hz, mono:\n" |
---|
| 21 | "\n" |
---|
| 22 | " >>> sink('/tmp/t.wav')\n" |
---|
| 23 | "\n" |
---|
| 24 | " Create a new sink at 8000Hz, mono:\n" |
---|
| 25 | "\n" |
---|
| 26 | " >>> sink('/tmp/t.wav', samplerate = 8000)\n" |
---|
| 27 | "\n" |
---|
| 28 | " Create a new sink at 32000Hz, stereo:\n" |
---|
| 29 | "\n" |
---|
| 30 | " >>> sink('/tmp/t.wav', samplerate = 32000, channels = 2)\n" |
---|
| 31 | "\n" |
---|
| 32 | " Create a new sink at 32000Hz, 5 channels:\n" |
---|
| 33 | "\n" |
---|
| 34 | " >>> sink('/tmp/t.wav', channels = 5, samplerate = 32000)\n" |
---|
| 35 | "\n" |
---|
| 36 | " __call__(vec, write)\n" |
---|
| 37 | " x(vec,write) <==> x.do(vec, write)\n" |
---|
| 38 | "\n" |
---|
| 39 | " Write vector to sink.\n" |
---|
| 40 | "\n" |
---|
| 41 | " See also\n" |
---|
| 42 | " --------\n" |
---|
| 43 | " aubio.sink.do\n" |
---|
| 44 | "\n"; |
---|
| 45 | |
---|
| 46 | static char Py_sink_do_doc[] = "" |
---|
| 47 | "x.do(vec, write) <==> x(vec, write)\n" |
---|
| 48 | "\n" |
---|
| 49 | "write monophonic vector to sink"; |
---|
| 50 | |
---|
| 51 | static char Py_sink_do_multi_doc[] = "" |
---|
| 52 | "x.do_multi(mat, write)\n" |
---|
| 53 | "\n" |
---|
| 54 | "write polyphonic vector to sink"; |
---|
| 55 | |
---|
| 56 | static char Py_sink_close_doc[] = "" |
---|
| 57 | "x.close()\n" |
---|
| 58 | "\n" |
---|
| 59 | "close this sink now"; |
---|
[f1100a4] | 60 | |
---|
| 61 | static PyObject * |
---|
| 62 | Py_sink_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
| 63 | { |
---|
| 64 | Py_sink *self; |
---|
| 65 | char_t* uri = NULL; |
---|
| 66 | uint_t samplerate = 0; |
---|
[1a121ca] | 67 | uint_t channels = 0; |
---|
| 68 | static char *kwlist[] = { "uri", "samplerate", "channels", NULL }; |
---|
[f1100a4] | 69 | |
---|
[1a121ca] | 70 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist, |
---|
| 71 | &uri, &samplerate, &channels)) { |
---|
[f1100a4] | 72 | return NULL; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | self = (Py_sink *) pytype->tp_alloc (pytype, 0); |
---|
| 76 | |
---|
| 77 | if (self == NULL) { |
---|
| 78 | return NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | self->uri = "none"; |
---|
| 82 | if (uri != NULL) { |
---|
| 83 | self->uri = uri; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | self->samplerate = Py_aubio_default_samplerate; |
---|
| 87 | if ((sint_t)samplerate > 0) { |
---|
| 88 | self->samplerate = samplerate; |
---|
| 89 | } else if ((sint_t)samplerate < 0) { |
---|
| 90 | PyErr_SetString (PyExc_ValueError, |
---|
| 91 | "can not use negative value for samplerate"); |
---|
| 92 | return NULL; |
---|
| 93 | } |
---|
| 94 | |
---|
[1a121ca] | 95 | self->channels = 1; |
---|
| 96 | if ((sint_t)channels > 0) { |
---|
| 97 | self->channels = channels; |
---|
| 98 | } else if ((sint_t)channels < 0) { |
---|
| 99 | PyErr_SetString (PyExc_ValueError, |
---|
| 100 | "can not use negative or null value for channels"); |
---|
| 101 | return NULL; |
---|
| 102 | } |
---|
| 103 | |
---|
[f1100a4] | 104 | return (PyObject *) self; |
---|
| 105 | } |
---|
| 106 | |
---|
[1a121ca] | 107 | static int |
---|
| 108 | Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds) |
---|
| 109 | { |
---|
| 110 | if (self->channels == 1) { |
---|
| 111 | self->o = new_aubio_sink ( self->uri, self->samplerate ); |
---|
| 112 | } else { |
---|
| 113 | self->o = new_aubio_sink ( self->uri, 0 ); |
---|
| 114 | aubio_sink_preset_channels ( self->o, self->channels ); |
---|
| 115 | aubio_sink_preset_samplerate ( self->o, self->samplerate ); |
---|
| 116 | } |
---|
| 117 | if (self->o == NULL) { |
---|
| 118 | PyErr_SetString (PyExc_StandardError, "error creating sink with this uri"); |
---|
| 119 | return -1; |
---|
| 120 | } |
---|
| 121 | self->samplerate = aubio_sink_get_samplerate ( self->o ); |
---|
| 122 | self->channels = aubio_sink_get_channels ( self->o ); |
---|
| 123 | |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
[f1100a4] | 126 | |
---|
| 127 | AUBIO_DEL(sink) |
---|
| 128 | |
---|
| 129 | /* function Py_sink_do */ |
---|
[1a121ca] | 130 | static PyObject * |
---|
[f1100a4] | 131 | Py_sink_do(Py_sink * self, PyObject * args) |
---|
| 132 | { |
---|
| 133 | /* input vectors python prototypes */ |
---|
| 134 | PyObject * write_data_obj; |
---|
| 135 | |
---|
| 136 | /* input vectors prototypes */ |
---|
| 137 | fvec_t* write_data; |
---|
| 138 | uint_t write; |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) { |
---|
| 142 | return NULL; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | /* input vectors parsing */ |
---|
| 147 | write_data = PyAubio_ArrayToCFvec (write_data_obj); |
---|
| 148 | |
---|
| 149 | if (write_data == NULL) { |
---|
| 150 | return NULL; |
---|
| 151 | } |
---|
| 152 | |
---|
[1a121ca] | 153 | |
---|
| 154 | |
---|
[f1100a4] | 155 | |
---|
| 156 | |
---|
| 157 | /* compute _do function */ |
---|
| 158 | aubio_sink_do (self->o, write_data, write); |
---|
| 159 | |
---|
| 160 | Py_RETURN_NONE; |
---|
| 161 | } |
---|
| 162 | |
---|
[a79ec76] | 163 | /* function Py_sink_do_multi */ |
---|
[1a121ca] | 164 | static PyObject * |
---|
| 165 | Py_sink_do_multi(Py_sink * self, PyObject * args) |
---|
| 166 | { |
---|
| 167 | /* input vectors python prototypes */ |
---|
| 168 | PyObject * write_data_obj; |
---|
| 169 | |
---|
| 170 | /* input vectors prototypes */ |
---|
| 171 | fmat_t * write_data; |
---|
| 172 | uint_t write; |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | if (!PyArg_ParseTuple (args, "OI", &write_data_obj, &write)) { |
---|
| 176 | return NULL; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | /* input vectors parsing */ |
---|
| 181 | write_data = PyAubio_ArrayToCFmat (write_data_obj); |
---|
| 182 | |
---|
| 183 | if (write_data == NULL) { |
---|
| 184 | return NULL; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | /* compute _do function */ |
---|
| 192 | aubio_sink_do_multi (self->o, write_data, write); |
---|
| 193 | Py_RETURN_NONE; |
---|
| 194 | } |
---|
| 195 | |
---|
[f1100a4] | 196 | AUBIO_MEMBERS_START(sink) |
---|
[a79ec76] | 197 | {"uri", T_STRING, offsetof (Py_sink, uri), READONLY, |
---|
| 198 | "path at which the sink was created"}, |
---|
| 199 | {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY, |
---|
| 200 | "samplerate at which the sink was created"}, |
---|
| 201 | {"channels", T_INT, offsetof (Py_sink, channels), READONLY, |
---|
| 202 | "number of channels with which the sink was created"}, |
---|
[f1100a4] | 203 | AUBIO_MEMBERS_STOP(sink) |
---|
| 204 | |
---|
[7b56229] | 205 | static PyObject * |
---|
| 206 | Pyaubio_sink_close (Py_sink *self, PyObject *unused) |
---|
| 207 | { |
---|
[3cc3fd8] | 208 | aubio_sink_close (self->o); |
---|
[7b56229] | 209 | Py_RETURN_NONE; |
---|
| 210 | } |
---|
[f1100a4] | 211 | |
---|
| 212 | static PyMethodDef Py_sink_methods[] = { |
---|
[a79ec76] | 213 | {"do", (PyCFunction) Py_sink_do, METH_VARARGS, Py_sink_do_doc}, |
---|
| 214 | {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc}, |
---|
| 215 | {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc}, |
---|
[f1100a4] | 216 | {NULL} /* sentinel */ |
---|
| 217 | }; |
---|
| 218 | |
---|
| 219 | AUBIO_TYPEOBJECT(sink, "aubio.sink") |
---|