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 #include "precomp.h"
10 
11 static int psrc_set_converter (SRC_PRIVATE	*psrc, int converter_type) ;
12 
13 
14 SRC_STATE *
src_new(int converter_type,int channels,int * error)15 src_new (int converter_type, int channels, int *error)
16 {	SRC_PRIVATE	*psrc ;
17 
18 	if (error)
19 		*error = SRC_ERR_NO_ERROR ;
20 
21 	if (channels < 1)
22 	{	if (error)
23 			*error = SRC_ERR_BAD_CHANNEL_COUNT ;
24 		return NULL ;
25 		} ;
26 
27 	if ((psrc = calloc (1, sizeof (*psrc))) == NULL)
28 	{	if (error)
29 			*error = SRC_ERR_MALLOC_FAILED ;
30 		return NULL ;
31 		} ;
32 
33 	psrc->channels = channels ;
34 	psrc->mode = SRC_MODE_PROCESS ;
35 
36 	if (psrc_set_converter (psrc, converter_type) != SRC_ERR_NO_ERROR)
37 	{	if (error)
38 			*error = SRC_ERR_BAD_CONVERTER ;
39 		free (psrc) ;
40 		psrc = NULL ;
41 		} ;
42 
43 	src_reset ((SRC_STATE*) psrc) ;
44 
45 	return (SRC_STATE*) psrc ;
46 } /* src_new */
47 
48 SRC_STATE*
src_callback_new(src_callback_t func,int converter_type,int channels,int * error,void * cb_data)49 src_callback_new (src_callback_t func, int converter_type, int channels, int *error, void* cb_data)
50 {	SRC_STATE	*src_state ;
51 
52 	if (func == NULL)
53 	{	if (error)
54 			*error = SRC_ERR_BAD_CALLBACK ;
55 		return NULL ;
56 		} ;
57 
58 	if (error != NULL)
59 		*error = 0 ;
60 
61 	if ((src_state = src_new (converter_type, channels, error)) == NULL)
62 		return NULL ;
63 
64 	src_reset (src_state) ;
65 
66 	((SRC_PRIVATE*) src_state)->mode = SRC_MODE_CALLBACK ;
67 	((SRC_PRIVATE*) src_state)->callback_func = func ;
68 	((SRC_PRIVATE*) src_state)->user_callback_data = cb_data ;
69 
70 	return src_state ;
71 } /* src_callback_new */
72 
73 SRC_STATE *
src_delete(SRC_STATE * state)74 src_delete (SRC_STATE *state)
75 {	SRC_PRIVATE *psrc ;
76 
77 	psrc = (SRC_PRIVATE*) state ;
78 	if (psrc)
79 	{	if (psrc->private_data)
80 			free (psrc->private_data) ;
81 		memset (psrc, 0, sizeof (SRC_PRIVATE)) ;
82 		free (psrc) ;
83 		} ;
84 
85 	return NULL ;
86 } /* src_state */
87 
88 int
src_process(SRC_STATE * state,SRC_DATA * data)89 src_process (SRC_STATE *state, SRC_DATA *data)
90 {	SRC_PRIVATE *psrc ;
91 	int error ;
92 
93 	psrc = (SRC_PRIVATE*) state ;
94 
95 	if (psrc == NULL)
96 		return SRC_ERR_BAD_STATE ;
97 	if (psrc->vari_process == NULL || psrc->const_process == NULL)
98 		return SRC_ERR_BAD_PROC_PTR ;
99 
100 	if (psrc->mode != SRC_MODE_PROCESS)
101 		return SRC_ERR_BAD_MODE ;
102 
103 	/* Check for valid SRC_DATA first. */
104 	if (data == NULL)
105 		return SRC_ERR_BAD_DATA ;
106 
107 	/* And that data_in and data_out are valid. */
108 	if (data->data_in == NULL || data->data_out == NULL)
109 		return SRC_ERR_BAD_DATA_PTR ;
110 
111 	/* Check src_ratio is in range. */
112 	if (is_bad_src_ratio (data->src_ratio))
113 		return SRC_ERR_BAD_SRC_RATIO ;
114 
115 	if (data->input_frames < 0)
116 		data->input_frames = 0 ;
117 	if (data->output_frames < 0)
118 		data->output_frames = 0 ;
119 
120 	if (data->data_in < data->data_out)
121 	{	if (data->data_in + data->input_frames * psrc->channels > data->data_out)
122 		{	/*-printf ("\n\ndata_in: %p    data_out: %p\n",
123 				(void*) (data->data_in + data->input_frames * psrc->channels), (void*) data->data_out) ;-*/
124 			return SRC_ERR_DATA_OVERLAP ;
125 			} ;
126 		}
127 	else if (data->data_out + data->output_frames * psrc->channels > data->data_in)
128 	{	/*-printf ("\n\ndata_in : %p   ouput frames: %ld    data_out: %p\n", (void*) data->data_in, data->output_frames, (void*) data->data_out) ;
129 
130 		printf ("data_out: %p (%p)    data_in: %p\n", (void*) data->data_out,
131 			(void*) (data->data_out + data->input_frames * psrc->channels), (void*) data->data_in) ;-*/
132 		return SRC_ERR_DATA_OVERLAP ;
133 		} ;
134 
135 	/* Set the input and output counts to zero. */
136 	data->input_frames_used = 0 ;
137 	data->output_frames_gen = 0 ;
138 
139 	/* Special case for when last_ratio has not been set. */
140 	if (psrc->last_ratio < (1.0 / SRC_MAX_RATIO))
141 		psrc->last_ratio = data->src_ratio ;
142 
143 	/* Now process. */
144 	if (fabs (psrc->last_ratio - data->src_ratio) < 1e-15)
145 		error = psrc->const_process (psrc, data) ;
146 	else
147 		error = psrc->vari_process (psrc, data) ;
148 
149 	return error ;
150 } /* src_process */
151 
152 long
src_callback_read(SRC_STATE * state,double src_ratio,long frames,float * data)153 src_callback_read (SRC_STATE *state, double src_ratio, long frames, float *data)
154 {	SRC_PRIVATE	*psrc ;
155 	SRC_DATA	src_data ;
156 
157 	long	output_frames_gen ;
158 	int		error = 0 ;
159 
160 	if (state == NULL)
161 		return 0 ;
162 
163 	if (frames <= 0)
164 		return 0 ;
165 
166 	psrc = (SRC_PRIVATE*) state ;
167 
168 	if (psrc->mode != SRC_MODE_CALLBACK)
169 	{	psrc->error = SRC_ERR_BAD_MODE ;
170 		return 0 ;
171 		} ;
172 
173 	if (psrc->callback_func == NULL)
174 	{	psrc->error = SRC_ERR_NULL_CALLBACK ;
175 		return 0 ;
176 		} ;
177 
178 	memset (&src_data, 0, sizeof (src_data)) ;
179 
180 	/* Check src_ratio is in range. */
181 	if (is_bad_src_ratio (src_ratio))
182 	{	psrc->error = SRC_ERR_BAD_SRC_RATIO ;
183 		return 0 ;
184 		} ;
185 
186 	/* Switch modes temporarily. */
187 	src_data.src_ratio = src_ratio ;
188 	src_data.data_out = data ;
189 	src_data.output_frames = frames ;
190 
191 	src_data.data_in = psrc->saved_data ;
192 	src_data.input_frames = psrc->saved_frames ;
193 
194 	output_frames_gen = 0 ;
195 	while (output_frames_gen < frames)
196 	{	/*	Use a dummy array for the case where the callback function
197 		**	returns without setting the ptr.
198 		*/
199 		float dummy [1] ;
200 
201 		if (src_data.input_frames == 0)
202 		{	float *ptr = dummy ;
203 
204 			src_data.input_frames = psrc->callback_func (psrc->user_callback_data, &ptr) ;
205 			src_data.data_in = ptr ;
206 
207 			if (src_data.input_frames == 0)
208 				src_data.end_of_input = 1 ;
209 			} ;
210 
211 		/*
212 		** Now call process function. However, we need to set the mode
213 		** to SRC_MODE_PROCESS first and when we return set it back to
214 		** SRC_MODE_CALLBACK.
215 		*/
216 		psrc->mode = SRC_MODE_PROCESS ;
217 		error = src_process (state, &src_data) ;
218 		psrc->mode = SRC_MODE_CALLBACK ;
219 
220 		if (error != 0)
221 			break ;
222 
223 		src_data.data_in += src_data.input_frames_used * psrc->channels ;
224 		src_data.input_frames -= src_data.input_frames_used ;
225 
226 		src_data.data_out += src_data.output_frames_gen * psrc->channels ;
227 		src_data.output_frames -= src_data.output_frames_gen ;
228 
229 		output_frames_gen += src_data.output_frames_gen ;
230 
231 		if (src_data.end_of_input == SRC_TRUE && src_data.output_frames_gen == 0)
232 			break ;
233 		} ;
234 
235 	psrc->saved_data = src_data.data_in ;
236 	psrc->saved_frames = src_data.input_frames ;
237 
238 	if (error != 0)
239 	{	psrc->error = error ;
240 	 	return 0 ;
241 		} ;
242 
243 	return output_frames_gen ;
244 } /* src_callback_read */
245 
246 /*==========================================================================
247 */
248 
249 int
src_set_ratio(SRC_STATE * state,double new_ratio)250 src_set_ratio (SRC_STATE *state, double new_ratio)
251 {	SRC_PRIVATE *psrc ;
252 
253 	psrc = (SRC_PRIVATE*) state ;
254 
255 	if (psrc == NULL)
256 		return SRC_ERR_BAD_STATE ;
257 	if (psrc->vari_process == NULL || psrc->const_process == NULL)
258 		return SRC_ERR_BAD_PROC_PTR ;
259 
260 	if (is_bad_src_ratio (new_ratio))
261 		return SRC_ERR_BAD_SRC_RATIO ;
262 
263 	psrc->last_ratio = new_ratio ;
264 
265 	return SRC_ERR_NO_ERROR ;
266 } /* src_set_ratio */
267 
268 int
src_get_channels(SRC_STATE * state)269 src_get_channels (SRC_STATE *state)
270 {	SRC_PRIVATE *psrc ;
271 
272 	psrc = (SRC_PRIVATE*) state ;
273 
274 	if (psrc == NULL)
275 		return SRC_ERR_BAD_STATE ;
276 	if (psrc->vari_process == NULL || psrc->const_process == NULL)
277 		return SRC_ERR_BAD_PROC_PTR ;
278 
279 	return psrc->channels ;
280 } /* src_get_channels */
281 
282 int
src_reset(SRC_STATE * state)283 src_reset (SRC_STATE *state)
284 {	SRC_PRIVATE *psrc ;
285 
286 	if ((psrc = (SRC_PRIVATE*) state) == NULL)
287 		return SRC_ERR_BAD_STATE ;
288 
289 	if (psrc->reset != NULL)
290 		psrc->reset (psrc) ;
291 
292 	psrc->last_position = 0.0 ;
293 	psrc->last_ratio = 0.0 ;
294 
295 	psrc->saved_data = NULL ;
296 	psrc->saved_frames = 0 ;
297 
298 	psrc->error = SRC_ERR_NO_ERROR ;
299 
300 	return SRC_ERR_NO_ERROR ;
301 } /* src_reset */
302 
303 /*==============================================================================
304 **	Control functions.
305 */
306 
307 const char *
src_get_name(int converter_type)308 src_get_name (int converter_type)
309 {	const char *desc ;
310 
311 	if ((desc = sinc_get_name (converter_type)) != NULL)
312 		return desc ;
313 
314 	if ((desc = zoh_get_name (converter_type)) != NULL)
315 		return desc ;
316 
317 	if ((desc = linear_get_name (converter_type)) != NULL)
318 		return desc ;
319 
320 	return NULL ;
321 } /* src_get_name */
322 
323 const char *
src_get_description(int converter_type)324 src_get_description (int converter_type)
325 {	const char *desc ;
326 
327 	if ((desc = sinc_get_description (converter_type)) != NULL)
328 		return desc ;
329 
330 	if ((desc = zoh_get_description (converter_type)) != NULL)
331 		return desc ;
332 
333 	if ((desc = linear_get_description (converter_type)) != NULL)
334 		return desc ;
335 
336 	return NULL ;
337 } /* src_get_description */
338 
339 const char *
src_get_version(void)340 src_get_version (void)
341 {	return PACKAGE "-" VERSION " (c) 2002-2008 Erik de Castro Lopo" ;
342 } /* src_get_version */
343 
344 int
src_is_valid_ratio(double ratio)345 src_is_valid_ratio (double ratio)
346 {
347 	if (is_bad_src_ratio (ratio))
348 		return SRC_FALSE ;
349 
350 	return SRC_TRUE ;
351 } /* src_is_valid_ratio */
352 
353 /*==============================================================================
354 **	Error reporting functions.
355 */
356 
357 int
src_error(SRC_STATE * state)358 src_error (SRC_STATE *state)
359 {	if (state)
360 		return ((SRC_PRIVATE*) state)->error ;
361 	return SRC_ERR_NO_ERROR ;
362 } /* src_error */
363 
364 const char*
src_strerror(int error)365 src_strerror (int error)
366 {
367 	switch (error)
368 	{	case SRC_ERR_NO_ERROR :
369 				return "No error." ;
370 		case SRC_ERR_MALLOC_FAILED :
371 				return "Malloc failed." ;
372 		case SRC_ERR_BAD_STATE :
373 				return "SRC_STATE pointer is NULL." ;
374 		case SRC_ERR_BAD_DATA :
375 				return "SRC_DATA pointer is NULL." ;
376 		case SRC_ERR_BAD_DATA_PTR :
377 				return "SRC_DATA->data_out or SRC_DATA->data_in is NULL." ;
378 		case SRC_ERR_NO_PRIVATE :
379 				return "Internal error. No private data." ;
380 
381 		case SRC_ERR_BAD_SRC_RATIO :
382 				return "SRC ratio outside [1/" SRC_MAX_RATIO_STR ", " SRC_MAX_RATIO_STR "] range." ;
383 
384 		case SRC_ERR_BAD_SINC_STATE :
385 				return "src_process() called without reset after end_of_input." ;
386 		case SRC_ERR_BAD_PROC_PTR :
387 				return "Internal error. No process pointer." ;
388 		case SRC_ERR_SHIFT_BITS :
389 				return "Internal error. SHIFT_BITS too large." ;
390 		case SRC_ERR_FILTER_LEN :
391 				return "Internal error. Filter length too large." ;
392 		case SRC_ERR_BAD_CONVERTER :
393 				return "Bad converter number." ;
394 		case SRC_ERR_BAD_CHANNEL_COUNT :
395 				return "Channel count must be >= 1." ;
396 		case SRC_ERR_SINC_BAD_BUFFER_LEN :
397 				return "Internal error. Bad buffer length. Please report this." ;
398 		case SRC_ERR_SIZE_INCOMPATIBILITY :
399 				return "Internal error. Input data / internal buffer size difference. Please report this." ;
400 		case SRC_ERR_BAD_PRIV_PTR :
401 				return "Internal error. Private pointer is NULL. Please report this." ;
402 		case SRC_ERR_DATA_OVERLAP :
403 				return "Input and output data arrays overlap." ;
404 		case SRC_ERR_BAD_CALLBACK :
405 				return "Supplied callback function pointer is NULL." ;
406 		case SRC_ERR_BAD_MODE :
407 				return "Calling mode differs from initialisation mode (ie process v callback)." ;
408 		case SRC_ERR_NULL_CALLBACK :
409 				return "Callback function pointer is NULL in src_callback_read ()." ;
410 		case SRC_ERR_NO_VARIABLE_RATIO :
411 				return "This converter only allows constant conversion ratios." ;
412 		case SRC_ERR_SINC_PREPARE_DATA_BAD_LEN :
413 				return "Internal error : Bad length in prepare_data ()." ;
414 		case SRC_ERR_BAD_INTERNAL_STATE :
415 				return "Error : Someone is trampling on my internal state." ;
416 
417 		case SRC_ERR_MAX_ERROR :
418 				return "Placeholder. No error defined for this error number." ;
419 
420 		default : 						break ;
421 		}
422 
423 	return NULL ;
424 } /* src_strerror */
425 
426 /*==============================================================================
427 **	Simple interface for performing a single conversion from input buffer to
428 **	output buffer at a fixed conversion ratio.
429 */
430 
431 int
src_simple(SRC_DATA * src_data,int converter,int channels)432 src_simple (SRC_DATA *src_data, int converter, int channels)
433 {	SRC_STATE	*src_state ;
434 	int 		error ;
435 
436 	if ((src_state = src_new (converter, channels, &error)) == NULL)
437 		return error ;
438 
439 	src_data->end_of_input = 1 ; /* Only one buffer worth of input. */
440 
441 	error = src_process (src_state, src_data) ;
442 
443 	src_delete (src_state) ;
444 
445 	return error ;
446 } /* src_simple */
447 
448 void
src_short_to_float_array(const short * in,float * out,int len)449 src_short_to_float_array (const short *in, float *out, int len)
450 {
451 	while (len)
452 	{	len -- ;
453 		out [len] = (float) (in [len] / (1.0 * 0x8000)) ;
454 		} ;
455 
456 	return ;
457 } /* src_short_to_float_array */
458 
459 void
src_float_to_short_array(const float * in,short * out,int len)460 src_float_to_short_array (const float *in, short *out, int len)
461 {	double scaled_value ;
462 
463 	while (len)
464 	{	len -- ;
465 
466 		scaled_value = in [len] * (8.0 * 0x10000000) ;
467 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
468 		{	out [len] = 32767 ;
469 			continue ;
470 			} ;
471 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
472 		{	out [len] = -32768 ;
473 			continue ;
474 			} ;
475 
476 		out [len] = (short) (lrint (scaled_value) >> 16) ;
477 		} ;
478 
479 } /* src_float_to_short_array */
480 
481 void
src_int_to_float_array(const int * in,float * out,int len)482 src_int_to_float_array (const int *in, float *out, int len)
483 {
484 	while (len)
485 	{	len -- ;
486 		out [len] = (float) (in [len] / (8.0 * 0x10000000)) ;
487 		} ;
488 
489 	return ;
490 } /* src_int_to_float_array */
491 
492 void
src_float_to_int_array(const float * in,int * out,int len)493 src_float_to_int_array (const float *in, int *out, int len)
494 {	double scaled_value ;
495 
496 	while (len)
497 	{	len -- ;
498 
499 		scaled_value = in [len] * (8.0 * 0x10000000) ;
500 		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
501 		{	out [len] = 0x7fffffff ;
502 			continue ;
503 			} ;
504 		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
505 		{	out [len] = -1 - 0x7fffffff ;
506 			continue ;
507 			} ;
508 
509 		out [len] = lrint (scaled_value) ;
510 		} ;
511 
512 } /* src_float_to_int_array */
513 
514 /*==============================================================================
515 **	Private functions.
516 */
517 
518 static int
psrc_set_converter(SRC_PRIVATE * psrc,int converter_type)519 psrc_set_converter (SRC_PRIVATE	*psrc, int converter_type)
520 {
521 	if (sinc_set_converter (psrc, converter_type) == SRC_ERR_NO_ERROR)
522 		return SRC_ERR_NO_ERROR ;
523 
524 	if (zoh_set_converter (psrc, converter_type) == SRC_ERR_NO_ERROR)
525 		return SRC_ERR_NO_ERROR ;
526 
527 	if (linear_set_converter (psrc, converter_type) == SRC_ERR_NO_ERROR)
528 		return SRC_ERR_NO_ERROR ;
529 
530 	return SRC_ERR_BAD_CONVERTER ;
531 } /* psrc_set_converter */
532 
533