1 /* g++ -o sftest sftest.cc `pkg-config --cflags --libs sndfile` `pkg-config --cflags --libs glibmm-2.4` */
15 #include <glibmm/miscutils.h>
21 bool with_sync = false;
24 write_one (SNDFILE* sf, uint32_t nframes)
26 if (sf_write_float (sf, (float*) data, nframes) != nframes) {
40 cout << "sftest [ -f HEADER-FORMAT ] [ -F DATA-FORMAT ] [ -r SAMPLERATE ] [ -n NFILES ] [ -b BLOCKSIZE ] [ -s ]" << endl;
41 cout << "\tHEADER-FORMAT is one of:" << endl
45 cout << "\tDATA-FORMAT is one of:" << endl
46 << "\t\tFLOAT" << endl
53 main (int argc, char* argv[])
55 vector<SNDFILE*> sndfiles;
57 char optstring[] = "f:r:F:n:c:b:sD";
58 int channels, samplerate;
59 char const *suffix = ".wav";
60 char const *header_format = "wav";
61 char const *data_format = "float";
62 uint32_t block_size = 64 * 1024;
63 uint32_t nfiles = 100;
65 const struct option longopts[] = {
66 { "header-format", 1, 0, 'f' },
67 { "data-format", 1, 0, 'F' },
68 { "rate", 1, 0, 'r' },
69 { "nfiles", 1, 0, 'n' },
70 { "blocksize", 1, 0, 'b' },
71 { "channels", 1, 0, 'c' },
72 { "sync", 0, 0, 's' },
73 { "direct", 0, 0, 'D' },
81 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
87 header_format = optarg;
95 samplerate = atoi (optarg);
99 nfiles = atoi (optarg);
103 channels = atoi (optarg);
106 block_size = atoi (optarg);
120 /* setup file format */
121 memset (&format_info, 0, sizeof (format_info));
123 if (samplerate == 0 || nfiles == 0 || block_size == 0 || channels == 0) {
128 format_info.samplerate = samplerate;
129 format_info.channels = channels;
131 if (strcasecmp (header_format, "wav") == 0) {
132 format_info.format |= SF_FORMAT_WAV;
134 } else if (strcasecmp (header_format, "caf") == 0) {
135 format_info.format |= SF_FORMAT_CAF;
137 } else if (strcasecmp (header_format, "w64") == 0) {
138 format_info.format |= SF_FORMAT_W64;
145 if (strcasecmp (data_format, "float") == 0) {
146 format_info.format |= SF_FORMAT_FLOAT;
147 sample_size = sizeof (float);
148 } else if (strcasecmp (data_format, "32") == 0) {
149 format_info.format |= SF_FORMAT_PCM_32;
151 } else if (strcasecmp (data_format, "24") == 0) {
152 format_info.format |= SF_FORMAT_PCM_24;
154 } else if (strcasecmp (data_format, "16") == 0) {
155 format_info.format |= SF_FORMAT_PCM_16;
162 char tmpdirname[] = "sftest-XXXXXX";
163 g_mkdtemp (tmpdirname);
165 for (uint32_t n = 0; n < nfiles; ++n) {
174 path = Glib::build_filename (tmpdirname, ss.str());
176 int flags = O_RDWR|O_CREAT|O_TRUNC;
184 int fd = open (path.c_str(), flags, 0644);
187 cerr << "Could not open file #" << n << " @ " << path << endl;
193 /* Apple man pages say only that it returns "a value other than -1 on success",
194 which probably means zero, but you just can't be too careful with
197 if (fcntl (fd, F_NOCACHE, 1) == -1) {
198 cerr << "Cannot set F_NOCACHE on file # " << n << endl;
202 if ((sf = sf_open_fd (fd, SFM_RDWR, &format_info, true)) == 0) {
203 cerr << "Could not open file #" << n << " @ " << path << endl;
207 sndfiles.push_back (sf);
210 cout << nfiles << " files are in " << tmpdirname << " all used " << (direct ? "without" : "with") << " OS buffer cache" << endl;
211 cout << "Format is " << suffix << ' ' << channels << " channel" << (channels > 1 ? "s" : "") << " written in chunks of " << block_size << " frames, synced ? " << (with_sync ? "yes" : "no") << endl;
213 data = new float[block_size*channels];
214 uint64_t written = 0;
218 before = g_get_monotonic_time();
219 for (vector<SNDFILE*>::iterator s = sndfiles.begin(); s != sndfiles.end(); ++s) {
220 if (write_one (*s, block_size)) {
221 cerr << "Write failed for file #" << distance (sndfiles.begin(), s) << endl;
225 written += block_size;
226 gint64 elapsed = g_get_monotonic_time() - before;
227 double bandwidth = (sndfiles.size() * block_size * channels * sample_size) / (elapsed/1000000.0);
228 double data_minutes = written / (double) (60.0 * 48000.0);
229 const double data_rate = sndfiles.size() * channels * sample_size * samplerate;
231 ds << setprecision (1) << data_minutes;
233 cout << "BW @ " << written << " frames (" << ds.str() << " minutes) = " << (bandwidth/1048576.0) << " MB/sec " << bandwidth / data_rate << " x faster than necessary " << endl;