totally untested initial code for MidiPlaylistSource, to provide for compound MIDI...
[ardour.git] / libs / ardour / location.cc
index d9d5911555c612dcce670367afd78ccf3a3ce308..086b6ed0c891ab21f6a59365c04c2288234a032d 100644 (file)
@@ -50,10 +50,11 @@ Location::Location (Session& s)
        , _locked (false)
        , _position_lock_style (AudioTime)
 {
-
+       assert (_start >= 0);
+       assert (_end >= 0);
 }
 
-Location::Location (Session& s, nframes64_t sample_start, nframes64_t sample_end, const std::string &name, Flags bits)
+Location::Location (Session& s, framepos_t sample_start, framepos_t sample_end, const std::string &name, Flags bits)
        : SessionHandleRef (s)
        , _name (name)
        , _start (sample_start)
@@ -63,6 +64,9 @@ Location::Location (Session& s, nframes64_t sample_start, nframes64_t sample_end
        , _position_lock_style (AudioTime)
 {
        recompute_bbt_from_frames ();
+
+       assert (_start >= 0);
+       assert (_end >= 0);
 }
 
 Location::Location (const Location& other)
@@ -79,14 +83,25 @@ Location::Location (const Location& other)
        /* copy is not locked even if original was */
 
        _locked = false;
+
+       assert (_start >= 0);
+       assert (_end >= 0);
 }
 
 Location::Location (Session& s, const XMLNode& node)
        : SessionHandleRef (s)
+       , _position_lock_style (AudioTime)
 {
+       /* Note: _position_lock_style is initialised above in case set_state doesn't set it
+          (for 2.X session file compatibility).
+       */
+       
        if (set_state (node, Stateful::loading_state_version)) {
                throw failed_constructor ();
        }
+
+       assert (_start >= 0);
+       assert (_end >= 0);
 }
 
 Location*
@@ -110,6 +125,9 @@ Location::operator= (const Location& other)
 
        /* "changed" not emitted on purpose */
 
+       assert (_start >= 0);
+       assert (_end >= 0);
+
        return this;
 }
 
@@ -119,7 +137,7 @@ Location::operator= (const Location& other)
  *  @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
  */
 int
-Location::set_start (nframes64_t s, bool force, bool allow_bbt_recompute)
+Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
 {
        if (_locked) {
                return -1;
@@ -138,24 +156,34 @@ Location::set_start (nframes64_t s, bool force, bool allow_bbt_recompute)
                        if (allow_bbt_recompute) {
                                recompute_bbt_from_frames ();
                        }
+
                        start_changed (this); /* EMIT SIGNAL */
                        end_changed (this); /* EMIT SIGNAL */
                }
+
+               assert (_start >= 0);
+               assert (_end >= 0);
+               
                return 0;
        }
        
        if (s != _start) {
+
+               framepos_t const old = _start;
+               
                _start = s;
                if (allow_bbt_recompute) {
                        recompute_bbt_from_frames ();
                }
                start_changed (this); /* EMIT SIGNAL */
                if (is_session_range ()) {
-                       Session::StartTimeChanged (); /* EMIT SIGNAL */
+                       Session::StartTimeChanged (old); /* EMIT SIGNAL */
                        AudioFileSource::set_header_position_offset (s);
                }
        }
 
+       assert (_start >= 0);
+       
        return 0;
 }
 
@@ -165,7 +193,7 @@ Location::set_start (nframes64_t s, bool force, bool allow_bbt_recompute)
  *  @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
  */
 int
-Location::set_end (nframes64_t e, bool force, bool allow_bbt_recompute)
+Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
 {
        if (_locked) {
                return -1;
@@ -187,10 +215,16 @@ Location::set_end (nframes64_t e, bool force, bool allow_bbt_recompute)
                        start_changed (this); /* EMIT SIGNAL */
                        end_changed (this); /* EMIT SIGNAL */
                }
+
+               assert (_start >= 0);
+               assert (_end >= 0);
+               
                return 0;
        }
 
        if (e != _end) {
+               framepos_t const old = _end;
+               
                _end = e;
                if (allow_bbt_recompute) {
                        recompute_bbt_from_frames ();
@@ -198,15 +232,17 @@ Location::set_end (nframes64_t e, bool force, bool allow_bbt_recompute)
                end_changed(this); /* EMIT SIGNAL */
 
                if (is_session_range()) {
-                       Session::EndTimeChanged (); /* EMIT SIGNAL */
+                       Session::EndTimeChanged (old); /* EMIT SIGNAL */
                }
        }
 
+       assert (_end >= 0);
+
        return 0;
 }
 
 int
-Location::set (nframes64_t start, nframes64_t end, bool allow_bbt_recompute)
+Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
 {
        /* check validity */
        if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
@@ -221,7 +257,7 @@ Location::set (nframes64_t start, nframes64_t end, bool allow_bbt_recompute)
 }
 
 int
-Location::move_to (nframes64_t pos)
+Location::move_to (framepos_t pos)
 {
        if (_locked) {
                return -1;
@@ -235,6 +271,9 @@ Location::move_to (nframes64_t pos)
                changed (this); /* EMIT SIGNAL */
        }
 
+       assert (_start >= 0);
+       assert (_end >= 0);
+
        return 0;
 }
 
@@ -455,6 +494,9 @@ Location::set_state (const XMLNode& node, int /*version*/)
 
        changed (this); /* EMIT SIGNAL */
 
+       assert (_start >= 0);
+       assert (_end >= 0);
+
        return 0;
 }
 
@@ -683,6 +725,11 @@ Locations::add (Location *loc, bool make_current)
        if (make_current) {
                 current_changed (current_location); /* EMIT SIGNAL */
        }
+
+       if (loc->is_session_range()) {
+               Session::StartTimeChanged (0);
+               Session::EndTimeChanged (1);
+       }
 }
 
 void
@@ -737,7 +784,7 @@ Locations::get_state ()
        LocationList::iterator iter;
        Glib::Mutex::Lock lm (lock);
 
-       for (iter  = locations.begin(); iter != locations.end(); ++iter) {
+       for (iter = locations.begin(); iter != locations.end(); ++iter) {
                node->add_child_nocopy ((*iter)->get_state ());
        }
 
@@ -754,13 +801,15 @@ Locations::set_state (const XMLNode& node, int version)
 
        XMLNodeList nlist = node.children();
 
-       locations.clear ();
+       /* build up a new locations list in here */
+       LocationList new_locations;
+
        current_location = 0;
 
        Location* session_range_location = 0;
        if (version < 3000) {
                session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
-               locations.push_back (session_range_location);
+               new_locations.push_back (session_range_location);
        }
 
        {
@@ -771,7 +820,23 @@ Locations::set_state (const XMLNode& node, int version)
 
                        try {
 
-                               Location *loc = new Location (_session, **niter);
+                               XMLProperty const * prop_id = (*niter)->property ("id");
+                               assert (prop_id);
+                               PBD::ID id (prop_id->value ());
+                               
+                               LocationList::const_iterator i = locations.begin();
+                               while (i != locations.end () && (*i)->id() != id) {
+                                       ++i;
+                               }
+
+                               Location* loc;
+                               if (i != locations.end()) {
+                                       /* we can re-use an old Location object */
+                                       loc = *i;
+                                       loc->set_state (**niter, version);
+                               } else {
+                                       loc = new Location (_session, **niter);
+                               }
 
                                bool add = true;
 
@@ -786,10 +851,10 @@ Locations::set_state (const XMLNode& node, int version)
                                                        string::size_type const c = v.find_first_of (',');
                                                        string const s = v.substr (0, c);
                                                        if (s == X_("IsStart")) {
-                                                               session_range_location->set_start (loc->start());
+                                                               session_range_location->set_start (loc->start(), true);
                                                                add = false;
                                                        } else if (s == X_("IsEnd")) {
-                                                               session_range_location->set_end (loc->start());
+                                                               session_range_location->set_end (loc->start(), true);
                                                                add = false;
                                                        }
 
@@ -803,7 +868,7 @@ Locations::set_state (const XMLNode& node, int version)
                                }
 
                                if (add) {
-                                       locations.push_back (loc);
+                                       new_locations.push_back (loc);
                                }
                        }
 
@@ -812,6 +877,8 @@ Locations::set_state (const XMLNode& node, int version)
                        }
                }
 
+               locations = new_locations;
+
                if (locations.size()) {
                        current_location = locations.front();
                } else {
@@ -839,7 +906,7 @@ struct LocationStartLaterComparison
 };
 
 Location *
-Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
+Locations::first_location_before (framepos_t frame, bool include_special_ranges)
 {
        LocationList locs;
 
@@ -866,7 +933,7 @@ Locations::first_location_before (nframes64_t frame, bool include_special_ranges
 }
 
 Location *
-Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
+Locations::first_location_after (framepos_t frame, bool include_special_ranges)
 {
        LocationList locs;
 
@@ -896,13 +963,13 @@ Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
  *  side of a frame.  Note that if frame is exactly on a `mark', that mark will not be considered for returning
  *  as before/after.
  *  @param frame Frame to look for.
- *  @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
- *  @param after Filled in with the position of the next `mark' after `frame' (or max_frames if none exists)
+ *  @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
+ *  @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
  */
 void
-Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
+Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
 {
-       before = after = max_frames;
+       before = after = max_framepos;
        
        LocationList locs;
 
@@ -913,7 +980,7 @@ Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nfra
 
        /* Get a list of positions; don't store any that are exactly on our requested position */
        
-       std::list<nframes64_t> positions;
+       std::list<framepos_t> positions;
 
        for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
                if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
@@ -942,7 +1009,7 @@ Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nfra
 
        positions.sort ();
 
-       std::list<nframes64_t>::iterator i = positions.begin ();
+       std::list<framepos_t>::iterator i = positions.begin ();
        while (i != positions.end () && *i < frame) {
                ++i;
        }
@@ -1022,7 +1089,7 @@ Locations::get_location_by_id(PBD::ID id)
 }
 
 void
-Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
+Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
 {
        Glib::Mutex::Lock lm (lock);