9a841e81b412ac538ef5e5db30fa72c83dfe692e
[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/xml++.h>
42 #include <ardour/utils.h>
43
44 #include "i18n.h"
45
46 using namespace ARDOUR;
47 using namespace std;
48 using namespace PBD;
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 string 
93 legalize_for_path (string str)
94 {
95         string::size_type pos;
96         string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
97         string 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 ostream&
111 operator<< (ostream& o, const BBT_Time& bbt)
112 {
113         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
114         return o;
115 }
116
117 XMLNode *
118 find_named_node (const XMLNode& node, string name)
119 {
120         XMLNodeList nlist;
121         XMLNodeConstIterator niter;
122         XMLNode* child;
123
124         nlist = node.children();
125
126         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
127
128                 child = *niter;
129
130                 if (child->name() == name) {
131                         return child;
132                 }
133         }
134
135         return 0;
136 }
137
138 int
139 cmp_nocase (const string& s, const string& s2)
140 {
141         string::const_iterator p = s.begin();
142         string::const_iterator p2 = s2.begin();
143         
144         while (p != s.end() && p2 != s2.end()) {
145                 if (toupper(*p) != toupper(*p2)) {
146                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
147                 }
148                 ++p;
149                 ++p2;
150         }
151         
152         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
153 }
154
155 int
156 tokenize_fullpath (string fullpath, string& path, string& name)
157 {
158         string::size_type m = fullpath.find_last_of("/");
159         
160         if (m == string::npos) {
161                 path = fullpath;
162                 name = fullpath;
163                 return 1;
164         }
165
166         // does it look like just a directory?
167         if (m == fullpath.length()-1) {
168                 return -1;
169         }
170         path = fullpath.substr(0, m+1);
171         
172         string::size_type n = fullpath.find(".ardour", m);
173         // no .ardour?
174         if (n == string::npos) {
175                 return -1;
176         }
177         name = fullpath.substr(m+1, n - m - 1);
178         return 1;
179 }
180
181 int
182 touch_file (string path)
183 {
184         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
185         if (fd >= 0) {
186                 close (fd);
187                 return 0;
188         }
189         return 1;
190 }
191
192 string
193 placement_as_string (Placement p)
194 {
195         switch (p) {
196         case PreFader:
197                 return _("pre");
198         default: /* to get g++ to realize we have all the cases covered */
199         case PostFader:
200                 return _("post");
201         }
202 }
203
204 string
205 region_name_from_path (string path)
206 {
207         string::size_type pos;
208
209         /* remove any leading path */
210
211         if ((pos = path.find_last_of ('/')) != string::npos) {
212                 path = path.substr(pos+1);
213         }
214
215         /* remove filename suffixes etc. */
216         
217         if ((pos = path.find_last_of ('.')) != string::npos) {
218                 path = path.substr (0, pos);
219         }
220
221         /* remove any "?R", "?L" or "?[a-z]" channel identifier */
222         
223         string::size_type len = path.length();
224         
225         if (len > 3 && (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         return path;
232 }       
233
234 string
235 path_expand (string path)
236 {
237 #ifdef HAVE_WORDEXP
238         /* Handle tilde and environment variable expansion in session path */
239         string ret = path;
240
241         wordexp_t expansion;
242         switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
243         case 0:
244                 break;
245         default:
246                 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
247                 goto out;
248         }
249
250         if (expansion.we_wordc > 1) {
251                 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
252                 goto out;
253         }
254
255         ret = expansion.we_wordv[0];
256   out:
257         wordfree (&expansion);
258         return ret;
259
260 #else 
261         return path;
262 #endif
263 }
264
265 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
266 string 
267 CFStringRefToStdString(CFStringRef stringRef)
268 {
269         CFIndex size = 
270                 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , 
271                 kCFStringEncodingUTF8);
272             char *buf = new char[size];
273         
274         std::string result;
275
276         if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
277             result = buf;
278         }
279         delete [] buf;
280         return result;
281 }
282 #endif // HAVE_COREAUDIO
283
284 void
285 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
286 {
287         double step;
288
289         step = 1.0/nframes;
290
291         in[0] = 0.0f;
292         
293         for (nframes_t i = 1; i < nframes - 1; ++i) {
294                 in[i] = in[i-1] + step;
295         }
296         
297         in[nframes-1] = 1.0;
298
299         const float pan_law_attenuation = -3.0f;
300         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
301
302         for (unsigned long n = 0; n < nframes; ++n) {
303                 float inVal = in[n];
304                 float outVal = 1 - inVal;
305                 out[n] = outVal * (scale * outVal + 1.0f - scale);
306                 in[n] = inVal * (scale * inVal + 1.0f - scale);
307         }
308 }
309
310 EditMode
311 string_to_edit_mode (string str)
312 {
313         if (str == _("Splice Edit")) {
314                 return Splice;
315         } else if (str == _("Slide Edit")) {
316                 return Slide;
317         }
318         fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
319         /*NOTREACHED*/
320         return Slide;
321 }
322
323 const char*
324 edit_mode_to_string (EditMode mode)
325 {
326         switch (mode) {
327         case Slide:
328                 return _("Slide Edit");
329
330         default:
331         case Splice:
332                 return _("Splice Edit");
333         }
334 }
335
336 SlaveSource
337 string_to_slave_source (string str)
338 {
339         if (str == _("Internal")) {
340                 return None;
341         }
342         
343         if (str == _("MTC")) {
344                 return MTC;
345         }
346
347         if (str == _("JACK")) {
348                 return JACK;
349         }
350
351         fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
352         /*NOTREACHED*/
353         return None;
354 }
355
356 const char*
357 slave_source_to_string (SlaveSource src)
358 {
359         switch (src) {
360         case JACK:
361                 return _("JACK");
362
363         case MTC:
364                 return _("MTC");
365                 
366         default:
367         case None:
368                 return _("Internal");
369                 
370         }
371 }
372
373 float
374 meter_falloff_to_float (MeterFalloff falloff)
375 {
376         switch (falloff) {
377         case MeterFalloffOff:
378                 return 0.0f;
379         case MeterFalloffSlowest:
380                 return 1.0f;
381         case MeterFalloffSlow:
382                 return 2.0f;
383         case MeterFalloffMedium:
384                 return 3.0f;
385         case MeterFalloffFast:
386                 return 4.0f;
387         case MeterFalloffFaster:
388                 return 5.0f;
389         case MeterFalloffFastest:
390         default:
391                 return 6.0f;
392         }
393 }
394
395 float
396 meter_hold_to_float (MeterHold hold)
397 {
398         switch (hold) {
399         case MeterHoldOff:
400                 return 0.0f;
401         case MeterHoldShort:
402                 return 40.0f;
403         case MeterHoldMedium:
404                 return 100.0f;
405         case MeterHoldLong:
406         default:
407                 return 200.0f;
408         }
409 }