1 /*
2 ** Copyright (c) 2002-2021, 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/libsndfile/libsamplerate/blob/master/COPYING
7 */
8 
9 #ifndef COMMON_H_INCLUDED
10 #define COMMON_H_INCLUDED
11 
12 #include <stdint.h>
13 #ifdef HAVE_STDBOOL_H
14 #include <stdbool.h>
15 #endif
16 
17 #include <math.h>
18 
19 #ifdef HAVE_VISIBILITY
20   #define LIBSAMPLERATE_DLL_PRIVATE __attribute__ ((visibility ("hidden")))
21 #elif defined (__APPLE__)
22   #define LIBSAMPLERATE_DLL_PRIVATE __private_extern__
23 #else
24   #define LIBSAMPLERATE_DLL_PRIVATE
25 #endif
26 
27 #define	SRC_MAX_RATIO			256
28 #define	SRC_MAX_RATIO_STR		"256"
29 
30 #define	SRC_MIN_RATIO_DIFF		(1e-20)
31 
32 #ifndef MAX
33 #define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
34 #endif
35 
36 #ifndef MIN
37 #define	MIN(a,b)	(((a) < (b)) ? (a) : (b))
38 #endif
39 
40 #define	ARRAY_LEN(x)			((int) (sizeof (x) / sizeof ((x) [0])))
41 #define OFFSETOF(type,member)	((int) (&((type*) 0)->member))
42 
43 #define	MAKE_MAGIC(a,b,c,d,e,f)	((a) + ((b) << 4) + ((c) << 8) + ((d) << 12) + ((e) << 16) + ((f) << 20))
44 
45 /*
46 ** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html
47 */
48 #ifdef UNUSED
49 #elif defined (__GNUC__)
50 #	define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
51 #elif defined (__LCLINT__)
52 #	define UNUSED(x) /*@unused@*/ x
53 #else
54 #	define UNUSED(x) x
55 #endif
56 
57 #ifdef __GNUC__
58 #	define WARN_UNUSED	__attribute__ ((warn_unused_result))
59 #else
60 #	define WARN_UNUSED
61 #endif
62 
63 #include "samplerate.h"
64 
65 enum
66 {	SRC_FALSE	= 0,
67 	SRC_TRUE	= 1,
68 } ;
69 
70 enum SRC_MODE
71 {
72 	SRC_MODE_PROCESS	= 0,
73 	SRC_MODE_CALLBACK	= 1
74 } ;
75 
76 typedef enum SRC_ERROR
77 {
78 	SRC_ERR_NO_ERROR = 0,
79 
80 	SRC_ERR_MALLOC_FAILED,
81 	SRC_ERR_BAD_STATE,
82 	SRC_ERR_BAD_DATA,
83 	SRC_ERR_BAD_DATA_PTR,
84 	SRC_ERR_NO_PRIVATE,
85 	SRC_ERR_BAD_SRC_RATIO,
86 	SRC_ERR_BAD_PROC_PTR,
87 	SRC_ERR_SHIFT_BITS,
88 	SRC_ERR_FILTER_LEN,
89 	SRC_ERR_BAD_CONVERTER,
90 	SRC_ERR_BAD_CHANNEL_COUNT,
91 	SRC_ERR_SINC_BAD_BUFFER_LEN,
92 	SRC_ERR_SIZE_INCOMPATIBILITY,
93 	SRC_ERR_BAD_PRIV_PTR,
94 	SRC_ERR_BAD_SINC_STATE,
95 	SRC_ERR_DATA_OVERLAP,
96 	SRC_ERR_BAD_CALLBACK,
97 	SRC_ERR_BAD_MODE,
98 	SRC_ERR_NULL_CALLBACK,
99 	SRC_ERR_NO_VARIABLE_RATIO,
100 	SRC_ERR_SINC_PREPARE_DATA_BAD_LEN,
101 	SRC_ERR_BAD_INTERNAL_STATE,
102 
103 	/* This must be the last error number. */
104 	SRC_ERR_MAX_ERROR
105 } SRC_ERROR ;
106 
107 typedef struct SRC_STATE_VT_tag
108 {
109 	/* Varispeed process function. */
110 	SRC_ERROR		(*vari_process) (SRC_STATE *state, SRC_DATA *data) ;
111 
112 	/* Constant speed process function. */
113 	SRC_ERROR		(*const_process) (SRC_STATE *state, SRC_DATA *data) ;
114 
115 	/* State reset. */
116 	void			(*reset) (SRC_STATE *state) ;
117 
118 	/* State clone. */
119 	SRC_STATE		*(*copy) (SRC_STATE *state) ;
120 
121 	/* State close. */
122 	void			(*close) (SRC_STATE *state) ;
123 } SRC_STATE_VT ;
124 
125 struct SRC_STATE_tag
126 {
127 	SRC_STATE_VT *vt ;
128 
129 	double	last_ratio, last_position ;
130 
131 	SRC_ERROR	error ;
132 	int		channels ;
133 
134 	/* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
135 	enum SRC_MODE	mode ;
136 
137 	/* Data specific to SRC_MODE_CALLBACK. */
138 	src_callback_t	callback_func ;
139 	void			*user_callback_data ;
140 	long			saved_frames ;
141 	const float		*saved_data ;
142 
143 	/* Pointer to data to converter specific data. */
144 	void	*private_data ;
145 } ;
146 
147 /* In src_sinc.c */
148 const char* sinc_get_name (int src_enum) ;
149 const char* sinc_get_description (int src_enum) ;
150 
151 SRC_STATE *sinc_state_new (int converter_type, int channels, SRC_ERROR *error) ;
152 
153 /* In src_linear.c */
154 const char* linear_get_name (int src_enum) ;
155 const char* linear_get_description (int src_enum) ;
156 
157 SRC_STATE *linear_state_new (int channels, SRC_ERROR *error) ;
158 
159 /* In src_zoh.c */
160 const char* zoh_get_name (int src_enum) ;
161 const char* zoh_get_description (int src_enum) ;
162 
163 SRC_STATE *zoh_state_new (int channels, SRC_ERROR *error) ;
164 
165 /*----------------------------------------------------------
166 **	Common static inline functions.
167 */
168 
169 static inline double
fmod_one(double x)170 fmod_one (double x)
171 {	double res ;
172 
173 	res = x - lrint (x) ;
174 	if (res < 0.0)
175 		return res + 1.0 ;
176 
177 	return res ;
178 } /* fmod_one */
179 
180 static inline int
is_bad_src_ratio(double ratio)181 is_bad_src_ratio (double ratio)
182 {	return (ratio < (1.0 / SRC_MAX_RATIO) || ratio > (1.0 * SRC_MAX_RATIO)) ;
183 } /* is_bad_src_ratio */
184 
185 
186 #endif	/* COMMON_H_INCLUDED */
187 
188