Chris@0: /* Chris@0: ** Copyright (C) 2003-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 10000 Chris@0: #define CB_READ_LEN 256 Chris@0: Chris@0: static void callback_test (int converter, double ratio) ; Chris@0: static void end_of_stream_test (int converter) ; Chris@0: Chris@0: int Chris@0: main (void) Chris@0: { static double src_ratios [] = Chris@0: { 1.0, 0.099, 0.1, 0.33333333, 0.789, 1.0001, 1.9, 3.1, 9.9 Chris@0: } ; Chris@0: Chris@0: int k ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: puts (" Zero Order Hold interpolator :") ; Chris@0: for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) Chris@0: callback_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ; Chris@0: Chris@0: puts (" Linear interpolator :") ; Chris@0: for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) Chris@0: callback_test (SRC_LINEAR, src_ratios [k]) ; Chris@0: Chris@0: puts (" Sinc interpolator :") ; Chris@0: for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++) Chris@0: callback_test (SRC_SINC_FASTEST, src_ratios [k]) ; Chris@0: Chris@0: puts ("") ; Chris@0: Chris@0: puts (" End of stream test :") ; Chris@0: end_of_stream_test (SRC_ZERO_ORDER_HOLD) ; Chris@0: end_of_stream_test (SRC_LINEAR) ; Chris@0: end_of_stream_test (SRC_SINC_FASTEST) ; Chris@0: Chris@0: puts ("") ; Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*===================================================================================== Chris@0: */ Chris@0: Chris@0: typedef struct Chris@0: { int channels ; Chris@0: long count, total ; Chris@0: int end_of_data ; Chris@0: float data [BUFFER_LEN] ; 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 > CB_READ_LEN) Chris@0: frames = CB_READ_LEN / pcb_data->channels ; Chris@0: else Chris@0: frames = (pcb_data->total - pcb_data->count) / pcb_data->channels ; 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: Chris@0: static void Chris@0: callback_test (int converter, double src_ratio) Chris@0: { static TEST_CB_DATA test_callback_data ; Chris@0: static float output [BUFFER_LEN] ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: Chris@0: long read_count, read_total ; Chris@0: int error ; Chris@0: Chris@0: printf ("\tcallback_test (SRC ratio = %6.4f) ........... ", src_ratio) ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: test_callback_data.channels = 2 ; Chris@0: test_callback_data.count = 0 ; Chris@0: test_callback_data.end_of_data = 0 ; Chris@0: test_callback_data.total = ARRAY_LEN (test_callback_data.data) ; Chris@0: Chris@0: if ((src_state = src_callback_new (test_callback_func, converter, test_callback_data.channels, &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: do Chris@0: { /* We will be throwing away output data, so just grab as much as possible. */ Chris@0: read_count = ARRAY_LEN (output) / test_callback_data.channels ; Chris@0: read_count = src_callback_read (src_state, src_ratio, read_count, output) ; Chris@0: read_total += read_count ; Chris@0: } Chris@0: while (read_count > 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 - ARRAY_LEN (test_callback_data.data)) > 2.0) Chris@0: { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ; Chris@0: printf (" input len : %d\n", ARRAY_LEN (test_callback_data.data)) ; Chris@0: printf (" output len : %ld (should be %g +/- 2)\n\n", read_total, Chris@0: floor (0.5 + src_ratio * ARRAY_LEN (test_callback_data.data))) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: puts ("ok") ; Chris@0: Chris@0: return ; Chris@0: } /* callback_test */ Chris@0: Chris@0: /*===================================================================================== Chris@0: */ Chris@0: Chris@0: static long Chris@0: eos_callback_func (void *cb_data, float **data) Chris@0: { Chris@0: TEST_CB_DATA *pcb_data ; Chris@0: long frames ; Chris@0: Chris@0: if (data == NULL) Chris@0: return 0 ; Chris@0: Chris@0: if ((pcb_data = cb_data) == NULL) Chris@0: return 0 ; Chris@0: Chris@0: /* Chris@0: ** Return immediately if there is no more data. Chris@0: ** In this case, the output pointer 'data' will not be set and Chris@0: ** valgrind should not warn about it. Chris@0: */ Chris@0: if (pcb_data->end_of_data) Chris@0: return 0 ; Chris@0: Chris@0: if (pcb_data->total - pcb_data->count > CB_READ_LEN) Chris@0: frames = CB_READ_LEN / pcb_data->channels ; Chris@0: else Chris@0: frames = (pcb_data->total - pcb_data->count) / pcb_data->channels ; Chris@0: Chris@0: *data = pcb_data->data + pcb_data->count ; Chris@0: pcb_data->count += frames ; Chris@0: Chris@0: /* Chris@0: ** Set end_of_data so that the next call to the callback function will Chris@0: ** return zero ocunt without setting the 'data' pointer. Chris@0: */ Chris@0: if (pcb_data->total < 2 * pcb_data->count) Chris@0: pcb_data->end_of_data = 1 ; Chris@0: Chris@0: return frames ; Chris@0: } /* eos_callback_data */ Chris@0: Chris@0: Chris@0: static void Chris@0: end_of_stream_test (int converter) Chris@0: { static TEST_CB_DATA test_callback_data ; Chris@0: static float output [BUFFER_LEN] ; Chris@0: Chris@0: SRC_STATE *src_state ; Chris@0: Chris@0: double src_ratio = 0.3 ; Chris@0: long read_count, read_total ; Chris@0: int error ; Chris@0: Chris@0: printf ("\t%-30s ........... ", src_get_name (converter)) ; Chris@0: fflush (stdout) ; Chris@0: Chris@0: test_callback_data.channels = 2 ; Chris@0: test_callback_data.count = 0 ; Chris@0: test_callback_data.end_of_data = 0 ; Chris@0: test_callback_data.total = ARRAY_LEN (test_callback_data.data) ; Chris@0: Chris@0: if ((src_state = src_callback_new (eos_callback_func, converter, test_callback_data.channels, &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: do Chris@0: { /* We will be throwing away output data, so just grab as much as possible. */ Chris@0: read_count = ARRAY_LEN (output) / test_callback_data.channels ; Chris@0: read_count = src_callback_read (src_state, src_ratio, read_count, output) ; Chris@0: read_total += read_count ; Chris@0: } Chris@0: while (read_count > 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 (test_callback_data.end_of_data == 0) Chris@0: { printf ("\n\nLine %d : test_callback_data.end_of_data should not be 0." Chris@0: " This is a bug in the test.\n\n", __LINE__) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: puts ("ok") ; Chris@0: return ; Chris@0: } /* end_of_stream_test */