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.
20 #define __STDC_FORMAT_MACROS 1
23 #include <cstdio> /* for sprintf */
30 #include <sys/types.h>
40 #include <pbd/error.h>
41 #include <pbd/stacktrace.h>
42 #include <pbd/xml++.h>
43 #include <pbd/basename.h>
44 #include <ardour/utils.h>
48 using namespace ARDOUR;
54 legalize_for_path (ustring str)
56 ustring::size_type pos;
57 ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
63 while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
64 legal.replace (pos, 1, "_");
71 string bump_name_once(std::string name)
73 string::size_type period;
76 if ((period = name.find_last_of ('.')) == string::npos) {
81 const char *last_element = name.c_str() + period + 1;
82 for (size_t i = 0; i < strlen(last_element); i++) {
83 if (!isdigit(last_element[i])) {
90 long int version = strtol (name.c_str()+period+1, (char **)NULL, 10);
92 if (isnumber == 0 || errno != 0) {
93 // last_element is not a number, or is too large
99 snprintf (buf, sizeof(buf), "%ld", version+1);
101 newname = name.substr (0, period+1);
111 operator<< (ostream& o, const BBT_Time& bbt)
113 o << bbt.bars << '|' << bbt.beats << '|' << bbt.ticks;
118 find_named_node (const XMLNode& node, string name)
121 XMLNodeConstIterator niter;
124 nlist = node.children();
126 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
130 if (child->name() == name) {
139 cmp_nocase (const string& s, const string& s2)
141 string::const_iterator p = s.begin();
142 string::const_iterator p2 = s2.begin();
144 while (p != s.end() && p2 != s2.end()) {
145 if (toupper(*p) != toupper(*p2)) {
146 return (toupper(*p) < toupper(*p2)) ? -1 : 1;
152 return (s2.size() == s.size()) ? 0 : (s.size() < s2.size()) ? -1 : 1;
156 touch_file (ustring path)
158 int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
167 region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
169 path = PBD::basename_nosuffix (path);
171 if (strip_channels) {
173 /* remove any "?R", "?L" or "?[a-z]" channel identifier */
175 ustring::size_type len = path.length();
177 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
178 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
180 path = path.substr (0, path.length() - 2);
184 if (add_channel_suffix) {
189 path += (char) ('a' + this_one);
191 path += (char) (this_one == 0 ? 'L' : 'R');
199 path_is_paired (ustring path, ustring& pair_base)
201 ustring::size_type pos;
203 /* remove any leading path */
205 if ((pos = path.find_last_of ('/')) != string::npos) {
206 path = path.substr(pos+1);
209 /* remove filename suffixes etc. */
211 if ((pos = path.find_last_of ('.')) != string::npos) {
212 path = path.substr (0, pos);
215 ustring::size_type len = path.length();
217 /* look for possible channel identifier: "?R", "%R", ".L" etc. */
219 if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') &&
220 (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
222 pair_base = path.substr (0, len-2);
231 path_expand (ustring path)
234 /* Handle tilde and environment variable expansion in session path */
238 switch (wordexp (path.c_str(), &expansion, WRDE_NOCMD|WRDE_UNDEF)) {
242 error << string_compose (_("illegal or badly-formed string used for path (%1)"), path) << endmsg;
246 if (expansion.we_wordc > 1) {
247 error << string_compose (_("path (%1) is ambiguous"), path) << endmsg;
251 ret = expansion.we_wordv[0];
253 wordfree (&expansion);
261 #if defined(HAVE_COREAUDIO) || defined(HAVE_AUDIOUNITS)
263 CFStringRefToStdString(CFStringRef stringRef)
266 CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) ,
267 kCFStringEncodingUTF8);
268 char *buf = new char[size];
272 if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingUTF8)) {
278 #endif // HAVE_COREAUDIO
281 compute_equal_power_fades (nframes_t nframes, float* in, float* out)
285 step = 1.0/(nframes-1);
289 for (nframes_t i = 1; i < nframes - 1; ++i) {
290 in[i] = in[i-1] + step;
295 const float pan_law_attenuation = -3.0f;
296 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
298 for (nframes_t n = 0; n < nframes; ++n) {
300 float outVal = 1 - inVal;
301 out[n] = outVal * (scale * outVal + 1.0f - scale);
302 in[n] = inVal * (scale * inVal + 1.0f - scale);
307 string_to_edit_mode (string str)
309 if (str == _("Splice Edit")) {
311 } else if (str == _("Slide Edit")) {
313 } else if (str == _("Lock Edit")) {
316 fatal << string_compose (_("programming error: unknown edit mode string \"%1\""), str) << endmsg;
322 edit_mode_to_string (EditMode mode)
326 return _("Slide Edit");
329 return _("Lock Edit");
333 return _("Splice Edit");
338 string_to_slave_source (string str)
340 if (str == _("Internal")) {
344 if (str == _("MTC")) {
348 if (str == _("MIDI Clock")) {
352 if (str == _("JACK")) {
356 fatal << string_compose (_("programming error: unknown slave source string \"%1\""), str) << endmsg;
362 slave_source_to_string (SlaveSource src)
372 return _("MIDI Clock");
376 return _("Internal");
381 /* I don't really like hard-coding these falloff rates here
382 * Probably should use a map of some kind that could be configured
383 * These rates are db/sec.
386 #define METER_FALLOFF_OFF 0.0f
387 #define METER_FALLOFF_SLOWEST 6.6f // BBC standard
388 #define METER_FALLOFF_SLOW 8.6f // BBC standard
389 #define METER_FALLOFF_MEDIUM 20.0f
390 #define METER_FALLOFF_FAST 32.0f
391 #define METER_FALLOFF_FASTER 46.0f
392 #define METER_FALLOFF_FASTEST 70.0f
395 meter_falloff_to_float (MeterFalloff falloff)
398 case MeterFalloffOff:
399 return METER_FALLOFF_OFF;
400 case MeterFalloffSlowest:
401 return METER_FALLOFF_SLOWEST;
402 case MeterFalloffSlow:
403 return METER_FALLOFF_SLOW;
404 case MeterFalloffMedium:
405 return METER_FALLOFF_MEDIUM;
406 case MeterFalloffFast:
407 return METER_FALLOFF_FAST;
408 case MeterFalloffFaster:
409 return METER_FALLOFF_FASTER;
410 case MeterFalloffFastest:
411 return METER_FALLOFF_FASTEST;
413 return METER_FALLOFF_FAST;
418 meter_falloff_from_float (float val)
420 if (val == METER_FALLOFF_OFF) {
421 return MeterFalloffOff;
423 else if (val <= METER_FALLOFF_SLOWEST) {
424 return MeterFalloffSlowest;
426 else if (val <= METER_FALLOFF_SLOW) {
427 return MeterFalloffSlow;
429 else if (val <= METER_FALLOFF_MEDIUM) {
430 return MeterFalloffMedium;
432 else if (val <= METER_FALLOFF_FAST) {
433 return MeterFalloffFast;
435 else if (val <= METER_FALLOFF_FASTER) {
436 return MeterFalloffFaster;
439 return MeterFalloffFastest;
444 meter_hold_to_float (MeterHold hold)
451 case MeterHoldMedium:
460 ARDOUR::string_to_auto_state (std::string str)
462 if (str == X_("Off")) {
464 } else if (str == X_("Play")) {
466 } else if (str == X_("Write")) {
468 } else if (str == X_("Touch")) {
472 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
478 ARDOUR::auto_state_to_string (AutoState as)
480 /* to be used only for XML serialization, no i18n done */
496 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
502 ARDOUR::string_to_auto_style (std::string str)
504 if (str == X_("Absolute")) {
506 } else if (str == X_("Trim")) {
510 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
516 ARDOUR::auto_style_to_string (AutoStyle as)
518 /* to be used only for XML serialization, no i18n done */
522 return X_("Absolute");
529 fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
535 void c_stacktrace() { stacktrace (cerr); }