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"
26 #include <cstdio> /* for sprintf */
35 #include <sys/types.h>
44 #include "pbd/gstdio_compat.h"
46 #include <glibmm/miscutils.h>
47 #include <glibmm/fileutils.h>
50 #include "pbd/error.h"
51 #include "pbd/stacktrace.h"
52 #include "pbd/xml++.h"
53 #include "pbd/basename.h"
54 #include "pbd/strsplit.h"
55 #include "pbd/replace_all.h"
57 #include "ardour/utils.h"
58 #include "ardour/rc_configuration.h"
62 using namespace ARDOUR;
67 replace_chars (const string& str, const string& illegal_chars)
69 string::size_type pos;
72 /* this is the one place in Ardour where we need to iterate across
73 * potential multibyte characters, and thus we need Glib::ustring
79 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
80 legal.replace (pos, 1, "_");
84 return string (legal);
86 /** take an arbitrary string as an argument, and return a version of it
87 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
88 * and later version of this code. It defines a very small number of characters
89 * that are not allowed in a path on the build target filesystem (basically,
90 * POSIX or Windows) and replaces any instances of them with an underscore.
92 * NOTE: this is intended only to legalize for the filesystem that Ardour
93 * is running on. Export should use legalize_for_universal_path() since
94 * the goal there is to be legal across filesystems.
97 ARDOUR::legalize_for_path (const string& str)
99 return replace_chars (str, "/\\");
102 /** take an arbitrary string as an argument, and return a version of it
103 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
104 * and later version of this code. It defines a small number
105 * of characters that are not allowed in a path on any of our target
106 * filesystems, and replaces any instances of them with an underscore.
108 * NOTE: this is intended to create paths that should be legal on
112 ARDOUR::legalize_for_universal_path (const string& str)
114 return replace_chars (str, "<>:\"/\\|?*");
117 /** Legalize for a URI path component. This is like
118 * legalize_for_universal_path, but stricter, disallowing spaces and hash.
119 * This avoids %20 escapes in URIs, but probably needs work to be more strictly
123 ARDOUR::legalize_for_uri (const string& str)
125 return replace_chars (str, "<>:\"/\\|?* #");
128 /** take an arbitrary string as an argument, and return a version of it
129 * suitable for use as a path (directory/folder name). This is the Ardour 2.X
130 * version of this code, which used an approach that came to be seen as
131 * problematic: defining the characters that were allowed and replacing all
132 * others with underscores. See legalize_for_path() for the 3.X and later
137 ARDOUR::legalize_for_path_2X (const string& str)
139 string::size_type pos;
140 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
143 /* this is the one place in Ardour where we need to iterate across
144 * potential multibyte characters, and thus we need Glib::ustring
150 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
151 legal.replace (pos, 1, "_");
155 return string (legal);
159 ARDOUR::bump_name_once (const std::string& name, char delimiter)
161 string::size_type delim;
164 if ((delim = name.find_last_of (delimiter)) == string::npos) {
166 newname += delimiter;
170 const char *last_element = name.c_str() + delim + 1;
171 for (size_t i = 0; i < strlen(last_element); i++) {
172 if (!isdigit(last_element[i])) {
179 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
181 if (isnumber == 0 || errno != 0) {
182 // last_element is not a number, or is too large
184 newname += delimiter;
189 snprintf (buf, sizeof(buf), "%d", version+1);
191 newname = name.substr (0, delim+1);
201 ARDOUR::bump_name_number (const std::string& name)
203 size_t pos = name.length();
205 bool have_number = false;
206 while (pos > 0 && isdigit(name.at(--pos))) {
213 int32_t seq = strtol (name.c_str() + num, (char **)NULL, 10);
215 snprintf (buf, sizeof(buf), "%d", seq + 1);
216 newname = name.substr (0, num);
227 ARDOUR::find_named_node (const XMLNode& node, string name)
230 XMLNodeConstIterator niter;
233 nlist = node.children();
235 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
239 if (child->name() == name) {
248 ARDOUR::cmp_nocase (const string& s, const string& s2)
250 string::const_iterator p = s.begin();
251 string::const_iterator p2 = s2.begin();
253 while (p != s.end() && p2 != s2.end()) {
254 if (toupper(*p) != toupper(*p2)) {
255 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
261 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
265 ARDOUR::cmp_nocase_utf8 (const string& s1, const string& s2)
267 const char *cstr1 = s1.c_str();
268 const char *cstr2 = s2.c_str();
269 gchar *cstr1folded = NULL;
270 gchar *cstr2folded = NULL;
273 if (!g_utf8_validate (cstr1, -1, NULL) ||
274 !g_utf8_validate (cstr2, -1, NULL)) {
275 // fall back to comparing ASCII
276 return g_ascii_strcasecmp (cstr1, cstr2);
279 cstr1folded = g_utf8_casefold (cstr1, -1);
280 cstr2folded = g_utf8_casefold (cstr2, -1);
282 if (cstr1folded && cstr2folded) {
283 retval = strcmp (cstr1folded, cstr2folded);
285 // this shouldn't happen, make the best of it
286 retval = g_ascii_strcasecmp (cstr1, cstr2);
290 g_free (cstr1folded);
294 g_free (cstr2folded);
301 ARDOUR::region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
303 path = PBD::basename_nosuffix (path);
305 if (strip_channels) {
307 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
309 string::size_type len = path.length();
311 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
312 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
314 path = path.substr (0, path.length() - 2);
318 if (add_channel_suffix) {
323 path += (char) ('a' + this_one);
325 path += (char) (this_one == 0 ? 'L' : 'R');
333 ARDOUR::path_is_paired (string path, string& pair_base)
335 string::size_type pos;
337 /* remove any leading path */
339 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
340 path = path.substr(pos+1);
343 /* remove filename suffixes etc. */
345 if ((pos = path.find_last_of ('.')) != string::npos) {
346 path = path.substr (0, pos);
349 string::size_type len = path.length();
351 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
353 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
354 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
356 pair_base = path.substr (0, len-2);
366 ARDOUR::CFStringRefToStdString(CFStringRef stringRef)
369 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
370 kCFStringEncodingUTF8);
371 char *buf = new char[size];
375 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
384 ARDOUR::compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
388 step = 1.0/(nframes-1);
392 for (framecnt_t i = 1; i < nframes - 1; ++i) {
393 in[i] = in[i-1] + step;
398 const float pan_law_attenuation = -3.0f;
399 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
401 for (framecnt_t n = 0; n < nframes; ++n) {
403 float outVal = 1 - inVal;
404 out[n] = outVal * (scale * outVal + 1.0f - scale);
405 in[n] = inVal * (scale * inVal + 1.0f - scale);
410 ARDOUR::string_to_edit_mode (string str)
412 if (str == _("Splice")) {
414 } else if (str == _("Slide")) {
416 } else if (str == _("Ripple")) {
418 } else if (str == _("Lock")) {
421 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
422 abort(); /*NOTREACHED*/
427 ARDOUR::edit_mode_to_string (EditMode mode)
446 ARDOUR::string_to_sync_source (string str)
448 if (str == _("MIDI Timecode") || str == _("MTC")) {
452 if (str == _("MIDI Clock")) {
456 if (str == _("JACK")) {
460 if (str == _("LTC")) {
464 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
465 abort(); /*NOTREACHED*/
469 /** @param sh Return a short version of the string */
471 ARDOUR::sync_source_to_string (SyncSource src, bool sh)
475 /* no other backends offer sync for now ... deal with this if we
484 return _("MIDI Timecode");
491 return _("MIDI Clock");
497 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
502 ARDOUR::meter_falloff_to_float (MeterFalloff falloff)
505 case MeterFalloffOff:
506 return METER_FALLOFF_OFF;
507 case MeterFalloffSlowest:
508 return METER_FALLOFF_SLOWEST;
509 case MeterFalloffSlow:
510 return METER_FALLOFF_SLOW;
511 case MeterFalloffSlowish:
512 return METER_FALLOFF_SLOWISH;
513 case MeterFalloffMedium:
514 return METER_FALLOFF_MEDIUM;
515 case MeterFalloffModerate:
516 return METER_FALLOFF_MODERATE;
517 case MeterFalloffFast:
518 case MeterFalloffFaster: // backwards compat enum MeterFalloff
519 case MeterFalloffFastest:
521 return METER_FALLOFF_FAST;
526 ARDOUR::meter_falloff_from_float (float val)
528 if (val == METER_FALLOFF_OFF) {
529 return MeterFalloffOff;
531 else if (val <= METER_FALLOFF_SLOWEST) {
532 return MeterFalloffSlowest;
534 else if (val <= METER_FALLOFF_SLOW) {
535 return MeterFalloffSlow;
537 else if (val <= METER_FALLOFF_SLOWISH) {
538 return MeterFalloffSlowish;
540 else if (val <= METER_FALLOFF_MODERATE) {
541 return MeterFalloffModerate;
543 else if (val <= METER_FALLOFF_MEDIUM) {
544 return MeterFalloffMedium;
547 return MeterFalloffFast;
552 ARDOUR::string_to_auto_state (std::string str)
554 if (str == X_("Off")) {
556 } else if (str == X_("Play")) {
558 } else if (str == X_("Write")) {
560 } else if (str == X_("Touch")) {
564 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
565 abort(); /*NOTREACHED*/
570 ARDOUR::auto_state_to_string (AutoState as)
572 /* to be used only for XML serialization, no i18n done */
588 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
589 abort(); /*NOTREACHED*/
594 ARDOUR::string_to_auto_style (std::string str)
596 if (str == X_("Absolute")) {
598 } else if (str == X_("Trim")) {
602 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
603 abort(); /*NOTREACHED*/
608 ARDOUR::auto_style_to_string (AutoStyle as)
610 /* to be used only for XML serialization, no i18n done */
614 return X_("Absolute");
621 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
622 abort(); /*NOTREACHED*/
627 bool_as_string (bool yn)
629 return (yn ? "yes" : "no");
633 ARDOUR::native_header_format_extension (HeaderFormat hf, const DataType& type)
635 if (type == DataType::MIDI) {
658 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
659 abort(); /*NOTREACHED*/
664 ARDOUR::matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
666 string bws = basename_nosuffix (path);
667 struct dirent* dentry;
672 if ((dead = ::opendir (dir.c_str())) == 0) {
673 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
677 while ((dentry = ::readdir (dead)) != 0) {
679 /* avoid '.' and '..' */
681 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
682 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
686 string fullpath = Glib::build_filename (dir, dentry->d_name);
688 if (g_stat (fullpath.c_str(), &statbuf)) {
692 if (!S_ISREG (statbuf.st_mode)) {
696 string bws2 = basename_nosuffix (dentry->d_name);
709 ARDOUR::how_many_dsp_threads ()
711 /* CALLER MUST HOLD PROCESS LOCK */
713 int num_cpu = hardware_concurrency();
714 int pu = Config->get_processor_usage ();
715 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
718 /* pu is negative: use "pu" less cores for DSP than appear to be available
722 num_threads = num_cpu + pu;
725 } else if (pu == 0) {
727 /* use all available CPUs
730 num_threads = num_cpu;
733 /* use "pu" cores, if available
736 num_threads = min (num_cpu, pu);
743 ARDOUR::gain_to_slider_position_with_max (double g, double max_gain)
745 return gain_to_slider_position (g * 2.0/max_gain);
749 ARDOUR::slider_position_to_gain_with_max (double g, double max_gain)
751 return slider_position_to_gain (g * max_gain/2.0);
755 void c_stacktrace() { stacktrace (cerr); }