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 #define __STDC_FORMAT_MACROS 1
23 #include <cstdio> /* for sprintf */
29 #include <sys/types.h>
39 #include <pbd/error.h>
40 #include <pbd/stacktrace.h>
41 #include <pbd/xml++.h>
42 #include <pbd/basename.h>
43 #include <ardour/utils.h>
47 using namespace ARDOUR;
53 elapsed_time_to_str (char *buf, uint32_t seconds)
62 days = s / (3600 * 24);
63 s -= (days * 3600 * 24);
70 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
74 hours > 1 ? "s" : "");
76 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
80 minutes > 1 ? "s" : "");
82 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
84 minutes > 1 ? "s" : "");
86 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
88 seconds > 1 ? "s" : "");
90 snprintf (buf, sizeof (buf), "no time");
95 legalize_for_path (ustring str)
97 ustring::size_type pos;
98 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
104 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
105 legal.replace (pos, 1, "_");
113 legalize_for_path (string str)
115 string::size_type pos;
116 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
122 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
123 legal.replace (pos, 1, "_");
132 operator<< (ostream& o, const BBT_Time& bbt)
134 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
139 find_named_node (const XMLNode& node, string name)
142 XMLNodeConstIterator niter;
145 nlist = node.children();
147 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
151 if (child->name() == name) {
160 cmp_nocase (const string& s, const string& s2)
162 string::const_iterator p = s.begin();
163 string::const_iterator p2 = s2.begin();
165 while (p != s.end() && p2 != s2.end()) {
166 if (toupper(*p) != toupper(*p2)) {
167 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
173 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
177 tokenize_fullpath (string fullpath, string& path, string& name)
179 string::size_type m = fullpath.find_last_of("/");
181 if (m == string::npos) {
187 // does it look like just a directory?
188 if (m == fullpath.length()-1) {
191 path = fullpath.substr(0, m+1);
193 string::size_type n = fullpath.find(".ardour", m);
195 if (n == string::npos) {
198 name = fullpath.substr(m+1, n - m - 1);
203 touch_file (ustring path)
205 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
214 region_name_from_path (ustring path, bool strip_channels)
216 path = PBD::basename_nosuffix (path);
218 if (strip_channels) {
220 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
222 ustring::size_type len = path.length();
224 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
225 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
227 path = path.substr (0, path.length() - 2);
235 path_is_paired (ustring path, ustring& pair_base)
237 ustring::size_type pos;
239 /* remove any leading path */
241 if ((pos = path.find_last_of ('/')) != string::npos) {
242 path = path.substr(pos+1);
245 /* remove filename suffixes etc. */
247 if ((pos = path.find_last_of ('.')) != string::npos) {
248 path = path.substr (0, pos);
251 ustring::size_type len = path.length();
253 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
255 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
256 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
258 pair_base = path.substr (0, len-2);
267 path_expand (ustring path)
270 /* Handle tilde and environment variable expansion in session path */
274 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
278 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
282 if (expansion.we_wordc > 1) {
283 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
287 ret = expansion.we_wordv[0];
289 wordfree (&expansion);
297 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
299 CFStringRefToStdString(CFStringRef stringRef)
302 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
303 kCFStringEncodingUTF8);
304 char *buf = new char[size];
308 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
314 #endif // HAVE_COREAUDIO
317 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
325 for (nframes_t i = 1; i < nframes - 1; ++i) {
326 in[i] = in[i-1] + step;
331 const float pan_law_attenuation = -3.0f;
332 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
334 for (nframes_t n = 0; n < nframes; ++n) {
336 float outVal = 1 - inVal;
337 out[n] = outVal * (scale * outVal + 1.0f - scale);
338 in[n] = inVal * (scale * inVal + 1.0f - scale);
343 string_to_edit_mode (string str)
345 if (str == _("Splice Edit")) {
347 } else if (str == _("Slide Edit")) {
350 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
356 edit_mode_to_string (EditMode mode)
360 return _("Slide Edit");
364 return _("Splice Edit");
369 string_to_slave_source (string str)
371 if (str == _("Internal")) {
375 if (str == _("MTC")) {
379 if (str == _("JACK")) {
383 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
389 slave_source_to_string (SlaveSource src)
400 return _("Internal");
405 /* I don't really like hard-coding these falloff rates here
406 * Probably should use a map of some kind that could be configured
407 * These rates are db/sec.
410 #define METER_FALLOFF_OFF 0.0f
411 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
412 #define METER_FALLOFF_SLOW 8.6f // BBC standard
413 #define METER_FALLOFF_MEDIUM 20.0f
414 #define METER_FALLOFF_FAST 32.0f
415 #define METER_FALLOFF_FASTER 46.0f
416 #define METER_FALLOFF_FASTEST 70.0f
419 meter_falloff_to_float (MeterFalloff falloff)
422 case MeterFalloffOff:
423 return METER_FALLOFF_OFF;
424 case MeterFalloffSlowest:
425 return METER_FALLOFF_SLOWEST;
426 case MeterFalloffSlow:
427 return METER_FALLOFF_SLOW;
428 case MeterFalloffMedium:
429 return METER_FALLOFF_MEDIUM;
430 case MeterFalloffFast:
431 return METER_FALLOFF_FAST;
432 case MeterFalloffFaster:
433 return METER_FALLOFF_FASTER;
434 case MeterFalloffFastest:
435 return METER_FALLOFF_FASTEST;
437 return METER_FALLOFF_FAST;
442 meter_falloff_from_float (float val)
444 if (val == METER_FALLOFF_OFF) {
445 return MeterFalloffOff;
447 else if (val <= METER_FALLOFF_SLOWEST) {
448 return MeterFalloffSlowest;
450 else if (val <= METER_FALLOFF_SLOW) {
451 return MeterFalloffSlow;
453 else if (val <= METER_FALLOFF_MEDIUM) {
454 return MeterFalloffMedium;
456 else if (val <= METER_FALLOFF_FAST) {
457 return MeterFalloffFast;
459 else if (val <= METER_FALLOFF_FASTER) {
460 return MeterFalloffFaster;
463 return MeterFalloffFastest;
468 meter_hold_to_float (MeterHold hold)
475 case MeterHoldMedium:
484 ARDOUR::string_to_auto_state (std::string str)
486 if (str == X_("Off")) {
488 } else if (str == X_("Play")) {
490 } else if (str == X_("Write")) {
492 } else if (str == X_("Touch")) {
496 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
502 ARDOUR::auto_state_to_string (AutoState as)
504 /* to be used only for XML serialization, no i18n done */
520 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
526 ARDOUR::string_to_auto_style (std::string str)
528 if (str == X_("Absolute")) {
530 } else if (str == X_("Trim")) {
534 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
540 ARDOUR::auto_style_to_string (AutoStyle as)
542 /* to be used only for XML serialization, no i18n done */
546 return X_("Absolute");
553 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
559 void c_stacktrace() { stacktrace (cerr); }