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::flags_changed;
51 PBD::Signal1<void,Location*> Location::lock_changed;
52 PBD::Signal1<void,Location*> Location::position_lock_style_changed;
53 PBD::Signal1<void,Location*> Location::changed;
55 Location::Location (Session& s)
56 : SessionHandleRef (s)
61 , _position_lock_style (AudioTime)
67 /** Construct a new Location, giving it the position lock style determined by glue-new-markers-to-bars-and-beats */
68 Location::Location (Session& s, framepos_t sample_start, framepos_t sample_end, const std::string &name, Flags bits)
69 : SessionHandleRef (s)
71 , _start (sample_start)
75 , _position_lock_style (s.config.get_glue_new_markers_to_bars_and_beats() ? MusicTime : AudioTime)
77 recompute_bbt_from_frames ();
83 Location::Location (const Location& other)
84 : SessionHandleRef (other._session)
85 , StatefulDestructible()
87 , _start (other._start)
88 , _bbt_start (other._bbt_start)
90 , _bbt_end (other._bbt_end)
91 , _flags (other._flags)
92 , _position_lock_style (other._position_lock_style)
94 /* copy is not locked even if original was */
101 /* scene change is NOT COPIED */
104 Location::Location (Session& s, const XMLNode& node)
105 : SessionHandleRef (s)
106 , _position_lock_style (AudioTime)
108 /* Note: _position_lock_style is initialised above in case set_state doesn't set it
109 (for 2.X session file compatibility).
112 if (set_state (node, Stateful::loading_state_version)) {
113 throw failed_constructor ();
116 assert (_start >= 0);
121 Location::operator== (const Location& other)
123 if (_name != other._name ||
124 _start != other._start ||
125 _end != other._end ||
126 _bbt_start != other._bbt_start ||
127 _bbt_end != other._bbt_end ||
128 _flags != other._flags ||
129 _position_lock_style != other._position_lock_style) {
136 Location::operator= (const Location& other)
138 if (this == &other) {
143 _start = other._start;
144 _bbt_start = other._bbt_start;
146 _bbt_end = other._bbt_end;
147 _flags = other._flags;
148 _position_lock_style = other._position_lock_style;
150 /* XXX need to copy scene change */
152 /* copy is not locked even if original was */
156 /* "changed" not emitted on purpose */
158 assert (_start >= 0);
164 /** Set start position.
165 * @param s New start.
166 * @param force true to force setting, even if the given new start is after the current end.
167 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
170 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
181 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
190 if (allow_bbt_recompute) {
191 recompute_bbt_from_frames ();
194 start_changed (this); /* EMIT SIGNAL */
195 StartChanged (); /* EMIT SIGNAL */
196 end_changed (this); /* EMIT SIGNAL */
197 EndChanged (); /* EMIT SIGNAL */
200 /* moving the start (position) of a marker with a scene change
201 requires an update in the Scene Changer.
205 scene_changed (); /* EMIT SIGNAL */
208 assert (_start >= 0);
216 framepos_t const old = _start;
219 if (allow_bbt_recompute) {
220 recompute_bbt_from_frames ();
222 start_changed (this); /* EMIT SIGNAL */
223 StartChanged (); /* EMIT SIGNAL */
225 if (is_session_range ()) {
226 Session::StartTimeChanged (old); /* EMIT SIGNAL */
227 AudioFileSource::set_header_position_offset (s);
231 assert (_start >= 0);
236 /** Set end position.
238 * @param force true to force setting, even if the given new start is after the current end.
239 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
242 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
253 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
262 if (allow_bbt_recompute) {
263 recompute_bbt_from_frames ();
265 start_changed (this); /* EMIT SIGNAL */
266 StartChanged (); /* EMIT SIGNAL */
267 end_changed (this); /* EMIT SIGNAL */
268 EndChanged (); /* EMIT SIGNAL */
271 assert (_start >= 0);
279 framepos_t const old = _end;
282 if (allow_bbt_recompute) {
283 recompute_bbt_from_frames ();
285 end_changed(this); /* EMIT SIGNAL */
286 EndChanged(); /* EMIT SIGNAL */
288 if (is_session_range()) {
289 Session::EndTimeChanged (old); /* EMIT SIGNAL */
299 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
301 if (start < 0 || end < 0) {
306 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
310 /* now we know these values are ok, so force-set them */
311 int const s = set_start (start, true, allow_bbt_recompute);
312 int const e = set_end (end, true, allow_bbt_recompute);
314 return (s == 0 && e == 0) ? 0 : -1;
318 Location::move_to (framepos_t pos)
330 _end = _start + length();
331 recompute_bbt_from_frames ();
333 changed (this); /* EMIT SIGNAL */
334 Changed (); /* EMIT SIGNAL */
337 assert (_start >= 0);
344 Location::set_hidden (bool yn, void*)
346 if (set_flag_internal (yn, IsHidden)) {
347 flags_changed (this); /* EMIT SIGNAL */
353 Location::set_cd (bool yn, void*)
355 // XXX this really needs to be session start
356 // but its not available here - leave to GUI
358 if (yn && _start == 0) {
359 error << _("You cannot put a CD marker at this position") << endmsg;
363 if (set_flag_internal (yn, IsCDMarker)) {
364 flags_changed (this); /* EMIT SIGNAL */
370 Location::set_is_range_marker (bool yn, void*)
372 if (set_flag_internal (yn, IsRangeMarker)) {
373 flags_changed (this);
374 FlagsChanged (); /* EMIT SIGNAL */
379 Location::set_auto_punch (bool yn, void*)
381 if (is_mark() || _start == _end) {
385 if (set_flag_internal (yn, IsAutoPunch)) {
386 flags_changed (this); /* EMIT SIGNAL */
387 FlagsChanged (); /* EMIT SIGNAL */
392 Location::set_auto_loop (bool yn, void*)
394 if (is_mark() || _start == _end) {
398 if (set_flag_internal (yn, IsAutoLoop)) {
399 flags_changed (this); /* EMIT SIGNAL */
400 FlagsChanged (); /* EMIT SIGNAL */
405 Location::set_flag_internal (bool yn, Flags flag)
408 if (!(_flags & flag)) {
409 _flags = Flags (_flags | flag);
414 _flags = Flags (_flags & ~flag);
422 Location::set_mark (bool yn)
424 /* This function is private, and so does not emit signals */
426 if (_start != _end) {
430 set_flag_internal (yn, IsMark);
435 Location::cd_info_node(const string & name, const string & value)
437 XMLNode* root = new XMLNode("CD-Info");
439 root->add_property("name", name);
440 root->add_property("value", value);
447 Location::get_state ()
449 XMLNode *node = new XMLNode ("Location");
452 typedef map<string, string>::const_iterator CI;
454 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
455 node->add_child_nocopy(cd_info_node(m->first, m->second));
458 id().print (buf, sizeof (buf));
459 node->add_property("id", buf);
460 node->add_property ("name", name());
461 snprintf (buf, sizeof (buf), "%" PRId64, start());
462 node->add_property ("start", buf);
463 snprintf (buf, sizeof (buf), "%" PRId64, end());
464 node->add_property ("end", buf);
465 node->add_property ("flags", enum_2_string (_flags));
466 node->add_property ("locked", (_locked ? "yes" : "no"));
467 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
470 node->add_child_nocopy (_scene_change->get_state());
477 Location::set_state (const XMLNode& node, int version)
479 const XMLProperty *prop;
481 XMLNodeList cd_list = node.children();
482 XMLNodeConstIterator cd_iter;
488 if (node.name() != "Location") {
489 error << _("incorrect XML node passed to Location::set_state") << endmsg;
493 if (!set_id (node)) {
494 warning << _("XML node for Location has no ID information") << endmsg;
497 if ((prop = node.property ("name")) == 0) {
498 error << _("XML node for Location has no name information") << endmsg;
502 set_name (prop->value());
504 if ((prop = node.property ("start")) == 0) {
505 error << _("XML node for Location has no start information") << endmsg;
509 /* can't use set_start() here, because _end
510 may make the value of _start illegal.
513 sscanf (prop->value().c_str(), "%" PRId64, &_start);
515 if ((prop = node.property ("end")) == 0) {
516 error << _("XML node for Location has no end information") << endmsg;
520 sscanf (prop->value().c_str(), "%" PRId64, &_end);
522 if ((prop = node.property ("flags")) == 0) {
523 error << _("XML node for Location has no flags information") << endmsg;
527 _flags = Flags (string_2_enum (prop->value(), _flags));
529 if ((prop = node.property ("locked")) != 0) {
530 _locked = string_is_affirmative (prop->value());
535 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
539 if (cd_node->name() != "CD-Info") {
543 if ((prop = cd_node->property ("name")) != 0) {
544 cd_name = prop->value();
546 throw failed_constructor ();
549 if ((prop = cd_node->property ("value")) != 0) {
550 cd_value = prop->value();
552 throw failed_constructor ();
556 cd_info[cd_name] = cd_value;
559 if ((prop = node.property ("position-lock-style")) != 0) {
560 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
563 XMLNode* scene_child = find_named_node (node, SceneChange::xml_node_name);
566 _scene_change = SceneChange::factory (*scene_child, version);
569 recompute_bbt_from_frames ();
571 changed (this); /* EMIT SIGNAL */
572 Changed (); /* EMIT SIGNAL */
574 assert (_start >= 0);
581 Location::set_position_lock_style (PositionLockStyle ps)
583 if (_position_lock_style == ps) {
587 _position_lock_style = ps;
589 recompute_bbt_from_frames ();
591 position_lock_style_changed (this); /* EMIT SIGNAL */
592 PositionLockStyleChanged (); /* EMIT SIGNAL */
596 Location::recompute_bbt_from_frames ()
598 if (_position_lock_style != MusicTime) {
602 _session.bbt_time (_start, _bbt_start);
603 _session.bbt_time (_end, _bbt_end);
607 Location::recompute_frames_from_bbt ()
609 if (_position_lock_style != MusicTime) {
613 TempoMap& map (_session.tempo_map());
614 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
634 Location::set_scene_change (boost::shared_ptr<SceneChange> sc)
638 scene_changed (); /* EMIT SIGNAL */
641 /*---------------------------------------------------------------------- */
643 Locations::Locations (Session& s)
644 : SessionHandleRef (s)
646 current_location = 0;
648 Location::changed.connect_same_thread (*this, boost::bind (&Locations::location_changed, this, _1));
649 Location::start_changed.connect_same_thread (*this, boost::bind (&Locations::location_changed, this, _1));
650 Location::end_changed.connect_same_thread (*this, boost::bind (&Locations::location_changed, this, _1));
653 Locations::~Locations ()
655 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
656 LocationList::iterator tmp = i;
664 Locations::set_current (Location *loc, bool want_lock)
669 Glib::Threads::Mutex::Lock lm (lock);
670 ret = set_current_unlocked (loc);
672 ret = set_current_unlocked (loc);
676 current_changed (current_location); /* EMIT SIGNAL */
682 Locations::next_available_name(string& result,string base)
684 LocationList::iterator i;
690 bool available[SUFFIX_MAX+1];
693 for (int k=1; k<SUFFIX_MAX; k++) {
697 for (i = locations.begin(); i != locations.end(); ++i) {
699 temp = location->name();
700 if (l && !temp.find(base,0)) {
701 suffix = atoi(temp.substr(l,3).c_str());
702 if (suffix) available[suffix] = false;
705 for (int k=1; k<=SUFFIX_MAX; k++) {
707 snprintf (buf, 31, "%d", k);
716 Locations::set_current_unlocked (Location *loc)
718 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
719 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
723 current_location = loc;
731 Glib::Threads::Mutex::Lock lm (lock);
733 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
735 LocationList::iterator tmp = i;
738 if (!(*i)->is_session_range()) {
746 current_location = 0;
749 changed (OTHER); /* EMIT SIGNAL */
750 current_changed (0); /* EMIT SIGNAL */
754 Locations::clear_markers ()
757 Glib::Threads::Mutex::Lock lm (lock);
758 LocationList::iterator tmp;
760 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
764 if ((*i)->is_mark() && !(*i)->is_session_range()) {
773 changed (OTHER); /* EMIT SIGNAL */
777 Locations::clear_ranges ()
780 Glib::Threads::Mutex::Lock lm (lock);
781 LocationList::iterator tmp;
783 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
788 /* We do not remove these ranges as part of this
792 if ((*i)->is_auto_punch() ||
793 (*i)->is_auto_loop() ||
794 (*i)->is_session_range()) {
799 if (!(*i)->is_mark()) {
808 current_location = 0;
811 changed (OTHER); /* EMIT SIGNAL */
812 current_changed (0); /* EMIT SIGNAL */
816 Locations::add (Location *loc, bool make_current)
821 Glib::Threads::Mutex::Lock lm (lock);
822 locations.push_back (loc);
825 current_location = loc;
829 added (loc); /* EMIT SIGNAL */
832 current_changed (current_location); /* EMIT SIGNAL */
835 if (loc->is_session_range()) {
836 Session::StartTimeChanged (0);
837 Session::EndTimeChanged (1);
842 Locations::remove (Location *loc)
844 bool was_removed = false;
845 bool was_current = false;
846 LocationList::iterator i;
848 if (loc->is_session_range()) {
853 Glib::Threads::Mutex::Lock lm (lock);
855 for (i = locations.begin(); i != locations.end(); ++i) {
860 if (current_location == loc) {
861 current_location = 0;
871 removed (loc); /* EMIT SIGNAL */
874 current_changed (0); /* EMIT SIGNAL */
877 changed (REMOVAL); /* EMIT_SIGNAL */
882 Locations::location_changed (Location* /*loc*/)
884 changed (OTHER); /* EMIT SIGNAL */
888 Locations::get_state ()
890 XMLNode *node = new XMLNode ("Locations");
891 LocationList::iterator iter;
892 Glib::Threads::Mutex::Lock lm (lock);
894 for (iter = locations.begin(); iter != locations.end(); ++iter) {
895 node->add_child_nocopy ((*iter)->get_state ());
902 Locations::set_state (const XMLNode& node, int version)
904 if (node.name() != "Locations") {
905 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
909 XMLNodeList nlist = node.children();
911 /* build up a new locations list in here */
912 LocationList new_locations;
914 current_location = 0;
916 Location* session_range_location = 0;
917 if (version < 3000) {
918 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
919 new_locations.push_back (session_range_location);
923 Glib::Threads::Mutex::Lock lm (lock);
925 XMLNodeConstIterator niter;
926 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
930 XMLProperty const * prop_id = (*niter)->property ("id");
932 PBD::ID id (prop_id->value ());
934 LocationList::const_iterator i = locations.begin();
935 while (i != locations.end () && (*i)->id() != id) {
940 if (i != locations.end()) {
941 /* we can re-use an old Location object */
943 loc->set_state (**niter, version);
945 loc = new Location (_session, **niter);
950 if (version < 3000) {
951 /* look for old-style IsStart / IsEnd properties in this location;
952 if they are present, update the session_range_location accordingly
954 XMLProperty const * prop = (*niter)->property ("flags");
956 string v = prop->value ();
958 string::size_type const c = v.find_first_of (',');
959 string const s = v.substr (0, c);
960 if (s == X_("IsStart")) {
961 session_range_location->set_start (loc->start(), true);
963 } else if (s == X_("IsEnd")) {
964 session_range_location->set_end (loc->start(), true);
968 if (c == string::npos) {
972 v = v.substr (c + 1);
978 new_locations.push_back (loc);
982 catch (failed_constructor& err) {
983 error << _("could not load location from session file - ignored") << endmsg;
987 locations = new_locations;
989 if (locations.size()) {
990 current_location = locations.front();
992 current_location = 0;
996 changed (OTHER); /* EMIT SIGNAL */
1002 typedef std::pair<framepos_t,Location*> LocationPair;
1004 struct LocationStartEarlierComparison
1006 bool operator() (LocationPair a, LocationPair b) {
1007 return a.first < b.first;
1011 struct LocationStartLaterComparison
1013 bool operator() (LocationPair a, LocationPair b) {
1014 return a.first > b.first;
1019 Locations::first_mark_before (framepos_t frame, bool include_special_ranges)
1021 Glib::Threads::Mutex::Lock lm (lock);
1022 vector<LocationPair> locs;
1024 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
1025 locs.push_back (make_pair ((*i)->start(), (*i)));
1026 if (!(*i)->is_mark()) {
1027 locs.push_back (make_pair ((*i)->end(), (*i)));
1031 LocationStartLaterComparison cmp;
1032 sort (locs.begin(), locs.end(), cmp);
1034 /* locs is sorted in ascending order */
1036 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
1037 if ((*i).second->is_hidden()) {
1040 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1043 if ((*i).first < frame) {
1052 Locations::mark_at (framepos_t pos, framecnt_t slop) const
1054 Glib::Threads::Mutex::Lock lm (lock);
1055 Location* closest = 0;
1056 frameoffset_t mindelta = max_framepos;
1057 frameoffset_t delta;
1059 /* locations are not necessarily stored in linear time order so we have
1060 * to iterate across all of them to find the one closest to a give point.
1063 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1065 if ((*i)->is_mark()) {
1066 if (pos > (*i)->start()) {
1067 delta = pos - (*i)->start();
1069 delta = (*i)->start() - pos;
1072 if (slop == 0 && delta == 0) {
1073 /* special case: no slop, and direct hit for position */
1077 if (delta <= slop) {
1078 if (delta < mindelta) {
1090 Locations::first_mark_after (framepos_t frame, bool include_special_ranges)
1092 Glib::Threads::Mutex::Lock lm (lock);
1093 vector<LocationPair> locs;
1095 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
1096 locs.push_back (make_pair ((*i)->start(), (*i)));
1097 if (!(*i)->is_mark()) {
1098 locs.push_back (make_pair ((*i)->end(), (*i)));
1102 LocationStartEarlierComparison cmp;
1103 sort (locs.begin(), locs.end(), cmp);
1105 /* locs is sorted in reverse order */
1107 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
1108 if ((*i).second->is_hidden()) {
1111 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1114 if ((*i).first > frame) {
1122 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
1123 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
1125 * @param frame Frame to look for.
1126 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
1127 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
1130 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
1132 before = after = max_framepos;
1137 Glib::Threads::Mutex::Lock lm (lock);
1141 /* Get a list of positions; don't store any that are exactly on our requested position */
1143 std::list<framepos_t> positions;
1145 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1146 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
1150 if (!(*i)->is_hidden()) {
1151 if ((*i)->is_mark ()) {
1152 if ((*i)->start() != frame) {
1153 positions.push_back ((*i)->start ());
1156 if ((*i)->start() != frame) {
1157 positions.push_back ((*i)->start ());
1159 if ((*i)->end() != frame) {
1160 positions.push_back ((*i)->end ());
1166 if (positions.empty ()) {
1172 std::list<framepos_t>::iterator i = positions.begin ();
1173 while (i != positions.end () && *i < frame) {
1177 if (i == positions.end ()) {
1178 /* run out of marks */
1179 before = positions.back ();
1185 if (i == positions.begin ()) {
1195 Locations::session_range_location () const
1197 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1198 if ((*i)->is_session_range()) {
1199 return const_cast<Location*> (*i);
1206 Locations::auto_loop_location () const
1208 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1209 if ((*i)->is_auto_loop()) {
1210 return const_cast<Location*> (*i);
1217 Locations::auto_punch_location () const
1219 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1220 if ((*i)->is_auto_punch()) {
1221 return const_cast<Location*> (*i);
1228 Locations::num_range_markers () const
1231 Glib::Threads::Mutex::Lock lm (lock);
1232 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1233 if ((*i)->is_range_marker()) {
1241 Locations::get_location_by_id(PBD::ID id)
1243 LocationList::iterator it;
1244 for (it = locations.begin(); it != locations.end(); ++it)
1245 if (id == (*it)->id())
1252 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1254 Glib::Threads::Mutex::Lock lm (lock);
1256 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1257 if ((flags == 0 || (*i)->matches (flags)) &&
1258 ((*i)->start() >= start && (*i)->end() < end)) {