shithub: aubio

Download patch

ref: 6a50b9e07fb992eb097657acbd34ca50ac70f121
parent: 68d049273e1a1aa0cda6e69448b27147b6008ea8
author: Paul Brossier <piem@piem.org>
date: Sun Mar 3 11:20:30 EST 2013

python/aubiomodule.c: add midi/bin/freq conversion

--- a/python/aubiomodule.c
+++ b/python/aubiomodule.c
@@ -39,6 +39,108 @@
   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 @@
 }
 
 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 */
 };
--- /dev/null
+++ b/python/tests/test_mathutils.py
@@ -1,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()