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;
48 Location::Location (Session& s)
49 : SessionHandleRef (s)
54 , _position_lock_style (AudioTime)
60 /** Construct a new Location, giving it the position lock style determined by glue-new-markers-to-bars-and-beats */
61 Location::Location (Session& s, framepos_t sample_start, framepos_t sample_end, const std::string &name, Flags bits)
62 : SessionHandleRef (s)
64 , _start (sample_start)
68 , _position_lock_style (s.config.get_glue_new_markers_to_bars_and_beats() ? MusicTime : AudioTime)
70 recompute_bbt_from_frames ();
76 Location::Location (const Location& other)
77 : SessionHandleRef (other._session)
78 , StatefulDestructible()
80 , _start (other._start)
81 , _bbt_start (other._bbt_start)
83 , _bbt_end (other._bbt_end)
84 , _flags (other._flags)
85 , _position_lock_style (other._position_lock_style)
87 /* copy is not locked even if original was */
94 /* scene change is NOT COPIED */
97 Location::Location (Session& s, const XMLNode& node)
98 : SessionHandleRef (s)
99 , _position_lock_style (AudioTime)
101 /* Note: _position_lock_style is initialised above in case set_state doesn't set it
102 (for 2.X session file compatibility).
105 if (set_state (node, Stateful::loading_state_version)) {
106 throw failed_constructor ();
109 assert (_start >= 0);
114 Location::operator== (const Location& other)
116 if (_name != other._name ||
117 _start != other._start ||
118 _end != other._end ||
119 _bbt_start != other._bbt_start ||
120 _bbt_end != other._bbt_end ||
121 _flags != other._flags ||
122 _position_lock_style != other._position_lock_style) {
129 Location::operator= (const Location& other)
131 if (this == &other) {
136 _start = other._start;
137 _bbt_start = other._bbt_start;
139 _bbt_end = other._bbt_end;
140 _flags = other._flags;
141 _position_lock_style = other._position_lock_style;
143 /* XXX need to copy scene change */
145 /* copy is not locked even if original was */
149 /* "changed" not emitted on purpose */
151 assert (_start >= 0);
157 /** Set start position.
158 * @param s New start.
159 * @param force true to force setting, even if the given new start is after the current end.
160 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
163 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
174 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
183 if (allow_bbt_recompute) {
184 recompute_bbt_from_frames ();
187 start_changed (this); /* EMIT SIGNAL */
188 end_changed (this); /* EMIT SIGNAL */
191 /* moving the start (position) of a marker with a scene change
192 requires an update in the Scene Changer.
196 scene_changed (); /* EMIT SIGNAL */
199 assert (_start >= 0);
207 framepos_t const old = _start;
210 if (allow_bbt_recompute) {
211 recompute_bbt_from_frames ();
213 start_changed (this); /* EMIT SIGNAL */
215 if (is_session_range ()) {
216 Session::StartTimeChanged (old); /* EMIT SIGNAL */
217 AudioFileSource::set_header_position_offset (s);
221 assert (_start >= 0);
226 /** Set end position.
228 * @param force true to force setting, even if the given new start is after the current end.
229 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
232 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
243 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
252 if (allow_bbt_recompute) {
253 recompute_bbt_from_frames ();
255 start_changed (this); /* EMIT SIGNAL */
256 end_changed (this); /* EMIT SIGNAL */
259 assert (_start >= 0);
267 framepos_t const old = _end;
270 if (allow_bbt_recompute) {
271 recompute_bbt_from_frames ();
273 end_changed(this); /* EMIT SIGNAL */
275 if (is_session_range()) {
276 Session::EndTimeChanged (old); /* EMIT SIGNAL */
286 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
288 if (start < 0 || end < 0) {
293 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
297 /* now we know these values are ok, so force-set them */
298 int const s = set_start (start, true, allow_bbt_recompute);
299 int const e = set_end (end, true, allow_bbt_recompute);
301 return (s == 0 && e == 0) ? 0 : -1;
305 Location::move_to (framepos_t pos)
317 _end = _start + length();
318 recompute_bbt_from_frames ();
320 changed (this); /* EMIT SIGNAL */
323 assert (_start >= 0);
330 Location::set_hidden (bool yn, void *src)
332 if (set_flag_internal (yn, IsHidden)) {
333 FlagsChanged (this, src); /* EMIT SIGNAL */
338 Location::set_cd (bool yn, void *src)
340 // XXX this really needs to be session start
341 // but its not available here - leave to GUI
344 error << _("You cannot put a CD marker at this position") << endmsg;
348 if (set_flag_internal (yn, IsCDMarker)) {
349 FlagsChanged (this, src); /* EMIT SIGNAL */
354 Location::set_is_range_marker (bool yn, void *src)
356 if (set_flag_internal (yn, IsRangeMarker)) {
357 FlagsChanged (this, src); /* EMIT SIGNAL */
362 Location::set_auto_punch (bool yn, void *src)
364 if (is_mark() || _start == _end) {
368 if (set_flag_internal (yn, IsAutoPunch)) {
369 FlagsChanged (this, src); /* EMIT SIGNAL */
374 Location::set_auto_loop (bool yn, void *src)
376 if (is_mark() || _start == _end) {
380 if (set_flag_internal (yn, IsAutoLoop)) {
381 FlagsChanged (this, src); /* EMIT SIGNAL */
386 Location::set_flag_internal (bool yn, Flags flag)
389 if (!(_flags & flag)) {
390 _flags = Flags (_flags | flag);
395 _flags = Flags (_flags & ~flag);
403 Location::set_mark (bool yn)
405 /* This function is private, and so does not emit signals */
407 if (_start != _end) {
411 set_flag_internal (yn, IsMark);
416 Location::cd_info_node(const string & name, const string & value)
418 XMLNode* root = new XMLNode("CD-Info");
420 root->add_property("name", name);
421 root->add_property("value", value);
428 Location::get_state ()
430 XMLNode *node = new XMLNode ("Location");
433 typedef map<string, string>::const_iterator CI;
435 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
436 node->add_child_nocopy(cd_info_node(m->first, m->second));
439 id().print (buf, sizeof (buf));
440 node->add_property("id", buf);
441 node->add_property ("name", name());
442 snprintf (buf, sizeof (buf), "%" PRId64, start());
443 node->add_property ("start", buf);
444 snprintf (buf, sizeof (buf), "%" PRId64, end());
445 node->add_property ("end", buf);
446 node->add_property ("flags", enum_2_string (_flags));
447 node->add_property ("locked", (_locked ? "yes" : "no"));
448 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
451 node->add_child_nocopy (_scene_change->get_state());
458 Location::set_state (const XMLNode& node, int version)
460 const XMLProperty *prop;
462 XMLNodeList cd_list = node.children();
463 XMLNodeConstIterator cd_iter;
469 if (node.name() != "Location") {
470 error << _("incorrect XML node passed to Location::set_state") << endmsg;
474 if (!set_id (node)) {
475 warning << _("XML node for Location has no ID information") << endmsg;
478 if ((prop = node.property ("name")) == 0) {
479 error << _("XML node for Location has no name information") << endmsg;
483 set_name (prop->value());
485 if ((prop = node.property ("start")) == 0) {
486 error << _("XML node for Location has no start information") << endmsg;
490 /* can't use set_start() here, because _end
491 may make the value of _start illegal.
494 sscanf (prop->value().c_str(), "%" PRId64, &_start);
496 if ((prop = node.property ("end")) == 0) {
497 error << _("XML node for Location has no end information") << endmsg;
501 sscanf (prop->value().c_str(), "%" PRId64, &_end);
503 if ((prop = node.property ("flags")) == 0) {
504 error << _("XML node for Location has no flags information") << endmsg;
508 _flags = Flags (string_2_enum (prop->value(), _flags));
510 if ((prop = node.property ("locked")) != 0) {
511 _locked = string_is_affirmative (prop->value());
516 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
520 if (cd_node->name() != "CD-Info") {
524 if ((prop = cd_node->property ("name")) != 0) {
525 cd_name = prop->value();
527 throw failed_constructor ();
530 if ((prop = cd_node->property ("value")) != 0) {
531 cd_value = prop->value();
533 throw failed_constructor ();
537 cd_info[cd_name] = cd_value;
540 if ((prop = node.property ("position-lock-style")) != 0) {
541 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
544 XMLNode* scene_child = find_named_node (node, SceneChange::xml_node_name);
547 _scene_change = SceneChange::factory (*scene_child, version);
550 recompute_bbt_from_frames ();
552 changed (this); /* EMIT SIGNAL */
554 assert (_start >= 0);
561 Location::set_position_lock_style (PositionLockStyle ps)
563 if (_position_lock_style == ps) {
567 _position_lock_style = ps;
569 recompute_bbt_from_frames ();
571 PositionLockStyleChanged (this); /* EMIT SIGNAL */
575 Location::recompute_bbt_from_frames ()
577 if (_position_lock_style != MusicTime) {
581 _session.bbt_time (_start, _bbt_start);
582 _session.bbt_time (_end, _bbt_end);
586 Location::recompute_frames_from_bbt ()
588 if (_position_lock_style != MusicTime) {
592 TempoMap& map (_session.tempo_map());
593 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
611 Location::set_scene_change (boost::shared_ptr<SceneChange> sc)
615 scene_changed (); /* EMIT SIGNAL */
618 /*---------------------------------------------------------------------- */
620 Locations::Locations (Session& s)
621 : SessionHandleRef (s)
623 current_location = 0;
626 Locations::~Locations ()
628 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
629 LocationList::iterator tmp = i;
637 Locations::set_current (Location *loc, bool want_lock)
642 Glib::Threads::Mutex::Lock lm (lock);
643 ret = set_current_unlocked (loc);
645 ret = set_current_unlocked (loc);
649 current_changed (current_location); /* EMIT SIGNAL */
655 Locations::next_available_name(string& result,string base)
657 LocationList::iterator i;
663 bool available[SUFFIX_MAX+1];
666 for (int k=1; k<SUFFIX_MAX; k++) {
670 for (i = locations.begin(); i != locations.end(); ++i) {
672 temp = location->name();
673 if (l && !temp.find(base,0)) {
674 suffix = atoi(temp.substr(l,3).c_str());
675 if (suffix) available[suffix] = false;
678 for (int k=1; k<=SUFFIX_MAX; k++) {
680 snprintf (buf, 31, "%d", k);
689 Locations::set_current_unlocked (Location *loc)
691 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
692 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
696 current_location = loc;
704 Glib::Threads::Mutex::Lock lm (lock);
706 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
708 LocationList::iterator tmp = i;
711 if (!(*i)->is_session_range()) {
719 current_location = 0;
722 changed (OTHER); /* EMIT SIGNAL */
723 current_changed (0); /* EMIT SIGNAL */
727 Locations::clear_markers ()
730 Glib::Threads::Mutex::Lock lm (lock);
731 LocationList::iterator tmp;
733 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
737 if ((*i)->is_mark() && !(*i)->is_session_range()) {
746 changed (OTHER); /* EMIT SIGNAL */
750 Locations::clear_ranges ()
753 Glib::Threads::Mutex::Lock lm (lock);
754 LocationList::iterator tmp;
756 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
761 if (!(*i)->is_mark()) {
770 current_location = 0;
773 changed (OTHER); /* EMIT SIGNAL */
774 current_changed (0); /* EMIT SIGNAL */
778 Locations::add (Location *loc, bool make_current)
783 Glib::Threads::Mutex::Lock lm (lock);
784 locations.push_back (loc);
787 current_location = loc;
791 added (loc); /* EMIT SIGNAL */
794 current_changed (current_location); /* EMIT SIGNAL */
797 if (loc->is_session_range()) {
798 Session::StartTimeChanged (0);
799 Session::EndTimeChanged (1);
804 Locations::remove (Location *loc)
806 bool was_removed = false;
807 bool was_current = false;
808 LocationList::iterator i;
810 if (loc->is_session_range()) {
815 Glib::Threads::Mutex::Lock lm (lock);
817 for (i = locations.begin(); i != locations.end(); ++i) {
822 if (current_location == loc) {
823 current_location = 0;
833 removed (loc); /* EMIT SIGNAL */
836 current_changed (0); /* EMIT SIGNAL */
839 changed (REMOVAL); /* EMIT_SIGNAL */
844 Locations::location_changed (Location* /*loc*/)
846 changed (OTHER); /* EMIT SIGNAL */
850 Locations::get_state ()
852 XMLNode *node = new XMLNode ("Locations");
853 LocationList::iterator iter;
854 Glib::Threads::Mutex::Lock lm (lock);
856 for (iter = locations.begin(); iter != locations.end(); ++iter) {
857 node->add_child_nocopy ((*iter)->get_state ());
864 Locations::set_state (const XMLNode& node, int version)
866 if (node.name() != "Locations") {
867 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
871 XMLNodeList nlist = node.children();
873 /* build up a new locations list in here */
874 LocationList new_locations;
876 current_location = 0;
878 Location* session_range_location = 0;
879 if (version < 3000) {
880 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
881 new_locations.push_back (session_range_location);
885 Glib::Threads::Mutex::Lock lm (lock);
887 XMLNodeConstIterator niter;
888 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
892 XMLProperty const * prop_id = (*niter)->property ("id");
894 PBD::ID id (prop_id->value ());
896 LocationList::const_iterator i = locations.begin();
897 while (i != locations.end () && (*i)->id() != id) {
902 if (i != locations.end()) {
903 /* we can re-use an old Location object */
905 loc->set_state (**niter, version);
907 loc = new Location (_session, **niter);
912 if (version < 3000) {
913 /* look for old-style IsStart / IsEnd properties in this location;
914 if they are present, update the session_range_location accordingly
916 XMLProperty const * prop = (*niter)->property ("flags");
918 string v = prop->value ();
920 string::size_type const c = v.find_first_of (',');
921 string const s = v.substr (0, c);
922 if (s == X_("IsStart")) {
923 session_range_location->set_start (loc->start(), true);
925 } else if (s == X_("IsEnd")) {
926 session_range_location->set_end (loc->start(), true);
930 if (c == string::npos) {
934 v = v.substr (c + 1);
940 new_locations.push_back (loc);
944 catch (failed_constructor& err) {
945 error << _("could not load location from session file - ignored") << endmsg;
949 locations = new_locations;
951 if (locations.size()) {
952 current_location = locations.front();
954 current_location = 0;
958 changed (OTHER); /* EMIT SIGNAL */
964 typedef std::pair<framepos_t,Location*> LocationPair;
966 struct LocationStartEarlierComparison
968 bool operator() (LocationPair a, LocationPair b) {
969 return a.first < b.first;
973 struct LocationStartLaterComparison
975 bool operator() (LocationPair a, LocationPair b) {
976 return a.first > b.first;
981 Locations::first_mark_before (framepos_t frame, bool include_special_ranges)
983 Glib::Threads::Mutex::Lock lm (lock);
984 vector<LocationPair> locs;
986 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
987 locs.push_back (make_pair ((*i)->start(), (*i)));
988 if (!(*i)->is_mark()) {
989 locs.push_back (make_pair ((*i)->end(), (*i)));
993 LocationStartLaterComparison cmp;
994 sort (locs.begin(), locs.end(), cmp);
996 /* locs is sorted in ascending order */
998 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
999 if ((*i).second->is_hidden()) {
1002 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1005 if ((*i).first < frame) {
1014 Locations::mark_at (framepos_t pos, framecnt_t slop) const
1016 Glib::Threads::Mutex::Lock lm (lock);
1017 Location* closest = 0;
1018 frameoffset_t mindelta = max_framepos;
1019 frameoffset_t delta;
1021 /* locations are not necessarily stored in linear time order so we have
1022 * to iterate across all of them to find the one closest to a give point.
1025 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1027 if ((*i)->is_mark()) {
1028 if (pos > (*i)->start()) {
1029 delta = pos - (*i)->start();
1031 delta = (*i)->start() - pos;
1034 if (slop == 0 && delta == 0) {
1035 /* special case: no slop, and direct hit for position */
1039 if (delta <= slop) {
1040 if (delta < mindelta) {
1052 Locations::first_mark_after (framepos_t frame, bool include_special_ranges)
1054 Glib::Threads::Mutex::Lock lm (lock);
1055 vector<LocationPair> locs;
1057 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
1058 locs.push_back (make_pair ((*i)->start(), (*i)));
1059 if (!(*i)->is_mark()) {
1060 locs.push_back (make_pair ((*i)->end(), (*i)));
1064 LocationStartEarlierComparison cmp;
1065 sort (locs.begin(), locs.end(), cmp);
1067 /* locs is sorted in reverse order */
1069 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
1070 if ((*i).second->is_hidden()) {
1073 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1076 if ((*i).first > frame) {
1084 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
1085 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
1087 * @param frame Frame to look for.
1088 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
1089 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
1092 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
1094 before = after = max_framepos;
1099 Glib::Threads::Mutex::Lock lm (lock);
1103 /* Get a list of positions; don't store any that are exactly on our requested position */
1105 std::list<framepos_t> positions;
1107 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1108 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
1112 if (!(*i)->is_hidden()) {
1113 if ((*i)->is_mark ()) {
1114 if ((*i)->start() != frame) {
1115 positions.push_back ((*i)->start ());
1118 if ((*i)->start() != frame) {
1119 positions.push_back ((*i)->start ());
1121 if ((*i)->end() != frame) {
1122 positions.push_back ((*i)->end ());
1128 if (positions.empty ()) {
1134 std::list<framepos_t>::iterator i = positions.begin ();
1135 while (i != positions.end () && *i < frame) {
1139 if (i == positions.end ()) {
1140 /* run out of marks */
1141 before = positions.back ();
1147 if (i == positions.begin ()) {
1157 Locations::session_range_location () const
1159 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1160 if ((*i)->is_session_range()) {
1161 return const_cast<Location*> (*i);
1168 Locations::auto_loop_location () const
1170 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1171 if ((*i)->is_auto_loop()) {
1172 return const_cast<Location*> (*i);
1179 Locations::auto_punch_location () const
1181 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1182 if ((*i)->is_auto_punch()) {
1183 return const_cast<Location*> (*i);
1190 Locations::num_range_markers () const
1193 Glib::Threads::Mutex::Lock lm (lock);
1194 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1195 if ((*i)->is_range_marker()) {
1203 Locations::get_location_by_id(PBD::ID id)
1205 LocationList::iterator it;
1206 for (it = locations.begin(); it != locations.end(); ++it)
1207 if (id == (*it)->id())
1214 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1216 Glib::Threads::Mutex::Lock lm (lock);
1218 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1219 if ((flags == 0 || (*i)->matches (flags)) &&
1220 ((*i)->start() >= start && (*i)->end() < end)) {