Showing the changes to python/lib/gen_code.py (1 of 2 files). Show all
Unified · Split
| @@ -38,7 +38,7 @@ pyfromtype_fn = { · 37 unmodified lines | ||
| 38 | 38 | pytoaubio_fn = { |
| 39 | 39 | 'fvec_t*': 'PyAubio_ArrayToCFvec', |
| 40 | 40 | 'cvec_t*': 'PyAubio_ArrayToCCvec', |
| 41 | − 'fmat_t*': 'PyAubio_ArrayToCFmat', | |
| 41 | + #'fmat_t*': 'PyAubio_ArrayToCFmat', | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | pyfromaubio_fn = { |
| @@ -74,6 +74,7 @@ pyargparse_chars = { · 29 unmodified lines | ||
| 74 | 74 | 'char_t*': 's', |
| 75 | 75 | 'fmat_t*': 'O', |
| 76 | 76 | 'fvec_t*': 'O', |
| 77 | + 'cvec_t*': 'O', | |
| 77 | 78 | } |
| 78 | 79 | |
| 79 | 80 | objoutsize = { |
| @@ -83,8 +84,9 @@ objoutsize = { · 3 unmodified lines | ||
| 83 | 84 | 'sampler': 'self->hop_size', |
| 84 | 85 | 'mfcc': 'self->n_coeffs', |
| 85 | 86 | 'specdesc': '1', |
| 86 | − 'tempo': '1', | |
| 87 | + 'tempo': '2', | |
| 87 | 88 | 'filterbank': 'self->n_filters', |
| 89 | + 'tss': 'self->hop_size', | |
| 88 | 90 | } |
| 89 | 91 | |
| 90 | 92 | def get_name(proto): |
| @@ -104,20 +106,20 @@ def split_type(arg): · 13 unmodified lines | ||
| 104 | 106 | """ arg = 'foo *name' |
| 105 | 107 | return ['foo*', 'name'] """ |
| 106 | 108 | l = arg.split() |
| 107 | − type_arg = {'type': l[0], 'name': l[1]} | |
| 108 | − # ['foo', '*name'] -> ['foo*', 'name'] | |
| 109 | − if l[-1].startswith('*'): | |
| 110 | − #return [l[0]+'*', l[1][1:]] | |
| 111 | − type_arg['type'] = l[0] + '*' | |
| 112 | − type_arg['name'] = l[1][1:] | |
| 113 | − # ['foo', '*', 'name'] -> ['foo*', 'name'] | |
| 114 | − if len(l) == 3: | |
| 115 | − #return [l[0]+l[1], l[2]] | |
| 116 | − type_arg['type'] = l[0]+l[1] | |
| 117 | − type_arg['name'] = l[2] | |
| 118 | − else: | |
| 119 | − #return l | |
| 120 | − pass | |
| 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 ','') | |
| 121 | 123 | return type_arg |
| 122 | 124 | |
| 123 | 125 | def get_params(proto): |
| @@ -126,16 +128,28 @@ def get_params(proto): · 2 unmodified lines | ||
| 126 | 128 | returns: ['int argc', 'char ** argv'] |
| 127 | 129 | """ |
| 128 | 130 | import re |
| 129 | − paramregex = re.compile('[\(, ](\w+ \*?\*? ?\w+)[, \)]') | |
| 130 | − return paramregex.findall(proto) | |
| 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:] | |
| 131 | 143 | |
| 132 | 144 | def get_params_types_names(proto): |
| 133 | 145 | """ get the list of parameters from a function prototype |
| 134 | 146 | example: proto = "int main (int argc, char ** argv)" |
| 135 | 147 | returns: [['int', 'argc'], ['char **','argv']] |
| 136 | 148 | """ |
| 137 | − return list(map(split_type, get_params(proto))) | |
| 138 | − | |
| 149 | + a = list(map(split_type, get_params(proto))) | |
| 150 | + #print proto, a | |
| 151 | + #import sys; sys.exit(1) | |
| 152 | + return a | |
| 139 | 153 | |
| 140 | 154 | class MappedObject(object): |
| 141 | 155 | |
| @@ -148,10 +162,15 @@ class MappedObject(object): · 6 unmodified lines | ||
| 148 | 162 | self.del_proto = prototypes['del'][0] |
| 149 | 163 | self.do_proto = prototypes['do'][0] |
| 150 | 164 | self.input_params = get_params_types_names(self.new_proto) |
| 151 | − self.input_params_list = "; ".join(get_params(self.new_proto)) | |
| 165 | + self.input_params_list = "; ".join(get_input_params(self.new_proto)) | |
| 152 | 166 | self.outputs = get_params_types_names(self.do_proto)[2:] |
| 153 | − self.outputs_flat = get_params(self.do_proto)[2:] | |
| 154 | − self.output_results = ", ".join(self.outputs_flat) | |
| 167 | + self.do_inputs = [get_params_types_names(self.do_proto)[1]] | |
| 168 | + self.do_outputs = get_params_types_names(self.do_proto)[2:] | |
| 169 | + self.outputs_flat = get_output_params(self.do_proto) | |
| 170 | + self.output_results = "; ".join(self.outputs_flat) | |
| 171 | + | |
| 172 | + print "input_params", map(split_type, get_input_params(self.do_proto)) | |
| 173 | + print "output_params", map(split_type, get_output_params(self.do_proto)) | |
| 155 | 174 | |
| 156 | 175 | def gen_code(self): |
| 157 | 176 | out = "" |
| @@ -177,17 +196,19 @@ typedef struct{{ · 19 unmodified lines | ||
| 177 | 196 | {longname} *o; |
| 178 | 197 | // input parameters |
| 179 | 198 | {input_params_list}; |
| 199 | + // do input vectors | |
| 200 | + {do_inputs_list}; | |
| 180 | 201 | // output results |
| 181 | 202 | {output_results}; |
| 182 | 203 | }} Py_{shortname}; |
| 183 | 204 | """ |
| 184 | − return out.format(**self.__dict__) | |
| 205 | + return out.format(do_inputs_list = "; ".join(get_input_params(self.do_proto)), **self.__dict__) | |
| 185 | 206 | |
| 186 | 207 | def gen_doc(self): |
| 187 | 208 | out = """ |
| 188 | 209 | // TODO: add documentation |
| 189 | 210 | static char Py_{shortname}_doc[] = \"undefined\"; |
| 190 | − """ | |
| 211 | +""" | |
| 191 | 212 | return out.format(**self.__dict__) |
| 192 | 213 | |
| 193 | 214 | def gen_new(self): |
| @@ -221,7 +242,7 @@ Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds) · 27 unmodified lines | ||
| 221 | 242 | """.format(**self.__dict__) |
| 222 | 243 | params = self.input_params |
| 223 | 244 | for p in params: |
| 224 | − out += self.check_valid(p) | |
| 245 | + out += self.check_valid(p) | |
| 225 | 246 | out += """ |
| 226 | 247 | return (PyObject *)self; |
| 227 | 248 | } |
| @@ -289,6 +310,9 @@ Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds) · 61 unmodified lines | ||
| 289 | 310 | out += """ |
| 290 | 311 | // TODO get internal params after actual object creation? |
| 291 | 312 | """ |
| 313 | + for input_param in self.do_inputs: | |
| 314 | + out += """ | |
| 315 | + self->{0} = ({1})malloc(sizeof({2}));""".format(input_param['name'], input_param['type'], input_param['type'][:-1]) | |
| 292 | 316 | out += """ |
| 293 | 317 | // create outputs{output_create} |
| 294 | 318 | """.format(output_create = output_create) |
| @@ -317,6 +341,9 @@ static PyMemberDef Py_{shortname}_members[] = {{ · 22 unmodified lines | ||
| 317 | 341 | static void |
| 318 | 342 | Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) |
| 319 | 343 | {{""".format(**self.__dict__) |
| 344 | + for input_param in self.do_inputs: | |
| 345 | + out += """ | |
| 346 | + free(self->{0[name]});""".format(input_param) | |
| 320 | 347 | for o in self.outputs: |
| 321 | 348 | name = o['name'] |
| 322 | 349 | del_out = delfromtype_fn[o['type']] |
| @@ -331,39 +358,45 @@ Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) · 8 unmodified lines | ||
| 331 | 358 | return out |
| 332 | 359 | |
| 333 | 360 | def gen_do(self): |
| 334 | − do_fn = get_name(self.do_proto) | |
| 335 | − input_param = get_params_types_names(self.do_proto)[1]; | |
| 336 | − pytoaubio = pytoaubio_fn[input_param['type']] | |
| 337 | 361 | output = self.outputs[0] |
| 338 | 362 | out = """ |
| 339 | 363 | // do {shortname} |
| 340 | 364 | static PyObject* |
| 341 | 365 | Py_{shortname}_do (Py_{shortname} * self, PyObject * args) |
| 342 | −{{ | |
| 343 | − PyObject * in_obj; | |
| 344 | − {input_type} {input_name}; | |
| 345 | − | |
| 346 | − if (!PyArg_ParseTuple (args, "O", &in_obj)) {{ | |
| 366 | +{{""".format(**self.__dict__) | |
| 367 | + input_params = self.do_inputs | |
| 368 | + output_params = self.do_outputs | |
| 369 | + #print input_params | |
| 370 | + #print output_params | |
| 371 | + for input_param in input_params: | |
| 372 | + out += """ | |
| 373 | + PyObject *py_{0};""".format(input_param['name'], input_param['type']) | |
| 374 | + refs = ", ".join(["&py_%s" % p['name'] for p in input_params]) | |
| 375 | + pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params]) | |
| 376 | + out += """ | |
| 377 | + if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{ | |
| 347 | 378 | return NULL; |
| 348 | − }} | |
| 349 | − {input_name} = {pytoaubio} (in_obj); | |
| 350 | − if ({input_name} == NULL) {{ | |
| 379 | + }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__) | |
| 380 | + for p in input_params: | |
| 381 | + out += """ | |
| 382 | + if (!{pytoaubio}(py_{0[name]}, self->{0[name]})) {{ | |
| 351 | 383 | return NULL; |
| 352 | − }} | |
| 384 | + }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']]) | |
| 385 | + do_fn = get_name(self.do_proto) | |
| 386 | + inputs = ", ".join(['self->'+p['name'] for p in input_params]) | |
| 387 | + outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs]) | |
| 388 | + out += """ | |
| 353 | 389 | |
| 354 | − {do_fn}(self->o, {input_name}, {outputs}); | |
| 390 | + {do_fn}(self->o, {inputs}, {outputs}); | |
| 355 | 391 | |
| 356 | 392 | return (PyObject *) {aubiotonumpy} ({outputs}); |
| 357 | 393 | }} |
| 358 | −""" | |
| 359 | − return out.format(do_fn = do_fn, | |
| 360 | − shortname = self.prototypes['shortname'], | |
| 361 | − input_name = input_param['name'], | |
| 362 | − input_type= input_param['type'], | |
| 363 | − pytoaubio = pytoaubio, | |
| 364 | − outputs = ", ".join(["self->%s" % p['name'] for p in self.outputs]), | |
| 365 | − aubiotonumpy = pyfromaubio_fn[output['type']], | |
| 366 | − ) | |
| 394 | +""".format( | |
| 395 | + do_fn = do_fn, | |
| 396 | + aubiotonumpy = pyfromaubio_fn[output['type']], | |
| 397 | + inputs = inputs, outputs = outputs, | |
| 398 | + ) | |
| 399 | + return out | |
| 367 | 400 | |
| 368 | 401 | def gen_set(self): |
| 369 | 402 | out = """ |