| 1 |
/* |
| 2 |
** Copyright (c) 2006-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 <math.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <samplerate.h> |
| 19 |
|
| 20 |
#include "util.h" |
| 21 |
|
| 22 |
#if HAVE_FFTW3 |
| 23 |
#include <fftw3.h> |
| 24 |
#else |
| 25 |
#define fftw_cleanup() |
| 26 |
#endif |
| 27 |
|
| 28 |
#define BUFFER_LEN (1 << 14) |
| 29 |
|
| 30 |
static void varispeed_test (int converter, double target_snr) ; |
| 31 |
static void varispeed_bounds_test (int converter) ; |
| 32 |
static void set_ratio_test (int converter, int channels, double initial_ratio, double second_ratio) ; |
| 33 |
|
| 34 |
int |
| 35 |
main (void) |
| 36 |
{ |
| 37 |
puts ("\n Varispeed SNR test") ; |
| 38 |
printf (" Zero Order Hold interpolator : ") ; |
| 39 |
fflush (stdout) ; |
| 40 |
varispeed_test (SRC_ZERO_ORDER_HOLD, 10.0) ; |
| 41 |
puts ("ok") ; |
| 42 |
|
| 43 |
printf (" Linear interpolator : ") ; |
| 44 |
fflush (stdout) ; |
| 45 |
varispeed_test (SRC_LINEAR, 10.0) ; |
| 46 |
puts ("ok") ; |
| 47 |
|
| 48 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 49 |
printf (" Sinc interpolator : ") ; |
| 50 |
fflush (stdout) ; |
| 51 |
varispeed_test (SRC_SINC_FASTEST, 115.0) ; |
| 52 |
puts ("ok") ; |
| 53 |
#endif |
| 54 |
|
| 55 |
puts ("\n Varispeed bounds test") ; |
| 56 |
printf (" Zero Order Hold interpolator : ") ; |
| 57 |
fflush (stdout) ; |
| 58 |
varispeed_bounds_test (SRC_ZERO_ORDER_HOLD) ; |
| 59 |
puts ("ok") ; |
| 60 |
|
| 61 |
printf (" Linear interpolator : ") ; |
| 62 |
fflush (stdout) ; |
| 63 |
varispeed_bounds_test (SRC_LINEAR) ; |
| 64 |
puts ("ok") ; |
| 65 |
|
| 66 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 67 |
printf (" Sinc interpolator : ") ; |
| 68 |
fflush (stdout) ; |
| 69 |
varispeed_bounds_test (SRC_SINC_FASTEST) ; |
| 70 |
puts ("ok") ; |
| 71 |
#endif |
| 72 |
|
| 73 |
fftw_cleanup () ; |
| 74 |
puts ("") ; |
| 75 |
|
| 76 |
return 0 ; |
| 77 |
} /* main */ |
| 78 |
|
| 79 |
static void |
| 80 |
varispeed_test (int converter, double target_snr) |
| 81 |
{ static float input [BUFFER_LEN], output [BUFFER_LEN] ; |
| 82 |
double sine_freq, snr ; |
| 83 |
|
| 84 |
SRC_STATE *src_state ; |
| 85 |
SRC_DATA src_data ; |
| 86 |
|
| 87 |
int input_len, error ; |
| 88 |
|
| 89 |
memset (input, 0, sizeof (input)) ; |
| 90 |
|
| 91 |
input_len = ARRAY_LEN (input) / 2 ; |
| 92 |
|
| 93 |
sine_freq = 0.0111 ; |
| 94 |
gen_windowed_sines (1, &sine_freq, 1.0, input, input_len) ; |
| 95 |
|
| 96 |
/* Perform sample rate conversion. */ |
| 97 |
if ((src_state = src_new (converter, 1, &error)) == NULL) |
| 98 |
{ printf ("\n\nLine %d : src_new () failed : %s\n\n", __LINE__, src_strerror (error)) ; |
| 99 |
exit (1) ; |
| 100 |
} ; |
| 101 |
|
| 102 |
src_data.end_of_input = 1 ; |
| 103 |
|
| 104 |
src_data.data_in = input ; |
| 105 |
src_data.input_frames = input_len ; |
| 106 |
|
| 107 |
src_data.src_ratio = 3.0 ; |
| 108 |
|
| 109 |
src_data.data_out = output ; |
| 110 |
src_data.output_frames = ARRAY_LEN (output) ; |
| 111 |
|
| 112 |
if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio))) |
| 113 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 114 |
exit (1) ; |
| 115 |
} ; |
| 116 |
|
| 117 |
if ((error = src_process (src_state, &src_data))) |
| 118 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 119 |
printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; |
| 120 |
printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; |
| 121 |
exit (1) ; |
| 122 |
} ; |
| 123 |
|
| 124 |
if (src_data.input_frames_used != input_len) |
| 125 |
{ printf ("\n\nLine %d : unused input.\n", __LINE__) ; |
| 126 |
printf ("\tinput_len : %d\n", input_len) ; |
| 127 |
printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ; |
| 128 |
exit (1) ; |
| 129 |
} ; |
| 130 |
|
| 131 |
/* Copy the last output to the input. */ |
| 132 |
memcpy (input, output, sizeof (input)) ; |
| 133 |
reverse_data (input, src_data.output_frames_gen) ; |
| 134 |
|
| 135 |
if ((error = src_reset (src_state))) |
| 136 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 137 |
exit (1) ; |
| 138 |
} ; |
| 139 |
|
| 140 |
src_data.end_of_input = 1 ; |
| 141 |
|
| 142 |
src_data.data_in = input ; |
| 143 |
input_len = src_data.input_frames = src_data.output_frames_gen ; |
| 144 |
|
| 145 |
src_data.data_out = output ; |
| 146 |
src_data.output_frames = ARRAY_LEN (output) ; |
| 147 |
|
| 148 |
if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio))) |
| 149 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 150 |
exit (1) ; |
| 151 |
} ; |
| 152 |
|
| 153 |
if ((error = src_process (src_state, &src_data))) |
| 154 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 155 |
printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; |
| 156 |
printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; |
| 157 |
exit (1) ; |
| 158 |
} ; |
| 159 |
|
| 160 |
if (src_data.input_frames_used != input_len) |
| 161 |
{ printf ("\n\nLine %d : unused input.\n", __LINE__) ; |
| 162 |
printf ("\tinput_len : %d\n", input_len) ; |
| 163 |
printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ; |
| 164 |
exit (1) ; |
| 165 |
} ; |
| 166 |
|
| 167 |
src_state = src_delete (src_state) ; |
| 168 |
|
| 169 |
snr = calculate_snr (output, src_data.output_frames_gen, 1) ; |
| 170 |
|
| 171 |
if (target_snr > snr) |
| 172 |
{ printf ("\n\nLine %d : snr (%3.1f) does not meet target (%3.1f)\n\n", __LINE__, snr, target_snr) ; |
| 173 |
save_oct_float ("varispeed.mat", input, src_data.input_frames, output, src_data.output_frames_gen) ; |
| 174 |
exit (1) ; |
| 175 |
} ; |
| 176 |
|
| 177 |
return ; |
| 178 |
} /* varispeed_test */ |
| 179 |
|
| 180 |
static void |
| 181 |
varispeed_bounds_test (int converter) |
| 182 |
{ double ratios [] = { 0.1, 0.01, 20 } ; |
| 183 |
int chan, r1, r2 ; |
| 184 |
|
| 185 |
for (chan = 1 ; chan <= 9 ; chan ++) |
| 186 |
for (r1 = 0 ; r1 < ARRAY_LEN (ratios) ; r1++) |
| 187 |
for (r2 = 0 ; r2 < ARRAY_LEN (ratios) ; r2 ++) |
| 188 |
if (r1 != r2) |
| 189 |
set_ratio_test (converter, chan, ratios [r1], ratios [r2]) ; |
| 190 |
} /* varispeed_bounds_test */ |
| 191 |
|
| 192 |
static void |
| 193 |
set_ratio_test (int converter, int channels, double initial_ratio, double second_ratio) |
| 194 |
{ const int total_input_frames = BUFFER_LEN ; |
| 195 |
/* Maximum upsample ratio is 20, use a value beigger. */ |
| 196 |
const int total_output_frames = 25 * BUFFER_LEN ; |
| 197 |
|
| 198 |
/* Interested in array boundary conditions, so all zero data here is fine. */ |
| 199 |
float *input = calloc (total_input_frames * channels, sizeof (float)) ; |
| 200 |
float *output = calloc (total_output_frames * channels, sizeof (float)) ; |
| 201 |
|
| 202 |
char details [128] ; |
| 203 |
|
| 204 |
const int max_loop_count = 100000 ; |
| 205 |
const int chunk_size = 128 ; |
| 206 |
|
| 207 |
SRC_STATE *src_state ; |
| 208 |
SRC_DATA src_data ; |
| 209 |
|
| 210 |
int error, k, total_frames_used, total_frames_gen ; |
| 211 |
|
| 212 |
snprintf (details, sizeof (details), "%d channels, ratio %g -> %g", channels, initial_ratio, second_ratio) ; |
| 213 |
|
| 214 |
if ((src_state = src_new (converter, channels, &error)) == NULL) |
| 215 |
{ printf ("\n\nLine %d : src_new () failed : %s\n\n", __LINE__, src_strerror (error)) ; |
| 216 |
exit (1) ; |
| 217 |
} ; |
| 218 |
|
| 219 |
total_frames_used = 0 ; |
| 220 |
total_frames_gen = 0 ; |
| 221 |
|
| 222 |
memset (&src_data, 0, sizeof (src_data)) ; |
| 223 |
src_data.end_of_input = 0 ; |
| 224 |
src_data.src_ratio = initial_ratio ; |
| 225 |
src_data.data_in = input ; |
| 226 |
src_data.data_out = output ; |
| 227 |
src_data.input_frames = chunk_size ; |
| 228 |
src_data.output_frames = total_output_frames ; |
| 229 |
|
| 230 |
/* Use a max_loop_count here to enable the detection of infinite loops |
| 231 |
** (due to end of input not being detected. |
| 232 |
*/ |
| 233 |
for (k = 0 ; k < max_loop_count ; k ++) |
| 234 |
{ if (k == 1) |
| 235 |
{ /* Hard switch to second_ratio after processing one chunk. */ |
| 236 |
src_data.src_ratio = second_ratio ; |
| 237 |
if ((error = src_set_ratio (src_state, second_ratio))) |
| 238 |
{ printf ("\n\nLine %d : %s : %s\n\n", __LINE__, details, src_strerror (error)) ; |
| 239 |
exit (1) ; |
| 240 |
} ; |
| 241 |
} ; |
| 242 |
|
| 243 |
if ((error = src_process (src_state, &src_data)) != 0) |
| 244 |
{ printf ("\n\nLine %d : %s : %s\n\n", __LINE__, details, src_strerror (error)) ; |
| 245 |
exit (1) ; |
| 246 |
} ; |
| 247 |
|
| 248 |
if (src_data.end_of_input && src_data.output_frames_gen == 0) |
| 249 |
break ; |
| 250 |
|
| 251 |
total_frames_used += src_data.input_frames_used ; |
| 252 |
total_frames_gen += src_data.output_frames_gen ; |
| 253 |
|
| 254 |
src_data.data_in += src_data.input_frames_used * channels ; |
| 255 |
src_data.data_out += src_data.output_frames_gen * channels ; |
| 256 |
|
| 257 |
src_data.input_frames = total_input_frames - total_frames_used ; |
| 258 |
src_data.output_frames = total_output_frames - total_frames_gen ; |
| 259 |
|
| 260 |
src_data.end_of_input = total_frames_used >= total_input_frames ? 1 : 0 ; |
| 261 |
} ; |
| 262 |
|
| 263 |
ASSERT (k < max_loop_count) ; |
| 264 |
ASSERT (total_frames_gen > 0) ; |
| 265 |
|
| 266 |
for (k = 0 ; k < total_frames_gen * channels ; k ++) |
| 267 |
ASSERT (! isnan (output [k])) ; |
| 268 |
|
| 269 |
src_state = src_delete (src_state) ; |
| 270 |
|
| 271 |
free (input) ; |
| 272 |
free (output) ; |
| 273 |
|
| 274 |
return ; |
| 275 |
} /* set_ratio_test */ |