Chris@41: /* Chris@41: ** Copyright (c) 2002-2016, Erik de Castro Lopo Chris@41: ** All rights reserved. Chris@41: ** Chris@41: ** This code is released under 2-clause BSD license. Please see the Chris@41: ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING Chris@41: */ Chris@41: Chris@41: #include "config.h" Chris@41: Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: #include Chris@41: Chris@41: #if (HAVE_FFTW3) Chris@41: #include Chris@41: #else Chris@41: static inline void Chris@41: fftw_cleanup (void) Chris@41: { return ; Chris@41: } Chris@41: #endif Chris@41: Chris@41: #include Chris@41: Chris@41: #include "util.h" Chris@41: #define BUFFER_LEN 50000 Chris@41: #define BLOCK_LEN (12) Chris@41: Chris@41: #define MAX_CHANNELS 10 Chris@41: Chris@41: static void simple_test (int converter, int channel_count, double target_snr) ; Chris@41: static void process_test (int converter, int channel_count, double target_snr) ; Chris@41: static void callback_test (int converter, int channel_count, double target_snr) ; Chris@41: Chris@41: int Chris@41: main (void) Chris@41: { double target ; Chris@41: int k ; Chris@41: Chris@41: puts ("\n Zero Order Hold interpolator :") ; Chris@41: target = 38.0 ; Chris@41: for (k = 1 ; k <= 3 ; k++) Chris@41: { simple_test (SRC_ZERO_ORDER_HOLD, k, target) ; Chris@41: process_test (SRC_ZERO_ORDER_HOLD, k, target) ; Chris@41: callback_test (SRC_ZERO_ORDER_HOLD, k, target) ; Chris@41: } ; Chris@41: Chris@41: puts ("\n Linear interpolator :") ; Chris@41: target = 79.0 ; Chris@41: for (k = 1 ; k <= 3 ; k++) Chris@41: { simple_test (SRC_LINEAR, k, target) ; Chris@41: process_test (SRC_LINEAR, k, target) ; Chris@41: callback_test (SRC_LINEAR, k, target) ; Chris@41: } ; Chris@41: Chris@41: puts ("\n Sinc interpolator :") ; Chris@41: target = 100.0 ; Chris@41: for (k = 1 ; k <= MAX_CHANNELS ; k++) Chris@41: { simple_test (SRC_SINC_FASTEST, k, target) ; Chris@41: process_test (SRC_SINC_FASTEST, k, target) ; Chris@41: callback_test (SRC_SINC_FASTEST, k, target) ; Chris@41: } ; Chris@41: Chris@41: fftw_cleanup () ; Chris@41: puts ("") ; Chris@41: Chris@41: return 0 ; Chris@41: } /* main */ Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: static float input_serial [BUFFER_LEN * MAX_CHANNELS] ; Chris@41: static float input_interleaved [BUFFER_LEN * MAX_CHANNELS] ; Chris@41: static float output_interleaved [BUFFER_LEN * MAX_CHANNELS] ; Chris@41: static float output_serial [BUFFER_LEN * MAX_CHANNELS] ; Chris@41: Chris@41: static void Chris@41: simple_test (int converter, int channel_count, double target_snr) Chris@41: { SRC_DATA src_data ; Chris@41: Chris@41: double freq, snr ; Chris@41: int ch, error, frames ; Chris@41: Chris@41: printf ("\t%-22s (%2d channel%c) ............ ", "simple_test", channel_count, channel_count > 1 ? 's' : ' ') ; Chris@41: fflush (stdout) ; Chris@41: Chris@41: assert (channel_count <= MAX_CHANNELS) ; Chris@41: Chris@41: memset (input_serial, 0, sizeof (input_serial)) ; Chris@41: memset (input_interleaved, 0, sizeof (input_interleaved)) ; Chris@41: memset (output_interleaved, 0, sizeof (output_interleaved)) ; Chris@41: memset (output_serial, 0, sizeof (output_serial)) ; Chris@41: Chris@41: frames = BUFFER_LEN ; Chris@41: Chris@41: /* Calculate channel_count separate windowed sine waves. */ Chris@41: for (ch = 0 ; ch < channel_count ; ch++) Chris@41: { freq = (200.0 + 33.333333333 * ch) / 44100.0 ; Chris@41: gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; Chris@41: } ; Chris@41: Chris@41: /* Interleave the data in preparation for SRC. */ Chris@41: interleave_data (input_serial, input_interleaved, frames, channel_count) ; Chris@41: Chris@41: /* Choose a converstion ratio <= 1.0. */ Chris@41: src_data.src_ratio = 0.95 ; Chris@41: Chris@41: src_data.data_in = input_interleaved ; Chris@41: src_data.input_frames = frames ; Chris@41: Chris@41: src_data.data_out = output_interleaved ; Chris@41: src_data.output_frames = frames ; Chris@41: Chris@41: if ((error = src_simple (&src_data, converter, channel_count))) Chris@41: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: if (fabs (src_data.output_frames_gen - src_data.src_ratio * src_data.input_frames) > 2) Chris@41: { printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__, Chris@41: src_data.output_frames_gen, (int) floor (src_data.src_ratio * src_data.input_frames)) ; Chris@41: printf ("\tsrc_ratio : %.4f\n", src_data.src_ratio) ; Chris@41: printf ("\tinput_len : %ld\n", src_data.input_frames) ; Chris@41: printf ("\toutput_len : %ld\n\n", src_data.output_frames_gen) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: /* De-interleave data so SNR can be calculated for each channel. */ Chris@41: deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; Chris@41: Chris@41: for (ch = 0 ; ch < channel_count ; ch++) Chris@41: { snr = calculate_snr (output_serial + ch * frames, frames, 1) ; Chris@41: if (snr < target_snr) Chris@41: { printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; Chris@41: save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: } ; Chris@41: Chris@41: puts ("ok") ; Chris@41: Chris@41: return ; Chris@41: } /* simple_test */ Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: static void Chris@41: process_test (int converter, int channel_count, double target_snr) Chris@41: { SRC_STATE *src_state ; Chris@41: SRC_DATA src_data ; Chris@41: Chris@41: double freq, snr ; Chris@41: int ch, error, frames, current_in, current_out ; Chris@41: Chris@41: printf ("\t%-22s (%2d channel%c) ............ ", "process_test", channel_count, channel_count > 1 ? 's' : ' ') ; Chris@41: fflush (stdout) ; Chris@41: Chris@41: assert (channel_count <= MAX_CHANNELS) ; Chris@41: Chris@41: memset (input_serial, 0, sizeof (input_serial)) ; Chris@41: memset (input_interleaved, 0, sizeof (input_interleaved)) ; Chris@41: memset (output_interleaved, 0, sizeof (output_interleaved)) ; Chris@41: memset (output_serial, 0, sizeof (output_serial)) ; Chris@41: Chris@41: frames = BUFFER_LEN ; Chris@41: Chris@41: /* Calculate channel_count separate windowed sine waves. */ Chris@41: for (ch = 0 ; ch < channel_count ; ch++) Chris@41: { freq = (400.0 + 11.333333333 * ch) / 44100.0 ; Chris@41: gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; Chris@41: } ; Chris@41: Chris@41: /* Interleave the data in preparation for SRC. */ Chris@41: interleave_data (input_serial, input_interleaved, frames, channel_count) ; Chris@41: Chris@41: /* Perform sample rate conversion. */ Chris@41: if ((src_state = src_new (converter, channel_count, &error)) == NULL) Chris@41: { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: src_data.end_of_input = 0 ; /* Set this later. */ Chris@41: Chris@41: /* Choose a converstion ratio < 1.0. */ Chris@41: src_data.src_ratio = 0.95 ; Chris@41: Chris@41: src_data.data_in = input_interleaved ; Chris@41: src_data.data_out = output_interleaved ; Chris@41: Chris@41: current_in = current_out = 0 ; Chris@41: Chris@41: while (1) Chris@41: { src_data.input_frames = MAX (MIN (BLOCK_LEN, frames - current_in), 0) ; Chris@41: src_data.output_frames = MAX (MIN (BLOCK_LEN, frames - current_out), 0) ; Chris@41: Chris@41: if ((error = src_process (src_state, &src_data))) Chris@41: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: if (src_data.end_of_input && src_data.output_frames_gen == 0) Chris@41: break ; Chris@41: Chris@41: current_in += src_data.input_frames_used ; Chris@41: current_out += src_data.output_frames_gen ; Chris@41: Chris@41: src_data.data_in += src_data.input_frames_used * channel_count ; Chris@41: src_data.data_out += src_data.output_frames_gen * channel_count ; Chris@41: Chris@41: src_data.end_of_input = (current_in >= frames) ? 1 : 0 ; Chris@41: } ; Chris@41: Chris@41: src_state = src_delete (src_state) ; Chris@41: Chris@41: if (fabs (current_out - src_data.src_ratio * current_in) > 2) Chris@41: { printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__, Chris@41: current_out, (int) floor (src_data.src_ratio * current_in)) ; Chris@41: printf ("\tsrc_ratio : %.4f\n", src_data.src_ratio) ; Chris@41: printf ("\tinput_len : %d\n", frames) ; Chris@41: printf ("\toutput_len : %d\n\n", current_out) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: /* De-interleave data so SNR can be calculated for each channel. */ Chris@41: deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; Chris@41: Chris@41: for (ch = 0 ; ch < channel_count ; ch++) Chris@41: { snr = calculate_snr (output_serial + ch * frames, frames, 1) ; Chris@41: if (snr < target_snr) Chris@41: { printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; Chris@41: save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: } ; Chris@41: Chris@41: puts ("ok") ; Chris@41: Chris@41: return ; Chris@41: } /* process_test */ Chris@41: Chris@41: /*============================================================================== Chris@41: */ Chris@41: Chris@41: typedef struct Chris@41: { int channels ; Chris@41: long total_frames ; Chris@41: long current_frame ; Chris@41: float *data ; Chris@41: } TEST_CB_DATA ; Chris@41: Chris@41: static long Chris@41: test_callback_func (void *cb_data, float **data) Chris@41: { TEST_CB_DATA *pcb_data ; Chris@41: Chris@41: long frames ; Chris@41: Chris@41: if ((pcb_data = cb_data) == NULL) Chris@41: return 0 ; Chris@41: Chris@41: if (data == NULL) Chris@41: return 0 ; Chris@41: Chris@41: *data = pcb_data->data + (pcb_data->current_frame * pcb_data->channels) ; Chris@41: Chris@41: if (pcb_data->total_frames - pcb_data->current_frame < BLOCK_LEN) Chris@41: frames = pcb_data->total_frames - pcb_data->current_frame ; Chris@41: else Chris@41: frames = BLOCK_LEN ; Chris@41: Chris@41: pcb_data->current_frame += frames ; Chris@41: Chris@41: return frames ; Chris@41: } /* test_callback_func */ Chris@41: Chris@41: static void Chris@41: callback_test (int converter, int channel_count, double target_snr) Chris@41: { TEST_CB_DATA test_callback_data ; Chris@41: SRC_STATE *src_state = NULL ; Chris@41: Chris@41: double freq, snr, src_ratio ; Chris@41: int ch, error, frames, read_total, read_count ; Chris@41: Chris@41: printf ("\t%-22s (%2d channel%c) ............ ", "callback_test", channel_count, channel_count > 1 ? 's' : ' ') ; Chris@41: fflush (stdout) ; Chris@41: Chris@41: assert (channel_count <= MAX_CHANNELS) ; Chris@41: Chris@41: memset (input_serial, 0, sizeof (input_serial)) ; Chris@41: memset (input_interleaved, 0, sizeof (input_interleaved)) ; Chris@41: memset (output_interleaved, 0, sizeof (output_interleaved)) ; Chris@41: memset (output_serial, 0, sizeof (output_serial)) ; Chris@41: memset (&test_callback_data, 0, sizeof (test_callback_data)) ; Chris@41: Chris@41: frames = BUFFER_LEN ; Chris@41: Chris@41: /* Calculate channel_count separate windowed sine waves. */ Chris@41: for (ch = 0 ; ch < channel_count ; ch++) Chris@41: { freq = (200.0 + 33.333333333 * ch) / 44100.0 ; Chris@41: gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; Chris@41: } ; Chris@41: Chris@41: /* Interleave the data in preparation for SRC. */ Chris@41: interleave_data (input_serial, input_interleaved, frames, channel_count) ; Chris@41: Chris@41: /* Perform sample rate conversion. */ Chris@41: src_ratio = 0.95 ; Chris@41: test_callback_data.channels = channel_count ; Chris@41: test_callback_data.total_frames = frames ; Chris@41: test_callback_data.current_frame = 0 ; Chris@41: test_callback_data.data = input_interleaved ; Chris@41: Chris@41: if ((src_state = src_callback_new (test_callback_func, converter, channel_count, &error, &test_callback_data)) == NULL) Chris@41: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: read_total = 0 ; Chris@41: while (read_total < frames) Chris@41: { read_count = src_callback_read (src_state, src_ratio, frames - read_total, output_interleaved + read_total * channel_count) ; Chris@41: Chris@41: if (read_count <= 0) Chris@41: break ; Chris@41: Chris@41: read_total += read_count ; Chris@41: } ; Chris@41: Chris@41: if ((error = src_error (src_state)) != 0) Chris@41: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: src_state = src_delete (src_state) ; Chris@41: Chris@41: if (fabs (read_total - src_ratio * frames) > 2) Chris@41: { printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__, Chris@41: read_total, (int) floor (src_ratio * frames)) ; Chris@41: printf ("\tsrc_ratio : %.4f\n", src_ratio) ; Chris@41: printf ("\tinput_len : %d\n", frames) ; Chris@41: printf ("\toutput_len : %d\n\n", read_total) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: Chris@41: /* De-interleave data so SNR can be calculated for each channel. */ Chris@41: deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; Chris@41: Chris@41: for (ch = 0 ; ch < channel_count ; ch++) Chris@41: { snr = calculate_snr (output_serial + ch * frames, frames, 1) ; Chris@41: if (snr < target_snr) Chris@41: { printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; Chris@41: save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; Chris@41: exit (1) ; Chris@41: } ; Chris@41: } ; Chris@41: Chris@41: puts ("ok") ; Chris@41: Chris@41: return ; Chris@41: } /* callback_test */ Chris@41: