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 legalize_for_path (ustring str)
55 ustring::size_type pos;
56 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
62 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
63 legal.replace (pos, 1, "_");
71 legalize_for_path (string str)
73 string::size_type pos;
74 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
80 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
81 legal.replace (pos, 1, "_");
90 operator<< (ostream& o, const BBT_Time& bbt)
92 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
97 find_named_node (const XMLNode& node, string name)
100 XMLNodeConstIterator niter;
103 nlist = node.children();
105 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
109 if (child->name() == name) {
118 cmp_nocase (const string& s, const string& s2)
120 string::const_iterator p = s.begin();
121 string::const_iterator p2 = s2.begin();
123 while (p != s.end() && p2 != s2.end()) {
124 if (toupper(*p) != toupper(*p2)) {
125 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
131 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
135 touch_file (ustring path)
137 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
146 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
148 path = PBD::basename_nosuffix (path);
150 if (strip_channels) {
152 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
154 ustring::size_type len = path.length();
156 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
157 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
159 path = path.substr (0, path.length() - 2);
163 if (add_channel_suffix) {
168 path += (char) ('a' + this_one);
170 path += (char) (this_one == 0 ? 'L' : 'R');
178 path_is_paired (ustring path, ustring& pair_base)
180 ustring::size_type pos;
182 /* remove any leading path */
184 if ((pos = path.find_last_of ('/')) != string::npos) {
185 path = path.substr(pos+1);
188 /* remove filename suffixes etc. */
190 if ((pos = path.find_last_of ('.')) != string::npos) {
191 path = path.substr (0, pos);
194 ustring::size_type len = path.length();
196 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
198 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
199 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
201 pair_base = path.substr (0, len-2);
210 path_expand (ustring path)
213 /* Handle tilde and environment variable expansion in session path */
217 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
221 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
225 if (expansion.we_wordc > 1) {
226 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
230 ret = expansion.we_wordv[0];
232 wordfree (&expansion);
240 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
242 CFStringRefToStdString(CFStringRef stringRef)
245 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
246 kCFStringEncodingUTF8);
247 char *buf = new char[size];
251 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
257 #endif // HAVE_COREAUDIO
260 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
268 for (nframes_t i = 1; i < nframes - 1; ++i) {
269 in[i] = in[i-1] + step;
274 const float pan_law_attenuation = -3.0f;
275 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
277 for (nframes_t n = 0; n < nframes; ++n) {
279 float outVal = 1 - inVal;
280 out[n] = outVal * (scale * outVal + 1.0f - scale);
281 in[n] = inVal * (scale * inVal + 1.0f - scale);
286 string_to_edit_mode (string str)
288 if (str == _("Splice Edit")) {
290 } else if (str == _("Slide Edit")) {
293 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
299 edit_mode_to_string (EditMode mode)
303 return _("Slide Edit");
307 return _("Splice Edit");
312 string_to_slave_source (string str)
314 if (str == _("Internal")) {
318 if (str == _("MTC")) {
322 if (str == _("JACK")) {
326 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
332 slave_source_to_string (SlaveSource src)
343 return _("Internal");
348 /* I don't really like hard-coding these falloff rates here
349 * Probably should use a map of some kind that could be configured
350 * These rates are db/sec.
353 #define METER_FALLOFF_OFF 0.0f
354 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
355 #define METER_FALLOFF_SLOW 8.6f // BBC standard
356 #define METER_FALLOFF_MEDIUM 20.0f
357 #define METER_FALLOFF_FAST 32.0f
358 #define METER_FALLOFF_FASTER 46.0f
359 #define METER_FALLOFF_FASTEST 70.0f
362 meter_falloff_to_float (MeterFalloff falloff)
365 case MeterFalloffOff:
366 return METER_FALLOFF_OFF;
367 case MeterFalloffSlowest:
368 return METER_FALLOFF_SLOWEST;
369 case MeterFalloffSlow:
370 return METER_FALLOFF_SLOW;
371 case MeterFalloffMedium:
372 return METER_FALLOFF_MEDIUM;
373 case MeterFalloffFast:
374 return METER_FALLOFF_FAST;
375 case MeterFalloffFaster:
376 return METER_FALLOFF_FASTER;
377 case MeterFalloffFastest:
378 return METER_FALLOFF_FASTEST;
380 return METER_FALLOFF_FAST;
385 meter_falloff_from_float (float val)
387 if (val == METER_FALLOFF_OFF) {
388 return MeterFalloffOff;
390 else if (val <= METER_FALLOFF_SLOWEST) {
391 return MeterFalloffSlowest;
393 else if (val <= METER_FALLOFF_SLOW) {
394 return MeterFalloffSlow;
396 else if (val <= METER_FALLOFF_MEDIUM) {
397 return MeterFalloffMedium;
399 else if (val <= METER_FALLOFF_FAST) {
400 return MeterFalloffFast;
402 else if (val <= METER_FALLOFF_FASTER) {
403 return MeterFalloffFaster;
406 return MeterFalloffFastest;
411 meter_hold_to_float (MeterHold hold)
418 case MeterHoldMedium:
427 ARDOUR::string_to_auto_state (std::string str)
429 if (str == X_("Off")) {
431 } else if (str == X_("Play")) {
433 } else if (str == X_("Write")) {
435 } else if (str == X_("Touch")) {
439 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
445 ARDOUR::auto_state_to_string (AutoState as)
447 /* to be used only for XML serialization, no i18n done */
463 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
469 ARDOUR::string_to_auto_style (std::string str)
471 if (str == X_("Absolute")) {
473 } else if (str == X_("Trim")) {
477 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
483 ARDOUR::auto_style_to_string (AutoStyle as)
485 /* to be used only for XML serialization, no i18n done */
489 return X_("Absolute");
496 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
502 void c_stacktrace() { stacktrace (cerr); }