Chris@0
|
1 /*
|
Chris@0
|
2 ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
|
Chris@0
|
3 **
|
Chris@0
|
4 ** This program is free software; you can redistribute it and/or modify
|
Chris@0
|
5 ** it under the terms of the GNU General Public License as published by
|
Chris@0
|
6 ** the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
7 ** (at your option) any later version.
|
Chris@0
|
8 **
|
Chris@0
|
9 ** This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 ** GNU General Public License for more details.
|
Chris@0
|
13 **
|
Chris@0
|
14 ** You should have received a copy of the GNU General Public License
|
Chris@0
|
15 ** along with this program; if not, write to the Free Software
|
Chris@0
|
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
Chris@0
|
17 */
|
Chris@0
|
18
|
Chris@0
|
19 #include "sfconfig.h"
|
Chris@0
|
20
|
Chris@0
|
21 #include <stdio.h>
|
Chris@0
|
22 #include <stdlib.h>
|
Chris@0
|
23
|
Chris@0
|
24 #if HAVE_UNISTD_H
|
Chris@0
|
25 #include <unistd.h>
|
Chris@0
|
26 #endif
|
Chris@0
|
27
|
Chris@0
|
28 #if (HAVE_DECL_S_IRGRP == 0)
|
Chris@0
|
29 #include <sf_unistd.h>
|
Chris@0
|
30 #endif
|
Chris@0
|
31
|
Chris@0
|
32 #include <string.h>
|
Chris@0
|
33 #include <math.h>
|
Chris@0
|
34 #include <time.h>
|
Chris@0
|
35 #include <fcntl.h>
|
Chris@0
|
36 #include <sys/stat.h>
|
Chris@0
|
37
|
Chris@0
|
38 #include <sndfile.h>
|
Chris@0
|
39
|
Chris@0
|
40 #ifndef M_PI
|
Chris@0
|
41 #define M_PI 3.14159265358979323846264338
|
Chris@0
|
42 #endif
|
Chris@0
|
43
|
Chris@0
|
44 /*
|
Chris@0
|
45 ** Neat solution to the Win32/OS2 binary file flage requirement.
|
Chris@0
|
46 ** If O_BINARY isn't already defined by the inclusion of the system
|
Chris@0
|
47 ** headers, set it to zero.
|
Chris@0
|
48 */
|
Chris@0
|
49 #ifndef O_BINARY
|
Chris@0
|
50 #define O_BINARY 0
|
Chris@0
|
51 #endif
|
Chris@0
|
52
|
Chris@0
|
53 #define WRITE_FLAGS (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY)
|
Chris@0
|
54 #define READ_FLAGS (O_RDONLY | O_BINARY)
|
Chris@0
|
55
|
Chris@0
|
56 #if (defined (WIN32) || defined (_WIN32) || defined (__OS2__))
|
Chris@0
|
57 #define WRITE_PERMS 0777
|
Chris@0
|
58 #else
|
Chris@0
|
59 #define WRITE_PERMS (S_IRUSR | S_IWUSR | S_IRGRP)
|
Chris@0
|
60 #endif
|
Chris@0
|
61
|
Chris@0
|
62 #define BUFFER_SIZE (1<<18)
|
Chris@0
|
63 #define BLOCK_COUNT (30)
|
Chris@0
|
64 #define TEST_DURATION (5) /* 5 Seconds. */
|
Chris@0
|
65
|
Chris@0
|
66 typedef struct
|
Chris@0
|
67 { double write_rate ;
|
Chris@0
|
68 double read_rate ;
|
Chris@0
|
69 } PERF_STATS ;
|
Chris@0
|
70
|
Chris@0
|
71 static void *data = NULL ;
|
Chris@0
|
72
|
Chris@0
|
73 static void calc_raw_performance (PERF_STATS *stats) ;
|
Chris@0
|
74
|
Chris@0
|
75 static void calc_short_performance (int format, double read_rate, double write_rate) ;
|
Chris@0
|
76 static void calc_int_performance (int format, double read_rate, double write_rate) ;
|
Chris@0
|
77 static void calc_float_performance (int format, double read_rate, double write_rate) ;
|
Chris@0
|
78
|
Chris@0
|
79
|
Chris@0
|
80 static int cpu_is_big_endian (void) ;
|
Chris@0
|
81
|
Chris@0
|
82 static const char* get_subtype_str (int subtype) ;
|
Chris@0
|
83
|
Chris@0
|
84 int
|
Chris@0
|
85 main (int argc, char *argv [])
|
Chris@0
|
86 { PERF_STATS stats ;
|
Chris@0
|
87 char buffer [256] = "Benchmarking " ;
|
Chris@0
|
88 int format_major ;
|
Chris@0
|
89
|
Chris@0
|
90 if (! (data = malloc (BUFFER_SIZE * sizeof (double))))
|
Chris@0
|
91 { perror ("Error : malloc failed") ;
|
Chris@0
|
92 exit (1) ;
|
Chris@0
|
93 } ;
|
Chris@0
|
94
|
Chris@0
|
95 sf_command (NULL, SFC_GET_LIB_VERSION, buffer + strlen (buffer), sizeof (buffer) - strlen (buffer)) ;
|
Chris@0
|
96
|
Chris@0
|
97 puts (buffer) ;
|
Chris@0
|
98 memset (buffer, '-', strlen (buffer)) ;
|
Chris@0
|
99 puts (buffer) ;
|
Chris@0
|
100 printf ("Each test takes a little over %d seconds.\n\n", TEST_DURATION) ;
|
Chris@0
|
101
|
Chris@0
|
102 calc_raw_performance (&stats) ;
|
Chris@0
|
103
|
Chris@0
|
104 if (argc < 2 || strcmp ("--native-only", argv [1]) == 0)
|
Chris@0
|
105 { puts ("\nNative endian I/O :") ;
|
Chris@0
|
106 format_major = cpu_is_big_endian () ? SF_FORMAT_AIFF : SF_FORMAT_WAV ;
|
Chris@0
|
107
|
Chris@0
|
108 calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
109 calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
110 calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
111 calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
112 calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
113 calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
114 calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
|
Chris@0
|
115 } ;
|
Chris@0
|
116
|
Chris@0
|
117 if (argc < 2 || strcmp ("--swap-only", argv [1]) == 0)
|
Chris@0
|
118 { puts ("\nEndian swapped I/O :") ;
|
Chris@0
|
119 format_major = cpu_is_big_endian () ? SF_FORMAT_WAV : SF_FORMAT_AIFF ;
|
Chris@0
|
120
|
Chris@0
|
121 calc_short_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
122 calc_int_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
123 calc_int_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
124 calc_float_performance (format_major | SF_FORMAT_PCM_16, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
125 calc_float_performance (format_major | SF_FORMAT_PCM_24, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
126 calc_float_performance (format_major | SF_FORMAT_PCM_32, stats.read_rate, stats.write_rate) ;
|
Chris@0
|
127 calc_float_performance (format_major | SF_FORMAT_FLOAT , stats.read_rate, stats.write_rate) ;
|
Chris@0
|
128 } ;
|
Chris@0
|
129
|
Chris@0
|
130 puts ("") ;
|
Chris@0
|
131
|
Chris@0
|
132 free (data) ;
|
Chris@0
|
133
|
Chris@0
|
134 return 0 ;
|
Chris@0
|
135 } /* main */
|
Chris@0
|
136
|
Chris@0
|
137 /*==============================================================================
|
Chris@0
|
138 */
|
Chris@0
|
139
|
Chris@0
|
140 static void
|
Chris@0
|
141 calc_raw_performance (PERF_STATS *stats)
|
Chris@0
|
142 { clock_t start_clock, clock_time ;
|
Chris@0
|
143 int fd, k, byte_count, retval, op_count ;
|
Chris@0
|
144 const char *filename ;
|
Chris@0
|
145
|
Chris@0
|
146 filename = "benchmark.dat" ;
|
Chris@0
|
147
|
Chris@0
|
148 byte_count = BUFFER_SIZE * sizeof (short) ;
|
Chris@0
|
149
|
Chris@0
|
150 /* Collect write stats */
|
Chris@0
|
151 printf (" Raw write PCM_16 : ") ;
|
Chris@0
|
152 fflush (stdout) ;
|
Chris@0
|
153
|
Chris@0
|
154 clock_time = 0 ;
|
Chris@0
|
155 op_count = 0 ;
|
Chris@0
|
156 start_clock = clock () ;
|
Chris@0
|
157
|
Chris@0
|
158 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
159 { if ((fd = open (filename, WRITE_FLAGS, WRITE_PERMS)) < 0)
|
Chris@0
|
160 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
161 perror ("") ;
|
Chris@0
|
162 exit (1) ;
|
Chris@0
|
163 } ;
|
Chris@0
|
164
|
Chris@0
|
165 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
166 { if ((retval = write (fd, data, byte_count)) != byte_count)
|
Chris@0
|
167 { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
|
Chris@0
|
168 exit (1) ;
|
Chris@0
|
169 } ;
|
Chris@0
|
170 } ;
|
Chris@0
|
171
|
Chris@0
|
172 close (fd) ;
|
Chris@0
|
173
|
Chris@0
|
174 clock_time = clock () - start_clock ;
|
Chris@0
|
175 op_count ++ ;
|
Chris@0
|
176 } ;
|
Chris@0
|
177
|
Chris@0
|
178 stats->write_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
Chris@0
|
179 stats->write_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
180 printf ("%10.0f samples per sec\n", stats->write_rate) ;
|
Chris@0
|
181
|
Chris@0
|
182 /* Collect read stats */
|
Chris@0
|
183 printf (" Raw read PCM_16 : ") ;
|
Chris@0
|
184 fflush (stdout) ;
|
Chris@0
|
185
|
Chris@0
|
186 clock_time = 0 ;
|
Chris@0
|
187 op_count = 0 ;
|
Chris@0
|
188 start_clock = clock () ;
|
Chris@0
|
189
|
Chris@0
|
190 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
191 { if ((fd = open (filename, READ_FLAGS)) < 0)
|
Chris@0
|
192 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
193 perror ("") ;
|
Chris@0
|
194 exit (1) ;
|
Chris@0
|
195 } ;
|
Chris@0
|
196
|
Chris@0
|
197 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
198 { if ((retval = read (fd, data, byte_count)) != byte_count)
|
Chris@0
|
199 { printf ("Error : write returned %d (should have been %d)\n", retval, byte_count) ;
|
Chris@0
|
200 exit (1) ;
|
Chris@0
|
201 } ;
|
Chris@0
|
202 } ;
|
Chris@0
|
203
|
Chris@0
|
204 close (fd) ;
|
Chris@0
|
205
|
Chris@0
|
206 clock_time = clock () - start_clock ;
|
Chris@0
|
207 op_count ++ ;
|
Chris@0
|
208 } ;
|
Chris@0
|
209
|
Chris@0
|
210 stats->read_rate = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
Chris@0
|
211 stats->read_rate *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
212 printf ("%10.0f samples per sec\n", stats->read_rate) ;
|
Chris@0
|
213
|
Chris@0
|
214 unlink (filename) ;
|
Chris@0
|
215 } /* calc_raw_performance */
|
Chris@0
|
216
|
Chris@0
|
217 /*------------------------------------------------------------------------------
|
Chris@0
|
218 */
|
Chris@0
|
219
|
Chris@0
|
220 static void
|
Chris@0
|
221 calc_short_performance (int format, double read_rate, double write_rate)
|
Chris@0
|
222 { SNDFILE *file ;
|
Chris@0
|
223 SF_INFO sfinfo ;
|
Chris@0
|
224 clock_t start_clock, clock_time ;
|
Chris@0
|
225 double performance ;
|
Chris@0
|
226 int k, item_count, retval, op_count ;
|
Chris@0
|
227 const char* subtype ;
|
Chris@0
|
228 short *short_data ;
|
Chris@0
|
229 const char *filename ;
|
Chris@0
|
230
|
Chris@0
|
231 filename = "benchmark.dat" ;
|
Chris@0
|
232 subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
|
Chris@0
|
233
|
Chris@0
|
234 short_data = data ;
|
Chris@0
|
235 item_count = BUFFER_SIZE ;
|
Chris@0
|
236 for (k = 0 ; k < item_count ; k++)
|
Chris@0
|
237 short_data [k] = 32700.0 * sin (2 * M_PI * k / 32000.0) ;
|
Chris@0
|
238
|
Chris@0
|
239 /* Collect write stats */
|
Chris@0
|
240 printf (" Write %-5s to %s : ", "short", subtype) ;
|
Chris@0
|
241 fflush (stdout) ;
|
Chris@0
|
242
|
Chris@0
|
243 sfinfo.channels = 1 ;
|
Chris@0
|
244 sfinfo.format = format ;
|
Chris@0
|
245 sfinfo.frames = 1 ;
|
Chris@0
|
246 sfinfo.samplerate = 32000 ;
|
Chris@0
|
247
|
Chris@0
|
248 clock_time = 0 ;
|
Chris@0
|
249 op_count = 0 ;
|
Chris@0
|
250 start_clock = clock () ;
|
Chris@0
|
251
|
Chris@0
|
252 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
253 { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
|
Chris@0
|
254 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
255 perror ("") ;
|
Chris@0
|
256 exit (1) ;
|
Chris@0
|
257 } ;
|
Chris@0
|
258
|
Chris@0
|
259 /* Turn off the addition of a PEAK chunk. */
|
Chris@0
|
260 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
|
Chris@0
|
261
|
Chris@0
|
262 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
263 { if ((retval = sf_write_short (file, short_data, item_count)) != item_count)
|
Chris@0
|
264 { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
|
Chris@0
|
265 exit (1) ;
|
Chris@0
|
266 } ;
|
Chris@0
|
267 } ;
|
Chris@0
|
268
|
Chris@0
|
269 sf_close (file) ;
|
Chris@0
|
270
|
Chris@0
|
271 clock_time = clock () - start_clock ;
|
Chris@0
|
272 op_count ++ ;
|
Chris@0
|
273 } ;
|
Chris@0
|
274
|
Chris@0
|
275 performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
Chris@0
|
276 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
277 printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;
|
Chris@0
|
278
|
Chris@0
|
279 /* Collect read stats */
|
Chris@0
|
280 printf (" Read %-5s from %s : ", "short", subtype) ;
|
Chris@0
|
281 fflush (stdout) ;
|
Chris@0
|
282
|
Chris@0
|
283 clock_time = 0 ;
|
Chris@0
|
284 op_count = 0 ;
|
Chris@0
|
285 start_clock = clock () ;
|
Chris@0
|
286
|
Chris@0
|
287 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
288 { if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
|
Chris@0
|
289 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
290 perror ("") ;
|
Chris@0
|
291 exit (1) ;
|
Chris@0
|
292 } ;
|
Chris@0
|
293
|
Chris@0
|
294 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
295 { if ((retval = sf_read_short (file, short_data, item_count)) != item_count)
|
Chris@0
|
296 { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
|
Chris@0
|
297 exit (1) ;
|
Chris@0
|
298 } ;
|
Chris@0
|
299 } ;
|
Chris@0
|
300
|
Chris@0
|
301 sf_close (file) ;
|
Chris@0
|
302
|
Chris@0
|
303 clock_time = clock () - start_clock ;
|
Chris@0
|
304 op_count ++ ;
|
Chris@0
|
305 } ;
|
Chris@0
|
306
|
Chris@0
|
307 performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
|
Chris@0
|
308 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
309 printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;
|
Chris@0
|
310
|
Chris@0
|
311 unlink (filename) ;
|
Chris@0
|
312
|
Chris@0
|
313 } /* calc_short_performance */
|
Chris@0
|
314 static void
|
Chris@0
|
315 calc_int_performance (int format, double read_rate, double write_rate)
|
Chris@0
|
316 { SNDFILE *file ;
|
Chris@0
|
317 SF_INFO sfinfo ;
|
Chris@0
|
318 clock_t start_clock, clock_time ;
|
Chris@0
|
319 double performance ;
|
Chris@0
|
320 int k, item_count, retval, op_count ;
|
Chris@0
|
321 const char* subtype ;
|
Chris@0
|
322 int *int_data ;
|
Chris@0
|
323 const char *filename ;
|
Chris@0
|
324
|
Chris@0
|
325 filename = "benchmark.dat" ;
|
Chris@0
|
326 subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
|
Chris@0
|
327
|
Chris@0
|
328 int_data = data ;
|
Chris@0
|
329 item_count = BUFFER_SIZE ;
|
Chris@0
|
330 for (k = 0 ; k < item_count ; k++)
|
Chris@0
|
331 int_data [k] = 32700.0 * (1<<16) * sin (2 * M_PI * k / 32000.0) ;
|
Chris@0
|
332
|
Chris@0
|
333 /* Collect write stats */
|
Chris@0
|
334 printf (" Write %-5s to %s : ", "int", subtype) ;
|
Chris@0
|
335 fflush (stdout) ;
|
Chris@0
|
336
|
Chris@0
|
337 sfinfo.channels = 1 ;
|
Chris@0
|
338 sfinfo.format = format ;
|
Chris@0
|
339 sfinfo.frames = 1 ;
|
Chris@0
|
340 sfinfo.samplerate = 32000 ;
|
Chris@0
|
341
|
Chris@0
|
342 clock_time = 0 ;
|
Chris@0
|
343 op_count = 0 ;
|
Chris@0
|
344 start_clock = clock () ;
|
Chris@0
|
345
|
Chris@0
|
346 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
347 { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
|
Chris@0
|
348 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
349 perror ("") ;
|
Chris@0
|
350 exit (1) ;
|
Chris@0
|
351 } ;
|
Chris@0
|
352
|
Chris@0
|
353 /* Turn off the addition of a PEAK chunk. */
|
Chris@0
|
354 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
|
Chris@0
|
355
|
Chris@0
|
356 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
357 { if ((retval = sf_write_int (file, int_data, item_count)) != item_count)
|
Chris@0
|
358 { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
|
Chris@0
|
359 exit (1) ;
|
Chris@0
|
360 } ;
|
Chris@0
|
361 } ;
|
Chris@0
|
362
|
Chris@0
|
363 sf_close (file) ;
|
Chris@0
|
364
|
Chris@0
|
365 clock_time = clock () - start_clock ;
|
Chris@0
|
366 op_count ++ ;
|
Chris@0
|
367 } ;
|
Chris@0
|
368
|
Chris@0
|
369 performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
Chris@0
|
370 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
371 printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;
|
Chris@0
|
372
|
Chris@0
|
373 /* Collect read stats */
|
Chris@0
|
374 printf (" Read %-5s from %s : ", "int", subtype) ;
|
Chris@0
|
375 fflush (stdout) ;
|
Chris@0
|
376
|
Chris@0
|
377 clock_time = 0 ;
|
Chris@0
|
378 op_count = 0 ;
|
Chris@0
|
379 start_clock = clock () ;
|
Chris@0
|
380
|
Chris@0
|
381 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
382 { if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
|
Chris@0
|
383 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
384 perror ("") ;
|
Chris@0
|
385 exit (1) ;
|
Chris@0
|
386 } ;
|
Chris@0
|
387
|
Chris@0
|
388 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
389 { if ((retval = sf_read_int (file, int_data, item_count)) != item_count)
|
Chris@0
|
390 { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
|
Chris@0
|
391 exit (1) ;
|
Chris@0
|
392 } ;
|
Chris@0
|
393 } ;
|
Chris@0
|
394
|
Chris@0
|
395 sf_close (file) ;
|
Chris@0
|
396
|
Chris@0
|
397 clock_time = clock () - start_clock ;
|
Chris@0
|
398 op_count ++ ;
|
Chris@0
|
399 } ;
|
Chris@0
|
400
|
Chris@0
|
401 performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
|
Chris@0
|
402 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
403 printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;
|
Chris@0
|
404
|
Chris@0
|
405 unlink (filename) ;
|
Chris@0
|
406
|
Chris@0
|
407 } /* calc_int_performance */
|
Chris@0
|
408 static void
|
Chris@0
|
409 calc_float_performance (int format, double read_rate, double write_rate)
|
Chris@0
|
410 { SNDFILE *file ;
|
Chris@0
|
411 SF_INFO sfinfo ;
|
Chris@0
|
412 clock_t start_clock, clock_time ;
|
Chris@0
|
413 double performance ;
|
Chris@0
|
414 int k, item_count, retval, op_count ;
|
Chris@0
|
415 const char* subtype ;
|
Chris@0
|
416 float *float_data ;
|
Chris@0
|
417 const char *filename ;
|
Chris@0
|
418
|
Chris@0
|
419 filename = "benchmark.dat" ;
|
Chris@0
|
420 subtype = get_subtype_str (format & SF_FORMAT_SUBMASK) ;
|
Chris@0
|
421
|
Chris@0
|
422 float_data = data ;
|
Chris@0
|
423 item_count = BUFFER_SIZE ;
|
Chris@0
|
424 for (k = 0 ; k < item_count ; k++)
|
Chris@0
|
425 float_data [k] = 1.0 * sin (2 * M_PI * k / 32000.0) ;
|
Chris@0
|
426
|
Chris@0
|
427 /* Collect write stats */
|
Chris@0
|
428 printf (" Write %-5s to %s : ", "float", subtype) ;
|
Chris@0
|
429 fflush (stdout) ;
|
Chris@0
|
430
|
Chris@0
|
431 sfinfo.channels = 1 ;
|
Chris@0
|
432 sfinfo.format = format ;
|
Chris@0
|
433 sfinfo.frames = 1 ;
|
Chris@0
|
434 sfinfo.samplerate = 32000 ;
|
Chris@0
|
435
|
Chris@0
|
436 clock_time = 0 ;
|
Chris@0
|
437 op_count = 0 ;
|
Chris@0
|
438 start_clock = clock () ;
|
Chris@0
|
439
|
Chris@0
|
440 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
441 { if (! (file = sf_open (filename, SFM_WRITE, &sfinfo)))
|
Chris@0
|
442 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
443 perror ("") ;
|
Chris@0
|
444 exit (1) ;
|
Chris@0
|
445 } ;
|
Chris@0
|
446
|
Chris@0
|
447 /* Turn off the addition of a PEAK chunk. */
|
Chris@0
|
448 sf_command (file, SFC_SET_ADD_PEAK_CHUNK, NULL, SF_FALSE) ;
|
Chris@0
|
449
|
Chris@0
|
450 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
451 { if ((retval = sf_write_float (file, float_data, item_count)) != item_count)
|
Chris@0
|
452 { printf ("Error : sf_write_short returned %d (should have been %d)\n", retval, item_count) ;
|
Chris@0
|
453 exit (1) ;
|
Chris@0
|
454 } ;
|
Chris@0
|
455 } ;
|
Chris@0
|
456
|
Chris@0
|
457 sf_close (file) ;
|
Chris@0
|
458
|
Chris@0
|
459 clock_time = clock () - start_clock ;
|
Chris@0
|
460 op_count ++ ;
|
Chris@0
|
461 } ;
|
Chris@0
|
462
|
Chris@0
|
463 performance = (1.0 * BUFFER_SIZE) * BLOCK_COUNT * op_count ;
|
Chris@0
|
464 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
465 printf ("%6.2f%% of raw write\n", 100.0 * performance / write_rate) ;
|
Chris@0
|
466
|
Chris@0
|
467 /* Collect read stats */
|
Chris@0
|
468 printf (" Read %-5s from %s : ", "float", subtype) ;
|
Chris@0
|
469 fflush (stdout) ;
|
Chris@0
|
470
|
Chris@0
|
471 clock_time = 0 ;
|
Chris@0
|
472 op_count = 0 ;
|
Chris@0
|
473 start_clock = clock () ;
|
Chris@0
|
474
|
Chris@0
|
475 while (clock_time < (CLOCKS_PER_SEC * TEST_DURATION))
|
Chris@0
|
476 { if (! (file = sf_open (filename, SFM_READ, &sfinfo)))
|
Chris@0
|
477 { printf ("Error : not able to open file : %s\n", filename) ;
|
Chris@0
|
478 perror ("") ;
|
Chris@0
|
479 exit (1) ;
|
Chris@0
|
480 } ;
|
Chris@0
|
481
|
Chris@0
|
482 for (k = 0 ; k < BLOCK_COUNT ; k++)
|
Chris@0
|
483 { if ((retval = sf_read_float (file, float_data, item_count)) != item_count)
|
Chris@0
|
484 { printf ("Error : write returned %d (should have been %d)\n", retval, item_count) ;
|
Chris@0
|
485 exit (1) ;
|
Chris@0
|
486 } ;
|
Chris@0
|
487 } ;
|
Chris@0
|
488
|
Chris@0
|
489 sf_close (file) ;
|
Chris@0
|
490
|
Chris@0
|
491 clock_time = clock () - start_clock ;
|
Chris@0
|
492 op_count ++ ;
|
Chris@0
|
493 } ;
|
Chris@0
|
494
|
Chris@0
|
495 performance = (1.0 * item_count) * BLOCK_COUNT * op_count ;
|
Chris@0
|
496 performance *= (1.0 * CLOCKS_PER_SEC) / clock_time ;
|
Chris@0
|
497 printf ("%6.2f%% of raw read\n", 100.0 * performance / read_rate) ;
|
Chris@0
|
498
|
Chris@0
|
499 unlink (filename) ;
|
Chris@0
|
500
|
Chris@0
|
501 } /* calc_float_performance */
|
Chris@0
|
502
|
Chris@0
|
503
|
Chris@0
|
504 /*==============================================================================
|
Chris@0
|
505 */
|
Chris@0
|
506
|
Chris@0
|
507 static int
|
Chris@0
|
508 cpu_is_big_endian (void)
|
Chris@0
|
509 { unsigned char *cptr ;
|
Chris@0
|
510 int endtest ;
|
Chris@0
|
511
|
Chris@0
|
512 endtest = 0x12345678 ;
|
Chris@0
|
513
|
Chris@0
|
514 cptr = (unsigned char*) (&endtest) ;
|
Chris@0
|
515
|
Chris@0
|
516 if (cptr [0] == 0x12 && cptr [1] == 0x34 && cptr [3] == 0x78)
|
Chris@0
|
517 return SF_TRUE ;
|
Chris@0
|
518
|
Chris@0
|
519 return SF_FALSE ;
|
Chris@0
|
520 } /* cpu_is_big_endian */
|
Chris@0
|
521
|
Chris@0
|
522 static const char*
|
Chris@0
|
523 get_subtype_str (int subtype)
|
Chris@0
|
524 { switch (subtype)
|
Chris@0
|
525 { case SF_FORMAT_PCM_16 :
|
Chris@0
|
526 return "PCM_16" ;
|
Chris@0
|
527
|
Chris@0
|
528 case SF_FORMAT_PCM_24 :
|
Chris@0
|
529 return "PCM_24" ;
|
Chris@0
|
530
|
Chris@0
|
531 case SF_FORMAT_PCM_32 :
|
Chris@0
|
532 return "PCM_32" ;
|
Chris@0
|
533
|
Chris@0
|
534 case SF_FORMAT_FLOAT :
|
Chris@0
|
535 return "FLOAT " ;
|
Chris@0
|
536
|
Chris@0
|
537 case SF_FORMAT_DOUBLE :
|
Chris@0
|
538 return "DOUBLE" ;
|
Chris@0
|
539
|
Chris@0
|
540 default : break ;
|
Chris@0
|
541 } ;
|
Chris@0
|
542
|
Chris@0
|
543 return "UNKNOWN" ;
|
Chris@0
|
544 } /* get_subtype_str */
|
Chris@0
|
545
|