summaryrefslogtreecommitdiff
path: root/src/aubio_priv.h
blob: 530edc9b7e4ec8880a69d5c2ec6db2547de76efb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
  Copyright (C) 2003-2015 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
 * Private include file
 *
 * This file is for inclusion from _within_ the library only.
 */

#ifndef AUBIO_PRIV_H
#define AUBIO_PRIV_H

/*********************
 *
 * External includes
 *
 */

#include "config.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

/* must be included before fftw3.h */
#ifdef HAVE_COMPLEX_H
#include <complex.h>
#endif

#if defined(HAVE_FFTW3) || defined(HAVE_FFTW3F)
#include <fftw3.h>
#endif

#ifdef HAVE_MATH_H
#include <math.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_LIMITS_H
#include <limits.h> // for CHAR_BIT, in C99 standard
#endif

#ifdef HAVE_ACCELERATE
#define HAVE_ATLAS 1
#include <Accelerate/Accelerate.h>
#elif defined(HAVE_ATLAS_CBLAS_H)
#define HAVE_ATLAS 1
#include <atlas/cblas.h>
#else
#undef HAVE_ATLAS
#endif

#ifdef HAVE_ACCELERATE
#include <Accelerate/Accelerate.h>
#ifndef HAVE_AUBIO_DOUBLE
#define aubio_vDSP_mmov       vDSP_mmov
#define aubio_vDSP_vmul       vDSP_vmul
#define aubio_vDSP_vfill      vDSP_vfill
#define aubio_vDSP_meanv      vDSP_meanv
#define aubio_vDSP_sve        vDSP_sve
#define aubio_vDSP_maxv       vDSP_maxv
#define aubio_vDSP_maxvi      vDSP_maxvi
#define aubio_vDSP_minv       vDSP_minv
#define aubio_vDSP_minvi      vDSP_minvi
#define aubio_vDSP_dotpr      vDSP_dotpr
#else /* HAVE_AUBIO_DOUBLE */
#define aubio_vDSP_mmov       vDSP_mmovD
#define aubio_vDSP_vmul       vDSP_vmulD
#define aubio_vDSP_vfill      vDSP_vfillD
#define aubio_vDSP_meanv      vDSP_meanvD
#define aubio_vDSP_sve        vDSP_sveD
#define aubio_vDSP_maxv       vDSP_maxvD
#define aubio_vDSP_maxvi      vDSP_maxviD
#define aubio_vDSP_minv       vDSP_minvD
#define aubio_vDSP_minvi      vDSP_minviD
#define aubio_vDSP_dotpr      vDSP_dotprD
#endif /* HAVE_AUBIO_DOUBLE */
#endif /* HAVE_ACCELERATE */

#ifdef HAVE_ATLAS
#ifndef HAVE_AUBIO_DOUBLE
#define aubio_catlas_set      catlas_sset
#define aubio_cblas_copy      cblas_scopy
#define aubio_cblas_swap      cblas_sswap
#define aubio_cblas_dot       cblas_sdot
#else /* HAVE_AUBIO_DOUBLE */
#define aubio_catlas_set      catlas_dset
#define aubio_cblas_copy      cblas_dcopy
#define aubio_cblas_swap      cblas_dswap
#define aubio_cblas_dot       cblas_ddot
#endif /* HAVE_AUBIO_DOUBLE */
#endif /* HAVE_ATLAS */

#if !defined(HAVE_MEMCPY_HACKS) && !defined(HAVE_ACCELERATE) && !defined(HAVE_ATLAS)
#define HAVE_NOOPT 1
#else
#undef HAVE_NOOPT
#endif

#include "types.h"

#define AUBIO_UNSTABLE 1

#include "mathutils.h"

/****
 *
 * SYSTEM INTERFACE
 *
 */

/* Memory management */
#define AUBIO_MALLOC(_n)             malloc(_n)
#define AUBIO_REALLOC(_p,_n)         realloc(_p,_n)
#define AUBIO_NEW(_t)                (_t*)calloc(sizeof(_t), 1)
#define AUBIO_ARRAY(_t,_n)           (_t*)calloc((_n)*sizeof(_t), 1)
#define AUBIO_MEMCPY(_dst,_src,_n)   memcpy(_dst,_src,_n)
#define AUBIO_MEMSET(_dst,_src,_t)   memset(_dst,_src,_t)
#define AUBIO_FREE(_p)               free(_p)


/* file interface */
#define AUBIO_FOPEN(_f,_m)           fopen(_f,_m)
#define AUBIO_FCLOSE(_f)             fclose(_f)
#define AUBIO_FREAD(_p,_s,_n,_f)     fread(_p,_s,_n,_f)
#define AUBIO_FSEEK(_f,_n,_set)      fseek(_f,_n,_set)

/* strings */
#define AUBIO_STRLEN(_s)             strlen(_s)
#define AUBIO_STRCMP(_s,_t)          strcmp(_s,_t)
#define AUBIO_STRNCMP(_s,_t,_n)      strncmp(_s,_t,_n)
#define AUBIO_STRCPY(_dst,_src)      strcpy(_dst,_src)
#define AUBIO_STRCHR(_s,_c)          strchr(_s,_c)
#ifdef strdup
#define AUBIO_STRDUP(s)              strdup(s)
#else
#define AUBIO_STRDUP(s)              AUBIO_STRCPY(AUBIO_MALLOC(AUBIO_STRLEN(s) + 1), s)
#endif


/* Error reporting */
typedef enum {
  AUBIO_OK = 0,
  AUBIO_FAIL = 1
} aubio_status;

#ifdef HAVE_C99_VARARGS_MACROS
#define AUBIO_ERR(...)               fprintf(stderr, "AUBIO ERROR: " __VA_ARGS__)
#define AUBIO_MSG(...)               fprintf(stdout, __VA_ARGS__)
#define AUBIO_DBG(...)               fprintf(stderr, __VA_ARGS__)
#define AUBIO_WRN(...)               fprintf(stderr, "AUBIO WARNING: " __VA_ARGS__)
#else
#define AUBIO_ERR(format, args...)   fprintf(stderr, "AUBIO ERROR: " format , ##args)
#define AUBIO_MSG(format, args...)   fprintf(stdout, format , ##args)
#define AUBIO_DBG(format, args...)   fprintf(stderr, format , ##args)
#define AUBIO_WRN(format, args...)   fprintf(stderr, "AUBIO WARNING: " format, ##args)
#endif

#define AUBIO_ERROR   AUBIO_ERR

#define AUBIO_QUIT(_s)               exit(_s)
#define AUBIO_SPRINTF                sprintf

/* pi and 2*pi */
#ifndef M_PI
#define PI         (3.14159265358979323846)
#else
#define PI         (M_PI)
#endif
#define TWO_PI     (PI*2.)

#ifndef PATH_MAX
#define PATH_MAX 1024
#endif

/* aliases to math.h functions */
#if !HAVE_AUBIO_DOUBLE
#define EXP        expf
#define COS        cosf
#define SIN        sinf
#define ABS        fabsf
#define POW        powf
#define SQRT       sqrtf
#define LOG10      log10f
#define LOG        logf
#define FLOOR      floorf
#define CEIL       ceilf
#define ATAN2      atan2f
#else
#define EXP        exp
#define COS        cos
#define SIN        sin
#define ABS        fabs
#define POW        pow
#define SQRT       sqrt
#define LOG10      log10
#define LOG        log
#define FLOOR      floor
#define CEIL       ceil
#define ATAN2      atan2
#endif
#define ROUND(x)   FLOOR(x+.5)

/* aliases to complex.h functions */
#if HAVE_AUBIO_DOUBLE || !defined(HAVE_COMPLEX_H) || defined(WIN32)
/* mingw32 does not know about c*f functions */
#define EXPC      cexp
/** complex = CEXPC(complex) */
#define CEXPC     cexp
/** sample = ARGC(complex) */
#define ARGC      carg
/** sample = ABSC(complex) norm */
#define ABSC      cabs
/** sample = REAL(complex) */
#define REAL      creal
/** sample = IMAG(complex) */
#define IMAG      cimag
#else
/** sample = EXPC(complex) */
#define EXPC      cexpf
/** complex = CEXPC(complex) */
#define CEXPC     cexp
/** sample = ARGC(complex) */
#define ARGC      cargf
/** sample = ABSC(complex) norm */
#define ABSC      cabsf
/** sample = REAL(complex) */
#define REAL      crealf
/** sample = IMAG(complex) */
#define IMAG      cimagf
#endif

/* avoid unresolved symbol with msvc 9 */
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define isnan _isnan
#endif

/* handy shortcuts */
#define DB2LIN(g) (POW(10.0,(g)*0.05f))
#define LIN2DB(v) (20.0*LOG10(v))
#define SQR(_a)   ((_a)*(_a))

#ifndef MAX
#define MAX(a,b)  (((a)>(b))?(a):(b))
#endif /* MAX */
#ifndef MIN
#define MIN(a,b)  (((a)<(b))?(a):(b))
#endif /* MIN */

#define ELEM_SWAP(a,b) { register smpl_t t=(a);(a)=(b);(b)=t; }

#define VERY_SMALL_NUMBER 2.e-42 //1.e-37

/** if ABS(f) < VERY_SMALL_NUMBER, returns 1, else 0 */
#define IS_DENORMAL(f) ABS(f) < VERY_SMALL_NUMBER

/** if ABS(f) < VERY_SMALL_NUMBER, returns 0., else f */
#define KILL_DENORMAL(f)  IS_DENORMAL(f) ? 0. : f

/** if f > VERY_SMALL_NUMBER, returns f, else returns VERY_SMALL_NUMBER */
#define CEIL_DENORMAL(f)  f < VERY_SMALL_NUMBER ? VERY_SMALL_NUMBER : f

#define SAFE_LOG10(f) LOG10(CEIL_DENORMAL(f))
#define SAFE_LOG(f)   LOG(CEIL_DENORMAL(f))

/** silence unused parameter warning by adding an attribute */
#if defined(__GNUC__)
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif

/* are we using gcc -std=c99 ? */
#if defined(__STRICT_ANSI__)
#define strnlen(a,b) MIN(strlen(a),b)
#if !HAVE_AUBIO_DOUBLE
#define floorf floor
#endif
#endif /* __STRICT_ANSI__ */

#endif /* AUBIO_PRIV_H */