libs/ardour/wscript: unit tests get a target name, inorder to be able to build them...
[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 ostream&
115 operator<< (ostream& o, const BBT_Time& bbt)
116 {
117         o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
118         return o;
119 }
120
121 XMLNode *
122 find_named_node (const XMLNode& node, string name)
123 {
124         XMLNodeList nlist;
125         XMLNodeConstIterator niter;
126         XMLNode* child;
127
128         nlist = node.children();
129
130         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
131
132                 child = *niter;
133
134                 if (child->name() == name) {
135                         return child;
136                 }
137         }
138
139         return 0;
140 }
141
142 int
143 cmp_nocase (const string& s, const string& s2)
144 {
145         string::const_iterator p = s.begin();
146         string::const_iterator p2 = s2.begin();
147
148         while (p != s.end() && p2 != s2.end()) {
149                 if (toupper(*p) != toupper(*p2)) {
150                         return (toupper(*p) < toupper(*p2)) ? -1 : 1;
151                 }
152                 ++p;
153                 ++p2;
154         }
155
156         return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
157 }
158
159 int
160 touch_file (ustring path)
161 {
162         int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
163         if (fd >= 0) {
164                 close (fd);
165                 return 0;
166         }
167         return 1;
168 }
169
170 ustring
171 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
172 {
173         path = PBD::basename_nosuffix (path);
174
175         if (strip_channels) {
176
177                 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
178
179                 ustring::size_type len = path.length();
180
181                 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
182                     (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
183
184                         path = path.substr (0, path.length() - 2);
185                 }
186         }
187
188         if (add_channel_suffix) {
189
190                 path += '%';
191
192                 if (total > 2) {
193                         path += (char) ('a' + this_one);
194                 } else {
195                         path += (char) (this_one == 0 ? 'L' : 'R');
196                 }
197         }
198
199         return path;
200 }
201
202 bool
203 path_is_paired (ustring path, ustring& pair_base)
204 {
205         ustring::size_type pos;
206
207         /* remove any leading path */
208
209         if ((pos = path.find_last_of ('/')) != string::npos) {
210                 path = path.substr(pos+1);
211         }
212
213         /* remove filename suffixes etc. */
214
215         if ((pos = path.find_last_of ('.')) != string::npos) {
216                 path = path.substr (0, pos);
217         }
218
219         ustring::size_type len = path.length();
220
221         /* look for possible channel identifier: "?R", "%R", ".L" etc. */
222
223         if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
224             (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
225
226                 pair_base = path.substr (0, len-2);
227                 return true;
228
229         }
230
231         return false;
232 }
233
234 ustring
235 path_expand (ustring 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-1);
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 (nframes_t 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         } else if (str == _("Lock Edit")) {
318                 return Lock;
319         }
320         fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
321         /*NOTREACHED*/
322         return Slide;
323 }
324
325 const char*
326 edit_mode_to_string (EditMode mode)
327 {
328         switch (mode) {
329         case Slide:
330                 return _("Slide Edit");
331
332         case Lock:
333                 return _("Lock Edit");
334
335         default:
336         case Splice:
337                 return _("Splice Edit");
338         }
339 }
340
341 SlaveSource
342 string_to_slave_source (string str)
343 {
344         if (str == _("Internal")) {
345                 return None;
346         }
347
348         if (str == _("MTC")) {
349                 return MTC;
350         }
351
352         if (str == _("MIDI Clock")) {
353                 return MIDIClock;
354         }
355
356         if (str == _("JACK")) {
357                 return JACK;
358         }
359
360         fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
361         /*NOTREACHED*/
362         return None;
363 }
364
365 const char*
366 slave_source_to_string (SlaveSource src)
367 {
368         switch (src) {
369         case JACK:
370                 return _("JACK");
371
372         case MTC:
373                 return _("MTC");
374
375         case MIDIClock:
376                 return _("MIDI Clock");
377
378         default:
379         case None:
380                 return _("Internal");
381
382         }
383 }
384
385 float
386 meter_falloff_to_float (MeterFalloff falloff)
387 {
388         switch (falloff) {
389         case MeterFalloffOff:
390                 return METER_FALLOFF_OFF;
391         case MeterFalloffSlowest:
392                 return METER_FALLOFF_SLOWEST;
393         case MeterFalloffSlow:
394                 return METER_FALLOFF_SLOW;
395         case MeterFalloffMedium:
396                 return METER_FALLOFF_MEDIUM;
397         case MeterFalloffFast:
398                 return METER_FALLOFF_FAST;
399         case MeterFalloffFaster:
400                 return METER_FALLOFF_FASTER;
401         case MeterFalloffFastest:
402                 return METER_FALLOFF_FASTEST;
403         default:
404                 return METER_FALLOFF_FAST;
405         }
406 }
407
408 MeterFalloff
409 meter_falloff_from_float (float val)
410 {
411         if (val == METER_FALLOFF_OFF) {
412                 return MeterFalloffOff;
413         }
414         else if (val <= METER_FALLOFF_SLOWEST) {
415                 return MeterFalloffSlowest;
416         }
417         else if (val <= METER_FALLOFF_SLOW) {
418                 return MeterFalloffSlow;
419         }
420         else if (val <= METER_FALLOFF_MEDIUM) {
421                 return MeterFalloffMedium;
422         }
423         else if (val <= METER_FALLOFF_FAST) {
424                 return MeterFalloffFast;
425         }
426         else if (val <= METER_FALLOFF_FASTER) {
427                 return MeterFalloffFaster;
428         }
429         else {
430                 return MeterFalloffFastest;
431         }
432 }
433
434 AutoState
435 ARDOUR::string_to_auto_state (std::string str)
436 {
437         if (str == X_("Off")) {
438                 return Off;
439         } else if (str == X_("Play")) {
440                 return Play;
441         } else if (str == X_("Write")) {
442                 return Write;
443         } else if (str == X_("Touch")) {
444                 return Touch;
445         }
446
447         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
448         /*NOTREACHED*/
449         return Touch;
450 }
451
452 string
453 ARDOUR::auto_state_to_string (AutoState as)
454 {
455         /* to be used only for XML serialization, no i18n done */
456
457         switch (as) {
458         case Off:
459                 return X_("Off");
460                 break;
461         case Play:
462                 return X_("Play");
463                 break;
464         case Write:
465                 return X_("Write");
466                 break;
467         case Touch:
468                 return X_("Touch");
469         }
470
471         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
472         /*NOTREACHED*/
473         return "";
474 }
475
476 AutoStyle
477 ARDOUR::string_to_auto_style (std::string str)
478 {
479         if (str == X_("Absolute")) {
480                 return Absolute;
481         } else if (str == X_("Trim")) {
482                 return Trim;
483         }
484
485         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
486         /*NOTREACHED*/
487         return Trim;
488 }
489
490 string
491 ARDOUR::auto_style_to_string (AutoStyle as)
492 {
493         /* to be used only for XML serialization, no i18n done */
494
495         switch (as) {
496         case Absolute:
497                 return X_("Absolute");
498                 break;
499         case Trim:
500                 return X_("Trim");
501                 break;
502         }
503
504         fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
505         /*NOTREACHED*/
506         return "";
507 }
508
509 extern "C" {
510         void c_stacktrace() { stacktrace (cerr); }
511 }