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 <ardour/utils.h>
47 using namespace ARDOUR;
52 elapsed_time_to_str (char *buf, uint32_t seconds)
61 days = s / (3600 * 24);
62 s -= (days * 3600 * 24);
69 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
73 hours > 1 ? "s" : "");
75 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
79 minutes > 1 ? "s" : "");
81 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
83 minutes > 1 ? "s" : "");
85 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
87 seconds > 1 ? "s" : "");
89 snprintf (buf, sizeof (buf), "no time");
94 legalize_for_path (string str)
96 string::size_type pos;
97 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
103 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
104 legal.replace (pos, 1, "_");
112 operator<< (ostream& o, const BBT_Time& bbt)
114 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
119 find_named_node (const XMLNode& node, string name)
122 XMLNodeConstIterator niter;
125 nlist = node.children();
127 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
131 if (child->name() == name) {
140 cmp_nocase (const string& s, const string& s2)
142 string::const_iterator p = s.begin();
143 string::const_iterator p2 = s2.begin();
145 while (p != s.end() && p2 != s2.end()) {
146 if (toupper(*p) != toupper(*p2)) {
147 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
153 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
157 tokenize_fullpath (string fullpath, string& path, string& name)
159 string::size_type m = fullpath.find_last_of("/");
161 if (m == string::npos) {
167 // does it look like just a directory?
168 if (m == fullpath.length()-1) {
171 path = fullpath.substr(0, m+1);
173 string::size_type n = fullpath.find(".ardour", m);
175 if (n == string::npos) {
178 name = fullpath.substr(m+1, n - m - 1);
183 touch_file (string path)
185 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
194 placement_as_string (Placement p)
199 default: /* to get g++ to realize we have all the cases covered */
206 region_name_from_path (string path)
208 string::size_type pos;
210 /* remove any leading path */
212 if ((pos = path.find_last_of ('/')) != string::npos) {
213 path = path.substr(pos+1);
216 /* remove filename suffixes etc. */
218 if ((pos = path.find_last_of ('.')) != string::npos) {
219 path = path.substr (0, pos);
222 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
224 string::size_type len = path.length();
226 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') &&
227 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
229 path = path.substr (0, path.length() - 2);
236 path_expand (string path)
239 /* Handle tilde and environment variable expansion in session path */
243 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
247 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
251 if (expansion.we_wordc > 1) {
252 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
256 ret = expansion.we_wordv[0];
258 wordfree (&expansion);
266 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
268 CFStringRefToStdString(CFStringRef stringRef)
271 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
272 kCFStringEncodingUTF8);
273 char *buf = new char[size];
277 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
283 #endif // HAVE_COREAUDIO
286 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
294 for (nframes_t i = 1; i < nframes - 1; ++i) {
295 in[i] = in[i-1] + step;
300 const float pan_law_attenuation = -3.0f;
301 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
303 for (nframes_t n = 0; n < nframes; ++n) {
305 float outVal = 1 - inVal;
306 out[n] = outVal * (scale * outVal + 1.0f - scale);
307 in[n] = inVal * (scale * inVal + 1.0f - scale);
312 string_to_edit_mode (string str)
314 if (str == _("Splice Edit")) {
316 } else if (str == _("Slide Edit")) {
319 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
325 edit_mode_to_string (EditMode mode)
329 return _("Slide Edit");
333 return _("Splice Edit");
338 string_to_slave_source (string str)
340 if (str == _("Internal")) {
344 if (str == _("MTC")) {
348 if (str == _("JACK")) {
352 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
358 slave_source_to_string (SlaveSource src)
369 return _("Internal");
375 meter_falloff_to_float (MeterFalloff falloff)
378 case MeterFalloffOff:
380 case MeterFalloffSlowest:
382 case MeterFalloffSlow:
384 case MeterFalloffMedium:
386 case MeterFalloffFast:
388 case MeterFalloffFaster:
390 case MeterFalloffFastest:
397 meter_hold_to_float (MeterHold hold)
404 case MeterHoldMedium:
413 ARDOUR::string_to_auto_state (std::string str)
415 if (str == X_("Off")) {
417 } else if (str == X_("Play")) {
419 } else if (str == X_("Write")) {
421 } else if (str == X_("Touch")) {
425 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
431 ARDOUR::auto_state_to_string (AutoState as)
433 /* to be used only for XML serialization, no i18n done */
449 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
455 ARDOUR::string_to_auto_style (std::string str)
457 if (str == X_("Absolute")) {
459 } else if (str == X_("Trim")) {
463 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
469 ARDOUR::auto_style_to_string (AutoStyle as)
471 /* to be used only for XML serialization, no i18n done */
475 return X_("Absolute");
482 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
488 void c_stacktrace() { stacktrace (cerr); }