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