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 <ardour/utils.h>
44 using namespace ARDOUR;
49 elapsed_time_to_str (char *buf, uint32_t seconds)
58 days = s / (3600 * 24);
59 s -= (days * 3600 * 24);
66 snprintf (buf, sizeof (buf), "%" PRIu32 " day%s %" PRIu32 " hour%s",
70 hours > 1 ? "s" : "");
72 snprintf (buf, sizeof (buf), "%" PRIu32 " hour%s %" PRIu32 " minute%s",
76 minutes > 1 ? "s" : "");
78 snprintf (buf, sizeof (buf), "%" PRIu32 " minute%s",
80 minutes > 1 ? "s" : "");
82 snprintf (buf, sizeof (buf), "%" PRIu32 " second%s",
84 seconds > 1 ? "s" : "");
86 snprintf (buf, sizeof (buf), "no time");
91 legalize_for_path (string str)
93 string::size_type pos;
94 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
100 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
101 legal.replace (pos, 1, "_");
109 operator<< (ostream& o, const BBT_Time& bbt)
111 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
116 find_named_node (const XMLNode& node, string name)
119 XMLNodeConstIterator niter;
122 nlist = node.children();
124 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
128 if (child->name() == name) {
137 cmp_nocase (const string& s, const string& s2)
139 string::const_iterator p = s.begin();
140 string::const_iterator p2 = s2.begin();
142 while (p != s.end() && p2 != s2.end()) {
143 if (toupper(*p) != toupper(*p2)) {
144 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
150 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
154 tokenize_fullpath (string fullpath, string& path, string& name)
156 string::size_type m = fullpath.find_last_of("/");
158 if (m == string::npos) {
164 // does it look like just a directory?
165 if (m == fullpath.length()-1) {
168 path = fullpath.substr(0, m+1);
170 string::size_type n = fullpath.find(".ardour", m);
172 if (n == string::npos) {
175 name = fullpath.substr(m+1, n - m - 1);
180 touch_file (string path)
182 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
191 placement_as_string (Placement p)
196 default: /* to get g++ to realize we have all the cases covered */
203 region_name_from_path (string path)
205 string::size_type pos;
207 /* remove filename suffixes etc. */
209 if ((pos = path.find_last_of ('.')) != string::npos) {
210 path = path.substr (0, pos);
213 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
215 string::size_type len = path.length();
217 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') &&
218 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
220 path = path.substr (0, path.length() - 2);
227 path_expand (string path)
230 /* Handle tilde and environment variable expansion in session path */
234 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
238 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
242 if (expansion.we_wordc > 1) {
243 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
247 ret = expansion.we_wordv[0];
249 wordfree (&expansion);
257 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
259 CFStringRefToStdString(CFStringRef stringRef)
262 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
263 kCFStringEncodingUTF8);
264 char *buf = new char[size];
268 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
274 #endif // HAVE_COREAUDIO
277 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
285 for (nframes_t i = 1; i < nframes - 1; ++i) {
286 in[i] = in[i-1] + step;
291 const float pan_law_attenuation = -3.0f;
292 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
294 for (nframes_t n = 0; n < nframes; ++n) {
296 float outVal = 1 - inVal;
297 out[n] = outVal * (scale * outVal + 1.0f - scale);
298 in[n] = inVal * (scale * inVal + 1.0f - scale);
303 string_to_edit_mode (string str)
305 if (str == _("Splice Edit")) {
307 } else if (str == _("Slide Edit")) {
310 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
316 edit_mode_to_string (EditMode mode)
320 return _("Slide Edit");
324 return _("Splice Edit");
329 string_to_slave_source (string str)
331 if (str == _("Internal")) {
335 if (str == _("MTC")) {
339 if (str == _("JACK")) {
343 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
349 slave_source_to_string (SlaveSource src)
360 return _("Internal");
366 meter_falloff_to_float (MeterFalloff falloff)
369 case MeterFalloffOff:
371 case MeterFalloffSlowest:
373 case MeterFalloffSlow:
375 case MeterFalloffMedium:
377 case MeterFalloffFast:
379 case MeterFalloffFaster:
381 case MeterFalloffFastest:
388 meter_hold_to_float (MeterHold hold)
395 case MeterHoldMedium:
404 ARDOUR::string_to_auto_state (std::string str)
406 if (str == X_("Off")) {
408 } else if (str == X_("Play")) {
410 } else if (str == X_("Write")) {
412 } else if (str == X_("Touch")) {
416 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
421 ARDOUR::auto_state_to_string (AutoState as)
423 /* to be used only for XML serialization, no i18n done */
441 ARDOUR::string_to_auto_style (std::string str)
443 if (str == X_("Absolute")) {
445 } else if (str == X_("Trim")) {
449 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
454 ARDOUR::auto_style_to_string (AutoStyle as)
456 /* to be used only for XML serialization, no i18n done */
460 return X_("Absolute");
469 void c_stacktrace() { stacktrace (cerr); }