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 */
33 #include <sys/types.h>
40 #include <glibmm/miscutils.h>
41 #include <glibmm/fileutils.h>
48 #include "pbd/error.h"
49 #include "pbd/stacktrace.h"
50 #include "pbd/xml++.h"
51 #include "pbd/basename.h"
52 #include "pbd/strsplit.h"
54 #include "ardour/utils.h"
55 #include "ardour/rc_configuration.h"
59 using namespace ARDOUR;
64 legalize_for_path (const string& str)
66 #if OLD_SCHOOL_PROHIBITIVE
67 string::size_type pos;
68 string legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
74 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
75 legal.replace (pos, 1, "_");
81 string::size_type pos;
82 string illegal_chars = "/\\"; /* DOS, POSIX. Yes, we're going to ignore HFS */
88 while ((pos = legal.find_first_of (illegal_chars, pos)) != string::npos) {
89 legal.replace (pos, 1, "_");
98 bump_name_once (const std::string& name, char delimiter)
100 string::size_type delim;
103 if ((delim = name.find_last_of (delimiter)) == string::npos) {
105 newname += delimiter;
109 const char *last_element = name.c_str() + delim + 1;
110 for (size_t i = 0; i < strlen(last_element); i++) {
111 if (!isdigit(last_element[i])) {
118 int32_t version = strtol (name.c_str()+delim+1, (char **)NULL, 10);
120 if (isnumber == 0 || errno != 0) {
121 // last_element is not a number, or is too large
123 newname += delimiter;
128 snprintf (buf, sizeof(buf), "%d", version+1);
130 newname = name.substr (0, delim+1);
140 could_be_a_valid_path (const string& path)
142 vector<string> posix_dirs;
143 vector<string> dos_dirs;
146 split (path, posix_dirs, '/');
147 split (path, dos_dirs, '\\');
149 /* remove the last component of each */
151 posix_dirs.erase (--posix_dirs.end());
152 dos_dirs.erase (--dos_dirs.end());
154 if (G_DIR_SEPARATOR == '/') {
155 for (vector<string>::iterator x = posix_dirs.begin(); x != posix_dirs.end(); ++x) {
156 testpath = Glib::build_filename (testpath, *x);
157 cerr << "Testing " << testpath << endl;
158 if (!Glib::file_test (testpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
164 if (G_DIR_SEPARATOR == '\\') {
166 for (vector<string>::iterator x = dos_dirs.begin(); x != dos_dirs.end(); ++x) {
167 testpath = Glib::build_filename (testpath, *x);
168 cerr << "Testing " << testpath << endl;
169 if (!Glib::file_test (testpath, Glib::FILE_TEST_IS_DIR|Glib::FILE_TEST_EXISTS)) {
180 find_named_node (const XMLNode& node, string name)
183 XMLNodeConstIterator niter;
186 nlist = node.children();
188 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
192 if (child->name() == name) {
201 cmp_nocase (const string& s, const string& s2)
203 string::const_iterator p = s.begin();
204 string::const_iterator p2 = s2.begin();
206 while (p != s.end() && p2 != s2.end()) {
207 if (toupper(*p) != toupper(*p2)) {
208 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
214 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
218 touch_file (string path)
220 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
229 region_name_from_path (string path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
231 path = PBD::basename_nosuffix (path);
233 if (strip_channels) {
235 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
237 string::size_type len = path.length();
239 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
240 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
242 path = path.substr (0, path.length() - 2);
246 if (add_channel_suffix) {
251 path += (char) ('a' + this_one);
253 path += (char) (this_one == 0 ? 'L' : 'R');
261 path_is_paired (string path, string& pair_base)
263 string::size_type pos;
265 /* remove any leading path */
267 if ((pos = path.find_last_of (G_DIR_SEPARATOR)) != string::npos) {
268 path = path.substr(pos+1);
271 /* remove filename suffixes etc. */
273 if ((pos = path.find_last_of ('.')) != string::npos) {
274 path = path.substr (0, pos);
277 string::size_type len = path.length();
279 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
281 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
282 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
284 pair_base = path.substr (0, len-2);
293 path_expand (string path)
300 /* Handle tilde and environment variable expansion in session path */
304 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
308 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
312 if (expansion.we_wordc > 1) {
313 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
317 ret = expansion.we_wordv[0];
319 wordfree (&expansion);
329 CFStringRefToStdString(CFStringRef stringRef)
332 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
333 kCFStringEncodingUTF8);
334 char *buf = new char[size];
338 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
347 compute_equal_power_fades (framecnt_t nframes, float* in, float* out)
351 step = 1.0/(nframes-1);
355 for (framecnt_t i = 1; i < nframes - 1; ++i) {
356 in[i] = in[i-1] + step;
361 const float pan_law_attenuation = -3.0f;
362 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
364 for (framecnt_t n = 0; n < nframes; ++n) {
366 float outVal = 1 - inVal;
367 out[n] = outVal * (scale * outVal + 1.0f - scale);
368 in[n] = inVal * (scale * inVal + 1.0f - scale);
373 string_to_edit_mode (string str)
375 if (str == _("Splice")) {
377 } else if (str == _("Slide")) {
379 } else if (str == _("Lock")) {
382 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
388 edit_mode_to_string (EditMode mode)
404 string_to_sync_source (string str)
406 if (str == _("MIDI Timecode") || str == _("MTC")) {
410 if (str == _("MIDI Clock")) {
414 if (str == _("JACK")) {
418 fatal << string_compose (_("programming error: unknown sync source string \"%1\""), str) << endmsg;
423 /** @param sh Return a short version of the string */
425 sync_source_to_string (SyncSource src, bool sh)
435 return _("MIDI Timecode");
439 return _("MIDI Clock");
441 /* GRRRR .... stupid, stupid gcc - you can't get here from there, all enum values are handled */
446 meter_falloff_to_float (MeterFalloff falloff)
449 case MeterFalloffOff:
450 return METER_FALLOFF_OFF;
451 case MeterFalloffSlowest:
452 return METER_FALLOFF_SLOWEST;
453 case MeterFalloffSlow:
454 return METER_FALLOFF_SLOW;
455 case MeterFalloffMedium:
456 return METER_FALLOFF_MEDIUM;
457 case MeterFalloffFast:
458 return METER_FALLOFF_FAST;
459 case MeterFalloffFaster:
460 return METER_FALLOFF_FASTER;
461 case MeterFalloffFastest:
462 return METER_FALLOFF_FASTEST;
464 return METER_FALLOFF_FAST;
469 meter_falloff_from_float (float val)
471 if (val == METER_FALLOFF_OFF) {
472 return MeterFalloffOff;
474 else if (val <= METER_FALLOFF_SLOWEST) {
475 return MeterFalloffSlowest;
477 else if (val <= METER_FALLOFF_SLOW) {
478 return MeterFalloffSlow;
480 else if (val <= METER_FALLOFF_MEDIUM) {
481 return MeterFalloffMedium;
483 else if (val <= METER_FALLOFF_FAST) {
484 return MeterFalloffFast;
486 else if (val <= METER_FALLOFF_FASTER) {
487 return MeterFalloffFaster;
490 return MeterFalloffFastest;
495 ARDOUR::string_to_auto_state (std::string str)
497 if (str == X_("Off")) {
499 } else if (str == X_("Play")) {
501 } else if (str == X_("Write")) {
503 } else if (str == X_("Touch")) {
507 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
513 ARDOUR::auto_state_to_string (AutoState as)
515 /* to be used only for XML serialization, no i18n done */
531 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
537 ARDOUR::string_to_auto_style (std::string str)
539 if (str == X_("Absolute")) {
541 } else if (str == X_("Trim")) {
545 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
551 ARDOUR::auto_style_to_string (AutoStyle as)
553 /* to be used only for XML serialization, no i18n done */
557 return X_("Absolute");
564 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
570 bool_as_string (bool yn)
572 return (yn ? "yes" : "no");
576 string_is_affirmative (const std::string& str)
578 /* to be used only with XML data - not intended to handle user input */
580 return str == "1" || str == "y" || str == "Y" || (!g_strncasecmp(str.c_str(), "yes", str.length()));
584 native_header_format_extension (HeaderFormat hf, const DataType& type)
586 if (type == DataType::MIDI) {
607 fatal << string_compose (_("programming error: unknown native header format: %1"), hf);
613 matching_unsuffixed_filename_exists_in (const string& dir, const string& path)
615 string bws = basename_nosuffix (path);
616 struct dirent* dentry;
621 if ((dead = ::opendir (dir.c_str())) == 0) {
622 error << string_compose (_("cannot open directory %1 (%2)"), dir, strerror (errno)) << endl;
626 while ((dentry = ::readdir (dead)) != 0) {
628 /* avoid '.' and '..' */
630 if ((dentry->d_name[0] == '.' && dentry->d_name[1] == '\0') ||
631 (dentry->d_name[2] == '\0' && dentry->d_name[0] == '.' && dentry->d_name[1] == '.')) {
635 string fullpath = Glib::build_filename (dir, dentry->d_name);
637 if (::stat (fullpath.c_str(), &statbuf)) {
641 if (!S_ISREG (statbuf.st_mode)) {
645 string bws2 = basename_nosuffix (dentry->d_name);
658 how_many_dsp_threads ()
660 int num_cpu = hardware_concurrency();
661 int pu = Config->get_processor_usage ();
662 uint32_t num_threads = max (num_cpu - 1, 2); // default to number of cpus minus one, or 2, whichever is larger
665 /* pu is negative: use "pu" less cores for DSP than appear to be available
669 num_threads = num_cpu + pu;
672 } else if (pu == 0) {
674 /* use all available CPUs
677 num_threads = num_cpu;
680 /* use "pu" cores, if available
683 num_threads = min (num_cpu, pu);
690 void c_stacktrace() { stacktrace (cerr); }