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/session.h"
34 #include "ardour/audiofilesource.h"
35 #include "ardour/tempo.h"
42 using namespace ARDOUR;
45 Location::Location (Session& s)
46 : SessionHandleRef (s)
51 , _position_lock_style (AudioTime)
57 /** Construct a new Location, giving it the position lock style determined by glue-new-markers-to-bars-and-beats */
58 Location::Location (Session& s, framepos_t sample_start, framepos_t sample_end, const std::string &name, Flags bits)
59 : SessionHandleRef (s)
61 , _start (sample_start)
65 , _position_lock_style (s.config.get_glue_new_markers_to_bars_and_beats() ? MusicTime : AudioTime)
67 recompute_bbt_from_frames ();
73 Location::Location (const Location& other)
74 : SessionHandleRef (other._session)
75 , StatefulDestructible()
77 , _start (other._start)
78 , _bbt_start (other._bbt_start)
80 , _bbt_end (other._bbt_end)
81 , _flags (other._flags)
82 , _position_lock_style (other._position_lock_style)
84 /* copy is not locked even if original was */
92 Location::Location (Session& s, const XMLNode& node)
93 : SessionHandleRef (s)
94 , _position_lock_style (AudioTime)
96 /* Note: _position_lock_style is initialised above in case set_state doesn't set it
97 (for 2.X session file compatibility).
100 if (set_state (node, Stateful::loading_state_version)) {
101 throw failed_constructor ();
104 assert (_start >= 0);
109 Location::operator== (const Location& other)
111 if (_name != other._name ||
112 _start != other._start ||
113 _end != other._end ||
114 _bbt_start != other._bbt_start ||
115 _bbt_end != other._bbt_end ||
116 _flags != other._flags ||
117 _position_lock_style != other._position_lock_style) {
124 Location::operator= (const Location& other)
126 if (this == &other) {
131 _start = other._start;
132 _bbt_start = other._bbt_start;
134 _bbt_end = other._bbt_end;
135 _flags = other._flags;
136 _position_lock_style = other._position_lock_style;
138 /* copy is not locked even if original was */
142 /* "changed" not emitted on purpose */
144 assert (_start >= 0);
150 /** Set start position.
151 * @param s New start.
152 * @param force true to force setting, even if the given new start is after the current end.
153 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
156 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
167 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
176 if (allow_bbt_recompute) {
177 recompute_bbt_from_frames ();
180 start_changed (this); /* EMIT SIGNAL */
181 end_changed (this); /* EMIT SIGNAL */
184 assert (_start >= 0);
192 framepos_t const old = _start;
195 if (allow_bbt_recompute) {
196 recompute_bbt_from_frames ();
198 start_changed (this); /* EMIT SIGNAL */
199 if (is_session_range ()) {
200 Session::StartTimeChanged (old); /* EMIT SIGNAL */
201 AudioFileSource::set_header_position_offset (s);
205 assert (_start >= 0);
210 /** Set end position.
212 * @param force true to force setting, even if the given new start is after the current end.
213 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
216 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
227 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
236 if (allow_bbt_recompute) {
237 recompute_bbt_from_frames ();
239 start_changed (this); /* EMIT SIGNAL */
240 end_changed (this); /* EMIT SIGNAL */
243 assert (_start >= 0);
251 framepos_t const old = _end;
254 if (allow_bbt_recompute) {
255 recompute_bbt_from_frames ();
257 end_changed(this); /* EMIT SIGNAL */
259 if (is_session_range()) {
260 Session::EndTimeChanged (old); /* EMIT SIGNAL */
270 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
272 if (start < 0 || end < 0) {
277 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
281 /* now we know these values are ok, so force-set them */
282 int const s = set_start (start, true, allow_bbt_recompute);
283 int const e = set_end (end, true, allow_bbt_recompute);
285 return (s == 0 && e == 0) ? 0 : -1;
289 Location::move_to (framepos_t pos)
301 _end = _start + length();
302 recompute_bbt_from_frames ();
304 changed (this); /* EMIT SIGNAL */
307 assert (_start >= 0);
314 Location::set_hidden (bool yn, void *src)
316 if (set_flag_internal (yn, IsHidden)) {
317 FlagsChanged (this, src); /* EMIT SIGNAL */
322 Location::set_cd (bool yn, void *src)
324 // XXX this really needs to be session start
325 // but its not available here - leave to GUI
328 error << _("You cannot put a CD marker at this position") << endmsg;
332 if (set_flag_internal (yn, IsCDMarker)) {
333 FlagsChanged (this, src); /* EMIT SIGNAL */
338 Location::set_is_range_marker (bool yn, void *src)
340 if (set_flag_internal (yn, IsRangeMarker)) {
341 FlagsChanged (this, src); /* EMIT SIGNAL */
346 Location::set_auto_punch (bool yn, void *src)
348 if (is_mark() || _start == _end) {
352 if (set_flag_internal (yn, IsAutoPunch)) {
353 FlagsChanged (this, src); /* EMIT SIGNAL */
358 Location::set_auto_loop (bool yn, void *src)
360 if (is_mark() || _start == _end) {
364 if (set_flag_internal (yn, IsAutoLoop)) {
365 FlagsChanged (this, src); /* EMIT SIGNAL */
370 Location::set_flag_internal (bool yn, Flags flag)
373 if (!(_flags & flag)) {
374 _flags = Flags (_flags | flag);
379 _flags = Flags (_flags & ~flag);
387 Location::set_mark (bool yn)
389 /* This function is private, and so does not emit signals */
391 if (_start != _end) {
395 set_flag_internal (yn, IsMark);
400 Location::cd_info_node(const string & name, const string & value)
402 XMLNode* root = new XMLNode("CD-Info");
404 root->add_property("name", name);
405 root->add_property("value", value);
412 Location::get_state ()
414 XMLNode *node = new XMLNode ("Location");
417 typedef map<string, string>::const_iterator CI;
419 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
420 node->add_child_nocopy(cd_info_node(m->first, m->second));
423 id().print (buf, sizeof (buf));
424 node->add_property("id", buf);
425 node->add_property ("name", name());
426 snprintf (buf, sizeof (buf), "%" PRId64, start());
427 node->add_property ("start", buf);
428 snprintf (buf, sizeof (buf), "%" PRId64, end());
429 node->add_property ("end", buf);
430 node->add_property ("flags", enum_2_string (_flags));
431 node->add_property ("locked", (_locked ? "yes" : "no"));
432 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
438 Location::set_state (const XMLNode& node, int /*version*/)
440 const XMLProperty *prop;
442 XMLNodeList cd_list = node.children();
443 XMLNodeConstIterator cd_iter;
449 if (node.name() != "Location") {
450 error << _("incorrect XML node passed to Location::set_state") << endmsg;
454 if (!set_id (node)) {
455 warning << _("XML node for Location has no ID information") << endmsg;
458 if ((prop = node.property ("name")) == 0) {
459 error << _("XML node for Location has no name information") << endmsg;
463 set_name (prop->value());
465 if ((prop = node.property ("start")) == 0) {
466 error << _("XML node for Location has no start information") << endmsg;
470 /* can't use set_start() here, because _end
471 may make the value of _start illegal.
474 sscanf (prop->value().c_str(), "%" PRId64, &_start);
476 if ((prop = node.property ("end")) == 0) {
477 error << _("XML node for Location has no end information") << endmsg;
481 sscanf (prop->value().c_str(), "%" PRId64, &_end);
483 if ((prop = node.property ("flags")) == 0) {
484 error << _("XML node for Location has no flags information") << endmsg;
488 _flags = Flags (string_2_enum (prop->value(), _flags));
490 if ((prop = node.property ("locked")) != 0) {
491 _locked = string_is_affirmative (prop->value());
496 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
500 if (cd_node->name() != "CD-Info") {
504 if ((prop = cd_node->property ("name")) != 0) {
505 cd_name = prop->value();
507 throw failed_constructor ();
510 if ((prop = cd_node->property ("value")) != 0) {
511 cd_value = prop->value();
513 throw failed_constructor ();
517 cd_info[cd_name] = cd_value;
520 if ((prop = node.property ("position-lock-style")) != 0) {
521 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
524 recompute_bbt_from_frames ();
526 changed (this); /* EMIT SIGNAL */
528 assert (_start >= 0);
535 Location::set_position_lock_style (PositionLockStyle ps)
537 if (_position_lock_style == ps) {
541 _position_lock_style = ps;
543 recompute_bbt_from_frames ();
545 PositionLockStyleChanged (this); /* EMIT SIGNAL */
549 Location::recompute_bbt_from_frames ()
551 if (_position_lock_style != MusicTime) {
555 _session.bbt_time (_start, _bbt_start);
556 _session.bbt_time (_end, _bbt_end);
560 Location::recompute_frames_from_bbt ()
562 if (_position_lock_style != MusicTime) {
566 TempoMap& map (_session.tempo_map());
567 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
584 /*---------------------------------------------------------------------- */
586 Locations::Locations (Session& s)
587 : SessionHandleRef (s)
589 current_location = 0;
592 Locations::~Locations ()
594 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
595 LocationList::iterator tmp = i;
603 Locations::set_current (Location *loc, bool want_lock)
608 Glib::Threads::Mutex::Lock lm (lock);
609 ret = set_current_unlocked (loc);
611 ret = set_current_unlocked (loc);
615 current_changed (current_location); /* EMIT SIGNAL */
621 Locations::next_available_name(string& result,string base)
623 LocationList::iterator i;
629 bool available[SUFFIX_MAX+1];
632 for (int k=1; k<SUFFIX_MAX; k++) {
636 for (i = locations.begin(); i != locations.end(); ++i) {
638 temp = location->name();
639 if (l && !temp.find(base,0)) {
640 suffix = atoi(temp.substr(l,3).c_str());
641 if (suffix) available[suffix] = false;
644 for (int k=1; k<=SUFFIX_MAX; k++) {
646 snprintf (buf, 31, "%d", k);
655 Locations::set_current_unlocked (Location *loc)
657 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
658 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
662 current_location = loc;
670 Glib::Threads::Mutex::Lock lm (lock);
672 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
674 LocationList::iterator tmp = i;
677 if (!(*i)->is_session_range()) {
684 current_location = 0;
687 changed (OTHER); /* EMIT SIGNAL */
688 current_changed (0); /* EMIT SIGNAL */
692 Locations::clear_markers ()
695 Glib::Threads::Mutex::Lock lm (lock);
696 LocationList::iterator tmp;
698 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
702 if ((*i)->is_mark() && !(*i)->is_session_range()) {
710 changed (OTHER); /* EMIT SIGNAL */
714 Locations::clear_ranges ()
717 Glib::Threads::Mutex::Lock lm (lock);
718 LocationList::iterator tmp;
720 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
725 if (!(*i)->is_mark()) {
733 current_location = 0;
736 changed (OTHER); /* EMIT SIGNAL */
737 current_changed (0); /* EMIT SIGNAL */
741 Locations::add (Location *loc, bool make_current)
746 Glib::Threads::Mutex::Lock lm (lock);
747 locations.push_back (loc);
750 current_location = loc;
754 added (loc); /* EMIT SIGNAL */
757 current_changed (current_location); /* EMIT SIGNAL */
760 if (loc->is_session_range()) {
761 Session::StartTimeChanged (0);
762 Session::EndTimeChanged (1);
767 Locations::remove (Location *loc)
769 bool was_removed = false;
770 bool was_current = false;
771 LocationList::iterator i;
773 if (loc->is_session_range()) {
778 Glib::Threads::Mutex::Lock lm (lock);
780 for (i = locations.begin(); i != locations.end(); ++i) {
784 if (current_location == loc) {
785 current_location = 0;
795 removed (loc); /* EMIT SIGNAL */
798 current_changed (0); /* EMIT SIGNAL */
801 changed (REMOVAL); /* EMIT_SIGNAL */
806 Locations::location_changed (Location* /*loc*/)
808 changed (OTHER); /* EMIT SIGNAL */
812 Locations::get_state ()
814 XMLNode *node = new XMLNode ("Locations");
815 LocationList::iterator iter;
816 Glib::Threads::Mutex::Lock lm (lock);
818 for (iter = locations.begin(); iter != locations.end(); ++iter) {
819 node->add_child_nocopy ((*iter)->get_state ());
826 Locations::set_state (const XMLNode& node, int version)
828 if (node.name() != "Locations") {
829 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
833 XMLNodeList nlist = node.children();
835 /* build up a new locations list in here */
836 LocationList new_locations;
838 current_location = 0;
840 Location* session_range_location = 0;
841 if (version < 3000) {
842 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
843 new_locations.push_back (session_range_location);
847 Glib::Threads::Mutex::Lock lm (lock);
849 XMLNodeConstIterator niter;
850 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
854 XMLProperty const * prop_id = (*niter)->property ("id");
856 PBD::ID id (prop_id->value ());
858 LocationList::const_iterator i = locations.begin();
859 while (i != locations.end () && (*i)->id() != id) {
864 if (i != locations.end()) {
865 /* we can re-use an old Location object */
867 loc->set_state (**niter, version);
869 loc = new Location (_session, **niter);
874 if (version < 3000) {
875 /* look for old-style IsStart / IsEnd properties in this location;
876 if they are present, update the session_range_location accordingly
878 XMLProperty const * prop = (*niter)->property ("flags");
880 string v = prop->value ();
882 string::size_type const c = v.find_first_of (',');
883 string const s = v.substr (0, c);
884 if (s == X_("IsStart")) {
885 session_range_location->set_start (loc->start(), true);
887 } else if (s == X_("IsEnd")) {
888 session_range_location->set_end (loc->start(), true);
892 if (c == string::npos) {
896 v = v.substr (c + 1);
902 new_locations.push_back (loc);
906 catch (failed_constructor& err) {
907 error << _("could not load location from session file - ignored") << endmsg;
911 locations = new_locations;
913 if (locations.size()) {
914 current_location = locations.front();
916 current_location = 0;
920 changed (OTHER); /* EMIT SIGNAL */
926 typedef std::pair<framepos_t,Location*> LocationPair;
928 struct LocationStartEarlierComparison
930 bool operator() (LocationPair a, LocationPair b) {
931 return a.first < b.first;
935 struct LocationStartLaterComparison
937 bool operator() (LocationPair a, LocationPair b) {
938 return a.first > b.first;
943 Locations::first_mark_before (framepos_t frame, bool include_special_ranges)
945 Glib::Threads::Mutex::Lock lm (lock);
946 vector<LocationPair> locs;
948 for (LocationList::iterator i = locations.begin(); i != locations.end(); ++i) {
949 locs.push_back (make_pair ((*i)->start(), (*i)));
950 if (!(*i)->is_mark()) {
951 locs.push_back (make_pair ((*i)->end(), (*i)));
955 LocationStartLaterComparison cmp;
956 sort (locs.begin(), locs.end(), cmp);
958 /* locs is sorted in ascending order */
960 for (vector<LocationPair>::iterator i = locs.begin(); i != locs.end(); ++i) {
961 if ((*i).second->is_hidden()) {
964 if (!include_special_ranges && ((*i).second->is_auto_loop() || (*i).second->is_auto_punch())) {
967 if ((*i).first < frame) {
976 Locations::first_mark_after (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 LocationStartEarlierComparison cmp;
989 sort (locs.begin(), locs.end(), cmp);
991 /* locs is sorted in reverse 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) {
1008 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
1009 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
1011 * @param frame Frame to look for.
1012 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
1013 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
1016 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
1018 before = after = max_framepos;
1023 Glib::Threads::Mutex::Lock lm (lock);
1027 /* Get a list of positions; don't store any that are exactly on our requested position */
1029 std::list<framepos_t> positions;
1031 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
1032 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
1036 if (!(*i)->is_hidden()) {
1037 if ((*i)->is_mark ()) {
1038 if ((*i)->start() != frame) {
1039 positions.push_back ((*i)->start ());
1042 if ((*i)->start() != frame) {
1043 positions.push_back ((*i)->start ());
1045 if ((*i)->end() != frame) {
1046 positions.push_back ((*i)->end ());
1052 if (positions.empty ()) {
1058 std::list<framepos_t>::iterator i = positions.begin ();
1059 while (i != positions.end () && *i < frame) {
1063 if (i == positions.end ()) {
1064 /* run out of marks */
1065 before = positions.back ();
1071 if (i == positions.begin ()) {
1081 Locations::session_range_location () const
1083 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1084 if ((*i)->is_session_range()) {
1085 return const_cast<Location*> (*i);
1092 Locations::auto_loop_location () const
1094 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1095 if ((*i)->is_auto_loop()) {
1096 return const_cast<Location*> (*i);
1103 Locations::auto_punch_location () const
1105 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1106 if ((*i)->is_auto_punch()) {
1107 return const_cast<Location*> (*i);
1114 Locations::num_range_markers () const
1117 Glib::Threads::Mutex::Lock lm (lock);
1118 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1119 if ((*i)->is_range_marker()) {
1127 Locations::get_location_by_id(PBD::ID id)
1129 LocationList::iterator it;
1130 for (it = locations.begin(); it != locations.end(); ++it)
1131 if (id == (*it)->id())
1138 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1140 Glib::Threads::Mutex::Lock lm (lock);
1142 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1143 if ((flags == 0 || (*i)->matches (flags)) &&
1144 ((*i)->start() >= start && (*i)->end() < end)) {