| 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 |
#include <math.h> |
| 17 |
#include <assert.h> |
| 18 |
|
| 19 |
#if (HAVE_FFTW3) |
| 20 |
#include <fftw3.h> |
| 21 |
#else |
| 22 |
static inline void |
| 23 |
fftw_cleanup (void) |
| 24 |
{ return ; |
| 25 |
} |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <samplerate.h> |
| 29 |
|
| 30 |
#include "util.h" |
| 31 |
#define BUFFER_LEN 50000 |
| 32 |
#define BLOCK_LEN (12) |
| 33 |
|
| 34 |
#define MAX_CHANNELS 10 |
| 35 |
|
| 36 |
static void simple_test (int converter, int channel_count, double target_snr) ; |
| 37 |
static void process_test (int converter, int channel_count, double target_snr) ; |
| 38 |
static void callback_test (int converter, int channel_count, double target_snr) ; |
| 39 |
|
| 40 |
int |
| 41 |
main (void) |
| 42 |
{ double target ; |
| 43 |
int k ; |
| 44 |
|
| 45 |
puts ("\n Zero Order Hold interpolator :") ; |
| 46 |
target = 38.0 ; |
| 47 |
for (k = 1 ; k <= 3 ; k++) |
| 48 |
{ simple_test (SRC_ZERO_ORDER_HOLD, k, target) ; |
| 49 |
process_test (SRC_ZERO_ORDER_HOLD, k, target) ; |
| 50 |
callback_test (SRC_ZERO_ORDER_HOLD, k, target) ; |
| 51 |
} ; |
| 52 |
|
| 53 |
puts ("\n Linear interpolator :") ; |
| 54 |
target = 79.0 ; |
| 55 |
for (k = 1 ; k <= 3 ; k++) |
| 56 |
{ simple_test (SRC_LINEAR, k, target) ; |
| 57 |
process_test (SRC_LINEAR, k, target) ; |
| 58 |
callback_test (SRC_LINEAR, k, target) ; |
| 59 |
} ; |
| 60 |
|
| 61 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 62 |
puts ("\n Sinc interpolator :") ; |
| 63 |
target = 100.0 ; |
| 64 |
for (k = 1 ; k <= MAX_CHANNELS ; k++) |
| 65 |
{ simple_test (SRC_SINC_FASTEST, k, target) ; |
| 66 |
process_test (SRC_SINC_FASTEST, k, target) ; |
| 67 |
callback_test (SRC_SINC_FASTEST, k, target) ; |
| 68 |
} ; |
| 69 |
#endif |
| 70 |
|
| 71 |
fftw_cleanup () ; |
| 72 |
puts ("") ; |
| 73 |
|
| 74 |
return 0 ; |
| 75 |
} /* main */ |
| 76 |
|
| 77 |
/*============================================================================== |
| 78 |
*/ |
| 79 |
|
| 80 |
static float input_serial [BUFFER_LEN * MAX_CHANNELS] ; |
| 81 |
static float input_interleaved [BUFFER_LEN * MAX_CHANNELS] ; |
| 82 |
static float output_interleaved [BUFFER_LEN * MAX_CHANNELS] ; |
| 83 |
static float output_serial [BUFFER_LEN * MAX_CHANNELS] ; |
| 84 |
|
| 85 |
static void |
| 86 |
simple_test (int converter, int channel_count, double target_snr) |
| 87 |
{ SRC_DATA src_data ; |
| 88 |
|
| 89 |
double freq, snr ; |
| 90 |
int ch, error, frames ; |
| 91 |
|
| 92 |
printf ("\t%-22s (%2d channel%c) ............ ", "simple_test", channel_count, channel_count > 1 ? 's' : ' ') ; |
| 93 |
fflush (stdout) ; |
| 94 |
|
| 95 |
assert (channel_count <= MAX_CHANNELS) ; |
| 96 |
|
| 97 |
memset (input_serial, 0, sizeof (input_serial)) ; |
| 98 |
memset (input_interleaved, 0, sizeof (input_interleaved)) ; |
| 99 |
memset (output_interleaved, 0, sizeof (output_interleaved)) ; |
| 100 |
memset (output_serial, 0, sizeof (output_serial)) ; |
| 101 |
|
| 102 |
frames = BUFFER_LEN ; |
| 103 |
|
| 104 |
/* Calculate channel_count separate windowed sine waves. */ |
| 105 |
for (ch = 0 ; ch < channel_count ; ch++) |
| 106 |
{ freq = (200.0 + 33.333333333 * ch) / 44100.0 ; |
| 107 |
gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; |
| 108 |
} ; |
| 109 |
|
| 110 |
/* Interleave the data in preparation for SRC. */ |
| 111 |
interleave_data (input_serial, input_interleaved, frames, channel_count) ; |
| 112 |
|
| 113 |
/* Choose a converstion ratio <= 1.0. */ |
| 114 |
src_data.src_ratio = 0.95 ; |
| 115 |
|
| 116 |
src_data.data_in = input_interleaved ; |
| 117 |
src_data.input_frames = frames ; |
| 118 |
|
| 119 |
src_data.data_out = output_interleaved ; |
| 120 |
src_data.output_frames = frames ; |
| 121 |
|
| 122 |
if ((error = src_simple (&src_data, converter, channel_count))) |
| 123 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 124 |
exit (1) ; |
| 125 |
} ; |
| 126 |
|
| 127 |
if (fabs (src_data.output_frames_gen - src_data.src_ratio * src_data.input_frames) > 2) |
| 128 |
{ printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__, |
| 129 |
src_data.output_frames_gen, (int) floor (src_data.src_ratio * src_data.input_frames)) ; |
| 130 |
printf ("\tsrc_ratio : %.4f\n", src_data.src_ratio) ; |
| 131 |
printf ("\tinput_len : %ld\n", src_data.input_frames) ; |
| 132 |
printf ("\toutput_len : %ld\n\n", src_data.output_frames_gen) ; |
| 133 |
exit (1) ; |
| 134 |
} ; |
| 135 |
|
| 136 |
/* De-interleave data so SNR can be calculated for each channel. */ |
| 137 |
deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; |
| 138 |
|
| 139 |
for (ch = 0 ; ch < channel_count ; ch++) |
| 140 |
{ snr = calculate_snr (output_serial + ch * frames, frames, 1) ; |
| 141 |
if (snr < target_snr) |
| 142 |
{ printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; |
| 143 |
save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; |
| 144 |
exit (1) ; |
| 145 |
} ; |
| 146 |
} ; |
| 147 |
|
| 148 |
puts ("ok") ; |
| 149 |
|
| 150 |
return ; |
| 151 |
} /* simple_test */ |
| 152 |
|
| 153 |
/*============================================================================== |
| 154 |
*/ |
| 155 |
|
| 156 |
static void |
| 157 |
process_test (int converter, int channel_count, double target_snr) |
| 158 |
{ SRC_STATE *src_state ; |
| 159 |
SRC_DATA src_data ; |
| 160 |
|
| 161 |
double freq, snr ; |
| 162 |
int ch, error, frames, current_in, current_out ; |
| 163 |
|
| 164 |
printf ("\t%-22s (%2d channel%c) ............ ", "process_test", channel_count, channel_count > 1 ? 's' : ' ') ; |
| 165 |
fflush (stdout) ; |
| 166 |
|
| 167 |
assert (channel_count <= MAX_CHANNELS) ; |
| 168 |
|
| 169 |
memset (input_serial, 0, sizeof (input_serial)) ; |
| 170 |
memset (input_interleaved, 0, sizeof (input_interleaved)) ; |
| 171 |
memset (output_interleaved, 0, sizeof (output_interleaved)) ; |
| 172 |
memset (output_serial, 0, sizeof (output_serial)) ; |
| 173 |
|
| 174 |
frames = BUFFER_LEN ; |
| 175 |
|
| 176 |
/* Calculate channel_count separate windowed sine waves. */ |
| 177 |
for (ch = 0 ; ch < channel_count ; ch++) |
| 178 |
{ freq = (400.0 + 11.333333333 * ch) / 44100.0 ; |
| 179 |
gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; |
| 180 |
} ; |
| 181 |
|
| 182 |
/* Interleave the data in preparation for SRC. */ |
| 183 |
interleave_data (input_serial, input_interleaved, frames, channel_count) ; |
| 184 |
|
| 185 |
/* Perform sample rate conversion. */ |
| 186 |
if ((src_state = src_new (converter, channel_count, &error)) == NULL) |
| 187 |
{ printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ; |
| 188 |
exit (1) ; |
| 189 |
} ; |
| 190 |
|
| 191 |
src_data.end_of_input = 0 ; /* Set this later. */ |
| 192 |
|
| 193 |
/* Choose a converstion ratio < 1.0. */ |
| 194 |
src_data.src_ratio = 0.95 ; |
| 195 |
|
| 196 |
src_data.data_in = input_interleaved ; |
| 197 |
src_data.data_out = output_interleaved ; |
| 198 |
|
| 199 |
current_in = current_out = 0 ; |
| 200 |
|
| 201 |
while (1) |
| 202 |
{ src_data.input_frames = MAX (MIN (BLOCK_LEN, frames - current_in), 0) ; |
| 203 |
src_data.output_frames = MAX (MIN (BLOCK_LEN, frames - current_out), 0) ; |
| 204 |
|
| 205 |
if ((error = src_process (src_state, &src_data))) |
| 206 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 207 |
exit (1) ; |
| 208 |
} ; |
| 209 |
|
| 210 |
if (src_data.end_of_input && src_data.output_frames_gen == 0) |
| 211 |
break ; |
| 212 |
|
| 213 |
current_in += src_data.input_frames_used ; |
| 214 |
current_out += src_data.output_frames_gen ; |
| 215 |
|
| 216 |
src_data.data_in += src_data.input_frames_used * channel_count ; |
| 217 |
src_data.data_out += src_data.output_frames_gen * channel_count ; |
| 218 |
|
| 219 |
src_data.end_of_input = (current_in >= frames) ? 1 : 0 ; |
| 220 |
} ; |
| 221 |
|
| 222 |
src_state = src_delete (src_state) ; |
| 223 |
|
| 224 |
if (fabs (current_out - src_data.src_ratio * current_in) > 2) |
| 225 |
{ printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__, |
| 226 |
current_out, (int) floor (src_data.src_ratio * current_in)) ; |
| 227 |
printf ("\tsrc_ratio : %.4f\n", src_data.src_ratio) ; |
| 228 |
printf ("\tinput_len : %d\n", frames) ; |
| 229 |
printf ("\toutput_len : %d\n\n", current_out) ; |
| 230 |
exit (1) ; |
| 231 |
} ; |
| 232 |
|
| 233 |
/* De-interleave data so SNR can be calculated for each channel. */ |
| 234 |
deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; |
| 235 |
|
| 236 |
for (ch = 0 ; ch < channel_count ; ch++) |
| 237 |
{ snr = calculate_snr (output_serial + ch * frames, frames, 1) ; |
| 238 |
if (snr < target_snr) |
| 239 |
{ printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; |
| 240 |
save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; |
| 241 |
exit (1) ; |
| 242 |
} ; |
| 243 |
} ; |
| 244 |
|
| 245 |
puts ("ok") ; |
| 246 |
|
| 247 |
return ; |
| 248 |
} /* process_test */ |
| 249 |
|
| 250 |
/*============================================================================== |
| 251 |
*/ |
| 252 |
|
| 253 |
typedef struct |
| 254 |
{ int channels ; |
| 255 |
long total_frames ; |
| 256 |
long current_frame ; |
| 257 |
float *data ; |
| 258 |
} TEST_CB_DATA ; |
| 259 |
|
| 260 |
static long |
| 261 |
test_callback_func (void *cb_data, float **data) |
| 262 |
{ TEST_CB_DATA *pcb_data ; |
| 263 |
|
| 264 |
long frames ; |
| 265 |
|
| 266 |
if ((pcb_data = cb_data) == NULL) |
| 267 |
return 0 ; |
| 268 |
|
| 269 |
if (data == NULL) |
| 270 |
return 0 ; |
| 271 |
|
| 272 |
*data = pcb_data->data + (pcb_data->current_frame * pcb_data->channels) ; |
| 273 |
|
| 274 |
if (pcb_data->total_frames - pcb_data->current_frame < BLOCK_LEN) |
| 275 |
frames = pcb_data->total_frames - pcb_data->current_frame ; |
| 276 |
else |
| 277 |
frames = BLOCK_LEN ; |
| 278 |
|
| 279 |
pcb_data->current_frame += frames ; |
| 280 |
|
| 281 |
return frames ; |
| 282 |
} /* test_callback_func */ |
| 283 |
|
| 284 |
static void |
| 285 |
callback_test (int converter, int channel_count, double target_snr) |
| 286 |
{ TEST_CB_DATA test_callback_data ; |
| 287 |
SRC_STATE *src_state = NULL ; |
| 288 |
|
| 289 |
double freq, snr, src_ratio ; |
| 290 |
int ch, error, frames, read_total, read_count ; |
| 291 |
|
| 292 |
printf ("\t%-22s (%2d channel%c) ............ ", "callback_test", channel_count, channel_count > 1 ? 's' : ' ') ; |
| 293 |
fflush (stdout) ; |
| 294 |
|
| 295 |
assert (channel_count <= MAX_CHANNELS) ; |
| 296 |
|
| 297 |
memset (input_serial, 0, sizeof (input_serial)) ; |
| 298 |
memset (input_interleaved, 0, sizeof (input_interleaved)) ; |
| 299 |
memset (output_interleaved, 0, sizeof (output_interleaved)) ; |
| 300 |
memset (output_serial, 0, sizeof (output_serial)) ; |
| 301 |
memset (&test_callback_data, 0, sizeof (test_callback_data)) ; |
| 302 |
|
| 303 |
frames = BUFFER_LEN ; |
| 304 |
|
| 305 |
/* Calculate channel_count separate windowed sine waves. */ |
| 306 |
for (ch = 0 ; ch < channel_count ; ch++) |
| 307 |
{ freq = (200.0 + 33.333333333 * ch) / 44100.0 ; |
| 308 |
gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; |
| 309 |
} ; |
| 310 |
|
| 311 |
/* Interleave the data in preparation for SRC. */ |
| 312 |
interleave_data (input_serial, input_interleaved, frames, channel_count) ; |
| 313 |
|
| 314 |
/* Perform sample rate conversion. */ |
| 315 |
src_ratio = 0.95 ; |
| 316 |
test_callback_data.channels = channel_count ; |
| 317 |
test_callback_data.total_frames = frames ; |
| 318 |
test_callback_data.current_frame = 0 ; |
| 319 |
test_callback_data.data = input_interleaved ; |
| 320 |
|
| 321 |
if ((src_state = src_callback_new (test_callback_func, converter, channel_count, &error, &test_callback_data)) == NULL) |
| 322 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 323 |
exit (1) ; |
| 324 |
} ; |
| 325 |
|
| 326 |
read_total = 0 ; |
| 327 |
while (read_total < frames) |
| 328 |
{ read_count = src_callback_read (src_state, src_ratio, frames - read_total, output_interleaved + read_total * channel_count) ; |
| 329 |
|
| 330 |
if (read_count <= 0) |
| 331 |
break ; |
| 332 |
|
| 333 |
read_total += read_count ; |
| 334 |
} ; |
| 335 |
|
| 336 |
if ((error = src_error (src_state)) != 0) |
| 337 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 338 |
exit (1) ; |
| 339 |
} ; |
| 340 |
|
| 341 |
src_state = src_delete (src_state) ; |
| 342 |
|
| 343 |
if (fabs (read_total - src_ratio * frames) > 2) |
| 344 |
{ printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__, |
| 345 |
read_total, (int) floor (src_ratio * frames)) ; |
| 346 |
printf ("\tsrc_ratio : %.4f\n", src_ratio) ; |
| 347 |
printf ("\tinput_len : %d\n", frames) ; |
| 348 |
printf ("\toutput_len : %d\n\n", read_total) ; |
| 349 |
exit (1) ; |
| 350 |
} ; |
| 351 |
|
| 352 |
/* De-interleave data so SNR can be calculated for each channel. */ |
| 353 |
deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; |
| 354 |
|
| 355 |
for (ch = 0 ; ch < channel_count ; ch++) |
| 356 |
{ snr = calculate_snr (output_serial + ch * frames, frames, 1) ; |
| 357 |
if (snr < target_snr) |
| 358 |
{ printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; |
| 359 |
save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; |
| 360 |
exit (1) ; |
| 361 |
} ; |
| 362 |
} ; |
| 363 |
|
| 364 |
puts ("ok") ; |
| 365 |
|
| 366 |
return ; |
| 367 |
} /* callback_test */ |
| 368 |
|