Changeset 633400d for python/ext/py-sink.c
- Timestamp:
- Dec 5, 2018, 10:34:39 PM (6 years ago)
- Branches:
- feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
- Children:
- 283a619a
- Parents:
- 5b46bc3 (diff), f19db54 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/ext/py-sink.c
r5b46bc3 r633400d 13 13 14 14 static char Py_sink_doc[] = "" 15 " __new__(path, samplerate = 44100, channels = 1)\n" 16 "\n" 17 " Create a new sink, opening the given path for writing.\n" 18 "\n" 19 " Examples\n" 20 " --------\n" 21 "\n" 22 " Create a new sink at 44100Hz, mono:\n" 23 "\n" 24 " >>> sink('/tmp/t.wav')\n" 25 "\n" 26 " Create a new sink at 8000Hz, mono:\n" 27 "\n" 28 " >>> sink('/tmp/t.wav', samplerate = 8000)\n" 29 "\n" 30 " Create a new sink at 32000Hz, stereo:\n" 31 "\n" 32 " >>> sink('/tmp/t.wav', samplerate = 32000, channels = 2)\n" 33 "\n" 34 " Create a new sink at 32000Hz, 5 channels:\n" 35 "\n" 36 " >>> sink('/tmp/t.wav', channels = 5, samplerate = 32000)\n" 37 "\n" 38 " __call__(vec, write)\n" 39 " x(vec,write) <==> x.do(vec, write)\n" 40 "\n" 41 " Write vector to sink.\n" 42 "\n" 43 " See also\n" 44 " --------\n" 45 " aubio.sink.do\n" 15 "sink(path, samplerate=44100, channels=1)\n" 16 "\n" 17 "Write audio samples to file.\n" 18 "\n" 19 "Parameters\n" 20 "----------\n" 21 "path : str\n" 22 " Pathname of the file to be opened for writing.\n" 23 "samplerate : int\n" 24 " Sampling rate of the file, in Hz.\n" 25 "channels : int\n" 26 " Number of channels to create the file with.\n" 27 "\n" 28 "Examples\n" 29 "--------\n" 30 "\n" 31 "Create a new sink at 44100Hz, mono:\n" 32 "\n" 33 ">>> snk = aubio.sink('out.wav')\n" 34 "\n" 35 "Create a new sink at 32000Hz, stereo, write 100 samples into it:\n" 36 "\n" 37 ">>> snk = aubio.sink('out.wav', samplerate=16000, channels=3)\n" 38 ">>> snk(aubio.fvec(100), 100)\n" 39 "\n" 40 "Open a new sink at 48000Hz, stereo, write `1234` samples into it:\n" 41 "\n" 42 ">>> with aubio.sink('out.wav', samplerate=48000, channels=2) as src:\n" 43 "... snk(aubio.fvec(1024), 1024)\n" 44 "... snk(aubio.fvec(210), 210)\n" 45 "...\n" 46 "\n" 47 "See also\n" 48 "--------\n" 49 "source: read audio samples from a file.\n" 46 50 "\n"; 47 51 48 52 static char Py_sink_do_doc[] = "" 49 "x.do(vec, write) <==> x(vec, write)\n" 50 "\n" 51 "write monophonic vector to sink"; 53 "do(vec, write)\n" 54 "\n" 55 "Write a single channel vector to sink.\n" 56 "\n" 57 "Parameters\n" 58 "----------\n" 59 "vec : fvec\n" 60 " input vector `(n,)` where `n >= 0`.\n" 61 "write : int\n" 62 " Number of samples to write.\n" 63 ""; 52 64 53 65 static char Py_sink_do_multi_doc[] = "" 54 "x.do_multi(mat, write)\n" 55 "\n" 56 "write polyphonic vector to sink"; 66 "do_multi(mat, write)\n" 67 "\n" 68 "Write a matrix containing vectors from multiple channels to sink.\n" 69 "\n" 70 "Parameters\n" 71 "----------\n" 72 "mat : numpy.ndarray\n" 73 " input matrix of shape `(channels, n)`, where `n >= 0`.\n" 74 "write : int\n" 75 " Number of frames to write.\n" 76 ""; 57 77 58 78 static char Py_sink_close_doc[] = "" 59 "x.close()\n" 60 "\n" 61 "close this sink now"; 79 "close()\n" 80 "\n" 81 "Close this sink now.\n" 82 "\n" 83 "By default, the sink will be closed before being deleted.\n" 84 "Explicitely closing a sink can be useful to control the number\n" 85 "of files simultaneously opened.\n" 86 ""; 62 87 63 88 static PyObject * … … 81 106 } 82 107 83 self->uri = "none";108 self->uri = NULL; 84 109 if (uri != NULL) { 85 self->uri = uri; 110 self->uri = (char_t *)malloc(sizeof(char_t) * (strnlen(uri, PATH_MAX) + 1)); 111 strncpy(self->uri, uri, strnlen(uri, PATH_MAX) + 1); 86 112 } 87 113 88 114 self->samplerate = Py_aubio_default_samplerate; 89 if ( (sint_t)samplerate >0) {115 if (samplerate != 0) { 90 116 self->samplerate = samplerate; 91 } else if ((sint_t)samplerate < 0) {92 PyErr_SetString (PyExc_ValueError,93 "can not use negative value for samplerate");94 return NULL;95 117 } 96 118 97 119 self->channels = 1; 98 if ( (sint_t)channels >0) {120 if (channels != 0) { 99 121 self->channels = channels; 100 } else if ((sint_t)channels < 0) {101 PyErr_SetString (PyExc_ValueError,102 "can not use negative or null value for channels");103 return NULL;104 122 } 105 123 … … 110 128 Py_sink_init (Py_sink * self, PyObject * args, PyObject * kwds) 111 129 { 112 if (self->channels == 1) { 113 self->o = new_aubio_sink ( self->uri, self->samplerate ); 114 } else { 115 self->o = new_aubio_sink ( self->uri, 0 ); 116 aubio_sink_preset_channels ( self->o, self->channels ); 117 aubio_sink_preset_samplerate ( self->o, self->samplerate ); 118 } 130 self->o = new_aubio_sink ( self->uri, 0 ); 119 131 if (self->o == NULL) { 120 PyErr_SetString (PyExc_RuntimeError, "error creating sink with this uri");132 // error string was set in new_aubio_sink 121 133 return -1; 122 134 } 135 if (aubio_sink_preset_channels(self->o, self->channels) != 0) { 136 // error string was set in aubio_sink_preset_channels 137 return -1; 138 } 139 if (aubio_sink_preset_samplerate(self->o, self->samplerate) != 0) { 140 // error string was set in aubio_sink_preset_samplerate 141 return -1; 142 } 143 123 144 self->samplerate = aubio_sink_get_samplerate ( self->o ); 124 145 self->channels = aubio_sink_get_channels ( self->o ); … … 132 153 del_aubio_sink(self->o); 133 154 free(self->mwrite_data.data); 155 if (self->uri) { 156 free(self->uri); 157 } 134 158 Py_TYPE(self)->tp_free((PyObject *) self); 135 159 } … … 190 214 static PyMemberDef Py_sink_members[] = { 191 215 {"uri", T_STRING, offsetof (Py_sink, uri), READONLY, 192 " path at which the sink was created"},216 "str (read-only): Path at which the sink was created."}, 193 217 {"samplerate", T_INT, offsetof (Py_sink, samplerate), READONLY, 194 " samplerate at which the sink was created"},218 "int (read-only): Samplerate at which the sink was created."}, 195 219 {"channels", T_INT, offsetof (Py_sink, channels), READONLY, 196 " number of channels with which the sink was created"},220 "int (read-only): Number of channels with which the sink was created."}, 197 221 { NULL } // sentinel 198 222 }; … … 203 227 aubio_sink_close (self->o); 204 228 Py_RETURN_NONE; 229 } 230 231 static char Pyaubio_sink_enter_doc[] = ""; 232 static PyObject* Pyaubio_sink_enter(Py_sink *self, PyObject *unused) { 233 Py_INCREF(self); 234 return (PyObject*)self; 235 } 236 237 static char Pyaubio_sink_exit_doc[] = ""; 238 static PyObject* Pyaubio_sink_exit(Py_sink *self, PyObject *unused) { 239 return Pyaubio_sink_close(self, unused); 205 240 } 206 241 … … 209 244 {"do_multi", (PyCFunction) Py_sink_do_multi, METH_VARARGS, Py_sink_do_multi_doc}, 210 245 {"close", (PyCFunction) Pyaubio_sink_close, METH_NOARGS, Py_sink_close_doc}, 246 {"__enter__", (PyCFunction)Pyaubio_sink_enter, METH_NOARGS, 247 Pyaubio_sink_enter_doc}, 248 {"__exit__", (PyCFunction)Pyaubio_sink_exit, METH_VARARGS, 249 Pyaubio_sink_exit_doc}, 211 250 {NULL} /* sentinel */ 212 251 };
Note: See TracChangeset
for help on using the changeset viewer.