From 6a50b9e07fb992eb097657acbd34ca50ac70f121 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sun, 3 Mar 2013 16:20:30 -0500 Subject: [PATCH] python/aubiomodule.c: add midi/bin/freq conversion --- python/aubiomodule.c | 111 ++++++++++++++++++++++++++++++++- python/tests/test_mathutils.py | 31 +++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100755 python/tests/test_mathutils.py diff --git a/python/aubiomodule.c b/python/aubiomodule.c index 9e607af4..a3f6d92b 100644 --- a/python/aubiomodule.c +++ b/python/aubiomodule.c @@ -39,6 +39,108 @@ Py_alpha_norm (PyObject * self, PyObject * args) return result; } +static char Py_bintomidi_doc[] = "convert bin to midi"; + +static PyObject * +Py_bintomidi (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_bintomidi (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_miditobin_doc[] = "convert midi to bin"; + +static PyObject * +Py_miditobin (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_miditobin (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_bintofreq_doc[] = "convert bin to freq"; + +static PyObject * +Py_bintofreq (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_bintofreq (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_freqtobin_doc[] = "convert freq to bin"; + +static PyObject * +Py_freqtobin (PyObject * self, PyObject * args) +{ + smpl_t input, samplerate, fftsize; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|fff", &input, &samplerate, &fftsize)) { + return NULL; + } + + output = aubio_freqtobin (input, samplerate, fftsize); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_freqtomidi_doc[] = "convert freq to midi"; + +static PyObject * +Py_freqtomidi (PyObject * self, PyObject * args) +{ + smpl_t input; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|f", &input)) { + return NULL; + } + + output = aubio_freqtomidi (input); + + return (PyObject *)PyFloat_FromDouble (output); +} + +static char Py_miditofreq_doc[] = "convert midi to freq"; + +static PyObject * +Py_miditofreq (PyObject * self, PyObject * args) +{ + smpl_t input; + smpl_t output; + + if (!PyArg_ParseTuple (args, "|f", &input)) { + return NULL; + } + + output = aubio_miditofreq (input); + + return (PyObject *)PyFloat_FromDouble (output); +} + static char Py_zero_crossing_rate_doc[] = "compute zero crossing rate"; static PyObject * @@ -106,9 +208,14 @@ Py_min_removal(PyObject * self, PyObject * args) } static PyMethodDef aubio_methods[] = { + {"bintomidi", Py_bintomidi, METH_VARARGS, Py_bintomidi_doc}, + {"miditobin", Py_miditobin, METH_VARARGS, Py_miditobin_doc}, + {"bintofreq", Py_bintofreq, METH_VARARGS, Py_bintofreq_doc}, + {"freqtobin", Py_freqtobin, METH_VARARGS, Py_freqtobin_doc}, + {"miditofreq", Py_miditofreq, METH_VARARGS, Py_miditofreq_doc}, + {"freqtomidi", Py_freqtomidi, METH_VARARGS, Py_freqtomidi_doc}, {"alpha_norm", Py_alpha_norm, METH_VARARGS, Py_alpha_norm_doc}, - {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, - Py_zero_crossing_rate_doc}, + {"zero_crossing_rate", Py_zero_crossing_rate, METH_VARARGS, Py_zero_crossing_rate_doc}, {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc}, {NULL, NULL} /* Sentinel */ }; diff --git a/python/tests/test_mathutils.py b/python/tests/test_mathutils.py new file mode 100755 index 00000000..9a8fea91 --- /dev/null +++ b/python/tests/test_mathutils.py @@ -0,0 +1,31 @@ +#! /usr/bin/env python + +from numpy.testing import TestCase, run_module_suite +from numpy.testing import assert_equal, assert_almost_equal +from aubio import bintomidi, miditobin, freqtobin, bintofreq, freqtomidi, miditofreq + +class aubio_mathutils(TestCase): + + def test_miditobin(self): + a = [ miditobin(a, 44100, 512) for a in range(128) ] + + def test_bintomidi(self): + a = [ bintomidi(a, 44100, 512) for a in range(128) ] + + def test_freqtobin(self): + a = [ freqtobin(a, 44100, 512) for a in range(128) ] + + def test_bintofreq(self): + a = [ bintofreq(a, 44100, 512) for a in range(128) ] + + def test_freqtomidi(self): + a = [ freqtomidi(a) for a in range(128) ] + + def test_miditofreq(self): + freqs = [ miditofreq(a) for a in range(128) ] + midis = [ freqtomidi(a) for a in freqs ] + print midis + +if __name__ == '__main__': + from unittest import main + main() -- 2.26.2