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