2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <cstdio> /* for sprintf */
28 #include "pbd/stl_delete.h"
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
32 #include "ardour/location.h"
33 #include "ardour/midi_scene_change.h"
34 #include "ardour/session.h"
35 #include "ardour/audiofilesource.h"
36 #include "ardour/tempo.h"
43 using namespace ARDOUR;
46 PBD::Signal0<void> Location::scene_changed;
47 PBD::Signal1<void,Location*> Location::name_changed;
48 PBD::Signal1<void,Location*> Location::end_changed;
49 PBD::Signal1<void,Location*> Location::start_changed;
50 PBD::Signal1<void,Location*> Location::changed;
52 Location::Location (Session& s)
53 : SessionHandleRef (s)
58 , _position_lock_style (AudioTime)
64 /** Construct a new Location, giving it the position lock style determined by glue-new-markers-to-bars-and-beats */
65 Location::Location (Session& s, framepos_t sample_start, framepos_t sample_end, const std::string &name, Flags bits)
66 : SessionHandleRef (s)
68 , _start (sample_start)
72 , _position_lock_style (s.config.get_glue_new_markers_to_bars_and_beats() ? MusicTime : AudioTime)
74 recompute_bbt_from_frames ();
80 Location::Location (const Location& other)
81 : SessionHandleRef (other._session)
82 , StatefulDestructible()
84 , _start (other._start)
85 , _bbt_start (other._bbt_start)
87 , _bbt_end (other._bbt_end)
88 , _flags (other._flags)
89 , _position_lock_style (other._position_lock_style)
91 /* copy is not locked even if original was */
98 /* scene change is NOT COPIED */
101 Location::Location (Session& s, const XMLNode& node)
102 : SessionHandleRef (s)
103 , _position_lock_style (AudioTime)
105 /* Note: _position_lock_style is initialised above in case set_state doesn't set it
106 (for 2.X session file compatibility).
109 if (set_state (node, Stateful::loading_state_version)) {
110 throw failed_constructor ();
113 assert (_start >= 0);
118 Location::operator== (const Location& other)
120 if (_name != other._name ||
121 _start != other._start ||
122 _end != other._end ||
123 _bbt_start != other._bbt_start ||
124 _bbt_end != other._bbt_end ||
125 _flags != other._flags ||
126 _position_lock_style != other._position_lock_style) {
133 Location::operator= (const Location& other)
135 if (this == &other) {
140 _start = other._start;
141 _bbt_start = other._bbt_start;
143 _bbt_end = other._bbt_end;
144 _flags = other._flags;
145 _position_lock_style = other._position_lock_style;
147 /* XXX need to copy scene change */
149 /* copy is not locked even if original was */
153 /* "changed" not emitted on purpose */
155 assert (_start >= 0);
161 /** Set start position.
162 * @param s New start.
163 * @param force true to force setting, even if the given new start is after the current end.
164 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
167 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
178 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
187 if (allow_bbt_recompute) {
188 recompute_bbt_from_frames ();
191 start_changed (this); /* EMIT SIGNAL */
192 end_changed (this); /* EMIT SIGNAL */
195 /* moving the start (position) of a marker with a scene change
196 requires an update in the Scene Changer.
200 scene_changed (); /* EMIT SIGNAL */
203 assert (_start >= 0);
211 framepos_t const old = _start;
214 if (allow_bbt_recompute) {
215 recompute_bbt_from_frames ();
217 start_changed (this); /* EMIT SIGNAL */
219 if (is_session_range ()) {
220 Session::StartTimeChanged (old); /* EMIT SIGNAL */
221 AudioFileSource::set_header_position_offset (s);
225 assert (_start >= 0);
230 /** Set end position.
232 * @param force true to force setting, even if the given new start is after the current end.
233 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
236 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
247 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
256 if (allow_bbt_recompute) {
257 recompute_bbt_from_frames ();
259 start_changed (this); /* EMIT SIGNAL */
260 end_changed (this); /* EMIT SIGNAL */
263 assert (_start >= 0);
271 framepos_t const old = _end;
274 if (allow_bbt_recompute) {
275 recompute_bbt_from_frames ();
277 end_changed(this); /* EMIT SIGNAL */
279 if (is_session_range()) {
280 Session::EndTimeChanged (old); /* EMIT SIGNAL */
290 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
292 if (start < 0 || end < 0) {
297 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
301 /* now we know these values are ok, so force-set them */
302 int const s = set_start (start, true, allow_bbt_recompute);
303 int const e = set_end (end, true, allow_bbt_recompute);
305 return (s == 0 && e == 0) ? 0 : -1;
309 Location::move_to (framepos_t pos)
321 _end = _start + length();
322 recompute_bbt_from_frames ();
324 changed (this); /* EMIT SIGNAL */
327 assert (_start >= 0);
334 Location::set_hidden (bool yn, void *src)
336 if (set_flag_internal (yn, IsHidden)) {
337 FlagsChanged (this, src); /* EMIT SIGNAL */
342 Location::set_cd (bool yn, void *src)
344 // XXX this really needs to be session start
345 // but its not available here - leave to GUI
347 if (yn && _start == 0) {
348 error << _("You cannot put a CD marker at this position") << endmsg;
352 if (set_flag_internal (yn, IsCDMarker)) {
353 FlagsChanged (this, src); /* EMIT SIGNAL */
358 Location::set_is_range_marker (bool yn, void *src)
360 if (set_flag_internal (yn, IsRangeMarker)) {
361 FlagsChanged (this, src); /* EMIT SIGNAL */
366 Location::set_auto_punch (bool yn, void *src)
368 if (is_mark() || _start == _end) {
372 if (set_flag_internal (yn, IsAutoPunch)) {
373 FlagsChanged (this, src); /* EMIT SIGNAL */
378 Location::set_auto_loop (bool yn, void *src)
380 if (is_mark() || _start == _end) {
384 if (set_flag_internal (yn, IsAutoLoop)) {
385 FlagsChanged (this, src); /* EMIT SIGNAL */
390 Location::set_flag_internal (bool yn, Flags flag)
393 if (!(_flags & flag)) {
394 _flags = Flags (_flags | flag);
399 _flags = Flags (_flags & ~flag);
407 Location::set_mark (bool yn)
409 /* This function is private, and so does not emit signals */
411 if (_start != _end) {
415 set_flag_internal (yn, IsMark);
420 Location::cd_info_node(const string & name, const string & value)
422 XMLNode* root = new XMLNode("CD-Info");
424 root->add_property("name", name);
425 root->add_property("value", value);
432 Location::get_state ()
434 XMLNode *node = new XMLNode ("Location");
437 typedef map<string, string>::const_iterator CI;
439 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
440 node->add_child_nocopy(cd_info_node(m->first, m->second));
443 id().print (buf, sizeof (buf));
444 node->add_property("id", buf);
445 node->add_property ("name", name());
446 snprintf (buf, sizeof (buf), "%" PRId64, start());
447 node->add_property ("start", buf);
448 snprintf (buf, sizeof (buf), "%" PRId64, end());
449 node->add_property ("end", buf);
450 node->add_property ("flags", enum_2_string (_flags));
451 node->add_property ("locked", (_locked ? "yes" : "no"));
452 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
455 node->add_child_nocopy (_scene_change->get_state());
462 Location::set_state (const XMLNode& node, int version)
464 const XMLProperty *prop;
466 XMLNodeList cd_list = node.children();
467 XMLNodeConstIterator cd_iter;
473 if (node.name() != "Location") {
474 error << _("incorrect XML node passed to Location::set_state") << endmsg;
478 if (!set_id (node)) {
479 warning << _("XML node for Location has no ID information") << endmsg;
482 if ((prop = node.property ("name")) == 0) {
483 error << _("XML node for Location has no name information") << endmsg;
487 set_name (prop->value());
489 if ((prop = node.property ("start")) == 0) {
490 error << _("XML node for Location has no start information") << endmsg;
494 /* can't use set_start() here, because _end
495 may make the value of _start illegal.
498 sscanf (prop->value().c_str(), "%" PRId64, &_start);
500 if ((prop = node.property ("end")) == 0) {
501 error << _("XML node for Location has no end information") << endmsg;
505 sscanf (prop->value().c_str(), "%" PRId64, &_end);
507 if ((prop = node.property ("flags")) == 0) {
508 error << _("XML node for Location has no flags information") << endmsg;
512 _flags = Flags (string_2_enum (prop->value(), _flags));
514 if ((prop = node.property ("locked")) != 0) {
515 _locked = string_is_affirmative (prop->value());
520 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
524 if (cd_node->name() != "CD-Info") {
528 if ((prop = cd_node->property ("name")) != 0) {
529 cd_name = prop->value();
531 throw failed_constructor ();
534 if ((prop = cd_node->property ("value")) != 0) {
535 cd_value = prop->value();
537 throw failed_constructor ();
541 cd_info[cd_name] = cd_value;
544 if ((prop = node.property ("position-lock-style")) != 0) {
545 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
548 XMLNode* scene_child = find_named_node (node, SceneChange::xml_node_name);
551 _scene_change = SceneChange::factory (*scene_child, version);
554 recompute_bbt_from_frames ();
556 changed (this); /* EMIT SIGNAL */
558 assert (_start >= 0);
565 Location::set_position_lock_style (PositionLockStyle ps)
567 if (_position_lock_style == ps) {
571 _position_lock_style = ps;
573 recompute_bbt_from_frames ();
575 PositionLockStyleChanged (this); /* EMIT SIGNAL */
579 Location::recompute_bbt_from_frames ()
581 if (_position_lock_style != MusicTime) {
585 _session.bbt_time (_start, _bbt_start);
586 _session.bbt_time (_end, _bbt_end);
590 Location::recompute_frames_from_bbt ()
592 if (_position_lock_style != MusicTime) {
596 TempoMap& map (_session.tempo_map());
597 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
615 Location::set_scene_change (boost::shared_ptr<SceneChange> sc)
619 scene_changed (); /* EMIT SIGNAL */
622 /*---------------------------------------------------------------------- */
624 Locations::Locations (Session& s)
625 : SessionHandleRef (s)
627 current_location = 0;
629 Location::changed.connect_same_thread (*this, boost::bind (&Locations::location_changed, this, _1));
630 Location::start_changed.connect_same_thread (*this, boost::bind (&Locations::location_changed, this, _1));
631 Location::end_changed.connect_same_thread (*this, boost::bind (&Locations::location_changed, this, _1));
634 Locations::~Locations ()
636 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
637 LocationList::iterator tmp = i;
645 Locations::set_current (Location *loc, bool want_lock)
650 Glib::Threads::Mutex::Lock lm (lock);
651 ret = set_current_unlocked (loc);
653 ret = set_current_unlocked (loc);
657 current_changed (current_location); /* EMIT SIGNAL */
663 Locations::next_available_name(string& result,string base)
665 LocationList::iterator i;
671 bool available[SUFFIX_MAX+1];
674 for (int k=1; k<SUFFIX_MAX; k++) {
678 for (i = locations.begin(); i != locations.end(); ++i) {
680 temp = location->name();
681 if (l && !temp.find(base,0)) {
682 suffix = atoi(temp.substr(l,3).c_str());
683 if (suffix) available[suffix] = false;
686 for (int k=1; k<=SUFFIX_MAX; k++) {
688 snprintf (buf, 31, "%d", k);
697 Locations::set_current_unlocked (Location *loc)
699 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
700 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
704 current_location = loc;
712 Glib::Threads::Mutex::Lock lm (lock);
714 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
716 LocationList::iterator tmp = i;
719 if (!(*i)->is_session_range()) {
727 current_location = 0;
730 changed (OTHER); /* EMIT SIGNAL */
731 current_changed (0); /* EMIT SIGNAL */
735 Locations::clear_markers ()
738 Glib::Threads::Mutex::Lock lm (lock);
739 LocationList::iterator tmp;
741 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
745 if ((*i)->is_mark() && !(*i)->is_session_range()) {
754 changed (OTHER); /* EMIT SIGNAL */
758 Locations::clear_ranges ()
761 Glib::Threads::Mutex::Lock lm (lock);
762 LocationList::iterator tmp;
764 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
769 if (!(*i)->is_mark()) {
778 current_location = 0;
781 changed (OTHER); /* EMIT SIGNAL */
782 current_changed (0); /* EMIT SIGNAL */
786 Locations::add (Location *loc, bool make_current)
791 Glib::Threads::Mutex::Lock lm (lock);
792 locations.push_back (loc);
795 current_location = loc;
799 added (loc); /* EMIT SIGNAL */
802 current_changed (current_location); /* EMIT SIGNAL */
805 if (loc->is_session_range()) {
806 Session::StartTimeChanged (0);
807 Session::EndTimeChanged (1);
812 Locations::remove (Location *loc)
814 bool was_removed = false;
815 bool was_current = false;
816 LocationList::iterator i;
818 if (loc->is_session_range()) {
823 Glib::Threads::Mutex::Lock lm (lock);
825 for (i = locations.begin(); i != locations.end(); ++i) {
830 if (current_location == loc) {
831 current_location = 0;
841 removed (loc); /* EMIT SIGNAL */
844 current_changed (0); /* EMIT SIGNAL */
847 changed (REMOVAL); /* EMIT_SIGNAL */
852 Locations::location_changed (Location* /*loc*/)
854 changed (OTHER); /* EMIT SIGNAL */
858 Locations::get_state ()
860 XMLNode *node = new XMLNode ("Locations");
861 LocationList::iterator iter;
862 Glib::Threads::Mutex::Lock lm (lock);
864 for (iter = locations.begin(); iter != locations.end(); ++iter) {
865 node->add_child_nocopy ((*iter)->get_state ());
872 Locations::set_state (const XMLNode& node, int version)
874 if (node.name() != "Locations") {
875 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
879 XMLNodeList nlist = node.children();
881 /* build up a new locations list in here */
882 LocationList new_locations;
884 current_location = 0;
886 Location* session_range_location = 0;
887 if (version < 3000) {
888 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
889 new_locations.push_back (session_range_location);
893 Glib::Threads::Mutex::Lock lm (lock);
895 XMLNodeConstIterator niter;
896 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
900 XMLProperty const * prop_id = (*niter)->property ("id");
902 PBD::ID id (prop_id->value ());
904 LocationList::const_iterator i = locations.begin();
905 while (i != locations.end () && (*i)->id() != id) {
910 if (i != locations.end()) {
911 /* we can re-use an old Location object */
913 loc->set_state (**niter, version);
915 loc = new Location (_session, **niter);
920 if (version < 3000) {
921 /* look for old-style IsStart / IsEnd properties in this location;
922 if they are present, update the session_range_location accordingly
924 XMLProperty const * prop = (*niter)->property ("flags");
926 string v = prop->value ();
928 string::size_type const c = v.find_first_of (',');
929 string const s = v.substr (0, c);
930 if (s == X_("IsStart")) {
931 session_range_location->set_start (loc->start(), true);
933 } else if (s == X_("IsEnd")) {
934 session_range_location->set_end (loc->start(), true);
938 if (c == string::npos) {
942 v = v.substr (c + 1);
948 new_locations.push_back (loc);
952 catch (failed_constructor& err) {
953 error << _("could not load location from session file - ignored") << endmsg;
957 locations = new_locations;
959 if (locations.size()) {
960 current_location = locations.front();
962 current_location = 0;
966 changed (OTHER); /* EMIT SIGNAL */
972 typedef std::pair<framepos_t,Location*> LocationPair;
974 struct LocationStartEarlierComparison
976 bool operator() (LocationPair a, LocationPair b) {
977 return a.first < b.first;
981 struct LocationStartLaterComparison
983 bool operator() (LocationPair a, LocationPair b) {
984 return a.first > b.first;
989 Locations::first_mark_before (framepos_t frame, bool include_special_ranges)
991 Glib::Threads::Mutex::Lock lm (lock);
992 vector<LocationPair> locs;
994 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
995 locs.push_back (make_pair ((*i)->start(), (*i)));
996 if (!(*i)->is_mark()) {
997 locs.push_back (make_pair ((*i)->end(), (*i)));
1001 LocationStartLaterComparison cmp;
1002 sort (locs.begin(), locs.end(), cmp);
1004 /* locs is sorted in ascending order */
1006 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
1007 if ((*i).second->is_hidden()) {
1010 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1013 if ((*i).first < frame) {
1022 Locations::mark_at (framepos_t pos, framecnt_t slop) const
1024 Glib::Threads::Mutex::Lock lm (lock);
1025 Location* closest = 0;
1026 frameoffset_t mindelta = max_framepos;
1027 frameoffset_t delta;
1029 /* locations are not necessarily stored in linear time order so we have
1030 * to iterate across all of them to find the one closest to a give point.
1033 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1035 if ((*i)->is_mark()) {
1036 if (pos > (*i)->start()) {
1037 delta = pos - (*i)->start();
1039 delta = (*i)->start() - pos;
1042 if (slop == 0 && delta == 0) {
1043 /* special case: no slop, and direct hit for position */
1047 if (delta <= slop) {
1048 if (delta < mindelta) {
1060 Locations::first_mark_after (framepos_t frame, bool include_special_ranges)
1062 Glib::Threads::Mutex::Lock lm (lock);
1063 vector<LocationPair> locs;
1065 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
1066 locs.push_back (make_pair ((*i)->start(), (*i)));
1067 if (!(*i)->is_mark()) {
1068 locs.push_back (make_pair ((*i)->end(), (*i)));
1072 LocationStartEarlierComparison cmp;
1073 sort (locs.begin(), locs.end(), cmp);
1075 /* locs is sorted in reverse order */
1077 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
1078 if ((*i).second->is_hidden()) {
1081 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1084 if ((*i).first > frame) {
1092 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
1093 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
1095 * @param frame Frame to look for.
1096 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
1097 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
1100 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
1102 before = after = max_framepos;
1107 Glib::Threads::Mutex::Lock lm (lock);
1111 /* Get a list of positions; don't store any that are exactly on our requested position */
1113 std::list<framepos_t> positions;
1115 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1116 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
1120 if (!(*i)->is_hidden()) {
1121 if ((*i)->is_mark ()) {
1122 if ((*i)->start() != frame) {
1123 positions.push_back ((*i)->start ());
1126 if ((*i)->start() != frame) {
1127 positions.push_back ((*i)->start ());
1129 if ((*i)->end() != frame) {
1130 positions.push_back ((*i)->end ());
1136 if (positions.empty ()) {
1142 std::list<framepos_t>::iterator i = positions.begin ();
1143 while (i != positions.end () && *i < frame) {
1147 if (i == positions.end ()) {
1148 /* run out of marks */
1149 before = positions.back ();
1155 if (i == positions.begin ()) {
1165 Locations::session_range_location () const
1167 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1168 if ((*i)->is_session_range()) {
1169 return const_cast<Location*> (*i);
1176 Locations::auto_loop_location () const
1178 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1179 if ((*i)->is_auto_loop()) {
1180 return const_cast<Location*> (*i);
1187 Locations::auto_punch_location () const
1189 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1190 if ((*i)->is_auto_punch()) {
1191 return const_cast<Location*> (*i);
1198 Locations::num_range_markers () const
1201 Glib::Threads::Mutex::Lock lm (lock);
1202 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1203 if ((*i)->is_range_marker()) {
1211 Locations::get_location_by_id(PBD::ID id)
1213 LocationList::iterator it;
1214 for (it = locations.begin(); it != locations.end(); ++it)
1215 if (id == (*it)->id())
1222 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1224 Glib::Threads::Mutex::Lock lm (lock);
1226 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1227 if ((flags == 0 || (*i)->matches (flags)) &&
1228 ((*i)->start() >= start && (*i)->end() < end)) {