annotate src/libsamplerate-0.1.8/tests/reset_test.c @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents c7265573341e
children
rev   line source
Chris@0 1 /*
Chris@0 2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@0 3 **
Chris@0 4 ** This program is free software; you can redistribute it and/or modify
Chris@0 5 ** it under the terms of the GNU General Public License as published by
Chris@0 6 ** the Free Software Foundation; either version 2 of the License, or
Chris@0 7 ** (at your option) any later version.
Chris@0 8 **
Chris@0 9 ** This program is distributed in the hope that it will be useful,
Chris@0 10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 ** GNU General Public License for more details.
Chris@0 13 **
Chris@0 14 ** You should have received a copy of the GNU General Public License
Chris@0 15 ** along with this program; if not, write to the Free Software
Chris@0 16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
Chris@0 17 */
Chris@0 18
Chris@0 19 #include <stdio.h>
Chris@0 20 #include <stdlib.h>
Chris@0 21
Chris@0 22 #include <samplerate.h>
Chris@0 23
Chris@0 24 #include "util.h"
Chris@0 25
Chris@0 26 #define BUFFER_LEN 2048
Chris@0 27 #define CB_READ_LEN 256
Chris@0 28
Chris@0 29 static void process_reset_test (int converter) ;
Chris@0 30 static void callback_reset_test (int converter) ;
Chris@0 31
Chris@0 32 static float data_one [BUFFER_LEN] ;
Chris@0 33 static float data_zero [BUFFER_LEN] ;
Chris@0 34
Chris@0 35 int
Chris@0 36 main (void)
Chris@0 37 {
Chris@0 38 puts ("") ;
Chris@0 39
Chris@0 40 process_reset_test (SRC_ZERO_ORDER_HOLD) ;
Chris@0 41 process_reset_test (SRC_LINEAR) ;
Chris@0 42 process_reset_test (SRC_SINC_FASTEST) ;
Chris@0 43
Chris@0 44 callback_reset_test (SRC_ZERO_ORDER_HOLD) ;
Chris@0 45 callback_reset_test (SRC_LINEAR) ;
Chris@0 46 callback_reset_test (SRC_SINC_FASTEST) ;
Chris@0 47
Chris@0 48 puts ("") ;
Chris@0 49
Chris@0 50 return 0 ;
Chris@0 51 } /* main */
Chris@0 52
Chris@0 53 static void
Chris@0 54 process_reset_test (int converter)
Chris@0 55 { static float output [BUFFER_LEN] ;
Chris@0 56
Chris@0 57 SRC_STATE *src_state ;
Chris@0 58 SRC_DATA src_data ;
Chris@0 59 int k, error ;
Chris@0 60
Chris@0 61 printf ("\tprocess_reset_test (%-28s) ....... ", src_get_name (converter)) ;
Chris@0 62 fflush (stdout) ;
Chris@0 63
Chris@0 64 for (k = 0 ; k < BUFFER_LEN ; k++)
Chris@0 65 { data_one [k] = 1.0 ;
Chris@0 66 data_zero [k] = 0.0 ;
Chris@0 67 } ;
Chris@0 68
Chris@0 69 /* Get a converter. */
Chris@0 70 if ((src_state = src_new (converter, 1, &error)) == NULL)
Chris@0 71 { printf ("\n\nLine %d : src_new() failed : %s.\n\n", __LINE__, src_strerror (error)) ;
Chris@0 72 exit (1) ;
Chris@0 73 } ;
Chris@0 74
Chris@0 75 /* Process a bunch of 1.0 valued samples. */
Chris@0 76 src_data.data_in = data_one ;
Chris@0 77 src_data.data_out = output ;
Chris@0 78 src_data.input_frames = BUFFER_LEN ;
Chris@0 79 src_data.output_frames = BUFFER_LEN ;
Chris@0 80 src_data.src_ratio = 0.9 ;
Chris@0 81 src_data.end_of_input = 1 ;
Chris@0 82
Chris@0 83 if ((error = src_process (src_state, &src_data)) != 0)
Chris@0 84 { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ;
Chris@0 85 exit (1) ;
Chris@0 86 } ;
Chris@0 87
Chris@0 88 /* Reset the state of the converter.*/
Chris@0 89 src_reset (src_state) ;
Chris@0 90
Chris@0 91 /* Now process some zero data. */
Chris@0 92 src_data.data_in = data_zero ;
Chris@0 93 src_data.data_out = output ;
Chris@0 94 src_data.input_frames = BUFFER_LEN ;
Chris@0 95 src_data.output_frames = BUFFER_LEN ;
Chris@0 96 src_data.src_ratio = 0.9 ;
Chris@0 97 src_data.end_of_input = 1 ;
Chris@0 98
Chris@0 99 if ((error = src_process (src_state, &src_data)) != 0)
Chris@0 100 { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__, src_strerror (error)) ;
Chris@0 101 exit (1) ;
Chris@0 102 } ;
Chris@0 103
Chris@0 104 /* Finally make sure that the output data is zero ie reset was sucessful. */
Chris@0 105 for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
Chris@0 106 if (output [k] != 0.0)
Chris@0 107 { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n", __LINE__, k, output [k]) ;
Chris@0 108 exit (1) ;
Chris@0 109 } ;
Chris@0 110
Chris@0 111 /* Make sure that this function has been exported. */
Chris@0 112 src_set_ratio (src_state, 1.0) ;
Chris@0 113
Chris@0 114 /* Delete converter. */
Chris@0 115 src_state = src_delete (src_state) ;
Chris@0 116
Chris@0 117 puts ("ok") ;
Chris@0 118 } /* process_reset_test */
Chris@0 119
Chris@0 120 /*==============================================================================
Chris@0 121 */
Chris@0 122
Chris@0 123 typedef struct
Chris@0 124 { int channels ;
Chris@0 125 long count, total ;
Chris@0 126 float *data ;
Chris@0 127 } TEST_CB_DATA ;
Chris@0 128
Chris@0 129 static long
Chris@0 130 test_callback_func (void *cb_data, float **data)
Chris@0 131 { TEST_CB_DATA *pcb_data ;
Chris@0 132
Chris@0 133 long frames ;
Chris@0 134
Chris@0 135 if ((pcb_data = cb_data) == NULL)
Chris@0 136 return 0 ;
Chris@0 137
Chris@0 138 if (data == NULL)
Chris@0 139 return 0 ;
Chris@0 140
Chris@0 141 if (pcb_data->total - pcb_data->count > 0)
Chris@0 142 frames = pcb_data->total - pcb_data->count ;
Chris@0 143 else
Chris@0 144 frames = 0 ;
Chris@0 145
Chris@0 146 *data = pcb_data->data + pcb_data->count ;
Chris@0 147 pcb_data->count += frames ;
Chris@0 148
Chris@0 149 return frames ;
Chris@0 150 } /* test_callback_func */
Chris@0 151
Chris@0 152 static void
Chris@0 153 callback_reset_test (int converter)
Chris@0 154 { static TEST_CB_DATA test_callback_data ;
Chris@0 155
Chris@0 156 static float output [BUFFER_LEN] ;
Chris@0 157
Chris@0 158 SRC_STATE *src_state ;
Chris@0 159
Chris@0 160 double src_ratio = 1.1 ;
Chris@0 161 long read_count, read_total ;
Chris@0 162 int k, error ;
Chris@0 163
Chris@0 164 printf ("\tcallback_reset_test (%-28s) ....... ", src_get_name (converter)) ;
Chris@0 165 fflush (stdout) ;
Chris@0 166
Chris@0 167 for (k = 0 ; k < ARRAY_LEN (data_one) ; k++)
Chris@0 168 { data_one [k] = 1.0 ;
Chris@0 169 data_zero [k] = 0.0 ;
Chris@0 170 } ;
Chris@0 171
Chris@0 172 if ((src_state = src_callback_new (test_callback_func, converter, 1, &error, &test_callback_data)) == NULL)
Chris@0 173 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
Chris@0 174 exit (1) ;
Chris@0 175 } ;
Chris@0 176
Chris@0 177 /* Process a bunch of 1.0 valued samples. */
Chris@0 178 test_callback_data.channels = 1 ;
Chris@0 179 test_callback_data.count = 0 ;
Chris@0 180 test_callback_data.total = ARRAY_LEN (data_one) ;
Chris@0 181 test_callback_data.data = data_one ;
Chris@0 182
Chris@0 183 read_total = 0 ;
Chris@0 184 do
Chris@0 185 { read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ;
Chris@0 186 read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ;
Chris@0 187 read_total += read_count ;
Chris@0 188 }
Chris@0 189 while (read_count > 0) ;
Chris@0 190
Chris@0 191 /* Check for errors. */
Chris@0 192 if ((error = src_error (src_state)) != 0)
Chris@0 193 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
Chris@0 194 exit (1) ;
Chris@0 195 } ;
Chris@0 196
Chris@0 197 /* Reset the state of the converter.*/
Chris@0 198 src_reset (src_state) ;
Chris@0 199
Chris@0 200 /* Process a bunch of 0.0 valued samples. */
Chris@0 201 test_callback_data.channels = 1 ;
Chris@0 202 test_callback_data.count = 0 ;
Chris@0 203 test_callback_data.total = ARRAY_LEN (data_zero) ;
Chris@0 204 test_callback_data.data = data_zero ;
Chris@0 205
Chris@0 206 /* Now process some zero data. */
Chris@0 207 read_total = 0 ;
Chris@0 208 do
Chris@0 209 { read_count = (ARRAY_LEN (output) - read_total > CB_READ_LEN) ? CB_READ_LEN : ARRAY_LEN (output) - read_total ;
Chris@0 210 read_count = src_callback_read (src_state, src_ratio, read_count, output + read_total) ;
Chris@0 211 read_total += read_count ;
Chris@0 212 }
Chris@0 213 while (read_count > 0) ;
Chris@0 214
Chris@0 215 /* Check for errors. */
Chris@0 216 if ((error = src_error (src_state)) != 0)
Chris@0 217 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
Chris@0 218 exit (1) ;
Chris@0 219 } ;
Chris@0 220
Chris@0 221 /* Finally make sure that the output data is zero ie reset was sucessful. */
Chris@0 222 for (k = 0 ; k < BUFFER_LEN / 2 ; k++)
Chris@0 223 if (output [k] != 0.0)
Chris@0 224 { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n\n", __LINE__, k, output [k]) ;
Chris@0 225 save_oct_float ("output.dat", data_one, ARRAY_LEN (data_one), output, ARRAY_LEN (output)) ;
Chris@0 226 exit (1) ;
Chris@0 227 } ;
Chris@0 228
Chris@0 229 /* Make sure that this function has been exported. */
Chris@0 230 src_set_ratio (src_state, 1.0) ;
Chris@0 231
Chris@0 232 /* Delete converter. */
Chris@0 233 src_state = src_delete (src_state) ;
Chris@0 234
Chris@0 235 puts ("ok") ;
Chris@0 236 } /* callback_reset_test */
Chris@0 237
Chris@0 238