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/control_math.h"
51 #include "pbd/error.h"
52 #include "pbd/stacktrace.h"
53 #include "pbd/xml++.h"
54 #include "pbd/basename.h"
55 #include "pbd/strsplit.h"
56 #include "pbd/replace_all.h"
58 #include "ardour/utils.h"
59 #include "ardour/rc_configuration.h"
63 using namespace ARDOUR;
68 replace_chars (const string& str, const string& illegal_chars)
70 string::size_type pos;
73 /* this is the one place in Ardour where we need to iterate across
74 * potential multibyte characters, and thus we need Glib::ustring
80 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
81 legal.replace (pos, 1, "_");
85 return string (legal);
87 /** take an arbitrary string as an argument, and return a version of it
88 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
89 * and later version of this code. It defines a very small number of characters
90 * that are not allowed in a path on the build target filesystem (basically,
91 * POSIX or Windows) and replaces any instances of them with an underscore.
93 * NOTE: this is intended only to legalize for the filesystem that Ardour
94 * is running on. Export should use legalize_for_universal_path() since
95 * the goal there is to be legal across filesystems.
98 ARDOUR::legalize_for_path (const string& str)
100 return replace_chars (str, "/\\");
103 /** take an arbitrary string as an argument, and return a version of it
104 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
105 * and later version of this code. It defines a small number
106 * of characters that are not allowed in a path on any of our target
107 * filesystems, and replaces any instances of them with an underscore.
109 * NOTE: this is intended to create paths that should be legal on
113 ARDOUR::legalize_for_universal_path (const string& str)
115 return replace_chars (str, "<>:\"/\\|?*");
118 /** Legalize for a URI path component. This is like
119 * legalize_for_universal_path, but stricter, disallowing spaces and hash.
120 * This avoids %20 escapes in URIs, but probably needs work to be more strictly
124 ARDOUR::legalize_for_uri (const string& str)
126 return replace_chars (str, "<>:\"/\\|?* #");
129 /** take an arbitrary string as an argument, and return a version of it
130 * suitable for use as a path (directory/folder name). This is the Ardour 2.X
131 * version of this code, which used an approach that came to be seen as
132 * problematic: defining the characters that were allowed and replacing all
133 * others with underscores. See legalize_for_path() for the 3.X and later
138 ARDOUR::legalize_for_path_2X (const string& str)
140 string::size_type pos;
141 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
144 /* this is the one place in Ardour where we need to iterate across
145 * potential multibyte characters, and thus we need Glib::ustring
151 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
152 legal.replace (pos, 1, "_");
156 return string (legal);
160 ARDOUR::bump_name_once (const std::string& name, char delimiter)
162 string::size_type delim;
165 if ((delim = name.find_last_of (delimiter)) == string::npos) {
167 newname += delimiter;
171 const char *last_element = name.c_str() + delim + 1;
172 for (size_t i = 0; i < strlen(last_element); i++) {
173 if (!isdigit(last_element[i])) {
180 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
182 if (isnumber == 0 || errno != 0) {
183 // last_element is not a number, or is too large
185 newname += delimiter;
190 snprintf (buf, sizeof(buf), "%d", version+1);
192 newname = name.substr (0, delim+1);
202 ARDOUR::bump_name_number (const std::string& name)
204 size_t pos = name.length();
206 bool have_number = false;
207 while (pos > 0 && isdigit(name.at(--pos))) {
214 int32_t seq = strtol (name.c_str() + num, (char **)NULL, 10);
216 snprintf (buf, sizeof(buf), "%d", seq + 1);
217 newname = name.substr (0, num);
228 ARDOUR::find_named_node (const XMLNode& node, string name)
231 XMLNodeConstIterator niter;
234 nlist = node.children();
236 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
240 if (child->name() == name) {
249 ARDOUR::cmp_nocase (const string& s, const string& s2)
251 string::const_iterator p = s.begin();
252 string::const_iterator p2 = s2.begin();
254 while (p != s.end() && p2 != s2.end()) {
255 if (toupper(*p) != toupper(*p2)) {
256 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
262 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
266 ARDOUR::cmp_nocase_utf8 (const string& s1, const string& s2)
268 const char *cstr1 = s1.c_str();
269 const char *cstr2 = s2.c_str();
270 gchar *cstr1folded = NULL;
271 gchar *cstr2folded = NULL;
274 if (!g_utf8_validate (cstr1, -1, NULL) ||
275 !g_utf8_validate (cstr2, -1, NULL)) {
276 // fall back to comparing ASCII
277 return g_ascii_strcasecmp (cstr1, cstr2);
280 cstr1folded = g_utf8_casefold (cstr1, -1);
281 cstr2folded = g_utf8_casefold (cstr2, -1);
283 if (cstr1folded && cstr2folded) {
284 retval = strcmp (cstr1folded, cstr2folded);
286 // this shouldn't happen, make the best of it
287 retval = g_ascii_strcasecmp (cstr1, cstr2);
291 g_free (cstr1folded);
295 g_free (cstr2folded);
302 ARDOUR::region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
304 path = PBD::basename_nosuffix (path);
306 if (strip_channels) {
308 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
310 string::size_type len = path.length();
312 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
313 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
315 path = path.substr (0, path.length() - 2);
319 if (add_channel_suffix) {
324 path += (char) ('a' + this_one);
326 path += (char) (this_one == 0 ? 'L' : 'R');
334 ARDOUR::path_is_paired (string path, string& pair_base)
336 string::size_type pos;
338 /* remove any leading path */
340 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
341 path = path.substr(pos+1);
344 /* remove filename suffixes etc. */
346 if ((pos = path.find_last_of ('.')) != string::npos) {
347 path = path.substr (0, pos);
350 string::size_type len = path.length();
352 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
354 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
355 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
357 pair_base = path.substr (0, len-2);
367 ARDOUR::CFStringRefToStdString(CFStringRef stringRef)
370 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
371 kCFStringEncodingUTF8);
372 char *buf = new char[size];
376 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
385 ARDOUR::compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
389 step = 1.0/(nframes-1);
393 for (framecnt_t i = 1; i < nframes - 1; ++i) {
394 in[i] = in[i-1] + step;
399 const float pan_law_attenuation = -3.0f;
400 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
402 for (framecnt_t n = 0; n < nframes; ++n) {
404 float outVal = 1 - inVal;
405 out[n] = outVal * (scale * outVal + 1.0f - scale);
406 in[n] = inVal * (scale * inVal + 1.0f - scale);
411 ARDOUR::string_to_edit_mode (string str)
413 if (str == _("Splice")) {
415 } else if (str == _("Slide")) {
417 } else if (str == _("Ripple")) {
419 } else if (str == _("Lock")) {
422 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
423 abort(); /*NOTREACHED*/
428 ARDOUR::edit_mode_to_string (EditMode mode)
447 ARDOUR::string_to_sync_source (string str)
449 if (str == _("MIDI Timecode") || str == _("MTC")) {
453 if (str == _("MIDI Clock")) {
457 if (str == _("JACK")) {
461 if (str == _("LTC")) {
465 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
466 abort(); /*NOTREACHED*/
470 /** @param sh Return a short version of the string */
472 ARDOUR::sync_source_to_string (SyncSource src, bool sh)
476 /* no other backends offer sync for now ... deal with this if we
479 return S_("SyncSource|JACK");
483 return S_("SyncSource|MTC");
485 return _("MIDI Timecode");
490 return S_("SyncSource|M-Clk");
492 return _("MIDI Clock");
496 return S_("SyncSource|LTC");
498 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
499 return S_("SyncSource|JACK");
503 ARDOUR::meter_falloff_to_float (MeterFalloff falloff)
506 case MeterFalloffOff:
507 return METER_FALLOFF_OFF;
508 case MeterFalloffSlowest:
509 return METER_FALLOFF_SLOWEST;
510 case MeterFalloffSlow:
511 return METER_FALLOFF_SLOW;
512 case MeterFalloffSlowish:
513 return METER_FALLOFF_SLOWISH;
514 case MeterFalloffMedium:
515 return METER_FALLOFF_MEDIUM;
516 case MeterFalloffModerate:
517 return METER_FALLOFF_MODERATE;
518 case MeterFalloffFast:
519 case MeterFalloffFaster: // backwards compat enum MeterFalloff
520 case MeterFalloffFastest:
522 return METER_FALLOFF_FAST;
527 ARDOUR::meter_falloff_from_float (float val)
529 if (val == METER_FALLOFF_OFF) {
530 return MeterFalloffOff;
532 else if (val <= METER_FALLOFF_SLOWEST) {
533 return MeterFalloffSlowest;
535 else if (val <= METER_FALLOFF_SLOW) {
536 return MeterFalloffSlow;
538 else if (val <= METER_FALLOFF_SLOWISH) {
539 return MeterFalloffSlowish;
541 else if (val <= METER_FALLOFF_MODERATE) {
542 return MeterFalloffModerate;
544 else if (val <= METER_FALLOFF_MEDIUM) {
545 return MeterFalloffMedium;
548 return MeterFalloffFast;
553 ARDOUR::string_to_auto_state (std::string str)
555 if (str == X_("Off")) {
557 } else if (str == X_("Play")) {
559 } else if (str == X_("Write")) {
561 } else if (str == X_("Touch")) {
565 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
566 abort(); /*NOTREACHED*/
571 ARDOUR::auto_state_to_string (AutoState as)
573 /* to be used only for XML serialization, no i18n done */
589 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
590 abort(); /*NOTREACHED*/
595 bool_as_string (bool yn)
597 return (yn ? "yes" : "no");
601 ARDOUR::native_header_format_extension (HeaderFormat hf, const DataType& type)
603 if (type == DataType::MIDI) {
626 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
627 abort(); /*NOTREACHED*/
632 ARDOUR::matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
634 string bws = basename_nosuffix (path);
635 struct dirent* dentry;
640 if ((dead = ::opendir (dir.c_str())) == 0) {
641 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
645 while ((dentry = ::readdir (dead)) != 0) {
647 /* avoid '.' and '..' */
649 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
650 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
654 string fullpath = Glib::build_filename (dir, dentry->d_name);
656 if (g_stat (fullpath.c_str(), &statbuf)) {
660 if (!S_ISREG (statbuf.st_mode)) {
664 string bws2 = basename_nosuffix (dentry->d_name);
677 ARDOUR::how_many_dsp_threads ()
679 /* CALLER MUST HOLD PROCESS LOCK */
681 int num_cpu = hardware_concurrency();
682 int pu = Config->get_processor_usage ();
683 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
686 /* pu is negative: use "pu" less cores for DSP than appear to be available
690 num_threads = num_cpu + pu;
693 } else if (pu == 0) {
695 /* use all available CPUs
698 num_threads = num_cpu;
701 /* use "pu" cores, if available
704 num_threads = min (num_cpu, pu);
711 ARDOUR::gain_to_slider_position_with_max (double g, double max_gain)
713 return gain_to_position (g * 2.0 / max_gain);
717 ARDOUR::slider_position_to_gain_with_max (double g, double max_gain)
719 return position_to_gain (g) * max_gain / 2.0;
723 void c_stacktrace() { stacktrace (cerr); }