2 Copyright (C) 2000-2003 Paul Davis
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.
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.
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.
20 #include <cstdio> /* for sprintf */
26 #include <sys/types.h>
36 #include <pbd/error.h>
37 #include <pbd/stacktrace.h>
38 #include <pbd/xml++.h>
39 #include <pbd/basename.h>
40 #include <ardour/utils.h>
44 using namespace ARDOUR;
50 elapsed_time_to_str (char *buf, uint32_t seconds)
59 days = s / (3600 * 24);
60 s -= (days * 3600 * 24);
67 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
71 hours > 1 ? "s" : "");
73 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
77 minutes > 1 ? "s" : "");
79 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
81 minutes > 1 ? "s" : "");
83 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
85 seconds > 1 ? "s" : "");
87 snprintf (buf, sizeof (buf), "no time");
92 legalize_for_path (ustring str)
94 ustring::size_type pos;
95 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
101 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
102 legal.replace (pos, 1, "_");
110 legalize_for_path (string str)
112 string::size_type pos;
113 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
119 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
120 legal.replace (pos, 1, "_");
129 operator<< (ostream& o, const BBT_Time& bbt)
131 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
136 find_named_node (const XMLNode& node, string name)
139 XMLNodeConstIterator niter;
142 nlist = node.children();
144 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
148 if (child->name() == name) {
157 cmp_nocase (const string& s, const string& s2)
159 string::const_iterator p = s.begin();
160 string::const_iterator p2 = s2.begin();
162 while (p != s.end() && p2 != s2.end()) {
163 if (toupper(*p) != toupper(*p2)) {
164 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
170 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
174 tokenize_fullpath (string fullpath, string& path, string& name)
176 string::size_type m = fullpath.find_last_of("/");
178 if (m == string::npos) {
184 // does it look like just a directory?
185 if (m == fullpath.length()-1) {
188 path = fullpath.substr(0, m+1);
190 string::size_type n = fullpath.find(".ardour", m);
192 if (n == string::npos) {
195 name = fullpath.substr(m+1, n - m - 1);
200 touch_file (ustring path)
202 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
211 region_name_from_path (ustring path, bool strip_channels)
213 path = PBD::basename_nosuffix (path);
215 if (strip_channels) {
217 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
219 ustring::size_type len = path.length();
221 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
222 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
224 path = path.substr (0, path.length() - 2);
232 path_is_paired (ustring path, ustring& pair_base)
234 ustring::size_type pos;
236 /* remove filename suffixes etc. */
238 if ((pos = path.find_last_of ('.')) != string::npos) {
239 path = path.substr (0, pos);
242 ustring::size_type len = path.length();
244 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
246 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
247 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
249 pair_base = path.substr (0, len-2);
258 path_expand (ustring path)
261 /* Handle tilde and environment variable expansion in session path */
265 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
269 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
273 if (expansion.we_wordc > 1) {
274 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
278 ret = expansion.we_wordv[0];
280 wordfree (&expansion);
288 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
290 CFStringRefToStdString(CFStringRef stringRef)
293 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
294 kCFStringEncodingUTF8);
295 char *buf = new char[size];
299 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
305 #endif // HAVE_COREAUDIO
308 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
316 for (nframes_t i = 1; i < nframes - 1; ++i) {
317 in[i] = in[i-1] + step;
322 const float pan_law_attenuation = -3.0f;
323 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
325 for (nframes_t n = 0; n < nframes; ++n) {
327 float outVal = 1 - inVal;
328 out[n] = outVal * (scale * outVal + 1.0f - scale);
329 in[n] = inVal * (scale * inVal + 1.0f - scale);
334 string_to_edit_mode (string str)
336 if (str == _("Splice Edit")) {
338 } else if (str == _("Slide Edit")) {
341 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
347 edit_mode_to_string (EditMode mode)
351 return _("Slide Edit");
355 return _("Splice Edit");
360 string_to_slave_source (string str)
362 if (str == _("Internal")) {
366 if (str == _("MTC")) {
370 if (str == _("JACK")) {
374 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
380 slave_source_to_string (SlaveSource src)
391 return _("Internal");
396 /* I don't really like hard-coding these falloff rates here
397 * Probably should use a map of some kind that could be configured
398 * These rates are db/sec.
401 #define METER_FALLOFF_OFF 0.0f
402 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
403 #define METER_FALLOFF_SLOW 8.6f // BBC standard
404 #define METER_FALLOFF_MEDIUM 20.0f
405 #define METER_FALLOFF_FAST 32.0f
406 #define METER_FALLOFF_FASTER 46.0f
407 #define METER_FALLOFF_FASTEST 70.0f
410 meter_falloff_to_float (MeterFalloff falloff)
413 case MeterFalloffOff:
414 return METER_FALLOFF_OFF;
415 case MeterFalloffSlowest:
416 return METER_FALLOFF_SLOWEST;
417 case MeterFalloffSlow:
418 return METER_FALLOFF_SLOW;
419 case MeterFalloffMedium:
420 return METER_FALLOFF_MEDIUM;
421 case MeterFalloffFast:
422 return METER_FALLOFF_FAST;
423 case MeterFalloffFaster:
424 return METER_FALLOFF_FASTER;
425 case MeterFalloffFastest:
426 return METER_FALLOFF_FASTEST;
428 return METER_FALLOFF_FAST;
433 meter_falloff_from_float (float val)
435 if (val == METER_FALLOFF_OFF) {
436 return MeterFalloffOff;
438 else if (val <= METER_FALLOFF_SLOWEST) {
439 return MeterFalloffSlowest;
441 else if (val <= METER_FALLOFF_SLOW) {
442 return MeterFalloffSlow;
444 else if (val <= METER_FALLOFF_MEDIUM) {
445 return MeterFalloffMedium;
447 else if (val <= METER_FALLOFF_FAST) {
448 return MeterFalloffFast;
450 else if (val <= METER_FALLOFF_FASTER) {
451 return MeterFalloffFaster;
454 return MeterFalloffFastest;
459 meter_hold_to_float (MeterHold hold)
466 case MeterHoldMedium:
475 ARDOUR::string_to_auto_state (std::string str)
477 if (str == X_("Off")) {
479 } else if (str == X_("Play")) {
481 } else if (str == X_("Write")) {
483 } else if (str == X_("Touch")) {
487 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
493 ARDOUR::auto_state_to_string (AutoState as)
495 /* to be used only for XML serialization, no i18n done */
511 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
517 ARDOUR::string_to_auto_style (std::string str)
519 if (str == X_("Absolute")) {
521 } else if (str == X_("Trim")) {
525 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
531 ARDOUR::auto_style_to_string (AutoStyle as)
533 /* to be used only for XML serialization, no i18n done */
537 return X_("Absolute");
544 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
550 void c_stacktrace() { stacktrace (cerr); }