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.
21 #include <cstdio> /* for sprintf */
27 #include <sys/types.h>
37 #include <pbd/error.h>
38 #include <pbd/stacktrace.h>
39 #include <pbd/xml++.h>
40 #include <pbd/basename.h>
41 #include <ardour/utils.h>
45 using namespace ARDOUR;
51 elapsed_time_to_str (char *buf, uint32_t seconds)
60 days = s / (3600 * 24);
61 s -= (days * 3600 * 24);
68 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
72 hours > 1 ? "s" : "");
74 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
78 minutes > 1 ? "s" : "");
80 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
82 minutes > 1 ? "s" : "");
84 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
86 seconds > 1 ? "s" : "");
88 snprintf (buf, sizeof (buf), "no time");
93 legalize_for_path (ustring str)
95 ustring::size_type pos;
96 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
102 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
103 legal.replace (pos, 1, "_");
111 legalize_for_path (string str)
113 string::size_type pos;
114 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
120 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
121 legal.replace (pos, 1, "_");
130 operator<< (ostream& o, const BBT_Time& bbt)
132 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
137 find_named_node (const XMLNode& node, string name)
140 XMLNodeConstIterator niter;
143 nlist = node.children();
145 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
149 if (child->name() == name) {
158 cmp_nocase (const string& s, const string& s2)
160 string::const_iterator p = s.begin();
161 string::const_iterator p2 = s2.begin();
163 while (p != s.end() && p2 != s2.end()) {
164 if (toupper(*p) != toupper(*p2)) {
165 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
171 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
175 tokenize_fullpath (string fullpath, string& path, string& name)
177 string::size_type m = fullpath.find_last_of("/");
179 if (m == string::npos) {
185 // does it look like just a directory?
186 if (m == fullpath.length()-1) {
189 path = fullpath.substr(0, m+1);
191 string::size_type n = fullpath.find(".ardour", m);
193 if (n == string::npos) {
196 name = fullpath.substr(m+1, n - m - 1);
201 touch_file (ustring path)
203 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
212 region_name_from_path (ustring path, bool strip_channels)
214 path = PBD::basename_nosuffix (path);
216 if (strip_channels) {
218 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
220 ustring::size_type len = path.length();
222 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
223 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
225 path = path.substr (0, path.length() - 2);
233 path_is_paired (ustring path, ustring& pair_base)
235 ustring::size_type pos;
237 /* remove filename suffixes etc. */
239 if ((pos = path.find_last_of ('.')) != string::npos) {
240 path = path.substr (0, pos);
243 ustring::size_type len = path.length();
245 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
247 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
248 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
250 pair_base = path.substr (0, len-2);
259 path_expand (ustring path)
262 /* Handle tilde and environment variable expansion in session path */
266 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
270 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
274 if (expansion.we_wordc > 1) {
275 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
279 ret = expansion.we_wordv[0];
281 wordfree (&expansion);
289 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
291 CFStringRefToStdString(CFStringRef stringRef)
294 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
295 kCFStringEncodingUTF8);
296 char *buf = new char[size];
300 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
306 #endif // HAVE_COREAUDIO
309 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
317 for (nframes_t i = 1; i < nframes - 1; ++i) {
318 in[i] = in[i-1] + step;
323 const float pan_law_attenuation = -3.0f;
324 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
326 for (nframes_t n = 0; n < nframes; ++n) {
328 float outVal = 1 - inVal;
329 out[n] = outVal * (scale * outVal + 1.0f - scale);
330 in[n] = inVal * (scale * inVal + 1.0f - scale);
335 string_to_edit_mode (string str)
337 if (str == _("Splice Edit")) {
339 } else if (str == _("Slide Edit")) {
342 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
348 edit_mode_to_string (EditMode mode)
352 return _("Slide Edit");
356 return _("Splice Edit");
361 string_to_slave_source (string str)
363 if (str == _("Internal")) {
367 if (str == _("MTC")) {
371 if (str == _("JACK")) {
375 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
381 slave_source_to_string (SlaveSource src)
392 return _("Internal");
397 /* I don't really like hard-coding these falloff rates here
398 * Probably should use a map of some kind that could be configured
399 * These rates are db/sec.
402 #define METER_FALLOFF_OFF 0.0f
403 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
404 #define METER_FALLOFF_SLOW 8.6f // BBC standard
405 #define METER_FALLOFF_MEDIUM 20.0f
406 #define METER_FALLOFF_FAST 32.0f
407 #define METER_FALLOFF_FASTER 46.0f
408 #define METER_FALLOFF_FASTEST 70.0f
411 meter_falloff_to_float (MeterFalloff falloff)
414 case MeterFalloffOff:
415 return METER_FALLOFF_OFF;
416 case MeterFalloffSlowest:
417 return METER_FALLOFF_SLOWEST;
418 case MeterFalloffSlow:
419 return METER_FALLOFF_SLOW;
420 case MeterFalloffMedium:
421 return METER_FALLOFF_MEDIUM;
422 case MeterFalloffFast:
423 return METER_FALLOFF_FAST;
424 case MeterFalloffFaster:
425 return METER_FALLOFF_FASTER;
426 case MeterFalloffFastest:
427 return METER_FALLOFF_FASTEST;
429 return METER_FALLOFF_FAST;
434 meter_falloff_from_float (float val)
436 if (val == METER_FALLOFF_OFF) {
437 return MeterFalloffOff;
439 else if (val <= METER_FALLOFF_SLOWEST) {
440 return MeterFalloffSlowest;
442 else if (val <= METER_FALLOFF_SLOW) {
443 return MeterFalloffSlow;
445 else if (val <= METER_FALLOFF_MEDIUM) {
446 return MeterFalloffMedium;
448 else if (val <= METER_FALLOFF_FAST) {
449 return MeterFalloffFast;
451 else if (val <= METER_FALLOFF_FASTER) {
452 return MeterFalloffFaster;
455 return MeterFalloffFastest;
460 meter_hold_to_float (MeterHold hold)
467 case MeterHoldMedium:
476 ARDOUR::string_to_auto_state (std::string str)
478 if (str == X_("Off")) {
480 } else if (str == X_("Play")) {
482 } else if (str == X_("Write")) {
484 } else if (str == X_("Touch")) {
488 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
494 ARDOUR::auto_state_to_string (AutoState as)
496 /* to be used only for XML serialization, no i18n done */
512 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
518 ARDOUR::string_to_auto_style (std::string str)
520 if (str == X_("Absolute")) {
522 } else if (str == X_("Trim")) {
526 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
532 ARDOUR::auto_style_to_string (AutoStyle as)
534 /* to be used only for XML serialization, no i18n done */
538 return X_("Absolute");
545 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
551 void c_stacktrace() { stacktrace (cerr); }