Debian Codegraph @

tests/callback_test.c

raw
1 /*
2 ** Copyright (c) 2003-2016, Erik de Castro Lopo <[email protected]>
3 ** All rights reserved.
4 **
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/libsndfile/libsamplerate/blob/master/COPYING
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <math.h>
17
18 #include <samplerate.h>
19
20 #include "util.h"
21
22 #define BUFFER_LEN 10000
23 #define CB_READ_LEN 256
24
25 static void callback_test (int converter, double ratio) ;
26 static void end_of_stream_test (int converter) ;
27
28 int
29 main (void)
30 { static double src_ratios [] =
31 { 1.0, 0.099, 0.1, 0.33333333, 0.789, 1.0001, 1.9, 3.1, 9.9
32 } ;
33
34 int k ;
35
36 puts ("") ;
37
38 puts (" Zero Order Hold interpolator :") ;
39 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
40 callback_test (SRC_ZERO_ORDER_HOLD, src_ratios [k]) ;
41
42 puts (" Linear interpolator :") ;
43 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
44 callback_test (SRC_LINEAR, src_ratios [k]) ;
45
46 #ifdef ENABLE_SINC_FAST_CONVERTER
47 puts (" Sinc interpolator :") ;
48 for (k = 0 ; k < ARRAY_LEN (src_ratios) ; k++)
49 callback_test (SRC_SINC_FASTEST, src_ratios [k]) ;
50 #endif
51 puts ("") ;
52
53 puts (" End of stream test :") ;
54 end_of_stream_test (SRC_ZERO_ORDER_HOLD) ;
55 end_of_stream_test (SRC_LINEAR) ;
56 #ifdef ENABLE_SINC_FAST_CONVERTER
57 end_of_stream_test (SRC_SINC_FASTEST) ;
58 #endif
59
60 puts ("") ;
61 return 0 ;
62 } /* main */
63
64 /*=====================================================================================
65 */
66
67 typedef struct
68 { int channels ;
69 long count, total ;
70 int end_of_data ;
71 float data [BUFFER_LEN] ;
72 } TEST_CB_DATA ;
73
74 static long
75 test_callback_func (void *cb_data, float **data)
76 { TEST_CB_DATA *pcb_data ;
77
78 long frames ;
79
80 if ((pcb_data = cb_data) == NULL)
81 return 0 ;
82
83 if (data == NULL)
84 return 0 ;
85
86 if (pcb_data->total - pcb_data->count > CB_READ_LEN)
87 frames = CB_READ_LEN / pcb_data->channels ;
88 else
89 frames = (pcb_data->total - pcb_data->count) / pcb_data->channels ;
90
91 *data = pcb_data->data + pcb_data->count ;
92 pcb_data->count += frames ;
93
94 return frames ;
95 } /* test_callback_func */
96
97
98 static void
99 callback_test (int converter, double src_ratio)
100 { static TEST_CB_DATA test_callback_data ;
101 static float output [BUFFER_LEN] ;
102
103 SRC_STATE *src_state ;
104
105 long read_count, read_total ;
106 int error ;
107
108 printf ("\tcallback_test (SRC ratio = %6.4f) ........... ", src_ratio) ;
109 fflush (stdout) ;
110
111 test_callback_data.channels = 2 ;
112 test_callback_data.count = 0 ;
113 test_callback_data.end_of_data = 0 ;
114 test_callback_data.total = ARRAY_LEN (test_callback_data.data) ;
115
116 if ((src_state = src_callback_new (test_callback_func, converter, test_callback_data.channels, &error, &test_callback_data)) == NULL)
117 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
118 exit (1) ;
119 } ;
120
121 read_total = 0 ;
122 do
123 { /* We will be throwing away output data, so just grab as much as possible. */
124 read_count = ARRAY_LEN (output) / test_callback_data.channels ;
125 read_count = src_callback_read (src_state, src_ratio, read_count, output) ;
126 read_total += read_count ;
127 }
128 while (read_count > 0) ;
129
130 if ((error = src_error (src_state)) != 0)
131 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
132 exit (1) ;
133 } ;
134
135 src_delete (src_state) ;
136
137 if (fabs (read_total / src_ratio - ARRAY_LEN (test_callback_data.data)) > 2.0)
138 { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ;
139 printf (" input len : %d\n", ARRAY_LEN (test_callback_data.data)) ;
140 printf (" output len : %ld (should be %g +/- 2)\n\n", read_total,
141 floor (0.5 + src_ratio * ARRAY_LEN (test_callback_data.data))) ;
142 exit (1) ;
143 } ;
144
145 puts ("ok") ;
146
147 return ;
148 } /* callback_test */
149
150 /*=====================================================================================
151 */
152
153 static long
154 eos_callback_func (void *cb_data, float **data)
155 {
156 TEST_CB_DATA *pcb_data ;
157 long frames ;
158
159 if (data == NULL)
160 return 0 ;
161
162 if ((pcb_data = cb_data) == NULL)
163 return 0 ;
164
165 /*
166 ** Return immediately if there is no more data.
167 ** In this case, the output pointer 'data' will not be set and
168 ** valgrind should not warn about it.
169 */
170 if (pcb_data->end_of_data)
171 return 0 ;
172
173 if (pcb_data->total - pcb_data->count > CB_READ_LEN)
174 frames = CB_READ_LEN / pcb_data->channels ;
175 else
176 frames = (pcb_data->total - pcb_data->count) / pcb_data->channels ;
177
178 *data = pcb_data->data + pcb_data->count ;
179 pcb_data->count += frames ;
180
181 /*
182 ** Set end_of_data so that the next call to the callback function will
183 ** return zero ocunt without setting the 'data' pointer.
184 */
185 if (pcb_data->total < 2 * pcb_data->count)
186 pcb_data->end_of_data = 1 ;
187
188 return frames ;
189 } /* eos_callback_data */
190
191
192 static void
193 end_of_stream_test (int converter)
194 { static TEST_CB_DATA test_callback_data ;
195 static float output [BUFFER_LEN] ;
196
197 SRC_STATE *src_state ;
198
199 double src_ratio = 0.3 ;
200 long read_count, read_total ;
201 int error ;
202
203 printf ("\t%-30s ........... ", src_get_name (converter)) ;
204 fflush (stdout) ;
205
206 test_callback_data.channels = 2 ;
207 test_callback_data.count = 0 ;
208 test_callback_data.end_of_data = 0 ;
209 test_callback_data.total = ARRAY_LEN (test_callback_data.data) ;
210
211 if ((src_state = src_callback_new (eos_callback_func, converter, test_callback_data.channels, &error, &test_callback_data)) == NULL)
212 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
213 exit (1) ;
214 } ;
215
216 read_total = 0 ;
217 do
218 { /* We will be throwing away output data, so just grab as much as possible. */
219 read_count = ARRAY_LEN (output) / test_callback_data.channels ;
220 read_count = src_callback_read (src_state, src_ratio, read_count, output) ;
221 read_total += read_count ;
222 }
223 while (read_count > 0) ;
224
225 if ((error = src_error (src_state)) != 0)
226 { printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
227 exit (1) ;
228 } ;
229
230 src_delete (src_state) ;
231
232 if (test_callback_data.end_of_data == 0)
233 { printf ("\n\nLine %d : test_callback_data.end_of_data should not be 0."
234 " This is a bug in the test.\n\n", __LINE__) ;
235 exit (1) ;
236 } ;
237
238 puts ("ok") ;
239 return ;
240 } /* end_of_stream_test */