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)
56 Location::Location (Session& s, nframes64_t sample_start, nframes64_t sample_end, const std::string &name, Flags bits)
57 : SessionHandleRef (s)
59 , _start (sample_start)
63 , _position_lock_style (AudioTime)
65 recompute_bbt_from_frames ();
68 Location::Location (const Location& other)
69 : SessionHandleRef (other._session)
70 , StatefulDestructible()
72 , _start (other._start)
73 , _bbt_start (other._bbt_start)
75 , _bbt_end (other._bbt_end)
76 , _flags (other._flags)
77 , _position_lock_style (other._position_lock_style)
79 /* copy is not locked even if original was */
84 Location::Location (Session& s, const XMLNode& node)
85 : SessionHandleRef (s)
87 if (set_state (node, Stateful::loading_state_version)) {
88 throw failed_constructor ();
93 Location::operator= (const Location& other)
100 _start = other._start;
101 _bbt_start = other._bbt_start;
103 _bbt_end = other._bbt_end;
104 _flags = other._flags;
105 _position_lock_style = other._position_lock_style;
107 /* copy is not locked even if original was */
111 /* "changed" not emitted on purpose */
116 /** Set start position.
117 * @param s New start.
118 * @param force true to force setting, even if the given new start is after the current end.
119 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
122 Location::set_start (nframes64_t s, bool force, bool allow_bbt_recompute)
129 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
138 if (allow_bbt_recompute) {
139 recompute_bbt_from_frames ();
141 start_changed (this); /* EMIT SIGNAL */
142 end_changed (this); /* EMIT SIGNAL */
149 if (allow_bbt_recompute) {
150 recompute_bbt_from_frames ();
152 start_changed (this); /* EMIT SIGNAL */
153 if (is_session_range ()) {
154 Session::StartTimeChanged (); /* EMIT SIGNAL */
155 AudioFileSource::set_header_position_offset (s);
162 /** Set end position.
164 * @param force true to force setting, even if the given new start is after the current end.
165 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
168 Location::set_end (nframes64_t e, bool force, bool allow_bbt_recompute)
175 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
184 if (allow_bbt_recompute) {
185 recompute_bbt_from_frames ();
187 start_changed (this); /* EMIT SIGNAL */
188 end_changed (this); /* EMIT SIGNAL */
195 if (allow_bbt_recompute) {
196 recompute_bbt_from_frames ();
198 end_changed(this); /* EMIT SIGNAL */
200 if (is_session_range()) {
201 Session::EndTimeChanged (); /* EMIT SIGNAL */
209 Location::set (nframes64_t start, nframes64_t end, bool allow_bbt_recompute)
212 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
216 /* now we know these values are ok, so force-set them */
217 int const s = set_start (start, true, allow_bbt_recompute);
218 int const e = set_end (end, true, allow_bbt_recompute);
220 return (s == 0 && e == 0) ? 0 : -1;
224 Location::move_to (nframes64_t pos)
232 _end = _start + length();
233 recompute_bbt_from_frames ();
235 changed (this); /* EMIT SIGNAL */
242 Location::set_hidden (bool yn, void *src)
244 if (set_flag_internal (yn, IsHidden)) {
245 FlagsChanged (this, src); /* EMIT SIGNAL */
250 Location::set_cd (bool yn, void *src)
252 // XXX this really needs to be session start
253 // but its not available here - leave to GUI
256 error << _("You cannot put a CD marker at this position") << endmsg;
260 if (set_flag_internal (yn, IsCDMarker)) {
261 FlagsChanged (this, src); /* EMIT SIGNAL */
266 Location::set_is_range_marker (bool yn, void *src)
268 if (set_flag_internal (yn, IsRangeMarker)) {
269 FlagsChanged (this, src); /* EMIT SIGNAL */
274 Location::set_auto_punch (bool yn, void *src)
276 if (is_mark() || _start == _end) {
280 if (set_flag_internal (yn, IsAutoPunch)) {
281 FlagsChanged (this, src); /* EMIT SIGNAL */
286 Location::set_auto_loop (bool yn, void *src)
288 if (is_mark() || _start == _end) {
292 if (set_flag_internal (yn, IsAutoLoop)) {
293 FlagsChanged (this, src); /* EMIT SIGNAL */
298 Location::set_flag_internal (bool yn, Flags flag)
301 if (!(_flags & flag)) {
302 _flags = Flags (_flags | flag);
307 _flags = Flags (_flags & ~flag);
315 Location::set_mark (bool yn)
317 /* This function is private, and so does not emit signals */
319 if (_start != _end) {
323 set_flag_internal (yn, IsMark);
328 Location::cd_info_node(const string & name, const string & value)
330 XMLNode* root = new XMLNode("CD-Info");
332 root->add_property("name", name);
333 root->add_property("value", value);
340 Location::get_state ()
342 XMLNode *node = new XMLNode ("Location");
345 typedef map<string, string>::const_iterator CI;
347 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
348 node->add_child_nocopy(cd_info_node(m->first, m->second));
351 id().print (buf, sizeof (buf));
352 node->add_property("id", buf);
353 node->add_property ("name", name());
354 snprintf (buf, sizeof (buf), "%" PRId64, start());
355 node->add_property ("start", buf);
356 snprintf (buf, sizeof (buf), "%" PRId64, end());
357 node->add_property ("end", buf);
358 node->add_property ("flags", enum_2_string (_flags));
359 node->add_property ("locked", (_locked ? "yes" : "no"));
360 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
366 Location::set_state (const XMLNode& node, int /*version*/)
368 const XMLProperty *prop;
370 XMLNodeList cd_list = node.children();
371 XMLNodeConstIterator cd_iter;
377 if (node.name() != "Location") {
378 error << _("incorrect XML node passed to Location::set_state") << endmsg;
382 if ((prop = node.property ("id")) == 0) {
383 warning << _("XML node for Location has no ID information") << endmsg;
385 _id = prop->value ();
388 if ((prop = node.property ("name")) == 0) {
389 error << _("XML node for Location has no name information") << endmsg;
393 set_name (prop->value());
395 if ((prop = node.property ("start")) == 0) {
396 error << _("XML node for Location has no start information") << endmsg;
400 /* can't use set_start() here, because _end
401 may make the value of _start illegal.
404 sscanf (prop->value().c_str(), "%" PRId64, &_start);
406 if ((prop = node.property ("end")) == 0) {
407 error << _("XML node for Location has no end information") << endmsg;
411 sscanf (prop->value().c_str(), "%" PRId64, &_end);
413 if ((prop = node.property ("flags")) == 0) {
414 error << _("XML node for Location has no flags information") << endmsg;
418 _flags = Flags (string_2_enum (prop->value(), _flags));
420 if ((prop = node.property ("locked")) != 0) {
421 _locked = string_is_affirmative (prop->value());
426 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
430 if (cd_node->name() != "CD-Info") {
434 if ((prop = cd_node->property ("name")) != 0) {
435 cd_name = prop->value();
437 throw failed_constructor ();
440 if ((prop = cd_node->property ("value")) != 0) {
441 cd_value = prop->value();
443 throw failed_constructor ();
447 cd_info[cd_name] = cd_value;
450 if ((prop = node.property ("position-lock-style")) != 0) {
451 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
454 recompute_bbt_from_frames ();
456 changed (this); /* EMIT SIGNAL */
462 Location::set_position_lock_style (PositionLockStyle ps)
464 if (_position_lock_style == ps) {
468 _position_lock_style = ps;
470 recompute_bbt_from_frames ();
472 PositionLockStyleChanged (this); /* EMIT SIGNAL */
476 Location::recompute_bbt_from_frames ()
478 if (_position_lock_style != MusicTime) {
482 _session.tempo_map().bbt_time (_start, _bbt_start);
483 _session.tempo_map().bbt_time (_end, _bbt_end);
487 Location::recompute_frames_from_bbt ()
489 if (_position_lock_style != MusicTime) {
493 TempoMap& map (_session.tempo_map());
494 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
511 /*---------------------------------------------------------------------- */
513 Locations::Locations (Session& s)
514 : SessionHandleRef (s)
516 current_location = 0;
519 Locations::~Locations ()
521 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
522 LocationList::iterator tmp = i;
530 Locations::set_current (Location *loc, bool want_lock)
535 Glib::Mutex::Lock lm (lock);
536 ret = set_current_unlocked (loc);
538 ret = set_current_unlocked (loc);
542 current_changed (current_location); /* EMIT SIGNAL */
548 Locations::next_available_name(string& result,string base)
550 LocationList::iterator i;
556 bool available[SUFFIX_MAX+1];
559 for (int k=1; k<SUFFIX_MAX; k++) {
563 for (i = locations.begin(); i != locations.end(); ++i) {
565 temp = location->name();
566 if (l && !temp.find(base,0)) {
567 suffix = atoi(temp.substr(l,3).c_str());
568 if (suffix) available[suffix] = false;
571 for (int k=1; k<=SUFFIX_MAX; k++) {
573 snprintf (buf, 31, "%d", k);
582 Locations::set_current_unlocked (Location *loc)
584 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
585 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
589 current_location = loc;
597 Glib::Mutex::Lock lm (lock);
599 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
601 LocationList::iterator tmp = i;
604 if (!(*i)->is_session_range()) {
611 current_location = 0;
614 changed (OTHER); /* EMIT SIGNAL */
615 current_changed (0); /* EMIT SIGNAL */
619 Locations::clear_markers ()
622 Glib::Mutex::Lock lm (lock);
623 LocationList::iterator tmp;
625 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
629 if ((*i)->is_mark() && !(*i)->is_session_range()) {
637 changed (OTHER); /* EMIT SIGNAL */
641 Locations::clear_ranges ()
644 Glib::Mutex::Lock lm (lock);
645 LocationList::iterator tmp;
647 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
652 if (!(*i)->is_mark()) {
660 current_location = 0;
663 changed (OTHER); /* EMIT SIGNAL */
664 current_changed (0); /* EMIT SIGNAL */
668 Locations::add (Location *loc, bool make_current)
673 Glib::Mutex::Lock lm (lock);
674 locations.push_back (loc);
677 current_location = loc;
681 added (loc); /* EMIT SIGNAL */
684 current_changed (current_location); /* EMIT SIGNAL */
689 Locations::remove (Location *loc)
691 bool was_removed = false;
692 bool was_current = false;
693 LocationList::iterator i;
695 if (loc->is_session_range()) {
700 Glib::Mutex::Lock lm (lock);
702 for (i = locations.begin(); i != locations.end(); ++i) {
706 if (current_location == loc) {
707 current_location = 0;
717 removed (loc); /* EMIT SIGNAL */
720 current_changed (0); /* EMIT SIGNAL */
723 changed (REMOVAL); /* EMIT_SIGNAL */
728 Locations::location_changed (Location* /*loc*/)
730 changed (OTHER); /* EMIT SIGNAL */
734 Locations::get_state ()
736 XMLNode *node = new XMLNode ("Locations");
737 LocationList::iterator iter;
738 Glib::Mutex::Lock lm (lock);
740 for (iter = locations.begin(); iter != locations.end(); ++iter) {
741 node->add_child_nocopy ((*iter)->get_state ());
748 Locations::set_state (const XMLNode& node, int version)
750 if (node.name() != "Locations") {
751 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
755 XMLNodeList nlist = node.children();
758 current_location = 0;
760 Location* session_range_location = 0;
761 if (version < 3000) {
762 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
763 locations.push_back (session_range_location);
767 Glib::Mutex::Lock lm (lock);
769 XMLNodeConstIterator niter;
770 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
774 Location *loc = new Location (_session, **niter);
778 if (version < 3000) {
779 /* look for old-style IsStart / IsEnd properties in this location;
780 if they are present, update the session_range_location accordingly
782 XMLProperty const * prop = (*niter)->property ("flags");
784 string v = prop->value ();
786 string::size_type const c = v.find_first_of (',');
787 string const s = v.substr (0, c);
788 if (s == X_("IsStart")) {
789 session_range_location->set_start (loc->start());
791 } else if (s == X_("IsEnd")) {
792 session_range_location->set_end (loc->start());
796 if (c == string::npos) {
800 v = v.substr (c + 1);
806 locations.push_back (loc);
810 catch (failed_constructor& err) {
811 error << _("could not load location from session file - ignored") << endmsg;
815 if (locations.size()) {
816 current_location = locations.front();
818 current_location = 0;
822 changed (OTHER); /* EMIT SIGNAL */
827 struct LocationStartEarlierComparison
829 bool operator() (Location *a, Location *b) {
830 return a->start() < b->start();
834 struct LocationStartLaterComparison
836 bool operator() (Location *a, Location *b) {
837 return a->start() > b->start();
842 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
847 Glib::Mutex::Lock lm (lock);
851 LocationStartLaterComparison cmp;
854 /* locs is now sorted latest..earliest */
856 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
857 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
860 if (!(*i)->is_hidden() && (*i)->start() < frame) {
869 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
874 Glib::Mutex::Lock lm (lock);
878 LocationStartEarlierComparison cmp;
881 /* locs is now sorted earliest..latest */
883 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
884 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
887 if (!(*i)->is_hidden() && (*i)->start() > frame) {
895 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
896 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
898 * @param frame Frame to look for.
899 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
900 * @param after Filled in with the position of the next `mark' after `frame' (or max_frames if none exists)
903 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
905 before = after = max_frames;
910 Glib::Mutex::Lock lm (lock);
914 /* Get a list of positions; don't store any that are exactly on our requested position */
916 std::list<nframes64_t> positions;
918 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
919 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
923 if (!(*i)->is_hidden()) {
924 if ((*i)->is_mark ()) {
925 if ((*i)->start() != frame) {
926 positions.push_back ((*i)->start ());
929 if ((*i)->start() != frame) {
930 positions.push_back ((*i)->start ());
932 if ((*i)->end() != frame) {
933 positions.push_back ((*i)->end ());
939 if (positions.empty ()) {
945 std::list<nframes64_t>::iterator i = positions.begin ();
946 while (i != positions.end () && *i < frame) {
950 if (i == positions.end ()) {
951 /* run out of marks */
952 before = positions.back ();
958 if (i == positions.begin ()) {
968 Locations::session_range_location () const
970 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
971 if ((*i)->is_session_range()) {
972 return const_cast<Location*> (*i);
979 Locations::auto_loop_location () const
981 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
982 if ((*i)->is_auto_loop()) {
983 return const_cast<Location*> (*i);
990 Locations::auto_punch_location () const
992 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
993 if ((*i)->is_auto_punch()) {
994 return const_cast<Location*> (*i);
1001 Locations::num_range_markers () const
1004 Glib::Mutex::Lock lm (lock);
1005 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1006 if ((*i)->is_range_marker()) {
1014 Locations::get_location_by_id(PBD::ID id)
1016 LocationList::iterator it;
1017 for (it = locations.begin(); it != locations.end(); ++it)
1018 if (id == (*it)->id())
1025 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
1027 Glib::Mutex::Lock lm (lock);
1029 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1030 if ((flags == 0 || (*i)->matches (flags)) &&
1031 ((*i)->start() >= start && (*i)->end() < end)) {