]>
Commit | Line | Data |
---|---|---|
8529da43 MG |
1 | /* |
2 | ** Copyright (c) 2008-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/erikd/libsamplerate/blob/master/COPYING | |
7 | */ | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
12 | #include <time.h> | |
13 | #include <unistd.h> | |
14 | ||
15 | #include <samplerate.h> | |
16 | ||
17 | #include "config.h" | |
18 | ||
19 | #include "util.h" | |
20 | #include "float_cast.h" | |
21 | ||
22 | #define BUFFER_LEN (1<<17) | |
23 | ||
24 | static float input [BUFFER_LEN] ; | |
25 | static float output [BUFFER_LEN] ; | |
26 | ||
27 | static long | |
28 | throughput_test (int converter, int channels, long best_throughput) | |
29 | { SRC_DATA src_data ; | |
30 | clock_t start_time, clock_time ; | |
31 | double duration ; | |
32 | long total_frames = 0, throughput ; | |
33 | int error ; | |
34 | ||
35 | printf (" %-30s %2d ", src_get_name (converter), channels) ; | |
36 | fflush (stdout) ; | |
37 | ||
38 | src_data.data_in = input ; | |
39 | src_data.input_frames = ARRAY_LEN (input) / channels ; | |
40 | ||
41 | src_data.data_out = output ; | |
42 | src_data.output_frames = ARRAY_LEN (output) / channels ; | |
43 | ||
44 | src_data.src_ratio = 0.99 ; | |
45 | ||
46 | sleep (2) ; | |
47 | ||
48 | start_time = clock () ; | |
49 | ||
50 | do | |
51 | { | |
52 | if ((error = src_simple (&src_data, converter, channels)) != 0) | |
53 | { puts (src_strerror (error)) ; | |
54 | exit (1) ; | |
55 | } ; | |
56 | ||
57 | total_frames += src_data.output_frames_gen ; | |
58 | ||
59 | clock_time = clock () - start_time ; | |
60 | duration = (1.0 * clock_time) / CLOCKS_PER_SEC ; | |
61 | } | |
62 | while (duration < 5.0) ; | |
63 | ||
64 | if (src_data.input_frames_used != src_data.input_frames) | |
65 | { printf ("\n\nLine %d : input frames used %ld should be %ld\n", __LINE__, src_data.input_frames_used, src_data.input_frames) ; | |
66 | exit (1) ; | |
67 | } ; | |
68 | ||
69 | if (fabs (src_data.src_ratio * src_data.input_frames_used - src_data.output_frames_gen) > 2) | |
70 | { printf ("\n\nLine %d : input / output length mismatch.\n\n", __LINE__) ; | |
71 | printf (" input len : %d\n", ARRAY_LEN (input) / channels) ; | |
72 | printf (" output len : %ld (should be %g +/- 2)\n\n", src_data.output_frames_gen, | |
73 | floor (0.5 + src_data.src_ratio * src_data.input_frames_used)) ; | |
74 | exit (1) ; | |
75 | } ; | |
76 | ||
77 | throughput = lrint (floor (total_frames / duration)) ; | |
78 | ||
79 | if (best_throughput == 0) | |
80 | { best_throughput = MAX (throughput, best_throughput) ; | |
81 | printf ("%5.2f %10ld\n", duration, throughput) ; | |
82 | } | |
83 | else | |
84 | { best_throughput = MAX (throughput, best_throughput) ; | |
85 | printf ("%5.2f %10ld %10ld\n", duration, throughput, best_throughput) ; | |
86 | } | |
87 | ||
88 | return best_throughput ; | |
89 | } /* throughput_test */ | |
90 | ||
91 | static void | |
92 | single_run (void) | |
93 | { const int max_channels = 10 ; | |
94 | int k ; | |
95 | ||
96 | printf ("\n CPU name : %s\n", get_cpu_name ()) ; | |
97 | ||
98 | puts ( | |
99 | "\n" | |
100 | " Converter Channels Duration Throughput\n" | |
101 | " ---------------------------------------------------------------------" | |
102 | ) ; | |
103 | ||
104 | for (k = 1 ; k <= max_channels / 2 ; k++) | |
105 | throughput_test (SRC_SINC_FASTEST, k, 0) ; | |
106 | ||
107 | puts ("") ; | |
108 | for (k = 1 ; k <= max_channels / 2 ; k++) | |
109 | throughput_test (SRC_SINC_MEDIUM_QUALITY, k, 0) ; | |
110 | ||
111 | puts ("") ; | |
112 | for (k = 1 ; k <= max_channels ; k++) | |
113 | throughput_test (SRC_SINC_BEST_QUALITY, k, 0) ; | |
114 | ||
115 | puts ("") ; | |
116 | return ; | |
117 | } /* single_run */ | |
118 | ||
119 | static void | |
120 | multi_run (int run_count) | |
121 | { int k, ch ; | |
122 | ||
123 | printf ("\n CPU name : %s\n", get_cpu_name ()) ; | |
124 | ||
125 | puts ( | |
126 | "\n" | |
127 | " Converter Channels Duration Throughput Best Throughput\n" | |
128 | " ----------------------------------------------------------------------------------------" | |
129 | ) ; | |
130 | ||
131 | for (ch = 1 ; ch <= 5 ; ch++) | |
132 | { long sinc_fastest = 0, sinc_medium = 0, sinc_best = 0 ; | |
133 | ||
134 | for (k = 0 ; k < run_count ; k++) | |
135 | { sinc_fastest = throughput_test (SRC_SINC_FASTEST, ch, sinc_fastest) ; | |
136 | sinc_medium = throughput_test (SRC_SINC_MEDIUM_QUALITY, ch, sinc_medium) ; | |
137 | sinc_best = throughput_test (SRC_SINC_BEST_QUALITY, ch, sinc_best) ; | |
138 | ||
139 | puts ("") ; | |
140 | ||
141 | /* Let the CPU cool down. We might be running on a laptop. */ | |
142 | sleep (10) ; | |
143 | } ; | |
144 | ||
145 | puts ( | |
146 | "\n" | |
147 | " Converter Best Throughput\n" | |
148 | " ------------------------------------------------" | |
149 | ) ; | |
150 | ||
151 | printf (" %-30s %10ld\n", src_get_name (SRC_SINC_FASTEST), sinc_fastest) ; | |
152 | printf (" %-30s %10ld\n", src_get_name (SRC_SINC_MEDIUM_QUALITY), sinc_medium) ; | |
153 | printf (" %-30s %10ld\n", src_get_name (SRC_SINC_BEST_QUALITY), sinc_best) ; | |
154 | } ; | |
155 | ||
156 | puts ("") ; | |
157 | } /* multi_run */ | |
158 | ||
159 | static void | |
160 | usage_exit (const char * argv0) | |
161 | { const char * cptr ; | |
162 | ||
163 | if ((cptr = strrchr (argv0, '/')) != NULL) | |
164 | argv0 = cptr ; | |
165 | ||
166 | printf ( | |
167 | "Usage :\n" | |
168 | " %s - Single run of the throughput test.\n" | |
169 | " %s --best-of N - Do N runs of test a print bext result.\n" | |
170 | "\n", | |
171 | argv0, argv0) ; | |
172 | ||
173 | exit (0) ; | |
174 | } /* usage_exit */ | |
175 | ||
176 | int | |
177 | main (int argc, char ** argv) | |
178 | { double freq ; | |
179 | ||
180 | memset (input, 0, sizeof (input)) ; | |
181 | freq = 0.01 ; | |
182 | gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ; | |
183 | ||
184 | if (argc == 1) | |
185 | single_run () ; | |
186 | else if (argc == 3 && strcmp (argv [1], "--best-of") == 0) | |
187 | { int run_count = atoi (argv [2]) ; | |
188 | ||
189 | if (run_count < 1 || run_count > 20) | |
190 | { printf ("Please be sensible. Run count should be in range (1, 10].\n") ; | |
191 | exit (1) ; | |
192 | } ; | |
193 | ||
194 | multi_run (run_count) ; | |
195 | } | |
196 | else | |
197 | usage_exit (argv [0]) ; | |
198 | ||
199 | puts ( | |
200 | " Duration is in seconds.\n" | |
201 | " Throughput is in frames/sec (more is better).\n" | |
202 | ) ; | |
203 | ||
204 | return 0 ; | |
205 | } /* main */ | |
206 |