5d9837060e1d5f931d3db70651e7e9df1dcf9c00
[aubio.git] / src / aubio_priv.h
1 /*
2   Copyright (C) 2003-2015 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 /** @file
22  * Private include file
23  *
24  * This file is for inclusion from _within_ the library only.
25  */
26
27 #ifndef _AUBIO__PRIV_H
28 #define _AUBIO__PRIV_H
29
30 /*********************
31  *
32  * External includes
33  *
34  */
35
36 #include "config.h"
37
38 #if HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41
42 #if HAVE_STDIO_H
43 #include <stdio.h>
44 #endif
45
46 /* must be included before fftw3.h */
47 #ifdef HAVE_COMPLEX_H
48 #include <complex.h>
49 #endif
50
51 #if defined(HAVE_FFTW3) || defined(HAVE_FFTW3F)
52 #include <fftw3.h>
53 #endif
54
55 #ifdef HAVE_MATH_H
56 #include <math.h>
57 #endif
58
59 #ifdef HAVE_STRING_H
60 #include <string.h>
61 #endif
62
63 #ifdef HAVE_LIMITS_H
64 #include <limits.h> // for CHAR_BIT, in C99 standard
65 #endif
66
67 #ifdef HAVE_ACCELERATE
68 #define HAVE_ATLAS 1
69 #include <Accelerate/Accelerate.h>
70 #elif HAVE_ATLAS_CBLAS_H
71 #define HAVE_ATLAS 1
72 #include <atlas/cblas.h>
73 #else
74 #undef HAVE_ATLAS
75 #endif
76
77 #ifdef HAVE_ACCELERATE
78 #include <Accelerate/Accelerate.h>
79 #if !HAVE_AUBIO_DOUBLE
80 #define aubio_vDSP_mmov       vDSP_mmov
81 #define aubio_vDSP_vmul       vDSP_vmul
82 #define aubio_vDSP_vfill      vDSP_vfill
83 #else /* HAVE_AUBIO_DOUBLE */
84 #define aubio_vDSP_mmov       vDSP_mmovD
85 #define aubio_vDSP_vmul       vDSP_vmulD
86 #define aubio_vDSP_vfill      vDSP_vfillD
87 #endif /* HAVE_AUBIO_DOUBLE */
88 #endif /* HAVE_ACCELERATE */
89
90 #ifdef HAVE_ATLAS
91 #if !HAVE_AUBIO_DOUBLE
92 #define aubio_catlas_set      catlas_sset
93 #define aubio_cblas_copy      cblas_scopy
94 #else /* HAVE_AUBIO_DOUBLE */
95 #define aubio_catlas_set      catlas_dset
96 #define aubio_cblas_copy      cblas_dcopy
97 #endif /* HAVE_AUBIO_DOUBLE */
98 #endif /* HAVE_ATLAS */
99
100 #if !defined(HAVE_MEMCPY_HACKS) && !defined(HAVE_ACCELERATE) && !defined(HAVE_ATLAS)
101 #define HAVE_NOOPT 1
102 #else
103 #undef HAVE_NOOPT
104 #endif
105
106 #include "types.h"
107
108 #define AUBIO_UNSTABLE 1
109
110 #include "mathutils.h"
111
112 /****
113  *
114  * SYSTEM INTERFACE
115  *
116  */
117
118 /* Memory management */
119 #define AUBIO_MALLOC(_n)             malloc(_n)
120 #define AUBIO_REALLOC(_p,_n)         realloc(_p,_n)
121 #define AUBIO_NEW(_t)                (_t*)calloc(sizeof(_t), 1)
122 #define AUBIO_ARRAY(_t,_n)           (_t*)calloc((_n)*sizeof(_t), 1)
123 #define AUBIO_MEMCPY(_dst,_src,_n)   memcpy(_dst,_src,_n)
124 #define AUBIO_MEMSET(_dst,_src,_t)   memset(_dst,_src,_t)
125 #define AUBIO_FREE(_p)               free(_p)
126
127
128 /* file interface */
129 #define AUBIO_FOPEN(_f,_m)           fopen(_f,_m)
130 #define AUBIO_FCLOSE(_f)             fclose(_f)
131 #define AUBIO_FREAD(_p,_s,_n,_f)     fread(_p,_s,_n,_f)
132 #define AUBIO_FSEEK(_f,_n,_set)      fseek(_f,_n,_set)
133
134 /* strings */
135 #define AUBIO_STRLEN(_s)             strlen(_s)
136 #define AUBIO_STRCMP(_s,_t)          strcmp(_s,_t)
137 #define AUBIO_STRNCMP(_s,_t,_n)      strncmp(_s,_t,_n)
138 #define AUBIO_STRCPY(_dst,_src)      strcpy(_dst,_src)
139 #define AUBIO_STRCHR(_s,_c)          strchr(_s,_c)
140 #ifdef strdup
141 #define AUBIO_STRDUP(s)              strdup(s)
142 #else
143 #define AUBIO_STRDUP(s)              AUBIO_STRCPY(AUBIO_MALLOC(AUBIO_STRLEN(s) + 1), s)
144 #endif
145
146
147 /* Error reporting */
148 typedef enum {
149   AUBIO_OK = 0,
150   AUBIO_FAIL = 1
151 } aubio_status;
152
153 #ifdef HAVE_C99_VARARGS_MACROS
154 #define AUBIO_ERR(...)               fprintf(stderr, "AUBIO ERROR: " __VA_ARGS__)
155 #define AUBIO_MSG(...)               fprintf(stdout, __VA_ARGS__)
156 #define AUBIO_DBG(...)               fprintf(stderr, __VA_ARGS__)
157 #define AUBIO_WRN(...)               fprintf(stderr, "AUBIO WARNING: " __VA_ARGS__)
158 #else
159 #define AUBIO_ERR(format, args...)   fprintf(stderr, "AUBIO ERROR: " format , ##args)
160 #define AUBIO_MSG(format, args...)   fprintf(stdout, format , ##args)
161 #define AUBIO_DBG(format, args...)   fprintf(stderr, format , ##args)
162 #define AUBIO_WRN(format, args...)   fprintf(stderr, "AUBIO WARNING: " format, ##args)
163 #endif
164
165 #define AUBIO_ERROR   AUBIO_ERR
166
167 #define AUBIO_QUIT(_s)               exit(_s)
168 #define AUBIO_SPRINTF                sprintf
169
170 /* pi and 2*pi */
171 #ifndef M_PI
172 #define PI         (3.14159265358979323846)
173 #else
174 #define PI         (M_PI)
175 #endif
176 #define TWO_PI     (PI*2.)
177
178 /* aliases to math.h functions */
179 #if !HAVE_AUBIO_DOUBLE
180 #define EXP        expf
181 #define COS        cosf
182 #define SIN        sinf
183 #define ABS        fabsf
184 #define POW        powf
185 #define SQRT       sqrtf
186 #define LOG10      log10f
187 #define LOG        logf
188 #define FLOOR      floorf
189 #define CEIL       ceilf
190 #define ATAN2      atan2f
191 #else
192 #define EXP        exp
193 #define COS        cos
194 #define SIN        sin
195 #define ABS        fabs
196 #define POW        pow
197 #define SQRT       sqrt
198 #define LOG10      log10
199 #define LOG        log
200 #define FLOOR      floor
201 #define CEIL       ceil
202 #define ATAN2      atan2
203 #endif
204 #define ROUND(x)   FLOOR(x+.5)
205
206 /* aliases to complex.h functions */
207 #if HAVE_AUBIO_DOUBLE || !defined(HAVE_COMPLEX_H) || defined(WIN32)
208 /* mingw32 does not know about c*f functions */
209 #define EXPC      cexp
210 /** complex = CEXPC(complex) */
211 #define CEXPC     cexp
212 /** sample = ARGC(complex) */
213 #define ARGC      carg
214 /** sample = ABSC(complex) norm */
215 #define ABSC      cabs
216 /** sample = REAL(complex) */
217 #define REAL      creal
218 /** sample = IMAG(complex) */
219 #define IMAG      cimag
220 #else
221 /** sample = EXPC(complex) */
222 #define EXPC      cexpf
223 /** complex = CEXPC(complex) */
224 #define CEXPC     cexp
225 /** sample = ARGC(complex) */
226 #define ARGC      cargf
227 /** sample = ABSC(complex) norm */
228 #define ABSC      cabsf
229 /** sample = REAL(complex) */
230 #define REAL      crealf
231 /** sample = IMAG(complex) */
232 #define IMAG      cimagf
233 #endif
234
235 /* handy shortcuts */
236 #define DB2LIN(g) (POW(10.0,(g)*0.05f))
237 #define LIN2DB(v) (20.0*LOG10(v))
238 #define SQR(_a)   ((_a)*(_a))
239
240 #ifndef MAX
241 #define MAX(a,b)  (((a)>(b))?(a):(b))
242 #endif /* MAX */
243 #ifndef MIN
244 #define MIN(a,b)  (((a)<(b))?(a):(b))
245 #endif /* MIN */
246
247 #define ELEM_SWAP(a,b) { register smpl_t t=(a);(a)=(b);(b)=t; }
248
249 #define VERY_SMALL_NUMBER 2.e-42 //1.e-37
250
251 /** if ABS(f) < VERY_SMALL_NUMBER, returns 1, else 0 */
252 #define IS_DENORMAL(f) ABS(f) < VERY_SMALL_NUMBER
253
254 /** if ABS(f) < VERY_SMALL_NUMBER, returns 0., else f */
255 #define KILL_DENORMAL(f)  IS_DENORMAL(f) ? 0. : f
256
257 /** if f > VERY_SMALL_NUMBER, returns f, else returns VERY_SMALL_NUMBER */
258 #define CEIL_DENORMAL(f)  f < VERY_SMALL_NUMBER ? VERY_SMALL_NUMBER : f
259
260 #define SAFE_LOG10(f) LOG10(CEIL_DENORMAL(f))
261 #define SAFE_LOG(f)   LOG(CEIL_DENORMAL(f))
262
263 /** silence unused parameter warning by adding an attribute */
264 #if defined(__GNUC__)
265 #define UNUSED __attribute__((unused))
266 #else
267 #define UNUSED
268 #endif
269
270 #endif /* _AUBIO__PRIV_H */