ReactOS 0.4.15-dev-7842-g558ab78
samplerate.c
Go to the documentation of this file.
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
11static int psrc_set_converter (SRC_PRIVATE *psrc, int converter_type) ;
12
13
15src_new (int converter_type, int channels, int *error)
16{ SRC_PRIVATE *psrc ;
17
18 if (error)
20
21 if (channels < 1)
22 { if (error)
24 return NULL ;
25 } ;
26
27 if ((psrc = calloc (1, sizeof (*psrc))) == NULL)
28 { if (error)
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)
39 free (psrc) ;
40 psrc = NULL ;
41 } ;
42
43 src_reset ((SRC_STATE*) psrc) ;
44
45 return (SRC_STATE*) psrc ;
46} /* src_new */
47
49src_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)
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
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
88int
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)
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
152long
153src_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
249int
250src_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
268int
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
282int
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
307const char *
308src_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
323const char *
324src_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
339const char *
341{ return PACKAGE "-" VERSION " (c) 2002-2008 Erik de Castro Lopo" ;
342} /* src_get_version */
343
344int
345src_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
357int
359{ if (state)
360 return ((SRC_PRIVATE*) state)->error ;
361 return SRC_ERR_NO_ERROR ;
362} /* src_error */
363
364const char*
366{
367 switch (error)
368 { case SRC_ERR_NO_ERROR :
369 return "No error." ;
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." ;
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
382 return "SRC ratio outside [1/" SRC_MAX_RATIO_STR ", " SRC_MAX_RATIO_STR "] range." ;
383
385 return "src_process() called without reset after end_of_input." ;
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." ;
393 return "Bad converter number." ;
395 return "Channel count must be >= 1." ;
397 return "Internal error. Bad buffer length. Please report this." ;
399 return "Internal error. Input data / internal buffer size difference. Please report this." ;
401 return "Internal error. Private pointer is NULL. Please report this." ;
403 return "Input and output data arrays overlap." ;
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)." ;
409 return "Callback function pointer is NULL in src_callback_read ()." ;
411 return "This converter only allows constant conversion ratios." ;
413 return "Internal error : Bad length in prepare_data ()." ;
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
431int
432src_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
448void
449src_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
459void
460src_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
481void
482src_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
492void
493src_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
518static int
519psrc_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
static int state
Definition: maze.c:121
#define VERSION
Definition: rdesktop.h:45
return
Definition: dirsup.c:529
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
Definition: typeof.h:94
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum func
Definition: glext.h:6028
GLuint in
Definition: glext.h:9616
GLenum GLsizei len
Definition: glext.h:6722
_Check_return_ _CRT_JIT_INTRINSIC double __cdecl fabs(_In_ double x)
Definition: fabs.c:17
_Check_return_ __CRT_INLINE long lrint(_In_ double x)
Definition: math.h:292
#define e
Definition: ke_i.h:82
if(dx< 0)
Definition: linetemp.h:194
#define error(str)
Definition: mkdosfs.c:1605
#define PACKAGE
Definition: config.h:21
static PVOID ptr
Definition: dispmode.c:27
static const WCHAR desc[]
Definition: protectdata.c:36
static float(__cdecl *square_half_float)(float x
int This channels
Definition: rdpsnd_libao.c:37
static FILE * out
Definition: regtests2xml.c:44
#define calloc
Definition: rosglue.h:14
int src_error(SRC_STATE *state)
Definition: samplerate.c:358
SRC_STATE * src_callback_new(src_callback_t func, int converter_type, int channels, int *error, void *cb_data)
Definition: samplerate.c:49
int src_set_ratio(SRC_STATE *state, double new_ratio)
Definition: samplerate.c:250
void src_int_to_float_array(const int *in, float *out, int len)
Definition: samplerate.c:482
int src_is_valid_ratio(double ratio)
Definition: samplerate.c:345
const char * src_get_version(void)
Definition: samplerate.c:340
void src_float_to_short_array(const float *in, short *out, int len)
Definition: samplerate.c:460
static int psrc_set_converter(SRC_PRIVATE *psrc, int converter_type)
Definition: samplerate.c:519
void src_short_to_float_array(const short *in, float *out, int len)
Definition: samplerate.c:449
int src_simple(SRC_DATA *src_data, int converter, int channels)
Definition: samplerate.c:432
int src_reset(SRC_STATE *state)
Definition: samplerate.c:283
const char * src_get_name(int converter_type)
Definition: samplerate.c:308
long src_callback_read(SRC_STATE *state, double src_ratio, long frames, float *data)
Definition: samplerate.c:153
void src_float_to_int_array(const float *in, int *out, int len)
Definition: samplerate.c:493
int src_process(SRC_STATE *state, SRC_DATA *data)
Definition: samplerate.c:89
SRC_STATE * src_delete(SRC_STATE *state)
Definition: samplerate.c:74
const char * src_strerror(int error)
Definition: samplerate.c:365
SRC_STATE * src_new(int converter_type, int channels, int *error)
Definition: samplerate.c:15
const char * src_get_description(int converter_type)
Definition: samplerate.c:324
int src_get_channels(SRC_STATE *state)
Definition: samplerate.c:269
struct SRC_STATE_tag SRC_STATE
Definition: samplerate.h:23
long(* src_callback_t)(void *cb_data, float **data)
Definition: samplerate.h:47
#define SRC_MAX_RATIO_STR
Definition: common.h:21
static int is_bad_src_ratio(double ratio)
Definition: common.h:154
#define SRC_MAX_RATIO
Definition: common.h:20
const char * sinc_get_description(int src_enum)
Definition: src_sinc.c:113
const char * zoh_get_description(int src_enum)
Definition: src_zoh.c:137
@ SRC_MODE_CALLBACK
Definition: common.h:59
@ SRC_MODE_PROCESS
Definition: common.h:58
@ SRC_FALSE
Definition: common.h:55
@ SRC_TRUE
Definition: common.h:56
int sinc_set_converter(SRC_PRIVATE *psrc, int src_enum)
Definition: src_sinc.c:133
const char * zoh_get_name(int src_enum)
Definition: src_zoh.c:128
int linear_set_converter(SRC_PRIVATE *psrc, int src_enum)
Definition: src_linear.c:155
const char * sinc_get_name(int src_enum)
Definition: src_sinc.c:94
@ SRC_ERR_DATA_OVERLAP
Definition: common.h:80
@ SRC_ERR_SIZE_INCOMPATIBILITY
Definition: common.h:77
@ SRC_ERR_FILTER_LEN
Definition: common.h:73
@ SRC_ERR_NO_VARIABLE_RATIO
Definition: common.h:84
@ SRC_ERR_NO_ERROR
Definition: common.h:63
@ SRC_ERR_BAD_SINC_STATE
Definition: common.h:79
@ SRC_ERR_BAD_PROC_PTR
Definition: common.h:71
@ SRC_ERR_BAD_INTERNAL_STATE
Definition: common.h:86
@ SRC_ERR_BAD_DATA_PTR
Definition: common.h:68
@ SRC_ERR_MAX_ERROR
Definition: common.h:89
@ SRC_ERR_BAD_STATE
Definition: common.h:66
@ SRC_ERR_BAD_SRC_RATIO
Definition: common.h:70
@ SRC_ERR_NULL_CALLBACK
Definition: common.h:83
@ SRC_ERR_BAD_CHANNEL_COUNT
Definition: common.h:75
@ SRC_ERR_BAD_MODE
Definition: common.h:82
@ SRC_ERR_SINC_PREPARE_DATA_BAD_LEN
Definition: common.h:85
@ SRC_ERR_BAD_CONVERTER
Definition: common.h:74
@ SRC_ERR_SINC_BAD_BUFFER_LEN
Definition: common.h:76
@ SRC_ERR_NO_PRIVATE
Definition: common.h:69
@ SRC_ERR_SHIFT_BITS
Definition: common.h:72
@ SRC_ERR_BAD_DATA
Definition: common.h:67
@ SRC_ERR_BAD_CALLBACK
Definition: common.h:81
@ SRC_ERR_BAD_PRIV_PTR
Definition: common.h:78
@ SRC_ERR_MALLOC_FAILED
Definition: common.h:65
const char * linear_get_name(int src_enum)
Definition: src_linear.c:137
int zoh_set_converter(SRC_PRIVATE *psrc, int src_enum)
Definition: src_zoh.c:146
const char * linear_get_description(int src_enum)
Definition: src_linear.c:146
#define CPU_CLIPS_NEGATIVE
Definition: config.h:41
#define CPU_CLIPS_POSITIVE
Definition: config.h:44
#define memset(x, y, z)
Definition: compat.h:39
long input_frames
Definition: samplerate.h:30
float * data_out
Definition: samplerate.h:28
long output_frames_gen
Definition: samplerate.h:31
long output_frames
Definition: samplerate.h:30
double src_ratio
Definition: samplerate.h:35
long input_frames_used
Definition: samplerate.h:31
const float * data_in
Definition: samplerate.h:27
int end_of_input
Definition: samplerate.h:33
double last_position
Definition: common.h:93
long saved_frames
Definition: common.h:116
int(* const_process)(struct SRC_PRIVATE_tag *psrc, SRC_DATA *data)
Definition: common.h:108
int(* vari_process)(struct SRC_PRIVATE_tag *psrc, SRC_DATA *data)
Definition: common.h:105
double last_ratio
Definition: common.h:93
const float * saved_data
Definition: common.h:117
src_callback_t callback_func
Definition: common.h:114
void * user_callback_data
Definition: common.h:115
void(* reset)(struct SRC_PRIVATE_tag *psrc)
Definition: common.h:111
int channels
Definition: common.h:96
void * private_data
Definition: common.h:102