annotate src/libsamplerate-0.1.8/tests/multi_channel_test.c @ 85:545efbb81310

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