[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 = "" |
---|
| 186 | out += self.gen_struct() |
---|
| 187 | out += self.gen_doc() |
---|
| 188 | out += self.gen_new() |
---|
| 189 | out += self.gen_init() |
---|
| 190 | out += self.gen_del() |
---|
| 191 | out += self.gen_do() |
---|
| 192 | out += self.gen_memberdef() |
---|
| 193 | out += self.gen_set() |
---|
| 194 | out += self.gen_get() |
---|
| 195 | out += self.gen_methodef() |
---|
| 196 | out += self.gen_typeobject() |
---|
| 197 | return out |
---|
| 198 | |
---|
| 199 | def gen_struct(self): |
---|
| 200 | out = """ |
---|
| 201 | // {shortname} structure |
---|
| 202 | typedef struct{{ |
---|
| 203 | PyObject_HEAD |
---|
| 204 | // pointer to aubio object |
---|
| 205 | {longname} *o; |
---|
| 206 | // input parameters |
---|
| 207 | {input_params_list}; |
---|
[a7f398d] | 208 | // do input vectors |
---|
| 209 | {do_inputs_list}; |
---|
[ccb9fb5] | 210 | // output results |
---|
[21e8408] | 211 | {struct_outputs}; |
---|
[ccb9fb5] | 212 | }} Py_{shortname}; |
---|
| 213 | """ |
---|
[569b363] | 214 | # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname} |
---|
| 215 | 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') |
---|
| 216 | return out.format(do_inputs_list = do_inputs_list, **self.__dict__) |
---|
[ccb9fb5] | 217 | |
---|
| 218 | def gen_doc(self): |
---|
| 219 | out = """ |
---|
| 220 | // TODO: add documentation |
---|
| 221 | static char Py_{shortname}_doc[] = \"undefined\"; |
---|
[a7f398d] | 222 | """ |
---|
[ccb9fb5] | 223 | return out.format(**self.__dict__) |
---|
| 224 | |
---|
| 225 | def gen_new(self): |
---|
| 226 | out = """ |
---|
| 227 | // new {shortname} |
---|
| 228 | static PyObject * |
---|
| 229 | Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) |
---|
| 230 | {{ |
---|
| 231 | Py_{shortname} *self; |
---|
| 232 | """.format(**self.__dict__) |
---|
| 233 | params = self.input_params |
---|
| 234 | for p in params: |
---|
| 235 | out += """ |
---|
| 236 | {type} {name} = {defval};""".format(defval = param_init[p['type']], **p) |
---|
| 237 | plist = ", ".join(["\"%s\"" % p['name'] for p in params]) |
---|
| 238 | out += """ |
---|
| 239 | static char *kwlist[] = {{ {plist}, NULL }};""".format(plist = plist) |
---|
| 240 | argchars = "".join([pyargparse_chars[p['type']] for p in params]) |
---|
| 241 | arglist = ", ".join(["&%s" % p['name'] for p in params]) |
---|
| 242 | out += """ |
---|
| 243 | if (!PyArg_ParseTupleAndKeywords (args, kwds, "|{argchars}", kwlist, |
---|
| 244 | {arglist})) {{ |
---|
| 245 | return NULL; |
---|
| 246 | }} |
---|
| 247 | """.format(argchars = argchars, arglist = arglist) |
---|
| 248 | out += """ |
---|
| 249 | self = (Py_{shortname} *) pytype->tp_alloc (pytype, 0); |
---|
| 250 | if (self == NULL) {{ |
---|
| 251 | return NULL; |
---|
| 252 | }} |
---|
| 253 | """.format(**self.__dict__) |
---|
| 254 | params = self.input_params |
---|
| 255 | for p in params: |
---|
[a7f398d] | 256 | out += self.check_valid(p) |
---|
[ccb9fb5] | 257 | out += """ |
---|
| 258 | return (PyObject *)self; |
---|
| 259 | } |
---|
| 260 | """ |
---|
| 261 | return out |
---|
| 262 | |
---|
| 263 | def check_valid(self, p): |
---|
| 264 | if p['type'] == 'uint_t': |
---|
| 265 | return self.check_valid_uint(p) |
---|
| 266 | if p['type'] == 'char_t*': |
---|
| 267 | return self.check_valid_char(p) |
---|
| 268 | else: |
---|
| 269 | print ("ERROR, no idea how to check %s for validity" % p['type']) |
---|
| 270 | |
---|
| 271 | def check_valid_uint(self, p): |
---|
| 272 | name = p['name'] |
---|
| 273 | return """ |
---|
| 274 | self->{name} = {defval}; |
---|
| 275 | if ((sint_t){name} > 0) {{ |
---|
| 276 | self->{name} = {name}; |
---|
| 277 | }} else if ((sint_t){name} < 0) {{ |
---|
| 278 | PyErr_SetString (PyExc_ValueError, "can not use negative value for {name}"); |
---|
| 279 | return NULL; |
---|
| 280 | }} |
---|
| 281 | """.format(defval = aubiodefvalue[name], name = name) |
---|
| 282 | |
---|
| 283 | def check_valid_char(self, p): |
---|
| 284 | name = p['name'] |
---|
| 285 | return """ |
---|
| 286 | self->{name} = {defval}; |
---|
| 287 | if ({name} != NULL) {{ |
---|
| 288 | self->{name} = {name}; |
---|
| 289 | }} |
---|
| 290 | """.format(defval = aubiodefvalue[name], name = name) |
---|
| 291 | |
---|
| 292 | def gen_init(self): |
---|
| 293 | out = """ |
---|
| 294 | // init {shortname} |
---|
| 295 | static int |
---|
| 296 | Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds) |
---|
| 297 | {{ |
---|
| 298 | """.format(**self.__dict__) |
---|
| 299 | new_name = get_name(self.new_proto) |
---|
| 300 | new_params = ", ".join(["self->%s" % s['name'] for s in self.input_params]) |
---|
| 301 | out += """ |
---|
| 302 | self->o = {new_name}({new_params}); |
---|
| 303 | """.format(new_name = new_name, new_params = new_params) |
---|
| 304 | paramchars = "%s" |
---|
| 305 | paramvals = "self->method" |
---|
| 306 | out += """ |
---|
| 307 | // return -1 and set error string on failure |
---|
| 308 | if (self->o == NULL) {{ |
---|
[6a72ff0] | 309 | PyErr_Format (PyExc_RuntimeError, "failed creating {shortname}"); |
---|
[ccb9fb5] | 310 | return -1; |
---|
| 311 | }} |
---|
| 312 | """.format(paramchars = paramchars, paramvals = paramvals, **self.__dict__) |
---|
| 313 | output_create = "" |
---|
| 314 | for o in self.outputs: |
---|
| 315 | output_create += """ |
---|
| 316 | self->{name} = {create_fn}({output_size});""".format(name = o['name'], create_fn = newfromtype_fn[o['type']], output_size = objoutsize[self.shortname]) |
---|
| 317 | out += """ |
---|
| 318 | // TODO get internal params after actual object creation? |
---|
| 319 | """ |
---|
| 320 | out += """ |
---|
| 321 | // create outputs{output_create} |
---|
| 322 | """.format(output_create = output_create) |
---|
| 323 | out += """ |
---|
| 324 | return 0; |
---|
| 325 | } |
---|
| 326 | """ |
---|
| 327 | return out |
---|
| 328 | |
---|
| 329 | def gen_memberdef(self): |
---|
| 330 | out = """ |
---|
| 331 | static PyMemberDef Py_{shortname}_members[] = {{ |
---|
| 332 | """.format(**self.__dict__) |
---|
| 333 | for p in get_params_types_names(self.new_proto): |
---|
| 334 | tmp = " {{\"{name}\", {ttype}, offsetof (Py_{shortname}, {name}), READONLY, \"TODO documentation\"}},\n" |
---|
| 335 | pytype = member_types[p['type']] |
---|
| 336 | out += tmp.format(name = p['name'], ttype = pytype, shortname = self.shortname) |
---|
| 337 | out += """ {NULL}, // sentinel |
---|
| 338 | }; |
---|
| 339 | """ |
---|
| 340 | return out |
---|
| 341 | |
---|
| 342 | def gen_del(self): |
---|
| 343 | out = """ |
---|
| 344 | // del {shortname} |
---|
| 345 | static void |
---|
| 346 | Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) |
---|
| 347 | {{""".format(**self.__dict__) |
---|
[a7f398d] | 348 | for input_param in self.do_inputs: |
---|
[569b363] | 349 | if input_param['type'] == 'fmat_t *': |
---|
| 350 | out += """ |
---|
[6a72ff0] | 351 | free(self->{0[name]}.data);""".format(input_param) |
---|
[ccb9fb5] | 352 | for o in self.outputs: |
---|
| 353 | name = o['name'] |
---|
| 354 | del_out = delfromtype_fn[o['type']] |
---|
| 355 | out += """ |
---|
[6a72ff0] | 356 | if (self->{name}) {{ |
---|
| 357 | {del_out}(self->{name}); |
---|
| 358 | }}""".format(del_out = del_out, name = name) |
---|
[ccb9fb5] | 359 | del_fn = get_name(self.del_proto) |
---|
| 360 | out += """ |
---|
[6a72ff0] | 361 | if (self->o) {{ |
---|
| 362 | {del_fn}(self->o); |
---|
| 363 | }} |
---|
| 364 | Py_TYPE(self)->tp_free((PyObject *) self); |
---|
[ccb9fb5] | 365 | }} |
---|
| 366 | """.format(del_fn = del_fn) |
---|
| 367 | return out |
---|
| 368 | |
---|
| 369 | def gen_do(self): |
---|
| 370 | out = """ |
---|
| 371 | // do {shortname} |
---|
| 372 | static PyObject* |
---|
| 373 | Py_{shortname}_do (Py_{shortname} * self, PyObject * args) |
---|
[a7f398d] | 374 | {{""".format(**self.__dict__) |
---|
| 375 | input_params = self.do_inputs |
---|
| 376 | output_params = self.do_outputs |
---|
| 377 | #print input_params |
---|
| 378 | #print output_params |
---|
[73e068d] | 379 | out += """ |
---|
| 380 | PyObject *outputs;""" |
---|
[a7f398d] | 381 | for input_param in input_params: |
---|
| 382 | out += """ |
---|
[bbc62b5] | 383 | PyObject *py_{0};""".format(input_param['name']) |
---|
[a7f398d] | 384 | refs = ", ".join(["&py_%s" % p['name'] for p in input_params]) |
---|
| 385 | pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params]) |
---|
| 386 | out += """ |
---|
| 387 | if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{ |
---|
[ccb9fb5] | 388 | return NULL; |
---|
[a7f398d] | 389 | }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__) |
---|
[21e8408] | 390 | for input_param in input_params: |
---|
[a7f398d] | 391 | out += """ |
---|
[21e8408] | 392 | |
---|
[569b363] | 393 | if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{ |
---|
[ccb9fb5] | 394 | return NULL; |
---|
[a7f398d] | 395 | }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']]) |
---|
[bf54364] | 396 | if self.shortname in objinputsize: |
---|
| 397 | out += """ |
---|
| 398 | |
---|
| 399 | if (self->{0[name]}.length != {expected_size}) {{ |
---|
| 400 | PyErr_Format (PyExc_ValueError, |
---|
| 401 | "input size of {shortname} should be %d, not %d", |
---|
| 402 | {expected_size}, self->{0[name]}.length); |
---|
| 403 | return NULL; |
---|
| 404 | }}""".format(input_param, expected_size = objinputsize[self.shortname], **self.__dict__) |
---|
| 405 | else: |
---|
| 406 | out += """ |
---|
[21e8408] | 407 | |
---|
| 408 | // TODO: check input sizes""" |
---|
| 409 | for output_param in output_params: |
---|
| 410 | out += """ |
---|
| 411 | |
---|
| 412 | Py_INCREF(self->{0[name]}); |
---|
| 413 | if (!{pytoaubio}(self->{0[name]}, &(self->c_{0[name]}))) {{ |
---|
| 414 | return NULL; |
---|
| 415 | }}""".format(output_param, pytoaubio = pytoaubio_fn[output_param['type']]) |
---|
[a7f398d] | 416 | do_fn = get_name(self.do_proto) |
---|
[569b363] | 417 | inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params]) |
---|
[21e8408] | 418 | c_outputs = ", ".join(["&(self->c_%s)" % p['name'] for p in self.do_outputs]) |
---|
[a7f398d] | 419 | outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs]) |
---|
| 420 | out += """ |
---|
[ccb9fb5] | 421 | |
---|
[21e8408] | 422 | {do_fn}(self->o, {inputs}, {c_outputs}); |
---|
[73e068d] | 423 | """.format( |
---|
| 424 | do_fn = do_fn, |
---|
| 425 | inputs = inputs, c_outputs = c_outputs, |
---|
| 426 | ) |
---|
| 427 | if len(self.do_outputs) > 1: |
---|
| 428 | out += """ |
---|
[31c6010] | 429 | outputs = PyTuple_New({:d});""".format(len(self.do_outputs)) |
---|
[73e068d] | 430 | for i, p in enumerate(self.do_outputs): |
---|
| 431 | out += """ |
---|
| 432 | PyTuple_SetItem( outputs, {i}, self->{p[name]});""".format(i = i, p = p) |
---|
| 433 | else: |
---|
| 434 | out += """ |
---|
| 435 | outputs = self->{p[name]};""".format(p = self.do_outputs[0]) |
---|
| 436 | out += """ |
---|
[ccb9fb5] | 437 | |
---|
[73e068d] | 438 | return outputs; |
---|
[ccb9fb5] | 439 | }} |
---|
[a7f398d] | 440 | """.format( |
---|
[73e068d] | 441 | outputs = outputs, |
---|
[a7f398d] | 442 | ) |
---|
| 443 | return out |
---|
[ccb9fb5] | 444 | |
---|
| 445 | def gen_set(self): |
---|
| 446 | out = """ |
---|
| 447 | // {shortname} setters |
---|
| 448 | """.format(**self.__dict__) |
---|
| 449 | for set_param in self.prototypes['set']: |
---|
| 450 | params = get_params_types_names(set_param)[1] |
---|
| 451 | paramtype = params['type'] |
---|
| 452 | method_name = get_name(set_param) |
---|
| 453 | param = method_name.split('aubio_'+self.shortname+'_set_')[-1] |
---|
| 454 | pyparamtype = pyargparse_chars[paramtype] |
---|
| 455 | out += """ |
---|
| 456 | static PyObject * |
---|
| 457 | Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args) |
---|
| 458 | {{ |
---|
| 459 | uint_t err = 0; |
---|
| 460 | {paramtype} {param}; |
---|
| 461 | |
---|
| 462 | if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{ |
---|
| 463 | return NULL; |
---|
| 464 | }} |
---|
| 465 | err = aubio_{shortname}_set_{param} (self->o, {param}); |
---|
| 466 | |
---|
| 467 | if (err > 0) {{ |
---|
| 468 | PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}"); |
---|
| 469 | return NULL; |
---|
| 470 | }} |
---|
| 471 | Py_RETURN_NONE; |
---|
| 472 | }} |
---|
| 473 | """.format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__) |
---|
| 474 | return out |
---|
| 475 | |
---|
| 476 | def gen_get(self): |
---|
| 477 | out = """ |
---|
| 478 | // {shortname} getters |
---|
| 479 | """.format(**self.__dict__) |
---|
| 480 | for method in self.prototypes['get']: |
---|
| 481 | params = get_params_types_names(method) |
---|
| 482 | method_name = get_name(method) |
---|
| 483 | assert len(params) == 1, \ |
---|
| 484 | "get method has more than one parameter %s" % params |
---|
| 485 | param = method_name.split('aubio_'+self.shortname+'_get_')[-1] |
---|
| 486 | paramtype = get_return_type(method) |
---|
| 487 | ptypeconv = pyfromtype_fn[paramtype] |
---|
| 488 | out += """ |
---|
| 489 | static PyObject * |
---|
| 490 | Pyaubio_{shortname}_get_{param} (Py_{shortname} *self, PyObject *unused) |
---|
| 491 | {{ |
---|
| 492 | {ptype} {param} = aubio_{shortname}_get_{param} (self->o); |
---|
| 493 | return (PyObject *){ptypeconv} ({param}); |
---|
| 494 | }} |
---|
| 495 | """.format(param = param, ptype = paramtype, ptypeconv = ptypeconv, |
---|
| 496 | **self.__dict__) |
---|
| 497 | return out |
---|
| 498 | |
---|
| 499 | def gen_methodef(self): |
---|
| 500 | out = """ |
---|
| 501 | static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__) |
---|
| 502 | for m in self.prototypes['set']: |
---|
| 503 | name = get_name(m) |
---|
| 504 | shortname = name.replace('aubio_%s_' % self.shortname, '') |
---|
| 505 | out += """ |
---|
| 506 | {{"{shortname}", (PyCFunction) Py{name}, |
---|
| 507 | METH_VARARGS, ""}},""".format(name = name, shortname = shortname) |
---|
| 508 | for m in self.prototypes['get']: |
---|
| 509 | name = get_name(m) |
---|
| 510 | shortname = name.replace('aubio_%s_' % self.shortname, '') |
---|
| 511 | out += """ |
---|
| 512 | {{"{shortname}", (PyCFunction) Py{name}, |
---|
| 513 | METH_NOARGS, ""}},""".format(name = name, shortname = shortname) |
---|
| 514 | out += """ |
---|
| 515 | {NULL} /* sentinel */ |
---|
| 516 | }; |
---|
| 517 | """ |
---|
| 518 | return out |
---|
| 519 | |
---|
| 520 | def gen_typeobject(self): |
---|
| 521 | return """ |
---|
| 522 | PyTypeObject Py_{shortname}Type = {{ |
---|
| 523 | //PyObject_HEAD_INIT (NULL) |
---|
| 524 | //0, |
---|
| 525 | PyVarObject_HEAD_INIT (NULL, 0) |
---|
| 526 | "aubio.{shortname}", |
---|
| 527 | sizeof (Py_{shortname}), |
---|
| 528 | 0, |
---|
| 529 | (destructor) Py_{shortname}_del, |
---|
| 530 | 0, |
---|
| 531 | 0, |
---|
| 532 | 0, |
---|
| 533 | 0, |
---|
| 534 | 0, |
---|
| 535 | 0, |
---|
| 536 | 0, |
---|
| 537 | 0, |
---|
| 538 | 0, |
---|
| 539 | (ternaryfunc)Py_{shortname}_do, |
---|
| 540 | 0, |
---|
| 541 | 0, |
---|
| 542 | 0, |
---|
| 543 | 0, |
---|
| 544 | Py_TPFLAGS_DEFAULT, |
---|
| 545 | Py_{shortname}_doc, |
---|
| 546 | 0, |
---|
| 547 | 0, |
---|
| 548 | 0, |
---|
| 549 | 0, |
---|
| 550 | 0, |
---|
| 551 | 0, |
---|
| 552 | Py_{shortname}_methods, |
---|
| 553 | Py_{shortname}_members, |
---|
| 554 | 0, |
---|
| 555 | 0, |
---|
| 556 | 0, |
---|
| 557 | 0, |
---|
| 558 | 0, |
---|
| 559 | 0, |
---|
| 560 | (initproc) Py_{shortname}_init, |
---|
| 561 | 0, |
---|
| 562 | Py_{shortname}_new, |
---|
[0e70ef9] | 563 | 0, |
---|
| 564 | 0, |
---|
| 565 | 0, |
---|
| 566 | 0, |
---|
| 567 | 0, |
---|
| 568 | 0, |
---|
| 569 | 0, |
---|
| 570 | 0, |
---|
| 571 | 0, |
---|
[ccb9fb5] | 572 | }}; |
---|
| 573 | """.format(**self.__dict__) |
---|