source: python/ext/py-source.c @ 52ca8a3

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 52ca8a3 was 7b56229, checked in by Paul Brossier <piem@piem.org>, 11 years ago

python/ext/py-{sink,source}.c: add close function

  • Property mode set to 100644
File size: 3.3 KB
Line 
1#include "aubiowraphell.h"
2
3typedef struct
4{
5  PyObject_HEAD
6  aubio_source_t * o;
7  char_t* uri;
8  uint_t samplerate;
9  uint_t hop_size;
10} Py_source;
11
12static char Py_source_doc[] = "source object";
13
14static PyObject *
15Py_source_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
16{
17  Py_source *self;
18  char_t* uri = NULL;
19  uint_t samplerate = 0;
20  uint_t hop_size = 0;
21  static char *kwlist[] = { "uri", "samplerate", "hop_size", NULL };
22
23  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|sII", kwlist,
24          &uri, &samplerate, &hop_size)) {
25    return NULL;
26  }
27
28  self = (Py_source *) pytype->tp_alloc (pytype, 0);
29
30  if (self == NULL) {
31    return NULL;
32  }
33
34  self->uri = "none";
35  if (uri != NULL) {
36    self->uri = uri;
37  }
38
39  self->samplerate = 0;
40  if ((sint_t)samplerate > 0) {
41    self->samplerate = samplerate;
42  } else if ((sint_t)samplerate < 0) {
43    PyErr_SetString (PyExc_ValueError,
44        "can not use negative value for samplerate");
45    return NULL;
46  }
47
48  self->hop_size = Py_default_vector_length / 2;
49  if ((sint_t)hop_size > 0) {
50    self->hop_size = hop_size;
51  } else if ((sint_t)hop_size < 0) {
52    PyErr_SetString (PyExc_ValueError,
53        "can not use negative value for hop_size");
54    return NULL;
55  }
56
57  return (PyObject *) self;
58}
59
60static int
61Py_source_init (Py_source * self, PyObject * args, PyObject * kwds)
62{
63  self->o = new_aubio_source ( self->uri, self->samplerate, self->hop_size );
64  if (self->o == NULL) {
65    PyErr_SetString (PyExc_StandardError, "error creating object");
66    return -1;
67  }
68  self->samplerate = aubio_source_get_samplerate ( self->o );
69
70  return 0;
71}
72
73AUBIO_DEL(source)
74
75/* function Py_source_do */
76static PyObject * 
77Py_source_do(Py_source * self, PyObject * args)
78{
79
80
81  /* output vectors prototypes */
82  fvec_t* read_to;
83  uint_t read;
84
85
86
87
88
89 
90  /* creating output read_to as a new_fvec of length self->hop_size */
91  read_to = new_fvec (self->hop_size);
92  read = 0;
93
94
95  /* compute _do function */
96  aubio_source_do (self->o, read_to, &read);
97
98  PyObject *outputs = PyList_New(0);
99  PyList_Append( outputs, (PyObject *)PyAubio_CFvecToArray (read_to));
100  //del_fvec (read_to);
101  PyList_Append( outputs, (PyObject *)PyInt_FromLong (read));
102  return outputs;
103}
104
105AUBIO_MEMBERS_START(source)
106  {"uri", T_STRING, offsetof (Py_source, uri), READONLY, ""},
107  {"samplerate", T_INT, offsetof (Py_source, samplerate), READONLY, ""},
108  {"hop_size", T_INT, offsetof (Py_source, hop_size), READONLY, ""},
109AUBIO_MEMBERS_STOP(source)
110
111
112static PyObject *
113Pyaubio_source_get_samplerate (Py_source *self, PyObject *unused)
114{
115  uint_t tmp = aubio_source_get_samplerate (self->o);
116  return (PyObject *)PyInt_FromLong (tmp);
117}
118
119static PyObject *
120Pyaubio_source_get_channels (Py_source *self, PyObject *unused)
121{
122  uint_t tmp = aubio_source_get_channels (self->o);
123  return (PyObject *)PyInt_FromLong (tmp);
124}
125
126static PyObject *
127Pyaubio_source_close (Py_source *self, PyObject *unused)
128{
129  del_aubio_source (self->o);
130  self->o = NULL;
131  Py_RETURN_NONE;
132}
133
134static PyMethodDef Py_source_methods[] = {
135  {"get_samplerate", (PyCFunction) Pyaubio_source_get_samplerate,
136    METH_NOARGS, ""},
137  {"get_channels", (PyCFunction) Pyaubio_source_get_channels,
138    METH_NOARGS, ""},
139  {"close", (PyCFunction) Pyaubio_source_close,
140    METH_NOARGS, ""},
141  {NULL} /* sentinel */
142};
143
144AUBIO_TYPEOBJECT(source, "aubio.source")
Note: See TracBrowser for help on using the repository browser.