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