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 legalize_for_path (const string& str)
66 string::size_type pos;
67 string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
73 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
74 legal.replace (pos, 1, "_");
78 return string (legal);
82 bump_name_once (const std::string& name, char delimiter)
84 string::size_type delim;
87 if ((delim = name.find_last_of (delimiter)) == string::npos) {
93 const char *last_element = name.c_str() + delim + 1;
94 for (size_t i = 0; i < strlen(last_element); i++) {
95 if (!isdigit(last_element[i])) {
102 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
104 if (isnumber == 0 || errno != 0) {
105 // last_element is not a number, or is too large
107 newname += delimiter;
112 snprintf (buf, sizeof(buf), "%d", version+1);
114 newname = name.substr (0, delim+1);
124 find_named_node (const XMLNode& node, string name)
127 XMLNodeConstIterator niter;
130 nlist = node.children();
132 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
136 if (child->name() == name) {
145 cmp_nocase (const string& s, const string& s2)
147 string::const_iterator p = s.begin();
148 string::const_iterator p2 = s2.begin();
150 while (p != s.end() && p2 != s2.end()) {
151 if (toupper(*p) != toupper(*p2)) {
152 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
158 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
162 touch_file (string path)
164 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
173 region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
175 path = PBD::basename_nosuffix (path);
177 if (strip_channels) {
179 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
181 string::size_type len = path.length();
183 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
184 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
186 path = path.substr (0, path.length() - 2);
190 if (add_channel_suffix) {
195 path += (char) ('a' + this_one);
197 path += (char) (this_one == 0 ? 'L' : 'R');
205 path_is_paired (string path, string& pair_base)
207 string::size_type pos;
209 /* remove any leading path */
211 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
212 path = path.substr(pos+1);
215 /* remove filename suffixes etc. */
217 if ((pos = path.find_last_of ('.')) != string::npos) {
218 path = path.substr (0, pos);
221 string::size_type len = path.length();
223 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
225 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
226 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
228 pair_base = path.substr (0, len-2);
237 path_expand (string path)
243 /* tilde expansion */
245 if (path[0] == '~') {
246 if (path.length() == 1) {
247 return Glib::get_home_dir();
250 if (path[1] == '/') {
251 path.replace (0, 1, Glib::get_home_dir());
253 /* can't handle ~roger, so just leave it */
257 /* now do $VAR substitution, since wordexp isn't reliable */
259 regex_t compiled_pattern;
260 const int nmatches = 100;
261 regmatch_t matches[nmatches];
263 if (regcomp (&compiled_pattern, "\\$([a-zA-Z_][a-zA-Z0-9_]*|\\{[a-zA-Z_][a-zA-Z0-9_]*\\})", REG_EXTENDED)) {
264 cerr << "bad regcomp\n";
270 if (regexec (&compiled_pattern, path.c_str(), nmatches, matches, 0)) {
274 /* matches[0] gives the entire match */
276 string match = path.substr (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so);
278 /* try to get match from the environment */
280 if (match[1] == '{') {
282 match = match.substr (2, match.length() - 3);
285 char* matched_value = getenv (match.c_str());
288 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, matched_value);
290 path.replace (matches[0].rm_so, matches[0].rm_eo - matches[0].rm_so, string());
293 /* go back and do it again with whatever remains after the
298 regfree (&compiled_pattern);
302 char buf[PATH_MAX+1];
304 if (realpath (path.c_str(), buf)) {
312 search_path_expand (string path)
321 split (path, s, ':');
323 for (vector<string>::iterator i = s.begin(); i != s.end(); ++i) {
324 string exp = path_expand (*i);
332 for (vector<string>::iterator i = n.begin(); i != n.end(); ++i) {
344 CFStringRefToStdString(CFStringRef stringRef)
347 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
348 kCFStringEncodingUTF8);
349 char *buf = new char[size];
353 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
362 compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
366 step = 1.0/(nframes-1);
370 for (framecnt_t i = 1; i < nframes - 1; ++i) {
371 in[i] = in[i-1] + step;
376 const float pan_law_attenuation = -3.0f;
377 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
379 for (framecnt_t n = 0; n < nframes; ++n) {
381 float outVal = 1 - inVal;
382 out[n] = outVal * (scale * outVal + 1.0f - scale);
383 in[n] = inVal * (scale * inVal + 1.0f - scale);
388 string_to_edit_mode (string str)
390 if (str == _("Splice")) {
392 } else if (str == _("Slide")) {
394 } else if (str == _("Lock")) {
397 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
403 edit_mode_to_string (EditMode mode)
419 string_to_sync_source (string str)
421 if (str == _("MIDI Timecode") || str == _("MTC")) {
425 if (str == _("MIDI Clock")) {
429 if (str == _("JACK")) {
433 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
438 /** @param sh Return a short version of the string */
440 sync_source_to_string (SyncSource src, bool sh)
450 return _("MIDI Timecode");
454 return _("MIDI Clock");
459 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
464 meter_falloff_to_float (MeterFalloff falloff)
467 case MeterFalloffOff:
468 return METER_FALLOFF_OFF;
469 case MeterFalloffSlowest:
470 return METER_FALLOFF_SLOWEST;
471 case MeterFalloffSlow:
472 return METER_FALLOFF_SLOW;
473 case MeterFalloffMedium:
474 return METER_FALLOFF_MEDIUM;
475 case MeterFalloffFast:
476 return METER_FALLOFF_FAST;
477 case MeterFalloffFaster:
478 return METER_FALLOFF_FASTER;
479 case MeterFalloffFastest:
480 return METER_FALLOFF_FASTEST;
482 return METER_FALLOFF_FAST;
487 meter_falloff_from_float (float val)
489 if (val == METER_FALLOFF_OFF) {
490 return MeterFalloffOff;
492 else if (val <= METER_FALLOFF_SLOWEST) {
493 return MeterFalloffSlowest;
495 else if (val <= METER_FALLOFF_SLOW) {
496 return MeterFalloffSlow;
498 else if (val <= METER_FALLOFF_MEDIUM) {
499 return MeterFalloffMedium;
501 else if (val <= METER_FALLOFF_FAST) {
502 return MeterFalloffFast;
504 else if (val <= METER_FALLOFF_FASTER) {
505 return MeterFalloffFaster;
508 return MeterFalloffFastest;
513 ARDOUR::string_to_auto_state (std::string str)
515 if (str == X_("Off")) {
517 } else if (str == X_("Play")) {
519 } else if (str == X_("Write")) {
521 } else if (str == X_("Touch")) {
525 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
531 ARDOUR::auto_state_to_string (AutoState as)
533 /* to be used only for XML serialization, no i18n done */
549 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
555 ARDOUR::string_to_auto_style (std::string str)
557 if (str == X_("Absolute")) {
559 } else if (str == X_("Trim")) {
563 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
569 ARDOUR::auto_style_to_string (AutoStyle as)
571 /* to be used only for XML serialization, no i18n done */
575 return X_("Absolute");
582 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
588 bool_as_string (bool yn)
590 return (yn ? "yes" : "no");
594 native_header_format_extension (HeaderFormat hf, const DataType& type)
596 if (type == DataType::MIDI) {
617 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
623 matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
625 string bws = basename_nosuffix (path);
626 struct dirent* dentry;
631 if ((dead = ::opendir (dir.c_str())) == 0) {
632 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
636 while ((dentry = ::readdir (dead)) != 0) {
638 /* avoid '.' and '..' */
640 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
641 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
645 string fullpath = Glib::build_filename (dir, dentry->d_name);
647 if (::stat (fullpath.c_str(), &statbuf)) {
651 if (!S_ISREG (statbuf.st_mode)) {
655 string bws2 = basename_nosuffix (dentry->d_name);
668 how_many_dsp_threads ()
670 /* CALLER MUST HOLD PROCESS LOCK */
672 int num_cpu = hardware_concurrency();
673 int pu = Config->get_processor_usage ();
674 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
677 /* pu is negative: use "pu" less cores for DSP than appear to be available
681 num_threads = num_cpu + pu;
684 } else if (pu == 0) {
686 /* use all available CPUs
689 num_threads = num_cpu;
692 /* use "pu" cores, if available
695 num_threads = min (num_cpu, pu);
701 double gain_to_slider_position_with_max (double g, double max_gain)
703 return gain_to_slider_position (g * 2.0/max_gain);
706 double slider_position_to_gain_with_max (double g, double max_gain)
708 return slider_position_to_gain (g * max_gain/2.0);
712 void c_stacktrace() { stacktrace (cerr); }