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 (!set_id (node)) {
423 warning << _("XML node for Location has no ID information") << endmsg;
426 if ((prop = node.property ("name")) == 0) {
427 error << _("XML node for Location has no name information") << endmsg;
431 set_name (prop->value());
433 if ((prop = node.property ("start")) == 0) {
434 error << _("XML node for Location has no start information") << endmsg;
438 /* can't use set_start() here, because _end
439 may make the value of _start illegal.
442 sscanf (prop->value().c_str(), "%" PRId64, &_start);
444 if ((prop = node.property ("end")) == 0) {
445 error << _("XML node for Location has no end information") << endmsg;
449 sscanf (prop->value().c_str(), "%" PRId64, &_end);
451 if ((prop = node.property ("flags")) == 0) {
452 error << _("XML node for Location has no flags information") << endmsg;
456 _flags = Flags (string_2_enum (prop->value(), _flags));
458 if ((prop = node.property ("locked")) != 0) {
459 _locked = string_is_affirmative (prop->value());
464 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
468 if (cd_node->name() != "CD-Info") {
472 if ((prop = cd_node->property ("name")) != 0) {
473 cd_name = prop->value();
475 throw failed_constructor ();
478 if ((prop = cd_node->property ("value")) != 0) {
479 cd_value = prop->value();
481 throw failed_constructor ();
485 cd_info[cd_name] = cd_value;
488 if ((prop = node.property ("position-lock-style")) != 0) {
489 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
492 recompute_bbt_from_frames ();
494 changed (this); /* EMIT SIGNAL */
496 assert (_start >= 0);
503 Location::set_position_lock_style (PositionLockStyle ps)
505 if (_position_lock_style == ps) {
509 _position_lock_style = ps;
511 recompute_bbt_from_frames ();
513 PositionLockStyleChanged (this); /* EMIT SIGNAL */
517 Location::recompute_bbt_from_frames ()
519 if (_position_lock_style != MusicTime) {
523 _session.bbt_time (_start, _bbt_start);
524 _session.bbt_time (_end, _bbt_end);
528 Location::recompute_frames_from_bbt ()
530 if (_position_lock_style != MusicTime) {
534 TempoMap& map (_session.tempo_map());
535 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
552 /*---------------------------------------------------------------------- */
554 Locations::Locations (Session& s)
555 : SessionHandleRef (s)
557 current_location = 0;
560 Locations::~Locations ()
562 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
563 LocationList::iterator tmp = i;
571 Locations::set_current (Location *loc, bool want_lock)
576 Glib::Mutex::Lock lm (lock);
577 ret = set_current_unlocked (loc);
579 ret = set_current_unlocked (loc);
583 current_changed (current_location); /* EMIT SIGNAL */
589 Locations::next_available_name(string& result,string base)
591 LocationList::iterator i;
597 bool available[SUFFIX_MAX+1];
600 for (int k=1; k<SUFFIX_MAX; k++) {
604 for (i = locations.begin(); i != locations.end(); ++i) {
606 temp = location->name();
607 if (l && !temp.find(base,0)) {
608 suffix = atoi(temp.substr(l,3).c_str());
609 if (suffix) available[suffix] = false;
612 for (int k=1; k<=SUFFIX_MAX; k++) {
614 snprintf (buf, 31, "%d", k);
623 Locations::set_current_unlocked (Location *loc)
625 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
626 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
630 current_location = loc;
638 Glib::Mutex::Lock lm (lock);
640 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
642 LocationList::iterator tmp = i;
645 if (!(*i)->is_session_range()) {
652 current_location = 0;
655 changed (OTHER); /* EMIT SIGNAL */
656 current_changed (0); /* EMIT SIGNAL */
660 Locations::clear_markers ()
663 Glib::Mutex::Lock lm (lock);
664 LocationList::iterator tmp;
666 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
670 if ((*i)->is_mark() && !(*i)->is_session_range()) {
678 changed (OTHER); /* EMIT SIGNAL */
682 Locations::clear_ranges ()
685 Glib::Mutex::Lock lm (lock);
686 LocationList::iterator tmp;
688 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
693 if (!(*i)->is_mark()) {
701 current_location = 0;
704 changed (OTHER); /* EMIT SIGNAL */
705 current_changed (0); /* EMIT SIGNAL */
709 Locations::add (Location *loc, bool make_current)
714 Glib::Mutex::Lock lm (lock);
715 locations.push_back (loc);
718 current_location = loc;
722 added (loc); /* EMIT SIGNAL */
725 current_changed (current_location); /* EMIT SIGNAL */
728 if (loc->is_session_range()) {
729 Session::StartTimeChanged (0);
730 Session::EndTimeChanged (1);
735 Locations::remove (Location *loc)
737 bool was_removed = false;
738 bool was_current = false;
739 LocationList::iterator i;
741 if (loc->is_session_range()) {
746 Glib::Mutex::Lock lm (lock);
748 for (i = locations.begin(); i != locations.end(); ++i) {
752 if (current_location == loc) {
753 current_location = 0;
763 removed (loc); /* EMIT SIGNAL */
766 current_changed (0); /* EMIT SIGNAL */
769 changed (REMOVAL); /* EMIT_SIGNAL */
774 Locations::location_changed (Location* /*loc*/)
776 changed (OTHER); /* EMIT SIGNAL */
780 Locations::get_state ()
782 XMLNode *node = new XMLNode ("Locations");
783 LocationList::iterator iter;
784 Glib::Mutex::Lock lm (lock);
786 for (iter = locations.begin(); iter != locations.end(); ++iter) {
787 node->add_child_nocopy ((*iter)->get_state ());
794 Locations::set_state (const XMLNode& node, int version)
796 if (node.name() != "Locations") {
797 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
801 XMLNodeList nlist = node.children();
803 /* build up a new locations list in here */
804 LocationList new_locations;
806 current_location = 0;
808 Location* session_range_location = 0;
809 if (version < 3000) {
810 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
811 new_locations.push_back (session_range_location);
815 Glib::Mutex::Lock lm (lock);
817 XMLNodeConstIterator niter;
818 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
822 XMLProperty const * prop_id = (*niter)->property ("id");
824 PBD::ID id (prop_id->value ());
826 LocationList::const_iterator i = locations.begin();
827 while (i != locations.end () && (*i)->id() != id) {
832 if (i != locations.end()) {
833 /* we can re-use an old Location object */
835 loc->set_state (**niter, version);
837 loc = new Location (_session, **niter);
842 if (version < 3000) {
843 /* look for old-style IsStart / IsEnd properties in this location;
844 if they are present, update the session_range_location accordingly
846 XMLProperty const * prop = (*niter)->property ("flags");
848 string v = prop->value ();
850 string::size_type const c = v.find_first_of (',');
851 string const s = v.substr (0, c);
852 if (s == X_("IsStart")) {
853 session_range_location->set_start (loc->start(), true);
855 } else if (s == X_("IsEnd")) {
856 session_range_location->set_end (loc->start(), true);
860 if (c == string::npos) {
864 v = v.substr (c + 1);
870 new_locations.push_back (loc);
874 catch (failed_constructor& err) {
875 error << _("could not load location from session file - ignored") << endmsg;
879 locations = new_locations;
881 if (locations.size()) {
882 current_location = locations.front();
884 current_location = 0;
888 changed (OTHER); /* EMIT SIGNAL */
893 struct LocationStartEarlierComparison
895 bool operator() (Location *a, Location *b) {
896 return a->start() < b->start();
900 struct LocationStartLaterComparison
902 bool operator() (Location *a, Location *b) {
903 return a->start() > b->start();
908 Locations::first_location_before (framepos_t frame, bool include_special_ranges)
913 Glib::Mutex::Lock lm (lock);
917 LocationStartLaterComparison cmp;
920 /* locs is now sorted latest..earliest */
922 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
923 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
926 if (!(*i)->is_hidden() && (*i)->start() < frame) {
935 Locations::first_location_after (framepos_t frame, bool include_special_ranges)
940 Glib::Mutex::Lock lm (lock);
944 LocationStartEarlierComparison cmp;
947 /* locs is now sorted earliest..latest */
949 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
950 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
953 if (!(*i)->is_hidden() && (*i)->start() > frame) {
961 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
962 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
964 * @param frame Frame to look for.
965 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
966 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
969 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
971 before = after = max_framepos;
976 Glib::Mutex::Lock lm (lock);
980 /* Get a list of positions; don't store any that are exactly on our requested position */
982 std::list<framepos_t> positions;
984 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
985 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
989 if (!(*i)->is_hidden()) {
990 if ((*i)->is_mark ()) {
991 if ((*i)->start() != frame) {
992 positions.push_back ((*i)->start ());
995 if ((*i)->start() != frame) {
996 positions.push_back ((*i)->start ());
998 if ((*i)->end() != frame) {
999 positions.push_back ((*i)->end ());
1005 if (positions.empty ()) {
1011 std::list<framepos_t>::iterator i = positions.begin ();
1012 while (i != positions.end () && *i < frame) {
1016 if (i == positions.end ()) {
1017 /* run out of marks */
1018 before = positions.back ();
1024 if (i == positions.begin ()) {
1034 Locations::session_range_location () const
1036 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1037 if ((*i)->is_session_range()) {
1038 return const_cast<Location*> (*i);
1045 Locations::auto_loop_location () const
1047 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1048 if ((*i)->is_auto_loop()) {
1049 return const_cast<Location*> (*i);
1056 Locations::auto_punch_location () const
1058 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1059 if ((*i)->is_auto_punch()) {
1060 return const_cast<Location*> (*i);
1067 Locations::num_range_markers () const
1070 Glib::Mutex::Lock lm (lock);
1071 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1072 if ((*i)->is_range_marker()) {
1080 Locations::get_location_by_id(PBD::ID id)
1082 LocationList::iterator it;
1083 for (it = locations.begin(); it != locations.end(); ++it)
1084 if (id == (*it)->id())
1091 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1093 Glib::Mutex::Lock lm (lock);
1095 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1096 if ((flags == 0 || (*i)->matches (flags)) &&
1097 ((*i)->start() >= start && (*i)->end() < end)) {