• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
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 
17 #include <samplerate.h>
18 
19 #include "util.h"
20 
21 static void name_test (void) ;
22 static void error_test (void) ;
23 static void src_ratio_test (void) ;
24 static void zero_input_test (int converter) ;
25 static void get_channels_test (int converter);
26 
27 int
main(void)28 main (void)
29 {
30 	puts ("") ;
31 
32 	printf ("    version : %s\n\n", src_get_version ()) ;
33 
34 	/* Current max converter is SRC_LINEAR. */
35 	name_test () ;
36 
37 	error_test () ;
38 
39 	src_ratio_test () ;
40 
41 	zero_input_test (SRC_ZERO_ORDER_HOLD) ;
42 	zero_input_test (SRC_LINEAR) ;
43 #ifdef ENABLE_SINC_FAST_CONVERTER
44 	zero_input_test (SRC_SINC_FASTEST) ;
45 #endif
46 	get_channels_test (SRC_ZERO_ORDER_HOLD) ;
47 	get_channels_test (SRC_LINEAR) ;
48 #ifdef ENABLE_SINC_FAST_CONVERTER
49 	get_channels_test (SRC_SINC_FASTEST) ;
50 #endif
51 	puts ("") ;
52 	return 0 ;
53 } /* main */
54 
55 static void
name_test(void)56 name_test (void)
57 {	const char	*name ;
58 	int	k = 0 ;
59 
60 	puts ("    name_test :") ;
61 
62 	while (1)
63 	{	name = src_get_name (k) ;
64 		if (name == NULL)
65 			break ;
66 		printf ("\tName %d : %s\n", k, name) ;
67 		printf ("\tDesc %d : %s\n", k, src_get_description (k)) ;
68 		k ++ ;
69 		} ;
70 
71 	puts ("") ;
72 
73 	return ;
74 } /* name_test */
75 
76 /*------------------------------------------------------------------------------
77 */
78 
79 typedef struct
80 {	double	ratio ;
81 	int		should_pass ;
82 } RATIO_TEST ;
83 
84 static RATIO_TEST ratio_test [] =
85 {	{	1.0 / 256.1,	0 },
86 	{	1.0 / 256.0,	1 },
87 	{	1.0,			1 },
88 	{	256.0, 			1 },
89 	{	256.1,			0 },
90 	{	-1.0,			0 }
91 } ;
92 
93 static void
src_ratio_test(void)94 src_ratio_test (void)
95 {	int k ;
96 
97 	puts ("    src_ratio_test (SRC ratio must be in range [1/256, 256]):" ) ;
98 
99 
100 	for (k = 0 ; k < ARRAY_LEN (ratio_test) ; k++)
101 	{	if (ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) == 0)
102 		{	printf ("\n\nLine %d : SRC ratio %f should have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
103 			exit (1) ;
104 			} ;
105 		if (! ratio_test [k].should_pass && src_is_valid_ratio (ratio_test [k].ratio) != 0)
106 		{	printf ("\n\nLine %d : SRC ratio %f should not have passed.\n\n", __LINE__, ratio_test [k].ratio) ;
107 			exit (1) ;
108 			} ;
109 		printf ("\t SRC ratio (%9.5f) : %s ................... ok\n", ratio_test [k].ratio,
110 			(ratio_test [k].should_pass ? "pass" : "fail")) ;
111 		} ;
112 
113 	puts ("") ;
114 
115 	return ;
116 } /* src_ratio_test */
117 
118 static void
error_test(void)119 error_test (void)
120 {	const char *errorstr ;
121 	int		k, errors = 0 ;
122 
123 	puts ("    error_test :") ;
124 
125 	for (k = 0 ; 1 ; k++)
126 	{	errorstr = src_strerror (k) ;
127 		printf ("\t%-2d : %s\n", k, errorstr) ;
128 		if (errorstr == NULL)
129 		{	errors ++ ;
130 			continue ;
131 			} ;
132 		if (strstr (errorstr, "Placeholder.") == errorstr)
133 			break ;
134 		} ;
135 
136 	if (errors != 0)
137 	{	printf ("\n\nLine %d : Missing error numbers above.\n\n", __LINE__) ;
138 		exit (1) ;
139 		} ;
140 
141 	puts ("") ;
142 
143 	return ;
144 } /* error_test */
145 
146 static void
zero_input_test(int converter)147 zero_input_test (int converter)
148 {	SRC_DATA data ;
149 	SRC_STATE *state ;
150 	float out [100] ;
151 	int error ;
152 
153 	printf ("    %s (%-26s) ........ ", __func__, src_get_name (converter)) ;
154 	fflush (stdout) ;
155 
156 	if ((state = src_new (converter, 1, &error)) == NULL)
157 	{	printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
158 		exit (1) ;
159 		} ;
160 
161 	data.data_in = (float *) (size_t) 0xdeadbeef ;
162 	data.input_frames = 0 ;
163 	data.data_out = out ;
164 	data.output_frames = ARRAY_LEN (out) ;
165 	data.end_of_input = 0 ;
166 	data.src_ratio = 1.0 ;
167 
168 	if ((error = src_process (state, &data)))
169 	{	printf ("\n\nLine %d : src_new failed : %s.\n\n", __LINE__, src_strerror (error)) ;
170 		exit (1) ;
171 		} ;
172 
173 	state = src_delete (state) ;
174 
175 	puts ("ok") ;
176 } /* zero_input_test */
177 
get_channels_test(int converter)178 static void get_channels_test(int converter)
179 {
180 	SRC_STATE *state;
181 	int channels_in, channels_out;
182 	const char *errorstr;
183 
184 	state = NULL;
185 	if ((channels_out = src_get_channels(state)) >= 0)
186 	{
187 		printf ("\n\nLine %d : Return value should be negative, was : %d.\n\n", __LINE__, channels_out) ;
188 		exit (1) ;
189 	}
190 	errorstr = src_strerror(-channels_out);
191 	if (!strstr(errorstr, "NULL"))
192 	{
193 		printf ("\n\nLine %d : Inverted output should be valid error code mentioning a NULL pointer, was : %s.\n\n", __LINE__, errorstr) ;
194 		exit (1) ;
195 	}
196 
197 	for (channels_in = 1; channels_in <= 8; channels_in++)
198 	{
199 		int error;
200 		state = src_new(converter, channels_in, &error);
201 		if ((channels_out = src_get_channels(state)) != channels_in)
202 		{
203 			printf ("\n\nLine %d : Return value should be %d, was : %d.\n\n", __LINE__, channels_in, channels_out) ;
204 			exit (1) ;
205 		}
206 		state = src_delete(state);
207 	}
208 }
209