better error message if VST SDK is not installed and neeed
[ardour.git] / libs / ardour / utils.cc
1 /*
2     Copyright (C) 2000-2003 Paul Davis 
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <cstdio> /* for sprintf */
22 #include <cmath>
23 #include <cctype>
24 #include <string>
25 #include <cerrno>
26 #include <iostream>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #ifdef HAVE_WORDEXP
34 #include <wordexp.h>
35 #endif
36
37 #include <pbd/error.h>
38 #include <pbd/stacktrace.h>
39 #include <pbd/xml++.h>
40 #include <ardour/utils.h>
41
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace std;
46 using namespace PBD;
47
48 void
49 elapsed_time_to_str (char *buf, uint32_t seconds)
50
51 {
52         uint32_t days;
53         uint32_t hours;
54         uint32_t minutes;
55         uint32_t s;
56
57         s = seconds;
58         days = s / (3600 * 24);
59         s -= (days * 3600 * 24);
60         hours = s / 3600;
61         s -= (hours * 3600);
62         minutes = s / 60;
63         s -= minutes * 60;
64         
65         if (days) {
66                 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s", 
67                          days, 
68                          days > 1 ? "s" : "",
69                          hours,
70                          hours > 1 ? "s" : "");
71         } else if (hours) {
72                 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s", 
73                          hours, 
74                          hours > 1 ? "s" : "",
75                          minutes,
76                          minutes > 1 ? "s" : "");
77         } else if (minutes) {
78                 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s", 
79                          minutes,
80                          minutes > 1 ? "s" : "");
81         } else if (s) {
82                 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s", 
83                          seconds,
84                          seconds > 1 ? "s" : "");
85         } else {
86                 snprintf (buf, sizeof (buf), "no time");
87         }
88 }
89
90 string 
91 legalize_for_path (string str)
92 {
93         string::size_type pos;
94         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
95         string legal;
96
97         legal = str;
98         pos = 0;
99
100         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
101                 legal.replace (pos, 1, "_");
102                 pos += 1;
103         }
104
105         return legal;
106 }
107
108 ostream&
109 operator<< (ostream& o, const BBT_Time& bbt)
110 {
111         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
112         return o;
113 }
114
115 XMLNode *
116 find_named_node (const XMLNode& node, string name)
117 {
118         XMLNodeList nlist;
119         XMLNodeConstIterator niter;
120         XMLNode* child;
121
122         nlist = node.children();
123
124         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
125
126                 child = *niter;
127
128                 if (child->name() == name) {
129                         return child;
130                 }
131         }
132
133         return 0;
134 }
135
136 int
137 cmp_nocase (const string& s, const string& s2)
138 {
139         string::const_iterator p = s.begin();
140         string::const_iterator p2 = s2.begin();
141         
142         while (p != s.end() && p2 != s2.end()) {
143                 if (toupper(*p) != toupper(*p2)) {
144                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
145                 }
146                 ++p;
147                 ++p2;
148         }
149         
150         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
151 }
152
153 int
154 tokenize_fullpath (string fullpath, string& path, string& name)
155 {
156         string::size_type m = fullpath.find_last_of("/");
157         
158         if (m == string::npos) {
159                 path = fullpath;
160                 name = fullpath;
161                 return 1;
162         }
163
164         // does it look like just a directory?
165         if (m == fullpath.length()-1) {
166                 return -1;
167         }
168         path = fullpath.substr(0, m+1);
169         
170         string::size_type n = fullpath.find(".ardour", m);
171         // no .ardour?
172         if (n == string::npos) {
173                 return -1;
174         }
175         name = fullpath.substr(m+1, n - m - 1);
176         return 1;
177 }
178
179 int
180 touch_file (string path)
181 {
182         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
183         if (fd >= 0) {
184                 close (fd);
185                 return 0;
186         }
187         return 1;
188 }
189
190 string
191 placement_as_string (Placement p)
192 {
193         switch (p) {
194         case PreFader:
195                 return _("pre");
196         default: /* to get g++ to realize we have all the cases covered */
197         case PostFader:
198                 return _("post");
199         }
200 }
201
202 string
203 region_name_from_path (string path)
204 {
205         string::size_type pos;
206
207         /* remove filename suffixes etc. */
208         
209         if ((pos = path.find_last_of ('.')) != string::npos) {
210                 path = path.substr (0, pos);
211         }
212
213         /* remove any "?R", "?L" or "?[a-z]" channel identifier */
214         
215         string::size_type len = path.length();
216
217         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') && 
218             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
219                 
220                 path = path.substr (0, path.length() - 2);
221         }
222
223         return path;
224 }       
225
226 string
227 path_expand (string path)
228 {
229 #ifdef HAVE_WORDEXP
230         /* Handle tilde and environment variable expansion in session path */
231         string ret = path;
232
233         wordexp_t expansion;
234         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
235         case 0:
236                 break;
237         default:
238                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
239                 goto out;
240         }
241
242         if (expansion.we_wordc > 1) {
243                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
244                 goto out;
245         }
246
247         ret = expansion.we_wordv[0];
248   out:
249         wordfree (&expansion);
250         return ret;
251
252 #else 
253         return path;
254 #endif
255 }
256
257 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
258 string 
259 CFStringRefToStdString(CFStringRef stringRef)
260 {
261         CFIndex size = 
262                 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , 
263                 kCFStringEncodingUTF8);
264             char *buf = new char[size];
265         
266         std::string result;
267
268         if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
269             result = buf;
270         }
271         delete [] buf;
272         return result;
273 }
274 #endif // HAVE_COREAUDIO
275
276 void
277 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
278 {
279         double step;
280
281         step = 1.0/nframes;
282
283         in[0] = 0.0f;
284         
285         for (nframes_t i = 1; i < nframes - 1; ++i) {
286                 in[i] = in[i-1] + step;
287         }
288         
289         in[nframes-1] = 1.0;
290
291         const float pan_law_attenuation = -3.0f;
292         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
293
294         for (nframes_t n = 0; n < nframes; ++n) {
295                 float inVal = in[n];
296                 float outVal = 1 - inVal;
297                 out[n] = outVal * (scale * outVal + 1.0f - scale);
298                 in[n] = inVal * (scale * inVal + 1.0f - scale);
299         }
300 }
301
302 EditMode
303 string_to_edit_mode (string str)
304 {
305         if (str == _("Splice Edit")) {
306                 return Splice;
307         } else if (str == _("Slide Edit")) {
308                 return Slide;
309         }
310         fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
311         /*NOTREACHED*/
312         return Slide;
313 }
314
315 const char*
316 edit_mode_to_string (EditMode mode)
317 {
318         switch (mode) {
319         case Slide:
320                 return _("Slide Edit");
321
322         default:
323         case Splice:
324                 return _("Splice Edit");
325         }
326 }
327
328 SlaveSource
329 string_to_slave_source (string str)
330 {
331         if (str == _("Internal")) {
332                 return None;
333         }
334         
335         if (str == _("MTC")) {
336                 return MTC;
337         }
338
339         if (str == _("JACK")) {
340                 return JACK;
341         }
342
343         fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
344         /*NOTREACHED*/
345         return None;
346 }
347
348 const char*
349 slave_source_to_string (SlaveSource src)
350 {
351         switch (src) {
352         case JACK:
353                 return _("JACK");
354
355         case MTC:
356                 return _("MTC");
357                 
358         default:
359         case None:
360                 return _("Internal");
361                 
362         }
363 }
364
365 float
366 meter_falloff_to_float (MeterFalloff falloff)
367 {
368         switch (falloff) {
369         case MeterFalloffOff:
370                 return 0.0f;
371         case MeterFalloffSlowest:
372                 return 1.0f;
373         case MeterFalloffSlow:
374                 return 2.0f;
375         case MeterFalloffMedium:
376                 return 3.0f;
377         case MeterFalloffFast:
378                 return 4.0f;
379         case MeterFalloffFaster:
380                 return 5.0f;
381         case MeterFalloffFastest:
382         default:
383                 return 6.0f;
384         }
385 }
386
387 float
388 meter_hold_to_float (MeterHold hold)
389 {
390         switch (hold) {
391         case MeterHoldOff:
392                 return 0.0f;
393         case MeterHoldShort:
394                 return 40.0f;
395         case MeterHoldMedium:
396                 return 100.0f;
397         case MeterHoldLong:
398         default:
399                 return 200.0f;
400         }
401 }
402
403 AutoState 
404 ARDOUR::string_to_auto_state (std::string str)
405 {
406         if (str == X_("Off")) {
407                 return Off;
408         } else if (str == X_("Play")) {
409                 return Play;
410         } else if (str == X_("Write")) {
411                 return Write;
412         } else if (str == X_("Touch")) {
413                 return Touch;
414         }
415
416         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
417         /*NOTREACHED*/
418 }
419
420 string 
421 ARDOUR::auto_state_to_string (AutoState as)
422 {
423         /* to be used only for XML serialization, no i18n done */
424
425         switch (as) {
426         case Off:
427                 return X_("Off");
428                 break;
429         case Play:
430                 return X_("Play");
431                 break;
432         case Write:
433                 return X_("Write");
434                 break;
435         case Touch:
436                 return X_("Touch");
437         }
438 }
439
440 AutoStyle 
441 ARDOUR::string_to_auto_style (std::string str)
442 {
443         if (str == X_("Absolute")) {
444                 return Absolute;
445         } else if (str == X_("Trim")) {
446                 return Trim;
447         }
448
449         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
450         /*NOTREACHED*/
451 }
452
453 string 
454 ARDOUR::auto_style_to_string (AutoStyle as)
455 {
456         /* to be used only for XML serialization, no i18n done */
457
458         switch (as) {
459         case Absolute:
460                 return X_("Absolute");
461                 break;
462         case Trim:
463                 return X_("Trim");
464                 break;
465         }
466 }
467
468 extern "C" {
469         void c_stacktrace() { stacktrace (cerr); }
470 }