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