X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fregion.cc;h=9d81cc5907830a24c5b76549f8bd49a8e5808e57;hb=f21094937a2ae7dae7929de3ed883d93ed12b66b;hp=08df23146b0bc607c0307698683def1fab00d92c;hpb=9fdc36bac5fa1c004751c0dd42f9ff62b9d60814;p=ardour.git diff --git a/libs/ardour/region.cc b/libs/ardour/region.cc index 08df23146b..9d81cc5907 100644 --- a/libs/ardour/region.cc +++ b/libs/ardour/region.cc @@ -28,33 +28,32 @@ #include #include +#include #include #include #include +#include #include "i18n.h" using namespace std; using namespace ARDOUR; +using namespace PBD; -Change Region::FadeChanged = ARDOUR::new_change (); +Change Region::FadeChanged = ARDOUR::new_change (); Change Region::SyncOffsetChanged = ARDOUR::new_change (); -Change Region::MuteChanged = ARDOUR::new_change (); -Change Region::OpacityChanged = ARDOUR::new_change (); -Change Region::LockChanged = ARDOUR::new_change (); -Change Region::LayerChanged = ARDOUR::new_change (); -Change Region::HiddenChanged = ARDOUR::new_change (); +Change Region::MuteChanged = ARDOUR::new_change (); +Change Region::OpacityChanged = ARDOUR::new_change (); +Change Region::LockChanged = ARDOUR::new_change (); +Change Region::LayerChanged = ARDOUR::new_change (); +Change Region::HiddenChanged = ARDOUR::new_change (); -sigc::signal Region::CheckNewRegion; - -Region::Region (jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Region::Flag flags) +Region::Region (nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags) { /* basic Region constructor */ - _id = ARDOUR::new_id(); _flags = flags; - _playlist = 0; _read_data_count = 0; _frozen = 0; pending_changed = Change (0); @@ -65,25 +64,22 @@ Region::Region (jack_nframes_t start, jack_nframes_t length, const string& name, _length = length; _position = 0; _layer = layer; - _current_state_id = 0; _read_data_count = 0; _first_edit = EditChangesNothing; _last_layer_op = 0; } -Region::Region (const Region& other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags) +Region::Region (boost::shared_ptr other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags) { /* create a new Region from part of an existing one */ - _id = ARDOUR::new_id(); _frozen = 0; pending_changed = Change (0); - _playlist = 0; _read_data_count = 0; - _start = other._start + offset; - if (other._sync_position < offset) { - _sync_position = other._sync_position; + _start = other->_start + offset; + if (other->_sync_position < offset) { + _sync_position = other->_sync_position; } else { _sync_position = _start; } @@ -92,47 +88,41 @@ Region::Region (const Region& other, jack_nframes_t offset, jack_nframes_t lengt _position = 0; _layer = layer; _flags = Flag (flags & ~(Locked|WholeFile|Hidden)); - _current_state_id = 0; _first_edit = EditChangesNothing; _last_layer_op = 0; } -Region::Region (const Region &other) +Region::Region (boost::shared_ptr other) { /* Pure copy constructor */ - _id = ARDOUR::new_id(); _frozen = 0; pending_changed = Change (0); - _playlist = 0; _read_data_count = 0; _first_edit = EditChangesID; - other._first_edit = EditChangesName; + other->_first_edit = EditChangesName; - if (other._extra_xml) { - _extra_xml = new XMLNode (*other._extra_xml); + if (other->_extra_xml) { + _extra_xml = new XMLNode (*other->_extra_xml); } else { _extra_xml = 0; } - _start = other._start; - _sync_position = other._sync_position; - _length = other._length; - _name = other._name; - _position = other._position; - _layer = other._layer; - _flags = Flag (other._flags & ~Locked); - _current_state_id = 0; - _last_layer_op = other._last_layer_op; + _start = other->_start; + _sync_position = other->_sync_position; + _length = other->_length; + _name = other->_name; + _position = other->_position; + _layer = other->_layer; + _flags = Flag (other->_flags & ~Locked); + _last_layer_op = other->_last_layer_op; } Region::Region (const XMLNode& node) { - _id = 0; _frozen = 0; pending_changed = Change (0); - _playlist = 0; _read_data_count = 0; _start = 0; _sync_position = _start; @@ -141,7 +131,6 @@ Region::Region (const XMLNode& node) _position = 0; _layer = 0; _flags = Flag (0); - _current_state_id = 0; _first_edit = EditChangesNothing; if (set_state (node)) { @@ -151,78 +140,17 @@ Region::Region (const XMLNode& node) Region::~Region () { + /* derived classes must call notify_callbacks() and then emit GoingAway */ } void -Region::set_playlist (Playlist* pl) +Region::set_playlist (boost::weak_ptr pl) { _playlist = pl; } -void -Region::store_state (RegionState& state) const -{ - state._start = _start; - state._length = _length; - state._position = _position; - state._flags = _flags; - state._sync_position = _sync_position; - state._layer = _layer; - state._name = _name; - state._first_edit = _first_edit; -} - -Change -Region::restore_and_return_flags (RegionState& state) -{ - Change what_changed = Change (0); - - { - Glib::Mutex::Lock lm (lock); - - if (_start != state._start) { - what_changed = Change (what_changed|StartChanged); - _start = state._start; - } - if (_length != state._length) { - what_changed = Change (what_changed|LengthChanged); - _length = state._length; - } - if (_position != state._position) { - what_changed = Change (what_changed|PositionChanged); - _position = state._position; - } - if (_sync_position != state._sync_position) { - _sync_position = state._sync_position; - what_changed = Change (what_changed|SyncOffsetChanged); - } - if (_layer != state._layer) { - what_changed = Change (what_changed|LayerChanged); - _layer = state._layer; - } - - uint32_t old_flags = _flags; - _flags = Flag (state._flags); - - if ((old_flags ^ state._flags) & Muted) { - what_changed = Change (what_changed|MuteChanged); - } - if ((old_flags ^ state._flags) & Opaque) { - what_changed = Change (what_changed|OpacityChanged); - } - if ((old_flags ^ state._flags) & Locked) { - what_changed = Change (what_changed|LockChanged); - } - - _first_edit = state._first_edit; - } - - return what_changed; -} - void Region::set_name (string str) - { if (_name != str) { _name = str; @@ -231,7 +159,7 @@ Region::set_name (string str) } void -Region::set_length (jack_nframes_t len, void *src) +Region::set_length (nframes_t len, void *src) { if (_flags & Locked) { return; @@ -239,6 +167,14 @@ Region::set_length (jack_nframes_t len, void *src) if (_length != len && len != 0) { + /* check that the current _position wouldn't make the new + length impossible. + */ + + if (max_frames - len < _position) { + return; + } + if (!verify_length (len)) { return; } @@ -252,10 +188,6 @@ Region::set_length (jack_nframes_t len, void *src) if (!_frozen) { recompute_at_end (); - - char buf[64]; - snprintf (buf, sizeof (buf), "length set to %u", len); - save_state (buf); } send_change (LengthChanged); @@ -270,24 +202,48 @@ Region::maybe_uncopy () void Region::first_edit () { - if (_first_edit != EditChangesNothing && _playlist) { + boost::shared_ptr pl (playlist()); + + if (_first_edit != EditChangesNothing && pl) { - _name = _playlist->session().new_region_name (_name); + _name = pl->session().new_region_name (_name); _first_edit = EditChangesNothing; send_change (NameChanged); - CheckNewRegion (this); + RegionFactory::CheckNewRegion (shared_from_this()); + } +} + +bool +Region::at_natural_position () const +{ + boost::shared_ptr pl (playlist()); + + if (!pl) { + return false; } + + boost::shared_ptr whole_file_region = get_parent(); + + if (whole_file_region) { + if (_position == whole_file_region->position() + _start) { + return true; + } + } + + return false; } void Region::move_to_natural_position (void *src) { - if (!_playlist) { + boost::shared_ptr pl (playlist()); + + if (!pl) { return; } - - Region* whole_file_region = get_parent(); + + boost::shared_ptr whole_file_region = get_parent(); if (whole_file_region) { set_position (whole_file_region->position() + _start, src); @@ -295,7 +251,7 @@ Region::move_to_natural_position (void *src) } void -Region::special_set_position (jack_nframes_t pos) +Region::special_set_position (nframes_t pos) { /* this is used when creating a whole file region as a way to store its "natural" or "captured" position. @@ -305,7 +261,7 @@ Region::special_set_position (jack_nframes_t pos) } void -Region::set_position (jack_nframes_t pos, void *src) +Region::set_position (nframes_t pos, void *src) { if (_flags & Locked) { return; @@ -314,10 +270,14 @@ Region::set_position (jack_nframes_t pos, void *src) if (_position != pos) { _position = pos; - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "position set to %u", pos); - save_state (buf); + /* check that the new _position wouldn't make the current + length impossible - if so, change the length. + + XXX is this the right thing to do? + */ + + if (max_frames - _length < _position) { + _length = max_frames - _position; } } @@ -329,7 +289,7 @@ Region::set_position (jack_nframes_t pos, void *src) } void -Region::set_position_on_top (jack_nframes_t pos, void *src) +Region::set_position_on_top (nframes_t pos, void *src) { if (_flags & Locked) { return; @@ -337,15 +297,13 @@ Region::set_position_on_top (jack_nframes_t pos, void *src) if (_position != pos) { _position = pos; - - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "position set to %u", pos); - save_state (buf); - } } - _playlist->raise_region_to_top (*this); + boost::shared_ptr pl (playlist()); + + if (pl) { + pl->raise_region_to_top (shared_from_this ()); + } /* do this even if the position is the same. this helps out a GUI that has moved its representation already. @@ -372,24 +330,18 @@ Region::nudge_position (long n, void *src) _position += n; } } else { - if (_position < (jack_nframes_t) -n) { + if (_position < (nframes_t) -n) { _position = 0; } else { _position += n; } } - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "position set to %u", _position); - save_state (buf); - } - send_change (PositionChanged); } void -Region::set_start (jack_nframes_t pos, void *src) +Region::set_start (nframes_t pos, void *src) { if (_flags & Locked) { return; @@ -409,23 +361,17 @@ Region::set_start (jack_nframes_t pos, void *src) _flags = Region::Flag (_flags & ~WholeFile); first_edit (); - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "start set to %u", pos); - save_state (buf); - } - send_change (StartChanged); } } void -Region::trim_start (jack_nframes_t new_position, void *src) +Region::trim_start (nframes_t new_position, void *src) { if (_flags & Locked) { return; } - jack_nframes_t new_start; + nframes_t new_start; int32_t start_shift; if (new_position > _position) { @@ -448,7 +394,7 @@ Region::trim_start (jack_nframes_t new_position, void *src) } else if (start_shift < 0) { - if (_start < (jack_nframes_t) -start_shift) { + if (_start < (nframes_t) -start_shift) { new_start = 0; } else { new_start = _start + start_shift; @@ -465,24 +411,18 @@ Region::trim_start (jack_nframes_t new_position, void *src) _flags = Region::Flag (_flags & ~WholeFile); first_edit (); - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "slipped start to %u", _start); - save_state (buf); - } - send_change (StartChanged); } void -Region::trim_front (jack_nframes_t new_position, void *src) +Region::trim_front (nframes_t new_position, void *src) { if (_flags & Locked) { return; } - jack_nframes_t end = _position + _length - 1; - jack_nframes_t source_zero; + nframes_t end = last_frame(); + nframes_t source_zero; if (_position > _start) { source_zero = _position - _start; @@ -492,7 +432,7 @@ Region::trim_front (jack_nframes_t new_position, void *src) if (new_position < end) { /* can't trim it zero or negative length */ - jack_nframes_t newlen; + nframes_t newlen; /* can't trim it back passed where source position zero is located */ @@ -513,7 +453,7 @@ Region::trim_front (jack_nframes_t new_position, void *src) } void -Region::trim_end (jack_nframes_t new_endpoint, void *src) +Region::trim_end (nframes_t new_endpoint, void *src) { if (_flags & Locked) { return; @@ -528,7 +468,7 @@ Region::trim_end (jack_nframes_t new_endpoint, void *src) } void -Region::trim_to (jack_nframes_t position, jack_nframes_t length, void *src) +Region::trim_to (nframes_t position, nframes_t length, void *src) { if (_flags & Locked) { return; @@ -543,10 +483,10 @@ Region::trim_to (jack_nframes_t position, jack_nframes_t length, void *src) } void -Region::trim_to_internal (jack_nframes_t position, jack_nframes_t length, void *src) +Region::trim_to_internal (nframes_t position, nframes_t length, void *src) { int32_t start_shift; - jack_nframes_t new_start; + nframes_t new_start; if (_flags & Locked) { return; @@ -569,7 +509,7 @@ Region::trim_to_internal (jack_nframes_t position, jack_nframes_t length, void * } else if (start_shift < 0) { - if (_start < (jack_nframes_t) -start_shift) { + if (_start < (nframes_t) -start_shift) { new_start = 0; } else { new_start = _start + start_shift; @@ -604,13 +544,6 @@ Region::trim_to_internal (jack_nframes_t position, jack_nframes_t length, void * } if (what_changed) { - - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "trimmed to %u-%u", _position, _position+_length-1); - save_state (buf); - } - send_change (what_changed); } } @@ -641,16 +574,6 @@ Region::set_muted (bool yn) _flags = Flag (_flags & ~Muted); } - if (!_frozen) { - char buf[64]; - if (yn) { - snprintf (buf, sizeof (buf), "muted"); - } else { - snprintf (buf, sizeof (buf), "unmuted"); - } - save_state (buf); - } - send_change (MuteChanged); } } @@ -659,16 +582,10 @@ void Region::set_opaque (bool yn) { if (opaque() != yn) { - if (!_frozen) { - char buf[64]; - if (yn) { - snprintf (buf, sizeof (buf), "opaque"); - _flags = Flag (_flags|Opaque); - } else { - snprintf (buf, sizeof (buf), "translucent"); - _flags = Flag (_flags & ~Opaque); - } - save_state (buf); + if (yn) { + _flags = Flag (_flags|Opaque); + } else { + _flags = Flag (_flags & ~Opaque); } send_change (OpacityChanged); } @@ -678,25 +595,19 @@ void Region::set_locked (bool yn) { if (locked() != yn) { - if (!_frozen) { - char buf[64]; - if (yn) { - snprintf (buf, sizeof (buf), "locked"); - _flags = Flag (_flags|Locked); - } else { - snprintf (buf, sizeof (buf), "unlocked"); - _flags = Flag (_flags & ~Locked); - } - save_state (buf); + if (yn) { + _flags = Flag (_flags|Locked); + } else { + _flags = Flag (_flags & ~Locked); } send_change (LockChanged); } } void -Region::set_sync_position (jack_nframes_t absolute_pos) +Region::set_sync_position (nframes_t absolute_pos) { - jack_nframes_t file_pos; + nframes_t file_pos; file_pos = _start + (absolute_pos - _position); @@ -706,10 +617,7 @@ Region::set_sync_position (jack_nframes_t absolute_pos) _flags = Flag (_flags|SyncMarked); if (!_frozen) { - char buf[64]; maybe_uncopy (); - snprintf (buf, sizeof (buf), "sync point set to %u", _sync_position); - save_state (buf); } send_change (SyncOffsetChanged); } @@ -723,13 +631,12 @@ Region::clear_sync_position () if (!_frozen) { maybe_uncopy (); - save_state ("sync point removed"); } send_change (SyncOffsetChanged); } } -jack_nframes_t +nframes_t Region::sync_offset (int& dir) const { /* returns the sync point relative the first frame of the region */ @@ -748,11 +655,11 @@ Region::sync_offset (int& dir) const } } -jack_nframes_t -Region::adjust_to_sync (jack_nframes_t pos) +nframes_t +Region::adjust_to_sync (nframes_t pos) { int sync_dir; - jack_nframes_t offset = sync_offset (sync_dir); + nframes_t offset = sync_offset (sync_dir); if (sync_dir > 0) { if (max_frames - pos > offset) { @@ -769,7 +676,7 @@ Region::adjust_to_sync (jack_nframes_t pos) return pos; } -jack_nframes_t +nframes_t Region::sync_position() const { if (_flags & SyncMarked) { @@ -783,42 +690,37 @@ Region::sync_position() const void Region::raise () { - if (_playlist == 0) { - return; + boost::shared_ptr pl (playlist()); + if (pl) { + pl->raise_region (shared_from_this ()); } - - _playlist->raise_region (*this); } void Region::lower () { - if (_playlist == 0) { - return; + boost::shared_ptr pl (playlist()); + if (pl) { + pl->lower_region (shared_from_this ()); } - - _playlist->lower_region (*this); } void Region::raise_to_top () { - - if (_playlist == 0) { - return; + boost::shared_ptr pl (playlist()); + if (pl) { + pl->raise_region_to_top (shared_from_this()); } - - _playlist->raise_region_to_top (*this); } void Region::lower_to_bottom () { - if (_playlist == 0) { - return; + boost::shared_ptr pl (playlist()); + if (pl) { + pl->lower_region_to_bottom (shared_from_this()); } - - _playlist->lower_region_to_bottom (*this); } void @@ -827,12 +729,6 @@ Region::set_layer (layer_t l) if (_layer != l) { _layer = l; - if (!_frozen) { - char buf[64]; - snprintf (buf, sizeof (buf), "layer set to %" PRIu32, _layer); - save_state (buf); - } - send_change (LayerChanged); } } @@ -842,8 +738,9 @@ Region::state (bool full_state) { XMLNode *node = new XMLNode ("Region"); char buf[64]; - - snprintf (buf, sizeof (buf), "%" PRIu64, _id); + char* fe; + + _id.print (buf, sizeof (buf)); node->add_property ("id", buf); node->add_property ("name", _name); snprintf (buf, sizeof (buf), "%u", _start); @@ -852,6 +749,20 @@ Region::state (bool full_state) node->add_property ("length", buf); snprintf (buf, sizeof (buf), "%u", _position); node->add_property ("position", buf); + + switch (_first_edit) { + case EditChangesNothing: + fe = X_("nothing"); + break; + case EditChangesName: + fe = X_("name"); + break; + case EditChangesID: + fe = X_("id"); + break; + } + + node->add_property ("first_edit", fe); /* note: flags are stored by derived classes */ @@ -870,54 +781,83 @@ Region::get_state () } int -Region::set_state (const XMLNode& node) +Region::set_live_state (const XMLNode& node, Change& what_changed, bool send) { const XMLNodeList& nlist = node.children(); const XMLProperty *prop; + nframes_t val; - if (_extra_xml) { - delete _extra_xml; - _extra_xml = 0; - } - - if ((prop = node.property ("id")) == 0) { - error << _("Session: XMLNode describing a Region is incomplete (no id)") << endmsg; - return -1; - } - - sscanf (prop->value().c_str(), "%" PRIu64, &_id); + /* this is responsible for setting those aspects of Region state + that are mutable after construction. + */ if ((prop = node.property ("name")) == 0) { - error << _("Session: XMLNode describing a Region is incomplete (no name)") << endmsg; + error << _("XMLNode describing a Region is incomplete (no name)") << endmsg; return -1; } _name = prop->value(); if ((prop = node.property ("start")) != 0) { - _start = (jack_nframes_t) atoi (prop->value().c_str()); + sscanf (prop->value().c_str(), "%" PRIu32, &val); + if (val != _start) { + what_changed = Change (what_changed|StartChanged); + _start = val; + } + } else { + _start = 0; } if ((prop = node.property ("length")) != 0) { - _length = (jack_nframes_t) atoi (prop->value().c_str()); + sscanf (prop->value().c_str(), "%" PRIu32, &val); + if (val != _length) { + what_changed = Change (what_changed|LengthChanged); + _length = val; + } + } else { + _length = 1; } if ((prop = node.property ("position")) != 0) { - _position = (jack_nframes_t) atoi (prop->value().c_str()); + sscanf (prop->value().c_str(), "%" PRIu32, &val); + if (val != _position) { + what_changed = Change (what_changed|PositionChanged); + _position = val; + } + } else { + _position = 0; } if ((prop = node.property ("layer")) != 0) { - _layer = (layer_t) atoi (prop->value().c_str()); + layer_t x; + x = (layer_t) atoi (prop->value().c_str()); + if (x != _layer) { + what_changed = Change (what_changed|LayerChanged); + _layer = x; + } + } else { + _layer = 0; } - /* note: derived classes set flags */ - if ((prop = node.property ("sync-position")) != 0) { - _sync_position = (jack_nframes_t) atoi (prop->value().c_str()); + sscanf (prop->value().c_str(), "%" PRIu32, &val); + if (val != _sync_position) { + what_changed = Change (what_changed|SyncOffsetChanged); + _sync_position = val; + } } else { _sync_position = _start; } + + /* XXX FIRST EDIT !!! */ + /* note: derived classes set flags */ + + if (_extra_xml) { + delete _extra_xml; + _extra_xml = 0; + } + for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) { XMLNode *child; @@ -930,7 +870,31 @@ Region::set_state (const XMLNode& node) } } + if (send) { + send_change (what_changed); + } + + return 0; +} + +int +Region::set_state (const XMLNode& node) +{ + const XMLProperty *prop; + Change what_changed = Change (0); + + /* ID is not allowed to change, ever */ + + if ((prop = node.property ("id")) == 0) { + error << _("Session: XMLNode describing a Region is incomplete (no id)") << endmsg; + return -1; + } + + _id = prop->value(); + _first_edit = EditChangesNothing; + + set_live_state (node, what_changed, true); return 0; } @@ -970,7 +934,6 @@ Region::thaw (const string& why) recompute_at_end (); } - save_state (why); StateChanged (what_changed); } @@ -985,7 +948,7 @@ Region::send_change (Change what_changed) } } - StateManager::send_state_changed (what_changed); + StateChanged (what_changed); } void @@ -993,3 +956,30 @@ Region::set_last_layer_op (uint64_t when) { _last_layer_op = when; } + +bool +Region::overlap_equivalent (boost::shared_ptr other) const +{ + return coverage (other->first_frame(), other->last_frame()) != OverlapNone; +} + +bool +Region::equivalent (boost::shared_ptr other) const +{ + return _start == other->_start && + _position == other->_position && + _length == other->_length; +} + +bool +Region::size_equivalent (boost::shared_ptr other) const +{ + return _start == other->_start && + _length == other->_length; +} + +bool +Region::region_list_equivalent (boost::shared_ptr other) const +{ + return size_equivalent (other) && source_equivalent (other) && _name == other->_name; +}