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