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