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 Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "util.h" Chris@0: Chris@0: #define BUFFER_LEN 2048 Chris@0: #define CB_READ_LEN 256 Chris@0: Chris@0: static void process_reset_test (int converter) ; Chris@0: static void callback_reset_test (int converter) ; Chris@0: Chris@0: static float data_one [BUFFER_LEN] ; Chris@0: static float data_zero [BUFFER_LEN] ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { Chris@0: puts ("") ; Chris@0: Chris@0: process_reset_test (SRC_ZERO_ORDER_HOLD) ; Chris@0: process_reset_test (SRC_LINEAR) ; Chris@0: process_reset_test (SRC_SINC_FASTEST) ; Chris@0: Chris@0: callback_reset_test (SRC_ZERO_ORDER_HOLD) ; Chris@0: callback_reset_test (SRC_LINEAR) ; Chris@0: callback_reset_test (SRC_SINC_FASTEST) ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: static void Chris@0: process_reset_test (int converter) Chris@0: { static float output [BUFFER_LEN] ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: SRC_DATA src_data ; Chris@0: int k, error ; Chris@0: Chris@0: printf ("\tprocess_reset_test (%-28s) ....... ", src_get_name (converter)) ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: for (k = 0 ; k < BUFFER_LEN ; k++) Chris@0: { data_one [k] = 1.0 ; Chris@0: data_zero [k] = 0.0 ; Chris@0: } ; Chris@0: Chris@0: /* Get a converter. */ 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: /* Process a bunch of 1.0 valued samples. */ Chris@0: src_data.data_in = data_one ; Chris@0: src_data.data_out = output ; Chris@0: src_data.input_frames = BUFFER_LEN ; Chris@0: src_data.output_frames = BUFFER_LEN ; Chris@0: src_data.src_ratio = 0.9 ; Chris@0: src_data.end_of_input = 1 ; Chris@0: Chris@0: if ((error = src_process (src_state, &src_data)) != 0) Chris@0: { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Reset the state of the converter.*/ Chris@0: src_reset (src_state) ; Chris@0: Chris@0: /* Now process some zero data. */ Chris@0: src_data.data_in = data_zero ; Chris@0: src_data.data_out = output ; Chris@0: src_data.input_frames = BUFFER_LEN ; Chris@0: src_data.output_frames = BUFFER_LEN ; Chris@0: src_data.src_ratio = 0.9 ; Chris@0: src_data.end_of_input = 1 ; Chris@0: Chris@0: if ((error = src_process (src_state, &src_data)) != 0) Chris@0: { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Finally make sure that the output data is zero ie reset was sucessful. */ Chris@0: for (k = 0 ; k < BUFFER_LEN / 2 ; k++) Chris@0: if (output [k] != 0.0) Chris@0: { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n", __LINE__, k, output [k]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Make sure that this function has been exported. */ Chris@0: src_set_ratio (src_state, 1.0) ; Chris@0: Chris@0: /* Delete converter. */ Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: puts ("ok") ; Chris@0: } /* process_reset_test */ Chris@0: Chris@0: /*============================================================================== Chris@0: */ Chris@0: Chris@0: typedef struct Chris@0: { int channels ; Chris@0: long count, total ; 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: if (pcb_data->total - pcb_data->count > 0) Chris@0: frames = pcb_data->total - pcb_data->count ; Chris@0: else Chris@0: frames = 0 ; Chris@0: Chris@0: *data = pcb_data->data + pcb_data->count ; Chris@0: pcb_data->count += frames ; Chris@0: Chris@0: return frames ; Chris@0: } /* test_callback_func */ Chris@0: Chris@0: static void Chris@0: callback_reset_test (int converter) Chris@0: { static TEST_CB_DATA test_callback_data ; Chris@0: Chris@0: static float output [BUFFER_LEN] ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: Chris@0: double src_ratio = 1.1 ; Chris@0: long read_count, read_total ; Chris@0: int k, error ; Chris@0: Chris@0: printf ("\tcallback_reset_test (%-28s) ....... ", src_get_name (converter)) ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: for (k = 0 ; k < ARRAY_LEN (data_one) ; k++) Chris@0: { data_one [k] = 1.0 ; Chris@0: data_zero [k] = 0.0 ; Chris@0: } ; Chris@0: Chris@0: if ((src_state = src_callback_new (test_callback_func, converter, 1, &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: /* Process a bunch of 1.0 valued samples. */ Chris@0: test_callback_data.channels = 1 ; Chris@0: test_callback_data.count = 0 ; Chris@0: test_callback_data.total = ARRAY_LEN (data_one) ; Chris@0: test_callback_data.data = data_one ; Chris@0: Chris@0: read_total = 0 ; Chris@0: do Chris@0: { read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ; Chris@0: read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ; Chris@0: read_total += read_count ; Chris@0: } Chris@0: while (read_count > 0) ; Chris@0: Chris@0: /* Check for errors. */ 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: /* Reset the state of the converter.*/ Chris@0: src_reset (src_state) ; Chris@0: Chris@0: /* Process a bunch of 0.0 valued samples. */ Chris@0: test_callback_data.channels = 1 ; Chris@0: test_callback_data.count = 0 ; Chris@0: test_callback_data.total = ARRAY_LEN (data_zero) ; Chris@0: test_callback_data.data = data_zero ; Chris@0: Chris@0: /* Now process some zero data. */ Chris@0: read_total = 0 ; Chris@0: do Chris@0: { read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ; Chris@0: read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ; Chris@0: read_total += read_count ; Chris@0: } Chris@0: while (read_count > 0) ; Chris@0: Chris@0: /* Check for errors. */ 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: /* Finally make sure that the output data is zero ie reset was sucessful. */ Chris@0: for (k = 0 ; k < BUFFER_LEN / 2 ; k++) Chris@0: if (output [k] != 0.0) Chris@0: { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n\n", __LINE__, k, output [k]) ; Chris@0: save_oct_float ("output.dat", data_one, ARRAY_LEN (data_one), output, ARRAY_LEN (output)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: /* Make sure that this function has been exported. */ Chris@0: src_set_ratio (src_state, 1.0) ; Chris@0: Chris@0: /* Delete converter. */ Chris@0: src_state = src_delete (src_state) ; Chris@0: Chris@0: puts ("ok") ; Chris@0: } /* callback_reset_test */ Chris@0: Chris@0: