Chris@0: /* Chris@0: ** Copyright (C) 2002-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** This program is free software; you can redistribute it and/or modify Chris@0: ** it under the terms of the GNU General Public License as published by Chris@0: ** the Free Software Foundation; either version 2 of the License, or Chris@0: ** (at your option) any later version. Chris@0: ** Chris@0: ** This program is distributed in the hope that it will be useful, Chris@0: ** but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: ** GNU General Public License for more details. Chris@0: ** Chris@0: ** You should have received a copy of the GNU General Public License Chris@0: ** along with this program; if not, write to the Free Software Chris@0: ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. Chris@0: */ Chris@0: Chris@0: #include "config.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "util.h" Chris@0: #define BUFFER_LEN 50000 Chris@0: #define BLOCK_LEN (12) Chris@0: Chris@0: #define MAX_CHANNELS 10 Chris@0: Chris@0: static void simple_test (int converter, int channel_count, double target_snr) ; Chris@0: static void process_test (int converter, int channel_count, double target_snr) ; Chris@0: static void callback_test (int converter, int channel_count, double target_snr) ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { double target ; Chris@0: int k ; Chris@0: Chris@0: puts ("\n Zero Order Hold interpolator :") ; Chris@0: target = 38.0 ; Chris@0: for (k = 1 ; k <= 3 ; k++) Chris@0: { simple_test (SRC_ZERO_ORDER_HOLD, k, target) ; Chris@0: process_test (SRC_ZERO_ORDER_HOLD, k, target) ; Chris@0: callback_test (SRC_ZERO_ORDER_HOLD, k, target) ; Chris@0: } ; Chris@0: Chris@0: puts ("\n Linear interpolator :") ; Chris@0: target = 79.0 ; Chris@0: for (k = 1 ; k <= 3 ; k++) Chris@0: { simple_test (SRC_LINEAR, k, target) ; Chris@0: process_test (SRC_LINEAR, k, target) ; Chris@0: callback_test (SRC_LINEAR, k, target) ; Chris@0: } ; Chris@0: Chris@0: puts ("\n Sinc interpolator :") ; Chris@0: target = 100.0 ; Chris@0: for (k = 1 ; k <= MAX_CHANNELS ; k++) Chris@0: { simple_test (SRC_SINC_FASTEST, k, target) ; Chris@0: process_test (SRC_SINC_FASTEST, k, target) ; Chris@0: callback_test (SRC_SINC_FASTEST, k, target) ; Chris@0: } ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static float input_serial [BUFFER_LEN * MAX_CHANNELS] ; Chris@0: static float input_interleaved [BUFFER_LEN * MAX_CHANNELS] ; Chris@0: static float output_interleaved [BUFFER_LEN * MAX_CHANNELS] ; Chris@0: static float output_serial [BUFFER_LEN * MAX_CHANNELS] ; Chris@0: Chris@0: static void Chris@0: simple_test (int converter, int channel_count, double target_snr) Chris@0: { SRC_DATA src_data ; Chris@0: Chris@0: double freq, snr ; Chris@0: int ch, error, frames ; Chris@0: Chris@0: printf ("\t%-22s (%2d channel%c) ............ ", "simple_test", channel_count, channel_count > 1 ? 's' : ' ') ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: assert (channel_count <= MAX_CHANNELS) ; Chris@0: Chris@0: memset (input_serial, 0, sizeof (input_serial)) ; Chris@0: memset (input_interleaved, 0, sizeof (input_interleaved)) ; Chris@0: memset (output_interleaved, 0, sizeof (output_interleaved)) ; Chris@0: memset (output_serial, 0, sizeof (output_serial)) ; Chris@0: Chris@0: frames = BUFFER_LEN ; Chris@0: Chris@0: /* Calculate channel_count separate windowed sine waves. */ Chris@0: for (ch = 0 ; ch < channel_count ; ch++) Chris@0: { freq = (200.0 + 33.333333333 * ch) / 44100.0 ; Chris@0: gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; Chris@0: } ; Chris@0: Chris@0: /* Interleave the data in preparation for SRC. */ Chris@0: interleave_data (input_serial, input_interleaved, frames, channel_count) ; Chris@0: Chris@0: /* Choose a converstion ratio <= 1.0. */ Chris@0: src_data.src_ratio = 0.95 ; Chris@0: Chris@0: src_data.data_in = input_interleaved ; Chris@0: src_data.input_frames = frames ; Chris@0: Chris@0: src_data.data_out = output_interleaved ; Chris@0: src_data.output_frames = frames ; Chris@0: Chris@0: if ((error = src_simple (&src_data, converter, channel_count))) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (fabs (src_data.output_frames_gen - src_data.src_ratio * src_data.input_frames) > 2) Chris@0: { printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__, Chris@0: src_data.output_frames_gen, (int) floor (src_data.src_ratio * src_data.input_frames)) ; Chris@0: printf ("\tsrc_ratio : %.4f\n", src_data.src_ratio) ; Chris@0: printf ("\tinput_len : %ld\n", src_data.input_frames) ; Chris@0: printf ("\toutput_len : %ld\n\n", src_data.output_frames_gen) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* De-interleave data so SNR can be calculated for each channel. */ Chris@0: deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; Chris@0: Chris@0: for (ch = 0 ; ch < channel_count ; ch++) Chris@0: { snr = calculate_snr (output_serial + ch * frames, frames, 1) ; Chris@0: if (snr < target_snr) Chris@0: { printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; Chris@0: save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: puts ("ok") ; Chris@0: Chris@0: return ; Chris@0: } /* simple_test */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: static void Chris@0: process_test (int converter, int channel_count, double target_snr) Chris@0: { SRC_STATE *src_state ; Chris@0: SRC_DATA src_data ; Chris@0: Chris@0: double freq, snr ; Chris@0: int ch, error, frames, current_in, current_out ; Chris@0: Chris@0: printf ("\t%-22s (%2d channel%c) ............ ", "process_test", channel_count, channel_count > 1 ? 's' : ' ') ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: assert (channel_count <= MAX_CHANNELS) ; Chris@0: Chris@0: memset (input_serial, 0, sizeof (input_serial)) ; Chris@0: memset (input_interleaved, 0, sizeof (input_interleaved)) ; Chris@0: memset (output_interleaved, 0, sizeof (output_interleaved)) ; Chris@0: memset (output_serial, 0, sizeof (output_serial)) ; Chris@0: Chris@0: frames = BUFFER_LEN ; Chris@0: Chris@0: /* Calculate channel_count separate windowed sine waves. */ Chris@0: for (ch = 0 ; ch < channel_count ; ch++) Chris@0: { freq = (400.0 + 11.333333333 * ch) / 44100.0 ; Chris@0: gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; Chris@0: } ; Chris@0: Chris@0: /* Interleave the data in preparation for SRC. */ Chris@0: interleave_data (input_serial, input_interleaved, frames, channel_count) ; Chris@0: Chris@0: /* Perform sample rate conversion. */ Chris@0: if ((src_state = src_new (converter, channel_count, &error)) == NULL) Chris@0: { printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: src_data.end_of_input = 0 ; /* Set this later. */ Chris@0: Chris@0: /* Choose a converstion ratio < 1.0. */ Chris@0: src_data.src_ratio = 0.95 ; Chris@0: Chris@0: src_data.data_in = input_interleaved ; Chris@0: src_data.data_out = output_interleaved ; Chris@0: Chris@0: current_in = current_out = 0 ; Chris@0: Chris@0: while (1) Chris@0: { src_data.input_frames = MAX (MIN (BLOCK_LEN, frames - current_in), 0) ; Chris@0: src_data.output_frames = MAX (MIN (BLOCK_LEN, frames - current_out), 0) ; Chris@0: Chris@0: if ((error = src_process (src_state, &src_data))) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (src_data.end_of_input && src_data.output_frames_gen == 0) Chris@0: break ; Chris@0: Chris@0: current_in += src_data.input_frames_used ; Chris@0: current_out += src_data.output_frames_gen ; Chris@0: Chris@0: src_data.data_in += src_data.input_frames_used * channel_count ; Chris@0: src_data.data_out += src_data.output_frames_gen * channel_count ; Chris@0: Chris@0: src_data.end_of_input = (current_in >= frames) ? 1 : 0 ; Chris@0: } ; Chris@0: Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: if (fabs (current_out - src_data.src_ratio * current_in) > 2) Chris@0: { printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__, Chris@0: current_out, (int) floor (src_data.src_ratio * current_in)) ; Chris@0: printf ("\tsrc_ratio : %.4f\n", src_data.src_ratio) ; Chris@0: printf ("\tinput_len : %d\n", frames) ; Chris@0: printf ("\toutput_len : %d\n\n", current_out) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* De-interleave data so SNR can be calculated for each channel. */ Chris@0: deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; Chris@0: Chris@0: for (ch = 0 ; ch < channel_count ; ch++) Chris@0: { snr = calculate_snr (output_serial + ch * frames, frames, 1) ; Chris@0: if (snr < target_snr) Chris@0: { printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; Chris@0: save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: puts ("ok") ; Chris@0: Chris@0: return ; Chris@0: } /* process_test */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: typedef struct Chris@0: { int channels ; Chris@0: long total_frames ; Chris@0: long current_frame ; Chris@0: float *data ; Chris@0: } TEST_CB_DATA ; Chris@0: Chris@0: static long Chris@0: test_callback_func (void *cb_data, float **data) Chris@0: { TEST_CB_DATA *pcb_data ; Chris@0: Chris@0: long frames ; Chris@0: Chris@0: if ((pcb_data = cb_data) == NULL) Chris@0: return 0 ; Chris@0: Chris@0: if (data == NULL) Chris@0: return 0 ; Chris@0: Chris@0: *data = pcb_data->data + (pcb_data->current_frame * pcb_data->channels) ; Chris@0: Chris@0: if (pcb_data->total_frames - pcb_data->current_frame < BLOCK_LEN) Chris@0: frames = pcb_data->total_frames - pcb_data->current_frame ; Chris@0: else Chris@0: frames = BLOCK_LEN ; Chris@0: Chris@0: pcb_data->current_frame += frames ; Chris@0: Chris@0: return frames ; Chris@0: } /* test_callback_func */ Chris@0: Chris@0: static void Chris@0: callback_test (int converter, int channel_count, double target_snr) Chris@0: { TEST_CB_DATA test_callback_data ; Chris@0: SRC_STATE *src_state = NULL ; Chris@0: Chris@0: double freq, snr, src_ratio ; Chris@0: int ch, error, frames, read_total, read_count ; Chris@0: Chris@0: printf ("\t%-22s (%2d channel%c) ............ ", "callback_test", channel_count, channel_count > 1 ? 's' : ' ') ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: assert (channel_count <= MAX_CHANNELS) ; Chris@0: Chris@0: memset (input_serial, 0, sizeof (input_serial)) ; Chris@0: memset (input_interleaved, 0, sizeof (input_interleaved)) ; Chris@0: memset (output_interleaved, 0, sizeof (output_interleaved)) ; Chris@0: memset (output_serial, 0, sizeof (output_serial)) ; Chris@0: memset (&test_callback_data, 0, sizeof (test_callback_data)) ; Chris@0: Chris@0: frames = BUFFER_LEN ; Chris@0: Chris@0: /* Calculate channel_count separate windowed sine waves. */ Chris@0: for (ch = 0 ; ch < channel_count ; ch++) Chris@0: { freq = (200.0 + 33.333333333 * ch) / 44100.0 ; Chris@0: gen_windowed_sines (1, &freq, 1.0, input_serial + ch * frames, frames) ; Chris@0: } ; Chris@0: Chris@0: /* Interleave the data in preparation for SRC. */ Chris@0: interleave_data (input_serial, input_interleaved, frames, channel_count) ; Chris@0: Chris@0: /* Perform sample rate conversion. */ Chris@0: src_ratio = 0.95 ; Chris@0: test_callback_data.channels = channel_count ; Chris@0: test_callback_data.total_frames = frames ; Chris@0: test_callback_data.current_frame = 0 ; Chris@0: test_callback_data.data = input_interleaved ; Chris@0: Chris@0: if ((src_state = src_callback_new (test_callback_func, converter, channel_count, &error, &test_callback_data)) == NULL) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: read_total = 0 ; Chris@0: while (read_total < frames) Chris@0: { read_count = src_callback_read (src_state, src_ratio, frames - read_total, output_interleaved + read_total * channel_count) ; Chris@0: Chris@0: if (read_count <= 0) Chris@0: break ; Chris@0: Chris@0: read_total += read_count ; Chris@0: } ; Chris@0: Chris@0: if ((error = src_error (src_state)) != 0) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: if (fabs (read_total - src_ratio * frames) > 2) Chris@0: { printf ("\n\nLine %d : bad output data length %d should be %d.\n", __LINE__, Chris@0: read_total, (int) floor (src_ratio * frames)) ; Chris@0: printf ("\tsrc_ratio : %.4f\n", src_ratio) ; Chris@0: printf ("\tinput_len : %d\n", frames) ; Chris@0: printf ("\toutput_len : %d\n\n", read_total) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* De-interleave data so SNR can be calculated for each channel. */ Chris@0: deinterleave_data (output_interleaved, output_serial, frames, channel_count) ; Chris@0: Chris@0: for (ch = 0 ; ch < channel_count ; ch++) Chris@0: { snr = calculate_snr (output_serial + ch * frames, frames, 1) ; Chris@0: if (snr < target_snr) Chris@0: { printf ("\n\nLine %d: channel %d snr %f should be %f\n", __LINE__, ch, snr, target_snr) ; Chris@0: save_oct_float ("output.dat", input_serial, channel_count * frames, output_serial, channel_count * frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: puts ("ok") ; Chris@0: Chris@0: return ; Chris@0: } /* callback_test */ Chris@0: