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