Mercurial > hg > btrack
diff modules-and-plug-ins/python-module/btrack_python_module.cpp @ 24:deb49a2590f3 develop
Updated README, commented more code, added a Vamp plug-in
author | Adam <adamstark.uk@gmail.com> |
---|---|
date | Mon, 27 Jan 2014 23:11:31 +0000 |
parents | a8e3e95d14e4 |
children | 0fdaf082ad1a |
line wrap: on
line diff
--- a/modules-and-plug-ins/python-module/btrack_python_module.cpp Sat Jan 25 18:17:51 2014 +0000 +++ b/modules-and-plug-ins/python-module/btrack_python_module.cpp Mon Jan 27 23:11:31 2014 +0000 @@ -5,7 +5,112 @@ #include <numpy/arrayobject.h> //======================================================================= -static PyObject * btrack_onsetdf(PyObject *dummy, PyObject *args) +static PyObject * btrack_trackBeats(PyObject *dummy, PyObject *args) +{ + PyObject *arg1=NULL; + PyObject *arr1=NULL; + + if (!PyArg_ParseTuple(args, "O", &arg1)) + { + return NULL; + } + + arr1 = PyArray_FROM_OTF(arg1, NPY_DOUBLE, NPY_IN_ARRAY); + if (arr1 == NULL) + { + return NULL; + } + + + + ////////// GET INPUT DATA /////////////////// + + // get data as array + double* data = (double*) PyArray_DATA(arr1); + + // get array size + long signal_length = PyArray_Size((PyObject*)arr1); + + + ////////// BEGIN PROCESS /////////////////// + int hopSize = 512; + int frameSize = 1024; + + int numframes; + double buffer[hopSize]; // buffer to hold one hopsize worth of audio samples + + + // get number of audio frames, given the hop size and signal length + numframes = (int) floor(((double) signal_length) / ((double) hopSize)); + + + BTrack b(hopSize,frameSize); + + + double beats[5000]; + int beatnum = 0; + + /////////////////////////////////////////// + //////// Begin Processing Loop //////////// + + for (int i=0;i < numframes;i++) + { + // add new samples to frame + for (int n = 0;n < hopSize;n++) + { + buffer[n] = data[(i*hopSize)+n]; + } + + // process the current audio frame + b.processAudioFrame(buffer); + + // if a beat is currently scheduled + if (b.beatDueInCurrentFrame()) + { + beats[beatnum] = BTrack::getBeatTimeInSeconds(i,hopSize,44100); + beatnum = beatnum + 1; + } + + } + + ///////// End Processing Loop ///////////// + /////////////////////////////////////////// + + + ////////// END PROCESS /////////////////// + + double beats_out[beatnum]; // create output array + + // copy beats into output array + for (int i = 0;i < beatnum;i++) + { + beats_out[i] = beats[i]; + } + + + + ////////// CREATE ARRAY AND RETURN IT /////////////////// + int nd=1; + npy_intp m= beatnum; + //double fArray[5] = {0,1,2,3,4}; + + PyObject* c=PyArray_SimpleNew(nd, &m, NPY_DOUBLE); + + void *arr_data = PyArray_DATA((PyArrayObject*)c); + + memcpy(arr_data, beats_out, PyArray_ITEMSIZE((PyArrayObject*) c) * m); + + + Py_DECREF(arr1); + Py_INCREF(Py_None); + //return Py_None; + + return (PyObject *)c; +} + + +//======================================================================= +static PyObject * btrack_calculateOnsetDF(PyObject *dummy, PyObject *args) { PyObject *arg1=NULL; PyObject *arr1=NULL; @@ -67,14 +172,12 @@ /////////////////////////////////////////// - ////////// END PROCESS /////////////////// - - + ////////// CREATE ARRAY AND RETURN IT /////////////////// int nd=1; npy_intp m= numframes; - //double fArray[5] = {0,1,2,3,4}; + PyObject* c=PyArray_SimpleNew(nd, &m, NPY_DOUBLE); @@ -90,112 +193,9 @@ return (PyObject *)c; } -//======================================================================= -static PyObject * btrack_btrack(PyObject *dummy, PyObject *args) -{ - PyObject *arg1=NULL; - PyObject *arr1=NULL; - - if (!PyArg_ParseTuple(args, "O", &arg1)) - { - return NULL; - } - - arr1 = PyArray_FROM_OTF(arg1, NPY_DOUBLE, NPY_IN_ARRAY); - if (arr1 == NULL) - { - return NULL; - } - - - - ////////// GET INPUT DATA /////////////////// - - // get data as array - double* data = (double*) PyArray_DATA(arr1); - - // get array size - long signal_length = PyArray_Size((PyObject*)arr1); - - - ////////// BEGIN PROCESS /////////////////// - int hopSize = 512; - int frameSize = 1024; - - int numframes; - double buffer[hopSize]; // buffer to hold one hopsize worth of audio samples - - - // get number of audio frames, given the hop size and signal length - numframes = (int) floor(((double) signal_length) / ((double) hopSize)); - - - BTrack b(hopSize,frameSize); - - - double beats[5000]; - int beatnum = 0; - - /////////////////////////////////////////// - //////// Begin Processing Loop //////////// - - for (int i=0;i < numframes;i++) - { - // add new samples to frame - for (int n = 0;n < hopSize;n++) - { - buffer[n] = data[(i*hopSize)+n]; - } - - // process the current audio frame - b.processAudioFrame(buffer); - - // if a beat is currently scheduled - if (b.beatDueInCurrentFrame()) - { - beats[beatnum] = BTrack::getBeatTimeInSeconds(i,hopSize,44100); - beatnum = beatnum + 1; - } - - } - - ///////// End Processing Loop ///////////// - /////////////////////////////////////////// - - - ////////// END PROCESS /////////////////// - - double beats_out[beatnum]; // create output array - - // copy beats into output array - for (int i = 0;i < beatnum;i++) - { - beats_out[i] = beats[i]; - } - - - - ////////// CREATE ARRAY AND RETURN IT /////////////////// - int nd=1; - npy_intp m= beatnum; - //double fArray[5] = {0,1,2,3,4}; - - PyObject* c=PyArray_SimpleNew(nd, &m, NPY_DOUBLE); - - void *arr_data = PyArray_DATA((PyArrayObject*)c); - - memcpy(arr_data, beats_out, PyArray_ITEMSIZE((PyArrayObject*) c) * m); - - - Py_DECREF(arr1); - Py_INCREF(Py_None); - //return Py_None; - - return (PyObject *)c; -} //======================================================================= -static PyObject * btrack_btrack_df(PyObject *dummy, PyObject *args) +static PyObject * btrack_trackBeatsFromOnsetDF(PyObject *dummy, PyObject *args) { PyObject *arg1=NULL; PyObject *arr1=NULL; @@ -283,12 +283,11 @@ return (PyObject *)c; } - //======================================================================= static PyMethodDef btrack_methods[] = { - { "onsetdf",btrack_onsetdf,METH_VARARGS,"onset detection function"}, - { "btrack",btrack_btrack,METH_VARARGS,"beat tracker"}, - { "btrack_df",btrack_btrack_df,METH_VARARGS,"beat tracker with detection function input"}, + { "calculateOnsetDF",btrack_calculateOnsetDF,METH_VARARGS,"Calculate the onset detection function"}, + { "trackBeats",btrack_trackBeats,METH_VARARGS,"Track beats from audio"}, + { "trackBeatsFromOnsetDF",btrack_trackBeatsFromOnsetDF,METH_VARARGS,"Track beats from an onset detection function"}, {NULL, NULL, 0, NULL} /* Sentinel */ };