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