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