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