use ustring more; handle embedding of "paired" files as per mantis #1362
[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 <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 #if 0
110 string 
111 legalize_for_path (string str)
112 {
113         string::size_type pos;
114         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
115         string legal;
116
117         legal = str;
118         pos = 0;
119
120         while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
121                 legal.replace (pos, 1, "_");
122                 pos += 1;
123         }
124
125         return legal;
126 }
127 #endif
128
129 ostream&
130 operator<< (ostream& o, const BBT_Time& bbt)
131 {
132         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
133         return o;
134 }
135
136 XMLNode *
137 find_named_node (const XMLNode& node, string name)
138 {
139         XMLNodeList nlist;
140         XMLNodeConstIterator niter;
141         XMLNode* child;
142
143         nlist = node.children();
144
145         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
146
147                 child = *niter;
148
149                 if (child->name() == name) {
150                         return child;
151                 }
152         }
153
154         return 0;
155 }
156
157 int
158 cmp_nocase (const string& s, const string& s2)
159 {
160         string::const_iterator p = s.begin();
161         string::const_iterator p2 = s2.begin();
162         
163         while (p != s.end() && p2 != s2.end()) {
164                 if (toupper(*p) != toupper(*p2)) {
165                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
166                 }
167                 ++p;
168                 ++p2;
169         }
170         
171         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
172 }
173
174 int
175 tokenize_fullpath (string fullpath, string& path, string& name)
176 {
177         string::size_type m = fullpath.find_last_of("/");
178         
179         if (m == string::npos) {
180                 path = fullpath;
181                 name = fullpath;
182                 return 1;
183         }
184
185         // does it look like just a directory?
186         if (m == fullpath.length()-1) {
187                 return -1;
188         }
189         path = fullpath.substr(0, m+1);
190         
191         string::size_type n = fullpath.find(".ardour", m);
192         // no .ardour?
193         if (n == string::npos) {
194                 return -1;
195         }
196         name = fullpath.substr(m+1, n - m - 1);
197         return 1;
198 }
199
200 int
201 touch_file (ustring path)
202 {
203         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
204         if (fd >= 0) {
205                 close (fd);
206                 return 0;
207         }
208         return 1;
209 }
210
211 string
212 placement_as_string (Placement p)
213 {
214         switch (p) {
215         case PreFader:
216                 return _("pre");
217         default: /* to get g++ to realize we have all the cases covered */
218         case PostFader:
219                 return _("post");
220         }
221 }
222
223 ustring
224 region_name_from_path (ustring path, bool strip_channels)
225 {
226         path = PBD::basename_nosuffix (path);
227
228         if (strip_channels) {
229
230                 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
231                 
232                 ustring::size_type len = path.length();
233                 
234                 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') && 
235                     (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
236                         
237                         path = path.substr (0, path.length() - 2);
238                 }
239         }
240
241         return path;
242 }       
243
244 bool
245 path_is_paired (ustring path, ustring& pair_base)
246 {
247         ustring::size_type pos;
248
249         /* remove filename suffixes etc. */
250         
251         if ((pos = path.find_last_of ('.')) != string::npos) {
252                 path = path.substr (0, pos);
253         }
254
255         ustring::size_type len = path.length();
256
257         /* look for possible channel identifier: "?R", "%R", ".L" etc. */
258
259         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') && 
260             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
261                 
262                 pair_base = path.substr (0, len-2);
263                 return true;
264
265         } 
266
267         return false;
268 }
269
270 ustring
271 path_expand (ustring path)
272 {
273 #ifdef HAVE_WORDEXP
274         /* Handle tilde and environment variable expansion in session path */
275         string ret = path;
276
277         wordexp_t expansion;
278         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
279         case 0:
280                 break;
281         default:
282                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
283                 goto out;
284         }
285
286         if (expansion.we_wordc > 1) {
287                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
288                 goto out;
289         }
290
291         ret = expansion.we_wordv[0];
292   out:
293         wordfree (&expansion);
294         return ret;
295
296 #else 
297         return path;
298 #endif
299 }
300
301 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
302 string 
303 CFStringRefToStdString(CFStringRef stringRef)
304 {
305         CFIndex size = 
306                 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , 
307                 kCFStringEncodingUTF8);
308             char *buf = new char[size];
309         
310         std::string result;
311
312         if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
313             result = buf;
314         }
315         delete [] buf;
316         return result;
317 }
318 #endif // HAVE_COREAUDIO
319
320 void
321 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
322 {
323         double step;
324
325         step = 1.0/nframes;
326
327         in[0] = 0.0f;
328         
329         for (nframes_t i = 1; i < nframes - 1; ++i) {
330                 in[i] = in[i-1] + step;
331         }
332         
333         in[nframes-1] = 1.0;
334
335         const float pan_law_attenuation = -3.0f;
336         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
337
338         for (nframes_t n = 0; n < nframes; ++n) {
339                 float inVal = in[n];
340                 float outVal = 1 - inVal;
341                 out[n] = outVal * (scale * outVal + 1.0f - scale);
342                 in[n] = inVal * (scale * inVal + 1.0f - scale);
343         }
344 }
345
346 EditMode
347 string_to_edit_mode (string str)
348 {
349         if (str == _("Splice Edit")) {
350                 return Splice;
351         } else if (str == _("Slide Edit")) {
352                 return Slide;
353         }
354         fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
355         /*NOTREACHED*/
356         return Slide;
357 }
358
359 const char*
360 edit_mode_to_string (EditMode mode)
361 {
362         switch (mode) {
363         case Slide:
364                 return _("Slide Edit");
365
366         default:
367         case Splice:
368                 return _("Splice Edit");
369         }
370 }
371
372 SlaveSource
373 string_to_slave_source (string str)
374 {
375         if (str == _("Internal")) {
376                 return None;
377         }
378         
379         if (str == _("MTC")) {
380                 return MTC;
381         }
382
383         if (str == _("JACK")) {
384                 return JACK;
385         }
386
387         fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
388         /*NOTREACHED*/
389         return None;
390 }
391
392 const char*
393 slave_source_to_string (SlaveSource src)
394 {
395         switch (src) {
396         case JACK:
397                 return _("JACK");
398
399         case MTC:
400                 return _("MTC");
401                 
402         default:
403         case None:
404                 return _("Internal");
405                 
406         }
407 }
408
409 float
410 meter_falloff_to_float (MeterFalloff falloff)
411 {
412         switch (falloff) {
413         case MeterFalloffOff:
414                 return 0.0f;
415         case MeterFalloffSlowest:
416                 return 1.0f;
417         case MeterFalloffSlow:
418                 return 2.0f;
419         case MeterFalloffMedium:
420                 return 3.0f;
421         case MeterFalloffFast:
422                 return 4.0f;
423         case MeterFalloffFaster:
424                 return 5.0f;
425         case MeterFalloffFastest:
426         default:
427                 return 6.0f;
428         }
429 }
430
431 float
432 meter_hold_to_float (MeterHold hold)
433 {
434         switch (hold) {
435         case MeterHoldOff:
436                 return 0.0f;
437         case MeterHoldShort:
438                 return 40.0f;
439         case MeterHoldMedium:
440                 return 100.0f;
441         case MeterHoldLong:
442         default:
443                 return 200.0f;
444         }
445 }
446
447 AutoState 
448 ARDOUR::string_to_auto_state (std::string str)
449 {
450         if (str == X_("Off")) {
451                 return Off;
452         } else if (str == X_("Play")) {
453                 return Play;
454         } else if (str == X_("Write")) {
455                 return Write;
456         } else if (str == X_("Touch")) {
457                 return Touch;
458         }
459
460         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
461         /*NOTREACHED*/
462         return Touch;
463 }
464
465 string 
466 ARDOUR::auto_state_to_string (AutoState as)
467 {
468         /* to be used only for XML serialization, no i18n done */
469
470         switch (as) {
471         case Off:
472                 return X_("Off");
473                 break;
474         case Play:
475                 return X_("Play");
476                 break;
477         case Write:
478                 return X_("Write");
479                 break;
480         case Touch:
481                 return X_("Touch");
482         }
483
484         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
485         /*NOTREACHED*/
486         return "";
487 }
488
489 AutoStyle 
490 ARDOUR::string_to_auto_style (std::string str)
491 {
492         if (str == X_("Absolute")) {
493                 return Absolute;
494         } else if (str == X_("Trim")) {
495                 return Trim;
496         }
497
498         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
499         /*NOTREACHED*/
500         return Trim;
501 }
502
503 string 
504 ARDOUR::auto_style_to_string (AutoStyle as)
505 {
506         /* to be used only for XML serialization, no i18n done */
507
508         switch (as) {
509         case Absolute:
510                 return X_("Absolute");
511                 break;
512         case Trim:
513                 return X_("Trim");
514                 break;
515         }
516
517         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
518         /*NOTREACHED*/
519         return "";
520 }
521
522 extern "C" {
523         void c_stacktrace() { stacktrace (cerr); }
524 }