Fix compilation with GCC 4.3 (at least with SYSLIBS=1).
[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 */
19
20 #include <cstdio> /* for sprintf */
21 #include <cmath>
22 #include <cctype>
23 #include <string>
24 #include <cstring>
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 <pbd/basename.h>
41 #include <ardour/utils.h>
42
43 #include "i18n.h"
44
45 using namespace ARDOUR;
46 using namespace std;
47 using namespace PBD;
48 using Glib::ustring;
49
50 void
51 elapsed_time_to_str (char *buf, uint32_t seconds)
52
53 {
54         uint32_t days;
55         uint32_t hours;
56         uint32_t minutes;
57         uint32_t s;
58
59         s = seconds;
60         days = s / (3600 * 24);
61         s -= (days * 3600 * 24);
62         hours = s / 3600;
63         s -= (hours * 3600);
64         minutes = s / 60;
65         s -= minutes * 60;
66         
67         if (days) {
68                 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s", 
69                          days, 
70                          days > 1 ? "s" : "",
71                          hours,
72                          hours > 1 ? "s" : "");
73         } else if (hours) {
74                 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s", 
75                          hours, 
76                          hours > 1 ? "s" : "",
77                          minutes,
78                          minutes > 1 ? "s" : "");
79         } else if (minutes) {
80                 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s", 
81                          minutes,
82                          minutes > 1 ? "s" : "");
83         } else if (s) {
84                 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s", 
85                          seconds,
86                          seconds > 1 ? "s" : "");
87         } else {
88                 snprintf (buf, sizeof (buf), "no time");
89         }
90 }
91
92 ustring 
93 legalize_for_path (ustring str)
94 {
95         ustring::size_type pos;
96         ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
97         ustring legal;
98
99         legal = str;
100         pos = 0;
101
102         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
103                 legal.replace (pos, 1, "_");
104                 pos += 1;
105         }
106
107         return legal;
108 }
109
110 string bump_name_once(std::string name)
111 {
112         string::size_type period;
113         string newname;
114
115         if ((period = name.find_last_of ('.')) == string::npos) {
116                 newname  = name;
117                 newname += ".1";
118         } else {
119                 int isnumber = 1;
120                 const char *last_element = name.c_str() + period + 1;
121                 for (size_t i = 0; i < strlen(last_element); i++) {
122                         if (!isdigit(last_element[i])) {
123                                 isnumber = 0;
124                                 break;
125                         }
126                 }
127
128                 errno = 0;
129                 long int version = strtol (name.c_str()+period+1, (char **)NULL, 10);
130
131                 if (isnumber == 0 || errno != 0) {
132                         // last_element is not a number, or is too large
133                         newname  = name;
134                         newname += ".1";
135                 } else {
136                         char buf[32];
137
138                         snprintf (buf, sizeof(buf), "%ld", version+1);
139                 
140                         newname  = name.substr (0, period+1);
141                         newname += buf;
142                 }
143         }
144
145         return newname;
146
147 }
148
149 ostream&
150 operator<< (ostream& o, const BBT_Time& bbt)
151 {
152         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
153         return o;
154 }
155
156 XMLNode *
157 find_named_node (const XMLNode& node, string name)
158 {
159         XMLNodeList nlist;
160         XMLNodeConstIterator niter;
161         XMLNode* child;
162
163         nlist = node.children();
164
165         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
166
167                 child = *niter;
168
169                 if (child->name() == name) {
170                         return child;
171                 }
172         }
173
174         return 0;
175 }
176
177 int
178 cmp_nocase (const string& s, const string& s2)
179 {
180         string::const_iterator p = s.begin();
181         string::const_iterator p2 = s2.begin();
182         
183         while (p != s.end() && p2 != s2.end()) {
184                 if (toupper(*p) != toupper(*p2)) {
185                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
186                 }
187                 ++p;
188                 ++p2;
189         }
190         
191         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
192 }
193
194 int
195 tokenize_fullpath (string fullpath, string& path, string& name)
196 {
197         string::size_type m = fullpath.find_last_of("/");
198         
199         if (m == string::npos) {
200                 path = fullpath;
201                 name = fullpath;
202                 return 1;
203         }
204
205         // does it look like just a directory?
206         if (m == fullpath.length()-1) {
207                 return -1;
208         }
209         path = fullpath.substr(0, m+1);
210         
211         string::size_type n = fullpath.find(".ardour", m);
212         // no .ardour?
213         if (n == string::npos) {
214                 return -1;
215         }
216         name = fullpath.substr(m+1, n - m - 1);
217         return 1;
218 }
219
220 int
221 touch_file (ustring path)
222 {
223         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
224         if (fd >= 0) {
225                 close (fd);
226                 return 0;
227         }
228         return 1;
229 }
230
231 ustring
232 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
233 {
234         path = PBD::basename_nosuffix (path);
235
236         if (strip_channels) {
237
238                 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
239                 
240                 ustring::size_type len = path.length();
241                 
242                 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') && 
243                     (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
244                         
245                         path = path.substr (0, path.length() - 2);
246                 }
247         }
248
249         if (add_channel_suffix) {
250
251                 path += '%';
252                 
253                 if (total > 2) {
254                         path += (char) ('a' + this_one);
255                 } else {
256                         path += (char) (this_one == 0 ? 'L' : 'R');
257                 }
258         }
259
260         return path;
261 }       
262
263 bool
264 path_is_paired (ustring path, ustring& pair_base)
265 {
266         ustring::size_type pos;
267
268         /* remove filename suffixes etc. */
269         
270         if ((pos = path.find_last_of ('.')) != string::npos) {
271                 path = path.substr (0, pos);
272         }
273
274         ustring::size_type len = path.length();
275
276         /* look for possible channel identifier: "?R", "%R", ".L" etc. */
277
278         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') && 
279             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
280                 
281                 pair_base = path.substr (0, len-2);
282                 return true;
283
284         } 
285
286         return false;
287 }
288
289 ustring
290 path_expand (ustring path)
291 {
292 #ifdef HAVE_WORDEXP
293         /* Handle tilde and environment variable expansion in session path */
294         string ret = path;
295
296         wordexp_t expansion;
297         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
298         case 0:
299                 break;
300         default:
301                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
302                 goto out;
303         }
304
305         if (expansion.we_wordc > 1) {
306                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
307                 goto out;
308         }
309
310         ret = expansion.we_wordv[0];
311   out:
312         wordfree (&expansion);
313         return ret;
314
315 #else 
316         return path;
317 #endif
318 }
319
320 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
321 string 
322 CFStringRefToStdString(CFStringRef stringRef)
323 {
324         CFIndex size = 
325                 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , 
326                 kCFStringEncodingUTF8);
327             char *buf = new char[size];
328         
329         std::string result;
330
331         if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
332             result = buf;
333         }
334         delete [] buf;
335         return result;
336 }
337 #endif // HAVE_COREAUDIO
338
339 void
340 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
341 {
342         double step;
343
344         step = 1.0/nframes;
345
346         in[0] = 0.0f;
347         
348         for (nframes_t i = 1; i < nframes - 1; ++i) {
349                 in[i] = in[i-1] + step;
350         }
351         
352         in[nframes-1] = 1.0;
353
354         const float pan_law_attenuation = -3.0f;
355         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
356
357         for (nframes_t n = 0; n < nframes; ++n) {
358                 float inVal = in[n];
359                 float outVal = 1 - inVal;
360                 out[n] = outVal * (scale * outVal + 1.0f - scale);
361                 in[n] = inVal * (scale * inVal + 1.0f - scale);
362         }
363 }
364
365 EditMode
366 string_to_edit_mode (string str)
367 {
368         if (str == _("Splice Edit")) {
369                 return Splice;
370         } else if (str == _("Slide Edit")) {
371                 return Slide;
372         } else if (str == _("Lock Edit")) {
373                 return Lock;
374         }
375         fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
376         /*NOTREACHED*/
377         return Slide;
378 }
379
380 const char*
381 edit_mode_to_string (EditMode mode)
382 {
383         switch (mode) {
384         case Slide:
385                 return _("Slide Edit");
386
387         case Lock:
388                 return _("Lock Edit");
389
390         default:
391         case Splice:
392                 return _("Splice Edit");
393         }
394 }
395
396 SlaveSource
397 string_to_slave_source (string str)
398 {
399         if (str == _("Internal")) {
400                 return None;
401         }
402         
403         if (str == _("MTC")) {
404                 return MTC;
405         }
406
407         if (str == _("JACK")) {
408                 return JACK;
409         }
410
411         fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
412         /*NOTREACHED*/
413         return None;
414 }
415
416 const char*
417 slave_source_to_string (SlaveSource src)
418 {
419         switch (src) {
420         case JACK:
421                 return _("JACK");
422
423         case MTC:
424                 return _("MTC");
425                 
426         default:
427         case None:
428                 return _("Internal");
429                 
430         }
431 }
432
433 /* I don't really like hard-coding these falloff rates here
434  * Probably should use a map of some kind that could be configured
435  * These rates are db/sec.
436 */
437
438 #define METER_FALLOFF_OFF     0.0f
439 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
440 #define METER_FALLOFF_SLOW    8.6f // BBC standard
441 #define METER_FALLOFF_MEDIUM  20.0f
442 #define METER_FALLOFF_FAST    32.0f
443 #define METER_FALLOFF_FASTER  46.0f
444 #define METER_FALLOFF_FASTEST 70.0f
445
446 float
447 meter_falloff_to_float (MeterFalloff falloff)
448 {
449         switch (falloff) {
450         case MeterFalloffOff:
451                 return METER_FALLOFF_OFF;
452         case MeterFalloffSlowest:
453                 return METER_FALLOFF_SLOWEST;
454         case MeterFalloffSlow:
455                 return METER_FALLOFF_SLOW;
456         case MeterFalloffMedium:
457                 return METER_FALLOFF_MEDIUM;
458         case MeterFalloffFast:
459                 return METER_FALLOFF_FAST;
460         case MeterFalloffFaster:
461                 return METER_FALLOFF_FASTER;
462         case MeterFalloffFastest:
463                 return METER_FALLOFF_FASTEST;
464         default:
465                 return METER_FALLOFF_FAST;
466         }
467 }
468
469 MeterFalloff
470 meter_falloff_from_float (float val)
471 {
472         if (val == METER_FALLOFF_OFF) {
473                 return MeterFalloffOff;
474         }
475         else if (val <= METER_FALLOFF_SLOWEST) {
476                 return MeterFalloffSlowest;
477         }
478         else if (val <= METER_FALLOFF_SLOW) {
479                 return MeterFalloffSlow;
480         }
481         else if (val <= METER_FALLOFF_MEDIUM) {
482                 return MeterFalloffMedium;
483         }
484         else if (val <= METER_FALLOFF_FAST) {
485                 return MeterFalloffFast;
486         }
487         else if (val <= METER_FALLOFF_FASTER) {
488                 return MeterFalloffFaster;
489         }
490         else {
491                 return MeterFalloffFastest;
492         }
493 }
494
495 float
496 meter_hold_to_float (MeterHold hold)
497 {
498         switch (hold) {
499         case MeterHoldOff:
500                 return 0.0f;
501         case MeterHoldShort:
502                 return 40.0f;
503         case MeterHoldMedium:
504                 return 100.0f;
505         case MeterHoldLong:
506         default:
507                 return 200.0f;
508         }
509 }
510
511 AutoState 
512 ARDOUR::string_to_auto_state (std::string str)
513 {
514         if (str == X_("Off")) {
515                 return Off;
516         } else if (str == X_("Play")) {
517                 return Play;
518         } else if (str == X_("Write")) {
519                 return Write;
520         } else if (str == X_("Touch")) {
521                 return Touch;
522         }
523
524         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
525         /*NOTREACHED*/
526         return Touch;
527 }
528
529 string 
530 ARDOUR::auto_state_to_string (AutoState as)
531 {
532         /* to be used only for XML serialization, no i18n done */
533
534         switch (as) {
535         case Off:
536                 return X_("Off");
537                 break;
538         case Play:
539                 return X_("Play");
540                 break;
541         case Write:
542                 return X_("Write");
543                 break;
544         case Touch:
545                 return X_("Touch");
546         }
547
548         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
549         /*NOTREACHED*/
550         return "";
551 }
552
553 AutoStyle 
554 ARDOUR::string_to_auto_style (std::string str)
555 {
556         if (str == X_("Absolute")) {
557                 return Absolute;
558         } else if (str == X_("Trim")) {
559                 return Trim;
560         }
561
562         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
563         /*NOTREACHED*/
564         return Trim;
565 }
566
567 string 
568 ARDOUR::auto_style_to_string (AutoStyle as)
569 {
570         /* to be used only for XML serialization, no i18n done */
571
572         switch (as) {
573         case Absolute:
574                 return X_("Absolute");
575                 break;
576         case Trim:
577                 return X_("Trim");
578                 break;
579         }
580
581         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
582         /*NOTREACHED*/
583         return "";
584 }
585
586 extern "C" {
587         void c_stacktrace() { stacktrace (cerr); }
588 }