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, "_");
128 string bump_name_once(std::string name)
130 string::size_type period;
133 if ((period = name.find_last_of ('.')) == string::npos) {
138 const char *last_element = name.c_str() + period + 1;
139 for (size_t i = 0; i < strlen(last_element); i++) {
140 if (!isdigit(last_element[i])) {
147 long int version = strtol (name.c_str()+period+1, (char **)NULL, 10);
149 if (isnumber == 0 || errno != 0) {
150 // last_element is not a number, or is too large
156 snprintf (buf, sizeof(buf), "%ld", version+1);
158 newname = name.substr (0, period+1);
168 operator<< (ostream& o, const BBT_Time& bbt)
170 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
175 find_named_node (const XMLNode& node, string name)
178 XMLNodeConstIterator niter;
181 nlist = node.children();
183 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
187 if (child->name() == name) {
196 cmp_nocase (const string& s, const string& s2)
198 string::const_iterator p = s.begin();
199 string::const_iterator p2 = s2.begin();
201 while (p != s.end() && p2 != s2.end()) {
202 if (toupper(*p) != toupper(*p2)) {
203 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
209 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
213 tokenize_fullpath (string fullpath, string& path, string& name)
215 string::size_type m = fullpath.find_last_of("/");
217 if (m == string::npos) {
223 // does it look like just a directory?
224 if (m == fullpath.length()-1) {
227 path = fullpath.substr(0, m+1);
229 string::size_type n = fullpath.find(".ardour", m);
231 if (n == string::npos) {
234 name = fullpath.substr(m+1, n - m - 1);
239 touch_file (ustring path)
241 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
250 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
252 path = PBD::basename_nosuffix (path);
254 if (strip_channels) {
256 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
258 ustring::size_type len = path.length();
260 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
261 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
263 path = path.substr (0, path.length() - 2);
267 if (add_channel_suffix) {
272 path += (char) ('a' + this_one);
274 path += (char) (this_one == 0 ? 'L' : 'R');
282 path_is_paired (ustring path, ustring& pair_base)
284 ustring::size_type pos;
286 /* remove filename suffixes etc. */
288 if ((pos = path.find_last_of ('.')) != string::npos) {
289 path = path.substr (0, pos);
292 ustring::size_type len = path.length();
294 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
296 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
297 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
299 pair_base = path.substr (0, len-2);
308 path_expand (ustring path)
311 /* Handle tilde and environment variable expansion in session path */
315 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
319 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
323 if (expansion.we_wordc > 1) {
324 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
328 ret = expansion.we_wordv[0];
330 wordfree (&expansion);
338 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
340 CFStringRefToStdString(CFStringRef stringRef)
343 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
344 kCFStringEncodingUTF8);
345 char *buf = new char[size];
349 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
355 #endif // HAVE_COREAUDIO
358 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
366 for (nframes_t i = 1; i < nframes - 1; ++i) {
367 in[i] = in[i-1] + step;
372 const float pan_law_attenuation = -3.0f;
373 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
375 for (nframes_t n = 0; n < nframes; ++n) {
377 float outVal = 1 - inVal;
378 out[n] = outVal * (scale * outVal + 1.0f - scale);
379 in[n] = inVal * (scale * inVal + 1.0f - scale);
384 string_to_edit_mode (string str)
386 if (str == _("Splice Edit")) {
388 } else if (str == _("Slide Edit")) {
391 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
397 edit_mode_to_string (EditMode mode)
401 return _("Slide Edit");
405 return _("Splice Edit");
410 string_to_slave_source (string str)
412 if (str == _("Internal")) {
416 if (str == _("MTC")) {
420 if (str == _("JACK")) {
424 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
430 slave_source_to_string (SlaveSource src)
441 return _("Internal");
446 /* I don't really like hard-coding these falloff rates here
447 * Probably should use a map of some kind that could be configured
448 * These rates are db/sec.
451 #define METER_FALLOFF_OFF 0.0f
452 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
453 #define METER_FALLOFF_SLOW 8.6f // BBC standard
454 #define METER_FALLOFF_MEDIUM 20.0f
455 #define METER_FALLOFF_FAST 32.0f
456 #define METER_FALLOFF_FASTER 46.0f
457 #define METER_FALLOFF_FASTEST 70.0f
460 meter_falloff_to_float (MeterFalloff falloff)
463 case MeterFalloffOff:
464 return METER_FALLOFF_OFF;
465 case MeterFalloffSlowest:
466 return METER_FALLOFF_SLOWEST;
467 case MeterFalloffSlow:
468 return METER_FALLOFF_SLOW;
469 case MeterFalloffMedium:
470 return METER_FALLOFF_MEDIUM;
471 case MeterFalloffFast:
472 return METER_FALLOFF_FAST;
473 case MeterFalloffFaster:
474 return METER_FALLOFF_FASTER;
475 case MeterFalloffFastest:
476 return METER_FALLOFF_FASTEST;
478 return METER_FALLOFF_FAST;
483 meter_falloff_from_float (float val)
485 if (val == METER_FALLOFF_OFF) {
486 return MeterFalloffOff;
488 else if (val <= METER_FALLOFF_SLOWEST) {
489 return MeterFalloffSlowest;
491 else if (val <= METER_FALLOFF_SLOW) {
492 return MeterFalloffSlow;
494 else if (val <= METER_FALLOFF_MEDIUM) {
495 return MeterFalloffMedium;
497 else if (val <= METER_FALLOFF_FAST) {
498 return MeterFalloffFast;
500 else if (val <= METER_FALLOFF_FASTER) {
501 return MeterFalloffFaster;
504 return MeterFalloffFastest;
509 meter_hold_to_float (MeterHold hold)
516 case MeterHoldMedium:
525 ARDOUR::string_to_auto_state (std::string str)
527 if (str == X_("Off")) {
529 } else if (str == X_("Play")) {
531 } else if (str == X_("Write")) {
533 } else if (str == X_("Touch")) {
537 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
543 ARDOUR::auto_state_to_string (AutoState as)
545 /* to be used only for XML serialization, no i18n done */
561 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
567 ARDOUR::string_to_auto_style (std::string str)
569 if (str == X_("Absolute")) {
571 } else if (str == X_("Trim")) {
575 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
581 ARDOUR::auto_style_to_string (AutoStyle as)
583 /* to be used only for XML serialization, no i18n done */
587 return X_("Absolute");
594 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
600 void c_stacktrace() { stacktrace (cerr); }