[ec1ce52] | 1 | #! /usr/bin/python |
---|
| 2 | |
---|
| 3 | """ This madness of code is used to generate the C code of the python interface |
---|
| 4 | to aubio. Don't try this at home. |
---|
| 5 | |
---|
| 6 | The list of typedefs and functions is obtained from the command line 'cpp |
---|
| 7 | aubio.h'. This list is then used to parse all the functions about this object. |
---|
| 8 | |
---|
| 9 | I hear the ones asking "why not use swig, or cython, or something like that?" |
---|
| 10 | |
---|
| 11 | The requirements for this extension are the following: |
---|
| 12 | |
---|
| 13 | - aubio vectors can be viewed as numpy arrays, and vice versa |
---|
| 14 | - aubio 'object' should be python classes, not just a bunch of functions |
---|
| 15 | |
---|
| 16 | I haven't met any python interface generator that can meet both these |
---|
| 17 | requirements. If you know of one, please let me know, it will spare me |
---|
| 18 | maintaining this bizarre file. |
---|
| 19 | """ |
---|
| 20 | |
---|
[0b2892f] | 21 | param_numbers = { |
---|
| 22 | 'source': [0, 2], |
---|
[6500367] | 23 | 'sink': [2, 0], |
---|
[0b2892f] | 24 | } |
---|
| 25 | |
---|
[ec1ce52] | 26 | # TODO |
---|
| 27 | # do function: for now, only the following pattern is supported: |
---|
| 28 | # void aubio_<foo>_do (aubio_foo_t * o, |
---|
| 29 | # [input1_t * input, [output1_t * output, ..., output3_t * output]]); |
---|
| 30 | # There is no way of knowing that output1 is actually input2. In the future, |
---|
| 31 | # const could be used for the inputs in the C prototypes. |
---|
| 32 | |
---|
[c71e405] | 33 | def write_msg(*args): |
---|
| 34 | pass |
---|
| 35 | # uncomment out for debugging |
---|
| 36 | #print args |
---|
| 37 | |
---|
[90077e4] | 38 | def split_type(arg): |
---|
| 39 | """ arg = 'foo *name' |
---|
| 40 | return ['foo*', 'name'] """ |
---|
| 41 | l = arg.split() |
---|
[abbd910] | 42 | type_arg = {'type': l[0], 'name': l[1]} |
---|
[90077e4] | 43 | # ['foo', '*name'] -> ['foo*', 'name'] |
---|
| 44 | if l[-1].startswith('*'): |
---|
[abbd910] | 45 | #return [l[0]+'*', l[1][1:]] |
---|
| 46 | type_arg['type'] = l[0] + '*' |
---|
| 47 | type_arg['name'] = l[1][1:] |
---|
[90077e4] | 48 | # ['foo', '*', 'name'] -> ['foo*', 'name'] |
---|
| 49 | if len(l) == 3: |
---|
[abbd910] | 50 | #return [l[0]+l[1], l[2]] |
---|
| 51 | type_arg['type'] = l[0]+l[1] |
---|
| 52 | type_arg['name'] = l[2] |
---|
[90077e4] | 53 | else: |
---|
[abbd910] | 54 | #return l |
---|
| 55 | pass |
---|
| 56 | return type_arg |
---|
[90077e4] | 57 | |
---|
| 58 | def get_params(proto): |
---|
| 59 | """ get the list of parameters from a function prototype |
---|
| 60 | example: proto = "int main (int argc, char ** argv)" |
---|
| 61 | returns: ['int argc', 'char ** argv'] |
---|
| 62 | """ |
---|
| 63 | import re |
---|
| 64 | paramregex = re.compile('[\(, ](\w+ \*?\*? ?\w+)[, \)]') |
---|
| 65 | return paramregex.findall(proto) |
---|
| 66 | |
---|
| 67 | def get_params_types_names(proto): |
---|
| 68 | """ get the list of parameters from a function prototype |
---|
| 69 | example: proto = "int main (int argc, char ** argv)" |
---|
| 70 | returns: [['int', 'argc'], ['char **','argv']] |
---|
| 71 | """ |
---|
| 72 | return map(split_type, get_params(proto)) |
---|
| 73 | |
---|
| 74 | def get_return_type(proto): |
---|
| 75 | import re |
---|
| 76 | paramregex = re.compile('(\w+ ?\*?).*') |
---|
| 77 | outputs = paramregex.findall(proto) |
---|
| 78 | assert len(outputs) == 1 |
---|
| 79 | return outputs[0].replace(' ', '') |
---|
| 80 | |
---|
| 81 | def get_name(proto): |
---|
| 82 | name = proto.split()[1].split('(')[0] |
---|
| 83 | return name.replace('*','') |
---|
| 84 | |
---|
[ec1ce52] | 85 | # the important bits: the size of the output for each objects. this data should |
---|
| 86 | # move into the C library at some point. |
---|
| 87 | defaultsizes = { |
---|
[0b2892f] | 88 | 'resampler': ['input->length * self->ratio'], |
---|
| 89 | 'specdesc': ['1'], |
---|
| 90 | 'onset': ['1'], |
---|
| 91 | 'pitchyin': ['1'], |
---|
| 92 | 'pitchyinfft': ['1'], |
---|
| 93 | 'pitchschmitt': ['1'], |
---|
| 94 | 'pitchmcomb': ['1'], |
---|
| 95 | 'pitchfcomb': ['1'], |
---|
| 96 | 'pitch': ['1'], |
---|
[f1d2af7] | 97 | 'tss': ['self->buf_size', 'self->buf_size'], |
---|
[0b2892f] | 98 | 'mfcc': ['self->n_coeffs'], |
---|
| 99 | 'beattracking': ['self->hop_size'], |
---|
| 100 | 'tempo': ['1'], |
---|
| 101 | 'peakpicker': ['1'], |
---|
| 102 | 'source': ['self->hop_size', '1'], |
---|
[ec1ce52] | 103 | } |
---|
| 104 | |
---|
| 105 | # default value for variables |
---|
| 106 | aubioinitvalue = { |
---|
| 107 | 'uint_t': 0, |
---|
| 108 | 'smpl_t': 0, |
---|
| 109 | 'lsmp_t': 0., |
---|
| 110 | 'char_t*': 'NULL', |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | aubiodefvalue = { |
---|
| 114 | # we have some clean up to do |
---|
| 115 | 'buf_size': 'Py_default_vector_length', |
---|
| 116 | # and here too |
---|
| 117 | 'hop_size': 'Py_default_vector_length / 2', |
---|
| 118 | # these should be alright |
---|
| 119 | 'samplerate': 'Py_aubio_default_samplerate', |
---|
| 120 | # now for the non obvious ones |
---|
| 121 | 'n_filters': '40', |
---|
| 122 | 'n_coeffs': '13', |
---|
| 123 | 'nelems': '10', |
---|
| 124 | 'flow': '0.', |
---|
| 125 | 'fhig': '1.', |
---|
| 126 | 'ilow': '0.', |
---|
| 127 | 'ihig': '1.', |
---|
| 128 | 'thrs': '0.5', |
---|
| 129 | 'ratio': '0.5', |
---|
[5c246b0] | 130 | 'method': '"default"', |
---|
[0b2892f] | 131 | 'uri': '"none"', |
---|
[ec1ce52] | 132 | } |
---|
| 133 | |
---|
| 134 | # aubio to python |
---|
| 135 | aubio2pytypes = { |
---|
| 136 | 'uint_t': 'I', |
---|
[75be651] | 137 | 'smpl_t': 'f', |
---|
| 138 | 'lsmp_t': 'd', |
---|
[90077e4] | 139 | 'fvec_t*': 'O', |
---|
| 140 | 'cvec_t*': 'O', |
---|
[ec1ce52] | 141 | 'char_t*': 's', |
---|
| 142 | } |
---|
| 143 | |
---|
[9c9cd54] | 144 | # python to aubio |
---|
[ec1ce52] | 145 | aubiovecfrompyobj = { |
---|
[91ce8d5] | 146 | 'fvec_t*': 'PyAubio_ArrayToCFvec', |
---|
| 147 | 'cvec_t*': 'PyAubio_ArrayToCCvec', |
---|
[6500367] | 148 | 'uint_t': '(uint_t)PyInt_AsLong', |
---|
[ec1ce52] | 149 | } |
---|
| 150 | |
---|
[9c9cd54] | 151 | # aubio to python |
---|
[ec1ce52] | 152 | aubiovectopyobj = { |
---|
[90077e4] | 153 | 'fvec_t*': 'PyAubio_CFvecToArray', |
---|
[91ce8d5] | 154 | 'cvec_t*': 'PyAubio_CCvecToPyCvec', |
---|
[90077e4] | 155 | 'smpl_t': 'PyFloat_FromDouble', |
---|
[0b2892f] | 156 | 'uint_t*': 'PyInt_FromLong', |
---|
[6500367] | 157 | 'uint_t': 'PyInt_FromLong', |
---|
[90077e4] | 158 | } |
---|
[ec1ce52] | 159 | |
---|
| 160 | def gen_new_init(newfunc, name): |
---|
[90077e4] | 161 | newparams = get_params_types_names(newfunc) |
---|
[ec1ce52] | 162 | # self->param1, self->param2, self->param3 |
---|
[96fe713] | 163 | if len(newparams): |
---|
[abbd910] | 164 | selfparams = ', self->'+', self->'.join([p['name'] for p in newparams]) |
---|
[96fe713] | 165 | else: |
---|
| 166 | selfparams = '' |
---|
[ec1ce52] | 167 | # "param1", "param2", "param3" |
---|
[abbd910] | 168 | paramnames = ", ".join(["\""+p['name']+"\"" for p in newparams]) |
---|
| 169 | pyparams = "".join(map(lambda p: aubio2pytypes[p['type']], newparams)) |
---|
| 170 | paramrefs = ", ".join(["&" + p['name'] for p in newparams]) |
---|
[ec1ce52] | 171 | s = """\ |
---|
| 172 | // WARNING: this file is generated, DO NOT EDIT |
---|
[90077e4] | 173 | |
---|
| 174 | // WARNING: if you haven't read the first line yet, please do so |
---|
[ec1ce52] | 175 | #include "aubiowraphell.h" |
---|
| 176 | |
---|
| 177 | typedef struct |
---|
| 178 | { |
---|
| 179 | PyObject_HEAD |
---|
| 180 | aubio_%(name)s_t * o; |
---|
| 181 | """ % locals() |
---|
[abbd910] | 182 | for p in newparams: |
---|
| 183 | ptype = p['type'] |
---|
| 184 | pname = p['name'] |
---|
[ec1ce52] | 185 | s += """\ |
---|
| 186 | %(ptype)s %(pname)s; |
---|
| 187 | """ % locals() |
---|
| 188 | s += """\ |
---|
| 189 | } Py_%(name)s; |
---|
| 190 | |
---|
| 191 | static char Py_%(name)s_doc[] = "%(name)s object"; |
---|
| 192 | |
---|
| 193 | static PyObject * |
---|
| 194 | Py_%(name)s_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
| 195 | { |
---|
[96fe713] | 196 | Py_%(name)s *self; |
---|
[ec1ce52] | 197 | """ % locals() |
---|
[abbd910] | 198 | for p in newparams: |
---|
| 199 | ptype = p['type'] |
---|
| 200 | pname = p['name'] |
---|
[09b1777] | 201 | initval = aubioinitvalue[ptype] |
---|
[ec1ce52] | 202 | s += """\ |
---|
[09b1777] | 203 | %(ptype)s %(pname)s = %(initval)s; |
---|
[ec1ce52] | 204 | """ % locals() |
---|
[09b1777] | 205 | # now the actual PyArg_Parse |
---|
[96fe713] | 206 | if len(paramnames): |
---|
| 207 | s += """\ |
---|
[ec1ce52] | 208 | static char *kwlist[] = { %(paramnames)s, NULL }; |
---|
| 209 | |
---|
| 210 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|%(pyparams)s", kwlist, |
---|
| 211 | %(paramrefs)s)) { |
---|
| 212 | return NULL; |
---|
| 213 | } |
---|
[96fe713] | 214 | """ % locals() |
---|
| 215 | s += """\ |
---|
[ec1ce52] | 216 | |
---|
| 217 | self = (Py_%(name)s *) pytype->tp_alloc (pytype, 0); |
---|
| 218 | |
---|
| 219 | if (self == NULL) { |
---|
| 220 | return NULL; |
---|
| 221 | } |
---|
| 222 | """ % locals() |
---|
[abbd910] | 223 | for p in newparams: |
---|
| 224 | ptype = p['type'] |
---|
| 225 | pname = p['name'] |
---|
[ec1ce52] | 226 | defval = aubiodefvalue[pname] |
---|
[09b1777] | 227 | if ptype == 'char_t*': |
---|
| 228 | s += """\ |
---|
| 229 | |
---|
| 230 | self->%(pname)s = %(defval)s; |
---|
| 231 | if (%(pname)s != NULL) { |
---|
| 232 | self->%(pname)s = %(pname)s; |
---|
| 233 | } |
---|
| 234 | """ % locals() |
---|
| 235 | elif ptype == 'uint_t': |
---|
| 236 | s += """\ |
---|
[ec1ce52] | 237 | |
---|
| 238 | self->%(pname)s = %(defval)s; |
---|
| 239 | if (%(pname)s > 0) { |
---|
| 240 | self->%(pname)s = %(pname)s; |
---|
| 241 | } else if (%(pname)s < 0) { |
---|
| 242 | PyErr_SetString (PyExc_ValueError, |
---|
[09b1777] | 243 | "can not use negative value for %(pname)s"); |
---|
[ec1ce52] | 244 | return NULL; |
---|
| 245 | } |
---|
| 246 | """ % locals() |
---|
[09b1777] | 247 | elif ptype == 'smpl_t': |
---|
| 248 | s += """\ |
---|
| 249 | |
---|
| 250 | self->%(pname)s = %(defval)s; |
---|
| 251 | if (%(pname)s != %(defval)s) { |
---|
| 252 | self->%(pname)s = %(pname)s; |
---|
| 253 | } |
---|
| 254 | """ % locals() |
---|
| 255 | else: |
---|
[c71e405] | 256 | write_msg ("ERROR, unknown type of parameter %s %s" % (ptype, pname) ) |
---|
[ec1ce52] | 257 | s += """\ |
---|
| 258 | |
---|
| 259 | return (PyObject *) self; |
---|
| 260 | } |
---|
| 261 | |
---|
[96fe713] | 262 | AUBIO_INIT(%(name)s %(selfparams)s) |
---|
[ec1ce52] | 263 | |
---|
| 264 | AUBIO_DEL(%(name)s) |
---|
| 265 | |
---|
| 266 | """ % locals() |
---|
| 267 | return s |
---|
| 268 | |
---|
[0b2892f] | 269 | def gen_do_input_params(inputparams): |
---|
| 270 | inputdefs = '' |
---|
| 271 | parseinput = '' |
---|
| 272 | inputrefs = '' |
---|
| 273 | inputvecs = '' |
---|
| 274 | pytypes = '' |
---|
| 275 | |
---|
| 276 | if len(inputparams): |
---|
[ec1ce52] | 277 | # build the parsing string for PyArg_ParseTuple |
---|
[0b2892f] | 278 | pytypes = "".join([aubio2pytypes[p['type']] for p in inputparams]) |
---|
[abbd910] | 279 | |
---|
[6500367] | 280 | inputdefs = " /* input vectors python prototypes */\n" |
---|
| 281 | for p in inputparams: |
---|
| 282 | if p['type'] != 'uint_t': |
---|
| 283 | inputdefs += " PyObject * " + p['name'] + "_obj;\n" |
---|
[abbd910] | 284 | |
---|
[6500367] | 285 | inputvecs = " /* input vectors prototypes */\n " |
---|
[abbd910] | 286 | inputvecs += "\n ".join(map(lambda p: p['type'] + ' ' + p['name'] + ";", inputparams)) |
---|
| 287 | |
---|
[6500367] | 288 | parseinput = " /* input vectors parsing */\n " |
---|
[ec1ce52] | 289 | for p in inputparams: |
---|
[abbd910] | 290 | inputvec = p['name'] |
---|
[6500367] | 291 | if p['type'] != 'uint_t': |
---|
| 292 | inputdef = p['name'] + "_obj" |
---|
| 293 | else: |
---|
| 294 | inputdef = p['name'] |
---|
[abbd910] | 295 | converter = aubiovecfrompyobj[p['type']] |
---|
[6500367] | 296 | if p['type'] != 'uint_t': |
---|
| 297 | parseinput += """%(inputvec)s = %(converter)s (%(inputdef)s); |
---|
[ec1ce52] | 298 | |
---|
| 299 | if (%(inputvec)s == NULL) { |
---|
| 300 | return NULL; |
---|
[6500367] | 301 | } |
---|
| 302 | |
---|
| 303 | """ % locals() |
---|
[abbd910] | 304 | |
---|
[ec1ce52] | 305 | # build the string for the input objects references |
---|
[6500367] | 306 | inputreflist = [] |
---|
| 307 | for p in inputparams: |
---|
| 308 | if p['type'] != 'uint_t': |
---|
| 309 | inputreflist += [ "&" + p['name'] + "_obj" ] |
---|
| 310 | else: |
---|
| 311 | inputreflist += [ "&" + p['name'] ] |
---|
| 312 | inputrefs = ", ".join(inputreflist) |
---|
[ec1ce52] | 313 | # end of inputs strings |
---|
[0b2892f] | 314 | return inputdefs, parseinput, inputrefs, inputvecs, pytypes |
---|
| 315 | |
---|
| 316 | def gen_do_output_params(outputparams, name): |
---|
| 317 | outputvecs = "" |
---|
| 318 | outputcreate = "" |
---|
| 319 | if len(outputparams): |
---|
| 320 | outputvecs = " /* output vectors prototypes */\n" |
---|
| 321 | for p in outputparams: |
---|
| 322 | params = { |
---|
| 323 | 'name': p['name'], 'pytype': p['type'], 'autype': p['type'][:-3], |
---|
| 324 | 'length': defaultsizes[name].pop(0) } |
---|
| 325 | if (p['type'] == 'uint_t*'): |
---|
| 326 | outputvecs += ' uint_t' + ' ' + p['name'] + ";\n" |
---|
| 327 | outputcreate += " %(name)s = 0;\n" % params |
---|
| 328 | else: |
---|
| 329 | outputvecs += " " + p['type'] + ' ' + p['name'] + ";\n" |
---|
| 330 | outputcreate += " /* creating output %(name)s as a new_%(autype)s of length %(length)s */\n" % params |
---|
| 331 | outputcreate += " %(name)s = new_%(autype)s (%(length)s);\n" % params |
---|
| 332 | |
---|
| 333 | returnval = ""; |
---|
| 334 | if len(outputparams) > 1: |
---|
| 335 | returnval += " PyObject *outputs = PyList_New(0);\n" |
---|
| 336 | for p in outputparams: |
---|
| 337 | returnval += " PyList_Append( outputs, (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" +");\n" |
---|
| 338 | returnval += " return outputs;" |
---|
| 339 | elif len(outputparams) == 1: |
---|
| 340 | if defaultsizes[name] == '1': |
---|
| 341 | returnval += " return (PyObject *)PyFloat_FromDouble(" + p['name'] + "->data[0])" |
---|
| 342 | else: |
---|
| 343 | returnval += " return (PyObject *)" + aubiovectopyobj[p['type']] + " (" + p['name'] + ")" |
---|
| 344 | else: |
---|
| 345 | returnval = " return Py_None;"; |
---|
| 346 | # end of output strings |
---|
| 347 | return outputvecs, outputcreate, returnval |
---|
[ec1ce52] | 348 | |
---|
[0b2892f] | 349 | def gen_do(dofunc, name): |
---|
| 350 | funcname = dofunc.split()[1].split('(')[0] |
---|
| 351 | doparams = get_params_types_names(dofunc) |
---|
| 352 | # make sure the first parameter is the object |
---|
| 353 | assert doparams[0]['type'] == "aubio_"+name+"_t*", \ |
---|
| 354 | "method is not in 'aubio_<name>_t" |
---|
| 355 | # and remove it |
---|
| 356 | doparams = doparams[1:] |
---|
| 357 | |
---|
| 358 | n_param = len(doparams) |
---|
| 359 | |
---|
| 360 | if name in param_numbers.keys(): |
---|
| 361 | n_input_param, n_output_param = param_numbers[name] |
---|
[ec1ce52] | 362 | else: |
---|
[0b2892f] | 363 | n_input_param, n_output_param = 1, n_param - 1 |
---|
[ec1ce52] | 364 | |
---|
[0b2892f] | 365 | assert n_output_param + n_input_param == n_param, "n_output_param + n_input_param != n_param for %s" % name |
---|
| 366 | |
---|
| 367 | inputparams = doparams[:n_input_param] |
---|
| 368 | outputparams = doparams[n_input_param:n_input_param + n_output_param] |
---|
[ec1ce52] | 369 | |
---|
[0b2892f] | 370 | inputdefs, parseinput, inputrefs, inputvecs, pytypes = gen_do_input_params(inputparams); |
---|
| 371 | outputvecs, outputcreate, returnval = gen_do_output_params(outputparams, name) |
---|
| 372 | |
---|
| 373 | # build strings for outputs |
---|
| 374 | # build the parameters for the _do() call |
---|
| 375 | doparams_string = "self->o" |
---|
| 376 | for p in doparams: |
---|
| 377 | if p['type'] == 'uint_t*': |
---|
| 378 | doparams_string += ", &" + p['name'] |
---|
| 379 | else: |
---|
| 380 | doparams_string += ", " + p['name'] |
---|
| 381 | |
---|
| 382 | if n_input_param: |
---|
| 383 | arg_parse_tuple = """\ |
---|
| 384 | if (!PyArg_ParseTuple (args, "%(pytypes)s", %(inputrefs)s)) { |
---|
| 385 | return NULL; |
---|
| 386 | } |
---|
| 387 | """ % locals() |
---|
| 388 | else: |
---|
| 389 | arg_parse_tuple = "" |
---|
[ec1ce52] | 390 | # put it all together |
---|
| 391 | s = """\ |
---|
[abbd910] | 392 | /* function Py_%(name)s_do */ |
---|
[ec1ce52] | 393 | static PyObject * |
---|
| 394 | Py_%(name)s_do(Py_%(name)s * self, PyObject * args) |
---|
| 395 | { |
---|
[0b2892f] | 396 | %(inputdefs)s |
---|
| 397 | %(inputvecs)s |
---|
| 398 | %(outputvecs)s |
---|
[ec1ce52] | 399 | |
---|
[0b2892f] | 400 | %(arg_parse_tuple)s |
---|
[ec1ce52] | 401 | |
---|
[0b2892f] | 402 | %(parseinput)s |
---|
[ec1ce52] | 403 | |
---|
[0b2892f] | 404 | %(outputcreate)s |
---|
[ec1ce52] | 405 | |
---|
| 406 | /* compute _do function */ |
---|
| 407 | %(funcname)s (%(doparams_string)s); |
---|
| 408 | |
---|
[0b2892f] | 409 | %(returnval)s; |
---|
[ec1ce52] | 410 | } |
---|
| 411 | """ % locals() |
---|
| 412 | return s |
---|
| 413 | |
---|
| 414 | def gen_members(new_method, name): |
---|
[90077e4] | 415 | newparams = get_params_types_names(new_method) |
---|
[ec1ce52] | 416 | s = """ |
---|
| 417 | AUBIO_MEMBERS_START(%(name)s)""" % locals() |
---|
| 418 | for param in newparams: |
---|
[abbd910] | 419 | if param['type'] == 'char_t*': |
---|
[09b1777] | 420 | s += """ |
---|
| 421 | {"%(pname)s", T_STRING, offsetof (Py_%(name)s, %(pname)s), READONLY, ""},""" \ |
---|
[abbd910] | 422 | % { 'pname': param['name'], 'ptype': param['type'], 'name': name} |
---|
| 423 | elif param['type'] == 'uint_t': |
---|
[09b1777] | 424 | s += """ |
---|
[ec1ce52] | 425 | {"%(pname)s", T_INT, offsetof (Py_%(name)s, %(pname)s), READONLY, ""},""" \ |
---|
[abbd910] | 426 | % { 'pname': param['name'], 'ptype': param['type'], 'name': name} |
---|
| 427 | elif param['type'] == 'smpl_t': |
---|
[5c246b0] | 428 | s += """ |
---|
| 429 | {"%(pname)s", T_FLOAT, offsetof (Py_%(name)s, %(pname)s), READONLY, ""},""" \ |
---|
[abbd910] | 430 | % { 'pname': param['name'], 'ptype': param['type'], 'name': name} |
---|
[09b1777] | 431 | else: |
---|
[c71e405] | 432 | write_msg ("-- ERROR, unknown member type ", param ) |
---|
[ec1ce52] | 433 | s += """ |
---|
| 434 | AUBIO_MEMBERS_STOP(%(name)s) |
---|
| 435 | |
---|
| 436 | """ % locals() |
---|
| 437 | return s |
---|
| 438 | |
---|
[90077e4] | 439 | |
---|
[ec1ce52] | 440 | def gen_methods(get_methods, set_methods, name): |
---|
[90077e4] | 441 | s = "" |
---|
| 442 | method_defs = "" |
---|
| 443 | for method in set_methods: |
---|
| 444 | method_name = get_name(method) |
---|
| 445 | params = get_params_types_names(method) |
---|
| 446 | out_type = get_return_type(method) |
---|
[abbd910] | 447 | assert params[0]['type'] == "aubio_"+name+"_t*", \ |
---|
[90077e4] | 448 | "get method is not in 'aubio_<name>_t" |
---|
[c71e405] | 449 | write_msg (method ) |
---|
| 450 | write_msg (params[1:]) |
---|
[abbd910] | 451 | setter_args = "self->o, " +",".join([p['name'] for p in params[1:]]) |
---|
[90077e4] | 452 | parse_args = "" |
---|
| 453 | for p in params[1:]: |
---|
[abbd910] | 454 | parse_args += p['type'] + " " + p['name'] + ";\n" |
---|
| 455 | argmap = "".join([aubio2pytypes[p['type']] for p in params[1:]]) |
---|
| 456 | arglist = ", ".join(["&"+p['name'] for p in params[1:]]) |
---|
[90077e4] | 457 | parse_args += """ |
---|
| 458 | if (!PyArg_ParseTuple (args, "%(argmap)s", %(arglist)s)) { |
---|
| 459 | return NULL; |
---|
| 460 | } """ % locals() |
---|
| 461 | s += """ |
---|
| 462 | static PyObject * |
---|
| 463 | Py%(funcname)s (Py_%(objname)s *self, PyObject *args) |
---|
| 464 | { |
---|
| 465 | uint_t err = 0; |
---|
| 466 | |
---|
| 467 | %(parse_args)s |
---|
| 468 | |
---|
| 469 | err = %(funcname)s (%(setter_args)s); |
---|
| 470 | |
---|
| 471 | if (err > 0) { |
---|
| 472 | PyErr_SetString (PyExc_ValueError, |
---|
| 473 | "error running %(funcname)s"); |
---|
| 474 | return NULL; |
---|
| 475 | } |
---|
| 476 | return Py_None; |
---|
| 477 | } |
---|
| 478 | """ % {'funcname': method_name, 'objname': name, |
---|
| 479 | 'out_type': out_type, 'setter_args': setter_args, 'parse_args': parse_args } |
---|
| 480 | shortname = method_name.split(name+'_')[-1] |
---|
| 481 | method_defs += """\ |
---|
| 482 | {"%(shortname)s", (PyCFunction) Py%(method_name)s, |
---|
| 483 | METH_VARARGS, ""}, |
---|
| 484 | """ % locals() |
---|
| 485 | |
---|
| 486 | for method in get_methods: |
---|
| 487 | method_name = get_name(method) |
---|
| 488 | params = get_params_types_names(method) |
---|
| 489 | out_type = get_return_type(method) |
---|
[abbd910] | 490 | assert params[0]['type'] == "aubio_"+name+"_t*", \ |
---|
| 491 | "get method is not in 'aubio_<name>_t %s" % params[0]['type'] |
---|
[90077e4] | 492 | assert len(params) == 1, \ |
---|
| 493 | "get method has more than one parameter %s" % params |
---|
| 494 | getter_args = "self->o" |
---|
[9c9cd54] | 495 | returnval = "(PyObject *)" + aubiovectopyobj[out_type] + " (tmp)" |
---|
[90077e4] | 496 | shortname = method_name.split(name+'_')[-1] |
---|
| 497 | method_defs += """\ |
---|
| 498 | {"%(shortname)s", (PyCFunction) Py%(method_name)s, |
---|
| 499 | METH_NOARGS, ""}, |
---|
| 500 | """ % locals() |
---|
| 501 | s += """ |
---|
| 502 | static PyObject * |
---|
| 503 | Py%(funcname)s (Py_%(objname)s *self, PyObject *unused) |
---|
| 504 | { |
---|
| 505 | %(out_type)s tmp = %(funcname)s (%(getter_args)s); |
---|
| 506 | return %(returnval)s; |
---|
| 507 | } |
---|
| 508 | """ % {'funcname': method_name, 'objname': name, |
---|
| 509 | 'out_type': out_type, 'getter_args': getter_args, 'returnval': returnval } |
---|
| 510 | |
---|
| 511 | s += """ |
---|
[ec1ce52] | 512 | static PyMethodDef Py_%(name)s_methods[] = { |
---|
| 513 | """ % locals() |
---|
[90077e4] | 514 | s += method_defs |
---|
[ec1ce52] | 515 | s += """\ |
---|
| 516 | {NULL} /* sentinel */ |
---|
| 517 | }; |
---|
| 518 | """ % locals() |
---|
| 519 | return s |
---|
| 520 | |
---|
| 521 | def gen_finish(name): |
---|
| 522 | s = """\ |
---|
| 523 | |
---|
| 524 | AUBIO_TYPEOBJECT(%(name)s, "aubio.%(name)s") |
---|
| 525 | """ % locals() |
---|
| 526 | return s |
---|