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/scoped_file_descriptor.h"
56 #include "pbd/strsplit.h"
57 #include "pbd/replace_all.h"
59 #include "ardour/utils.h"
60 #include "ardour/rc_configuration.h"
64 using namespace ARDOUR;
69 replace_chars (const string& str, const string& illegal_chars)
71 string::size_type pos;
74 /* this is the one place in Ardour where we need to iterate across
75 * potential multibyte characters, and thus we need Glib::ustring
81 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
82 legal.replace (pos, 1, "_");
86 return string (legal);
88 /** take an arbitrary string as an argument, and return a version of it
89 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
90 * and later version of this code. It defines a very small number of characters
91 * that are not allowed in a path on the build target filesystem (basically,
92 * POSIX or Windows) and replaces any instances of them with an underscore.
94 * NOTE: this is intended only to legalize for the filesystem that Ardour
95 * is running on. Export should use legalize_for_universal_path() since
96 * the goal there is to be legal across filesystems.
99 ARDOUR::legalize_for_path (const string& str)
101 return replace_chars (str, "/\\");
104 /** take an arbitrary string as an argument, and return a version of it
105 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
106 * and later version of this code. It defines a small number
107 * of characters that are not allowed in a path on any of our target
108 * filesystems, and replaces any instances of them with an underscore.
110 * NOTE: this is intended to create paths that should be legal on
114 ARDOUR::legalize_for_universal_path (const string& str)
116 return replace_chars (str, "<>:\"/\\|?*");
119 /** Legalize for a URI path component. This is like
120 * legalize_for_universal_path, but stricter, disallowing spaces and hash.
121 * This avoids %20 escapes in URIs, but probably needs work to be more strictly
125 ARDOUR::legalize_for_uri (const string& str)
127 return replace_chars (str, "<>:\"/\\|?* #");
130 /** take an arbitrary string as an argument, and return a version of it
131 * suitable for use as a path (directory/folder name). This is the Ardour 2.X
132 * version of this code, which used an approach that came to be seen as
133 * problematic: defining the characters that were allowed and replacing all
134 * others with underscores. See legalize_for_path() for the 3.X and later
139 ARDOUR::legalize_for_path_2X (const string& str)
141 string::size_type pos;
142 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
145 /* this is the one place in Ardour where we need to iterate across
146 * potential multibyte characters, and thus we need Glib::ustring
152 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
153 legal.replace (pos, 1, "_");
157 return string (legal);
161 ARDOUR::bump_name_once (const std::string& name, char delimiter)
163 string::size_type delim;
166 if ((delim = name.find_last_of (delimiter)) == string::npos) {
168 newname += delimiter;
172 const char *last_element = name.c_str() + delim + 1;
173 for (size_t i = 0; i < strlen(last_element); i++) {
174 if (!isdigit(last_element[i])) {
181 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
183 if (isnumber == 0 || errno != 0) {
184 // last_element is not a number, or is too large
186 newname += delimiter;
191 snprintf (buf, sizeof(buf), "%d", version+1);
193 newname = name.substr (0, delim+1);
203 ARDOUR::bump_name_number (const std::string& name)
205 size_t pos = name.length();
207 bool have_number = false;
208 while (pos > 0 && isdigit(name.at(--pos))) {
215 int32_t seq = strtol (name.c_str() + num, (char **)NULL, 10);
217 snprintf (buf, sizeof(buf), "%d", seq + 1);
218 newname = name.substr (0, num);
229 ARDOUR::find_named_node (const XMLNode& node, string name)
232 XMLNodeConstIterator niter;
235 nlist = node.children();
237 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
241 if (child->name() == name) {
250 ARDOUR::cmp_nocase (const string& s, const string& s2)
252 string::const_iterator p = s.begin();
253 string::const_iterator p2 = s2.begin();
255 while (p != s.end() && p2 != s2.end()) {
256 if (toupper(*p) != toupper(*p2)) {
257 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
263 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
267 ARDOUR::cmp_nocase_utf8 (const string& s1, const string& s2)
269 const char *cstr1 = s1.c_str();
270 const char *cstr2 = s2.c_str();
271 gchar *cstr1folded = NULL;
272 gchar *cstr2folded = NULL;
275 if (!g_utf8_validate (cstr1, -1, NULL) ||
276 !g_utf8_validate (cstr2, -1, NULL)) {
277 // fall back to comparing ASCII
278 return g_ascii_strcasecmp (cstr1, cstr2);
281 cstr1folded = g_utf8_casefold (cstr1, -1);
282 cstr2folded = g_utf8_casefold (cstr2, -1);
284 if (cstr1folded && cstr2folded) {
285 retval = strcmp (cstr1folded, cstr2folded);
287 // this shouldn't happen, make the best of it
288 retval = g_ascii_strcasecmp (cstr1, cstr2);
292 g_free (cstr1folded);
296 g_free (cstr2folded);
303 ARDOUR::region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
305 path = PBD::basename_nosuffix (path);
307 if (strip_channels) {
309 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
311 string::size_type len = path.length();
313 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
314 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
316 path = path.substr (0, path.length() - 2);
320 if (add_channel_suffix) {
325 path += (char) ('a' + this_one);
327 path += (char) (this_one == 0 ? 'L' : 'R');
335 ARDOUR::path_is_paired (string path, string& pair_base)
337 string::size_type pos;
339 /* remove any leading path */
341 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
342 path = path.substr(pos+1);
345 /* remove filename suffixes etc. */
347 if ((pos = path.find_last_of ('.')) != string::npos) {
348 path = path.substr (0, pos);
351 string::size_type len = path.length();
353 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
355 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
356 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
358 pair_base = path.substr (0, len-2);
368 ARDOUR::CFStringRefToStdString(CFStringRef stringRef)
371 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
372 kCFStringEncodingUTF8);
373 char *buf = new char[size];
377 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
386 ARDOUR::compute_equal_power_fades (samplecnt_t nframes, float* in, float* out)
390 step = 1.0/(nframes-1);
394 for (samplecnt_t i = 1; i < nframes - 1; ++i) {
395 in[i] = in[i-1] + step;
400 const float pan_law_attenuation = -3.0f;
401 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
403 for (samplecnt_t n = 0; n < nframes; ++n) {
405 float outVal = 1 - inVal;
406 out[n] = outVal * (scale * outVal + 1.0f - scale);
407 in[n] = inVal * (scale * inVal + 1.0f - scale);
412 ARDOUR::string_to_edit_mode (string str)
414 if (str == _("Splice")) {
416 } else if (str == _("Slide")) {
418 } else if (str == _("Ripple")) {
420 } else if (str == _("Lock")) {
423 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
424 abort(); /*NOTREACHED*/
429 ARDOUR::edit_mode_to_string (EditMode mode)
448 ARDOUR::meter_falloff_to_float (MeterFalloff falloff)
451 case MeterFalloffOff:
452 return METER_FALLOFF_OFF;
453 case MeterFalloffSlowest:
454 return METER_FALLOFF_SLOWEST;
455 case MeterFalloffSlow:
456 return METER_FALLOFF_SLOW;
457 case MeterFalloffSlowish:
458 return METER_FALLOFF_SLOWISH;
459 case MeterFalloffMedium:
460 return METER_FALLOFF_MEDIUM;
461 case MeterFalloffModerate:
462 return METER_FALLOFF_MODERATE;
463 case MeterFalloffFast:
464 case MeterFalloffFaster: // backwards compat enum MeterFalloff
465 case MeterFalloffFastest:
467 return METER_FALLOFF_FAST;
472 ARDOUR::meter_falloff_from_float (float val)
474 if (val == METER_FALLOFF_OFF) {
475 return MeterFalloffOff;
477 else if (val <= METER_FALLOFF_SLOWEST) {
478 return MeterFalloffSlowest;
480 else if (val <= METER_FALLOFF_SLOW) {
481 return MeterFalloffSlow;
483 else if (val <= METER_FALLOFF_SLOWISH) {
484 return MeterFalloffSlowish;
486 else if (val <= METER_FALLOFF_MODERATE) {
487 return MeterFalloffModerate;
489 else if (val <= METER_FALLOFF_MEDIUM) {
490 return MeterFalloffMedium;
493 return MeterFalloffFast;
498 ARDOUR::string_to_auto_state (std::string str)
500 if (str == X_("Off")) {
502 } else if (str == X_("Play")) {
504 } else if (str == X_("Write")) {
506 } else if (str == X_("Touch")) {
508 } else if (str == X_("Latch")) {
512 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
513 abort(); /*NOTREACHED*/
518 ARDOUR::auto_state_to_string (AutoState as)
520 /* to be used only for XML serialization, no i18n done */
540 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
541 abort(); /*NOTREACHED*/
546 bool_as_string (bool yn)
548 return (yn ? "yes" : "no");
552 ARDOUR::native_header_format_extension (HeaderFormat hf, const DataType& type)
554 if (type == DataType::MIDI) {
579 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
580 abort(); /*NOTREACHED*/
585 ARDOUR::matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
587 string bws = basename_nosuffix (path);
588 struct dirent* dentry;
593 if ((dead = ::opendir (dir.c_str())) == 0) {
594 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
598 while ((dentry = ::readdir (dead)) != 0) {
600 /* avoid '.' and '..' */
602 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
603 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
607 string fullpath = Glib::build_filename (dir, dentry->d_name);
609 if (g_stat (fullpath.c_str(), &statbuf)) {
613 if (!S_ISREG (statbuf.st_mode)) {
617 string bws2 = basename_nosuffix (dentry->d_name);
630 ARDOUR::how_many_dsp_threads ()
632 /* CALLER MUST HOLD PROCESS LOCK */
634 int num_cpu = hardware_concurrency();
635 int pu = Config->get_processor_usage ();
636 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
639 /* pu is negative: use "pu" less cores for DSP than appear to be available
643 num_threads = num_cpu + pu;
646 } else if (pu == 0) {
648 /* use all available CPUs
651 num_threads = num_cpu;
654 /* use "pu" cores, if available
657 num_threads = min (num_cpu, pu);
664 ARDOUR::gain_to_slider_position_with_max (double g, double max_gain)
666 return gain_to_position (g * 2.0 / max_gain);
670 ARDOUR::slider_position_to_gain_with_max (double g, double max_gain)
672 return position_to_gain (g) * max_gain / 2.0;
678 ARDOUR::compute_sha1_of_file (std::string path)
680 PBD::ScopedFileDescriptor fd (g_open (path.c_str(), O_RDONLY, 0444));
682 return std::string ();
690 while ((n_read = ::read(fd, buf, sizeof(buf))) > 0) {
691 sha1_write (&s, (const uint8_t*) buf, n_read);
694 sha1_result_hash (&s, hash);
695 return std::string (hash);
699 void c_stacktrace() { stacktrace (cerr); }