| 1 |
/* |
| 2 |
** Copyright (c) 2002-2016, Erik de Castro Lopo <[email protected]> |
| 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 |
#ifdef HAVE_CONFIG_H |
| 10 |
#include "config.h" |
| 11 |
#endif |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include <samplerate.h> |
| 18 |
|
| 19 |
#include "util.h" |
| 20 |
|
| 21 |
#define BUFFER_LEN (1 << 16) |
| 22 |
#define NUM_CHANNELS 2 |
| 23 |
#define FRAMES_PER_PASS (BUFFER_LEN >> 1) |
| 24 |
|
| 25 |
static void |
| 26 |
clone_test (int converter) |
| 27 |
{ static float input_serial [BUFFER_LEN * NUM_CHANNELS], input_interleaved [BUFFER_LEN * NUM_CHANNELS] ; |
| 28 |
static float output [BUFFER_LEN * NUM_CHANNELS], output_cloned [BUFFER_LEN * NUM_CHANNELS] ; |
| 29 |
double sine_freq ; |
| 30 |
|
| 31 |
SRC_STATE* src_state ; |
| 32 |
SRC_STATE* src_state_cloned ; |
| 33 |
SRC_DATA src_data, src_data_cloned ; |
| 34 |
|
| 35 |
int error, frame, ch, idx ; |
| 36 |
|
| 37 |
printf (" clone_test (%-28s) ....... ", src_get_name (converter)) ; |
| 38 |
fflush (stdout) ; |
| 39 |
|
| 40 |
memset (input_serial, 0, sizeof (input_serial)) ; |
| 41 |
memset (input_interleaved, 0, sizeof (input_interleaved)) ; |
| 42 |
memset (output, 0, sizeof (output)) ; |
| 43 |
memset (output_cloned, 0, sizeof (output_cloned)) ; |
| 44 |
|
| 45 |
/* Fill input buffer with an n-channel interleaved sine wave */ |
| 46 |
sine_freq = 0.0111 ; |
| 47 |
gen_windowed_sines (1, &sine_freq, 1.0, input_serial, BUFFER_LEN) ; |
| 48 |
gen_windowed_sines (1, &sine_freq, 1.0, input_serial + BUFFER_LEN, BUFFER_LEN) ; |
| 49 |
interleave_data (input_serial, input_interleaved, BUFFER_LEN, NUM_CHANNELS) ; |
| 50 |
|
| 51 |
if ((src_state = src_new (converter, NUM_CHANNELS, &error)) == NULL) |
| 52 |
{ printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ; |
| 53 |
exit (1) ; |
| 54 |
} ; |
| 55 |
|
| 56 |
/* Perform initial pass using first half of buffer so that src_state has non-trivial state */ |
| 57 |
src_data.src_ratio = 1.1 ; |
| 58 |
src_data.input_frames = FRAMES_PER_PASS ; |
| 59 |
src_data.output_frames = FRAMES_PER_PASS ; |
| 60 |
src_data.data_in = input_interleaved ; |
| 61 |
src_data.data_out = output ; |
| 62 |
src_data.output_frames_gen = 0 ; |
| 63 |
|
| 64 |
if ((error = src_process (src_state, &src_data))) |
| 65 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 66 |
printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; |
| 67 |
printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; |
| 68 |
exit (1) ; |
| 69 |
} ; |
| 70 |
|
| 71 |
/* Clone handle */ |
| 72 |
if ((src_state_cloned = src_clone (src_state, &error)) == NULL) |
| 73 |
{ printf ("\n\nLine %d : src_clone() failed : %s\n\n", __LINE__, src_strerror (error)) ; |
| 74 |
exit (1) ; |
| 75 |
} ; |
| 76 |
|
| 77 |
/* Process second half of buffer with both handles */ |
| 78 |
src_data.data_in = input_interleaved + FRAMES_PER_PASS ; |
| 79 |
|
| 80 |
src_data_cloned = src_data ; |
| 81 |
src_data_cloned.data_out = output_cloned ; |
| 82 |
|
| 83 |
if ((error = src_process (src_state, &src_data))) |
| 84 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 85 |
printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; |
| 86 |
printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; |
| 87 |
exit (1) ; |
| 88 |
} ; |
| 89 |
|
| 90 |
if ((error = src_process (src_state_cloned, &src_data_cloned))) |
| 91 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 92 |
printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; |
| 93 |
printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; |
| 94 |
exit (1) ; |
| 95 |
} ; |
| 96 |
|
| 97 |
/* Check that both handles generated the same number of output frames */ |
| 98 |
if (src_data.output_frames_gen != src_data_cloned.output_frames_gen) |
| 99 |
{ printf ("\n\nLine %d : cloned output_frames_gen (%ld) != original (%ld)\n\n", __LINE__, |
| 100 |
src_data_cloned.output_frames_gen, src_data.output_frames_gen) ; |
| 101 |
exit (1) ; |
| 102 |
} ; |
| 103 |
|
| 104 |
for (ch = 0 ; ch < NUM_CHANNELS ; ch++) |
| 105 |
{ for (frame = 0 ; frame < src_data.output_frames_gen ; frame++) |
| 106 |
{ idx = ch * NUM_CHANNELS + ch ; |
| 107 |
if (output[idx] != output_cloned[idx]) |
| 108 |
{ printf ("\n\nLine %d : cloned data does not equal original data at channel %d, frame %d\n\n", |
| 109 |
__LINE__, ch, frame) ; |
| 110 |
exit (1) ; |
| 111 |
} ; |
| 112 |
} ; |
| 113 |
} ; |
| 114 |
|
| 115 |
src_delete (src_state) ; |
| 116 |
src_delete (src_state_cloned) ; |
| 117 |
|
| 118 |
puts ("ok") ; |
| 119 |
} /* clone_test */ |
| 120 |
|
| 121 |
int |
| 122 |
main (void) |
| 123 |
{ |
| 124 |
puts(""); |
| 125 |
|
| 126 |
clone_test (SRC_ZERO_ORDER_HOLD) ; |
| 127 |
clone_test (SRC_LINEAR) ; |
| 128 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 129 |
clone_test (SRC_SINC_FASTEST) ; |
| 130 |
#endif |
| 131 |
puts(""); |
| 132 |
|
| 133 |
return 0 ; |
| 134 |
} /* main */ |