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>
45 #include <glibmm/miscutils.h>
46 #include <glibmm/fileutils.h>
49 #include "pbd/error.h"
50 #include "pbd/stacktrace.h"
51 #include "pbd/xml++.h"
52 #include "pbd/basename.h"
53 #include "pbd/strsplit.h"
54 #include "pbd/replace_all.h"
56 #include "ardour/utils.h"
57 #include "ardour/rc_configuration.h"
61 using namespace ARDOUR;
66 replace_chars (const string& str, const string& illegal_chars)
68 string::size_type pos;
71 /* this is the one place in Ardour where we need to iterate across
72 * potential multibyte characters, and thus we need Glib::ustring
78 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
79 legal.replace (pos, 1, "_");
83 return string (legal);
85 /** take an arbitrary string as an argument, and return a version of it
86 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
87 * and later version of this code. It defines a very small number of characters
88 * that are not allowed in a path on the build target filesystem (basically,
89 * POSIX or Windows) and replaces any instances of them with an underscore.
91 * NOTE: this is intended only to legalize for the filesystem that Ardour
92 * is running on. Export should use legalize_for_universal_path() since
93 * the goal there is to be legal across filesystems.
96 ARDOUR::legalize_for_path (const string& str)
98 return replace_chars (str, "/\\");
101 /** take an arbitrary string as an argument, and return a version of it
102 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
103 * and later version of this code. It defines a small number
104 * of characters that are not allowed in a path on any of our target
105 * filesystems, and replaces any instances of them with an underscore.
107 * NOTE: this is intended to create paths that should be legal on
111 ARDOUR::legalize_for_universal_path (const string& str)
113 return replace_chars (str, "<>:\"/\\|?*");
116 /** Legalize for a URI path component. This is like
117 * legalize_for_universal_path, but stricter, disallowing spaces and hash.
118 * This avoids %20 escapes in URIs, but probably needs work to be more strictly
122 ARDOUR::legalize_for_uri (const string& str)
124 return replace_chars (str, "<>:\"/\\|?* #");
127 /** take an arbitrary string as an argument, and return a version of it
128 * suitable for use as a path (directory/folder name). This is the Ardour 2.X
129 * version of this code, which used an approach that came to be seen as
130 * problematic: defining the characters that were allowed and replacing all
131 * others with underscores. See legalize_for_path() for the 3.X and later
136 ARDOUR::legalize_for_path_2X (const string& str)
138 string::size_type pos;
139 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
142 /* this is the one place in Ardour where we need to iterate across
143 * potential multibyte characters, and thus we need Glib::ustring
149 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
150 legal.replace (pos, 1, "_");
154 return string (legal);
158 ARDOUR::bump_name_once (const std::string& name, char delimiter)
160 string::size_type delim;
163 if ((delim = name.find_last_of (delimiter)) == string::npos) {
165 newname += delimiter;
169 const char *last_element = name.c_str() + delim + 1;
170 for (size_t i = 0; i < strlen(last_element); i++) {
171 if (!isdigit(last_element[i])) {
178 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
180 if (isnumber == 0 || errno != 0) {
181 // last_element is not a number, or is too large
183 newname += delimiter;
188 snprintf (buf, sizeof(buf), "%d", version+1);
190 newname = name.substr (0, delim+1);
200 ARDOUR::bump_name_number (const std::string& name)
202 size_t pos = name.length();
204 bool have_number = false;
205 while (pos > 0 && isdigit(name.at(--pos))) {
212 int32_t seq = strtol (name.c_str() + num, (char **)NULL, 10);
214 snprintf (buf, sizeof(buf), "%d", seq + 1);
215 newname = name.substr (0, num);
226 ARDOUR::find_named_node (const XMLNode& node, string name)
229 XMLNodeConstIterator niter;
232 nlist = node.children();
234 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
238 if (child->name() == name) {
247 ARDOUR::cmp_nocase (const string& s, const string& s2)
249 string::const_iterator p = s.begin();
250 string::const_iterator p2 = s2.begin();
252 while (p != s.end() && p2 != s2.end()) {
253 if (toupper(*p) != toupper(*p2)) {
254 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
260 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
264 ARDOUR::cmp_nocase_utf8 (const string& s1, const string& s2)
266 const char *cstr1 = s1.c_str();
267 const char *cstr2 = s2.c_str();
268 gchar *cstr1folded = NULL;
269 gchar *cstr2folded = NULL;
272 if (!g_utf8_validate (cstr1, -1, NULL) ||
273 !g_utf8_validate (cstr2, -1, NULL)) {
274 // fall back to comparing ASCII
275 return g_ascii_strcasecmp (cstr1, cstr2);
278 cstr1folded = g_utf8_casefold (cstr1, -1);
279 cstr2folded = g_utf8_casefold (cstr2, -1);
281 if (cstr1folded && cstr2folded) {
282 retval = strcmp (cstr1folded, cstr2folded);
284 // this shouldn't happen, make the best of it
285 retval = g_ascii_strcasecmp (cstr1, cstr2);
289 g_free (cstr1folded);
293 g_free (cstr2folded);
300 ARDOUR::touch_file (string path)
302 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
311 ARDOUR::region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
313 path = PBD::basename_nosuffix (path);
315 if (strip_channels) {
317 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
319 string::size_type len = path.length();
321 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
322 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
324 path = path.substr (0, path.length() - 2);
328 if (add_channel_suffix) {
333 path += (char) ('a' + this_one);
335 path += (char) (this_one == 0 ? 'L' : 'R');
343 ARDOUR::path_is_paired (string path, string& pair_base)
345 string::size_type pos;
347 /* remove any leading path */
349 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
350 path = path.substr(pos+1);
353 /* remove filename suffixes etc. */
355 if ((pos = path.find_last_of ('.')) != string::npos) {
356 path = path.substr (0, pos);
359 string::size_type len = path.length();
361 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
363 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
364 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
366 pair_base = path.substr (0, len-2);
376 ARDOUR::CFStringRefToStdString(CFStringRef stringRef)
379 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
380 kCFStringEncodingUTF8);
381 char *buf = new char[size];
385 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
394 ARDOUR::compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
398 step = 1.0/(nframes-1);
402 for (framecnt_t i = 1; i < nframes - 1; ++i) {
403 in[i] = in[i-1] + step;
408 const float pan_law_attenuation = -3.0f;
409 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
411 for (framecnt_t n = 0; n < nframes; ++n) {
413 float outVal = 1 - inVal;
414 out[n] = outVal * (scale * outVal + 1.0f - scale);
415 in[n] = inVal * (scale * inVal + 1.0f - scale);
420 ARDOUR::string_to_edit_mode (string str)
422 if (str == _("Splice")) {
424 } else if (str == _("Slide")) {
426 } else if (str == _("Ripple")) {
428 } else if (str == _("Constrained")) {
431 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
432 abort(); /*NOTREACHED*/
437 ARDOUR::edit_mode_to_string (EditMode mode)
444 return _("Constrained");
456 ARDOUR::string_to_sync_source (string str)
458 if (str == _("MIDI Timecode") || str == _("MTC")) {
462 if (str == _("MIDI Clock")) {
466 if (str == _("JACK")) {
470 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
471 abort(); /*NOTREACHED*/
475 /** @param sh Return a short version of the string */
477 ARDOUR::sync_source_to_string (SyncSource src, bool sh)
481 /* no other backends offer sync for now ... deal with this if we
490 return _("MIDI Timecode");
497 return _("MIDI Clock");
503 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
508 ARDOUR::meter_falloff_to_float (MeterFalloff falloff)
511 case MeterFalloffOff:
512 return METER_FALLOFF_OFF;
513 case MeterFalloffSlowest:
514 return METER_FALLOFF_SLOWEST;
515 case MeterFalloffSlow:
516 return METER_FALLOFF_SLOW;
517 case MeterFalloffSlowish:
518 return METER_FALLOFF_SLOWISH;
519 case MeterFalloffMedium:
520 return METER_FALLOFF_MEDIUM;
521 case MeterFalloffModerate:
522 return METER_FALLOFF_MODERATE;
523 case MeterFalloffFast:
524 return METER_FALLOFF_FAST;
525 case MeterFalloffFaster:
526 return METER_FALLOFF_FASTER;
527 case MeterFalloffFastest:
528 return METER_FALLOFF_FASTEST;
530 return METER_FALLOFF_FAST;
535 ARDOUR::meter_falloff_from_float (float val)
537 if (val == METER_FALLOFF_OFF) {
538 return MeterFalloffOff;
540 else if (val <= METER_FALLOFF_SLOWEST) {
541 return MeterFalloffSlowest;
543 else if (val <= METER_FALLOFF_SLOW) {
544 return MeterFalloffSlow;
546 else if (val <= METER_FALLOFF_SLOWISH) {
547 return MeterFalloffSlowish;
549 else if (val <= METER_FALLOFF_MODERATE) {
550 return MeterFalloffModerate;
552 else if (val <= METER_FALLOFF_MEDIUM) {
553 return MeterFalloffMedium;
555 else if (val <= METER_FALLOFF_FAST) {
556 return MeterFalloffFast;
558 else if (val <= METER_FALLOFF_FASTER) {
559 return MeterFalloffFaster;
562 return MeterFalloffFastest;
567 ARDOUR::string_to_auto_state (std::string str)
569 if (str == X_("Off")) {
571 } else if (str == X_("Play")) {
573 } else if (str == X_("Write")) {
575 } else if (str == X_("Touch")) {
579 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
580 abort(); /*NOTREACHED*/
585 ARDOUR::auto_state_to_string (AutoState as)
587 /* to be used only for XML serialization, no i18n done */
603 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
604 abort(); /*NOTREACHED*/
609 ARDOUR::string_to_auto_style (std::string str)
611 if (str == X_("Absolute")) {
613 } else if (str == X_("Trim")) {
617 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
618 abort(); /*NOTREACHED*/
623 ARDOUR::auto_style_to_string (AutoStyle as)
625 /* to be used only for XML serialization, no i18n done */
629 return X_("Absolute");
636 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
637 abort(); /*NOTREACHED*/
642 bool_as_string (bool yn)
644 return (yn ? "yes" : "no");
648 ARDOUR::native_header_format_extension (HeaderFormat hf, const DataType& type)
650 if (type == DataType::MIDI) {
671 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
672 abort(); /*NOTREACHED*/
677 ARDOUR::matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
679 string bws = basename_nosuffix (path);
680 struct dirent* dentry;
685 if ((dead = ::opendir (dir.c_str())) == 0) {
686 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
690 while ((dentry = ::readdir (dead)) != 0) {
692 /* avoid '.' and '..' */
694 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
695 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
699 string fullpath = Glib::build_filename (dir, dentry->d_name);
701 if (::stat (fullpath.c_str(), &statbuf)) {
705 if (!S_ISREG (statbuf.st_mode)) {
709 string bws2 = basename_nosuffix (dentry->d_name);
722 ARDOUR::how_many_dsp_threads ()
724 /* CALLER MUST HOLD PROCESS LOCK */
726 int num_cpu = hardware_concurrency();
727 int pu = Config->get_processor_usage ();
728 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
731 /* pu is negative: use "pu" less cores for DSP than appear to be available
735 num_threads = num_cpu + pu;
738 } else if (pu == 0) {
740 /* use all available CPUs
743 num_threads = num_cpu;
746 /* use "pu" cores, if available
749 num_threads = min (num_cpu, pu);
756 ARDOUR::gain_to_slider_position_with_max (double g, double max_gain)
758 return gain_to_slider_position (g * 2.0/max_gain);
762 ARDOUR::slider_position_to_gain_with_max (double g, double max_gain)
764 return slider_position_to_gain (g * max_gain/2.0);
768 void c_stacktrace() { stacktrace (cerr); }