interpolation.cc/.h: Spline-Bugfixes: Crash bug at tempos close to 0, wrong calculati...
[ardour.git] / libs / ardour / location.cc
index bec87e5dd6b6d41863304aa3e5e0183720bdde81..b66d0f3942606ad52982f717e34644bdb0de7878 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 <algorithm>
 #include <ctime>
 #include <sigc++/bind.h>
 
-#include <pbd/stl_delete.h>
-#include <pbd/xml++.h>
-#include <pbd/enumwriter.h>
+#include "pbd/stl_delete.h"
+#include "pbd/xml++.h"
+#include "pbd/enumwriter.h"
 
-#include <ardour/location.h>
-#include <ardour/session.h>
-#include <ardour/audiofilesource.h>
+#include "ardour/location.h"
+#include "ardour/session.h"
+#include "ardour/audiofilesource.h"
 
 #include "i18n.h"
 
@@ -44,7 +43,8 @@ using namespace sigc;
 using namespace PBD;
 
 Location::Location (const Location& other)
-       : _name (other._name),
+       : StatefulDestructible(),
+         _name (other._name),
          _start (other._start),
          _end (other._end),
          _flags (other._flags)
@@ -53,6 +53,10 @@ Location::Location (const Location& other)
 
        _flags = Flags (_flags & ~IsStart);
        _flags = Flags (_flags & ~IsEnd);
+
+       /* copy is not locked even if original was */
+
+       _locked = false;
 }
 
 Location::Location (const XMLNode& node)
@@ -74,14 +78,22 @@ Location::operator= (const Location& other)
        _end = other._end;
        _flags = other._flags;
 
+       /* copy is not locked even if original was */
+
+       _locked = false;
+
        /* "changed" not emitted on purpose */
        
        return this;
 }
 
 int
-Location::set_start (nframes_t s)
+Location::set_start (nframes64_t s)
 {
+       if (_locked) {
+               return -1;
+       }
+
        if (is_mark()) {
                if (_start != s) {
 
@@ -89,6 +101,7 @@ Location::set_start (nframes_t s)
                        _end = s;
 
                        start_changed(this); /* EMIT SIGNAL */
+                       end_changed(this); /* EMIT SIGNAL */
 
                        if ( is_start() ) {
 
@@ -116,13 +129,27 @@ Location::set_start (nframes_t s)
 }
 
 int
-Location::set_end (nframes_t e)
+Location::set_end (nframes64_t e)
 {
+       if (_locked) {
+               return -1;
+       }
+
        if (is_mark()) {
                if (_start != e) {
                        _start = e;
                        _end = e;
+                       start_changed(this); /* EMIT SIGNAL */
                        end_changed(this); /* EMIT SIGNAL */
+
+                       if ( is_start() ) {
+                               Session::StartTimeChanged (); /* EMIT SIGNAL */
+                       }
+
+                       if ( is_end() ) {
+                               Session::EndTimeChanged (); /* EMIT SIGNAL */
+                       }
+
                }
                return 0;
        }
@@ -139,8 +166,12 @@ Location::set_end (nframes_t e)
 }
 
 int
-Location::set (nframes_t start, nframes_t end)
+Location::set (nframes64_t start, nframes64_t end)
 {
+       if (_locked) {
+               return -1;
+       }
+
        if (is_mark() && start != end) {
                return -1;
        } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
@@ -159,6 +190,23 @@ Location::set (nframes_t start, nframes_t end)
        return 0;
 }
 
+int
+Location::move_to (nframes64_t pos) 
+{
+       if (_locked) {
+               return -1;
+       }
+
+       if (_start != pos) {
+               _start = pos;
+               _end = _start + length();
+               
+               changed (this); /* EMIT SIGNAL */
+       }
+       
+       return 0;
+}
+
 void
 Location::set_hidden (bool yn, void *src)
 {
@@ -170,6 +218,14 @@ Location::set_hidden (bool yn, void *src)
 void
 Location::set_cd (bool yn, void *src)
 {
+       // XXX this really needs to be session start
+       // but its not available here - leave to GUI
+
+       if (_start == 0) {
+               error << _("You cannot put a CD marker at this position") << endmsg;
+               return;
+       }
+
        if (set_flag_internal (yn, IsCDMarker)) {
                 FlagsChanged (this, src); /* EMIT SIGNAL */
        }
@@ -191,6 +247,14 @@ Location::set_is_start (bool yn, void *src)
        }
 }
 
+void
+Location::set_is_range_marker (bool yn, void *src)
+{
+       if (set_flag_internal (yn, IsRangeMarker)) {
+                FlagsChanged (this, src); /* EMIT SIGNAL */
+       }
+}
+
 void
 Location::set_auto_punch (bool yn, void *src) 
 {
@@ -272,11 +336,12 @@ Location::get_state (void)
        id().print (buf, sizeof (buf));
        node->add_property("id", buf);
        node->add_property ("name", name());
-       snprintf (buf, sizeof (buf), "%u", start());
+       snprintf (buf, sizeof (buf), "%" PRId64, start());
        node->add_property ("start", buf);
-       snprintf (buf, sizeof (buf), "%u", end());
+       snprintf (buf, sizeof (buf), "%" PRId64, end());
        node->add_property ("end", buf);
        node->add_property ("flags", enum_2_string (_flags));
+       node->add_property ("locked", (_locked ? "yes" : "no"));
 
        return *node;
 }
@@ -320,14 +385,14 @@ Location::set_state (const XMLNode& node)
                   may make the value of _start illegal.
                */
                
-       _start = atoi (prop->value().c_str());
-               
+       sscanf (prop->value().c_str(), "%" PRId64, &_start);
+       
        if ((prop = node.property ("end")) == 0) {
                  error << _("XML node for Location has no end information") << endmsg; 
                  return -1;
        }
                
-       _end = atoi (prop->value().c_str());
+       sscanf (prop->value().c_str(), "%" PRId64, &_end);
                
        if ((prop = node.property ("flags")) == 0) {
                  error << _("XML node for Location has no flags information") << endmsg; 
@@ -336,6 +401,12 @@ Location::set_state (const XMLNode& node)
                
        _flags = Flags (string_2_enum (prop->value(), _flags));
 
+       if ((prop = node.property ("locked")) != 0) {
+               _locked = (prop->value() == "yes");
+       } else {
+               _locked = false;
+       }
+
        for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
                  
                  cd_node = *cd_iter;
@@ -423,7 +494,7 @@ Locations::next_available_name(string& result,string base)
                location =* i;
                temp = location->name();
                if (l && !temp.find(base,0)) {
-                       suffix = atoi(temp.substr(l,3));
+                       suffix = atoi(temp.substr(l,3).c_str());
                        if (suffix) available[suffix] = false;
                }
        }
@@ -583,7 +654,7 @@ Locations::remove (Location *loc)
 }
 
 void
-Locations::location_changed (Location* loc)
+Locations::location_changed (Location* /*loc*/)
 {
        changed (); /* EMIT SIGNAL */
 }
@@ -662,7 +733,7 @@ struct LocationStartLaterComparison
 };
 
 Location *
-Locations::first_location_before (nframes_t frame)
+Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
 {
        LocationList locs;
 
@@ -677,6 +748,9 @@ Locations::first_location_before (nframes_t frame)
        /* locs is now sorted latest..earliest */
        
        for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
+               if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
+                       continue;
+               }
                if (!(*i)->is_hidden() && (*i)->start() < frame) {
                        return (*i);
                }
@@ -686,7 +760,7 @@ Locations::first_location_before (nframes_t frame)
 }
 
 Location *
-Locations::first_location_after (nframes_t frame)
+Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
 {
        LocationList locs;
 
@@ -701,6 +775,9 @@ Locations::first_location_after (nframes_t frame)
        /* locs is now sorted earliest..latest */
        
        for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
+               if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
+                       continue;
+               }
                if (!(*i)->is_hidden() && (*i)->start() > frame) {
                        return (*i);
                }
@@ -709,8 +786,8 @@ Locations::first_location_after (nframes_t frame)
        return 0;
 }
 
-nframes_t
-Locations::first_mark_before (nframes_t frame)
+nframes64_t
+Locations::first_mark_before (nframes64_t frame, bool include_special_ranges)
 {
        LocationList locs;
 
@@ -725,6 +802,9 @@ Locations::first_mark_before (nframes_t frame)
        /* locs is now sorted latest..earliest */
        
        for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
+               if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
+                       continue;
+               }
                if (!(*i)->is_hidden()) {
                        if ((*i)->is_mark()) {
                                /* MARK: start == end */
@@ -746,8 +826,8 @@ Locations::first_mark_before (nframes_t frame)
        return 0;
 }
 
-nframes_t
-Locations::first_mark_after (nframes_t frame)
+nframes64_t
+Locations::first_mark_after (nframes64_t frame, bool include_special_ranges)
 {
        LocationList locs;
 
@@ -762,6 +842,9 @@ Locations::first_mark_after (nframes_t frame)
        /* locs is now sorted earliest..latest */
        
        for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
+               if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
+                       continue;
+               }
                if (!(*i)->is_hidden()) {
                        if ((*i)->is_mark()) {
                                /* MARK, start == end so just compare start */
@@ -850,3 +933,16 @@ Locations::get_location_by_id(PBD::ID id)
 
     return 0;
 }
+
+void
+Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
+{
+       Glib::Mutex::Lock lm (lock);
+
+       for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
+               if ((flags == 0 || (*i)->matches (flags)) && 
+                   ((*i)->start() >= start && (*i)->end() < end)) {
+                       ll.push_back (*i);
+               }
+       }
+}