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