2 * Copyright (C) 2000-2018 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006-2013 David Robillard <d@drobilla.net>
4 * Copyright (C) 2007-2015 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2012-2018 Robin Gareus <robin@gareus.org>
7 * Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
8 * Copyright (C) 2013-2015 John Emmas <john@creativepost.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "libardour-config.h"
31 #include <cstdio> /* for sprintf */
40 #include <sys/types.h>
49 #include "pbd/gstdio_compat.h"
51 #include <glibmm/miscutils.h>
52 #include <glibmm/fileutils.h>
55 #include "pbd/control_math.h"
56 #include "pbd/error.h"
57 #include "pbd/stacktrace.h"
58 #include "pbd/xml++.h"
59 #include "pbd/basename.h"
60 #include "pbd/scoped_file_descriptor.h"
61 #include "pbd/strsplit.h"
62 #include "pbd/replace_all.h"
64 #include "ardour/utils.h"
65 #include "ardour/rc_configuration.h"
69 using namespace ARDOUR;
74 replace_chars (const string& str, const string& illegal_chars)
76 string::size_type pos;
79 /* this is the one place in Ardour where we need to iterate across
80 * potential multibyte characters, and thus we need Glib::ustring
86 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
87 legal.replace (pos, 1, "_");
91 return string (legal);
93 /** take an arbitrary string as an argument, and return a version of it
94 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
95 * and later version of this code. It defines a very small number of characters
96 * that are not allowed in a path on the build target filesystem (basically,
97 * POSIX or Windows) and replaces any instances of them with an underscore.
99 * NOTE: this is intended only to legalize for the filesystem that Ardour
100 * is running on. Export should use legalize_for_universal_path() since
101 * the goal there is to be legal across filesystems.
104 ARDOUR::legalize_for_path (const string& str)
106 return replace_chars (str, "/\\");
109 /** take an arbitrary string as an argument, and return a version of it
110 * suitable for use as a path (directory/folder name). This is the Ardour 3.X
111 * and later version of this code. It defines a small number
112 * of characters that are not allowed in a path on any of our target
113 * filesystems, and replaces any instances of them with an underscore.
115 * NOTE: this is intended to create paths that should be legal on
119 ARDOUR::legalize_for_universal_path (const string& str)
121 return replace_chars (str, "<>:\"/\\|?*");
124 /** Legalize for a URI path component. This is like
125 * legalize_for_universal_path, but stricter, disallowing spaces and hash.
126 * This avoids %20 escapes in URIs, but probably needs work to be more strictly
130 ARDOUR::legalize_for_uri (const string& str)
132 return replace_chars (str, "<>:\"/\\|?* #");
135 /** take an arbitrary string as an argument, and return a version of it
136 * suitable for use as a path (directory/folder name). This is the Ardour 2.X
137 * version of this code, which used an approach that came to be seen as
138 * problematic: defining the characters that were allowed and replacing all
139 * others with underscores. See legalize_for_path() for the 3.X and later
144 ARDOUR::legalize_for_path_2X (const string& str)
146 string::size_type pos;
147 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
150 /* this is the one place in Ardour where we need to iterate across
151 * potential multibyte characters, and thus we need Glib::ustring
157 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
158 legal.replace (pos, 1, "_");
162 return string (legal);
166 ARDOUR::bump_name_once (const std::string& name, char delimiter)
168 string::size_type delim;
171 if ((delim = name.find_last_of (delimiter)) == string::npos) {
173 newname += delimiter;
177 const char *last_element = name.c_str() + delim + 1;
178 for (size_t i = 0; i < strlen(last_element); i++) {
179 if (!isdigit(last_element[i])) {
186 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
188 if (isnumber == 0 || errno != 0) {
189 // last_element is not a number, or is too large
191 newname += delimiter;
196 snprintf (buf, sizeof(buf), "%d", version+1);
198 newname = name.substr (0, delim+1);
208 ARDOUR::bump_name_number (const std::string& name)
210 size_t pos = name.length();
212 bool have_number = false;
213 while (pos > 0 && isdigit(name.at(--pos))) {
220 int32_t seq = strtol (name.c_str() + num, (char **)NULL, 10);
222 snprintf (buf, sizeof(buf), "%d", seq + 1);
223 newname = name.substr (0, num);
234 ARDOUR::find_named_node (const XMLNode& node, string name)
237 XMLNodeConstIterator niter;
240 nlist = node.children();
242 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
246 if (child->name() == name) {
255 ARDOUR::cmp_nocase (const string& s, const string& s2)
257 string::const_iterator p = s.begin();
258 string::const_iterator p2 = s2.begin();
260 while (p != s.end() && p2 != s2.end()) {
261 if (toupper(*p) != toupper(*p2)) {
262 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
268 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
272 ARDOUR::cmp_nocase_utf8 (const string& s1, const string& s2)
274 const char *cstr1 = s1.c_str();
275 const char *cstr2 = s2.c_str();
276 gchar *cstr1folded = NULL;
277 gchar *cstr2folded = NULL;
280 if (!g_utf8_validate (cstr1, -1, NULL) ||
281 !g_utf8_validate (cstr2, -1, NULL)) {
282 // fall back to comparing ASCII
283 return g_ascii_strcasecmp (cstr1, cstr2);
286 cstr1folded = g_utf8_casefold (cstr1, -1);
287 cstr2folded = g_utf8_casefold (cstr2, -1);
289 if (cstr1folded && cstr2folded) {
290 retval = strcmp (cstr1folded, cstr2folded);
292 // this shouldn't happen, make the best of it
293 retval = g_ascii_strcasecmp (cstr1, cstr2);
297 g_free (cstr1folded);
301 g_free (cstr2folded);
308 ARDOUR::region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
310 path = PBD::basename_nosuffix (path);
312 if (strip_channels) {
314 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
316 string::size_type len = path.length();
318 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
319 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
321 path = path.substr (0, path.length() - 2);
325 if (add_channel_suffix) {
330 path += (char) ('a' + this_one);
332 path += (char) (this_one == 0 ? 'L' : 'R');
340 ARDOUR::path_is_paired (string path, string& pair_base)
342 string::size_type pos;
344 /* remove any leading path */
346 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
347 path = path.substr(pos+1);
350 /* remove filename suffixes etc. */
352 if ((pos = path.find_last_of ('.')) != string::npos) {
353 path = path.substr (0, pos);
356 string::size_type len = path.length();
358 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
360 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
361 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
363 pair_base = path.substr (0, len-2);
373 ARDOUR::CFStringRefToStdString(CFStringRef stringRef)
376 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
377 kCFStringEncodingUTF8);
378 char *buf = new char[size];
382 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
391 ARDOUR::compute_equal_power_fades (samplecnt_t nframes, float* in, float* out)
395 step = 1.0/(nframes-1);
399 for (samplecnt_t i = 1; i < nframes - 1; ++i) {
400 in[i] = in[i-1] + step;
405 const float pan_law_attenuation = -3.0f;
406 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
408 for (samplecnt_t n = 0; n < nframes; ++n) {
410 float outVal = 1 - inVal;
411 out[n] = outVal * (scale * outVal + 1.0f - scale);
412 in[n] = inVal * (scale * inVal + 1.0f - scale);
417 ARDOUR::string_to_edit_mode (string str)
419 if (str == _("Splice")) {
421 } else if (str == _("Slide")) {
423 } else if (str == _("Ripple")) {
425 } else if (str == _("Lock")) {
428 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
429 abort(); /*NOTREACHED*/
434 ARDOUR::edit_mode_to_string (EditMode mode)
453 ARDOUR::meter_falloff_to_float (MeterFalloff falloff)
456 case MeterFalloffOff:
457 return METER_FALLOFF_OFF;
458 case MeterFalloffSlowest:
459 return METER_FALLOFF_SLOWEST;
460 case MeterFalloffSlow:
461 return METER_FALLOFF_SLOW;
462 case MeterFalloffSlowish:
463 return METER_FALLOFF_SLOWISH;
464 case MeterFalloffMedium:
465 return METER_FALLOFF_MEDIUM;
466 case MeterFalloffModerate:
467 return METER_FALLOFF_MODERATE;
468 case MeterFalloffFast:
469 case MeterFalloffFaster: // backwards compat enum MeterFalloff
470 case MeterFalloffFastest:
472 return METER_FALLOFF_FAST;
477 ARDOUR::meter_falloff_from_float (float val)
479 if (val == METER_FALLOFF_OFF) {
480 return MeterFalloffOff;
482 else if (val <= METER_FALLOFF_SLOWEST) {
483 return MeterFalloffSlowest;
485 else if (val <= METER_FALLOFF_SLOW) {
486 return MeterFalloffSlow;
488 else if (val <= METER_FALLOFF_SLOWISH) {
489 return MeterFalloffSlowish;
491 else if (val <= METER_FALLOFF_MODERATE) {
492 return MeterFalloffModerate;
494 else if (val <= METER_FALLOFF_MEDIUM) {
495 return MeterFalloffMedium;
498 return MeterFalloffFast;
503 ARDOUR::string_to_auto_state (std::string str)
505 if (str == X_("Off")) {
507 } else if (str == X_("Play")) {
509 } else if (str == X_("Write")) {
511 } else if (str == X_("Touch")) {
513 } else if (str == X_("Latch")) {
517 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
518 abort(); /*NOTREACHED*/
523 ARDOUR::auto_state_to_string (AutoState as)
525 /* to be used only for XML serialization, no i18n done */
545 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
546 abort(); /*NOTREACHED*/
551 bool_as_string (bool yn)
553 return (yn ? "yes" : "no");
557 ARDOUR::native_header_format_extension (HeaderFormat hf, const DataType& type)
559 if (type == DataType::MIDI) {
584 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
585 abort(); /*NOTREACHED*/
590 ARDOUR::matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
592 string bws = basename_nosuffix (path);
593 struct dirent* dentry;
598 if ((dead = ::opendir (dir.c_str())) == 0) {
599 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
603 while ((dentry = ::readdir (dead)) != 0) {
605 /* avoid '.' and '..' */
607 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
608 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
612 string fullpath = Glib::build_filename (dir, dentry->d_name);
614 if (g_stat (fullpath.c_str(), &statbuf)) {
618 if (!S_ISREG (statbuf.st_mode)) {
622 string bws2 = basename_nosuffix (dentry->d_name);
635 ARDOUR::how_many_dsp_threads ()
637 /* CALLER MUST HOLD PROCESS LOCK */
639 int num_cpu = hardware_concurrency();
640 int pu = Config->get_processor_usage ();
641 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
644 /* pu is negative: use "pu" less cores for DSP than appear to be available
648 num_threads = num_cpu + pu;
651 } else if (pu == 0) {
653 /* use all available CPUs
656 num_threads = num_cpu;
659 /* use "pu" cores, if available
662 num_threads = min (num_cpu, pu);
669 ARDOUR::gain_to_slider_position_with_max (double g, double max_gain)
671 return gain_to_position (g * 2.0 / max_gain);
675 ARDOUR::slider_position_to_gain_with_max (double g, double max_gain)
677 return position_to_gain (g) * max_gain / 2.0;
683 ARDOUR::compute_sha1_of_file (std::string path)
685 PBD::ScopedFileDescriptor fd (g_open (path.c_str(), O_RDONLY, 0444));
687 return std::string ();
695 while ((n_read = ::read(fd, buf, sizeof(buf))) > 0) {
696 sha1_write (&s, (const uint8_t*) buf, n_read);
699 sha1_result_hash (&s, hash);
700 return std::string (hash);
704 void c_stacktrace() { stacktrace (cerr); }