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 assert (_start >= 0);
199 framepos_t const old = _start;
202 if (allow_bbt_recompute) {
203 recompute_bbt_from_frames ();
205 start_changed (this); /* EMIT SIGNAL */
206 if (is_session_range ()) {
207 Session::StartTimeChanged (old); /* EMIT SIGNAL */
208 AudioFileSource::set_header_position_offset (s);
212 assert (_start >= 0);
217 /** Set end position.
219 * @param force true to force setting, even if the given new start is after the current end.
220 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
223 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
234 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
243 if (allow_bbt_recompute) {
244 recompute_bbt_from_frames ();
246 start_changed (this); /* EMIT SIGNAL */
247 end_changed (this); /* EMIT SIGNAL */
250 assert (_start >= 0);
258 framepos_t const old = _end;
261 if (allow_bbt_recompute) {
262 recompute_bbt_from_frames ();
264 end_changed(this); /* EMIT SIGNAL */
266 if (is_session_range()) {
267 Session::EndTimeChanged (old); /* EMIT SIGNAL */
277 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
279 if (start < 0 || end < 0) {
284 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
288 /* now we know these values are ok, so force-set them */
289 int const s = set_start (start, true, allow_bbt_recompute);
290 int const e = set_end (end, true, allow_bbt_recompute);
292 return (s == 0 && e == 0) ? 0 : -1;
296 Location::move_to (framepos_t pos)
308 _end = _start + length();
309 recompute_bbt_from_frames ();
311 changed (this); /* EMIT SIGNAL */
314 assert (_start >= 0);
321 Location::set_hidden (bool yn, void *src)
323 if (set_flag_internal (yn, IsHidden)) {
324 FlagsChanged (this, src); /* EMIT SIGNAL */
329 Location::set_cd (bool yn, void *src)
331 // XXX this really needs to be session start
332 // but its not available here - leave to GUI
335 error << _("You cannot put a CD marker at this position") << endmsg;
339 if (set_flag_internal (yn, IsCDMarker)) {
340 FlagsChanged (this, src); /* EMIT SIGNAL */
345 Location::set_is_range_marker (bool yn, void *src)
347 if (set_flag_internal (yn, IsRangeMarker)) {
348 FlagsChanged (this, src); /* EMIT SIGNAL */
353 Location::set_auto_punch (bool yn, void *src)
355 if (is_mark() || _start == _end) {
359 if (set_flag_internal (yn, IsAutoPunch)) {
360 FlagsChanged (this, src); /* EMIT SIGNAL */
365 Location::set_auto_loop (bool yn, void *src)
367 if (is_mark() || _start == _end) {
371 if (set_flag_internal (yn, IsAutoLoop)) {
372 FlagsChanged (this, src); /* EMIT SIGNAL */
377 Location::set_flag_internal (bool yn, Flags flag)
380 if (!(_flags & flag)) {
381 _flags = Flags (_flags | flag);
386 _flags = Flags (_flags & ~flag);
394 Location::set_mark (bool yn)
396 /* This function is private, and so does not emit signals */
398 if (_start != _end) {
402 set_flag_internal (yn, IsMark);
407 Location::cd_info_node(const string & name, const string & value)
409 XMLNode* root = new XMLNode("CD-Info");
411 root->add_property("name", name);
412 root->add_property("value", value);
419 Location::get_state ()
421 XMLNode *node = new XMLNode ("Location");
424 typedef map<string, string>::const_iterator CI;
426 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
427 node->add_child_nocopy(cd_info_node(m->first, m->second));
430 id().print (buf, sizeof (buf));
431 node->add_property("id", buf);
432 node->add_property ("name", name());
433 snprintf (buf, sizeof (buf), "%" PRId64, start());
434 node->add_property ("start", buf);
435 snprintf (buf, sizeof (buf), "%" PRId64, end());
436 node->add_property ("end", buf);
437 node->add_property ("flags", enum_2_string (_flags));
438 node->add_property ("locked", (_locked ? "yes" : "no"));
439 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
442 node->add_child_nocopy (_scene_change->get_state());
449 Location::set_state (const XMLNode& node, int version)
451 const XMLProperty *prop;
453 XMLNodeList cd_list = node.children();
454 XMLNodeConstIterator cd_iter;
460 if (node.name() != "Location") {
461 error << _("incorrect XML node passed to Location::set_state") << endmsg;
465 if (!set_id (node)) {
466 warning << _("XML node for Location has no ID information") << endmsg;
469 if ((prop = node.property ("name")) == 0) {
470 error << _("XML node for Location has no name information") << endmsg;
474 set_name (prop->value());
476 if ((prop = node.property ("start")) == 0) {
477 error << _("XML node for Location has no start information") << endmsg;
481 /* can't use set_start() here, because _end
482 may make the value of _start illegal.
485 sscanf (prop->value().c_str(), "%" PRId64, &_start);
487 if ((prop = node.property ("end")) == 0) {
488 error << _("XML node for Location has no end information") << endmsg;
492 sscanf (prop->value().c_str(), "%" PRId64, &_end);
494 if ((prop = node.property ("flags")) == 0) {
495 error << _("XML node for Location has no flags information") << endmsg;
499 _flags = Flags (string_2_enum (prop->value(), _flags));
501 if ((prop = node.property ("locked")) != 0) {
502 _locked = string_is_affirmative (prop->value());
507 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
511 if (cd_node->name() != "CD-Info") {
515 if ((prop = cd_node->property ("name")) != 0) {
516 cd_name = prop->value();
518 throw failed_constructor ();
521 if ((prop = cd_node->property ("value")) != 0) {
522 cd_value = prop->value();
524 throw failed_constructor ();
528 cd_info[cd_name] = cd_value;
531 if ((prop = node.property ("position-lock-style")) != 0) {
532 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
535 XMLNode* scene_child = find_named_node (node, SceneChange::xml_node_name);
538 _scene_change = SceneChange::factory (*scene_child, version);
541 _scene_change->set_time (_start);
545 recompute_bbt_from_frames ();
547 changed (this); /* EMIT SIGNAL */
549 assert (_start >= 0);
556 Location::set_position_lock_style (PositionLockStyle ps)
558 if (_position_lock_style == ps) {
562 _position_lock_style = ps;
564 recompute_bbt_from_frames ();
566 PositionLockStyleChanged (this); /* EMIT SIGNAL */
570 Location::recompute_bbt_from_frames ()
572 if (_position_lock_style != MusicTime) {
576 _session.bbt_time (_start, _bbt_start);
577 _session.bbt_time (_end, _bbt_end);
581 Location::recompute_frames_from_bbt ()
583 if (_position_lock_style != MusicTime) {
587 TempoMap& map (_session.tempo_map());
588 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
606 Location::set_scene_change (boost::shared_ptr<SceneChange> sc)
610 scene_changed (); /* EMIT SIGNAL */
613 /*---------------------------------------------------------------------- */
615 Locations::Locations (Session& s)
616 : SessionHandleRef (s)
618 current_location = 0;
621 Locations::~Locations ()
623 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
624 LocationList::iterator tmp = i;
632 Locations::set_current (Location *loc, bool want_lock)
637 Glib::Threads::Mutex::Lock lm (lock);
638 ret = set_current_unlocked (loc);
640 ret = set_current_unlocked (loc);
644 current_changed (current_location); /* EMIT SIGNAL */
650 Locations::next_available_name(string& result,string base)
652 LocationList::iterator i;
658 bool available[SUFFIX_MAX+1];
661 for (int k=1; k<SUFFIX_MAX; k++) {
665 for (i = locations.begin(); i != locations.end(); ++i) {
667 temp = location->name();
668 if (l && !temp.find(base,0)) {
669 suffix = atoi(temp.substr(l,3).c_str());
670 if (suffix) available[suffix] = false;
673 for (int k=1; k<=SUFFIX_MAX; k++) {
675 snprintf (buf, 31, "%d", k);
684 Locations::set_current_unlocked (Location *loc)
686 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
687 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
691 current_location = loc;
699 Glib::Threads::Mutex::Lock lm (lock);
701 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
703 LocationList::iterator tmp = i;
706 if (!(*i)->is_session_range()) {
714 current_location = 0;
717 changed (OTHER); /* EMIT SIGNAL */
718 current_changed (0); /* EMIT SIGNAL */
722 Locations::clear_markers ()
725 Glib::Threads::Mutex::Lock lm (lock);
726 LocationList::iterator tmp;
728 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
732 if ((*i)->is_mark() && !(*i)->is_session_range()) {
741 changed (OTHER); /* EMIT SIGNAL */
745 Locations::clear_ranges ()
748 Glib::Threads::Mutex::Lock lm (lock);
749 LocationList::iterator tmp;
751 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
756 if (!(*i)->is_mark()) {
765 current_location = 0;
768 changed (OTHER); /* EMIT SIGNAL */
769 current_changed (0); /* EMIT SIGNAL */
773 Locations::add (Location *loc, bool make_current)
778 Glib::Threads::Mutex::Lock lm (lock);
779 locations.push_back (loc);
782 current_location = loc;
786 added (loc); /* EMIT SIGNAL */
789 current_changed (current_location); /* EMIT SIGNAL */
792 if (loc->is_session_range()) {
793 Session::StartTimeChanged (0);
794 Session::EndTimeChanged (1);
799 Locations::remove (Location *loc)
801 bool was_removed = false;
802 bool was_current = false;
803 LocationList::iterator i;
805 if (loc->is_session_range()) {
810 Glib::Threads::Mutex::Lock lm (lock);
812 for (i = locations.begin(); i != locations.end(); ++i) {
817 if (current_location == loc) {
818 current_location = 0;
828 removed (loc); /* EMIT SIGNAL */
831 current_changed (0); /* EMIT SIGNAL */
834 changed (REMOVAL); /* EMIT_SIGNAL */
839 Locations::location_changed (Location* /*loc*/)
841 changed (OTHER); /* EMIT SIGNAL */
845 Locations::get_state ()
847 XMLNode *node = new XMLNode ("Locations");
848 LocationList::iterator iter;
849 Glib::Threads::Mutex::Lock lm (lock);
851 for (iter = locations.begin(); iter != locations.end(); ++iter) {
852 node->add_child_nocopy ((*iter)->get_state ());
859 Locations::set_state (const XMLNode& node, int version)
861 if (node.name() != "Locations") {
862 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
866 XMLNodeList nlist = node.children();
868 /* build up a new locations list in here */
869 LocationList new_locations;
871 current_location = 0;
873 Location* session_range_location = 0;
874 if (version < 3000) {
875 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
876 new_locations.push_back (session_range_location);
880 Glib::Threads::Mutex::Lock lm (lock);
882 XMLNodeConstIterator niter;
883 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
887 XMLProperty const * prop_id = (*niter)->property ("id");
889 PBD::ID id (prop_id->value ());
891 LocationList::const_iterator i = locations.begin();
892 while (i != locations.end () && (*i)->id() != id) {
897 if (i != locations.end()) {
898 /* we can re-use an old Location object */
900 loc->set_state (**niter, version);
902 loc = new Location (_session, **niter);
907 if (version < 3000) {
908 /* look for old-style IsStart / IsEnd properties in this location;
909 if they are present, update the session_range_location accordingly
911 XMLProperty const * prop = (*niter)->property ("flags");
913 string v = prop->value ();
915 string::size_type const c = v.find_first_of (',');
916 string const s = v.substr (0, c);
917 if (s == X_("IsStart")) {
918 session_range_location->set_start (loc->start(), true);
920 } else if (s == X_("IsEnd")) {
921 session_range_location->set_end (loc->start(), true);
925 if (c == string::npos) {
929 v = v.substr (c + 1);
935 new_locations.push_back (loc);
939 catch (failed_constructor& err) {
940 error << _("could not load location from session file - ignored") << endmsg;
944 locations = new_locations;
946 if (locations.size()) {
947 current_location = locations.front();
949 current_location = 0;
953 changed (OTHER); /* EMIT SIGNAL */
959 typedef std::pair<framepos_t,Location*> LocationPair;
961 struct LocationStartEarlierComparison
963 bool operator() (LocationPair a, LocationPair b) {
964 return a.first < b.first;
968 struct LocationStartLaterComparison
970 bool operator() (LocationPair a, LocationPair b) {
971 return a.first > b.first;
976 Locations::first_mark_before (framepos_t frame, bool include_special_ranges)
978 Glib::Threads::Mutex::Lock lm (lock);
979 vector<LocationPair> locs;
981 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
982 locs.push_back (make_pair ((*i)->start(), (*i)));
983 if (!(*i)->is_mark()) {
984 locs.push_back (make_pair ((*i)->end(), (*i)));
988 LocationStartLaterComparison cmp;
989 sort (locs.begin(), locs.end(), cmp);
991 /* locs is sorted in ascending order */
993 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
994 if ((*i).second->is_hidden()) {
997 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1000 if ((*i).first < frame) {
1009 Locations::mark_at (framepos_t pos, framecnt_t slop) const
1011 Glib::Threads::Mutex::Lock lm (lock);
1012 Location* closest = 0;
1013 frameoffset_t mindelta = max_framepos;
1014 frameoffset_t delta;
1016 /* locations are not necessarily stored in linear time order so we have
1017 * to iterate across all of them to find the one closest to a give point.
1020 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1022 if ((*i)->is_mark()) {
1023 if (pos > (*i)->start()) {
1024 delta = pos - (*i)->start();
1026 delta = (*i)->start() - pos;
1029 if (slop == 0 && delta == 0) {
1030 /* special case: no slop, and direct hit for position */
1034 if (delta <= slop) {
1035 if (delta < mindelta) {
1047 Locations::first_mark_after (framepos_t frame, bool include_special_ranges)
1049 Glib::Threads::Mutex::Lock lm (lock);
1050 vector<LocationPair> locs;
1052 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
1053 locs.push_back (make_pair ((*i)->start(), (*i)));
1054 if (!(*i)->is_mark()) {
1055 locs.push_back (make_pair ((*i)->end(), (*i)));
1059 LocationStartEarlierComparison cmp;
1060 sort (locs.begin(), locs.end(), cmp);
1062 /* locs is sorted in reverse order */
1064 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
1065 if ((*i).second->is_hidden()) {
1068 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
1071 if ((*i).first > frame) {
1079 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
1080 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
1082 * @param frame Frame to look for.
1083 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
1084 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
1087 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
1089 before = after = max_framepos;
1094 Glib::Threads::Mutex::Lock lm (lock);
1098 /* Get a list of positions; don't store any that are exactly on our requested position */
1100 std::list<framepos_t> positions;
1102 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1103 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
1107 if (!(*i)->is_hidden()) {
1108 if ((*i)->is_mark ()) {
1109 if ((*i)->start() != frame) {
1110 positions.push_back ((*i)->start ());
1113 if ((*i)->start() != frame) {
1114 positions.push_back ((*i)->start ());
1116 if ((*i)->end() != frame) {
1117 positions.push_back ((*i)->end ());
1123 if (positions.empty ()) {
1129 std::list<framepos_t>::iterator i = positions.begin ();
1130 while (i != positions.end () && *i < frame) {
1134 if (i == positions.end ()) {
1135 /* run out of marks */
1136 before = positions.back ();
1142 if (i == positions.begin ()) {
1152 Locations::session_range_location () const
1154 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1155 if ((*i)->is_session_range()) {
1156 return const_cast<Location*> (*i);
1163 Locations::auto_loop_location () const
1165 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1166 if ((*i)->is_auto_loop()) {
1167 return const_cast<Location*> (*i);
1174 Locations::auto_punch_location () const
1176 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1177 if ((*i)->is_auto_punch()) {
1178 return const_cast<Location*> (*i);
1185 Locations::num_range_markers () const
1188 Glib::Threads::Mutex::Lock lm (lock);
1189 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1190 if ((*i)->is_range_marker()) {
1198 Locations::get_location_by_id(PBD::ID id)
1200 LocationList::iterator it;
1201 for (it = locations.begin(); it != locations.end(); ++it)
1202 if (id == (*it)->id())
1209 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1211 Glib::Threads::Mutex::Lock lm (lock);
1213 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1214 if ((flags == 0 || (*i)->matches (flags)) &&
1215 ((*i)->start() >= start && (*i)->end() < end)) {