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 (this == &other) {
116 _start = other._start;
117 _bbt_start = other._bbt_start;
119 _bbt_end = other._bbt_end;
120 _flags = other._flags;
121 _position_lock_style = other._position_lock_style;
123 /* copy is not locked even if original was */
127 /* "changed" not emitted on purpose */
129 assert (_start >= 0);
135 /** Set start position.
136 * @param s New start.
137 * @param force true to force setting, even if the given new start is after the current end.
138 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
141 Location::set_start (framepos_t s, bool force, bool allow_bbt_recompute)
148 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
157 if (allow_bbt_recompute) {
158 recompute_bbt_from_frames ();
161 start_changed (this); /* EMIT SIGNAL */
162 end_changed (this); /* EMIT SIGNAL */
165 assert (_start >= 0);
173 framepos_t const old = _start;
176 if (allow_bbt_recompute) {
177 recompute_bbt_from_frames ();
179 start_changed (this); /* EMIT SIGNAL */
180 if (is_session_range ()) {
181 Session::StartTimeChanged (old); /* EMIT SIGNAL */
182 AudioFileSource::set_header_position_offset (s);
186 assert (_start >= 0);
191 /** Set end position.
193 * @param force true to force setting, even if the given new start is after the current end.
194 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
197 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
204 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
213 if (allow_bbt_recompute) {
214 recompute_bbt_from_frames ();
216 start_changed (this); /* EMIT SIGNAL */
217 end_changed (this); /* EMIT SIGNAL */
220 assert (_start >= 0);
227 framepos_t const old = _end;
230 if (allow_bbt_recompute) {
231 recompute_bbt_from_frames ();
233 end_changed(this); /* EMIT SIGNAL */
235 if (is_session_range()) {
236 Session::EndTimeChanged (old); /* EMIT SIGNAL */
246 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
249 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
253 /* now we know these values are ok, so force-set them */
254 int const s = set_start (start, true, allow_bbt_recompute);
255 int const e = set_end (end, true, allow_bbt_recompute);
257 return (s == 0 && e == 0) ? 0 : -1;
261 Location::move_to (framepos_t pos)
269 _end = _start + length();
270 recompute_bbt_from_frames ();
272 changed (this); /* EMIT SIGNAL */
275 assert (_start >= 0);
282 Location::set_hidden (bool yn, void *src)
284 if (set_flag_internal (yn, IsHidden)) {
285 FlagsChanged (this, src); /* EMIT SIGNAL */
290 Location::set_cd (bool yn, void *src)
292 // XXX this really needs to be session start
293 // but its not available here - leave to GUI
296 error << _("You cannot put a CD marker at this position") << endmsg;
300 if (set_flag_internal (yn, IsCDMarker)) {
301 FlagsChanged (this, src); /* EMIT SIGNAL */
306 Location::set_is_range_marker (bool yn, void *src)
308 if (set_flag_internal (yn, IsRangeMarker)) {
309 FlagsChanged (this, src); /* EMIT SIGNAL */
314 Location::set_auto_punch (bool yn, void *src)
316 if (is_mark() || _start == _end) {
320 if (set_flag_internal (yn, IsAutoPunch)) {
321 FlagsChanged (this, src); /* EMIT SIGNAL */
326 Location::set_auto_loop (bool yn, void *src)
328 if (is_mark() || _start == _end) {
332 if (set_flag_internal (yn, IsAutoLoop)) {
333 FlagsChanged (this, src); /* EMIT SIGNAL */
338 Location::set_flag_internal (bool yn, Flags flag)
341 if (!(_flags & flag)) {
342 _flags = Flags (_flags | flag);
347 _flags = Flags (_flags & ~flag);
355 Location::set_mark (bool yn)
357 /* This function is private, and so does not emit signals */
359 if (_start != _end) {
363 set_flag_internal (yn, IsMark);
368 Location::cd_info_node(const string & name, const string & value)
370 XMLNode* root = new XMLNode("CD-Info");
372 root->add_property("name", name);
373 root->add_property("value", value);
380 Location::get_state ()
382 XMLNode *node = new XMLNode ("Location");
385 typedef map<string, string>::const_iterator CI;
387 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
388 node->add_child_nocopy(cd_info_node(m->first, m->second));
391 id().print (buf, sizeof (buf));
392 node->add_property("id", buf);
393 node->add_property ("name", name());
394 snprintf (buf, sizeof (buf), "%" PRId64, start());
395 node->add_property ("start", buf);
396 snprintf (buf, sizeof (buf), "%" PRId64, end());
397 node->add_property ("end", buf);
398 node->add_property ("flags", enum_2_string (_flags));
399 node->add_property ("locked", (_locked ? "yes" : "no"));
400 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
406 Location::set_state (const XMLNode& node, int /*version*/)
408 const XMLProperty *prop;
410 XMLNodeList cd_list = node.children();
411 XMLNodeConstIterator cd_iter;
417 if (node.name() != "Location") {
418 error << _("incorrect XML node passed to Location::set_state") << endmsg;
422 if ((prop = node.property ("id")) == 0) {
423 warning << _("XML node for Location has no ID information") << endmsg;
425 _id = prop->value ();
428 if ((prop = node.property ("name")) == 0) {
429 error << _("XML node for Location has no name information") << endmsg;
433 set_name (prop->value());
435 if ((prop = node.property ("start")) == 0) {
436 error << _("XML node for Location has no start information") << endmsg;
440 /* can't use set_start() here, because _end
441 may make the value of _start illegal.
444 sscanf (prop->value().c_str(), "%" PRId64, &_start);
446 if ((prop = node.property ("end")) == 0) {
447 error << _("XML node for Location has no end information") << endmsg;
451 sscanf (prop->value().c_str(), "%" PRId64, &_end);
453 if ((prop = node.property ("flags")) == 0) {
454 error << _("XML node for Location has no flags information") << endmsg;
458 _flags = Flags (string_2_enum (prop->value(), _flags));
460 if ((prop = node.property ("locked")) != 0) {
461 _locked = string_is_affirmative (prop->value());
466 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
470 if (cd_node->name() != "CD-Info") {
474 if ((prop = cd_node->property ("name")) != 0) {
475 cd_name = prop->value();
477 throw failed_constructor ();
480 if ((prop = cd_node->property ("value")) != 0) {
481 cd_value = prop->value();
483 throw failed_constructor ();
487 cd_info[cd_name] = cd_value;
490 if ((prop = node.property ("position-lock-style")) != 0) {
491 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
494 recompute_bbt_from_frames ();
496 changed (this); /* EMIT SIGNAL */
498 assert (_start >= 0);
505 Location::set_position_lock_style (PositionLockStyle ps)
507 if (_position_lock_style == ps) {
511 _position_lock_style = ps;
513 recompute_bbt_from_frames ();
515 PositionLockStyleChanged (this); /* EMIT SIGNAL */
519 Location::recompute_bbt_from_frames ()
521 if (_position_lock_style != MusicTime) {
525 _session.tempo_map().bbt_time (_start, _bbt_start);
526 _session.tempo_map().bbt_time (_end, _bbt_end);
530 Location::recompute_frames_from_bbt ()
532 if (_position_lock_style != MusicTime) {
536 TempoMap& map (_session.tempo_map());
537 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
554 /*---------------------------------------------------------------------- */
556 Locations::Locations (Session& s)
557 : SessionHandleRef (s)
559 current_location = 0;
562 Locations::~Locations ()
564 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
565 LocationList::iterator tmp = i;
573 Locations::set_current (Location *loc, bool want_lock)
578 Glib::Mutex::Lock lm (lock);
579 ret = set_current_unlocked (loc);
581 ret = set_current_unlocked (loc);
585 current_changed (current_location); /* EMIT SIGNAL */
591 Locations::next_available_name(string& result,string base)
593 LocationList::iterator i;
599 bool available[SUFFIX_MAX+1];
602 for (int k=1; k<SUFFIX_MAX; k++) {
606 for (i = locations.begin(); i != locations.end(); ++i) {
608 temp = location->name();
609 if (l && !temp.find(base,0)) {
610 suffix = atoi(temp.substr(l,3).c_str());
611 if (suffix) available[suffix] = false;
614 for (int k=1; k<=SUFFIX_MAX; k++) {
616 snprintf (buf, 31, "%d", k);
625 Locations::set_current_unlocked (Location *loc)
627 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
628 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
632 current_location = loc;
640 Glib::Mutex::Lock lm (lock);
642 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
644 LocationList::iterator tmp = i;
647 if (!(*i)->is_session_range()) {
654 current_location = 0;
657 changed (OTHER); /* EMIT SIGNAL */
658 current_changed (0); /* EMIT SIGNAL */
662 Locations::clear_markers ()
665 Glib::Mutex::Lock lm (lock);
666 LocationList::iterator tmp;
668 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
672 if ((*i)->is_mark() && !(*i)->is_session_range()) {
680 changed (OTHER); /* EMIT SIGNAL */
684 Locations::clear_ranges ()
687 Glib::Mutex::Lock lm (lock);
688 LocationList::iterator tmp;
690 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
695 if (!(*i)->is_mark()) {
703 current_location = 0;
706 changed (OTHER); /* EMIT SIGNAL */
707 current_changed (0); /* EMIT SIGNAL */
711 Locations::add (Location *loc, bool make_current)
716 Glib::Mutex::Lock lm (lock);
717 locations.push_back (loc);
720 current_location = loc;
724 added (loc); /* EMIT SIGNAL */
727 current_changed (current_location); /* EMIT SIGNAL */
730 if (loc->is_session_range()) {
731 Session::StartTimeChanged (0);
732 Session::EndTimeChanged (1);
737 Locations::remove (Location *loc)
739 bool was_removed = false;
740 bool was_current = false;
741 LocationList::iterator i;
743 if (loc->is_session_range()) {
748 Glib::Mutex::Lock lm (lock);
750 for (i = locations.begin(); i != locations.end(); ++i) {
754 if (current_location == loc) {
755 current_location = 0;
765 removed (loc); /* EMIT SIGNAL */
768 current_changed (0); /* EMIT SIGNAL */
771 changed (REMOVAL); /* EMIT_SIGNAL */
776 Locations::location_changed (Location* /*loc*/)
778 changed (OTHER); /* EMIT SIGNAL */
782 Locations::get_state ()
784 XMLNode *node = new XMLNode ("Locations");
785 LocationList::iterator iter;
786 Glib::Mutex::Lock lm (lock);
788 for (iter = locations.begin(); iter != locations.end(); ++iter) {
789 node->add_child_nocopy ((*iter)->get_state ());
796 Locations::set_state (const XMLNode& node, int version)
798 if (node.name() != "Locations") {
799 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
803 XMLNodeList nlist = node.children();
805 /* build up a new locations list in here */
806 LocationList new_locations;
808 current_location = 0;
810 Location* session_range_location = 0;
811 if (version < 3000) {
812 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
813 new_locations.push_back (session_range_location);
817 Glib::Mutex::Lock lm (lock);
819 XMLNodeConstIterator niter;
820 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
824 XMLProperty const * prop_id = (*niter)->property ("id");
826 PBD::ID id (prop_id->value ());
828 LocationList::const_iterator i = locations.begin();
829 while (i != locations.end () && (*i)->id() != id) {
834 if (i != locations.end()) {
835 /* we can re-use an old Location object */
837 loc->set_state (**niter, version);
839 loc = new Location (_session, **niter);
844 if (version < 3000) {
845 /* look for old-style IsStart / IsEnd properties in this location;
846 if they are present, update the session_range_location accordingly
848 XMLProperty const * prop = (*niter)->property ("flags");
850 string v = prop->value ();
852 string::size_type const c = v.find_first_of (',');
853 string const s = v.substr (0, c);
854 if (s == X_("IsStart")) {
855 session_range_location->set_start (loc->start(), true);
857 } else if (s == X_("IsEnd")) {
858 session_range_location->set_end (loc->start(), true);
862 if (c == string::npos) {
866 v = v.substr (c + 1);
872 new_locations.push_back (loc);
876 catch (failed_constructor& err) {
877 error << _("could not load location from session file - ignored") << endmsg;
881 locations = new_locations;
883 if (locations.size()) {
884 current_location = locations.front();
886 current_location = 0;
890 changed (OTHER); /* EMIT SIGNAL */
895 struct LocationStartEarlierComparison
897 bool operator() (Location *a, Location *b) {
898 return a->start() < b->start();
902 struct LocationStartLaterComparison
904 bool operator() (Location *a, Location *b) {
905 return a->start() > b->start();
910 Locations::first_location_before (framepos_t frame, bool include_special_ranges)
915 Glib::Mutex::Lock lm (lock);
919 LocationStartLaterComparison cmp;
922 /* locs is now sorted latest..earliest */
924 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
925 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
928 if (!(*i)->is_hidden() && (*i)->start() < frame) {
937 Locations::first_location_after (framepos_t frame, bool include_special_ranges)
942 Glib::Mutex::Lock lm (lock);
946 LocationStartEarlierComparison cmp;
949 /* locs is now sorted earliest..latest */
951 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
952 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
955 if (!(*i)->is_hidden() && (*i)->start() > frame) {
963 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
964 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
966 * @param frame Frame to look for.
967 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
968 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
971 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
973 before = after = max_framepos;
978 Glib::Mutex::Lock lm (lock);
982 /* Get a list of positions; don't store any that are exactly on our requested position */
984 std::list<framepos_t> positions;
986 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
987 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
991 if (!(*i)->is_hidden()) {
992 if ((*i)->is_mark ()) {
993 if ((*i)->start() != frame) {
994 positions.push_back ((*i)->start ());
997 if ((*i)->start() != frame) {
998 positions.push_back ((*i)->start ());
1000 if ((*i)->end() != frame) {
1001 positions.push_back ((*i)->end ());
1007 if (positions.empty ()) {
1013 std::list<framepos_t>::iterator i = positions.begin ();
1014 while (i != positions.end () && *i < frame) {
1018 if (i == positions.end ()) {
1019 /* run out of marks */
1020 before = positions.back ();
1026 if (i == positions.begin ()) {
1036 Locations::session_range_location () const
1038 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1039 if ((*i)->is_session_range()) {
1040 return const_cast<Location*> (*i);
1047 Locations::auto_loop_location () const
1049 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1050 if ((*i)->is_auto_loop()) {
1051 return const_cast<Location*> (*i);
1058 Locations::auto_punch_location () const
1060 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1061 if ((*i)->is_auto_punch()) {
1062 return const_cast<Location*> (*i);
1069 Locations::num_range_markers () const
1072 Glib::Mutex::Lock lm (lock);
1073 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1074 if ((*i)->is_range_marker()) {
1082 Locations::get_location_by_id(PBD::ID id)
1084 LocationList::iterator it;
1085 for (it = locations.begin(); it != locations.end(); ++it)
1086 if (id == (*it)->id())
1093 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1095 Glib::Mutex::Lock lm (lock);
1097 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1098 if ((flags == 0 || (*i)->matches (flags)) &&
1099 ((*i)->start() >= start && (*i)->end() < end)) {