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, framepos_t sample_start, framepos_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 (framepos_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 framepos_t const old = _start;
157 if (allow_bbt_recompute) {
158 recompute_bbt_from_frames ();
160 start_changed (this); /* EMIT SIGNAL */
161 if (is_session_range ()) {
162 Session::StartTimeChanged (old); /* EMIT SIGNAL */
163 AudioFileSource::set_header_position_offset (s);
170 /** Set end position.
172 * @param force true to force setting, even if the given new start is after the current end.
173 * @param allow_bbt_recompute True to recompute BBT end time from the new given end time.
176 Location::set_end (framepos_t e, bool force, bool allow_bbt_recompute)
183 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
192 if (allow_bbt_recompute) {
193 recompute_bbt_from_frames ();
195 start_changed (this); /* EMIT SIGNAL */
196 end_changed (this); /* EMIT SIGNAL */
202 framepos_t const old = _end;
205 if (allow_bbt_recompute) {
206 recompute_bbt_from_frames ();
208 end_changed(this); /* EMIT SIGNAL */
210 if (is_session_range()) {
211 Session::EndTimeChanged (old); /* EMIT SIGNAL */
219 Location::set (framepos_t start, framepos_t end, bool allow_bbt_recompute)
222 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
226 /* now we know these values are ok, so force-set them */
227 int const s = set_start (start, true, allow_bbt_recompute);
228 int const e = set_end (end, true, allow_bbt_recompute);
230 return (s == 0 && e == 0) ? 0 : -1;
234 Location::move_to (framepos_t pos)
242 _end = _start + length();
243 recompute_bbt_from_frames ();
245 changed (this); /* EMIT SIGNAL */
252 Location::set_hidden (bool yn, void *src)
254 if (set_flag_internal (yn, IsHidden)) {
255 FlagsChanged (this, src); /* EMIT SIGNAL */
260 Location::set_cd (bool yn, void *src)
262 // XXX this really needs to be session start
263 // but its not available here - leave to GUI
266 error << _("You cannot put a CD marker at this position") << endmsg;
270 if (set_flag_internal (yn, IsCDMarker)) {
271 FlagsChanged (this, src); /* EMIT SIGNAL */
276 Location::set_is_range_marker (bool yn, void *src)
278 if (set_flag_internal (yn, IsRangeMarker)) {
279 FlagsChanged (this, src); /* EMIT SIGNAL */
284 Location::set_auto_punch (bool yn, void *src)
286 if (is_mark() || _start == _end) {
290 if (set_flag_internal (yn, IsAutoPunch)) {
291 FlagsChanged (this, src); /* EMIT SIGNAL */
296 Location::set_auto_loop (bool yn, void *src)
298 if (is_mark() || _start == _end) {
302 if (set_flag_internal (yn, IsAutoLoop)) {
303 FlagsChanged (this, src); /* EMIT SIGNAL */
308 Location::set_flag_internal (bool yn, Flags flag)
311 if (!(_flags & flag)) {
312 _flags = Flags (_flags | flag);
317 _flags = Flags (_flags & ~flag);
325 Location::set_mark (bool yn)
327 /* This function is private, and so does not emit signals */
329 if (_start != _end) {
333 set_flag_internal (yn, IsMark);
338 Location::cd_info_node(const string & name, const string & value)
340 XMLNode* root = new XMLNode("CD-Info");
342 root->add_property("name", name);
343 root->add_property("value", value);
350 Location::get_state ()
352 XMLNode *node = new XMLNode ("Location");
355 typedef map<string, string>::const_iterator CI;
357 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
358 node->add_child_nocopy(cd_info_node(m->first, m->second));
361 id().print (buf, sizeof (buf));
362 node->add_property("id", buf);
363 node->add_property ("name", name());
364 snprintf (buf, sizeof (buf), "%" PRId64, start());
365 node->add_property ("start", buf);
366 snprintf (buf, sizeof (buf), "%" PRId64, end());
367 node->add_property ("end", buf);
368 node->add_property ("flags", enum_2_string (_flags));
369 node->add_property ("locked", (_locked ? "yes" : "no"));
370 node->add_property ("position-lock-style", enum_2_string (_position_lock_style));
376 Location::set_state (const XMLNode& node, int /*version*/)
378 const XMLProperty *prop;
380 XMLNodeList cd_list = node.children();
381 XMLNodeConstIterator cd_iter;
387 if (node.name() != "Location") {
388 error << _("incorrect XML node passed to Location::set_state") << endmsg;
392 if ((prop = node.property ("id")) == 0) {
393 warning << _("XML node for Location has no ID information") << endmsg;
395 _id = prop->value ();
398 if ((prop = node.property ("name")) == 0) {
399 error << _("XML node for Location has no name information") << endmsg;
403 set_name (prop->value());
405 if ((prop = node.property ("start")) == 0) {
406 error << _("XML node for Location has no start information") << endmsg;
410 /* can't use set_start() here, because _end
411 may make the value of _start illegal.
414 sscanf (prop->value().c_str(), "%" PRId64, &_start);
416 if ((prop = node.property ("end")) == 0) {
417 error << _("XML node for Location has no end information") << endmsg;
421 sscanf (prop->value().c_str(), "%" PRId64, &_end);
423 if ((prop = node.property ("flags")) == 0) {
424 error << _("XML node for Location has no flags information") << endmsg;
428 _flags = Flags (string_2_enum (prop->value(), _flags));
430 if ((prop = node.property ("locked")) != 0) {
431 _locked = string_is_affirmative (prop->value());
436 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
440 if (cd_node->name() != "CD-Info") {
444 if ((prop = cd_node->property ("name")) != 0) {
445 cd_name = prop->value();
447 throw failed_constructor ();
450 if ((prop = cd_node->property ("value")) != 0) {
451 cd_value = prop->value();
453 throw failed_constructor ();
457 cd_info[cd_name] = cd_value;
460 if ((prop = node.property ("position-lock-style")) != 0) {
461 _position_lock_style = PositionLockStyle (string_2_enum (prop->value(), _position_lock_style));
464 recompute_bbt_from_frames ();
466 changed (this); /* EMIT SIGNAL */
472 Location::set_position_lock_style (PositionLockStyle ps)
474 if (_position_lock_style == ps) {
478 _position_lock_style = ps;
480 recompute_bbt_from_frames ();
482 PositionLockStyleChanged (this); /* EMIT SIGNAL */
486 Location::recompute_bbt_from_frames ()
488 if (_position_lock_style != MusicTime) {
492 _session.tempo_map().bbt_time (_start, _bbt_start);
493 _session.tempo_map().bbt_time (_end, _bbt_end);
497 Location::recompute_frames_from_bbt ()
499 if (_position_lock_style != MusicTime) {
503 TempoMap& map (_session.tempo_map());
504 set (map.frame_time (_bbt_start), map.frame_time (_bbt_end), false);
521 /*---------------------------------------------------------------------- */
523 Locations::Locations (Session& s)
524 : SessionHandleRef (s)
526 current_location = 0;
529 Locations::~Locations ()
531 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
532 LocationList::iterator tmp = i;
540 Locations::set_current (Location *loc, bool want_lock)
545 Glib::Mutex::Lock lm (lock);
546 ret = set_current_unlocked (loc);
548 ret = set_current_unlocked (loc);
552 current_changed (current_location); /* EMIT SIGNAL */
558 Locations::next_available_name(string& result,string base)
560 LocationList::iterator i;
566 bool available[SUFFIX_MAX+1];
569 for (int k=1; k<SUFFIX_MAX; k++) {
573 for (i = locations.begin(); i != locations.end(); ++i) {
575 temp = location->name();
576 if (l && !temp.find(base,0)) {
577 suffix = atoi(temp.substr(l,3).c_str());
578 if (suffix) available[suffix] = false;
581 for (int k=1; k<=SUFFIX_MAX; k++) {
583 snprintf (buf, 31, "%d", k);
592 Locations::set_current_unlocked (Location *loc)
594 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
595 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
599 current_location = loc;
607 Glib::Mutex::Lock lm (lock);
609 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
611 LocationList::iterator tmp = i;
614 if (!(*i)->is_session_range()) {
621 current_location = 0;
624 changed (OTHER); /* EMIT SIGNAL */
625 current_changed (0); /* EMIT SIGNAL */
629 Locations::clear_markers ()
632 Glib::Mutex::Lock lm (lock);
633 LocationList::iterator tmp;
635 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
639 if ((*i)->is_mark() && !(*i)->is_session_range()) {
647 changed (OTHER); /* EMIT SIGNAL */
651 Locations::clear_ranges ()
654 Glib::Mutex::Lock lm (lock);
655 LocationList::iterator tmp;
657 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
662 if (!(*i)->is_mark()) {
670 current_location = 0;
673 changed (OTHER); /* EMIT SIGNAL */
674 current_changed (0); /* EMIT SIGNAL */
678 Locations::add (Location *loc, bool make_current)
683 Glib::Mutex::Lock lm (lock);
684 locations.push_back (loc);
687 current_location = loc;
691 added (loc); /* EMIT SIGNAL */
694 current_changed (current_location); /* EMIT SIGNAL */
697 if (loc->is_session_range()) {
698 Session::StartTimeChanged (0);
699 Session::EndTimeChanged (1);
704 Locations::remove (Location *loc)
706 bool was_removed = false;
707 bool was_current = false;
708 LocationList::iterator i;
710 if (loc->is_session_range()) {
715 Glib::Mutex::Lock lm (lock);
717 for (i = locations.begin(); i != locations.end(); ++i) {
721 if (current_location == loc) {
722 current_location = 0;
732 removed (loc); /* EMIT SIGNAL */
735 current_changed (0); /* EMIT SIGNAL */
738 changed (REMOVAL); /* EMIT_SIGNAL */
743 Locations::location_changed (Location* /*loc*/)
745 changed (OTHER); /* EMIT SIGNAL */
749 Locations::get_state ()
751 XMLNode *node = new XMLNode ("Locations");
752 LocationList::iterator iter;
753 Glib::Mutex::Lock lm (lock);
755 for (iter = locations.begin(); iter != locations.end(); ++iter) {
756 node->add_child_nocopy ((*iter)->get_state ());
763 Locations::set_state (const XMLNode& node, int version)
765 if (node.name() != "Locations") {
766 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
770 XMLNodeList nlist = node.children();
773 current_location = 0;
775 Location* session_range_location = 0;
776 if (version < 3000) {
777 session_range_location = new Location (_session, 0, 0, _("session"), Location::IsSessionRange);
778 locations.push_back (session_range_location);
782 Glib::Mutex::Lock lm (lock);
784 XMLNodeConstIterator niter;
785 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
789 Location *loc = new Location (_session, **niter);
793 if (version < 3000) {
794 /* look for old-style IsStart / IsEnd properties in this location;
795 if they are present, update the session_range_location accordingly
797 XMLProperty const * prop = (*niter)->property ("flags");
799 string v = prop->value ();
801 string::size_type const c = v.find_first_of (',');
802 string const s = v.substr (0, c);
803 if (s == X_("IsStart")) {
804 session_range_location->set_start (loc->start(), true);
806 } else if (s == X_("IsEnd")) {
807 session_range_location->set_end (loc->start(), true);
811 if (c == string::npos) {
815 v = v.substr (c + 1);
821 locations.push_back (loc);
825 catch (failed_constructor& err) {
826 error << _("could not load location from session file - ignored") << endmsg;
830 if (locations.size()) {
831 current_location = locations.front();
833 current_location = 0;
837 changed (OTHER); /* EMIT SIGNAL */
842 struct LocationStartEarlierComparison
844 bool operator() (Location *a, Location *b) {
845 return a->start() < b->start();
849 struct LocationStartLaterComparison
851 bool operator() (Location *a, Location *b) {
852 return a->start() > b->start();
857 Locations::first_location_before (framepos_t frame, bool include_special_ranges)
862 Glib::Mutex::Lock lm (lock);
866 LocationStartLaterComparison cmp;
869 /* locs is now sorted latest..earliest */
871 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
872 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
875 if (!(*i)->is_hidden() && (*i)->start() < frame) {
884 Locations::first_location_after (framepos_t frame, bool include_special_ranges)
889 Glib::Mutex::Lock lm (lock);
893 LocationStartEarlierComparison cmp;
896 /* locs is now sorted earliest..latest */
898 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
899 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
902 if (!(*i)->is_hidden() && (*i)->start() > frame) {
910 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
911 * side of a frame. Note that if frame is exactly on a `mark', that mark will not be considered for returning
913 * @param frame Frame to look for.
914 * @param before Filled in with the position of the last `mark' before `frame' (or max_framepos if none exists)
915 * @param after Filled in with the position of the next `mark' after `frame' (or max_framepos if none exists)
918 Locations::marks_either_side (framepos_t const frame, framepos_t& before, framepos_t& after) const
920 before = after = max_framepos;
925 Glib::Mutex::Lock lm (lock);
929 /* Get a list of positions; don't store any that are exactly on our requested position */
931 std::list<framepos_t> positions;
933 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
934 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
938 if (!(*i)->is_hidden()) {
939 if ((*i)->is_mark ()) {
940 if ((*i)->start() != frame) {
941 positions.push_back ((*i)->start ());
944 if ((*i)->start() != frame) {
945 positions.push_back ((*i)->start ());
947 if ((*i)->end() != frame) {
948 positions.push_back ((*i)->end ());
954 if (positions.empty ()) {
960 std::list<framepos_t>::iterator i = positions.begin ();
961 while (i != positions.end () && *i < frame) {
965 if (i == positions.end ()) {
966 /* run out of marks */
967 before = positions.back ();
973 if (i == positions.begin ()) {
983 Locations::session_range_location () const
985 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
986 if ((*i)->is_session_range()) {
987 return const_cast<Location*> (*i);
994 Locations::auto_loop_location () const
996 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
997 if ((*i)->is_auto_loop()) {
998 return const_cast<Location*> (*i);
1005 Locations::auto_punch_location () const
1007 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1008 if ((*i)->is_auto_punch()) {
1009 return const_cast<Location*> (*i);
1016 Locations::num_range_markers () const
1019 Glib::Mutex::Lock lm (lock);
1020 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1021 if ((*i)->is_range_marker()) {
1029 Locations::get_location_by_id(PBD::ID id)
1031 LocationList::iterator it;
1032 for (it = locations.begin(); it != locations.end(); ++it)
1033 if (id == (*it)->id())
1040 Locations::find_all_between (framepos_t start, framepos_t end, LocationList& ll, Location::Flags flags)
1042 Glib::Mutex::Lock lm (lock);
1044 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
1045 if ((flags == 0 || (*i)->matches (flags)) &&
1046 ((*i)->start() >= start && (*i)->end() < end)) {