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)
86 , _position_lock_style (AudioTime)
88 /* Note: _position_lock_style is initialised above in case set_state doesn't set it
89 (for 2.X session file compatibility).
92 if (set_state (node, Stateful::loading_state_version)) {
93 throw failed_constructor ();
98 Location::operator= (const Location& other)
100 if (this == &other) {
105 _start = other._start;
106 _bbt_start = other._bbt_start;
108 _bbt_end = other._bbt_end;
109 _flags = other._flags;
110 _position_lock_style = other._position_lock_style;
112 /* copy is not locked even if original was */
116 /* "changed" not emitted on purpose */
121 /** Set start position.
122 * @param s New start.
123 * @param force true to force setting, even if the given new start is after the current end.
124 * @param allow_bbt_recompute True to recompute BBT start time from the new given start time.
127 Location::set_start (nframes64_t s, bool force, bool allow_bbt_recompute)
134 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
143 if (allow_bbt_recompute) {
144 recompute_bbt_from_frames ();
146 start_changed (this); /* EMIT SIGNAL */
147 end_changed (this); /* EMIT SIGNAL */
154 if (allow_bbt_recompute) {
155 recompute_bbt_from_frames ();
157 start_changed (this); /* EMIT SIGNAL */
158 if (is_session_range ()) {
159 Session::StartTimeChanged (); /* EMIT SIGNAL */
160 AudioFileSource::set_header_position_offset (s);
167 /** Set end position.
169 * @param force true to force setting, even if the given new start is after the current end.
170 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
173 Location::set_end (nframes64_t e, bool force, bool allow_bbt_recompute)
180 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
189 if (allow_bbt_recompute) {
190 recompute_bbt_from_frames ();
192 start_changed (this); /* EMIT SIGNAL */
193 end_changed (this); /* EMIT SIGNAL */
200 if (allow_bbt_recompute) {
201 recompute_bbt_from_frames ();
203 end_changed(this); /* EMIT SIGNAL */
205 if (is_session_range()) {
206 Session::EndTimeChanged (); /* EMIT SIGNAL */
214 Location::set (nframes64_t start, nframes64_t end, bool allow_bbt_recompute)
217 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
221 /* now we know these values are ok, so force-set them */
222 int const s = set_start (start, true, allow_bbt_recompute);
223 int const e = set_end (end, true, allow_bbt_recompute);
225 return (s == 0 && e == 0) ? 0 : -1;
229 Location::move_to (nframes64_t pos)
237 _end = _start + length();
238 recompute_bbt_from_frames ();
240 changed (this); /* EMIT SIGNAL */
247 Location::set_hidden (bool yn, void *src)
249 if (set_flag_internal (yn, IsHidden)) {
250 FlagsChanged (this, src); /* EMIT SIGNAL */
255 Location::set_cd (bool yn, void *src)
257 // XXX this really needs to be session start
258 // but its not available here - leave to GUI
261 error << _("You cannot put a CD marker at this position") << endmsg;
265 if (set_flag_internal (yn, IsCDMarker)) {
266 FlagsChanged (this, src); /* EMIT SIGNAL */
271 Location::set_is_range_marker (bool yn, void *src)
273 if (set_flag_internal (yn, IsRangeMarker)) {
274 FlagsChanged (this, src); /* EMIT SIGNAL */
279 Location::set_auto_punch (bool yn, void *src)
281 if (is_mark() || _start == _end) {
285 if (set_flag_internal (yn, IsAutoPunch)) {
286 FlagsChanged (this, src); /* EMIT SIGNAL */
291 Location::set_auto_loop (bool yn, void *src)
293 if (is_mark() || _start == _end) {
297 if (set_flag_internal (yn, IsAutoLoop)) {
298 FlagsChanged (this, src); /* EMIT SIGNAL */
303 Location::set_flag_internal (bool yn, Flags flag)
306 if (!(_flags & flag)) {
307 _flags = Flags (_flags | flag);
312 _flags = Flags (_flags & ~flag);
320 Location::set_mark (bool yn)
322 /* This function is private, and so does not emit signals */
324 if (_start != _end) {
328 set_flag_internal (yn, IsMark);
333 Location::cd_info_node(const string & name, const string & value)
335 XMLNode* root = new XMLNode("CD-Info");
337 root->add_property("name", name);
338 root->add_property("value", value);
345 Location::get_state ()
347 XMLNode *node = new XMLNode ("Location");
350 typedef map<string, string>::const_iterator CI;
352 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
353 node->add_child_nocopy(cd_info_node(m->first, m->second));
356 id().print (buf, sizeof (buf));
357 node->add_property("id", buf);
358 node->add_property ("name", name());
359 snprintf (buf, sizeof (buf), "%" PRId64, start());
360 node->add_property ("start", buf);
361 snprintf (buf, sizeof (buf), "%" PRId64, end());
362 node->add_property ("end", buf);
363 node->add_property ("flags", enum_2_string (_flags));
364 node->add_property ("locked", (_locked ? "yes" : "no"));
365 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
371 Location::set_state (const XMLNode& node, int /*version*/)
373 const XMLProperty *prop;
375 XMLNodeList cd_list = node.children();
376 XMLNodeConstIterator cd_iter;
382 if (node.name() != "Location") {
383 error << _("incorrect XML node passed to Location::set_state") << endmsg;
387 if ((prop = node.property ("id")) == 0) {
388 warning << _("XML node for Location has no ID information") << endmsg;
390 _id = prop->value ();
393 if ((prop = node.property ("name")) == 0) {
394 error << _("XML node for Location has no name information") << endmsg;
398 set_name (prop->value());
400 if ((prop = node.property ("start")) == 0) {
401 error << _("XML node for Location has no start information") << endmsg;
405 /* can't use set_start() here, because _end
406 may make the value of _start illegal.
409 sscanf (prop->value().c_str(), "%" PRId64, &_start);
411 if ((prop = node.property ("end")) == 0) {
412 error << _("XML node for Location has no end information") << endmsg;
416 sscanf (prop->value().c_str(), "%" PRId64, &_end);
418 if ((prop = node.property ("flags")) == 0) {
419 error << _("XML node for Location has no flags information") << endmsg;
423 _flags = Flags (string_2_enum (prop->value(), _flags));
425 if ((prop = node.property ("locked")) != 0) {
426 _locked = string_is_affirmative (prop->value());
431 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
435 if (cd_node->name() != "CD-Info") {
439 if ((prop = cd_node->property ("name")) != 0) {
440 cd_name = prop->value();
442 throw failed_constructor ();
445 if ((prop = cd_node->property ("value")) != 0) {
446 cd_value = prop->value();
448 throw failed_constructor ();
452 cd_info[cd_name] = cd_value;
455 if ((prop = node.property ("position-lock-style")) != 0) {
456 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
459 recompute_bbt_from_frames ();
461 changed (this); /* EMIT SIGNAL */
467 Location::set_position_lock_style (PositionLockStyle ps)
469 if (_position_lock_style == ps) {
473 _position_lock_style = ps;
475 recompute_bbt_from_frames ();
477 PositionLockStyleChanged (this); /* EMIT SIGNAL */
481 Location::recompute_bbt_from_frames ()
483 if (_position_lock_style != MusicTime) {
487 _session.tempo_map().bbt_time (_start, _bbt_start);
488 _session.tempo_map().bbt_time (_end, _bbt_end);
492 Location::recompute_frames_from_bbt ()
494 if (_position_lock_style != MusicTime) {
498 TempoMap& map (_session.tempo_map());
499 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
516 /*---------------------------------------------------------------------- */
518 Locations::Locations (Session& s)
519 : SessionHandleRef (s)
521 current_location = 0;
524 Locations::~Locations ()
526 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
527 LocationList::iterator tmp = i;
535 Locations::set_current (Location *loc, bool want_lock)
540 Glib::Mutex::Lock lm (lock);
541 ret = set_current_unlocked (loc);
543 ret = set_current_unlocked (loc);
547 current_changed (current_location); /* EMIT SIGNAL */
553 Locations::next_available_name(string& result,string base)
555 LocationList::iterator i;
561 bool available[SUFFIX_MAX+1];
564 for (int k=1; k<SUFFIX_MAX; k++) {
568 for (i = locations.begin(); i != locations.end(); ++i) {
570 temp = location->name();
571 if (l && !temp.find(base,0)) {
572 suffix = atoi(temp.substr(l,3).c_str());
573 if (suffix) available[suffix] = false;
576 for (int k=1; k<=SUFFIX_MAX; k++) {
578 snprintf (buf, 31, "%d", k);
587 Locations::set_current_unlocked (Location *loc)
589 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
590 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
594 current_location = loc;
602 Glib::Mutex::Lock lm (lock);
604 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
606 LocationList::iterator tmp = i;
609 if (!(*i)->is_session_range()) {
616 current_location = 0;
619 changed (OTHER); /* EMIT SIGNAL */
620 current_changed (0); /* EMIT SIGNAL */
624 Locations::clear_markers ()
627 Glib::Mutex::Lock lm (lock);
628 LocationList::iterator tmp;
630 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
634 if ((*i)->is_mark() && !(*i)->is_session_range()) {
642 changed (OTHER); /* EMIT SIGNAL */
646 Locations::clear_ranges ()
649 Glib::Mutex::Lock lm (lock);
650 LocationList::iterator tmp;
652 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
657 if (!(*i)->is_mark()) {
665 current_location = 0;
668 changed (OTHER); /* EMIT SIGNAL */
669 current_changed (0); /* EMIT SIGNAL */
673 Locations::add (Location *loc, bool make_current)
678 Glib::Mutex::Lock lm (lock);
679 locations.push_back (loc);
682 current_location = loc;
686 added (loc); /* EMIT SIGNAL */
689 current_changed (current_location); /* EMIT SIGNAL */
694 Locations::remove (Location *loc)
696 bool was_removed = false;
697 bool was_current = false;
698 LocationList::iterator i;
700 if (loc->is_session_range()) {
705 Glib::Mutex::Lock lm (lock);
707 for (i = locations.begin(); i != locations.end(); ++i) {
711 if (current_location == loc) {
712 current_location = 0;
722 removed (loc); /* EMIT SIGNAL */
725 current_changed (0); /* EMIT SIGNAL */
728 changed (REMOVAL); /* EMIT_SIGNAL */
733 Locations::location_changed (Location* /*loc*/)
735 changed (OTHER); /* EMIT SIGNAL */
739 Locations::get_state ()
741 XMLNode *node = new XMLNode ("Locations");
742 LocationList::iterator iter;
743 Glib::Mutex::Lock lm (lock);
745 for (iter = locations.begin(); iter != locations.end(); ++iter) {
746 node->add_child_nocopy ((*iter)->get_state ());
753 Locations::set_state (const XMLNode& node, int version)
755 if (node.name() != "Locations") {
756 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
760 XMLNodeList nlist = node.children();
763 current_location = 0;
765 Location* session_range_location = 0;
766 if (version < 3000) {
767 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
768 locations.push_back (session_range_location);
772 Glib::Mutex::Lock lm (lock);
774 XMLNodeConstIterator niter;
775 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
779 Location *loc = new Location (_session, **niter);
783 if (version < 3000) {
784 /* look for old-style IsStart / IsEnd properties in this location;
785 if they are present, update the session_range_location accordingly
787 XMLProperty const * prop = (*niter)->property ("flags");
789 string v = prop->value ();
791 string::size_type const c = v.find_first_of (',');
792 string const s = v.substr (0, c);
793 if (s == X_("IsStart")) {
794 session_range_location->set_start (loc->start(), true);
796 } else if (s == X_("IsEnd")) {
797 session_range_location->set_end (loc->start(), true);
801 if (c == string::npos) {
805 v = v.substr (c + 1);
811 locations.push_back (loc);
815 catch (failed_constructor& err) {
816 error << _("could not load location from session file - ignored") << endmsg;
820 if (locations.size()) {
821 current_location = locations.front();
823 current_location = 0;
827 changed (OTHER); /* EMIT SIGNAL */
832 struct LocationStartEarlierComparison
834 bool operator() (Location *a, Location *b) {
835 return a->start() < b->start();
839 struct LocationStartLaterComparison
841 bool operator() (Location *a, Location *b) {
842 return a->start() > b->start();
847 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
852 Glib::Mutex::Lock lm (lock);
856 LocationStartLaterComparison cmp;
859 /* locs is now sorted latest..earliest */
861 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
862 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
865 if (!(*i)->is_hidden() && (*i)->start() < frame) {
874 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
879 Glib::Mutex::Lock lm (lock);
883 LocationStartEarlierComparison cmp;
886 /* locs is now sorted earliest..latest */
888 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
889 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
892 if (!(*i)->is_hidden() && (*i)->start() > frame) {
900 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
901 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
903 * @param frame Frame to look for.
904 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
905 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
908 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
910 before = after = max_framepos;
915 Glib::Mutex::Lock lm (lock);
919 /* Get a list of positions; don't store any that are exactly on our requested position */
921 std::list<nframes64_t> positions;
923 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
924 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
928 if (!(*i)->is_hidden()) {
929 if ((*i)->is_mark ()) {
930 if ((*i)->start() != frame) {
931 positions.push_back ((*i)->start ());
934 if ((*i)->start() != frame) {
935 positions.push_back ((*i)->start ());
937 if ((*i)->end() != frame) {
938 positions.push_back ((*i)->end ());
944 if (positions.empty ()) {
950 std::list<nframes64_t>::iterator i = positions.begin ();
951 while (i != positions.end () && *i < frame) {
955 if (i == positions.end ()) {
956 /* run out of marks */
957 before = positions.back ();
963 if (i == positions.begin ()) {
973 Locations::session_range_location () const
975 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
976 if ((*i)->is_session_range()) {
977 return const_cast<Location*> (*i);
984 Locations::auto_loop_location () const
986 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
987 if ((*i)->is_auto_loop()) {
988 return const_cast<Location*> (*i);
995 Locations::auto_punch_location () const
997 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
998 if ((*i)->is_auto_punch()) {
999 return const_cast<Location*> (*i);
1006 Locations::num_range_markers () const
1009 Glib::Mutex::Lock lm (lock);
1010 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1011 if ((*i)->is_range_marker()) {
1019 Locations::get_location_by_id(PBD::ID id)
1021 LocationList::iterator it;
1022 for (it = locations.begin(); it != locations.end(); ++it)
1023 if (id == (*it)->id())
1030 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
1032 Glib::Mutex::Lock lm (lock);
1034 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1035 if ((flags == 0 || (*i)->matches (flags)) &&
1036 ((*i)->start() >= start && (*i)->end() < end)) {