Mercurial > hg > vampy
comparison PyRealTime.cpp @ 31:4f1894c7591b vampy2
Created Vampy2 branch
author | fazekasgy |
---|---|
date | Sun, 20 Sep 2009 17:31:20 +0000 |
parents | |
children | c905122f79e7 |
comparison
equal
deleted
inserted
replaced
28:5139bf30f208 | 31:4f1894c7591b |
---|---|
1 #include <Python.h> | |
2 #include "PyRealTime.h" | |
3 #include "vamp-sdk/Plugin.h" | |
4 #include <string> | |
5 | |
6 using namespace std; | |
7 using namespace Vamp; | |
8 using Vamp::Plugin; | |
9 using Vamp::RealTime; | |
10 | |
11 | |
12 /* CONSTRUCTOR: New RealTime object from sec and nsec */ | |
13 static PyObject* | |
14 RealTime_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |
15 { | |
16 unsigned int sec = 0; | |
17 unsigned int nsec = 0; | |
18 double unary = 0; | |
19 const char *fmt = NULL; | |
20 | |
21 if ( | |
22 /// new RealTime from ('format',float) e.g. ('seconds',2.34123) | |
23 !PyArg_ParseTuple(args, "|sd:RealTime.new ", | |
24 (const char *) &fmt, | |
25 (double *) &unary) && | |
26 | |
27 /// new RealTime from (sec{int},nsec{int}) e.g. (2,34) | |
28 !PyArg_ParseTuple(args, "|II:RealTime.new ", | |
29 (unsigned int*) &sec, | |
30 (unsigned int*) &nsec) | |
31 | |
32 ) { | |
33 PyErr_SetString(PyExc_TypeError, | |
34 "RealTime initialised with wrong arguments."); | |
35 return NULL; | |
36 } | |
37 | |
38 // PyErr_Clear(); | |
39 | |
40 // RealTimeObject *self = PyObject_New(RealTimeObject, &RealTime_Type); | |
41 RealTimeObject *self = (RealTimeObject*)type->tp_alloc(type, 0); | |
42 | |
43 if (self == NULL) return NULL; | |
44 | |
45 self->rt = NULL; | |
46 | |
47 if (sec == 0 && nsec == 0 && fmt == 0) | |
48 self->rt = new RealTime::RealTime(); | |
49 else if (fmt == 0) | |
50 self->rt = new RealTime::RealTime(sec,nsec); | |
51 else { | |
52 /// new RealTime from seconds or milliseconds: i.e. >>>RealTime('seconds',12.3) | |
53 if (!string(fmt).compare("float") || | |
54 !string(fmt).compare("seconds")) | |
55 self->rt = new RealTime::RealTime( | |
56 RealTime::fromSeconds((double) unary)); | |
57 | |
58 if (!string(fmt).compare("milliseconds")) { | |
59 self->rt = new RealTime::RealTime( | |
60 RealTime::fromSeconds((double) unary / 1000.0)); } | |
61 } | |
62 | |
63 if (!self->rt) { | |
64 PyErr_SetString(PyExc_TypeError, | |
65 "RealTime initialised with wrong arguments."); | |
66 return NULL; | |
67 } | |
68 | |
69 return (PyObject *) self; | |
70 } | |
71 | |
72 /* DESTRUCTOR: delete type object */ | |
73 static void | |
74 RealTimeObject_dealloc(RealTimeObject *self) | |
75 { | |
76 if (self->rt) delete self->rt; //delete the C object | |
77 PyObject_Del(self); //delete the Python object (original) | |
78 /// this requires PyType_Ready() which fills ob_type | |
79 // self->ob_type->tp_free((PyObject*)self); | |
80 } | |
81 | |
82 /* RealTime Object's Methods */ | |
83 //these are internals not exposed by the module but the object | |
84 | |
85 /* Returns a Tuple containing sec and nsec values */ | |
86 static PyObject * | |
87 RealTime_values(RealTimeObject *self) | |
88 { | |
89 return Py_BuildValue("(ii)",self->rt->sec,self->rt->nsec); | |
90 } | |
91 | |
92 /* Returns a Text representation */ | |
93 static PyObject * | |
94 RealTime_toString(RealTimeObject *self, PyObject *args) | |
95 { | |
96 return Py_BuildValue("s",self->rt->toText().c_str()); | |
97 } | |
98 | |
99 /* Frame representation */ | |
100 static PyObject * | |
101 RealTime_toFrame(PyObject *self, PyObject *args) | |
102 { | |
103 unsigned int samplerate; | |
104 | |
105 if ( !PyArg_ParseTuple(args, "I:realtime.toFrame object ", | |
106 (unsigned int *) &samplerate )) { | |
107 PyErr_SetString(PyExc_ValueError,"Integer Sample Rate Required."); | |
108 return NULL; | |
109 } | |
110 | |
111 return Py_BuildValue("k", | |
112 RealTime::realTime2Frame( | |
113 *(const RealTime*) ((RealTimeObject*)self)->rt, | |
114 (unsigned int) samplerate)); | |
115 } | |
116 | |
117 /* Conversion of realtime to a double precision floating point value */ | |
118 /* ...in Python called by e.g. float(realtime) */ | |
119 static PyObject * | |
120 RealTime_float(PyObject *s) | |
121 { | |
122 double drt = ((double) ((RealTimeObject*)s)->rt->sec + | |
123 (double)((double) ((RealTimeObject*)s)->rt->nsec)/1000000000); | |
124 return PyFloat_FromDouble(drt); | |
125 } | |
126 | |
127 | |
128 /* Type object's (RealTime) methods table */ | |
129 static PyMethodDef RealTime_methods[] = | |
130 { | |
131 {"values", (PyCFunction)RealTime_values, METH_NOARGS, | |
132 PyDoc_STR("values() -> Tuple of sec,nsec representation.")}, | |
133 | |
134 {"toString", (PyCFunction)RealTime_toString, METH_NOARGS, | |
135 PyDoc_STR("toString() -> Return a user-readable string to the nearest millisecond in a form like HH:MM:SS.mmm")}, | |
136 | |
137 {"toFrame", (PyCFunction)RealTime_toFrame, METH_VARARGS, | |
138 PyDoc_STR("toFrame(samplerate) -> Sample count for given sample rate.")}, | |
139 | |
140 {"toFloat", (PyCFunction)RealTime_float, METH_NOARGS, | |
141 PyDoc_STR("toFloat() -> Floating point representation.")}, | |
142 | |
143 {NULL, NULL} /* sentinel */ | |
144 }; | |
145 | |
146 | |
147 /* Methods implementing protocols */ | |
148 // these functions are called by the interpreter | |
149 | |
150 /* Object Protocol */ | |
151 | |
152 static int | |
153 RealTime_setattr(RealTimeObject *self, char *name, PyObject *value) | |
154 { | |
155 | |
156 if ( !string(name).compare("sec")) { | |
157 self->rt->sec= (int) PyInt_AS_LONG(value); | |
158 return 0; | |
159 } | |
160 | |
161 if ( !string(name).compare("nsec")) { | |
162 self->rt->nsec= (int) PyInt_AS_LONG(value); | |
163 return 0; | |
164 } | |
165 | |
166 return -1; | |
167 } | |
168 | |
169 static PyObject * | |
170 RealTime_getattr(RealTimeObject *self, char *name) | |
171 { | |
172 | |
173 if ( !string(name).compare("sec") ) { | |
174 return PyInt_FromSsize_t( | |
175 (Py_ssize_t) self->rt->sec); | |
176 } | |
177 | |
178 if ( !string(name).compare("nsec") ) { | |
179 return PyInt_FromSsize_t( | |
180 (Py_ssize_t) self->rt->nsec); | |
181 } | |
182 | |
183 return Py_FindMethod(RealTime_methods, | |
184 (PyObject *)self, name); | |
185 } | |
186 | |
187 /* String representation called by e.g. str(realtime), print realtime*/ | |
188 static PyObject * | |
189 RealTime_repr(PyObject *self) | |
190 { | |
191 return Py_BuildValue("s", | |
192 ((RealTimeObject*)self)->rt->toString().c_str()); | |
193 } | |
194 | |
195 | |
196 /* Number Protocol */ | |
197 /// Only add and substract make sense, or what about the | |
198 /// square root of Monday morning? | |
199 /// Divide by integer maybe for durations? | |
200 | |
201 static PyObject * | |
202 RealTime_add(PyObject *s, PyObject *w) | |
203 { | |
204 RealTimeObject *result = | |
205 PyObject_New(RealTimeObject, &RealTime_Type); | |
206 if (result == NULL) return NULL; | |
207 | |
208 result->rt = new RealTime::RealTime( | |
209 *((RealTimeObject*)s)->rt + *((RealTimeObject*)w)->rt); | |
210 return (PyObject*)result; | |
211 } | |
212 | |
213 static PyObject * | |
214 RealTime_subtract(PyObject *s, PyObject *w) | |
215 { | |
216 RealTimeObject *result = | |
217 PyObject_New(RealTimeObject, &RealTime_Type); | |
218 if (result == NULL) return NULL; | |
219 | |
220 result->rt = new RealTime::RealTime( | |
221 *((RealTimeObject*)s)->rt - *((RealTimeObject*)w)->rt); | |
222 return (PyObject*)result; | |
223 } | |
224 | |
225 static PyNumberMethods realtime_as_number = | |
226 { | |
227 RealTime_add, /*nb_add*/ | |
228 RealTime_subtract, /*nb_subtract*/ | |
229 0, /*nb_multiply*/ | |
230 0, /*nb_divide*/ | |
231 0, /*nb_remainder*/ | |
232 0, /*nb_divmod*/ | |
233 0, /*nb_power*/ | |
234 0, /*nb_neg*/ | |
235 0, /*nb_pos*/ | |
236 0, /*(unaryfunc)array_abs,*/ | |
237 0, /*nb_nonzero*/ | |
238 0, /*nb_invert*/ | |
239 0, /*nb_lshift*/ | |
240 0, /*nb_rshift*/ | |
241 0, /*nb_and*/ | |
242 0, /*nb_xor*/ | |
243 0, /*nb_or*/ | |
244 0, /*nb_coerce*/ | |
245 0, /*nb_int*/ | |
246 0, /*nb_long*/ | |
247 (unaryfunc)RealTime_float,/*nb_float*/ | |
248 0, /*nb_oct*/ | |
249 0, /*nb_hex*/ | |
250 }; | |
251 | |
252 /* REAL-TIME TYPE OBJECT */ | |
253 | |
254 #define RealTime_alloc PyType_GenericAlloc | |
255 #define RealTime_free PyObject_Del | |
256 | |
257 /* Doc:: 10.3 Type Objects */ /* static */ | |
258 PyTypeObject RealTime_Type = | |
259 { | |
260 PyObject_HEAD_INIT(NULL) | |
261 0, /*ob_size*/ | |
262 "vampy.RealTime", /*tp_name*/ | |
263 sizeof(RealTimeObject), /*tp_basicsize*/ | |
264 0,//sizeof(RealTime), /*tp_itemsize*/ | |
265 /* methods */ | |
266 (destructor)RealTimeObject_dealloc, /*tp_dealloc*/ | |
267 0, /*tp_print*/ | |
268 (getattrfunc)RealTime_getattr, /*tp_getattr*/ | |
269 (setattrfunc)RealTime_setattr, /*tp_setattr*/ | |
270 0, /*tp_compare*/ | |
271 RealTime_repr, /*tp_repr*/ | |
272 &realtime_as_number, /*tp_as_number*/ | |
273 0, /*tp_as_sequence*/ | |
274 0, /*tp_as_mapping*/ | |
275 0, /*tp_hash*/ | |
276 0,//(ternaryfunc)RealTime_new, /*tp_call*/ | |
277 0, /*tp_str*/ | |
278 0, /*tp_getattro*/ | |
279 0, /*tp_setattro*/ | |
280 0, /*tp_as_buffer*/ | |
281 Py_TPFLAGS_DEFAULT, /*tp_flags*/ | |
282 "RealTime Object", /*tp_doc*/ | |
283 0, /*tp_traverse*/ | |
284 0, /*tp_clear*/ | |
285 0, /*tp_richcompare*/ | |
286 0, /*tp_weaklistoffset*/ | |
287 0, /*tp_iter*/ | |
288 0, /*tp_iternext*/ | |
289 RealTime_methods, /*tp_methods*/ //TypeObject Methods | |
290 0, /*tp_members*/ | |
291 0, /*tp_getset*/ | |
292 0, /*tp_base*/ | |
293 0, /*tp_dict*/ | |
294 0, /*tp_descr_get*/ | |
295 0, /*tp_descr_set*/ | |
296 0, /*tp_dictoffset*/ | |
297 0, /*tp_init*/ | |
298 RealTime_alloc, /*tp_alloc*/ | |
299 RealTime_new, /*tp_new*/ | |
300 RealTime_free, /*tp_free*/ | |
301 0, /*tp_is_gc*/ | |
302 }; | |
303 | |
304 | |
305 | |
306 /* PyRealTime C++ API */ | |
307 | |
308 /*PyRealTime from RealTime pointer | |
309 PyObject* | |
310 PyRealTime_FromRealTime(Vamp::RealTime *rt) { | |
311 | |
312 RealTimeObject *self = | |
313 PyObject_New(RealTimeObject, &RealTime_Type); | |
314 if (self == NULL) return NULL; | |
315 | |
316 self->rt = new RealTime::RealTime(*rt); | |
317 return (PyObject*) self; | |
318 }*/ | |
319 | |
320 | |
321 /*PyRealTime from RealTime*/ | |
322 PyObject* | |
323 PyRealTime_FromRealTime(Vamp::RealTime& rt) { | |
324 | |
325 RealTimeObject *self = | |
326 PyObject_New(RealTimeObject, &RealTime_Type); | |
327 if (self == NULL) return NULL; | |
328 | |
329 self->rt = new RealTime::RealTime(rt); | |
330 return (PyObject*) self; | |
331 } | |
332 | |
333 /*RealTime* from PyRealTime*/ | |
334 const Vamp::RealTime::RealTime* | |
335 PyRealTime_AsRealTime (PyObject *self) { | |
336 | |
337 RealTimeObject *s = (RealTimeObject*) self; | |
338 | |
339 if (!PyRealTime_Check(s)) { | |
340 PyErr_SetString(PyExc_TypeError, "RealTime Object Expected."); | |
341 cerr << "in call PyRealTime_AsPointer(): RealTime Object Expected. " << endl; | |
342 return NULL; } | |
343 return s->rt; | |
344 }; | |
345 |