| 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 |
|
| 16 |
#include <samplerate.h> |
| 17 |
|
| 18 |
#include "util.h" |
| 19 |
|
| 20 |
#define BUFFER_LEN 2048 |
| 21 |
#define CB_READ_LEN 256 |
| 22 |
|
| 23 |
static void process_reset_test (int converter) ; |
| 24 |
static void callback_reset_test (int converter) ; |
| 25 |
|
| 26 |
static float data_one [BUFFER_LEN] ; |
| 27 |
static float data_zero [BUFFER_LEN] ; |
| 28 |
|
| 29 |
int |
| 30 |
main (void) |
| 31 |
{ |
| 32 |
puts ("") ; |
| 33 |
|
| 34 |
process_reset_test (SRC_ZERO_ORDER_HOLD) ; |
| 35 |
process_reset_test (SRC_LINEAR) ; |
| 36 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 37 |
process_reset_test (SRC_SINC_FASTEST) ; |
| 38 |
#endif |
| 39 |
|
| 40 |
callback_reset_test (SRC_ZERO_ORDER_HOLD) ; |
| 41 |
callback_reset_test (SRC_LINEAR) ; |
| 42 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 43 |
callback_reset_test (SRC_SINC_FASTEST) ; |
| 44 |
#endif |
| 45 |
|
| 46 |
puts ("") ; |
| 47 |
|
| 48 |
return 0 ; |
| 49 |
} /* main */ |
| 50 |
|
| 51 |
static void |
| 52 |
process_reset_test (int converter) |
| 53 |
{ static float output [BUFFER_LEN] ; |
| 54 |
|
| 55 |
SRC_STATE *src_state ; |
| 56 |
SRC_DATA src_data ; |
| 57 |
int k, error ; |
| 58 |
|
| 59 |
printf ("\tprocess_reset_test (%-28s) ....... ", src_get_name (converter)) ; |
| 60 |
fflush (stdout) ; |
| 61 |
|
| 62 |
for (k = 0 ; k < BUFFER_LEN ; k++) |
| 63 |
{ data_one [k] = 1.0 ; |
| 64 |
data_zero [k] = 0.0 ; |
| 65 |
} ; |
| 66 |
|
| 67 |
/* Get a converter. */ |
| 68 |
if ((src_state = src_new (converter, 1, &error)) == NULL) |
| 69 |
{ printf ("\n\nLine %d : src_new() failed : %s.\n\n", __LINE__, src_strerror (error)) ; |
| 70 |
exit (1) ; |
| 71 |
} ; |
| 72 |
|
| 73 |
/* Process a bunch of 1.0 valued samples. */ |
| 74 |
src_data.data_in = data_one ; |
| 75 |
src_data.data_out = output ; |
| 76 |
src_data.input_frames = BUFFER_LEN ; |
| 77 |
src_data.output_frames = BUFFER_LEN ; |
| 78 |
src_data.src_ratio = 0.9 ; |
| 79 |
src_data.end_of_input = 1 ; |
| 80 |
|
| 81 |
if ((error = src_process (src_state, &src_data)) != 0) |
| 82 |
{ printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ; |
| 83 |
exit (1) ; |
| 84 |
} ; |
| 85 |
|
| 86 |
/* Reset the state of the converter.*/ |
| 87 |
src_reset (src_state) ; |
| 88 |
|
| 89 |
/* Now process some zero data. */ |
| 90 |
src_data.data_in = data_zero ; |
| 91 |
src_data.data_out = output ; |
| 92 |
src_data.input_frames = BUFFER_LEN ; |
| 93 |
src_data.output_frames = BUFFER_LEN ; |
| 94 |
src_data.src_ratio = 0.9 ; |
| 95 |
src_data.end_of_input = 1 ; |
| 96 |
|
| 97 |
if ((error = src_process (src_state, &src_data)) != 0) |
| 98 |
{ printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ; |
| 99 |
exit (1) ; |
| 100 |
} ; |
| 101 |
|
| 102 |
/* Finally make sure that the output data is zero ie reset was sucessful. */ |
| 103 |
for (k = 0 ; k < BUFFER_LEN / 2 ; k++) |
| 104 |
if (output [k] != 0.0) |
| 105 |
{ printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n", __LINE__, k, output [k]) ; |
| 106 |
exit (1) ; |
| 107 |
} ; |
| 108 |
|
| 109 |
/* Make sure that this function has been exported. */ |
| 110 |
src_set_ratio (src_state, 1.0) ; |
| 111 |
|
| 112 |
/* Delete converter. */ |
| 113 |
src_state = src_delete (src_state) ; |
| 114 |
|
| 115 |
puts ("ok") ; |
| 116 |
} /* process_reset_test */ |
| 117 |
|
| 118 |
/*============================================================================== |
| 119 |
*/ |
| 120 |
|
| 121 |
typedef struct |
| 122 |
{ int channels ; |
| 123 |
long count, total ; |
| 124 |
float *data ; |
| 125 |
} TEST_CB_DATA ; |
| 126 |
|
| 127 |
static long |
| 128 |
test_callback_func (void *cb_data, float **data) |
| 129 |
{ TEST_CB_DATA *pcb_data ; |
| 130 |
|
| 131 |
long frames ; |
| 132 |
|
| 133 |
if ((pcb_data = cb_data) == NULL) |
| 134 |
return 0 ; |
| 135 |
|
| 136 |
if (data == NULL) |
| 137 |
return 0 ; |
| 138 |
|
| 139 |
if (pcb_data->total - pcb_data->count > 0) |
| 140 |
frames = pcb_data->total - pcb_data->count ; |
| 141 |
else |
| 142 |
frames = 0 ; |
| 143 |
|
| 144 |
*data = pcb_data->data + pcb_data->count ; |
| 145 |
pcb_data->count += frames ; |
| 146 |
|
| 147 |
return frames ; |
| 148 |
} /* test_callback_func */ |
| 149 |
|
| 150 |
static void |
| 151 |
callback_reset_test (int converter) |
| 152 |
{ static TEST_CB_DATA test_callback_data ; |
| 153 |
|
| 154 |
static float output [BUFFER_LEN] ; |
| 155 |
|
| 156 |
SRC_STATE *src_state ; |
| 157 |
|
| 158 |
double src_ratio = 1.1 ; |
| 159 |
long read_count, read_total ; |
| 160 |
int k, error ; |
| 161 |
|
| 162 |
printf ("\tcallback_reset_test (%-28s) ....... ", src_get_name (converter)) ; |
| 163 |
fflush (stdout) ; |
| 164 |
|
| 165 |
for (k = 0 ; k < ARRAY_LEN (data_one) ; k++) |
| 166 |
{ data_one [k] = 1.0 ; |
| 167 |
data_zero [k] = 0.0 ; |
| 168 |
} ; |
| 169 |
|
| 170 |
if ((src_state = src_callback_new (test_callback_func, converter, 1, &error, &test_callback_data)) == NULL) |
| 171 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 172 |
exit (1) ; |
| 173 |
} ; |
| 174 |
|
| 175 |
/* Process a bunch of 1.0 valued samples. */ |
| 176 |
test_callback_data.channels = 1 ; |
| 177 |
test_callback_data.count = 0 ; |
| 178 |
test_callback_data.total = ARRAY_LEN (data_one) ; |
| 179 |
test_callback_data.data = data_one ; |
| 180 |
|
| 181 |
read_total = 0 ; |
| 182 |
do |
| 183 |
{ read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ; |
| 184 |
read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ; |
| 185 |
read_total += read_count ; |
| 186 |
} |
| 187 |
while (read_count > 0) ; |
| 188 |
|
| 189 |
/* Check for errors. */ |
| 190 |
if ((error = src_error (src_state)) != 0) |
| 191 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 192 |
exit (1) ; |
| 193 |
} ; |
| 194 |
|
| 195 |
/* Reset the state of the converter.*/ |
| 196 |
src_reset (src_state) ; |
| 197 |
|
| 198 |
/* Process a bunch of 0.0 valued samples. */ |
| 199 |
test_callback_data.channels = 1 ; |
| 200 |
test_callback_data.count = 0 ; |
| 201 |
test_callback_data.total = ARRAY_LEN (data_zero) ; |
| 202 |
test_callback_data.data = data_zero ; |
| 203 |
|
| 204 |
/* Now process some zero data. */ |
| 205 |
read_total = 0 ; |
| 206 |
do |
| 207 |
{ read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ; |
| 208 |
read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ; |
| 209 |
read_total += read_count ; |
| 210 |
} |
| 211 |
while (read_count > 0) ; |
| 212 |
|
| 213 |
/* Check for errors. */ |
| 214 |
if ((error = src_error (src_state)) != 0) |
| 215 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 216 |
exit (1) ; |
| 217 |
} ; |
| 218 |
|
| 219 |
/* Finally make sure that the output data is zero ie reset was sucessful. */ |
| 220 |
for (k = 0 ; k < BUFFER_LEN / 2 ; k++) |
| 221 |
if (output [k] != 0.0) |
| 222 |
{ printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n\n", __LINE__, k, output [k]) ; |
| 223 |
save_oct_float ("output.dat", data_one, ARRAY_LEN (data_one), output, ARRAY_LEN (output)) ; |
| 224 |
exit (1) ; |
| 225 |
} ; |
| 226 |
|
| 227 |
/* Make sure that this function has been exported. */ |
| 228 |
src_set_ratio (src_state, 1.0) ; |
| 229 |
|
| 230 |
/* Delete converter. */ |
| 231 |
src_state = src_delete (src_state) ; |
| 232 |
|
| 233 |
puts ("ok") ; |
| 234 |
} /* callback_reset_test */ |
| 235 |
|
| 236 |
|