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 "libardour-config.h"
24 #define __STDC_FORMAT_MACROS 1
27 #include <cstdio> /* for sprintf */
34 #include <sys/types.h>
44 #include "pbd/error.h"
45 #include "pbd/stacktrace.h"
46 #include "pbd/xml++.h"
47 #include "pbd/basename.h"
48 #include "ardour/utils.h"
52 using namespace ARDOUR;
58 legalize_for_path (ustring str)
60 ustring::size_type pos;
61 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
67 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
68 legal.replace (pos, 1, "_");
75 string bump_name_once(std::string name)
77 string::size_type period;
80 if ((period = name.find_last_of ('.')) == string::npos) {
85 const char *last_element = name.c_str() + period + 1;
86 for (size_t i = 0; i < strlen(last_element); i++) {
87 if (!isdigit(last_element[i])) {
94 long int version = strtol (name.c_str()+period+1, (char **)NULL, 10);
96 if (isnumber == 0 || errno != 0) {
97 // last_element is not a number, or is too large
103 snprintf (buf, sizeof(buf), "%ld", version+1);
105 newname = name.substr (0, period+1);
115 operator<< (ostream& o, const BBT_Time& bbt)
117 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
122 find_named_node (const XMLNode& node, string name)
125 XMLNodeConstIterator niter;
128 nlist = node.children();
130 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
134 if (child->name() == name) {
143 cmp_nocase (const string& s, const string& s2)
145 string::const_iterator p = s.begin();
146 string::const_iterator p2 = s2.begin();
148 while (p != s.end() && p2 != s2.end()) {
149 if (toupper(*p) != toupper(*p2)) {
150 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
156 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
160 touch_file (ustring path)
162 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
171 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
173 path = PBD::basename_nosuffix (path);
175 if (strip_channels) {
177 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
179 ustring::size_type len = path.length();
181 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
182 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
184 path = path.substr (0, path.length() - 2);
188 if (add_channel_suffix) {
193 path += (char) ('a' + this_one);
195 path += (char) (this_one == 0 ? 'L' : 'R');
203 path_is_paired (ustring path, ustring& pair_base)
205 ustring::size_type pos;
207 /* remove any leading path */
209 if ((pos = path.find_last_of ('/')) != string::npos) {
210 path = path.substr(pos+1);
213 /* remove filename suffixes etc. */
215 if ((pos = path.find_last_of ('.')) != string::npos) {
216 path = path.substr (0, pos);
219 ustring::size_type len = path.length();
221 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
223 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
224 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
226 pair_base = path.substr (0, len-2);
235 path_expand (ustring path)
238 /* Handle tilde and environment variable expansion in session path */
242 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
246 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
250 if (expansion.we_wordc > 1) {
251 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
255 ret = expansion.we_wordv[0];
257 wordfree (&expansion);
265 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
267 CFStringRefToStdString(CFStringRef stringRef)
270 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
271 kCFStringEncodingUTF8);
272 char *buf = new char[size];
276 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
282 #endif // HAVE_COREAUDIO
285 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
289 step = 1.0/(nframes-1);
293 for (nframes_t i = 1; i < nframes - 1; ++i) {
294 in[i] = in[i-1] + step;
299 const float pan_law_attenuation = -3.0f;
300 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
302 for (nframes_t n = 0; n < nframes; ++n) {
304 float outVal = 1 - inVal;
305 out[n] = outVal * (scale * outVal + 1.0f - scale);
306 in[n] = inVal * (scale * inVal + 1.0f - scale);
311 string_to_edit_mode (string str)
313 if (str == _("Splice Edit")) {
315 } else if (str == _("Slide Edit")) {
317 } else if (str == _("Lock Edit")) {
320 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
326 edit_mode_to_string (EditMode mode)
330 return _("Slide Edit");
333 return _("Lock Edit");
337 return _("Splice Edit");
342 string_to_slave_source (string str)
344 if (str == _("Internal")) {
348 if (str == _("MTC")) {
352 if (str == _("MIDI Clock")) {
356 if (str == _("JACK")) {
360 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
366 slave_source_to_string (SlaveSource src)
376 return _("MIDI Clock");
380 return _("Internal");
386 meter_falloff_to_float (MeterFalloff falloff)
389 case MeterFalloffOff:
390 return METER_FALLOFF_OFF;
391 case MeterFalloffSlowest:
392 return METER_FALLOFF_SLOWEST;
393 case MeterFalloffSlow:
394 return METER_FALLOFF_SLOW;
395 case MeterFalloffMedium:
396 return METER_FALLOFF_MEDIUM;
397 case MeterFalloffFast:
398 return METER_FALLOFF_FAST;
399 case MeterFalloffFaster:
400 return METER_FALLOFF_FASTER;
401 case MeterFalloffFastest:
402 return METER_FALLOFF_FASTEST;
404 return METER_FALLOFF_FAST;
409 meter_falloff_from_float (float val)
411 if (val == METER_FALLOFF_OFF) {
412 return MeterFalloffOff;
414 else if (val <= METER_FALLOFF_SLOWEST) {
415 return MeterFalloffSlowest;
417 else if (val <= METER_FALLOFF_SLOW) {
418 return MeterFalloffSlow;
420 else if (val <= METER_FALLOFF_MEDIUM) {
421 return MeterFalloffMedium;
423 else if (val <= METER_FALLOFF_FAST) {
424 return MeterFalloffFast;
426 else if (val <= METER_FALLOFF_FASTER) {
427 return MeterFalloffFaster;
430 return MeterFalloffFastest;
435 ARDOUR::string_to_auto_state (std::string str)
437 if (str == X_("Off")) {
439 } else if (str == X_("Play")) {
441 } else if (str == X_("Write")) {
443 } else if (str == X_("Touch")) {
447 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
453 ARDOUR::auto_state_to_string (AutoState as)
455 /* to be used only for XML serialization, no i18n done */
471 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
477 ARDOUR::string_to_auto_style (std::string str)
479 if (str == X_("Absolute")) {
481 } else if (str == X_("Trim")) {
485 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
491 ARDOUR::auto_style_to_string (AutoStyle as)
493 /* to be used only for XML serialization, no i18n done */
497 return X_("Absolute");
504 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
510 bool_as_string (bool yn)
512 return (yn ? "yes" : "no");
516 string_is_affirmative (const std::string& str)
518 /* to be used only with XML data - not intended to handle user input */
520 return str == "1" || str == "y" || str == "Y" || (!g_strncasecmp(str.c_str(), "yes", str.length()));
524 void c_stacktrace() { stacktrace (cerr); }