2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** All rights reserved.
5 ** This code is released under 2-clause BSD license. Please see the
6 ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
12 #include <samplerate.h>
16 #define BUFFER_LEN 2048
17 #define CB_READ_LEN 256
19 static void process_reset_test (int converter
) ;
20 static void callback_reset_test (int converter
) ;
22 static float data_one
[BUFFER_LEN
] ;
23 static float data_zero
[BUFFER_LEN
] ;
30 process_reset_test (SRC_ZERO_ORDER_HOLD
) ;
31 process_reset_test (SRC_LINEAR
) ;
32 process_reset_test (SRC_SINC_FASTEST
) ;
34 callback_reset_test (SRC_ZERO_ORDER_HOLD
) ;
35 callback_reset_test (SRC_LINEAR
) ;
36 callback_reset_test (SRC_SINC_FASTEST
) ;
44 process_reset_test (int converter
)
45 { static float output
[BUFFER_LEN
] ;
47 SRC_STATE
*src_state
;
51 printf ("\tprocess_reset_test (%-28s) ....... ", src_get_name (converter
)) ;
54 for (k
= 0 ; k
< BUFFER_LEN
; k
++)
55 { data_one
[k
] = 1.0 ;
59 /* Get a converter. */
60 if ((src_state
= src_new (converter
, 1, &error
)) == NULL
)
61 { printf ("\n\nLine %d : src_new() failed : %s.\n\n", __LINE__
, src_strerror (error
)) ;
65 /* Process a bunch of 1.0 valued samples. */
66 src_data
.data_in
= data_one
;
67 src_data
.data_out
= output
;
68 src_data
.input_frames
= BUFFER_LEN
;
69 src_data
.output_frames
= BUFFER_LEN
;
70 src_data
.src_ratio
= 0.9 ;
71 src_data
.end_of_input
= 1 ;
73 if ((error
= src_process (src_state
, &src_data
)) != 0)
74 { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__
, src_strerror (error
)) ;
78 /* Reset the state of the converter.*/
79 src_reset (src_state
) ;
81 /* Now process some zero data. */
82 src_data
.data_in
= data_zero
;
83 src_data
.data_out
= output
;
84 src_data
.input_frames
= BUFFER_LEN
;
85 src_data
.output_frames
= BUFFER_LEN
;
86 src_data
.src_ratio
= 0.9 ;
87 src_data
.end_of_input
= 1 ;
89 if ((error
= src_process (src_state
, &src_data
)) != 0)
90 { printf ("\n\nLine %d : src_simple () returned error : %s\n\n", __LINE__
, src_strerror (error
)) ;
94 /* Finally make sure that the output data is zero ie reset was sucessful. */
95 for (k
= 0 ; k
< BUFFER_LEN
/ 2 ; k
++)
96 if (output
[k
] != 0.0)
97 { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n", __LINE__
, k
, output
[k
]) ;
101 /* Make sure that this function has been exported. */
102 src_set_ratio (src_state
, 1.0) ;
104 /* Delete converter. */
105 src_state
= src_delete (src_state
) ;
108 } /* process_reset_test */
110 /*==============================================================================
120 test_callback_func (void *cb_data
, float **data
)
121 { TEST_CB_DATA
*pcb_data
;
125 if ((pcb_data
= cb_data
) == NULL
)
131 if (pcb_data
->total
- pcb_data
->count
> 0)
132 frames
= pcb_data
->total
- pcb_data
->count
;
136 *data
= pcb_data
->data
+ pcb_data
->count
;
137 pcb_data
->count
+= frames
;
140 } /* test_callback_func */
143 callback_reset_test (int converter
)
144 { static TEST_CB_DATA test_callback_data
;
146 static float output
[BUFFER_LEN
] ;
148 SRC_STATE
*src_state
;
150 double src_ratio
= 1.1 ;
151 long read_count
, read_total
;
154 printf ("\tcallback_reset_test (%-28s) ....... ", src_get_name (converter
)) ;
157 for (k
= 0 ; k
< ARRAY_LEN (data_one
) ; k
++)
158 { data_one
[k
] = 1.0 ;
159 data_zero
[k
] = 0.0 ;
162 if ((src_state
= src_callback_new (test_callback_func
, converter
, 1, &error
, &test_callback_data
)) == NULL
)
163 { printf ("\n\nLine %d : %s\n\n", __LINE__
, src_strerror (error
)) ;
167 /* Process a bunch of 1.0 valued samples. */
168 test_callback_data
.channels
= 1 ;
169 test_callback_data
.count
= 0 ;
170 test_callback_data
.total
= ARRAY_LEN (data_one
) ;
171 test_callback_data
.data
= data_one
;
175 { read_count
= (ARRAY_LEN (output
) - read_total
> CB_READ_LEN
) ? CB_READ_LEN
: ARRAY_LEN (output
) - read_total
;
176 read_count
= src_callback_read (src_state
, src_ratio
, read_count
, output
+ read_total
) ;
177 read_total
+= read_count
;
179 while (read_count
> 0) ;
181 /* Check for errors. */
182 if ((error
= src_error (src_state
)) != 0)
183 { printf ("\n\nLine %d : %s\n\n", __LINE__
, src_strerror (error
)) ;
187 /* Reset the state of the converter.*/
188 src_reset (src_state
) ;
190 /* Process a bunch of 0.0 valued samples. */
191 test_callback_data
.channels
= 1 ;
192 test_callback_data
.count
= 0 ;
193 test_callback_data
.total
= ARRAY_LEN (data_zero
) ;
194 test_callback_data
.data
= data_zero
;
196 /* Now process some zero data. */
199 { read_count
= (ARRAY_LEN (output
) - read_total
> CB_READ_LEN
) ? CB_READ_LEN
: ARRAY_LEN (output
) - read_total
;
200 read_count
= src_callback_read (src_state
, src_ratio
, read_count
, output
+ read_total
) ;
201 read_total
+= read_count
;
203 while (read_count
> 0) ;
205 /* Check for errors. */
206 if ((error
= src_error (src_state
)) != 0)
207 { printf ("\n\nLine %d : %s\n\n", __LINE__
, src_strerror (error
)) ;
211 /* Finally make sure that the output data is zero ie reset was sucessful. */
212 for (k
= 0 ; k
< BUFFER_LEN
/ 2 ; k
++)
213 if (output
[k
] != 0.0)
214 { printf ("\n\nLine %d : output [%d] should be 0.0, is %f.\n\n", __LINE__
, k
, output
[k
]) ;
215 save_oct_float ("output.dat", data_one
, ARRAY_LEN (data_one
), output
, ARRAY_LEN (output
)) ;
219 /* Make sure that this function has been exported. */
220 src_set_ratio (src_state
, 1.0) ;
222 /* Delete converter. */
223 src_state
= src_delete (src_state
) ;
226 } /* callback_reset_test */