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 #define __STDC_FORMAT_MACROS 1
24 #include <cstdio> /* for sprintf */
30 #include <sys/types.h>
40 #include <pbd/error.h>
41 #include <pbd/stacktrace.h>
42 #include <pbd/xml++.h>
43 #include <pbd/basename.h>
44 #include <ardour/utils.h>
48 using namespace ARDOUR;
54 elapsed_time_to_str (char *buf, uint32_t seconds)
63 days = s / (3600 * 24);
64 s -= (days * 3600 * 24);
71 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
75 hours > 1 ? "s" : "");
77 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
81 minutes > 1 ? "s" : "");
83 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
85 minutes > 1 ? "s" : "");
87 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
89 seconds > 1 ? "s" : "");
91 snprintf (buf, sizeof (buf), "no time");
96 legalize_for_path (ustring str)
98 ustring::size_type pos;
99 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
105 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
106 legal.replace (pos, 1, "_");
114 legalize_for_path (string str)
116 string::size_type pos;
117 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
123 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
124 legal.replace (pos, 1, "_");
133 operator<< (ostream& o, const BBT_Time& bbt)
135 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
140 find_named_node (const XMLNode& node, string name)
143 XMLNodeConstIterator niter;
146 nlist = node.children();
148 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
152 if (child->name() == name) {
161 cmp_nocase (const string& s, const string& s2)
163 string::const_iterator p = s.begin();
164 string::const_iterator p2 = s2.begin();
166 while (p != s.end() && p2 != s2.end()) {
167 if (toupper(*p) != toupper(*p2)) {
168 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
174 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
178 tokenize_fullpath (string fullpath, string& path, string& name)
180 string::size_type m = fullpath.find_last_of("/");
182 if (m == string::npos) {
188 // does it look like just a directory?
189 if (m == fullpath.length()-1) {
192 path = fullpath.substr(0, m+1);
194 string::size_type n = fullpath.find(".ardour", m);
196 if (n == string::npos) {
199 name = fullpath.substr(m+1, n - m - 1);
204 touch_file (ustring path)
206 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
215 region_name_from_path (ustring path, bool strip_channels)
217 path = PBD::basename_nosuffix (path);
219 if (strip_channels) {
221 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
223 ustring::size_type len = path.length();
225 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
226 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
228 path = path.substr (0, path.length() - 2);
236 path_is_paired (ustring path, ustring& pair_base)
238 ustring::size_type pos;
240 /* remove any leading path */
242 if ((pos = path.find_last_of ('/')) != string::npos) {
243 path = path.substr(pos+1);
246 /* remove filename suffixes etc. */
248 if ((pos = path.find_last_of ('.')) != string::npos) {
249 path = path.substr (0, pos);
252 ustring::size_type len = path.length();
254 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
256 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
257 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
259 pair_base = path.substr (0, len-2);
268 path_expand (ustring path)
271 /* Handle tilde and environment variable expansion in session path */
275 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
279 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
283 if (expansion.we_wordc > 1) {
284 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
288 ret = expansion.we_wordv[0];
290 wordfree (&expansion);
298 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
300 CFStringRefToStdString(CFStringRef stringRef)
303 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
304 kCFStringEncodingUTF8);
305 char *buf = new char[size];
309 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
315 #endif // HAVE_COREAUDIO
318 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
326 for (nframes_t i = 1; i < nframes - 1; ++i) {
327 in[i] = in[i-1] + step;
332 const float pan_law_attenuation = -3.0f;
333 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
335 for (nframes_t n = 0; n < nframes; ++n) {
337 float outVal = 1 - inVal;
338 out[n] = outVal * (scale * outVal + 1.0f - scale);
339 in[n] = inVal * (scale * inVal + 1.0f - scale);
344 string_to_edit_mode (string str)
346 if (str == _("Splice Edit")) {
348 } else if (str == _("Slide Edit")) {
351 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
357 edit_mode_to_string (EditMode mode)
361 return _("Slide Edit");
365 return _("Splice Edit");
370 string_to_slave_source (string str)
372 if (str == _("Internal")) {
376 if (str == _("MTC")) {
380 if (str == _("JACK")) {
384 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
390 slave_source_to_string (SlaveSource src)
401 return _("Internal");
406 /* I don't really like hard-coding these falloff rates here
407 * Probably should use a map of some kind that could be configured
408 * These rates are db/sec.
411 #define METER_FALLOFF_OFF 0.0f
412 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
413 #define METER_FALLOFF_SLOW 8.6f // BBC standard
414 #define METER_FALLOFF_MEDIUM 20.0f
415 #define METER_FALLOFF_FAST 32.0f
416 #define METER_FALLOFF_FASTER 46.0f
417 #define METER_FALLOFF_FASTEST 70.0f
420 meter_falloff_to_float (MeterFalloff falloff)
423 case MeterFalloffOff:
424 return METER_FALLOFF_OFF;
425 case MeterFalloffSlowest:
426 return METER_FALLOFF_SLOWEST;
427 case MeterFalloffSlow:
428 return METER_FALLOFF_SLOW;
429 case MeterFalloffMedium:
430 return METER_FALLOFF_MEDIUM;
431 case MeterFalloffFast:
432 return METER_FALLOFF_FAST;
433 case MeterFalloffFaster:
434 return METER_FALLOFF_FASTER;
435 case MeterFalloffFastest:
436 return METER_FALLOFF_FASTEST;
438 return METER_FALLOFF_FAST;
443 meter_falloff_from_float (float val)
445 if (val == METER_FALLOFF_OFF) {
446 return MeterFalloffOff;
448 else if (val <= METER_FALLOFF_SLOWEST) {
449 return MeterFalloffSlowest;
451 else if (val <= METER_FALLOFF_SLOW) {
452 return MeterFalloffSlow;
454 else if (val <= METER_FALLOFF_MEDIUM) {
455 return MeterFalloffMedium;
457 else if (val <= METER_FALLOFF_FAST) {
458 return MeterFalloffFast;
460 else if (val <= METER_FALLOFF_FASTER) {
461 return MeterFalloffFaster;
464 return MeterFalloffFastest;
469 meter_hold_to_float (MeterHold hold)
476 case MeterHoldMedium:
485 ARDOUR::string_to_auto_state (std::string str)
487 if (str == X_("Off")) {
489 } else if (str == X_("Play")) {
491 } else if (str == X_("Write")) {
493 } else if (str == X_("Touch")) {
497 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
503 ARDOUR::auto_state_to_string (AutoState as)
505 /* to be used only for XML serialization, no i18n done */
521 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
527 ARDOUR::string_to_auto_style (std::string str)
529 if (str == X_("Absolute")) {
531 } else if (str == X_("Trim")) {
535 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
541 ARDOUR::auto_style_to_string (AutoStyle as)
543 /* to be used only for XML serialization, no i18n done */
547 return X_("Absolute");
554 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
560 void c_stacktrace() { stacktrace (cerr); }