[ccb9fb5] | 1 | aubiodefvalue = { |
---|
| 2 | # we have some clean up to do |
---|
| 3 | 'buf_size': 'Py_default_vector_length', |
---|
| 4 | 'win_s': 'Py_default_vector_length', |
---|
[f633b90] | 5 | 'size': 'Py_default_vector_length', |
---|
[ccb9fb5] | 6 | # and here too |
---|
| 7 | 'hop_size': 'Py_default_vector_length / 2', |
---|
| 8 | 'hop_s': 'Py_default_vector_length / 2', |
---|
| 9 | # these should be alright |
---|
| 10 | 'samplerate': 'Py_aubio_default_samplerate', |
---|
| 11 | # now for the non obvious ones |
---|
| 12 | 'n_filters': '40', |
---|
| 13 | 'n_coeffs': '13', |
---|
| 14 | 'nelems': '10', |
---|
| 15 | 'flow': '0.', |
---|
| 16 | 'fhig': '1.', |
---|
| 17 | 'ilow': '0.', |
---|
| 18 | 'ihig': '1.', |
---|
| 19 | 'thrs': '0.5', |
---|
| 20 | 'ratio': '0.5', |
---|
| 21 | 'method': '"default"', |
---|
| 22 | 'uri': '"none"', |
---|
[f633b90] | 23 | 'bins_per_octave': 24, |
---|
[ccb9fb5] | 24 | } |
---|
| 25 | |
---|
| 26 | member_types = { |
---|
| 27 | 'name': 'type', |
---|
| 28 | 'char_t*': 'T_STRING', |
---|
| 29 | 'uint_t': 'T_INT', |
---|
[c6388f4] | 30 | 'smpl_t': 'AUBIO_NPY_SMPL', |
---|
[ccb9fb5] | 31 | } |
---|
| 32 | |
---|
| 33 | pyfromtype_fn = { |
---|
| 34 | 'smpl_t': 'PyFloat_FromDouble', |
---|
| 35 | 'uint_t': 'PyLong_FromLong', # was: 'PyInt_FromLong', |
---|
| 36 | 'fvec_t*': 'PyAubio_CFvecToArray', |
---|
| 37 | 'fmat_t*': 'PyAubio_CFmatToArray', |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | pytoaubio_fn = { |
---|
| 41 | 'fvec_t*': 'PyAubio_ArrayToCFvec', |
---|
[92a8800] | 42 | 'cvec_t*': 'PyAubio_PyCvecToCCvec', |
---|
[a7f398d] | 43 | #'fmat_t*': 'PyAubio_ArrayToCFmat', |
---|
[ccb9fb5] | 44 | } |
---|
| 45 | |
---|
| 46 | newfromtype_fn = { |
---|
[21e8408] | 47 | 'fvec_t*': 'new_py_fvec', |
---|
| 48 | 'fmat_t*': 'new_py_fmat', |
---|
| 49 | 'cvec_t*': 'new_py_cvec', |
---|
[ccb9fb5] | 50 | } |
---|
| 51 | |
---|
| 52 | delfromtype_fn = { |
---|
[21e8408] | 53 | 'fvec_t*': 'Py_DECREF', |
---|
| 54 | 'fmat_t*': 'Py_DECREF', |
---|
| 55 | 'cvec_t*': 'Py_DECREF', |
---|
[ccb9fb5] | 56 | } |
---|
| 57 | |
---|
| 58 | param_init = { |
---|
| 59 | 'char_t*': 'NULL', |
---|
| 60 | 'uint_t': '0', |
---|
| 61 | 'sint_t': 0, |
---|
| 62 | 'smpl_t': 0., |
---|
| 63 | 'lsmp_t': 0., |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | pyargparse_chars = { |
---|
[fbcee4f] | 67 | 'smpl_t': 'f', # if not usedouble else 'd', |
---|
[ccb9fb5] | 68 | 'uint_t': 'I', |
---|
| 69 | 'sint_t': 'I', |
---|
| 70 | 'char_t*': 's', |
---|
| 71 | 'fmat_t*': 'O', |
---|
| 72 | 'fvec_t*': 'O', |
---|
[a7f398d] | 73 | 'cvec_t*': 'O', |
---|
[ccb9fb5] | 74 | } |
---|
| 75 | |
---|
| 76 | objoutsize = { |
---|
| 77 | 'onset': '1', |
---|
| 78 | 'pitch': '1', |
---|
[8470144] | 79 | 'notes': '3', |
---|
[ccb9fb5] | 80 | 'wavetable': 'self->hop_size', |
---|
| 81 | 'sampler': 'self->hop_size', |
---|
| 82 | 'mfcc': 'self->n_coeffs', |
---|
| 83 | 'specdesc': '1', |
---|
[de63017] | 84 | 'tempo': '1', |
---|
[ccb9fb5] | 85 | 'filterbank': 'self->n_filters', |
---|
[73e068d] | 86 | 'tss': 'self->buf_size', |
---|
[60583a3] | 87 | 'dct': 'self->size', |
---|
[f633b90] | 88 | 'constantq': 'aubio_constantq_get_numbins (self->o)', |
---|
[ccb9fb5] | 89 | } |
---|
| 90 | |
---|
[bf54364] | 91 | objinputsize = { |
---|
| 92 | 'mfcc': 'self->buf_size / 2 + 1', |
---|
| 93 | 'notes': 'self->hop_size', |
---|
| 94 | 'onset': 'self->hop_size', |
---|
| 95 | 'pitch': 'self->hop_size', |
---|
| 96 | 'sampler': 'self->hop_size', |
---|
| 97 | 'specdesc': 'self->buf_size / 2 + 1', |
---|
| 98 | 'tempo': 'self->hop_size', |
---|
| 99 | 'wavetable': 'self->hop_size', |
---|
[73e068d] | 100 | 'tss': 'self->buf_size / 2 + 1', |
---|
[bf54364] | 101 | } |
---|
| 102 | |
---|
[ccb9fb5] | 103 | def get_name(proto): |
---|
| 104 | name = proto.replace(' *', '* ').split()[1].split('(')[0] |
---|
| 105 | name = name.replace('*','') |
---|
| 106 | if name == '': raise ValueError(proto + "gave empty name") |
---|
| 107 | return name |
---|
| 108 | |
---|
| 109 | def get_return_type(proto): |
---|
| 110 | import re |
---|
| 111 | paramregex = re.compile('(\w+ ?\*?).*') |
---|
| 112 | outputs = paramregex.findall(proto) |
---|
| 113 | assert len(outputs) == 1 |
---|
| 114 | return outputs[0].replace(' ', '') |
---|
| 115 | |
---|
| 116 | def split_type(arg): |
---|
| 117 | """ arg = 'foo *name' |
---|
| 118 | return ['foo*', 'name'] """ |
---|
| 119 | l = arg.split() |
---|
[a7f398d] | 120 | type_arg = {} #'type': l[0], 'name': l[1]} |
---|
| 121 | type_arg['type'] = " ".join(l[:-1]) |
---|
| 122 | type_arg['name'] = l[-1] |
---|
| 123 | # fix up type / name |
---|
| 124 | if type_arg['name'].startswith('*'): |
---|
| 125 | # ['foo', '*name'] -> ['foo*', 'name'] |
---|
| 126 | type_arg['type'] += '*' |
---|
| 127 | type_arg['name'] = type_arg['name'][1:] |
---|
| 128 | if type_arg['type'].endswith(' *'): |
---|
| 129 | # ['foo *', 'name'] -> ['foo*', 'name'] |
---|
| 130 | type_arg['type'] = type_arg['type'].replace(' *','*') |
---|
| 131 | if type_arg['type'].startswith('const '): |
---|
| 132 | # ['foo *', 'name'] -> ['foo*', 'name'] |
---|
| 133 | type_arg['type'] = type_arg['type'].replace('const ','') |
---|
[ccb9fb5] | 134 | return type_arg |
---|
| 135 | |
---|
| 136 | def get_params(proto): |
---|
| 137 | """ get the list of parameters from a function prototype |
---|
| 138 | example: proto = "int main (int argc, char ** argv)" |
---|
| 139 | returns: ['int argc', 'char ** argv'] |
---|
| 140 | """ |
---|
| 141 | import re |
---|
[a7f398d] | 142 | paramregex = re.compile('.*\((.*)\);') |
---|
| 143 | a = paramregex.findall(proto)[0].split(', ') |
---|
| 144 | #a = [i.replace('const ', '') for i in a] |
---|
| 145 | return a |
---|
| 146 | |
---|
| 147 | def get_input_params(proto): |
---|
| 148 | a = get_params(proto) |
---|
| 149 | return [i.replace('const ', '') for i in a if (i.startswith('const ') or i.startswith('uint_t ') or i.startswith('smpl_t '))] |
---|
| 150 | |
---|
| 151 | def get_output_params(proto): |
---|
| 152 | a = get_params(proto) |
---|
| 153 | return [i for i in a if not i.startswith('const ')][1:] |
---|
[ccb9fb5] | 154 | |
---|
| 155 | def get_params_types_names(proto): |
---|
| 156 | """ get the list of parameters from a function prototype |
---|
| 157 | example: proto = "int main (int argc, char ** argv)" |
---|
| 158 | returns: [['int', 'argc'], ['char **','argv']] |
---|
| 159 | """ |
---|
[a7f398d] | 160 | a = list(map(split_type, get_params(proto))) |
---|
| 161 | #print proto, a |
---|
| 162 | #import sys; sys.exit(1) |
---|
| 163 | return a |
---|
[ccb9fb5] | 164 | |
---|
| 165 | class MappedObject(object): |
---|
| 166 | |
---|
[fbcee4f] | 167 | def __init__(self, prototypes, usedouble = False): |
---|
| 168 | if usedouble: |
---|
| 169 | pyargparse_chars['smpl_t'] = 'd' |
---|
[ccb9fb5] | 170 | self.prototypes = prototypes |
---|
| 171 | |
---|
| 172 | self.shortname = prototypes['shortname'] |
---|
| 173 | self.longname = prototypes['longname'] |
---|
| 174 | self.new_proto = prototypes['new'][0] |
---|
| 175 | self.del_proto = prototypes['del'][0] |
---|
| 176 | self.do_proto = prototypes['do'][0] |
---|
| 177 | self.input_params = get_params_types_names(self.new_proto) |
---|
[a7f398d] | 178 | self.input_params_list = "; ".join(get_input_params(self.new_proto)) |
---|
[ccb9fb5] | 179 | self.outputs = get_params_types_names(self.do_proto)[2:] |
---|
[a7f398d] | 180 | self.do_inputs = [get_params_types_names(self.do_proto)[1]] |
---|
| 181 | self.do_outputs = get_params_types_names(self.do_proto)[2:] |
---|
[21e8408] | 182 | struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs] |
---|
[a243d10] | 183 | if len(self.prototypes['rdo']): |
---|
| 184 | rdo_outputs = get_params_types_names(prototypes['rdo'][0])[2:] |
---|
| 185 | struct_output_str += ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in rdo_outputs] |
---|
| 186 | self.outputs += rdo_outputs |
---|
[21e8408] | 187 | self.struct_outputs = ";\n ".join(struct_output_str) |
---|
[a7f398d] | 188 | |
---|
[950a80c] | 189 | #print ("input_params: ", map(split_type, get_input_params(self.do_proto))) |
---|
| 190 | #print ("output_params", map(split_type, get_output_params(self.do_proto))) |
---|
[ccb9fb5] | 191 | |
---|
| 192 | def gen_code(self): |
---|
| 193 | out = "" |
---|
[ce79a9e] | 194 | try: |
---|
| 195 | out += self.gen_struct() |
---|
| 196 | out += self.gen_doc() |
---|
| 197 | out += self.gen_new() |
---|
| 198 | out += self.gen_init() |
---|
| 199 | out += self.gen_del() |
---|
| 200 | out += self.gen_do() |
---|
[c96e6c0] | 201 | if len(self.prototypes['rdo']): |
---|
| 202 | self.do_proto = self.prototypes['rdo'][0] |
---|
| 203 | self.do_inputs = [get_params_types_names(self.do_proto)[1]] |
---|
| 204 | self.do_outputs = get_params_types_names(self.do_proto)[2:] |
---|
| 205 | out += self.gen_do(method='rdo') |
---|
[ce79a9e] | 206 | out += self.gen_memberdef() |
---|
| 207 | out += self.gen_set() |
---|
| 208 | out += self.gen_get() |
---|
| 209 | out += self.gen_methodef() |
---|
| 210 | out += self.gen_typeobject() |
---|
| 211 | except Exception as e: |
---|
| 212 | print ("Failed generating code for", self.shortname) |
---|
| 213 | raise |
---|
[ccb9fb5] | 214 | return out |
---|
| 215 | |
---|
| 216 | def gen_struct(self): |
---|
| 217 | out = """ |
---|
| 218 | // {shortname} structure |
---|
| 219 | typedef struct{{ |
---|
| 220 | PyObject_HEAD |
---|
| 221 | // pointer to aubio object |
---|
| 222 | {longname} *o; |
---|
| 223 | // input parameters |
---|
| 224 | {input_params_list}; |
---|
[a7f398d] | 225 | // do input vectors |
---|
| 226 | {do_inputs_list}; |
---|
[ccb9fb5] | 227 | // output results |
---|
[21e8408] | 228 | {struct_outputs}; |
---|
[ccb9fb5] | 229 | }} Py_{shortname}; |
---|
| 230 | """ |
---|
[569b363] | 231 | # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname} |
---|
| 232 | do_inputs_list = "; ".join(get_input_params(self.do_proto)).replace('fvec_t *','fvec_t').replace('fmat_t *', 'fmat_t').replace('cvec_t *', 'cvec_t') |
---|
| 233 | return out.format(do_inputs_list = do_inputs_list, **self.__dict__) |
---|
[ccb9fb5] | 234 | |
---|
| 235 | def gen_doc(self): |
---|
| 236 | out = """ |
---|
| 237 | // TODO: add documentation |
---|
| 238 | static char Py_{shortname}_doc[] = \"undefined\"; |
---|
[a7f398d] | 239 | """ |
---|
[ccb9fb5] | 240 | return out.format(**self.__dict__) |
---|
| 241 | |
---|
| 242 | def gen_new(self): |
---|
| 243 | out = """ |
---|
| 244 | // new {shortname} |
---|
| 245 | static PyObject * |
---|
| 246 | Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
| 247 | {{ |
---|
| 248 | Py_{shortname} *self; |
---|
| 249 | """.format(**self.__dict__) |
---|
| 250 | params = self.input_params |
---|
| 251 | for p in params: |
---|
| 252 | out += """ |
---|
| 253 | {type} {name} = {defval};""".format(defval = param_init[p['type']], **p) |
---|
| 254 | plist = ", ".join(["\"%s\"" % p['name'] for p in params]) |
---|
| 255 | out += """ |
---|
| 256 | static char *kwlist[] = {{ {plist}, NULL }};""".format(plist = plist) |
---|
| 257 | argchars = "".join([pyargparse_chars[p['type']] for p in params]) |
---|
| 258 | arglist = ", ".join(["&%s" % p['name'] for p in params]) |
---|
| 259 | out += """ |
---|
| 260 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|{argchars}", kwlist, |
---|
| 261 | {arglist})) {{ |
---|
| 262 | return NULL; |
---|
| 263 | }} |
---|
| 264 | """.format(argchars = argchars, arglist = arglist) |
---|
| 265 | out += """ |
---|
| 266 | self = (Py_{shortname} *) pytype->tp_alloc (pytype, 0); |
---|
| 267 | if (self == NULL) {{ |
---|
| 268 | return NULL; |
---|
| 269 | }} |
---|
| 270 | """.format(**self.__dict__) |
---|
| 271 | params = self.input_params |
---|
| 272 | for p in params: |
---|
[a7f398d] | 273 | out += self.check_valid(p) |
---|
[ccb9fb5] | 274 | out += """ |
---|
| 275 | return (PyObject *)self; |
---|
| 276 | } |
---|
| 277 | """ |
---|
| 278 | return out |
---|
| 279 | |
---|
| 280 | def check_valid(self, p): |
---|
| 281 | if p['type'] == 'uint_t': |
---|
| 282 | return self.check_valid_uint(p) |
---|
| 283 | if p['type'] == 'char_t*': |
---|
| 284 | return self.check_valid_char(p) |
---|
| 285 | else: |
---|
| 286 | print ("ERROR, no idea how to check %s for validity" % p['type']) |
---|
| 287 | |
---|
| 288 | def check_valid_uint(self, p): |
---|
| 289 | name = p['name'] |
---|
| 290 | return """ |
---|
| 291 | self->{name} = {defval}; |
---|
| 292 | if ((sint_t){name} > 0) {{ |
---|
| 293 | self->{name} = {name}; |
---|
| 294 | }} else if ((sint_t){name} < 0) {{ |
---|
| 295 | PyErr_SetString (PyExc_ValueError, "can not use negative value for {name}"); |
---|
| 296 | return NULL; |
---|
| 297 | }} |
---|
| 298 | """.format(defval = aubiodefvalue[name], name = name) |
---|
| 299 | |
---|
| 300 | def check_valid_char(self, p): |
---|
| 301 | name = p['name'] |
---|
| 302 | return """ |
---|
| 303 | self->{name} = {defval}; |
---|
| 304 | if ({name} != NULL) {{ |
---|
| 305 | self->{name} = {name}; |
---|
| 306 | }} |
---|
| 307 | """.format(defval = aubiodefvalue[name], name = name) |
---|
| 308 | |
---|
| 309 | def gen_init(self): |
---|
| 310 | out = """ |
---|
| 311 | // init {shortname} |
---|
| 312 | static int |
---|
| 313 | Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds) |
---|
| 314 | {{ |
---|
| 315 | """.format(**self.__dict__) |
---|
| 316 | new_name = get_name(self.new_proto) |
---|
| 317 | new_params = ", ".join(["self->%s" % s['name'] for s in self.input_params]) |
---|
| 318 | out += """ |
---|
| 319 | self->o = {new_name}({new_params}); |
---|
| 320 | """.format(new_name = new_name, new_params = new_params) |
---|
| 321 | paramchars = "%s" |
---|
| 322 | paramvals = "self->method" |
---|
| 323 | out += """ |
---|
| 324 | // return -1 and set error string on failure |
---|
| 325 | if (self->o == NULL) {{ |
---|
[6a72ff0] | 326 | PyErr_Format (PyExc_RuntimeError, "failed creating {shortname}"); |
---|
[ccb9fb5] | 327 | return -1; |
---|
| 328 | }} |
---|
| 329 | """.format(paramchars = paramchars, paramvals = paramvals, **self.__dict__) |
---|
| 330 | output_create = "" |
---|
| 331 | for o in self.outputs: |
---|
| 332 | output_create += """ |
---|
| 333 | self->{name} = {create_fn}({output_size});""".format(name = o['name'], create_fn = newfromtype_fn[o['type']], output_size = objoutsize[self.shortname]) |
---|
| 334 | out += """ |
---|
| 335 | // TODO get internal params after actual object creation? |
---|
| 336 | """ |
---|
| 337 | out += """ |
---|
| 338 | // create outputs{output_create} |
---|
| 339 | """.format(output_create = output_create) |
---|
| 340 | out += """ |
---|
| 341 | return 0; |
---|
| 342 | } |
---|
| 343 | """ |
---|
| 344 | return out |
---|
| 345 | |
---|
| 346 | def gen_memberdef(self): |
---|
| 347 | out = """ |
---|
| 348 | static PyMemberDef Py_{shortname}_members[] = {{ |
---|
| 349 | """.format(**self.__dict__) |
---|
| 350 | for p in get_params_types_names(self.new_proto): |
---|
| 351 | tmp = " {{\"{name}\", {ttype}, offsetof (Py_{shortname}, {name}), READONLY, \"TODO documentation\"}},\n" |
---|
| 352 | pytype = member_types[p['type']] |
---|
| 353 | out += tmp.format(name = p['name'], ttype = pytype, shortname = self.shortname) |
---|
| 354 | out += """ {NULL}, // sentinel |
---|
| 355 | }; |
---|
| 356 | """ |
---|
| 357 | return out |
---|
| 358 | |
---|
| 359 | def gen_del(self): |
---|
| 360 | out = """ |
---|
| 361 | // del {shortname} |
---|
| 362 | static void |
---|
| 363 | Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) |
---|
| 364 | {{""".format(**self.__dict__) |
---|
[a7f398d] | 365 | for input_param in self.do_inputs: |
---|
[569b363] | 366 | if input_param['type'] == 'fmat_t *': |
---|
| 367 | out += """ |
---|
[6a72ff0] | 368 | free(self->{0[name]}.data);""".format(input_param) |
---|
[ccb9fb5] | 369 | for o in self.outputs: |
---|
| 370 | name = o['name'] |
---|
| 371 | del_out = delfromtype_fn[o['type']] |
---|
| 372 | out += """ |
---|
[6a72ff0] | 373 | if (self->{name}) {{ |
---|
| 374 | {del_out}(self->{name}); |
---|
| 375 | }}""".format(del_out = del_out, name = name) |
---|
[ccb9fb5] | 376 | del_fn = get_name(self.del_proto) |
---|
| 377 | out += """ |
---|
[6a72ff0] | 378 | if (self->o) {{ |
---|
| 379 | {del_fn}(self->o); |
---|
| 380 | }} |
---|
| 381 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[ccb9fb5] | 382 | }} |
---|
| 383 | """.format(del_fn = del_fn) |
---|
| 384 | return out |
---|
| 385 | |
---|
[c96e6c0] | 386 | def gen_do(self, method = 'do'): |
---|
[ccb9fb5] | 387 | out = """ |
---|
| 388 | // do {shortname} |
---|
| 389 | static PyObject* |
---|
[c96e6c0] | 390 | Pyaubio_{shortname}_{method} (Py_{shortname} * self, PyObject * args) |
---|
[a2ed169] | 391 | {{""".format(method = method, **self.__dict__) |
---|
[a7f398d] | 392 | input_params = self.do_inputs |
---|
| 393 | output_params = self.do_outputs |
---|
| 394 | #print input_params |
---|
| 395 | #print output_params |
---|
[73e068d] | 396 | out += """ |
---|
| 397 | PyObject *outputs;""" |
---|
[a7f398d] | 398 | for input_param in input_params: |
---|
| 399 | out += """ |
---|
[bbc62b5] | 400 | PyObject *py_{0};""".format(input_param['name']) |
---|
[a7f398d] | 401 | refs = ", ".join(["&py_%s" % p['name'] for p in input_params]) |
---|
| 402 | pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params]) |
---|
| 403 | out += """ |
---|
| 404 | if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{ |
---|
[ccb9fb5] | 405 | return NULL; |
---|
[a7f398d] | 406 | }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__) |
---|
[21e8408] | 407 | for input_param in input_params: |
---|
[a7f398d] | 408 | out += """ |
---|
[21e8408] | 409 | |
---|
[569b363] | 410 | if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{ |
---|
[ccb9fb5] | 411 | return NULL; |
---|
[a7f398d] | 412 | }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']]) |
---|
[bf54364] | 413 | if self.shortname in objinputsize: |
---|
| 414 | out += """ |
---|
| 415 | |
---|
| 416 | if (self->{0[name]}.length != {expected_size}) {{ |
---|
| 417 | PyErr_Format (PyExc_ValueError, |
---|
| 418 | "input size of {shortname} should be %d, not %d", |
---|
| 419 | {expected_size}, self->{0[name]}.length); |
---|
| 420 | return NULL; |
---|
| 421 | }}""".format(input_param, expected_size = objinputsize[self.shortname], **self.__dict__) |
---|
| 422 | else: |
---|
| 423 | out += """ |
---|
[21e8408] | 424 | |
---|
| 425 | // TODO: check input sizes""" |
---|
| 426 | for output_param in output_params: |
---|
| 427 | out += """ |
---|
| 428 | |
---|
| 429 | Py_INCREF(self->{0[name]}); |
---|
| 430 | if (!{pytoaubio}(self->{0[name]}, &(self->c_{0[name]}))) {{ |
---|
| 431 | return NULL; |
---|
| 432 | }}""".format(output_param, pytoaubio = pytoaubio_fn[output_param['type']]) |
---|
[a7f398d] | 433 | do_fn = get_name(self.do_proto) |
---|
[569b363] | 434 | inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params]) |
---|
[21e8408] | 435 | c_outputs = ", ".join(["&(self->c_%s)" % p['name'] for p in self.do_outputs]) |
---|
[a7f398d] | 436 | outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs]) |
---|
| 437 | out += """ |
---|
[ccb9fb5] | 438 | |
---|
[21e8408] | 439 | {do_fn}(self->o, {inputs}, {c_outputs}); |
---|
[73e068d] | 440 | """.format( |
---|
| 441 | do_fn = do_fn, |
---|
| 442 | inputs = inputs, c_outputs = c_outputs, |
---|
| 443 | ) |
---|
| 444 | if len(self.do_outputs) > 1: |
---|
| 445 | out += """ |
---|
[31c6010] | 446 | outputs = PyTuple_New({:d});""".format(len(self.do_outputs)) |
---|
[73e068d] | 447 | for i, p in enumerate(self.do_outputs): |
---|
| 448 | out += """ |
---|
| 449 | PyTuple_SetItem( outputs, {i}, self->{p[name]});""".format(i = i, p = p) |
---|
| 450 | else: |
---|
| 451 | out += """ |
---|
| 452 | outputs = self->{p[name]};""".format(p = self.do_outputs[0]) |
---|
| 453 | out += """ |
---|
[ccb9fb5] | 454 | |
---|
[73e068d] | 455 | return outputs; |
---|
[ccb9fb5] | 456 | }} |
---|
[a7f398d] | 457 | """.format( |
---|
[73e068d] | 458 | outputs = outputs, |
---|
[a7f398d] | 459 | ) |
---|
| 460 | return out |
---|
[ccb9fb5] | 461 | |
---|
| 462 | def gen_set(self): |
---|
| 463 | out = """ |
---|
| 464 | // {shortname} setters |
---|
| 465 | """.format(**self.__dict__) |
---|
| 466 | for set_param in self.prototypes['set']: |
---|
| 467 | params = get_params_types_names(set_param)[1] |
---|
| 468 | paramtype = params['type'] |
---|
| 469 | method_name = get_name(set_param) |
---|
| 470 | param = method_name.split('aubio_'+self.shortname+'_set_')[-1] |
---|
| 471 | pyparamtype = pyargparse_chars[paramtype] |
---|
| 472 | out += """ |
---|
| 473 | static PyObject * |
---|
| 474 | Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args) |
---|
| 475 | {{ |
---|
| 476 | uint_t err = 0; |
---|
| 477 | {paramtype} {param}; |
---|
| 478 | |
---|
| 479 | if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{ |
---|
| 480 | return NULL; |
---|
| 481 | }} |
---|
| 482 | err = aubio_{shortname}_set_{param} (self->o, {param}); |
---|
| 483 | |
---|
| 484 | if (err > 0) {{ |
---|
| 485 | PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}"); |
---|
| 486 | return NULL; |
---|
| 487 | }} |
---|
| 488 | Py_RETURN_NONE; |
---|
| 489 | }} |
---|
| 490 | """.format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__) |
---|
| 491 | return out |
---|
| 492 | |
---|
| 493 | def gen_get(self): |
---|
| 494 | out = """ |
---|
| 495 | // {shortname} getters |
---|
| 496 | """.format(**self.__dict__) |
---|
| 497 | for method in self.prototypes['get']: |
---|
| 498 | params = get_params_types_names(method) |
---|
| 499 | method_name = get_name(method) |
---|
| 500 | assert len(params) == 1, \ |
---|
| 501 | "get method has more than one parameter %s" % params |
---|
| 502 | param = method_name.split('aubio_'+self.shortname+'_get_')[-1] |
---|
| 503 | paramtype = get_return_type(method) |
---|
| 504 | ptypeconv = pyfromtype_fn[paramtype] |
---|
| 505 | out += """ |
---|
| 506 | static PyObject * |
---|
| 507 | Pyaubio_{shortname}_get_{param} (Py_{shortname} *self, PyObject *unused) |
---|
| 508 | {{ |
---|
| 509 | {ptype} {param} = aubio_{shortname}_get_{param} (self->o); |
---|
| 510 | return (PyObject *){ptypeconv} ({param}); |
---|
| 511 | }} |
---|
| 512 | """.format(param = param, ptype = paramtype, ptypeconv = ptypeconv, |
---|
| 513 | **self.__dict__) |
---|
| 514 | return out |
---|
| 515 | |
---|
| 516 | def gen_methodef(self): |
---|
| 517 | out = """ |
---|
| 518 | static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__) |
---|
| 519 | for m in self.prototypes['set']: |
---|
| 520 | name = get_name(m) |
---|
| 521 | shortname = name.replace('aubio_%s_' % self.shortname, '') |
---|
| 522 | out += """ |
---|
| 523 | {{"{shortname}", (PyCFunction) Py{name}, |
---|
| 524 | METH_VARARGS, ""}},""".format(name = name, shortname = shortname) |
---|
| 525 | for m in self.prototypes['get']: |
---|
| 526 | name = get_name(m) |
---|
| 527 | shortname = name.replace('aubio_%s_' % self.shortname, '') |
---|
| 528 | out += """ |
---|
| 529 | {{"{shortname}", (PyCFunction) Py{name}, |
---|
| 530 | METH_NOARGS, ""}},""".format(name = name, shortname = shortname) |
---|
[c96e6c0] | 531 | for m in self.prototypes['rdo']: |
---|
| 532 | name = get_name(m) |
---|
| 533 | shortname = name.replace('aubio_%s_' % self.shortname, '') |
---|
| 534 | out += """ |
---|
| 535 | {{"{shortname}", (PyCFunction) Py{name}, |
---|
| 536 | METH_VARARGS, ""}},""".format(name = name, shortname = shortname) |
---|
[ccb9fb5] | 537 | out += """ |
---|
| 538 | {NULL} /* sentinel */ |
---|
| 539 | }; |
---|
| 540 | """ |
---|
| 541 | return out |
---|
| 542 | |
---|
| 543 | def gen_typeobject(self): |
---|
| 544 | return """ |
---|
| 545 | PyTypeObject Py_{shortname}Type = {{ |
---|
| 546 | //PyObject_HEAD_INIT (NULL) |
---|
| 547 | //0, |
---|
| 548 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 549 | "aubio.{shortname}", |
---|
| 550 | sizeof (Py_{shortname}), |
---|
| 551 | 0, |
---|
| 552 | (destructor) Py_{shortname}_del, |
---|
| 553 | 0, |
---|
| 554 | 0, |
---|
| 555 | 0, |
---|
| 556 | 0, |
---|
| 557 | 0, |
---|
| 558 | 0, |
---|
| 559 | 0, |
---|
| 560 | 0, |
---|
| 561 | 0, |
---|
[c96e6c0] | 562 | (ternaryfunc)Pyaubio_{shortname}_do, |
---|
[ccb9fb5] | 563 | 0, |
---|
| 564 | 0, |
---|
| 565 | 0, |
---|
| 566 | 0, |
---|
| 567 | Py_TPFLAGS_DEFAULT, |
---|
| 568 | Py_{shortname}_doc, |
---|
| 569 | 0, |
---|
| 570 | 0, |
---|
| 571 | 0, |
---|
| 572 | 0, |
---|
| 573 | 0, |
---|
| 574 | 0, |
---|
| 575 | Py_{shortname}_methods, |
---|
| 576 | Py_{shortname}_members, |
---|
| 577 | 0, |
---|
| 578 | 0, |
---|
| 579 | 0, |
---|
| 580 | 0, |
---|
| 581 | 0, |
---|
| 582 | 0, |
---|
| 583 | (initproc) Py_{shortname}_init, |
---|
| 584 | 0, |
---|
| 585 | Py_{shortname}_new, |
---|
[0e70ef9] | 586 | 0, |
---|
| 587 | 0, |
---|
| 588 | 0, |
---|
| 589 | 0, |
---|
| 590 | 0, |
---|
| 591 | 0, |
---|
| 592 | 0, |
---|
| 593 | 0, |
---|
| 594 | 0, |
---|
[ccb9fb5] | 595 | }}; |
---|
| 596 | """.format(**self.__dict__) |
---|