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"));
365 Location::set_state (const XMLNode& node, int /*version*/)
367 const XMLProperty *prop;
369 XMLNodeList cd_list = node.children();
370 XMLNodeConstIterator cd_iter;
376 if (node.name() != "Location") {
377 error << _("incorrect XML node passed to Location::set_state") << endmsg;
381 if ((prop = node.property ("id")) == 0) {
382 warning << _("XML node for Location has no ID information") << endmsg;
384 _id = prop->value ();
387 if ((prop = node.property ("name")) == 0) {
388 error << _("XML node for Location has no name information") << endmsg;
392 set_name (prop->value());
394 if ((prop = node.property ("start")) == 0) {
395 error << _("XML node for Location has no start information") << endmsg;
399 /* can't use set_start() here, because _end
400 may make the value of _start illegal.
403 sscanf (prop->value().c_str(), "%" PRId64, &_start);
405 if ((prop = node.property ("end")) == 0) {
406 error << _("XML node for Location has no end information") << endmsg;
410 sscanf (prop->value().c_str(), "%" PRId64, &_end);
412 if ((prop = node.property ("flags")) == 0) {
413 error << _("XML node for Location has no flags information") << endmsg;
417 _flags = Flags (string_2_enum (prop->value(), _flags));
419 if ((prop = node.property ("locked")) != 0) {
420 _locked = string_is_affirmative (prop->value());
425 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
429 if (cd_node->name() != "CD-Info") {
433 if ((prop = cd_node->property ("name")) != 0) {
434 cd_name = prop->value();
436 throw failed_constructor ();
439 if ((prop = cd_node->property ("value")) != 0) {
440 cd_value = prop->value();
442 throw failed_constructor ();
446 cd_info[cd_name] = cd_value;
450 recompute_bbt_from_frames ();
452 changed (this); /* EMIT SIGNAL */
458 Location::set_position_lock_style (PositionLockStyle ps)
460 if (_position_lock_style == ps) {
464 _position_lock_style = ps;
466 recompute_bbt_from_frames ();
470 Location::recompute_bbt_from_frames ()
472 if (_position_lock_style != MusicTime) {
476 _session.tempo_map().bbt_time (_start, _bbt_start);
477 _session.tempo_map().bbt_time (_end, _bbt_end);
481 Location::recompute_frames_from_bbt ()
483 if (_position_lock_style != MusicTime) {
487 TempoMap& map (_session.tempo_map());
488 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
491 /*---------------------------------------------------------------------- */
493 Locations::Locations (Session& s)
494 : SessionHandleRef (s)
496 current_location = 0;
499 Locations::~Locations ()
501 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
502 LocationList::iterator tmp = i;
510 Locations::set_current (Location *loc, bool want_lock)
515 Glib::Mutex::Lock lm (lock);
516 ret = set_current_unlocked (loc);
518 ret = set_current_unlocked (loc);
522 current_changed (current_location); /* EMIT SIGNAL */
528 Locations::next_available_name(string& result,string base)
530 LocationList::iterator i;
536 bool available[SUFFIX_MAX+1];
539 for (int k=1; k<SUFFIX_MAX; k++) {
543 for (i = locations.begin(); i != locations.end(); ++i) {
545 temp = location->name();
546 if (l && !temp.find(base,0)) {
547 suffix = atoi(temp.substr(l,3).c_str());
548 if (suffix) available[suffix] = false;
551 for (int k=1; k<=SUFFIX_MAX; k++) {
553 snprintf (buf, 31, "%d", k);
562 Locations::set_current_unlocked (Location *loc)
564 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
565 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
569 current_location = loc;
577 Glib::Mutex::Lock lm (lock);
579 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
581 LocationList::iterator tmp = i;
584 if (!(*i)->is_session_range()) {
591 current_location = 0;
594 changed (OTHER); /* EMIT SIGNAL */
595 current_changed (0); /* EMIT SIGNAL */
599 Locations::clear_markers ()
602 Glib::Mutex::Lock lm (lock);
603 LocationList::iterator tmp;
605 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
609 if ((*i)->is_mark() && !(*i)->is_session_range()) {
617 changed (OTHER); /* EMIT SIGNAL */
621 Locations::clear_ranges ()
624 Glib::Mutex::Lock lm (lock);
625 LocationList::iterator tmp;
627 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
632 if (!(*i)->is_mark()) {
640 current_location = 0;
643 changed (OTHER); /* EMIT SIGNAL */
644 current_changed (0); /* EMIT SIGNAL */
648 Locations::add (Location *loc, bool make_current)
653 Glib::Mutex::Lock lm (lock);
654 locations.push_back (loc);
657 current_location = loc;
661 added (loc); /* EMIT SIGNAL */
664 current_changed (current_location); /* EMIT SIGNAL */
669 Locations::remove (Location *loc)
671 bool was_removed = false;
672 bool was_current = false;
673 LocationList::iterator i;
675 if (loc->is_session_range()) {
680 Glib::Mutex::Lock lm (lock);
682 for (i = locations.begin(); i != locations.end(); ++i) {
686 if (current_location == loc) {
687 current_location = 0;
697 removed (loc); /* EMIT SIGNAL */
700 current_changed (0); /* EMIT SIGNAL */
703 changed (REMOVAL); /* EMIT_SIGNAL */
708 Locations::location_changed (Location* /*loc*/)
710 changed (OTHER); /* EMIT SIGNAL */
714 Locations::get_state ()
716 XMLNode *node = new XMLNode ("Locations");
717 LocationList::iterator iter;
718 Glib::Mutex::Lock lm (lock);
720 for (iter = locations.begin(); iter != locations.end(); ++iter) {
721 node->add_child_nocopy ((*iter)->get_state ());
728 Locations::set_state (const XMLNode& node, int version)
730 if (node.name() != "Locations") {
731 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
735 XMLNodeList nlist = node.children();
738 current_location = 0;
740 Location* session_range_location = 0;
741 if (version < 3000) {
742 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
743 locations.push_back (session_range_location);
747 Glib::Mutex::Lock lm (lock);
749 XMLNodeConstIterator niter;
750 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
754 Location *loc = new Location (_session, **niter);
758 if (version < 3000) {
759 /* look for old-style IsStart / IsEnd properties in this location;
760 if they are present, update the session_range_location accordingly
762 XMLProperty const * prop = (*niter)->property ("flags");
764 string v = prop->value ();
766 string::size_type const c = v.find_first_of (',');
767 string const s = v.substr (0, c);
768 if (s == X_("IsStart")) {
769 session_range_location->set_start (loc->start());
771 } else if (s == X_("IsEnd")) {
772 session_range_location->set_end (loc->start());
776 if (c == string::npos) {
780 v = v.substr (c + 1);
786 locations.push_back (loc);
790 catch (failed_constructor& err) {
791 error << _("could not load location from session file - ignored") << endmsg;
795 if (locations.size()) {
796 current_location = locations.front();
798 current_location = 0;
802 changed (OTHER); /* EMIT SIGNAL */
807 struct LocationStartEarlierComparison
809 bool operator() (Location *a, Location *b) {
810 return a->start() < b->start();
814 struct LocationStartLaterComparison
816 bool operator() (Location *a, Location *b) {
817 return a->start() > b->start();
822 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
827 Glib::Mutex::Lock lm (lock);
831 LocationStartLaterComparison cmp;
834 /* locs is now sorted latest..earliest */
836 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
837 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
840 if (!(*i)->is_hidden() && (*i)->start() < frame) {
849 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
854 Glib::Mutex::Lock lm (lock);
858 LocationStartEarlierComparison cmp;
861 /* locs is now sorted earliest..latest */
863 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
864 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
867 if (!(*i)->is_hidden() && (*i)->start() > frame) {
875 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
876 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
878 * @param frame Frame to look for.
879 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
880 * @param after Filled in with the position of the next `mark' after `frame' (or max_frames if none exists)
883 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
885 before = after = max_frames;
890 Glib::Mutex::Lock lm (lock);
894 /* Get a list of positions; don't store any that are exactly on our requested position */
896 std::list<nframes64_t> positions;
898 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
899 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
903 if (!(*i)->is_hidden()) {
904 if ((*i)->is_mark ()) {
905 if ((*i)->start() != frame) {
906 positions.push_back ((*i)->start ());
909 if ((*i)->start() != frame) {
910 positions.push_back ((*i)->start ());
912 if ((*i)->end() != frame) {
913 positions.push_back ((*i)->end ());
919 if (positions.empty ()) {
925 std::list<nframes64_t>::iterator i = positions.begin ();
926 while (i != positions.end () && *i < frame) {
930 if (i == positions.end ()) {
931 /* run out of marks */
932 before = positions.back ();
938 if (i == positions.begin ()) {
948 Locations::session_range_location () const
950 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
951 if ((*i)->is_session_range()) {
952 return const_cast<Location*> (*i);
959 Locations::auto_loop_location () const
961 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
962 if ((*i)->is_auto_loop()) {
963 return const_cast<Location*> (*i);
970 Locations::auto_punch_location () const
972 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
973 if ((*i)->is_auto_punch()) {
974 return const_cast<Location*> (*i);
981 Locations::num_range_markers () const
984 Glib::Mutex::Lock lm (lock);
985 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
986 if ((*i)->is_range_marker()) {
994 Locations::get_location_by_id(PBD::ID id)
996 LocationList::iterator it;
997 for (it = locations.begin(); it != locations.end(); ++it)
998 if (id == (*it)->id())
1005 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
1007 Glib::Mutex::Lock lm (lock);
1009 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1010 if ((flags == 0 || (*i)->matches (flags)) &&
1011 ((*i)->start() >= start && (*i)->end() < end)) {