provide support for playhead-to-next/previous-region-boundary actions, and bindings...
[ardour.git] / libs / ardour / utils.cc
index f346f2a8a627fa2f7938026b1b14587cb4cb67e2..e34fdd75a112fadd0732c5eb44c87c95dd5e8d43 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <cstdio> /* for sprintf */
@@ -37,6 +36,7 @@
 #include <pbd/error.h>
 #include <pbd/stacktrace.h>
 #include <pbd/xml++.h>
+#include <pbd/basename.h>
 #include <ardour/utils.h>
 
 #include "i18n.h"
@@ -44,6 +44,7 @@
 using namespace ARDOUR;
 using namespace std;
 using namespace PBD;
+using Glib::ustring;
 
 void
 elapsed_time_to_str (char *buf, uint32_t seconds)
@@ -87,6 +88,24 @@ elapsed_time_to_str (char *buf, uint32_t seconds)
        }
 }
 
+ustring 
+legalize_for_path (ustring str)
+{
+       ustring::size_type pos;
+       ustring legal_chars = "abcdefghijklmnopqrtsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+=: ";
+       ustring legal;
+
+       legal = str;
+       pos = 0;
+
+       while ((pos = legal.find_first_not_of (legal_chars, pos)) != string::npos) {
+               legal.replace (pos, 1, "_");
+               pos += 1;
+       }
+
+       return legal;
+}
+#if 0
 string 
 legalize_for_path (string str)
 {
@@ -104,6 +123,7 @@ legalize_for_path (string str)
 
        return legal;
 }
+#endif
 
 ostream&
 operator<< (ostream& o, const BBT_Time& bbt)
@@ -177,7 +197,7 @@ tokenize_fullpath (string fullpath, string& path, string& name)
 }
 
 int
-touch_file (string path)
+touch_file (ustring path)
 {
        int fd = open (path.c_str(), O_RDWR|O_CREAT, 0660);
        if (fd >= 0) {
@@ -187,22 +207,42 @@ touch_file (string path)
        return 1;
 }
 
-string
-placement_as_string (Placement p)
+ustring
+region_name_from_path (ustring path, bool strip_channels, bool add_channel_suffix, uint32_t total, uint32_t this_one)
 {
-       switch (p) {
-       case PreFader:
-               return _("pre");
-       default: /* to get g++ to realize we have all the cases covered */
-       case PostFader:
-               return _("post");
+       path = PBD::basename_nosuffix (path);
+
+       if (strip_channels) {
+
+               /* remove any "?R", "?L" or "?[a-z]" channel identifier */
+               
+               ustring::size_type len = path.length();
+               
+               if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') && 
+                   (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
+                       
+                       path = path.substr (0, path.length() - 2);
+               }
        }
-}
 
-string
-region_name_from_path (string path)
+       if (add_channel_suffix) {
+
+               path += '%';
+               
+               if (total > 2) {
+                       path += (char) ('a' + this_one);
+               } else {
+                       path += (char) (this_one == 0 ? 'L' : 'R');
+               }
+       }
+
+       return path;
+}      
+
+bool
+path_is_paired (ustring path, ustring& pair_base)
 {
-       string::size_type pos;
+       ustring::size_type pos;
 
        /* remove filename suffixes etc. */
        
@@ -210,21 +250,23 @@ region_name_from_path (string path)
                path = path.substr (0, pos);
        }
 
-       /* remove any "?R", "?L" or "?[a-z]" channel identifier */
-       
-       string::size_type len = path.length();
-       
-       if (len > 3 && (path[len-2] == '%' || path[len-2] == '?') && 
+       ustring::size_type len = path.length();
+
+       /* look for possible channel identifier: "?R", "%R", ".L" etc. */
+
+       if (len > 3 && (path[len-2] == '%' || path[len-2] == '?' || path[len-2] == '.') && 
            (path[len-1] == 'R' || path[len-1] == 'L' || (islower (path[len-1])))) {
                
-               path = path.substr (0, path.length() - 2);
-       }
+               pair_base = path.substr (0, len-2);
+               return true;
 
-       return path;
-}      
+       } 
+
+       return false;
+}
 
-string
-path_expand (string path)
+ustring
+path_expand (ustring path)
 {
 #ifdef HAVE_WORDEXP
        /* Handle tilde and environment variable expansion in session path */
@@ -362,25 +404,65 @@ slave_source_to_string (SlaveSource src)
        }
 }
 
+/* I don't really like hard-coding these falloff rates here
+ * Probably should use a map of some kind that could be configured
+ * These rates are db/sec.
+*/
+
+#define METER_FALLOFF_OFF     0.0f
+#define METER_FALLOFF_SLOWEST 6.6f // BBC standard
+#define METER_FALLOFF_SLOW    8.6f // BBC standard
+#define METER_FALLOFF_MEDIUM  20.0f
+#define METER_FALLOFF_FAST    32.0f
+#define METER_FALLOFF_FASTER  46.0f
+#define METER_FALLOFF_FASTEST 70.0f
+
 float
 meter_falloff_to_float (MeterFalloff falloff)
 {
        switch (falloff) {
        case MeterFalloffOff:
-               return 0.0f;
+               return METER_FALLOFF_OFF;
        case MeterFalloffSlowest:
-               return 1.0f;
+               return METER_FALLOFF_SLOWEST;
        case MeterFalloffSlow:
-               return 2.0f;
+               return METER_FALLOFF_SLOW;
        case MeterFalloffMedium:
-               return 3.0f;
+               return METER_FALLOFF_MEDIUM;
        case MeterFalloffFast:
-               return 4.0f;
+               return METER_FALLOFF_FAST;
        case MeterFalloffFaster:
-               return 5.0f;
+               return METER_FALLOFF_FASTER;
        case MeterFalloffFastest:
+               return METER_FALLOFF_FASTEST;
        default:
-               return 6.0f;
+               return METER_FALLOFF_FAST;
+       }
+}
+
+MeterFalloff
+meter_falloff_from_float (float val)
+{
+       if (val == METER_FALLOFF_OFF) {
+               return MeterFalloffOff;
+       }
+       else if (val <= METER_FALLOFF_SLOWEST) {
+               return MeterFalloffSlowest;
+       }
+       else if (val <= METER_FALLOFF_SLOW) {
+               return MeterFalloffSlow;
+       }
+       else if (val <= METER_FALLOFF_MEDIUM) {
+               return MeterFalloffMedium;
+       }
+       else if (val <= METER_FALLOFF_FAST) {
+               return MeterFalloffFast;
+       }
+       else if (val <= METER_FALLOFF_FASTER) {
+               return MeterFalloffFaster;
+       }
+       else {
+               return MeterFalloffFastest;
        }
 }
 
@@ -415,6 +497,7 @@ ARDOUR::string_to_auto_state (std::string str)
 
        fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState string: ", str) << endmsg;
        /*NOTREACHED*/
+       return Touch;
 }
 
 string 
@@ -435,6 +518,10 @@ ARDOUR::auto_state_to_string (AutoState as)
        case Touch:
                return X_("Touch");
        }
+
+       fatal << string_compose (_("programming error: %1 %2"), "illegal AutoState type: ", as) << endmsg;
+       /*NOTREACHED*/
+       return "";
 }
 
 AutoStyle 
@@ -448,6 +535,7 @@ ARDOUR::string_to_auto_style (std::string str)
 
        fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle string: ", str) << endmsg;
        /*NOTREACHED*/
+       return Trim;
 }
 
 string 
@@ -463,6 +551,10 @@ ARDOUR::auto_style_to_string (AutoStyle as)
                return X_("Trim");
                break;
        }
+
+       fatal << string_compose (_("programming error: %1 %2"), "illegal AutoStyle type: ", as) << endmsg;
+       /*NOTREACHED*/
+       return "";
 }
 
 extern "C" {