#include <glibmm/thread.h>
#include <pbd/xml++.h>
+#include <pbd/stacktrace.h>
#include <ardour/region.h>
#include <ardour/playlist.h>
#include <ardour/session.h>
+#include <ardour/region_factory.h>
#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<void,Region *> 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);
_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<const Region> 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;
}
_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<const Region> 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;
_position = 0;
_layer = 0;
_flags = Flag (0);
- _current_state_id = 0;
_first_edit = EditChangesNothing;
if (set_state (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<Playlist> 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;
}
void
-Region::set_length (jack_nframes_t len, void *src)
+Region::set_length (nframes_t len, void *src)
{
if (_flags & Locked) {
return;
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;
}
if (!_frozen) {
recompute_at_end ();
-
- char buf[64];
- snprintf (buf, sizeof (buf), "length set to %u", len);
- save_state (buf);
}
send_change (LengthChanged);
void
Region::first_edit ()
{
- if (_first_edit != EditChangesNothing && _playlist) {
+ boost::shared_ptr<Playlist> 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<Playlist> pl (playlist());
+
+ if (!pl) {
+ return false;
}
+
+ boost::shared_ptr<Region> 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<Playlist> pl (playlist());
+
+ if (!pl) {
return;
}
-
- Region* whole_file_region = get_parent();
+
+ boost::shared_ptr<Region> whole_file_region = get_parent();
if (whole_file_region) {
set_position (whole_file_region->position() + _start, 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.
}
void
-Region::set_position (jack_nframes_t pos, void *src)
+Region::set_position (nframes_t pos, void *src)
{
if (_flags & Locked) {
return;
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;
}
}
}
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;
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<Playlist> 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.
_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;
_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) {
} 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;
_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;
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 */
}
void
-Region::trim_end (jack_nframes_t new_endpoint, void *src)
+Region::trim_end (nframes_t new_endpoint, void *src)
{
if (_flags & Locked) {
return;
}
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;
}
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;
} 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;
}
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);
}
}
_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);
}
}
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);
}
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);
_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);
}
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 */
}
}
-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) {
return pos;
}
-jack_nframes_t
+nframes_t
Region::sync_position() const
{
if (_flags & SyncMarked) {
void
Region::raise ()
{
- if (_playlist == 0) {
- return;
+ boost::shared_ptr<Playlist> pl (playlist());
+ if (pl) {
+ pl->raise_region (shared_from_this ());
}
-
- _playlist->raise_region (*this);
}
void
Region::lower ()
{
- if (_playlist == 0) {
- return;
+ boost::shared_ptr<Playlist> 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<Playlist> 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<Playlist> pl (playlist());
+ if (pl) {
+ pl->lower_region_to_bottom (shared_from_this());
}
-
- _playlist->lower_region_to_bottom (*this);
}
void
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);
}
}
{
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);
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 */
}
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;
}
}
+ 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;
}
recompute_at_end ();
}
- save_state (why);
StateChanged (what_changed);
}
}
}
- StateManager::send_state_changed (what_changed);
+ StateChanged (what_changed);
}
void
{
_last_layer_op = when;
}
+
+bool
+Region::overlap_equivalent (boost::shared_ptr<const Region> other) const
+{
+ return coverage (other->first_frame(), other->last_frame()) != OverlapNone;
+}
+
+bool
+Region::equivalent (boost::shared_ptr<const Region> other) const
+{
+ return _start == other->_start &&
+ _position == other->_position &&
+ _length == other->_length;
+}
+
+bool
+Region::size_equivalent (boost::shared_ptr<const Region> other) const
+{
+ return _start == other->_start &&
+ _length == other->_length;
+}
+
+bool
+Region::region_list_equivalent (boost::shared_ptr<const Region> other) const
+{
+ return size_equivalent (other) && source_equivalent (other) && _name == other->_name;
+}