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