src/spectral/dct_accelerate.c: add vdsp dct
[aubio.git] / src / spectral / dct_accelerate.c
1 /*
2   Copyright (C) 2017 Paul Brossier <piem@aubio.org>
3
4   This file is part of aubio.
5
6   aubio is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   aubio is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "aubio_priv.h"
22 #include "fvec.h"
23 #include "cvec.h"
24 #include "spectral/dct.h"
25
26 #if defined(HAVE_ACCELERATE)
27
28 #if HAVE_AUBIO_DOUBLE
29 #warning "no double-precision dct with accelerate"
30 #endif
31
32 struct _aubio_dct_t {
33   uint_t size;
34   fvec_t *tmp;
35   vDSP_DFT_Setup setup;
36   vDSP_DFT_Setup setupInv;
37 };
38
39 aubio_dct_t * new_aubio_dct (uint_t size) {
40   aubio_dct_t * s = AUBIO_NEW(aubio_dct_t);
41
42   if ((sint_t)size < 16 || !aubio_is_power_of_two(size)) {
43     AUBIO_ERR("dct: can only create with sizes greater than 16 and"
44         "that are powers of two, requested %d\n", size);
45     goto beach;
46   }
47
48   s->setup = vDSP_DCT_CreateSetup(NULL, (vDSP_Length)size, vDSP_DCT_II);
49   s->setupInv = vDSP_DCT_CreateSetup(NULL, (vDSP_Length)size, vDSP_DCT_III);
50   if (s->setup == NULL || s->setupInv == NULL) {
51     goto beach;
52   }
53
54   s->size = size;
55
56   return s;
57
58 beach:
59   del_aubio_dct(s);
60   return NULL;
61 }
62
63 void del_aubio_dct(aubio_dct_t *s) {
64   if (s->setup) vDSP_DFT_DestroySetup(s->setup);
65   if (s->setupInv) vDSP_DFT_DestroySetup(s->setupInv);
66   AUBIO_FREE(s);
67 }
68
69 void aubio_dct_do(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
70
71   vDSP_DCT_Execute(s->setup, (const float *)input->data, (float *)output->data);
72
73   // apply orthonormal scaling
74   output->data[0] *= SQRT(1./s->size);
75   smpl_t scaler = SQRT(2./s->size);
76
77   aubio_vDSP_vsmul(output->data + 1, 1, &scaler, output->data + 1, 1,
78       output->length - 1);
79
80 }
81
82 void aubio_dct_rdo(aubio_dct_t *s, const fvec_t *input, fvec_t *output) {
83
84   output->data[0] = input->data[0] / SQRT(1./s->size);
85   smpl_t scaler = 1./SQRT(2./s->size);
86
87   aubio_vDSP_vsmul(input->data + 1, 1, &scaler, output->data + 1, 1,
88       output->length - 1);
89
90   vDSP_DCT_Execute(s->setupInv, (const float *)output->data,
91       (float *)output->data);
92
93   scaler = 2./s->size;
94
95   aubio_vDSP_vsmul(output->data, 1, &scaler, output->data, 1, output->length);
96
97 }
98
99 #endif //defined(HAVE_ACCELERATE)