-void
-Playlist::split_region (boost::shared_ptr<Region> region, framepos_t playlist_position)
-{
- RegionLock rl (this);
- _split_region (region, playlist_position);
-}
-
-void
-Playlist::_split_region (boost::shared_ptr<Region> region, framepos_t playlist_position)
-{
- if (!region->covers (playlist_position)) {
- return;
- }
-
- if (region->position() == playlist_position ||
- region->last_frame() == playlist_position) {
- return;
- }
-
- boost::shared_ptr<Region> left;
- boost::shared_ptr<Region> right;
- frameoffset_t before;
- frameoffset_t after;
- string before_name;
- string after_name;
-
- /* split doesn't change anything about length, so don't try to splice */
-
- bool old_sp = _splicing;
- _splicing = true;
-
- before = playlist_position - region->position();
- after = region->length() - before;
-
- RegionFactory::region_name (before_name, region->name(), false);
-
- {
- PropertyList plist;
-
- plist.add (Properties::length, before);
- plist.add (Properties::name, before_name);
- plist.add (Properties::left_of_split, true);
-
- /* note: we must use the version of ::create with an offset here,
- since it supplies that offset to the Region constructor, which
- is necessary to get audio region gain envelopes right.
- */
- left = RegionFactory::create (region, 0, plist);
- }
-
- RegionFactory::region_name (after_name, region->name(), false);
-
- {
- PropertyList plist;
-
- plist.add (Properties::length, after);
- plist.add (Properties::name, after_name);
- plist.add (Properties::right_of_split, true);
-
- /* same note as above */
- right = RegionFactory::create (region, before, plist);
- }
-
- add_region_internal (left, region->position());
- add_region_internal (right, region->position() + before);
-
- uint64_t orig_layer_op = region->last_layer_op();
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
- if ((*i)->last_layer_op() > orig_layer_op) {
- (*i)->set_last_layer_op( (*i)->last_layer_op() + 1 );
- }
- }
-
- left->set_last_layer_op ( orig_layer_op );
- right->set_last_layer_op ( orig_layer_op + 1);
-
- layer_op_counter++;
-
- finalize_split_region (region, left, right);
-
- remove_region_internal (region);
-
- _splicing = old_sp;
-}
-
-void
-Playlist::possibly_splice (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude)
-{
- if (_splicing || in_set_state) {
- /* don't respond to splicing moves or state setting */
- return;
- }
-
- if (_edit_mode == Splice) {
- splice_locked (at, distance, exclude);
- }
-}
-
-void
-Playlist::possibly_splice_unlocked (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude)
-{
- if (_splicing || in_set_state) {
- /* don't respond to splicing moves or state setting */
- return;
- }
-
- if (_edit_mode == Splice) {
- splice_unlocked (at, distance, exclude);
- }
-}
-
-void
-Playlist::splice_locked (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude)
-{
- {
- RegionLock rl (this);
- core_splice (at, distance, exclude);
- }
-}
-
-void
-Playlist::splice_unlocked (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude)
-{
- core_splice (at, distance, exclude);
-}
-
-void
-Playlist::core_splice (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude)
-{
- _splicing = true;
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
-
- if (exclude && (*i) == exclude) {
- continue;
- }
-
- if ((*i)->position() >= at) {
- framepos_t new_pos = (*i)->position() + distance;
- if (new_pos < 0) {
- new_pos = 0;
- } else if (new_pos >= max_frames - (*i)->length()) {
- new_pos = max_frames - (*i)->length();
- }
-
- (*i)->set_position (new_pos, this);
- }
- }
-
- _splicing = false;
-
- notify_length_changed ();
-}
-
-void
-Playlist::region_bounds_changed (const PropertyChange& what_changed, boost::shared_ptr<Region> region)
-{
- if (in_set_state || _splicing || _nudging || _shuffling) {
- return;
- }
-
- if (what_changed.contains (Properties::position)) {
-
- /* remove it from the list then add it back in
- the right place again.
- */
-
- RegionSortByPosition cmp;
-
- RegionList::iterator i = find (regions.begin(), regions.end(), region);
-
- if (i == regions.end()) {
- /* the region bounds are being modified but its not currently
- in the region list. we will use its bounds correctly when/if
- it is added
- */
- return;
- }
-
- regions.erase (i);
- regions.insert (upper_bound (regions.begin(), regions.end(), region, cmp), region);
- }
-
- if (what_changed.contains (Properties::position) || what_changed.contains (Properties::length)) {
-
- frameoffset_t delta = 0;
-
- if (what_changed.contains (Properties::position)) {
- delta = region->position() - region->last_position();
- }
-
- if (what_changed.contains (Properties::length)) {
- delta += region->length() - region->last_length();
- }
-
- if (delta) {
- possibly_splice (region->last_position() + region->last_length(), delta, region);
- }
-
- if (holding_state ()) {
- pending_bounds.push_back (region);
- } else {
- if (_session.config.get_layer_model() == MoveAddHigher) {
- /* it moved or changed length, so change the timestamp */
- timestamp_layer_op (region);
- }
-
- notify_length_changed ();
- relayer ();
- check_dependents (region, false);
- }
- }
-}
-
-void
-Playlist::region_changed_proxy (const PropertyChange& what_changed, boost::weak_ptr<Region> weak_region)
-{
- boost::shared_ptr<Region> region (weak_region.lock());
-
- if (!region) {
- return;
- }
-
- /* this makes a virtual call to the right kind of playlist ... */
-
- region_changed (what_changed, region);
-}
-
-bool
-Playlist::region_changed (const PropertyChange& what_changed, boost::shared_ptr<Region> region)
-{
- PropertyChange our_interests;
- PropertyChange bounds;
- PropertyChange pos_and_length;
- bool save = false;
-
- if (in_set_state || in_flush) {
- return false;
- }
-
- our_interests.add (Properties::muted);
- our_interests.add (Properties::layer);
- our_interests.add (Properties::opaque);
-
- bounds.add (Properties::start);
- bounds.add (Properties::position);
- bounds.add (Properties::length);
-
- pos_and_length.add (Properties::position);
- pos_and_length.add (Properties::length);
-
- if (what_changed.contains (bounds)) {
- region_bounds_changed (what_changed, region);
- save = !(_splicing || _nudging);
- }
-
- if (what_changed.contains (our_interests) && !what_changed.contains (pos_and_length)) {
- check_dependents (region, false);
- }
-
- if (what_changed.contains (Properties::position) && !what_changed.contains (Properties::length)) {
- notify_region_moved (region);
- }
-
-
- /* don't notify about layer changes, since we are the only object that can initiate
- them, and we notify in ::relayer()
- */
-
- if (what_changed.contains (our_interests)) {
- save = true;
- }
-
- return save;
-}
-
-void
-Playlist::drop_regions ()
-{
- RegionLock rl (this);
- regions.clear ();
- all_regions.clear ();
-}
-
-void
-Playlist::clear (bool with_signals)
-{
- {
- RegionLock rl (this);
-
- region_state_changed_connections.drop_connections ();
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
- pending_removes.insert (*i);
- }
-
- regions.clear ();
-
- for (set<boost::shared_ptr<Region> >::iterator s = pending_removes.begin(); s != pending_removes.end(); ++s) {
- remove_dependents (*s);
- }
- }
-
- if (with_signals) {
-
- for (set<boost::shared_ptr<Region> >::iterator s = pending_removes.begin(); s != pending_removes.end(); ++s) {
- RegionRemoved (boost::weak_ptr<Region> (*s)); /* EMIT SIGNAL */
- }
-
- pending_removes.clear ();
- pending_length = false;
- LengthChanged ();
- pending_contents_change = false;
- ContentsChanged ();
- }
-
-}
-
-/***********************************************************************
- FINDING THINGS
- **********************************************************************/
-
-Playlist::RegionList *
-Playlist::regions_at (framepos_t frame)
-
-{
- RegionLock rlock (this);
- return find_regions_at (frame);
-}
-
-boost::shared_ptr<Region>
-Playlist::top_region_at (framepos_t frame)
-
-{
- RegionLock rlock (this);
- RegionList *rlist = find_regions_at (frame);
- boost::shared_ptr<Region> region;
-
- if (rlist->size()) {
- RegionSortByLayer cmp;
- rlist->sort (cmp);
- region = rlist->back();
- }
-
- delete rlist;
- return region;
-}
-
-boost::shared_ptr<Region>
-Playlist::top_unmuted_region_at (framepos_t frame)
-
-{
- RegionLock rlock (this);
- RegionList *rlist = find_regions_at (frame);
-
- for (RegionList::iterator i = rlist->begin(); i != rlist->end(); ) {
-
- RegionList::iterator tmp = i;
- ++tmp;
-
- if ((*i)->muted()) {
- rlist->erase (i);
- }
-
- i = tmp;
- }
-
- boost::shared_ptr<Region> region;
-
- if (rlist->size()) {
- RegionSortByLayer cmp;
- rlist->sort (cmp);
- region = rlist->back();
- }
-
- delete rlist;
- return region;
-}
-
-Playlist::RegionList*
-Playlist::regions_to_read (framepos_t start, framepos_t end)
-{
- /* Caller must hold lock */
-
- RegionList covering;
- set<framepos_t> to_check;
- set<boost::shared_ptr<Region> > unique;
-
- to_check.insert (start);
- to_check.insert (end);
-
- DEBUG_TRACE (DEBUG::AudioPlayback, ">>>>> REGIONS TO READ\n");
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
-
- /* find all/any regions that span start+end */
-
- switch ((*i)->coverage (start, end)) {
- case OverlapNone:
- break;
-
- case OverlapInternal:
- covering.push_back (*i);
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("toread: will cover %1 (OInternal)\n", (*i)->name()));
- break;
-
- case OverlapStart:
- to_check.insert ((*i)->position());
- if ((*i)->position() != 0) {
- to_check.insert ((*i)->position()-1);
- }
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("toread: will check %1 for %2\n", (*i)->position(), (*i)->name()));
- covering.push_back (*i);
- break;
-
- case OverlapEnd:
- to_check.insert ((*i)->last_frame());
- to_check.insert ((*i)->last_frame()+1);
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("toread: will cover %1 (OEnd)\n", (*i)->name()));
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("\ttoread: will check %1 for %2\n", (*i)->last_frame(), (*i)->name()));
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("\ttoread: will check %1 for %2\n", (*i)->last_frame(), (*i)->name()));
- covering.push_back (*i);
- break;
-
- case OverlapExternal:
- covering.push_back (*i);
- to_check.insert ((*i)->position());
- if ((*i)->position() != 0) {
- to_check.insert ((*i)->position()-1);
- }
- to_check.insert ((*i)->last_frame());
- to_check.insert ((*i)->last_frame()+1);
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("toread: will cover %1 (OExt)\n", (*i)->name()));
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("\ttoread: will check %1 for %2\n", (*i)->position(), (*i)->name()));
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("\ttoread: will check %1 for %2\n", (*i)->last_frame(), (*i)->name()));
- break;
- }
-
- /* don't go too far */
-
- if ((*i)->position() > end) {
- break;
- }
- }
-
- RegionList* rlist = new RegionList;
-
- /* find all the regions that cover each position .... */
-
- if (covering.size() == 1) {
-
- rlist->push_back (covering.front());
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("Just one covering region (%1)\n", covering.front()->name()));
-
- } else {
-
- RegionList here;
- for (set<framepos_t>::iterator t = to_check.begin(); t != to_check.end(); ++t) {
-
- here.clear ();
-
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("++++ Considering %1\n", *t));
-
- for (RegionList::iterator x = covering.begin(); x != covering.end(); ++x) {
-
- if ((*x)->covers (*t)) {
- here.push_back (*x);
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("region %1 covers %2\n",
- (*x)->name(),
- (*t)));
- } else {
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("region %1 does NOT covers %2\n",
- (*x)->name(),
- (*t)));
- }
-
- }
-
- RegionSortByLayer cmp;
- here.sort (cmp);
-
- /* ... and get the top/transparent regions at "here" */
-
- for (RegionList::reverse_iterator c = here.rbegin(); c != here.rend(); ++c) {
-
- unique.insert (*c);
-
- if ((*c)->opaque()) {
-
- /* the other regions at this position are hidden by this one */
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("%1 is opaque, ignore all others\n",
- (*c)->name()));
- break;
- }
- }
- }
-
- for (set<boost::shared_ptr<Region> >::iterator s = unique.begin(); s != unique.end(); ++s) {
- rlist->push_back (*s);
- }
-
- if (rlist->size() > 1) {
- /* now sort by time order */
-
- RegionSortByPosition cmp;
- rlist->sort (cmp);
- }
- }
-
- DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("<<<<< REGIONS TO READ returns %1\n", rlist->size()));
-
- return rlist;
-}
-
-Playlist::RegionList *
-Playlist::find_regions_at (framepos_t frame)
-{
- /* Caller must hold lock */
-
- RegionList *rlist = new RegionList;
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
- if ((*i)->covers (frame)) {
- rlist->push_back (*i);
- }
- }
-
- return rlist;
-}
-
-Playlist::RegionList *
-Playlist::regions_touched (framepos_t start, framepos_t end)
-{
- RegionLock rlock (this);
- RegionList *rlist = new RegionList;
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
- if ((*i)->coverage (start, end) != OverlapNone) {
- rlist->push_back (*i);
- }
- }
-
- return rlist;
-}
-
-framepos_t
-Playlist::find_next_transient (framepos_t from, int dir)
-{
- RegionLock rlock (this);
- AnalysisFeatureList points;
- AnalysisFeatureList these_points;
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
- if (dir > 0) {
- if ((*i)->last_frame() < from) {
- continue;
- }
- } else {
- if ((*i)->first_frame() > from) {
- continue;
- }
- }
-
- (*i)->get_transients (these_points);
-
- /* add first frame, just, err, because */
-
- these_points.push_back ((*i)->first_frame());
-
- points.insert (points.end(), these_points.begin(), these_points.end());
- these_points.clear ();
- }
-
- if (points.empty()) {
- return -1;
- }
-
- TransientDetector::cleanup_transients (points, _session.frame_rate(), 3.0);
- bool reached = false;
-
- if (dir > 0) {
- for (AnalysisFeatureList::iterator x = points.begin(); x != points.end(); ++x) {
- if ((*x) >= from) {
- reached = true;
- }
-
- if (reached && (*x) > from) {
- return *x;
- }
- }
- } else {
- for (AnalysisFeatureList::reverse_iterator x = points.rbegin(); x != points.rend(); ++x) {
- if ((*x) <= from) {
- reached = true;
- }
-
- if (reached && (*x) < from) {
- return *x;
- }
- }
- }
-
- return -1;
-}
-
-boost::shared_ptr<Region>
-Playlist::find_next_region (framepos_t frame, RegionPoint point, int dir)
-{
- RegionLock rlock (this);
- boost::shared_ptr<Region> ret;
- framepos_t closest = max_frames;
-
- bool end_iter = false;
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
-
- if(end_iter) break;
-
- frameoffset_t distance;
- boost::shared_ptr<Region> r = (*i);
- framepos_t pos = 0;
-
- switch (point) {
- case Start:
- pos = r->first_frame ();
- break;
- case End:
- pos = r->last_frame ();
- break;
- case SyncPoint:
- pos = r->sync_position ();
- // r->adjust_to_sync (r->first_frame());
- break;
- }
-
- switch (dir) {
- case 1: /* forwards */
-
- if (pos > frame) {
- if ((distance = pos - frame) < closest) {
- closest = distance;
- ret = r;
- end_iter = true;
- }
- }
-
- break;
-
- default: /* backwards */
-
- if (pos < frame) {
- if ((distance = frame - pos) < closest) {
- closest = distance;
- ret = r;
- }
- }
- else {
- end_iter = true;
- }
-
- break;
- }
- }
-
- return ret;
-}
-
-framepos_t
-Playlist::find_next_region_boundary (framepos_t frame, int dir)
-{
- RegionLock rlock (this);
-
- framepos_t closest = max_frames;
- framepos_t ret = -1;
-
- if (dir > 0) {
-
- for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
-
- boost::shared_ptr<Region> r = (*i);
- frameoffset_t distance;
-
- if (r->first_frame() > frame) {
-
- distance = r->first_frame() - frame;
-
- if (distance < closest) {
- ret = r->first_frame();
- closest = distance;
- }
- }
-
- if (r->last_frame () > frame) {
-
- distance = r->last_frame () - frame;
-
- if (distance < closest) {
- ret = r->last_frame ();
- closest = distance;
- }
- }
- }
-
- } else {
-
- for (RegionList::reverse_iterator i = regions.rbegin(); i != regions.rend(); ++i) {
-
- boost::shared_ptr<Region> r = (*i);
- frameoffset_t distance;
-
- if (r->last_frame() < frame) {
-
- distance = frame - r->last_frame();
-
- if (distance < closest) {
- ret = r->last_frame();
- closest = distance;
- }
- }
-
- if (r->first_frame() < frame) {
-
- distance = frame - r->first_frame();
-
- if (distance < closest) {
- ret = r->first_frame();
- closest = distance;
- }
- }
- }
- }
-
- return ret;
-}
-
-
-/***********************************************************************/
-
-
-
-
-void
-Playlist::mark_session_dirty ()
-{
- if (!in_set_state && !holding_state ()) {
- _session.set_dirty();
- }
-}
-
-void
-Playlist::rdiff (vector<StatefulDiffCommand*>& cmds) const
-{
- RegionLock rlock (const_cast<Playlist *> (this));
- Stateful::rdiff (cmds);
-}
-
-void
-Playlist::clear_owned_history ()
-{
- RegionLock rlock (this);
- Stateful::clear_owned_history ();
-}
-
-void
-Playlist::update (const RegionListProperty::ChangeRecord& change)
-{
- DEBUG_TRACE (DEBUG::Properties, string_compose ("Playlist %1 updates from a change record with %2 adds %3 removes\n",
- name(), change.added.size(), change.removed.size()));
-
- freeze ();
- /* add the added regions */
- for (RegionListProperty::ChangeContainer::iterator i = change.added.begin(); i != change.added.end(); ++i) {
- add_region ((*i), (*i)->position());
- }
- /* remove the removed regions */
- for (RegionListProperty::ChangeContainer::iterator i = change.removed.begin(); i != change.removed.end(); ++i) {
- remove_region (*i);
- }
-
- thaw ();
-}
-
-int
-Playlist::set_state (const XMLNode& node, int version)
-{
- XMLNode *child;
- XMLNodeList nlist;
- XMLNodeConstIterator niter;
- XMLPropertyList plist;
- XMLPropertyConstIterator piter;
- XMLProperty *prop;
- boost::shared_ptr<Region> region;
- string region_name;
-
- in_set_state++;
-
- if (node.name() != "Playlist") {
- in_set_state--;
- return -1;
- }
-
- freeze ();
-
- plist = node.properties();
-
- for (piter = plist.begin(); piter != plist.end(); ++piter) {