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