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