1 /* g++ -o sfrtest sfrtest.cc `pkg-config --cflags --libs sndfile` `pkg-config --cflags --libs glibmm-2.4` */
17 #include <glibmm/miscutils.h>
18 #include <glibmm/fileutils.h>
25 read_one (SNDFILE* sf, uint32_t nframes)
27 if (sf_read_float (sf, (float*) data, nframes) != nframes) {
41 cout << "sfrtest [ -n NFILES ] [ -b BLOCKSIZE ] [ -s ] [ -D ] filename-template" << endl;
45 main (int argc, char* argv[])
47 vector<SNDFILE*> sndfiles;
48 uint32_t sample_size = sizeof (float);
49 char optstring[] = "n:b:sD";
50 uint32_t block_size = 64 * 1024;
51 uint32_t nfiles = 100;
53 const struct option longopts[] = {
54 { "nfiles", 1, 0, 'n' },
55 { "blocksize", 1, 0, 'b' },
56 { "direct", 0, 0, 'D' },
62 char const * name_template = 0;
66 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
72 nfiles = atoi (optarg);
75 block_size = atoi (optarg);
87 name_template = argv[optind];
93 for (uint32_t n = 1; n <= nfiles; ++n) {
95 char path[PATH_MAX+1];
97 snprintf (path, sizeof (path), name_template, n);
99 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_REGULAR)) {
103 int flags = O_RDONLY;
104 int fd = open (path, flags, 0644);
107 cerr << "Could not open file #" << n << " @ " << path << " (" << strerror (errno) << ")" << endl;
113 /* Apple man pages say only that it returns "a value other than -1 on success",
114 which probably means zero, but you just can't be too careful with
117 if (fcntl (fd, F_NOCACHE, 1) == -1) {
118 cerr << "Cannot set F_NOCACHE on file # " << n << endl;
123 if ((sf = sf_open_fd (fd, SFM_READ, &format_info, true)) == 0) {
124 cerr << "Could not open SNDFILE #" << n << " @ " << path << " (" << sf_strerror (0) << ")" << endl;
128 samplerate = format_info.samplerate;
130 sndfiles.push_back (sf);
133 cout << "Discovered " << sndfiles.size() << " files using " << name_template << endl;
135 data = new float[block_size];
140 before = g_get_monotonic_time();
141 for (vector<SNDFILE*>::iterator s = sndfiles.begin(); s != sndfiles.end(); ++s) {
142 if (read_one (*s, block_size)) {
143 cerr << "Read failed for file #" << distance (sndfiles.begin(), s) << endl;
148 gint64 elapsed = g_get_monotonic_time() - before;
149 double bandwidth = ((sndfiles.size() * block_size * sample_size)/1048576.0) / (elapsed/1000000.0);
151 printf ("BW @ %Lu %.3f seconds bandwidth %.4f MB/sec\n", read, elapsed/1000000.0, bandwidth);