python/ext/py-musicutils.{c,h}: first .c and test
[aubio.git] / python / ext / aubiomodule.c
index 293758a..a54a0cb 100644 (file)
@@ -1,8 +1,83 @@
 #define PY_AUBIO_MODULE_MAIN
 #include "aubio-types.h"
-#include "generated/aubio-generated.h"
+#include "aubio-generated.h"
+#include "py-musicutils.h"
 
-static char Py_alpha_norm_doc[] = "compute alpha normalisation factor";
+static char aubio_module_doc[] = "Python module for the aubio library";
+
+static char Py_alpha_norm_doc[] = ""
+"alpha_norm(fvec, integer) -> float\n"
+"\n"
+"Compute alpha normalisation factor on vector, given alpha\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> b = alpha_norm(a, 9)";
+
+static char Py_bintomidi_doc[] = ""
+"bintomidi(float, samplerate = integer, fftsize = integer) -> float\n"
+"\n"
+"Convert bin (float) to midi (float), given the sampling rate and the FFT size\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> midi = bintomidi(float, samplerate = 44100, fftsize = 1024)";
+
+static char Py_miditobin_doc[] = ""
+"miditobin(float, samplerate = integer, fftsize = integer) -> float\n"
+"\n"
+"Convert midi (float) to bin (float), given the sampling rate and the FFT size\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> bin = miditobin(midi, samplerate = 44100, fftsize = 1024)";
+
+static char Py_bintofreq_doc[] = ""
+"bintofreq(float, samplerate = integer, fftsize = integer) -> float\n"
+"\n"
+"Convert bin number (float) in frequency (Hz), given the sampling rate and the FFT size\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> freq = bintofreq(bin, samplerate = 44100, fftsize = 1024)";
+
+static char Py_freqtobin_doc[] = ""
+"freqtobin(float, samplerate = integer, fftsize = integer) -> float\n"
+"\n"
+"Convert frequency (Hz) in bin number (float), given the sampling rate and the FFT size\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> bin = freqtobin(freq, samplerate = 44100, fftsize = 1024)";
+
+static char Py_zero_crossing_rate_doc[] = ""
+"zero_crossing_rate(fvec) -> float\n"
+"\n"
+"Compute Zero crossing rate of a vector\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> z = zero_crossing_rate(a)";
+
+static char Py_min_removal_doc[] = ""
+"min_removal(fvec) -> float\n"
+"\n"
+"Remove the minimum value of a vector, in-place modification\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> min_removal(a)";
+
+extern void add_generated_objects ( PyObject *m );
+extern void add_ufuncs ( PyObject *m );
+extern int generated_types_ready(void);
 
 static PyObject *
 Py_alpha_norm (PyObject * self, PyObject * args)
@@ -35,25 +110,6 @@ Py_alpha_norm (PyObject * self, PyObject * args)
   return result;
 }
 
-static char Py_unwrap2pi_doc[] = "unwrap phase value to [-pi, pi]";
-
-static PyObject *
-Py_unwrap2pi (PyObject * self, PyObject * args)
-{
-  smpl_t input;
-  smpl_t output;
-
-  if (!PyArg_ParseTuple (args, "|f", &input)) {
-    return NULL;
-  }
-
-  output = aubio_unwrap2pi (input);
-
-  return (PyObject *)PyFloat_FromDouble (output);
-}
-
-static char Py_bintomidi_doc[] = "convert bin to midi";
-
 static PyObject *
 Py_bintomidi (PyObject * self, PyObject * args)
 {
@@ -69,8 +125,6 @@ Py_bintomidi (PyObject * self, PyObject * args)
   return (PyObject *)PyFloat_FromDouble (output);
 }
 
-static char Py_miditobin_doc[] = "convert midi to bin";
-
 static PyObject *
 Py_miditobin (PyObject * self, PyObject * args)
 {
@@ -86,8 +140,6 @@ Py_miditobin (PyObject * self, PyObject * args)
   return (PyObject *)PyFloat_FromDouble (output);
 }
 
-static char Py_bintofreq_doc[] = "convert bin to freq";
-
 static PyObject *
 Py_bintofreq (PyObject * self, PyObject * args)
 {
@@ -103,8 +155,6 @@ Py_bintofreq (PyObject * self, PyObject * args)
   return (PyObject *)PyFloat_FromDouble (output);
 }
 
-static char Py_freqtobin_doc[] = "convert freq to bin";
-
 static PyObject *
 Py_freqtobin (PyObject * self, PyObject * args)
 {
@@ -120,42 +170,6 @@ Py_freqtobin (PyObject * self, PyObject * args)
   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 *
 Py_zero_crossing_rate (PyObject * self, PyObject * args)
 {
@@ -186,8 +200,6 @@ Py_zero_crossing_rate (PyObject * self, PyObject * args)
   return result;
 }
 
-static char Py_min_removal_doc[] = "compute zero crossing rate";
-
 static PyObject *
 Py_min_removal(PyObject * self, PyObject * args)
 {
@@ -212,7 +224,7 @@ Py_min_removal(PyObject * self, PyObject * args)
   fvec_min_removal (vec);
 
   // since this function does not return, we could return None
-  //return Py_None;
+  //Py_RETURN_NONE;
   // however it is convenient to return the modified vector
   return (PyObject *) PyAubio_CFvecToArray(vec);
   // or even without converting it back to an array
@@ -221,21 +233,17 @@ Py_min_removal(PyObject * self, PyObject * args)
 }
 
 static PyMethodDef aubio_methods[] = {
-  {"unwrap2pi", Py_unwrap2pi, METH_VARARGS, Py_unwrap2pi_doc},
   {"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},
   {"min_removal", Py_min_removal, METH_VARARGS, Py_min_removal_doc},
+  {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
   {NULL, NULL} /* Sentinel */
 };
 
-static char aubio_module_doc[] = "Python module for the aubio library";
-
 PyMODINIT_FUNC
 init_aubio (void)
 {
@@ -248,6 +256,8 @@ init_aubio (void)
       || (PyType_Ready (&Py_filterbankType) < 0)
       || (PyType_Ready (&Py_fftType) < 0)
       || (PyType_Ready (&Py_pvocType) < 0)
+      || (PyType_Ready (&Py_sourceType) < 0)
+      || (PyType_Ready (&Py_sinkType) < 0)
       // generated objects
       || (generated_types_ready() < 0 )
   ) {
@@ -261,10 +271,9 @@ init_aubio (void)
   }
 
   err = _import_array ();
-
   if (err != 0) {
     fprintf (stderr,
-        "Unable to import Numpy C API from aubio module (error %d)\n", err);
+        "Unable to import Numpy array from aubio module (error %d)\n", err);
   }
 
   Py_INCREF (&Py_cvecType);
@@ -277,7 +286,14 @@ init_aubio (void)
   PyModule_AddObject (m, "fft", (PyObject *) & Py_fftType);
   Py_INCREF (&Py_pvocType);
   PyModule_AddObject (m, "pvoc", (PyObject *) & Py_pvocType);
+  Py_INCREF (&Py_sourceType);
+  PyModule_AddObject (m, "source", (PyObject *) & Py_sourceType);
+  Py_INCREF (&Py_sinkType);
+  PyModule_AddObject (m, "sink", (PyObject *) & Py_sinkType);
 
   // add generated objects
   add_generated_objects(m);
+
+  // add ufunc
+  add_ufuncs(m);
 }