| 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 |
|
| 18 |
#include <samplerate.h> |
| 19 |
|
| 20 |
#include "util.h" |
| 21 |
|
| 22 |
#define BUFFER_LEN 2048 |
| 23 |
|
| 24 |
static void simple_test (int converter, double ratio) ; |
| 25 |
static void src_simple_produces_output (int converter, int channels, double src_ratio) ; |
| 26 |
static void src_simple_produces_output_test (int converter, double src_ratio) ; |
| 27 |
|
| 28 |
int |
| 29 |
main (void) |
| 30 |
{ static double src_ratios [] = |
| 31 |
{ 1.0001, 0.099, 0.1, 0.33333333, 0.789, 1.9, 3.1, 9.9, 256.0, 1.0 / 256.0 |
| 32 |
} ; |
| 33 |
|
| 34 |
int k ; |
| 35 |
|
| 36 |
puts ("") ; |
| 37 |
|
| 38 |
puts (" Zero Order Hold interpolator :") ; |
| 39 |
for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) |
| 40 |
{ simple_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ; |
| 41 |
src_simple_produces_output_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ; |
| 42 |
} |
| 43 |
|
| 44 |
puts (" Linear interpolator :") ; |
| 45 |
for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) |
| 46 |
{ simple_test (SRC_LINEAR, src_ratios [k]) ; |
| 47 |
src_simple_produces_output_test (SRC_LINEAR, src_ratios [k]) ; |
| 48 |
} |
| 49 |
|
| 50 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 51 |
puts (" Sinc interpolator :") ; |
| 52 |
for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) |
| 53 |
{ simple_test (SRC_SINC_FASTEST, src_ratios [k]) ; |
| 54 |
src_simple_produces_output_test (SRC_SINC_FASTEST, src_ratios [k]) ; |
| 55 |
} |
| 56 |
#endif |
| 57 |
|
| 58 |
puts ("") ; |
| 59 |
|
| 60 |
return 0 ; |
| 61 |
} /* main */ |
| 62 |
|
| 63 |
static void |
| 64 |
src_simple_produces_output_test (int converter, double src_ratio) |
| 65 |
{ |
| 66 |
for (int channels = 1; channels <= 9; channels++) |
| 67 |
src_simple_produces_output(converter, channels, src_ratio); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
src_simple_produces_output (int converter, int channels, double src_ratio) |
| 72 |
{ |
| 73 |
// Choose a suitable number of frames. |
| 74 |
// At least 256 so a conversion ratio of 1/256 can produce any output |
| 75 |
const long NUM_FRAMES = 1000; |
| 76 |
int error; |
| 77 |
|
| 78 |
printf ("\tproduces_output\t(SRC ratio = %6.4f, channels = %d) ... ", src_ratio, channels) ; |
| 79 |
fflush (stdout) ; |
| 80 |
|
| 81 |
float *input = calloc (NUM_FRAMES * channels, sizeof (float)); |
| 82 |
float *output = calloc (NUM_FRAMES * channels, sizeof (float)); |
| 83 |
|
| 84 |
SRC_DATA src_data; |
| 85 |
memset (&src_data, 0, sizeof (src_data)) ; |
| 86 |
src_data.data_in = input; |
| 87 |
src_data.data_out = output; |
| 88 |
src_data.input_frames = NUM_FRAMES; |
| 89 |
src_data.output_frames = NUM_FRAMES; |
| 90 |
src_data.src_ratio = src_ratio; |
| 91 |
|
| 92 |
if ((error = src_simple (&src_data, converter, channels))) |
| 93 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 94 |
exit (1) ; |
| 95 |
} ; |
| 96 |
if (src_data.input_frames_used == 0) |
| 97 |
{ printf ("\n\nLine %d : No input frames used.\n\n", __LINE__) ; |
| 98 |
exit (1) ; |
| 99 |
} ; |
| 100 |
if (src_data.output_frames_gen == 0) |
| 101 |
{ printf ("\n\nLine %d : No output frames generated.\n\n", __LINE__) ; |
| 102 |
exit (1) ; |
| 103 |
} ; |
| 104 |
free(input); |
| 105 |
free(output); |
| 106 |
puts ("ok") ; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
static void |
| 111 |
simple_test (int converter, double src_ratio) |
| 112 |
{ static float input [BUFFER_LEN], output [BUFFER_LEN] ; |
| 113 |
|
| 114 |
SRC_DATA src_data ; |
| 115 |
|
| 116 |
int input_len, output_len, error, terminate ; |
| 117 |
|
| 118 |
printf ("\tsimple_test\t(SRC ratio = %6.4f) ................. ", src_ratio) ; |
| 119 |
fflush (stdout) ; |
| 120 |
|
| 121 |
/* Calculate maximun input and output lengths. */ |
| 122 |
if (src_ratio >= 1.0) |
| 123 |
{ output_len = BUFFER_LEN ; |
| 124 |
input_len = (int) floor (BUFFER_LEN / src_ratio) ; |
| 125 |
} |
| 126 |
else |
| 127 |
{ input_len = BUFFER_LEN ; |
| 128 |
output_len = (int) floor (BUFFER_LEN * src_ratio) ; |
| 129 |
} ; |
| 130 |
|
| 131 |
/* Reduce input_len by 10 so output is longer than necessary. */ |
| 132 |
input_len -= 10 ; |
| 133 |
|
| 134 |
if (output_len > BUFFER_LEN) |
| 135 |
{ printf ("\n\nLine %d : output_len > BUFFER_LEN\n\n", __LINE__) ; |
| 136 |
exit (1) ; |
| 137 |
} ; |
| 138 |
|
| 139 |
memset (&src_data, 0, sizeof (src_data)) ; |
| 140 |
|
| 141 |
src_data.data_in = input ; |
| 142 |
src_data.input_frames = input_len ; |
| 143 |
|
| 144 |
src_data.src_ratio = src_ratio ; |
| 145 |
|
| 146 |
src_data.data_out = output ; |
| 147 |
src_data.output_frames = BUFFER_LEN ; |
| 148 |
|
| 149 |
if ((error = src_simple (&src_data, converter, 1))) |
| 150 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 151 |
exit (1) ; |
| 152 |
} ; |
| 153 |
|
| 154 |
terminate = (int) ceil ((src_ratio >= 1.0) ? src_ratio : 1.0 / src_ratio) ; |
| 155 |
|
| 156 |
if (fabs (src_data.output_frames_gen - src_ratio * input_len) > 2 * terminate) |
| 157 |
{ printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__, |
| 158 |
src_data.output_frames_gen, (int) floor (src_ratio * input_len)) ; |
| 159 |
printf ("\tsrc_ratio : %.4f\n", src_ratio) ; |
| 160 |
printf ("\tinput_len : %d\n\toutput_len : %d\n\n", input_len, output_len) ; |
| 161 |
exit (1) ; |
| 162 |
} ; |
| 163 |
|
| 164 |
puts ("ok") ; |
| 165 |
|
| 166 |
return ; |
| 167 |
} /* simple_test */ |
| 168 |
|