Changeset 569b363


Ignore:
Timestamp:
Apr 24, 2016, 6:23:14 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
51b9c83
Parents:
a35db12
Message:

python/ext: simplify memory allocations, removed unneeded malloc/free calls

Location:
python
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • python/ext/aubiomodule.c

    ra35db12 r569b363  
    8484{
    8585  PyObject *input;
    86   fvec_t *vec;
     86  fvec_t vec;
    8787  smpl_t alpha;
    8888  PyObject *result;
     
    9696  }
    9797
    98   vec = (fvec_t *)malloc(sizeof(fvec_t));
    99   if (!PyAubio_ArrayToCFvec(input, vec)) {
    100     free(vec);
     98  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    10199    return NULL;
    102100  }
    103101
    104102  // compute the function
    105   result = Py_BuildValue ("f", fvec_alpha_norm (vec, alpha));
    106   free(vec);
     103  result = Py_BuildValue ("f", fvec_alpha_norm (&vec, alpha));
    107104  if (result == NULL) {
    108105    return NULL;
     
    176173{
    177174  PyObject *input;
    178   fvec_t *vec;
     175  fvec_t vec;
    179176  PyObject *result;
    180177
     
    187184  }
    188185
    189   vec = (fvec_t *)malloc(sizeof(fvec_t));
    190   if (!PyAubio_ArrayToCFvec(input, vec)) {
    191     free(vec);
     186  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    192187    return NULL;
    193188  }
    194189
    195190  // compute the function
    196   result = Py_BuildValue ("f", aubio_zero_crossing_rate (vec));
    197   free(vec);
     191  result = Py_BuildValue ("f", aubio_zero_crossing_rate (&vec));
    198192  if (result == NULL) {
    199193    return NULL;
     
    207201{
    208202  PyObject *input;
    209   fvec_t *vec;
     203  fvec_t vec;
    210204
    211205  if (!PyArg_ParseTuple (args, "O:min_removal", &input)) {
     
    217211  }
    218212
    219   vec = (fvec_t *)malloc(sizeof(fvec_t));
    220   if (!PyAubio_ArrayToCFvec(input, vec)) {
    221     free(vec);
     213  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    222214    return NULL;
    223215  }
    224216
    225217  // compute the function
    226   fvec_min_removal (vec);
     218  fvec_min_removal (&vec);
    227219
    228220  // since this function does not return, we could return None
    229221  //Py_RETURN_NONE;
    230222  // however it is convenient to return the modified vector
    231   return (PyObject *) PyAubio_CFvecToArray(vec);
     223  return (PyObject *) PyAubio_CFvecToArray(&vec);
    232224  // or even without converting it back to an array
    233225  //Py_INCREF(vec);
  • python/ext/aubioproxy.c

    ra35db12 r569b363  
    144144  }
    145145
    146   if (mat->height != (uint_t)PyArray_DIM ((PyArrayObject *)input, 0)) {
    147     /*
    148     free(mat->data);
    149     mat->height = (uint_t)PyArray_DIM ((PyArrayObject *)input, 0);
    150     mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height);
    151     */
    152     PyErr_Format(PyExc_ValueError, "too many rows, %d but %ld expected",
    153                       mat->height, PyArray_DIM ((PyArrayObject *)input, 0) );
    154     return 0;
     146  uint_t new_height = (uint_t)PyArray_DIM ((PyArrayObject *)input, 0);
     147  if (mat->height != new_height) {
     148    if (mat->data) {
     149      free(mat->data);
     150    }
     151    mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * new_height);
    155152  }
    156153
     154  mat->height = new_height;
    157155  mat->length = (uint_t)PyArray_DIM ((PyArrayObject *)input, 1);
    158156  for (i=0; i< mat->height; i++) {
  • python/ext/py-fft.c

    ra35db12 r569b363  
    88  aubio_fft_t * o;
    99  uint_t win_s;
    10   fvec_t *vecin;
     10  // do / rdo input vectors
     11  fvec_t vecin;
     12  cvec_t cvecin;
     13  // do / rdo output results
    1114  cvec_t *out;
     15  fvec_t *rout;
     16  // bridge for cvec output
    1217  Py_cvec *py_out;
    13   cvec_t *cvecin;
    14   fvec_t *rout;
    1518} Py_fft;
    1619
     
    5760  }
    5861
    59   self->cvecin = (cvec_t *)malloc(sizeof(cvec_t));
    60   self->vecin = (fvec_t *)malloc(sizeof(fvec_t));
    61 
    6262  self->out = new_cvec(self->win_s);
    6363  self->py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
     
    7575  del_cvec(self->out);
    7676  del_fvec(self->rout);
    77   free(self->cvecin);
    78   free(self->vecin);
    7977  Py_TYPE(self)->tp_free((PyObject *) self);
    8078}
     
    8987  }
    9088
    91   if (!PyAubio_ArrayToCFvec(input, self->vecin)) {
     89  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
    9290    return NULL;
    9391  }
    9492
    9593  // compute the function
    96   aubio_fft_do (((Py_fft *)self)->o, self->vecin, self->out);
     94  aubio_fft_do (((Py_fft *)self)->o, &(self->vecin), self->out);
    9795#if 0
    9896  Py_cvec * py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
     
    120118  }
    121119
    122   if (!PyAubio_ArrayToCCvec (input, self->cvecin) ) {
     120  if (!PyAubio_ArrayToCCvec (input, &(self->cvecin)) ) {
    123121    return NULL;
    124122  }
    125123
    126124  // compute the function
    127   aubio_fft_rdo (self->o, self->cvecin, self->rout);
     125  aubio_fft_rdo (self->o, &(self->cvecin), self->rout);
    128126  return PyAubio_CFvecToArray(self->rout);
    129127}
  • python/ext/py-filter.c

    ra35db12 r569b363  
    66  aubio_filter_t * o;
    77  uint_t order;
    8   fvec_t *vec;
     8  fvec_t vec;
    99  fvec_t *out;
    1010} Py_filter;
     
    5151  }
    5252  self->out = new_fvec(Py_default_vector_length);
    53   self->vec = (fvec_t *)malloc(sizeof(fvec_t));
    5453  return 0;
    5554}
     
    6059  del_fvec(self->out);
    6160  del_aubio_filter (self->o);
    62   free(self->vec);
    6361  Py_TYPE(self)->tp_free ((PyObject *) self);
    6462}
     
    7775  }
    7876
    79   if (!PyAubio_ArrayToCFvec(input, self->vec)) {
     77  if (!PyAubio_ArrayToCFvec(input, &(self->vec))) {
    8078    return NULL;
    8179  }
    8280
    8381  // reallocate the output if needed
    84   if (self->vec->length != self->out->length) {
     82  if (self->vec.length != self->out->length) {
    8583    del_fvec(self->out);
    86     self->out = new_fvec(self->vec->length);
     84    self->out = new_fvec(self->vec.length);
    8785  }
    8886  // compute the function
    89   aubio_filter_do_outplace (self->o, self->vec, self->out);
     87  aubio_filter_do_outplace (self->o, &(self->vec), self->out);
    9088  return PyAubio_CFvecToArray(self->out);
    9189}
  • python/ext/py-filterbank.c

    ra35db12 r569b363  
    99  uint_t n_filters;
    1010  uint_t win_s;
    11   cvec_t *vec;
     11  cvec_t vec;
     12  fvec_t freqs;
     13  fmat_t coeffs;
    1214  fvec_t *out;
    13   fvec_t *freqs;
    14   fmat_t *coeffs;
    1515} Py_filterbank;
    1616
     
    6767  self->out = new_fvec(self->n_filters);
    6868
    69   self->vec = (cvec_t *)malloc(sizeof(cvec_t));
    70 
    71   self->freqs = (fvec_t *)malloc(sizeof(fvec_t));
    72 
    73   self->coeffs = (fmat_t *)malloc(sizeof(fmat_t));
    74   self->coeffs->data = (smpl_t **)malloc(sizeof(smpl_t*) * self->n_filters);
    75   self->coeffs->height = self->n_filters;
    76 
    7769  return 0;
    7870}
     
    8375  del_aubio_filterbank(self->o);
    8476  del_fvec(self->out);
    85   free(self->vec);
    86   free(self->freqs);
    87   free(self->coeffs->data);
    88   free(self->coeffs);
     77  free(self->coeffs.data);
    8978  Py_TYPE(self)->tp_free((PyObject *) self);
    9079}
     
    9988  }
    10089
    101   if (!PyAubio_ArrayToCCvec(input, self->vec)) {
     90  if (!PyAubio_ArrayToCCvec(input, &(self->vec) )) {
    10291    return NULL;
    10392  }
    10493
    10594  // compute the function
    106   aubio_filterbank_do (self->o, self->vec, self->out);
     95  aubio_filterbank_do (self->o, &(self->vec), self->out);
    10796  return (PyObject *)PyAubio_CFvecToArray(self->out);
    10897}
     
    131120  }
    132121
    133   if (!PyAubio_ArrayToCFvec(input, self->freqs)) {
     122  if (!PyAubio_ArrayToCFvec(input, &(self->freqs) )) {
    134123    return NULL;
    135124  }
    136125
    137126  err = aubio_filterbank_set_triangle_bands (self->o,
    138       self->freqs, samplerate);
     127      &(self->freqs), samplerate);
    139128  if (err > 0) {
    140129    PyErr_SetString (PyExc_ValueError,
     
    174163  }
    175164
    176   if (!PyAubio_ArrayToCFmat(input, self->coeffs)) {
    177     return NULL;
    178   }
    179 
    180   err = aubio_filterbank_set_coeffs (self->o, self->coeffs);
     165  if (!PyAubio_ArrayToCFmat(input, &(self->coeffs))) {
     166    return NULL;
     167  }
     168
     169  err = aubio_filterbank_set_coeffs (self->o, &(self->coeffs));
    181170
    182171  if (err > 0) {
  • python/ext/py-musicutils.c

    ra35db12 r569b363  
    2626{
    2727  PyObject *input;
    28   fvec_t *vec;
     28  fvec_t vec;
    2929  PyObject *level_lin;
    3030
     
    3838  }
    3939
    40   vec = (fvec_t *)malloc(sizeof(fvec_t));
    41   if (!PyAubio_ArrayToCFvec(input, vec)) {
    42     free(vec);
     40  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    4341    return NULL;
    4442  }
    4543
    46   level_lin = Py_BuildValue("f", aubio_level_lin(vec));
    47   free(vec);
     44  level_lin = Py_BuildValue("f", aubio_level_lin(&vec));
    4845  if (level_lin == NULL) {
    4946    PyErr_SetString (PyExc_ValueError, "failed computing level_lin");
     
    5855{
    5956  PyObject *input;
    60   fvec_t *vec;
     57  fvec_t vec;
    6158  PyObject *db_spl;
    6259
     
    7067  }
    7168
    72   vec = (fvec_t *)malloc(sizeof(fvec_t));
    73   if (!PyAubio_ArrayToCFvec(input, vec)) {
    74     free(vec);
     69  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    7570    return NULL;
    7671  }
    7772
    78   db_spl = Py_BuildValue("f", aubio_db_spl(vec));
    79   free(vec);
     73  db_spl = Py_BuildValue("f", aubio_db_spl(&vec));
    8074  if (db_spl == NULL) {
    8175    PyErr_SetString (PyExc_ValueError, "failed computing db_spl");
     
    9084{
    9185  PyObject *input;
    92   fvec_t *vec;
     86  fvec_t vec;
    9387  PyObject *silence_detection;
    9488  smpl_t threshold;
     
    10397  }
    10498
    105   vec = (fvec_t *)malloc(sizeof(fvec_t));
    106   if (!PyAubio_ArrayToCFvec(input, vec)) {
    107     free(vec);
     99  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    108100    return NULL;
    109101  }
    110102
    111   silence_detection = Py_BuildValue("I", aubio_silence_detection(vec, threshold));
    112   free(vec);
     103  silence_detection = Py_BuildValue("I", aubio_silence_detection(&vec, threshold));
    113104  if (silence_detection == NULL) {
    114105    PyErr_SetString (PyExc_ValueError, "failed computing silence_detection");
     
    123114{
    124115  PyObject *input;
    125   fvec_t *vec;
     116  fvec_t vec;
    126117  PyObject *level_detection;
    127118  smpl_t threshold;
     
    136127  }
    137128
    138   vec = (fvec_t *)malloc(sizeof(fvec_t));
    139   if (!PyAubio_ArrayToCFvec(input, vec)) {
    140     free(vec);
     129  if (!PyAubio_ArrayToCFvec(input, &vec)) {
    141130    return NULL;
    142131  }
    143132
    144   level_detection = Py_BuildValue("f", aubio_level_detection(vec, threshold));
    145   free(vec);
     133  level_detection = Py_BuildValue("f", aubio_level_detection(&vec, threshold));
    146134  if (level_detection == NULL) {
    147135    PyErr_SetString (PyExc_ValueError, "failed computing level_detection");
  • python/ext/py-phasevoc.c

    ra35db12 r569b363  
    11#include "aubio-types.h"
    2 
    3 static char Py_pvoc_doc[] = "pvoc object";
    42
    53typedef struct
     
    97  uint_t win_s;
    108  uint_t hop_s;
    11   fvec_t *vecin;
     9  fvec_t vecin;
    1210  cvec_t *output;
    1311  Py_cvec *py_out;
    14   cvec_t *cvecin;
     12  cvec_t cvecin;
    1513  fvec_t *routput;
    1614} Py_pvoc;
     
    7270  }
    7371
    74   self->cvecin = (cvec_t *)malloc(sizeof(cvec_t));
    75   self->vecin = (fvec_t *)malloc(sizeof(fvec_t));
    76 
    7772  self->output = new_cvec(self->win_s);
    7873  self->py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
     
    8984  del_cvec(self->output);
    9085  del_fvec(self->routput);
    91   free(self->cvecin);
    92   free(self->vecin);
    9386  Py_TYPE(self)->tp_free((PyObject *) self);
    9487}
     
    10497  }
    10598
    106   if (!PyAubio_ArrayToCFvec (input, self->vecin)) {
     99  if (!PyAubio_ArrayToCFvec (input, &(self->vecin) )) {
    107100    return NULL;
    108101  }
    109102
    110103  // compute the function
    111   aubio_pvoc_do (self->o, self->vecin, self->output);
     104  aubio_pvoc_do (self->o, &(self->vecin), self->output);
    112105#if 0
    113106  Py_cvec * py_out = (Py_cvec*) PyObject_New (Py_cvec, &Py_cvecType);
     
    136129  }
    137130
    138   if (!PyAubio_ArrayToCCvec (input, self->cvecin)) {
     131  if (!PyAubio_ArrayToCCvec (input, &(self->cvecin) )) {
    139132    return NULL;
    140133  }
    141134
    142135  // compute the function
    143   aubio_pvoc_rdo (self->o, self->cvecin, self->routput);
     136  aubio_pvoc_rdo (self->o, &(self->cvecin), self->routput);
    144137  return PyAubio_CFvecToArray(self->routput);
    145138}
  • python/ext/py-sink.c

    ra35db12 r569b363  
    88  uint_t samplerate;
    99  uint_t channels;
    10   fvec_t *write_data;
    11   fmat_t *mwrite_data;
     10  fvec_t write_data;
     11  fmat_t mwrite_data;
    1212} Py_sink;
    1313
     
    124124  self->channels = aubio_sink_get_channels ( self->o );
    125125
    126   self->write_data = (fvec_t *)malloc(sizeof(fvec_t));
    127   self->mwrite_data = (fmat_t *)malloc(sizeof(fmat_t));
    128   self->mwrite_data->height = self->channels;
    129   self->mwrite_data->data = (smpl_t **)malloc(sizeof(smpl_t*) * self->channels);
    130126  return 0;
    131127}
     
    135131{
    136132  del_aubio_sink(self->o);
    137   free(self->write_data);
    138   free(self->mwrite_data->data);
    139   free(self->mwrite_data);
     133  free(self->mwrite_data.data);
    140134  Py_TYPE(self)->tp_free((PyObject *) self);
    141135}
     
    157151
    158152  /* input vectors parsing */
    159   if (!PyAubio_ArrayToCFvec(write_data_obj, self->write_data)) {
     153  if (!PyAubio_ArrayToCFvec(write_data_obj, &(self->write_data))) {
    160154    return NULL;
    161155  }
     
    163157
    164158  /* compute _do function */
    165   aubio_sink_do (self->o, self->write_data, write);
     159  aubio_sink_do (self->o, &(self->write_data), write);
    166160
    167161  Py_RETURN_NONE;
     
    185179
    186180  /* input vectors parsing */
    187   if (!PyAubio_ArrayToCFmat(write_data_obj, self->mwrite_data)) {
     181  if (!PyAubio_ArrayToCFmat(write_data_obj, &(self->mwrite_data))) {
    188182    return NULL;
    189183  }
    190184
    191185  /* compute _do function */
    192   aubio_sink_do_multi (self->o, self->mwrite_data, write);
     186  aubio_sink_do_multi (self->o, &(self->mwrite_data), write);
    193187  Py_RETURN_NONE;
    194188}
  • python/lib/gen_code.py

    ra35db12 r569b363  
    203203}} Py_{shortname};
    204204"""
    205         return out.format(do_inputs_list = "; ".join(get_input_params(self.do_proto)), **self.__dict__)
     205        # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname}
     206        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')
     207        return out.format(do_inputs_list = do_inputs_list, **self.__dict__)
    206208
    207209    def gen_doc(self):
     
    311313  // TODO get internal params after actual object creation?
    312314"""
    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])
    316315        out += """
    317316  // create outputs{output_create}
     
    343342{{""".format(**self.__dict__)
    344343        for input_param in self.do_inputs:
    345             out += """
    346     free(self->{0[name]});""".format(input_param)
     344            if input_param['type'] == 'fmat_t *':
     345                out += """
     346    free(self->{0[name]}.data);""".format(input_param)
    347347        for o in self.outputs:
    348348            name = o['name']
     
    380380        for p in input_params:
    381381            out += """
    382     if (!{pytoaubio}(py_{0[name]}, self->{0[name]})) {{
     382    if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{
    383383        return NULL;
    384384    }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
    385385        do_fn = get_name(self.do_proto)
    386         inputs = ", ".join(['self->'+p['name'] for p in input_params])
     386        inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params])
    387387        outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
    388388        out += """
Note: See TracChangeset for help on using the changeset viewer.