ext/py-musicutils.c: complete window implementation
authorPaul Brossier <piem@piem.org>
Thu, 9 Jul 2015 22:59:23 +0000 (00:59 +0200)
committerPaul Brossier <piem@piem.org>
Thu, 9 Jul 2015 22:59:23 +0000 (00:59 +0200)
python/ext/py-musicutils.c
python/ext/py-musicutils.h
python/tests/test_musicutils.py

index e50939d..ca21fbe 100644 (file)
@@ -3,17 +3,20 @@
 PyObject *
 Py_aubio_window(PyObject *self, PyObject *args)
 {
 PyObject *
 Py_aubio_window(PyObject *self, PyObject *args)
 {
-  PyObject *output = NULL;
   char_t *wintype = NULL;
   uint_t winlen = 0;
   char_t *wintype = NULL;
   uint_t winlen = 0;
-  fvec_t *window;
+  fvec_t *window = NULL;
 
 
-  if (!PyArg_ParseTuple (args, "|sd", &wintype, &winlen)) {
-    PyErr_SetString (PyExc_ValueError,
-        "failed parsing arguments");
+  if (!PyArg_ParseTuple (args, "|sI", &wintype, &winlen)) {
+    PyErr_SetString (PyExc_ValueError, "failed parsing arguments");
     return NULL;
   }
 
     return NULL;
   }
 
-  //return (PyObject *) PyAubio_CFvecToArray(vec);
-  Py_RETURN_NONE;
+  window = new_aubio_window(wintype, winlen);
+  if (window == NULL) {
+    PyErr_SetString (PyExc_ValueError, "failed computing window");
+    return NULL;
+  }
+
+  return (PyObject *) PyAubio_CFvecToArray(window);
 }
 }
index ed15a02..bdf85ab 100644 (file)
@@ -9,7 +9,9 @@ static char Py_aubio_window_doc[] = ""
 "Example\n"
 "-------\n"
 "\n"
 "Example\n"
 "-------\n"
 "\n"
-">>> window('hanningz', 1024)";
+">>> window('hanningz', 1024)\n"
+"array([  0.00000000e+00,   9.41753387e-06,   3.76403332e-05, ...,\n"
+"         8.46982002e-05,   3.76403332e-05,   9.41753387e-06], dtype=float32)";
 
 PyObject * Py_aubio_window(PyObject *self, PyObject *args);
 
 
 PyObject * Py_aubio_window(PyObject *self, PyObject *args);
 
index 15282ae..ece079c 100755 (executable)
@@ -1,6 +1,7 @@
 #! /usr/bin/env python
 
 from numpy.testing import TestCase
 #! /usr/bin/env python
 
 from numpy.testing import TestCase
+from numpy.testing.utils import assert_almost_equal
 from aubio import window
 
 class aubio_window(TestCase):
 from aubio import window
 
 class aubio_window(TestCase):
@@ -24,6 +25,14 @@ class aubio_window(TestCase):
         else:
             self.fail('non-integer window length does not raise a ValueError')
 
         else:
             self.fail('non-integer window length does not raise a ValueError')
 
+    def test_compute_hanning_1024(self):
+        from numpy import cos, arange
+        from math import pi
+        size = 1024
+        aubio_window = window("hanning", size)
+        numpy_window = .5 - .5 * cos(2. * pi * arange(size) / size)
+        assert_almost_equal(aubio_window, numpy_window)
+
 if __name__ == '__main__':
     from unittest import main
     main()
 if __name__ == '__main__':
     from unittest import main
     main()