Merge branch 'jack-midi-buffer-fix' of https://github.com/cyclopsian/aubio into fix...
authorPaul Brossier <piem@piem.org>
Mon, 17 Sep 2018 11:47:22 +0000 (13:47 +0200)
committerPaul Brossier <piem@piem.org>
Mon, 17 Sep 2018 11:47:22 +0000 (13:47 +0200)
23 files changed:
README.md
doc/statuslinks.rst
examples/utils.h
python/ext/aubiomodule.c
python/ext/py-musicutils.c
python/ext/py-musicutils.h
python/lib/gen_code.py
python/lib/gen_external.py
python/tests/test_dct.py [new file with mode: 0755]
python/tests/test_fvec_shift.py [new file with mode: 0644]
scripts/get_waf.sh
src/aubio.h
src/aubio_priv.h
src/io/source_avcodec.c
src/spectral/dct.c [new file with mode: 0644]
src/spectral/dct.h [new file with mode: 0644]
src/spectral/dct_accelerate.c [new file with mode: 0644]
src/spectral/dct_fftw.c [new file with mode: 0644]
src/spectral/dct_ipp.c [new file with mode: 0644]
src/spectral/dct_ooura.c [new file with mode: 0644]
src/spectral/dct_plain.c [new file with mode: 0644]
src/spectral/mfcc.c
tests/src/spectral/test-dct.c [new file with mode: 0644]

index b3ca16f..5f6cfbd 100644 (file)
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ aubio
 [![Travis build status](https://travis-ci.org/aubio/aubio.svg?branch=master)](https://travis-ci.org/aubio/aubio "Travis build status")
 [![Appveyor build status](https://img.shields.io/appveyor/ci/piem/aubio/master.svg)](https://ci.appveyor.com/project/piem/aubio "Appveyor build status")
 [![Landscape code health](https://landscape.io/github/aubio/aubio/master/landscape.svg?style=flat)](https://landscape.io/github/aubio/aubio/master "Landscape code health")
-[![Commits since last release](https://img.shields.io/github/commits-since/aubio/aubio/0.4.6.svg)](https://github.com/aubio/aubio "Commits since last release")
+[![Commits since last release](https://img.shields.io/github/commits-since/aubio/aubio/latest.svg)](https://github.com/aubio/aubio "Commits since last release")
 
 [![Documentation](https://readthedocs.org/projects/aubio/badge/?version=latest)](http://aubio.readthedocs.io/en/latest/?badge=latest "Latest documentation")
 [![DOI](https://zenodo.org/badge/396389.svg)](https://zenodo.org/badge/latestdoi/396389)
index 4bb540e..5be7ffb 100644 (file)
@@ -17,7 +17,7 @@ Current status
    :target: https://aubio.readthedocs.io/en/latest/?badge=latest
    :alt: Documentation status
 
-.. image:: https://img.shields.io/github/commits-since/aubio/aubio/0.4.6.svg?maxAge=2592000
+.. image:: https://img.shields.io/github/commits-since/aubio/aubio/latest.svg
    :target: https://github.com/aubio/aubio
    :alt: Commits since last release
 
index e911bef..5cc9ef9 100644 (file)
@@ -66,7 +66,7 @@ typedef void (aubio_print_func_t) (void);
 void send_noteon (smpl_t pitch, smpl_t velo);
 
 /** common process function */
-typedef int (*aubio_process_func_t) (fvec_t * input, fvec_t * output);
+typedef void (*aubio_process_func_t) (fvec_t * input, fvec_t * output);
 
 void process_block (fvec_t *ibuf, fvec_t *obuf);
 void process_print (void);
index 76ed9c9..e97c3d3 100644 (file)
@@ -242,6 +242,8 @@ static PyMethodDef aubio_methods[] = {
   {"silence_detection", Py_aubio_silence_detection, METH_VARARGS, Py_aubio_silence_detection_doc},
   {"level_detection", Py_aubio_level_detection, METH_VARARGS, Py_aubio_level_detection_doc},
   {"window", Py_aubio_window, METH_VARARGS, Py_aubio_window_doc},
+  {"shift", Py_aubio_shift, METH_VARARGS, Py_aubio_shift_doc},
+  {"ishift", Py_aubio_ishift, METH_VARARGS, Py_aubio_ishift_doc},
   {NULL, NULL, 0, NULL} /* Sentinel */
 };
 
index 3078e07..a0d450a 100644 (file)
@@ -133,3 +133,51 @@ Py_aubio_level_detection(PyObject *self, PyObject *args)
 
   return level_detection;
 }
+
+PyObject *
+Py_aubio_shift(PyObject *self, PyObject *args)
+{
+  PyObject *input;
+  fvec_t vec;
+
+  if (!PyArg_ParseTuple (args, "O:shift", &input)) {
+    return NULL;
+  }
+
+  if (input == NULL) {
+    return NULL;
+  }
+
+  if (!PyAubio_ArrayToCFvec(input, &vec)) {
+    return NULL;
+  }
+
+  fvec_shift(&vec);
+
+  //Py_RETURN_NONE;
+  return (PyObject *) PyAubio_CFvecToArray(&vec);
+}
+
+PyObject *
+Py_aubio_ishift(PyObject *self, PyObject *args)
+{
+  PyObject *input;
+  fvec_t vec;
+
+  if (!PyArg_ParseTuple (args, "O:shift", &input)) {
+    return NULL;
+  }
+
+  if (input == NULL) {
+    return NULL;
+  }
+
+  if (!PyAubio_ArrayToCFvec(input, &vec)) {
+    return NULL;
+  }
+
+  fvec_ishift(&vec);
+
+  //Py_RETURN_NONE;
+  return (PyObject *) PyAubio_CFvecToArray(&vec);
+}
index 54ee230..cbe94ae 100644 (file)
@@ -71,4 +71,34 @@ static char Py_aubio_level_detection_doc[] = ""
 
 PyObject * Py_aubio_level_detection(PyObject *self, PyObject *args);
 
+static char Py_aubio_shift_doc[] = ""
+"Swap left and right partitions of a vector\n"
+"\n"
+"Returns the swapped vector. The input vector is also modified.\n"
+"\n"
+"For a vector of length N, the partition is split at index N - N//2.\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> import numpy\n"
+">>> shift(numpy.arange(3, dtype=aubio.float_type))\n"
+"array([2., 0., 1.], dtype=" AUBIO_NPY_SMPL_STR ")";
+PyObject * Py_aubio_shift(PyObject *self, PyObject *args);
+
+static char Py_aubio_ishift_doc[] = ""
+"Swap right and left partitions of a vector\n"
+"\n"
+"Returns the swapped vector. The input vector is also modified.\n"
+"\n"
+"Unlike with shift(), the partition is split at index N//2.\n"
+"\n"
+"Example\n"
+"-------\n"
+"\n"
+">>> import numpy\n"
+">>> ishift(numpy.arange(3, dtype=aubio.float_type))\n"
+"array([1., 2., 0.], dtype=" AUBIO_NPY_SMPL_STR ")";
+PyObject * Py_aubio_ishift(PyObject *self, PyObject *args);
+
 #endif /* PY_AUBIO_MUSICUTILS_H */
index d391197..7527fe9 100644 (file)
@@ -2,6 +2,7 @@ aubiodefvalue = {
     # we have some clean up to do
     'buf_size': 'Py_default_vector_length',
     'win_s': 'Py_default_vector_length',
+    'size': 'Py_default_vector_length',
     # and here too
     'hop_size': 'Py_default_vector_length / 2',
     'hop_s': 'Py_default_vector_length / 2',
@@ -82,6 +83,7 @@ objoutsize = {
         'tempo': '1',
         'filterbank': 'self->n_filters',
         'tss': 'self->buf_size',
+        'dct': 'self->size',
         }
 
 objinputsize = {
@@ -176,6 +178,10 @@ class MappedObject(object):
         self.do_inputs = [get_params_types_names(self.do_proto)[1]]
         self.do_outputs = get_params_types_names(self.do_proto)[2:]
         struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs]
+        if len(self.prototypes['rdo']):
+            rdo_outputs = get_params_types_names(prototypes['rdo'][0])[2:]
+            struct_output_str += ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in rdo_outputs]
+            self.outputs += rdo_outputs
         self.struct_outputs = ";\n    ".join(struct_output_str)
 
         #print ("input_params: ", map(split_type, get_input_params(self.do_proto)))
@@ -190,6 +196,11 @@ class MappedObject(object):
             out += self.gen_init()
             out += self.gen_del()
             out += self.gen_do()
+            if len(self.prototypes['rdo']):
+                self.do_proto = self.prototypes['rdo'][0]
+                self.do_inputs = [get_params_types_names(self.do_proto)[1]]
+                self.do_outputs = get_params_types_names(self.do_proto)[2:]
+                out += self.gen_do(method='rdo')
             out += self.gen_memberdef()
             out += self.gen_set()
             out += self.gen_get()
@@ -370,12 +381,12 @@ Py_{shortname}_del  (Py_{shortname} * self, PyObject * unused)
 """.format(del_fn = del_fn)
         return out
 
-    def gen_do(self):
+    def gen_do(self, method = 'do'):
         out = """
 // do {shortname}
 static PyObject*
-Py_{shortname}_do  (Py_{shortname} * self, PyObject * args)
-{{""".format(**self.__dict__)
+Pyaubio_{shortname}_{method}  (Py_{shortname} * self, PyObject * args)
+{{""".format(method = method, **self.__dict__)
         input_params = self.do_inputs
         output_params = self.do_outputs
         #print input_params
@@ -515,6 +526,12 @@ static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__)
             out += """
   {{"{shortname}", (PyCFunction) Py{name},
     METH_NOARGS, ""}},""".format(name = name, shortname = shortname)
+        for m in self.prototypes['rdo']:
+            name = get_name(m)
+            shortname = name.replace('aubio_%s_' % self.shortname, '')
+            out += """
+  {{"{shortname}", (PyCFunction) Py{name},
+    METH_VARARGS, ""}},""".format(name = name, shortname = shortname)
         out += """
   {NULL} /* sentinel */
 };
@@ -540,7 +557,7 @@ PyTypeObject Py_{shortname}Type = {{
   0,
   0,
   0,
-  (ternaryfunc)Py_{shortname}_do,
+  (ternaryfunc)Pyaubio_{shortname}_do,
   0,
   0,
   0,
index 8095cb6..1e35bfd 100644 (file)
@@ -181,7 +181,7 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations):
         if o[:6] == 'aubio_':
             shortname = o[6:-2]  # without aubio_ prefix and _t suffix
 
-        lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'get': [], 'set': [], 'other': []}
+        lib[shortname] = {'struct': [], 'new': [], 'del': [], 'do': [], 'rdo': [], 'get': [], 'set': [], 'other': []}
         lib[shortname]['longname'] = o
         lib[shortname]['shortname'] = shortname
 
@@ -195,6 +195,8 @@ def generate_lib_from_c_declarations(cpp_objects, c_declarations):
                     lib[shortname]['struct'].append(fn)
                 elif '_do' in fn:
                     lib[shortname]['do'].append(fn)
+                elif '_rdo' in fn:
+                    lib[shortname]['rdo'].append(fn)
                 elif 'new_' in fn:
                     lib[shortname]['new'].append(fn)
                 elif 'del_' in fn:
diff --git a/python/tests/test_dct.py b/python/tests/test_dct.py
new file mode 100755 (executable)
index 0000000..0c990f9
--- /dev/null
@@ -0,0 +1,68 @@
+#! /usr/bin/env python
+
+
+import numpy as np
+from numpy.testing import TestCase, assert_almost_equal
+import aubio
+
+precomputed_arange = [ 9.89949512, -6.44232273,  0., -0.67345482, 0.,
+        -0.20090288, 0., -0.05070186]
+
+precomputed_some_ones = [ 4.28539848,  0.2469689,  -0.14625292, -0.58121818,
+        -0.83483052, -0.75921834, -0.35168475,  0.24087936,
+        0.78539824, 1.06532764,  0.97632152,  0.57164496, 0.03688532,
+        -0.39446154, -0.54619485, -0.37771079]
+
+class aubio_dct(TestCase):
+
+    def test_init(self):
+        """ test that aubio.dct() is created with expected size """
+        a_dct = aubio.dct()
+        self.assertEqual(a_dct.size, 1024)
+
+    def test_arange(self):
+        """ test that dct(arange(8)) is computed correctly
+
+        >>> from scipy.fftpack import dct
+        >>> a_in = np.arange(8).astype(aubio.float_type)
+        >>> precomputed = dct(a_in, norm='ortho')
+        """
+        N = len(precomputed_arange)
+        a_dct = aubio.dct(8)
+        a_in = np.arange(8).astype(aubio.float_type)
+        a_expected = aubio.fvec(precomputed_arange)
+        assert_almost_equal(a_dct(a_in), a_expected, decimal=5)
+
+    def test_some_ones(self):
+        """ test that dct(somevector) is computed correctly """
+        a_dct = aubio.dct(16)
+        a_in = np.ones(16).astype(aubio.float_type)
+        a_in[1] = 0
+        a_in[3] = np.pi
+        a_expected = aubio.fvec(precomputed_some_ones)
+        assert_almost_equal(a_dct(a_in), a_expected, decimal=6)
+
+    def test_reconstruction(self):
+        """ test that some_ones vector can be recontructed """
+        a_dct = aubio.dct(16)
+        a_in = np.ones(16).astype(aubio.float_type)
+        a_in[1] = 0
+        a_in[3] = np.pi
+        a_dct_in = a_dct(a_in)
+        a_dct_reconstructed = a_dct.rdo(a_dct_in)
+        assert_almost_equal(a_dct_reconstructed, a_in, decimal=6)
+
+    def test_negative_size(self):
+        """ test that creation fails with a negative size """
+        with self.assertRaises(ValueError):
+            aubio.dct(-1)
+
+    def test_wrong_size(self):
+        """ test that creation fails with a non power-of-two size """
+        # supports for non 2** fft sizes only when compiled with fftw3
+        size = 13
+        try:
+            with self.assertRaises(RuntimeError):
+                aubio.dct(size)
+        except AssertionError:
+            self.skipTest('creating aubio.dct with size %d did not fail' % size)
diff --git a/python/tests/test_fvec_shift.py b/python/tests/test_fvec_shift.py
new file mode 100644 (file)
index 0000000..929fd56
--- /dev/null
@@ -0,0 +1,35 @@
+#! /usr/bin/env python
+
+import numpy as np
+from numpy.testing import TestCase, assert_equal
+import aubio
+
+class aubio_shift_test_case(TestCase):
+
+    def run_shift_ishift(self, n):
+        ramp = np.arange(n, dtype=aubio.float_type)
+        # construct expected output
+        # even length: [5. 6. 7. 8. 9. 0. 1. 2. 3. 4.]
+        # odd length: [4. 5. 6. 0. 1. 2. 3.]
+        half = n - n//2
+        expected = np.concatenate([np.arange(half, n), np.arange(half)])
+        # shift in place, returns modified copy
+        assert_equal(aubio.shift(ramp), expected)
+        # check input was changed as expected
+        assert_equal(ramp, expected)
+        # construct expected output
+        expected = np.arange(n)
+        # revert shift in place, returns modifed copy
+        assert_equal(aubio.ishift(ramp), expected)
+        # check input was shifted back
+        assert_equal(ramp, expected)
+
+    def test_can_shift_fvec(self):
+        self.run_shift_ishift(10)
+
+    def test_can_shift_fvec_odd(self):
+        self.run_shift_ishift(7)
+
+from unittest import main
+if __name__ == '__main__':
+    main()
index 99f0cb1..e24ad24 100755 (executable)
@@ -3,7 +3,7 @@
 set -e
 set -x
 
-WAFVERSION=2.0.1
+WAFVERSION=2.0.11
 WAFTARBALL=waf-$WAFVERSION.tar.bz2
 WAFURL=https://waf.io/$WAFTARBALL
 
index 0d02136..73deb24 100644 (file)
@@ -182,6 +182,7 @@ extern "C"
 #include "temporal/a_weighting.h"
 #include "temporal/c_weighting.h"
 #include "spectral/fft.h"
+#include "spectral/dct.h"
 #include "spectral/phasevoc.h"
 #include "spectral/filterbank.h"
 #include "spectral/filterbank_mel.h"
index 65a3d8a..1e9881e 100644 (file)
@@ -85,6 +85,8 @@
 #ifndef HAVE_AUBIO_DOUBLE
 #define aubio_vDSP_mmov       vDSP_mmov
 #define aubio_vDSP_vmul       vDSP_vmul
+#define aubio_vDSP_vsmul      vDSP_vsmul
+#define aubio_vDSP_vsadd      vDSP_vsadd
 #define aubio_vDSP_vfill      vDSP_vfill
 #define aubio_vDSP_meanv      vDSP_meanv
 #define aubio_vDSP_sve        vDSP_sve
@@ -97,6 +99,8 @@
 #else /* HAVE_AUBIO_DOUBLE */
 #define aubio_vDSP_mmov       vDSP_mmovD
 #define aubio_vDSP_vmul       vDSP_vmulD
+#define aubio_vDSP_vsmul      vDSP_vsmulD
+#define aubio_vDSP_vsadd      vDSP_vsaddD
 #define aubio_vDSP_vfill      vDSP_vfillD
 #define aubio_vDSP_meanv      vDSP_meanvD
 #define aubio_vDSP_sve        vDSP_sveD
index 2186858..3631d44 100644 (file)
@@ -143,8 +143,10 @@ aubio_source_avcodec_t * new_aubio_source_avcodec(const char_t * path, uint_t sa
   s->path = AUBIO_ARRAY(char_t, strnlen(path, PATH_MAX) + 1);
   strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
 
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,0,0)
   // register all formats and codecs
   av_register_all();
+#endif
 
   if (aubio_source_avcodec_has_network_url(s)) {
     avformat_network_init();
diff --git a/src/spectral/dct.c b/src/spectral/dct.c
new file mode 100644 (file)
index 0000000..1a2e510
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+/** \file
+
+  Discrete Cosine Transform
+
+  Functions aubio_dct_do() and aubio_dct_rdo() are equivalent to MATLAB/Octave
+  dct() and idct() functions, as well as scipy.fftpack.dct(x, norm='ortho') and
+  scipy.fftpack.idct(x, norm='ortho')
+
+  \example spectral/test-dct.c
+
+*/
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "spectral/dct.h"
+
+// function pointers prototypes
+typedef void (*aubio_dct_do_t)(aubio_dct_t * s, const fvec_t * input, fvec_t * output);
+typedef void (*aubio_dct_rdo_t)(aubio_dct_t * s, const fvec_t * input, fvec_t * output);
+typedef void (*del_aubio_dct_t)(aubio_dct_t * s);
+
+#if defined(HAVE_ACCELERATE)
+typedef struct _aubio_dct_accelerate_t aubio_dct_accelerate_t;
+extern aubio_dct_accelerate_t * new_aubio_dct_accelerate (uint_t size);
+extern void aubio_dct_accelerate_do(aubio_dct_accelerate_t *s, const fvec_t *input, fvec_t *output);
+extern void aubio_dct_accelerate_rdo(aubio_dct_accelerate_t *s, const fvec_t *input, fvec_t *output);
+extern void del_aubio_dct_accelerate (aubio_dct_accelerate_t *s);
+#elif defined(HAVE_FFTW3)
+typedef struct _aubio_dct_fftw_t aubio_dct_fftw_t;
+extern aubio_dct_fftw_t * new_aubio_dct_fftw (uint_t size);
+extern void aubio_dct_fftw_do(aubio_dct_fftw_t *s, const fvec_t *input, fvec_t *output);
+extern void aubio_dct_fftw_rdo(aubio_dct_fftw_t *s, const fvec_t *input, fvec_t *output);
+extern void del_aubio_dct_fftw (aubio_dct_fftw_t *s);
+#elif defined(HAVE_INTEL_IPP)
+typedef struct _aubio_dct_ipp_t aubio_dct_ipp_t;
+extern aubio_dct_ipp_t * new_aubio_dct_ipp (uint_t size);
+extern void aubio_dct_ipp_do(aubio_dct_ipp_t *s, const fvec_t *input, fvec_t *output);
+extern void aubio_dct_ipp_rdo(aubio_dct_ipp_t *s, const fvec_t *input, fvec_t *output);
+extern void del_aubio_dct_ipp (aubio_dct_ipp_t *s);
+#else
+typedef struct _aubio_dct_ooura_t aubio_dct_ooura_t;
+extern aubio_dct_ooura_t * new_aubio_dct_ooura (uint_t size);
+extern void aubio_dct_ooura_do(aubio_dct_ooura_t *s, const fvec_t *input, fvec_t *output);
+extern void aubio_dct_ooura_rdo(aubio_dct_ooura_t *s, const fvec_t *input, fvec_t *output);
+extern void del_aubio_dct_ooura (aubio_dct_ooura_t *s);
+#endif
+
+// plain mode
+typedef struct _aubio_dct_plain_t aubio_dct_plain_t;
+extern aubio_dct_plain_t * new_aubio_dct_plain (uint_t size);
+extern void aubio_dct_plain_do(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output);
+extern void aubio_dct_plain_rdo(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output);
+extern void del_aubio_dct_plain (aubio_dct_plain_t *s);
+
+struct _aubio_dct_t {
+  void *dct;
+  aubio_dct_do_t dct_do;
+  aubio_dct_rdo_t dct_rdo;
+  del_aubio_dct_t del_dct;
+};
+
+aubio_dct_t* new_aubio_dct (uint_t size) {
+  aubio_dct_t * s = AUBIO_NEW(aubio_dct_t);
+  if ((sint_t)size <= 0) goto beach;
+#if defined(HAVE_ACCELERATE)
+  // vDSP supports sizes = f * 2 ** n, where n >= 4 and f in [1, 3, 5, 15]
+  // see https://developer.apple.com/documentation/accelerate/1449930-vdsp_dct_createsetup
+  {
+    uint_t radix = size;
+    uint_t order = 0;
+    while ((radix / 2) * 2 == radix) {
+      radix /= 2;
+      order++;
+    }
+    if (order < 4 || (radix != 1 && radix != 3 && radix != 5 && radix != 15)) {
+      goto plain;
+    }
+  }
+  s->dct = (void *)new_aubio_dct_accelerate (size);
+  if (s->dct) {
+    s->dct_do = (aubio_dct_do_t)aubio_dct_accelerate_do;
+    s->dct_rdo = (aubio_dct_rdo_t)aubio_dct_accelerate_rdo;
+    s->del_dct = (del_aubio_dct_t)del_aubio_dct_accelerate;
+    return s;
+  }
+#elif defined(HAVE_FFTW3)
+  // fftw supports any positive integer size
+  s->dct = (void *)new_aubio_dct_fftw (size);
+  if (s->dct) {
+    s->dct_do = (aubio_dct_do_t)aubio_dct_fftw_do;
+    s->dct_rdo = (aubio_dct_rdo_t)aubio_dct_fftw_rdo;
+    s->del_dct = (del_aubio_dct_t)del_aubio_dct_fftw;
+    return s;
+  } else {
+    AUBIO_WRN("dct: unexcepected error while creating dct_fftw with size %d",
+        size);
+    goto plain;
+  }
+#elif defined(HAVE_INTEL_IPP)
+  // unclear from the docs, but intel ipp seems to support any size
+  s->dct = (void *)new_aubio_dct_ipp (size);
+  if (s->dct) {
+    s->dct_do = (aubio_dct_do_t)aubio_dct_ipp_do;
+    s->dct_rdo = (aubio_dct_rdo_t)aubio_dct_ipp_rdo;
+    s->del_dct = (del_aubio_dct_t)del_aubio_dct_ipp;
+    return s;
+  } else {
+    AUBIO_WRN("dct: unexcepected error while creating dct_ipp with size %d",
+        size);
+    goto plain;
+  }
+#else
+  // ooura support sizes that are power of 2
+  if (aubio_is_power_of_two(size) != 1 || size == 1) {
+    goto plain;
+  }
+  s->dct = (void *)new_aubio_dct_ooura (size);
+  if (s->dct) {
+    s->dct_do = (aubio_dct_do_t)aubio_dct_ooura_do;
+    s->dct_rdo = (aubio_dct_rdo_t)aubio_dct_ooura_rdo;
+    s->del_dct = (del_aubio_dct_t)del_aubio_dct_ooura;
+    return s;
+  }
+#endif
+  // falling back to plain mode
+  AUBIO_WRN("dct: d no optimised implementation could be created for size %d",
+      size);
+plain:
+  s->dct = (void *)new_aubio_dct_plain (size);
+  if (s->dct) {
+    s->dct_do = (aubio_dct_do_t)aubio_dct_plain_do;
+    s->dct_rdo = (aubio_dct_rdo_t)aubio_dct_plain_rdo;
+    s->del_dct = (del_aubio_dct_t)del_aubio_dct_plain;
+    return s;
+  } else {
+    goto beach;
+  }
+beach:
+  AUBIO_ERROR("dct: failed creating with size %d, should be > 0", size);
+  AUBIO_FREE (s);
+  return NULL;
+}
+
+void del_aubio_dct(aubio_dct_t *s) {
+  if (s->dct && s->del_dct) s->del_dct (s->dct);
+  AUBIO_FREE (s);
+}
+
+void aubio_dct_do(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
+  s->dct_do ((void *)s->dct, input, output);
+}
+
+void aubio_dct_rdo(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
+  s->dct_rdo ((void *)s->dct, input, output);
+}
diff --git a/src/spectral/dct.h b/src/spectral/dct.h
new file mode 100644 (file)
index 0000000..6ceb006
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+  Copyright (C) 2017 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+/** \file
+
+  Discrete Cosine Transform
+
+  Functions aubio_dct_do() and aubio_dct_rdo() are equivalent to MATLAB/Octave
+  dct() and idct() functions, as well as scipy.fftpack.dct(x, norm='ortho') and
+  scipy.fftpack.idct(x, norm='ortho')
+
+  \example spectral/test-dct.c
+
+*/
+
+#ifndef AUBIO_DCT_H
+#define AUBIO_DCT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** DCT object
+
+  This object computes forward and backward DCT type 2 with orthonormal
+  scaling.
+
+*/
+typedef struct _aubio_dct_t aubio_dct_t;
+
+/** create new DCT computation object
+
+  \param size length of the DCT
+
+*/
+aubio_dct_t * new_aubio_dct(uint_t size);
+
+/** compute forward DCT
+
+  \param s dct object as returned by new_aubio_dct
+  \param input input signal
+  \param dct_output transformed input array
+
+*/
+void aubio_dct_do (aubio_dct_t *s, const fvec_t * input, fvec_t * dct_output);
+
+/** compute backward DCT
+
+  \param s dct object as returned by new_aubio_dct
+  \param input input signal
+  \param idct_output transformed input array
+
+*/
+void aubio_dct_rdo (aubio_dct_t *s, const fvec_t * input, fvec_t * idct_output);
+
+
+/** delete DCT object
+
+  \param s dct object as returned by new_aubio_dct
+
+*/
+void del_aubio_dct (aubio_dct_t *s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* AUBIO_DCT_H */
diff --git a/src/spectral/dct_accelerate.c b/src/spectral/dct_accelerate.c
new file mode 100644 (file)
index 0000000..4765c0d
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+  Copyright (C) 2017 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "spectral/dct.h"
+
+#if defined(HAVE_ACCELERATE)
+
+#if HAVE_AUBIO_DOUBLE
+#warning "no double-precision dct with accelerate"
+#endif
+
+struct _aubio_dct_accelerate_t {
+  uint_t size;
+  fvec_t *tmp;
+  vDSP_DFT_Setup setup;
+  vDSP_DFT_Setup setupInv;
+};
+
+typedef struct _aubio_dct_accelerate_t aubio_dct_accelerate_t;
+
+void del_aubio_dct_accelerate (aubio_dct_accelerate_t *s);
+
+aubio_dct_accelerate_t * new_aubio_dct_accelerate (uint_t size) {
+  aubio_dct_accelerate_t * s = AUBIO_NEW(aubio_dct_accelerate_t);
+
+  if ((sint_t)size < 16 || !aubio_is_power_of_two(size)) {
+    AUBIO_ERR("dct: can only create with sizes greater than 16 and"
+        " that are powers of two, requested %d\n", size);
+    goto beach;
+  }
+
+  s->setup = vDSP_DCT_CreateSetup(NULL, (vDSP_Length)size, vDSP_DCT_II);
+  s->setupInv = vDSP_DCT_CreateSetup(NULL, (vDSP_Length)size, vDSP_DCT_III);
+  if (s->setup == NULL || s->setupInv == NULL) {
+    goto beach;
+  }
+
+  s->size = size;
+
+  return s;
+
+beach:
+  del_aubio_dct_accelerate(s);
+  return NULL;
+}
+
+void del_aubio_dct_accelerate(aubio_dct_accelerate_t *s) {
+  if (s->setup) vDSP_DFT_DestroySetup(s->setup);
+  if (s->setupInv) vDSP_DFT_DestroySetup(s->setupInv);
+  AUBIO_FREE(s);
+}
+
+void aubio_dct_accelerate_do(aubio_dct_accelerate_t *s, const fvec_t *input, fvec_t *output) {
+
+  vDSP_DCT_Execute(s->setup, (const float *)input->data, (float *)output->data);
+
+  // apply orthonormal scaling
+  output->data[0] *= SQRT(1./s->size);
+  smpl_t scaler = SQRT(2./s->size);
+
+  aubio_vDSP_vsmul(output->data + 1, 1, &scaler, output->data + 1, 1,
+      output->length - 1);
+
+}
+
+void aubio_dct_accelerate_rdo(aubio_dct_accelerate_t *s, const fvec_t *input, fvec_t *output) {
+
+  output->data[0] = input->data[0] / SQRT(1./s->size);
+  smpl_t scaler = 1./SQRT(2./s->size);
+
+  aubio_vDSP_vsmul(input->data + 1, 1, &scaler, output->data + 1, 1,
+      output->length - 1);
+
+  vDSP_DCT_Execute(s->setupInv, (const float *)output->data,
+      (float *)output->data);
+
+  scaler = 2./s->size;
+
+  aubio_vDSP_vsmul(output->data, 1, &scaler, output->data, 1, output->length);
+
+}
+
+#endif //defined(HAVE_ACCELERATE)
diff --git a/src/spectral/dct_fftw.c b/src/spectral/dct_fftw.c
new file mode 100644 (file)
index 0000000..2a4fa7a
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+  Copyright (C) 2017 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "spectral/dct.h"
+
+#ifdef HAVE_FFTW3
+
+#include <fftw3.h>
+#include <pthread.h>
+
+#ifdef HAVE_FFTW3F
+#if HAVE_AUBIO_DOUBLE
+#error "Using aubio in double precision with fftw3 in single precision"
+#endif /* HAVE_AUBIO_DOUBLE */
+#else  /* HAVE_FFTW3F */
+#if !HAVE_AUBIO_DOUBLE
+#error "Using aubio in single precision with fftw3 in double precision"
+#endif /* HAVE_AUBIO_DOUBLE */
+#endif /* HAVE_FFTW3F */
+
+#ifdef HAVE_FFTW3F
+#define fftw_malloc            fftwf_malloc
+#define fftw_free              fftwf_free
+#define fftw_execute           fftwf_execute
+#define fftw_plan_dft_r2c_1d   fftwf_plan_dft_r2c_1d
+#define fftw_plan_dft_c2r_1d   fftwf_plan_dft_c2r_1d
+#define fftw_plan_r2r_1d       fftwf_plan_r2r_1d
+#define fftw_plan              fftwf_plan
+#define fftw_destroy_plan      fftwf_destroy_plan
+#endif
+
+// defined in src/spectral/fft.c
+extern pthread_mutex_t aubio_fftw_mutex;
+
+typedef struct _aubio_dct_fftw_t aubio_dct_fftw_t;
+
+struct _aubio_dct_fftw_t {
+  uint_t size;
+  fvec_t *in, *out;
+  smpl_t *data;
+  fftw_plan pfw, pbw;
+  smpl_t scalers[5];
+};
+
+aubio_dct_fftw_t * new_aubio_dct_fftw (uint_t size) {
+  aubio_dct_fftw_t * s = AUBIO_NEW(aubio_dct_fftw_t);
+  if (!s) {
+    goto beach;
+  }
+  s->size = size;
+  s->in = new_fvec(size);
+  s->out = new_fvec(size);
+  pthread_mutex_lock(&aubio_fftw_mutex);
+  s->data = (smpl_t *)fftw_malloc(sizeof(smpl_t) * size);
+  s->pfw = fftw_plan_r2r_1d(size, s->in->data,  s->data, FFTW_REDFT10,
+      FFTW_ESTIMATE);
+  s->pbw = fftw_plan_r2r_1d(size, s->data, s->out->data, FFTW_REDFT01,
+      FFTW_ESTIMATE);
+  pthread_mutex_unlock(&aubio_fftw_mutex);
+  s->scalers[0] = SQRT(1./(4.*s->size));
+  s->scalers[1] = SQRT(1./(2.*s->size));
+  s->scalers[2] = 1. / s->scalers[0];
+  s->scalers[3] = 1. / s->scalers[1];
+  s->scalers[4] = .5 / s->size;
+  return s;
+beach:
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+void del_aubio_dct_fftw(aubio_dct_fftw_t *s) {
+  pthread_mutex_lock(&aubio_fftw_mutex);
+  fftw_destroy_plan(s->pfw);
+  fftw_destroy_plan(s->pbw);
+  fftw_free(s->data);
+  pthread_mutex_unlock(&aubio_fftw_mutex);
+  del_fvec(s->in);
+  del_fvec(s->out);
+  AUBIO_FREE(s);
+}
+
+void aubio_dct_fftw_do(aubio_dct_fftw_t *s, const fvec_t *input, fvec_t *output) {
+  uint_t i;
+  fvec_copy(input, s->in);
+  fftw_execute(s->pfw);
+  //fvec_copy(s->out, output);
+  s->data[0] *= s->scalers[0];
+  for (i = 1; i < s->size; i++) {
+    s->data[i] *= s->scalers[1];
+  }
+  memcpy(output->data, s->data, output->length * sizeof(smpl_t));
+}
+
+void aubio_dct_fftw_rdo(aubio_dct_fftw_t *s, const fvec_t *input, fvec_t *output) {
+  uint_t i;
+  memcpy(s->data, input->data, input->length * sizeof(smpl_t));
+  //s->data[0] *= .5;
+  s->data[0] *= s->scalers[2];
+  for (i = 1; i < s->size; i++) {
+    s->data[i] *= s->scalers[3];
+  }
+  fftw_execute(s->pbw);
+  for (i = 0; i < s->size; i++) {
+    s->out->data[i] *= s->scalers[4];
+  }
+  fvec_copy(s->out, output);
+}
+
+#endif //HAVE_FFTW3
diff --git a/src/spectral/dct_ipp.c b/src/spectral/dct_ipp.c
new file mode 100644 (file)
index 0000000..273aa00
--- /dev/null
@@ -0,0 +1,150 @@
+/*
+  Copyright (C) 2017 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "spectral/dct.h"
+
+#if defined(HAVE_INTEL_IPP)
+
+#if !HAVE_AUBIO_DOUBLE
+#define aubio_IppFloat                 Ipp32f
+#define aubio_ippsDCTFwdSpec           IppsDCTFwdSpec_32f
+#define aubio_ippsDCTInvSpec           IppsDCTInvSpec_32f
+#define aubio_ippsDCTFwdGetSize        ippsDCTFwdGetSize_32f
+#define aubio_ippsDCTInvGetSize        ippsDCTInvGetSize_32f
+#define aubio_ippsDCTFwdInit           ippsDCTFwdInit_32f
+#define aubio_ippsDCTInvInit           ippsDCTInvInit_32f
+#define aubio_ippsDCTFwd               ippsDCTFwd_32f
+#define aubio_ippsDCTInv               ippsDCTInv_32f
+#else /* HAVE_AUBIO_DOUBLE */
+#define aubio_IppFloat                 Ipp64f
+#define aubio_ippsDCTFwdSpec           IppsDCTFwdSpec_64f
+#define aubio_ippsDCTInvSpec           IppsDCTInvSpec_64f
+#define aubio_ippsDCTFwdGetSize        ippsDCTFwdGetSize_64f
+#define aubio_ippsDCTInvGetSize        ippsDCTInvGetSize_64f
+#define aubio_ippsDCTFwdInit           ippsDCTFwdInit_64f
+#define aubio_ippsDCTInvInit           ippsDCTInvInit_64f
+#define aubio_ippsDCTFwd               ippsDCTFwd_64f
+#define aubio_ippsDCTInv               ippsDCTInv_64f
+#endif
+
+typedef struct _aubio_dct_ipp_t aubio_dct_ipp_t;
+
+struct _aubio_dct_ipp_t {
+  uint_t size;
+  Ipp8u* pSpecFwd;
+  Ipp8u* pSpecInv;
+  Ipp8u* pSpecBuffer;
+  Ipp8u* pBuffer;
+  aubio_ippsDCTFwdSpec* pFwdDCTSpec;
+  aubio_ippsDCTInvSpec* pInvDCTSpec;
+};
+
+void del_aubio_dct_ipp (aubio_dct_ipp_t *s);
+
+aubio_dct_ipp_t * new_aubio_dct_ipp (uint_t size) {
+  aubio_dct_ipp_t * s = AUBIO_NEW(aubio_dct_ipp_t);
+
+  const IppHintAlgorithm qualityHint = ippAlgHintAccurate; // ippAlgHintFast;
+  int pSpecSize, pSpecBufferSize, pBufferSize;
+  IppStatus status;
+
+  if ((sint_t)size <= 0) {
+    AUBIO_ERR("dct: can only create with sizes greater than 0, requested %d\n",
+        size);
+    goto beach;
+  }
+
+  status = aubio_ippsDCTFwdGetSize(size, qualityHint, &pSpecSize,
+      &pSpecBufferSize, &pBufferSize);
+  if (status != ippStsNoErr) {
+    AUBIO_ERR("dct: failed to initialize dct. IPP error: %d\n", status);
+    goto beach;
+  }
+
+  //AUBIO_INF("dct: fwd initialized with %d %d %d\n", pSpecSize, pSpecBufferSize,
+  //    pBufferSize);
+
+  s->pSpecFwd = ippsMalloc_8u(pSpecSize);
+  s->pSpecInv = ippsMalloc_8u(pSpecSize);
+  if (pSpecSize > 0) {
+    s->pSpecBuffer = ippsMalloc_8u(pSpecBufferSize);
+  } else {
+    s->pSpecBuffer = NULL;
+  }
+  s->pBuffer = ippsMalloc_8u(pBufferSize);
+
+  status = aubio_ippsDCTInvGetSize(size, qualityHint, &pSpecSize,
+      &pSpecBufferSize, &pBufferSize);
+  if (status != ippStsNoErr) {
+    AUBIO_ERR("dct: failed to initialize dct. IPP error: %d\n", status);
+    goto beach;
+  }
+
+  //AUBIO_INF("dct: inv initialized with %d %d %d\n", pSpecSize, pSpecBufferSize,
+  //    pBufferSize);
+
+  status = aubio_ippsDCTFwdInit(&(s->pFwdDCTSpec), size, qualityHint, s->pSpecFwd,
+      s->pSpecBuffer);
+  if (status != ippStsNoErr) {
+    AUBIO_ERR("dct: failed to initialize fwd dct. IPP error: %d\n", status);
+    goto beach;
+  }
+
+  status = aubio_ippsDCTInvInit(&(s->pInvDCTSpec), size, qualityHint, s->pSpecInv,
+      s->pSpecBuffer);
+  if (status != ippStsNoErr) {
+    AUBIO_ERR("dct: failed to initialize inv dct. IPP error: %d\n", status);
+    goto beach;
+  }
+
+  s->size = size;
+
+  return s;
+
+beach:
+  del_aubio_dct_ipp(s);
+  return NULL;
+}
+
+void del_aubio_dct_ipp(aubio_dct_ipp_t *s) {
+  ippFree(s->pSpecFwd);
+  ippFree(s->pSpecInv);
+  ippFree(s->pSpecBuffer);
+  ippFree(s->pBuffer);
+  AUBIO_FREE(s);
+}
+
+void aubio_dct_ipp_do(aubio_dct_ipp_t *s, const fvec_t *input, fvec_t *output) {
+
+  aubio_ippsDCTFwd((const aubio_IppFloat*)input->data,
+      (aubio_IppFloat*)output->data, s->pFwdDCTSpec, s->pBuffer);
+
+}
+
+void aubio_dct_ipp_rdo(aubio_dct_ipp_t *s, const fvec_t *input, fvec_t *output) {
+
+  aubio_ippsDCTInv((const aubio_IppFloat*)input->data,
+      (aubio_IppFloat*)output->data, s->pInvDCTSpec, s->pBuffer);
+
+}
+
+#endif //defined(HAVE_INTEL_IPP)
diff --git a/src/spectral/dct_ooura.c b/src/spectral/dct_ooura.c
new file mode 100644 (file)
index 0000000..adfc452
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+  Copyright (C) 2017 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "spectral/dct.h"
+
+#if !defined(HAVE_ACCELERATE) && !defined(HAVE_FFTW3) && !defined(HAVE_INTEL_IPP)
+
+typedef struct _aubio_dct_ooura_t aubio_dct_ooura_t;
+
+extern void aubio_ooura_ddct(int, int, smpl_t *, int *, smpl_t *);
+
+struct _aubio_dct_ooura_t {
+  uint_t size;
+  fvec_t *input;
+  smpl_t *w;
+  int *ip;
+  smpl_t scalers[5];
+};
+
+aubio_dct_ooura_t * new_aubio_dct_ooura (uint_t size) {
+  aubio_dct_ooura_t * s = AUBIO_NEW(aubio_dct_ooura_t);
+  if (aubio_is_power_of_two(size) != 1) {
+    AUBIO_ERR("dct: can only create with sizes power of two, requested %d\n",
+        size);
+    goto beach;
+  }
+  s->size = size;
+  s->input = new_fvec(s->size);
+  s->w = AUBIO_ARRAY(smpl_t, s->size * 5 / 4);
+  s->ip = AUBIO_ARRAY(int, 3 + (1 << (int)FLOOR(LOG(s->size/2) / LOG(2))) / 2);
+  s->ip[0] = 0;
+  s->scalers[0] = 2. * SQRT(1./(4.*s->size));
+  s->scalers[1] = 2. * SQRT(1./(2.*s->size));
+  s->scalers[2] = 1. / s->scalers[0];
+  s->scalers[3] = 1. / s->scalers[1];
+  s->scalers[4] = 2. / s->size;
+  return s;
+beach:
+  AUBIO_FREE(s);
+  return NULL;
+}
+
+void del_aubio_dct_ooura(aubio_dct_ooura_t *s) {
+  del_fvec(s->input);
+  AUBIO_FREE(s->ip);
+  AUBIO_FREE(s->w);
+  AUBIO_FREE(s);
+}
+
+void aubio_dct_ooura_do(aubio_dct_ooura_t *s, const fvec_t *input, fvec_t *output) {
+  uint_t i = 0;
+  fvec_copy(input, s->input);
+  aubio_ooura_ddct(s->size, -1, s->input->data, s->ip, s->w);
+  // apply orthonormal scaling
+  s->input->data[0] *= s->scalers[0];
+  for (i = 1; i < s->input->length; i++) {
+    s->input->data[i] *= s->scalers[1];
+  }
+  fvec_copy(s->input, output);
+}
+
+void aubio_dct_ooura_rdo(aubio_dct_ooura_t *s, const fvec_t *input, fvec_t *output) {
+  uint_t i = 0;
+  fvec_copy(input, s->input);
+  s->input->data[0] *= s->scalers[2];
+  for (i = 1; i < s->input->length; i++) {
+    s->input->data[i] *= s->scalers[3];
+  }
+  s->input->data[0] *= .5;
+  aubio_ooura_ddct(s->size, 1, s->input->data, s->ip, s->w);
+  for (i = 0; i < s->input->length; i++) {
+    s->input->data[i] *= s->scalers[4];
+  }
+  fvec_copy(s->input, output);
+}
+
+#endif //!defined(HAVE_ACCELERATE) && !defined(HAVE_FFTW3)
diff --git a/src/spectral/dct_plain.c b/src/spectral/dct_plain.c
new file mode 100644 (file)
index 0000000..d9d1e93
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
+
+  This file is part of aubio.
+
+  aubio is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  aubio is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "aubio_priv.h"
+#include "fvec.h"
+#include "fmat.h"
+#include "spectral/dct.h"
+
+typedef struct _aubio_dct_plain_t aubio_dct_plain_t;
+
+struct _aubio_dct_plain_t {
+  uint_t size;
+  fmat_t *dct_coeffs;       /** DCT type II orthonormal transform, size * size */
+  fmat_t *idct_coeffs;      /** DCT type III orthonormal transform, size * size */
+};
+
+aubio_dct_plain_t * new_aubio_dct_plain (uint_t size) {
+  aubio_dct_plain_t * s = AUBIO_NEW(aubio_dct_plain_t);
+  uint_t i, j;
+  smpl_t scaling;
+  if (aubio_is_power_of_two (size) == 1 && size > 16) {
+    AUBIO_WRN("dct_plain: using plain dct but size %d is a power of two\n", size);
+  }
+
+  s->size = size;
+
+  s->dct_coeffs = new_fmat (size, size);
+  s->idct_coeffs = new_fmat (size, size);
+
+  /* compute DCT type-II transformation matrix
+     dct_coeffs[j][i] = cos ( j * (i+.5) * PI / n_filters )
+  */
+  scaling = SQRT (2. / size);
+  for (i = 0; i < size; i++) {
+    for (j = 1; j < size; j++) {
+      s->dct_coeffs->data[j][i] =
+          scaling * COS (j * (i + 0.5) * PI / size );
+    }
+    s->dct_coeffs->data[0][i] = 1. / SQRT (size);
+  }
+
+  /* compute DCT type-III transformation matrix
+     idct_coeffs[j][i] = cos ( i * (j+.5) * PI / n_filters )
+  */
+  scaling = SQRT (2. / size);
+  for (j = 0; j < size; j++) {
+    for (i = 1; i < size; i++) {
+      s->idct_coeffs->data[j][i] =
+          scaling * COS (i * (j + 0.5) * PI / size );
+    }
+    s->idct_coeffs->data[j][0] = 1. / SQRT (size);
+  }
+  return s;
+}
+
+void del_aubio_dct_plain (aubio_dct_plain_t *s) {
+  del_fmat(s->dct_coeffs);
+  del_fmat(s->idct_coeffs);
+  AUBIO_FREE(s);
+}
+
+void aubio_dct_plain_do(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output) {
+  if (input->length != output->length || input->length != s->size) {
+    AUBIO_WRN("dct_plain: using input length %d, but output length = %d and size = %d",
+        input->length, output->length, s->size);
+  }
+  fmat_vecmul(s->dct_coeffs, input, output);
+}
+
+void aubio_dct_plain_rdo(aubio_dct_plain_t *s, const fvec_t *input, fvec_t *output) {
+  if (input->length != output->length || input->length != s->size) {
+    AUBIO_WRN("dct_plain: using input length %d, but output length = %d and size = %d",
+        input->length, output->length, s->size);
+  }
+  fmat_vecmul(s->idct_coeffs, input, output);
+}
index f54799d..83d1963 100644 (file)
 #include "spectral/fft.h"
 #include "spectral/filterbank.h"
 #include "spectral/filterbank_mel.h"
+#include "spectral/dct.h"
 #include "spectral/mfcc.h"
 
+#undef HAVE_SLOW_DCT
+
 /** Internal structure for mfcc object */
 
 struct _aubio_mfcc_t
@@ -40,7 +43,12 @@ struct _aubio_mfcc_t
   uint_t n_coefs;           /** number of coefficients (<= n_filters/2 +1) */
   aubio_filterbank_t *fb;   /** filter bank */
   fvec_t *in_dct;           /** input buffer for dct * [fb->n_filters] */
+#if defined(HAVE_SLOW_DCT)
   fmat_t *dct_coeffs;       /** DCT transform n_filters * n_coeffs */
+#else
+  aubio_dct_t *dct;
+  fvec_t *output;
+#endif
 };
 
 
@@ -51,9 +59,11 @@ new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
 
   /* allocate space for mfcc object */
   aubio_mfcc_t *mfcc = AUBIO_NEW (aubio_mfcc_t);
+#if defined(HAVE_SLOW_DCT)
   smpl_t scaling;
 
   uint_t i, j;
+#endif
 
   mfcc->win_s = win_s;
   mfcc->samplerate = samplerate;
@@ -67,6 +77,7 @@ new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
   /* allocating buffers */
   mfcc->in_dct = new_fvec (n_filters);
 
+#if defined(HAVE_SLOW_DCT)
   mfcc->dct_coeffs = new_fmat (n_coefs, n_filters);
 
   /* compute DCT transform dct_coeffs[j][i] as
@@ -79,6 +90,10 @@ new_aubio_mfcc (uint_t win_s, uint_t n_filters, uint_t n_coefs,
     }
     mfcc->dct_coeffs->data[0][i] *= SQRT (2.) / 2.;
   }
+#else
+  mfcc->dct = new_aubio_dct (n_filters);
+  mfcc->output = new_fvec (n_filters);
+#endif
 
   return mfcc;
 }
@@ -92,7 +107,12 @@ del_aubio_mfcc (aubio_mfcc_t * mf)
 
   /* delete buffers */
   del_fvec (mf->in_dct);
+#if defined(HAVE_SLOW_DCT)
   del_fmat (mf->dct_coeffs);
+#else
+  del_aubio_dct (mf->dct);
+  del_fvec (mf->output);
+#endif
 
   /* delete mfcc object */
   AUBIO_FREE (mf);
@@ -102,6 +122,9 @@ del_aubio_mfcc (aubio_mfcc_t * mf)
 void
 aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out)
 {
+#ifndef HAVE_SLOW_DCT
+  fvec_t tmp;
+#endif
   /* compute filterbank */
   aubio_filterbank_do (mf->fb, in, mf->in_dct);
 
@@ -112,7 +135,16 @@ aubio_mfcc_do (aubio_mfcc_t * mf, const cvec_t * in, fvec_t * out)
   //fvec_pow (mf->in_dct, 3.);
 
   /* compute mfccs */
+#if defined(HAVE_SLOW_DCT)
   fmat_vecmul(mf->dct_coeffs, mf->in_dct, out);
+#else
+  aubio_dct_do(mf->dct, mf->in_dct, mf->output);
+  // copy only first n_coeffs elements
+  // TODO assert mf->output->length == n_coeffs
+  tmp.data = mf->output->data;
+  tmp.length = out->length;
+  fvec_copy(&tmp, out);
+#endif
 
   return;
 }
diff --git a/tests/src/spectral/test-dct.c b/tests/src/spectral/test-dct.c
new file mode 100644 (file)
index 0000000..ebd9846
--- /dev/null
@@ -0,0 +1,43 @@
+#include <math.h>
+#include "aubio.h"
+#include "utils_tests.h"
+
+int main (void)
+{
+  int return_code = 0;
+  uint_t win_s = 32; // window size
+  uint_t i, j, n_iters = 10; // number of iterations
+  // create dct object
+  aubio_dct_t * dct = new_aubio_dct(win_s);
+
+  fvec_t * in = new_fvec (win_s); // input buffer
+  fvec_t * dctout = new_fvec (win_s); // output buffer
+  fvec_t * out = new_fvec (win_s); // input buffer
+
+  if (!dct || !in || !dctout) {
+    return_code = 1;
+    return return_code;
+  }
+
+  in->data[0] = 1.;
+  for (i = 0; i < n_iters; i++) {
+    aubio_dct_do (dct, in, dctout);
+    aubio_dct_rdo (dct, dctout, out);
+    for (j = 0; j < in->length; j++) {
+      if (fabsf(in->data[j] - out->data[j]) > 10.e-4) {
+        fprintf(stderr, "dct reconstruction failed\n");
+      }
+    }
+  }
+
+  fvec_print(in);
+  fvec_print(dctout);
+  fvec_print(out);
+
+  del_fvec(dctout);
+  del_fvec(in);
+  del_fvec(out);
+  del_aubio_dct(dct);
+
+  return return_code;
+}