Chris@0: /* Chris@0: ** Copyright (C) 2006-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 Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "util.h" Chris@0: Chris@0: #define BUFFER_LEN (1 << 16) Chris@0: Chris@0: static void varispeed_test (int converter, double target_snr) ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { Chris@0: puts ("") ; Chris@0: printf (" Zero Order Hold interpolator : ") ; Chris@0: varispeed_test (SRC_ZERO_ORDER_HOLD, 10.0) ; Chris@0: Chris@0: printf (" Linear interpolator : ") ; Chris@0: varispeed_test (SRC_LINEAR, 10.0) ; Chris@0: Chris@0: printf (" Sinc interpolator : ") ; Chris@0: varispeed_test (SRC_SINC_FASTEST, 115.0) ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: static void Chris@0: varispeed_test (int converter, double target_snr) Chris@0: { static float input [BUFFER_LEN], output [BUFFER_LEN] ; Chris@0: double sine_freq, snr ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: SRC_DATA src_data ; Chris@0: Chris@0: int input_len, error ; Chris@0: Chris@0: memset (input, 0, sizeof (input)) ; Chris@0: Chris@0: input_len = ARRAY_LEN (input) / 2 ; Chris@0: Chris@0: sine_freq = 0.0111 ; Chris@0: gen_windowed_sines (1, &sine_freq, 1.0, input, input_len) ; Chris@0: Chris@0: /* Perform sample rate conversion. */ Chris@0: if ((src_state = src_new (converter, 1, &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 = 1 ; Chris@0: Chris@0: src_data.data_in = input ; Chris@0: src_data.input_frames = input_len ; Chris@0: Chris@0: src_data.src_ratio = 3.0 ; Chris@0: Chris@0: src_data.data_out = output ; Chris@0: src_data.output_frames = ARRAY_LEN (output) ; Chris@0: Chris@0: if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio))) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@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: printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; Chris@0: printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (src_data.input_frames_used != input_len) Chris@0: { printf ("\n\nLine %d : unused input.\n", __LINE__) ; Chris@0: printf ("\tinput_len : %d\n", input_len) ; Chris@0: printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Copy the last output to the input. */ Chris@0: memcpy (input, output, sizeof (input)) ; Chris@0: reverse_data (input, src_data.output_frames_gen) ; Chris@0: Chris@0: if ((error = src_reset (src_state))) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: src_data.end_of_input = 1 ; Chris@0: Chris@0: src_data.data_in = input ; Chris@0: input_len = src_data.input_frames = src_data.output_frames_gen ; Chris@0: Chris@0: src_data.data_out = output ; Chris@0: src_data.output_frames = ARRAY_LEN (output) ; Chris@0: Chris@0: if ((error = src_set_ratio (src_state, 1.0 / src_data.src_ratio))) Chris@0: { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@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: printf (" src_data.input_frames : %ld\n", src_data.input_frames) ; Chris@0: printf (" src_data.output_frames : %ld\n\n", src_data.output_frames) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (src_data.input_frames_used != input_len) Chris@0: { printf ("\n\nLine %d : unused input.\n", __LINE__) ; Chris@0: printf ("\tinput_len : %d\n", input_len) ; Chris@0: printf ("\tinput_frames_used : %ld\n\n", src_data.input_frames_used) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: snr = calculate_snr (output, src_data.output_frames_gen, 1) ; Chris@0: Chris@0: if (target_snr > snr) Chris@0: { printf ("\n\nLine %d : snr (%3.1f) does not meet target (%3.1f)\n\n", __LINE__, snr, target_snr) ; Chris@0: save_oct_float ("varispeed.mat", input, src_data.input_frames, output, src_data.output_frames_gen) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: puts ("ok") ; Chris@0: Chris@0: return ; Chris@0: } /* varispeed_test */ Chris@0: