772fc3fd438bd0d88244fb53121457d39550e145
[ardour.git] / tools / sftest.cc
1 /* g++ -o sftest sftest.cc `pkg-config --cflags --libs sndfile` `pkg-config --cflags --libs glibmm-2.4` */
2
3 #include <vector>
4 #include <iostream>
5 #include <iomanip>
6 #include <sstream>
7 #include <cstdlib>
8 #include <cerrno>
9
10 #include <unistd.h>
11 #include <sndfile.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <getopt.h>
15 #include <fcntl.h>
16
17 #include <glibmm/miscutils.h>
18
19 using namespace std;
20
21 SF_INFO format_info;
22 float* data = 0;
23 bool with_sync = false;
24
25 int
26 write_one (SNDFILE* sf, uint32_t nframes)
27 {
28         if (sf_write_float (sf, (float*) data, nframes) != nframes) {
29                 return -1;
30         }
31
32         if (with_sync) {
33                 sf_write_sync (sf);
34         }
35
36         return 0;
37 }
38
39 void
40 usage ()
41 {
42         cout << "sftest [ -f HEADER-FORMAT ] [ -F DATA-FORMAT ] [ -r SAMPLERATE ] [ -n NFILES ] [ -b BLOCKSIZE ] [ -s ]" << endl;
43         cout << "\tHEADER-FORMAT is one of:" << endl
44              << "\t\tWAV" << endl
45              << "\t\tCAF" << endl
46              << "\t\tW64" << endl;
47         cout << "\tDATA-FORMAT is one of:" << endl
48              << "\t\tFLOAT" << endl
49              << "\t\t32" << endl
50              << "\t\t24" << endl
51              << "\t\t16" << endl;
52 }
53
54 int
55 main (int argc, char* argv[])
56 {
57         vector<SNDFILE*> sndfiles;
58         uint32_t sample_size;
59         char optstring[] = "f:r:F:n:c:b:sD";
60         int channels, samplerate;
61         char const *suffix = ".wav";
62         char const *header_format = "wav";
63         char const *data_format = "float";
64         uint32_t block_size = 64 * 1024;
65         uint32_t nfiles = 100;
66         bool direct = false;
67         const struct option longopts[] = {
68                 { "header-format", 1, 0, 'f' },
69                 { "data-format", 1, 0, 'F' },
70                 { "rate", 1, 0, 'r' },
71                 { "nfiles", 1, 0, 'n' },
72                 { "blocksize", 1, 0, 'b' },
73                 { "channels", 1, 0, 'c' },
74                 { "sync", 0, 0, 's' },
75                 { "direct", 0, 0, 'D' },
76                 { 0, 0, 0, 0 }
77         };
78
79         int option_index = 0;
80         int c = 0;
81         
82         while (1) {
83                 if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
84                         break;
85                 }
86
87                 switch (c) {
88                 case 'f':
89                         header_format = optarg;
90                         break;
91
92                 case 'F':
93                         data_format = optarg;
94                         break;
95
96                 case 'r':
97                         samplerate = atoi (optarg);
98                         break;
99
100                 case 'n':
101                         nfiles = atoi (optarg);
102                         break;
103
104                 case 'c':
105                         channels = atoi (optarg);
106
107                 case 'b':
108                         block_size = atoi (optarg);
109                         break;
110                 case 's':
111                         with_sync = true;
112                         break;
113                 case 'D':
114                         direct = true;
115                         break;
116                 default:
117                         usage ();
118                         return 0;
119                 }
120         }
121
122         /* setup file format */
123         memset (&format_info, 0, sizeof (format_info));
124
125         if (samplerate == 0 || nfiles == 0 || block_size == 0 || channels == 0) {
126                 usage ();
127                 return 1;
128         }
129
130         format_info.samplerate = samplerate;
131         format_info.channels = channels;
132         
133         if (strcasecmp (header_format, "wav") == 0) {
134                 format_info.format |= SF_FORMAT_WAV;
135                 suffix = ".wav";
136         } else if (strcasecmp (header_format, "caf") == 0) {
137                 format_info.format |= SF_FORMAT_CAF;
138                 suffix = ".caf";
139         } else if (strcasecmp (header_format, "w64") == 0) {
140                 format_info.format |= SF_FORMAT_W64;
141                 suffix = ".w64";
142         } else {
143                 usage ();
144                 return 0;
145         }
146
147         if (strcasecmp (data_format, "float") == 0) {
148                 format_info.format |= SF_FORMAT_FLOAT;
149                 sample_size = sizeof (float);
150         } else if (strcasecmp (data_format, "32") == 0) {
151                 format_info.format |= SF_FORMAT_PCM_32;
152                 sample_size = 4;
153         } else if (strcasecmp (data_format, "24") == 0) {
154                 format_info.format |= SF_FORMAT_PCM_24;
155                 sample_size = 3;
156         } else if (strcasecmp (data_format, "16") == 0) {
157                 format_info.format |= SF_FORMAT_PCM_16;
158                 sample_size = 2;
159         } else {
160                 usage ();
161                 return 0;
162         }
163         
164         char tmpdirname[] = "sftest-XXXXXX";
165         g_mkdtemp (tmpdirname);
166
167         for (uint32_t n = 0; n < nfiles; ++n) {
168                 SNDFILE* sf;
169                 string path;
170                 stringstream ss;
171
172                 ss << "sf-";
173                 ss << n;
174                 ss << suffix;
175                 
176                 path = Glib::build_filename (tmpdirname, ss.str());
177
178                 int flags = O_RDWR|O_CREAT|O_TRUNC;
179                 int fd = open (path.c_str(), flags, 0644);
180
181                 if (fd < 0) {
182                         cerr << "Could not open file #" << n << " @ " << path << " (" << strerror (errno) << ")" << endl;
183                         return 1;
184                 }
185
186 #ifdef __APPLE__
187                 if (direct) {
188                         /* Apple man pages say only that it returns "a value other than -1 on success",
189                            which probably means zero, but you just can't be too careful with
190                            those guys.
191                         */
192                         if (fcntl (fd, F_NOCACHE, 1) == -1) {
193                                 cerr << "Cannot set F_NOCACHE on file # " << n << endl;
194                         }
195                 }
196 #endif
197                 if ((sf = sf_open_fd (fd, SFM_RDWR, &format_info, true)) == 0) {
198                         cerr << "Could not open SNDFILE #" << n << " @ " << path << " (" << sf_strerror (0) << ")" << endl;
199                         return 1;
200                 }
201
202                 sndfiles.push_back (sf);
203         }
204
205         cout << nfiles << " files are in " << tmpdirname << " all used " << (direct ? "without" : "with") << " OS buffer cache" << endl;
206         cout << "Format is " << suffix << ' ' << channels << " channel" << (channels > 1 ? "s" : "") << " written in chunks of " << block_size << " frames, synced ? " << (with_sync ? "yes" : "no") << endl;
207                 
208         data = new float[block_size*channels];
209         uint64_t written = 0;
210         
211         while (true) {
212                 gint64 before;
213                 before = g_get_monotonic_time();
214                 for (vector<SNDFILE*>::iterator s = sndfiles.begin(); s != sndfiles.end(); ++s) {
215                         if (write_one (*s, block_size)) {
216                                 cerr << "Write failed for file #" << distance (sndfiles.begin(), s) << endl;
217                                 return 1;
218                         }
219                 }
220                 written += block_size;
221                 gint64 elapsed = g_get_monotonic_time() - before;
222                 double bandwidth = (sndfiles.size() * block_size * channels * sample_size) / (elapsed/1000000.0);
223                 double data_minutes = written / (double) (60.0 * 48000.0);
224                 const double data_rate = sndfiles.size() * channels * sample_size * samplerate;
225                 stringstream ds;
226                 ds << setprecision (1) << data_minutes;
227                 
228                 cout << "BW @ " << written << " frames (" << ds.str() << " minutes) = " << (bandwidth/1048576.0) <<  " MB/sec " << bandwidth / data_rate << " x faster than necessary " << endl;
229         }
230
231         return 0;
232 }
233
234