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>
43 #include <glibmm/miscutils.h>
44 #include <glibmm/fileutils.h>
47 #include "pbd/error.h"
48 #include "pbd/stacktrace.h"
49 #include "pbd/xml++.h"
50 #include "pbd/basename.h"
51 #include "pbd/strsplit.h"
52 #include "pbd/replace_all.h"
54 #include "ardour/utils.h"
55 #include "ardour/rc_configuration.h"
59 using namespace ARDOUR;
64 replace_chars (const string& str, const string& illegal_chars)
66 string::size_type pos;
69 /* this is the one place in Ardour where we need to iterate across
70 * potential multibyte characters, and thus we need Glib::ustring
76 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
77 legal.replace (pos, 1, "_");
81 return string (legal);
83 /** take an arbitrary string as an argument, and return a version of it
84 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
85 * and later version of this code. It defines a very small number of characters
86 * that are not allowed in a path on the build target filesystem (basically,
87 * POSIX or Windows) and replaces any instances of them with an underscore.
89 * NOTE: this is intended only to legalize for the filesystem that Ardour
90 * is running on. Export should use legalize_for_universal_path() since
91 * the goal there is to be legal across filesystems.
94 legalize_for_path (const string& str)
96 return replace_chars (str, "/\\");
99 /** take an arbitrary string as an argument, and return a version of it
100 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
101 * and later version of this code. It defines a small number
102 * of characters that are not allowed in a path on any of our target
103 * filesystems, and replaces any instances of them with an underscore.
105 * NOTE: this is intended to create paths that should be legal on
109 legalize_for_universal_path (const string& str)
111 return replace_chars (str, "<>:\"/\\|?*");
114 /** take an arbitrary string as an argument, and return a version of it
115 * suitable for use as a path (directory/folder name). This is the Ardour 2.X
116 * version of this code, which used an approach that came to be seen as
117 * problematic: defining the characters that were allowed and replacing all
118 * others with underscores. See legalize_for_path() for the 3.X and later
123 legalize_for_path_2X (const string& str)
125 string::size_type pos;
126 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
129 /* this is the one place in Ardour where we need to iterate across
130 * potential multibyte characters, and thus we need Glib::ustring
136 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
137 legal.replace (pos, 1, "_");
141 return string (legal);
145 bump_name_once (const std::string& name, char delimiter)
147 string::size_type delim;
150 if ((delim = name.find_last_of (delimiter)) == string::npos) {
152 newname += delimiter;
156 const char *last_element = name.c_str() + delim + 1;
157 for (size_t i = 0; i < strlen(last_element); i++) {
158 if (!isdigit(last_element[i])) {
165 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
167 if (isnumber == 0 || errno != 0) {
168 // last_element is not a number, or is too large
170 newname += delimiter;
175 snprintf (buf, sizeof(buf), "%d", version+1);
177 newname = name.substr (0, delim+1);
187 find_named_node (const XMLNode& node, string name)
190 XMLNodeConstIterator niter;
193 nlist = node.children();
195 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
199 if (child->name() == name) {
208 cmp_nocase (const string& s, const string& s2)
210 string::const_iterator p = s.begin();
211 string::const_iterator p2 = s2.begin();
213 while (p != s.end() && p2 != s2.end()) {
214 if (toupper(*p) != toupper(*p2)) {
215 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
221 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
225 touch_file (string path)
227 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
236 region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
238 path = PBD::basename_nosuffix (path);
240 if (strip_channels) {
242 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
244 string::size_type len = path.length();
246 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
247 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
249 path = path.substr (0, path.length() - 2);
253 if (add_channel_suffix) {
258 path += (char) ('a' + this_one);
260 path += (char) (this_one == 0 ? 'L' : 'R');
268 path_is_paired (string path, string& pair_base)
270 string::size_type pos;
272 /* remove any leading path */
274 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
275 path = path.substr(pos+1);
278 /* remove filename suffixes etc. */
280 if ((pos = path.find_last_of ('.')) != string::npos) {
281 path = path.substr (0, pos);
284 string::size_type len = path.length();
286 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
288 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
289 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
291 pair_base = path.substr (0, len-2);
300 path_expand (string path)
306 /* tilde expansion */
308 if (path[0] == '~') {
309 if (path.length() == 1) {
310 return Glib::get_home_dir();
313 if (path[1] == '/') {
314 path.replace (0, 1, Glib::get_home_dir());
316 /* can't handle ~roger, so just leave it */
320 /* now do $VAR substitution, since wordexp isn't reliable */
322 regex_t compiled_pattern;
323 const int nmatches = 100;
324 regmatch_t matches[nmatches];
326 if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
327 cerr << "bad regcomp\n";
333 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
337 /* matches[0] gives the entire match */
339 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
341 /* try to get match from the environment */
343 if (match[1] == '{') {
345 match = match.substr (2, match.length() - 3);
348 char* matched_value = getenv (match.c_str());
351 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
353 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
356 /* go back and do it again with whatever remains after the
361 regfree (&compiled_pattern);
365 char buf[PATH_MAX+1];
367 if (realpath (path.c_str(), buf)) {
375 search_path_expand (string path)
384 split (path, s, ':');
386 for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
387 string exp = path_expand (*i);
395 for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
407 CFStringRefToStdString(CFStringRef stringRef)
410 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
411 kCFStringEncodingUTF8);
412 char *buf = new char[size];
416 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
425 compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
429 step = 1.0/(nframes-1);
433 for (framecnt_t i = 1; i < nframes - 1; ++i) {
434 in[i] = in[i-1] + step;
439 const float pan_law_attenuation = -3.0f;
440 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
442 for (framecnt_t n = 0; n < nframes; ++n) {
444 float outVal = 1 - inVal;
445 out[n] = outVal * (scale * outVal + 1.0f - scale);
446 in[n] = inVal * (scale * inVal + 1.0f - scale);
451 string_to_edit_mode (string str)
453 if (str == _("Splice")) {
455 } else if (str == _("Slide")) {
457 } else if (str == _("Lock")) {
460 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
466 edit_mode_to_string (EditMode mode)
482 string_to_sync_source (string str)
484 if (str == _("MIDI Timecode") || str == _("MTC")) {
488 if (str == _("MIDI Clock")) {
492 if (str == _("JACK")) {
496 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
501 /** @param sh Return a short version of the string */
503 sync_source_to_string (SyncSource src, bool sh)
513 return _("MIDI Timecode");
520 return _("MIDI Clock");
526 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
531 meter_falloff_to_float (MeterFalloff falloff)
534 case MeterFalloffOff:
535 return METER_FALLOFF_OFF;
536 case MeterFalloffSlowest:
537 return METER_FALLOFF_SLOWEST;
538 case MeterFalloffSlow:
539 return METER_FALLOFF_SLOW;
540 case MeterFalloffMedium:
541 return METER_FALLOFF_MEDIUM;
542 case MeterFalloffFast:
543 return METER_FALLOFF_FAST;
544 case MeterFalloffFaster:
545 return METER_FALLOFF_FASTER;
546 case MeterFalloffFastest:
547 return METER_FALLOFF_FASTEST;
549 return METER_FALLOFF_FAST;
554 meter_falloff_from_float (float val)
556 if (val == METER_FALLOFF_OFF) {
557 return MeterFalloffOff;
559 else if (val <= METER_FALLOFF_SLOWEST) {
560 return MeterFalloffSlowest;
562 else if (val <= METER_FALLOFF_SLOW) {
563 return MeterFalloffSlow;
565 else if (val <= METER_FALLOFF_MEDIUM) {
566 return MeterFalloffMedium;
568 else if (val <= METER_FALLOFF_FAST) {
569 return MeterFalloffFast;
571 else if (val <= METER_FALLOFF_FASTER) {
572 return MeterFalloffFaster;
575 return MeterFalloffFastest;
580 ARDOUR::string_to_auto_state (std::string str)
582 if (str == X_("Off")) {
584 } else if (str == X_("Play")) {
586 } else if (str == X_("Write")) {
588 } else if (str == X_("Touch")) {
592 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
598 ARDOUR::auto_state_to_string (AutoState as)
600 /* to be used only for XML serialization, no i18n done */
616 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
622 ARDOUR::string_to_auto_style (std::string str)
624 if (str == X_("Absolute")) {
626 } else if (str == X_("Trim")) {
630 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
636 ARDOUR::auto_style_to_string (AutoStyle as)
638 /* to be used only for XML serialization, no i18n done */
642 return X_("Absolute");
649 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
655 bool_as_string (bool yn)
657 return (yn ? "yes" : "no");
661 native_header_format_extension (HeaderFormat hf, const DataType& type)
663 if (type == DataType::MIDI) {
684 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
690 matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
692 string bws = basename_nosuffix (path);
693 struct dirent* dentry;
698 if ((dead = ::opendir (dir.c_str())) == 0) {
699 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
703 while ((dentry = ::readdir (dead)) != 0) {
705 /* avoid '.' and '..' */
707 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
708 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
712 string fullpath = Glib::build_filename (dir, dentry->d_name);
714 if (::stat (fullpath.c_str(), &statbuf)) {
718 if (!S_ISREG (statbuf.st_mode)) {
722 string bws2 = basename_nosuffix (dentry->d_name);
735 how_many_dsp_threads ()
737 /* CALLER MUST HOLD PROCESS LOCK */
739 int num_cpu = hardware_concurrency();
740 int pu = Config->get_processor_usage ();
741 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
744 /* pu is negative: use "pu" less cores for DSP than appear to be available
748 num_threads = num_cpu + pu;
751 } else if (pu == 0) {
753 /* use all available CPUs
756 num_threads = num_cpu;
759 /* use "pu" cores, if available
762 num_threads = min (num_cpu, pu);
768 double gain_to_slider_position_with_max (double g, double max_gain)
770 return gain_to_slider_position (g * 2.0/max_gain);
773 double slider_position_to_gain_with_max (double g, double max_gain)
775 return slider_position_to_gain (g * max_gain/2.0);
779 void c_stacktrace() { stacktrace (cerr); }