1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
7 */
8
9 #ifndef COMMON_H_INCLUDED
10 #define COMMON_H_INCLUDED
11
12 #ifdef HAVE_STDINT_H
13 #include <stdint.h>
14 #elif (SIZEOF_INT == 4)
15 typedef int int32_t ;
16 #elif (SIZEOF_LONG == 4)
17 typedef long int32_t ;
18 #endif
19
20 #define SRC_MAX_RATIO 256
21 #define SRC_MAX_RATIO_STR "256"
22
23 #define SRC_MIN_RATIO_DIFF (1e-20)
24
25 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
26 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
27
28 #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0])))
29 #define OFFSETOF(type,member) ((int) (&((type*) 0)->member))
30
31 #define MAKE_MAGIC(a,b,c,d,e,f) ((a) + ((b) << 4) + ((c) << 8) + ((d) << 12) + ((e) << 16) + ((f) << 20))
32
33 /*
34 ** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html
35 */
36 #ifdef UNUSED
37 #elif defined (__GNUC__)
38 # define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
39 #elif defined (__LCLINT__)
40 # define UNUSED(x) /*@unused@*/ x
41 #else
42 # define UNUSED(x) x
43 #endif
44
45 #ifdef __GNUC__
46 # define WARN_UNUSED __attribute__ ((warn_unused_result))
47 #else
48 # define WARN_UNUSED
49 #endif
50
51
52 #include "samplerate.h"
53
54 enum
55 { SRC_FALSE = 0,
56 SRC_TRUE = 1,
57
58 SRC_MODE_PROCESS = 555,
59 SRC_MODE_CALLBACK = 556
60 } ;
61
62 enum
63 { SRC_ERR_NO_ERROR = 0,
64
65 SRC_ERR_MALLOC_FAILED,
66 SRC_ERR_BAD_STATE,
67 SRC_ERR_BAD_DATA,
68 SRC_ERR_BAD_DATA_PTR,
69 SRC_ERR_NO_PRIVATE,
70 SRC_ERR_BAD_SRC_RATIO,
71 SRC_ERR_BAD_PROC_PTR,
72 SRC_ERR_SHIFT_BITS,
73 SRC_ERR_FILTER_LEN,
74 SRC_ERR_BAD_CONVERTER,
75 SRC_ERR_BAD_CHANNEL_COUNT,
76 SRC_ERR_SINC_BAD_BUFFER_LEN,
77 SRC_ERR_SIZE_INCOMPATIBILITY,
78 SRC_ERR_BAD_PRIV_PTR,
79 SRC_ERR_BAD_SINC_STATE,
80 SRC_ERR_DATA_OVERLAP,
81 SRC_ERR_BAD_CALLBACK,
82 SRC_ERR_BAD_MODE,
83 SRC_ERR_NULL_CALLBACK,
84 SRC_ERR_NO_VARIABLE_RATIO,
85 SRC_ERR_SINC_PREPARE_DATA_BAD_LEN,
86 SRC_ERR_BAD_INTERNAL_STATE,
87
88 /* This must be the last error number. */
89 SRC_ERR_MAX_ERROR
90 } ;
91
92 typedef struct SRC_PRIVATE_tag
93 { double last_ratio, last_position ;
94
95 int error ;
96 int channels ;
97
98 /* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
99 int mode ;
100
101 /* Pointer to data to converter specific data. */
102 void *private_data ;
103
104 /* Varispeed process function. */
105 int (*vari_process) (struct SRC_PRIVATE_tag *psrc, SRC_DATA *data) ;
106
107 /* Constant speed process function. */
108 int (*const_process) (struct SRC_PRIVATE_tag *psrc, SRC_DATA *data) ;
109
110 /* State reset. */
111 void (*reset) (struct SRC_PRIVATE_tag *psrc) ;
112
113 /* Data specific to SRC_MODE_CALLBACK. */
114 src_callback_t callback_func ;
115 void *user_callback_data ;
116 long saved_frames ;
117 const float *saved_data ;
118 } SRC_PRIVATE ;
119
120 /* In src_sinc.c */
121 const char* sinc_get_name (int src_enum) ;
122 const char* sinc_get_description (int src_enum) ;
123
124 int sinc_set_converter (SRC_PRIVATE *psrc, int src_enum) ;
125
126 /* In src_linear.c */
127 const char* linear_get_name (int src_enum) ;
128 const char* linear_get_description (int src_enum) ;
129
130 int linear_set_converter (SRC_PRIVATE *psrc, int src_enum) ;
131
132 /* In src_zoh.c */
133 const char* zoh_get_name (int src_enum) ;
134 const char* zoh_get_description (int src_enum) ;
135
136 int zoh_set_converter (SRC_PRIVATE *psrc, int src_enum) ;
137
138 /*----------------------------------------------------------
139 ** Common static inline functions.
140 */
141
142 static inline double
fmod_one(double x)143 fmod_one (double x)
144 { double res ;
145
146 res = x - lrint (x) ;
147 if (res < 0.0)
148 return res + 1.0 ;
149
150 return res ;
151 } /* fmod_one */
152
153 static inline int
is_bad_src_ratio(double ratio)154 is_bad_src_ratio (double ratio)
155 { return (ratio < (1.0 / SRC_MAX_RATIO) || ratio > (1.0 * SRC_MAX_RATIO)) ;
156 } /* is_bad_src_ratio */
157
158
159 #endif /* COMMON_H_INCLUDED */
160
161