History

python/lib/gen_code.py: switch to using PyObjects instead of fvec, cvec, fmat

Paul Brossier committed 10 years ago

Commit 21e84081 parent b055b4e2 files · +42 −26Browse files

Showing the changes to python (2 of 2 files). Show all

Unified · Split

2 files
  1. python/ext/py-filter.c +17 −12
  2. python/lib/gen_code.py +25 −14
python/ext/py-filter.c+17−12
@@ -6,7 +6,8 @@ typedef struct · 5 unmodified lines
6 aubio_filter_t * o;6 aubio_filter_t * o;
7 uint_t order;7 uint_t order;
8 fvec_t vec;8 fvec_t vec;
9 fvec_t *out;9 PyObject *out;
10 fvec_t c_out;
10} Py_filter;11} Py_filter;
11 12
12static char Py_filter_doc[] = "filter object";13static char Py_filter_doc[] = "filter object";
@@ -49,19 +50,19 @@ Py_filter_init (Py_filter * self, PyObject * args, PyObject * kwds) · 36 unmodified lines
49 if (self->o == NULL) {50 if (self->o == NULL) {
50 return -1;51 return -1;
51 }52 }
52 self->out = new_fvec(Py_default_vector_length);53 self->out = NULL;
53 return 0;54 return 0;
54}55}
55 56
56static void57static void
57Py_filter_del (Py_filter * self)58Py_filter_del (Py_filter * self)
58{59{
59 del_fvec(self->out);60 Py_XDECREF(self->out);
60 del_aubio_filter (self->o);61 del_aubio_filter (self->o);
61 Py_TYPE(self)->tp_free ((PyObject *) self);62 Py_TYPE(self)->tp_free ((PyObject *) self);
62}63}
63 64
64static PyObject * 65static PyObject *
65Py_filter_do(Py_filter * self, PyObject * args)66Py_filter_do(Py_filter * self, PyObject * args)
66{67{
67 PyObject *input;68 PyObject *input;
@@ -78,17 +79,21 @@ Py_filter_do(Py_filter * self, PyObject * args) · 10 unmodified lines
78 return NULL;79 return NULL;
79 }80 }
80 81
81 // reallocate the output if needed82 // initialize output now
82 if (self->vec.length != self->out->length) {83 if (self->out == NULL) {
83 del_fvec(self->out);84 self->out = new_py_fvec(self->vec.length);
84 self->out = new_fvec(self->vec.length);85 }
86
87 Py_INCREF(self->out);
88 if (!PyAubio_ArrayToCFvec(self->out, &(self->c_out)) ) {
89 return NULL;
85 }90 }
86 // compute the function91 // compute the function
87 aubio_filter_do_outplace (self->o, &(self->vec), self->out);92 aubio_filter_do_outplace (self->o, &(self->vec), &(self->c_out));
88 return PyAubio_CFvecToArray(self->out);93 return self->out;
89}94}
90 95
91static PyObject * 96static PyObject *
92Py_filter_set_c_weighting (Py_filter * self, PyObject *args)97Py_filter_set_c_weighting (Py_filter * self, PyObject *args)
93{98{
94 uint_t err = 0;99 uint_t err = 0;
@@ -106,7 +111,7 @@ Py_filter_set_c_weighting (Py_filter * self, PyObject *args) · 11 unmodified lines
106 Py_RETURN_NONE;111 Py_RETURN_NONE;
107}112}
108 113
109static PyObject * 114static PyObject *
110Py_filter_set_a_weighting (Py_filter * self, PyObject *args)115Py_filter_set_a_weighting (Py_filter * self, PyObject *args)
111{116{
112 uint_t err = 0;117 uint_t err = 0;
python/lib/gen_code.py+25−14
@@ -48,15 +48,15 @@ pyfromaubio_fn = { · 47 unmodified lines
48 }48 }
49 49
50newfromtype_fn = {50newfromtype_fn = {
51 'fvec_t*': 'new_fvec',51 'fvec_t*': 'new_py_fvec',
52 'fmat_t*': 'new_fmat',52 'fmat_t*': 'new_py_fmat',
53 'cvec_t*': 'new_cvec',53 'cvec_t*': 'new_py_cvec',
54 }54 }
55 55
56delfromtype_fn = {56delfromtype_fn = {
57 'fvec_t*': 'del_fvec',57 'fvec_t*': 'Py_DECREF',
58 'fmat_t*': 'del_fmat',58 'fmat_t*': 'Py_DECREF',
59 'cvec_t*': 'del_cvec',59 'cvec_t*': 'Py_DECREF',
60 }60 }
61 61
62param_init = {62param_init = {
@@ -168,8 +168,8 @@ class MappedObject(object): · 105 unmodified lines
168 self.outputs = get_params_types_names(self.do_proto)[2:]168 self.outputs = get_params_types_names(self.do_proto)[2:]
169 self.do_inputs = [get_params_types_names(self.do_proto)[1]]169 self.do_inputs = [get_params_types_names(self.do_proto)[1]]
170 self.do_outputs = get_params_types_names(self.do_proto)[2:]170 self.do_outputs = get_params_types_names(self.do_proto)[2:]
171 self.outputs_flat = get_output_params(self.do_proto)171 struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs]
172 self.output_results = "; ".join(self.outputs_flat)172 self.struct_outputs = ";\n ".join(struct_output_str)
173 173
174 #print ("input_params: ", map(split_type, get_input_params(self.do_proto)))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)))175 #print ("output_params", map(split_type, get_output_params(self.do_proto)))
@@ -201,7 +201,7 @@ typedef struct{{ · 25 unmodified lines
201 // do input vectors201 // do input vectors
202 {do_inputs_list};202 {do_inputs_list};
203 // output results203 // output results
204 {output_results};204 {struct_outputs};
205}} Py_{shortname};205}} Py_{shortname};
206"""206"""
207 # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname}207 # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname}
@@ -379,24 +379,35 @@ Py_{shortname}_do (Py_{shortname} * self, PyObject * args) · 171 unmodified lines
379 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{379 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
380 return NULL;380 return NULL;
381 }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)381 }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
382 for p in input_params:382 for input_param in input_params:
383 out += """383 out += """
384
384 if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{385 if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{
385 return NULL;386 return NULL;
386 }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])387 }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
388 out += """
389
390 // TODO: check input sizes"""
391 for output_param in output_params:
392 out += """
393
394 Py_INCREF(self->{0[name]});
395 if (!{pytoaubio}(self->{0[name]}, &(self->c_{0[name]}))) {{
396 return NULL;
397 }}""".format(output_param, pytoaubio = pytoaubio_fn[output_param['type']])
387 do_fn = get_name(self.do_proto)398 do_fn = get_name(self.do_proto)
388 inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params])399 inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params])
400 c_outputs = ", ".join(["&(self->c_%s)" % p['name'] for p in self.do_outputs])
389 outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])401 outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
390 out += """402 out += """
391 403
392 {do_fn}(self->o, {inputs}, {outputs});404 {do_fn}(self->o, {inputs}, {c_outputs});
393 405
394 return (PyObject *) {aubiotonumpy} ({outputs});406 return {outputs};
395}}407}}
396""".format(408""".format(
397 do_fn = do_fn,409 do_fn = do_fn,
398 aubiotonumpy = pyfromaubio_fn[output['type']], 410 inputs = inputs, outputs = outputs, c_outputs = c_outputs,
399 inputs = inputs, outputs = outputs,
400 )411 )
401 return out412 return out
402 413

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t