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 */
29 #include "pbd/stl_delete.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
33 #include "ardour/location.h"
34 #include "ardour/session.h"
35 #include "ardour/audiofilesource.h"
42 using namespace ARDOUR;
45 Location::Location (const Location& other)
46 : StatefulDestructible(),
48 _start (other._start),
52 /* copy is not locked even if original was */
57 Location::Location (const XMLNode& node)
59 if (set_state (node, Stateful::loading_state_version)) {
60 throw failed_constructor ();
65 Location::operator= (const Location& other)
72 _start = other._start;
74 _flags = other._flags;
76 /* copy is not locked even if original was */
80 /* "changed" not emitted on purpose */
86 Location::set_start (nframes64_t s)
92 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
100 start_changed (this); /* EMIT SIGNAL */
101 end_changed (this); /* EMIT SIGNAL */
108 start_changed (this); /* EMIT SIGNAL */
109 if (is_session_range ()) {
110 Session::StartTimeChanged (); /* EMIT SIGNAL */
111 AudioFileSource::set_header_position_offset (s);
119 Location::set_end (nframes64_t e)
125 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
133 start_changed (this); /* EMIT SIGNAL */
134 end_changed (this); /* EMIT SIGNAL */
141 end_changed(this); /* EMIT SIGNAL */
143 if (is_session_range()) {
144 Session::EndTimeChanged (); /* EMIT SIGNAL */
152 Location::set (nframes64_t start, nframes64_t end)
154 int const s = set_start (start);
155 int const e = set_end (end);
157 return (s == 0 && e == 0) ? 0 : -1;
161 Location::move_to (nframes64_t pos)
169 _end = _start + length();
171 changed (this); /* EMIT SIGNAL */
178 Location::set_hidden (bool yn, void *src)
180 if (set_flag_internal (yn, IsHidden)) {
181 FlagsChanged (this, src); /* EMIT SIGNAL */
186 Location::set_cd (bool yn, void *src)
188 // XXX this really needs to be session start
189 // but its not available here - leave to GUI
192 error << _("You cannot put a CD marker at this position") << endmsg;
196 if (set_flag_internal (yn, IsCDMarker)) {
197 FlagsChanged (this, src); /* EMIT SIGNAL */
202 Location::set_is_range_marker (bool yn, void *src)
204 if (set_flag_internal (yn, IsRangeMarker)) {
205 FlagsChanged (this, src); /* EMIT SIGNAL */
210 Location::set_auto_punch (bool yn, void *src)
212 if (is_mark() || _start == _end) {
216 if (set_flag_internal (yn, IsAutoPunch)) {
217 FlagsChanged (this, src); /* EMIT SIGNAL */
222 Location::set_auto_loop (bool yn, void *src)
224 if (is_mark() || _start == _end) {
228 if (set_flag_internal (yn, IsAutoLoop)) {
229 FlagsChanged (this, src); /* EMIT SIGNAL */
234 Location::set_flag_internal (bool yn, Flags flag)
237 if (!(_flags & flag)) {
238 _flags = Flags (_flags | flag);
243 _flags = Flags (_flags & ~flag);
251 Location::set_mark (bool yn)
253 /* This function is private, and so does not emit signals */
255 if (_start != _end) {
259 set_flag_internal (yn, IsMark);
264 Location::cd_info_node(const string & name, const string & value)
266 XMLNode* root = new XMLNode("CD-Info");
268 root->add_property("name", name);
269 root->add_property("value", value);
276 Location::get_state (void)
278 XMLNode *node = new XMLNode ("Location");
281 typedef map<string, string>::const_iterator CI;
283 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
284 node->add_child_nocopy(cd_info_node(m->first, m->second));
287 id().print (buf, sizeof (buf));
288 node->add_property("id", buf);
289 node->add_property ("name", name());
290 snprintf (buf, sizeof (buf), "%" PRId64, start());
291 node->add_property ("start", buf);
292 snprintf (buf, sizeof (buf), "%" PRId64, end());
293 node->add_property ("end", buf);
294 node->add_property ("flags", enum_2_string (_flags));
295 node->add_property ("locked", (_locked ? "yes" : "no"));
301 Location::set_state (const XMLNode& node, int /*version*/)
303 const XMLProperty *prop;
305 XMLNodeList cd_list = node.children();
306 XMLNodeConstIterator cd_iter;
312 if (node.name() != "Location") {
313 error << _("incorrect XML node passed to Location::set_state") << endmsg;
317 if ((prop = node.property ("id")) == 0) {
318 warning << _("XML node for Location has no ID information") << endmsg;
320 _id = prop->value ();
323 if ((prop = node.property ("name")) == 0) {
324 error << _("XML node for Location has no name information") << endmsg;
328 set_name (prop->value());
330 if ((prop = node.property ("start")) == 0) {
331 error << _("XML node for Location has no start information") << endmsg;
335 /* can't use set_start() here, because _end
336 may make the value of _start illegal.
339 sscanf (prop->value().c_str(), "%" PRId64, &_start);
341 if ((prop = node.property ("end")) == 0) {
342 error << _("XML node for Location has no end information") << endmsg;
346 sscanf (prop->value().c_str(), "%" PRId64, &_end);
348 if ((prop = node.property ("flags")) == 0) {
349 error << _("XML node for Location has no flags information") << endmsg;
353 _flags = Flags (string_2_enum (prop->value(), _flags));
355 if ((prop = node.property ("locked")) != 0) {
356 _locked = string_is_affirmative (prop->value());
361 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
365 if (cd_node->name() != "CD-Info") {
369 if ((prop = cd_node->property ("name")) != 0) {
370 cd_name = prop->value();
372 throw failed_constructor ();
375 if ((prop = cd_node->property ("value")) != 0) {
376 cd_value = prop->value();
378 throw failed_constructor ();
382 cd_info[cd_name] = cd_value;
386 changed(this); /* EMIT SIGNAL */
391 /*---------------------------------------------------------------------- */
393 Locations::Locations ()
396 current_location = 0;
399 Locations::~Locations ()
401 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
402 LocationList::iterator tmp = i;
410 Locations::set_current (Location *loc, bool want_lock)
416 Glib::Mutex::Lock lm (lock);
417 ret = set_current_unlocked (loc);
419 ret = set_current_unlocked (loc);
423 current_changed (current_location); /* EMIT SIGNAL */
429 Locations::next_available_name(string& result,string base)
431 LocationList::iterator i;
437 bool available[SUFFIX_MAX+1];
440 for (int k=1; k<SUFFIX_MAX; k++) {
444 for (i = locations.begin(); i != locations.end(); ++i) {
446 temp = location->name();
447 if (l && !temp.find(base,0)) {
448 suffix = atoi(temp.substr(l,3).c_str());
449 if (suffix) available[suffix] = false;
452 for (int k=1; k<=SUFFIX_MAX; k++) {
454 snprintf (buf, 31, "%d", k);
463 Locations::set_current_unlocked (Location *loc)
465 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
466 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
470 current_location = loc;
478 Glib::Mutex::Lock lm (lock);
480 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
482 LocationList::iterator tmp = i;
485 if (!(*i)->is_session_range()) {
492 current_location = 0;
495 changed (); /* EMIT SIGNAL */
496 current_changed (0); /* EMIT SIGNAL */
500 Locations::clear_markers ()
503 Glib::Mutex::Lock lm (lock);
504 LocationList::iterator tmp;
506 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
510 if ((*i)->is_mark() && !(*i)->is_session_range()) {
518 changed (); /* EMIT SIGNAL */
522 Locations::clear_ranges ()
525 Glib::Mutex::Lock lm (lock);
526 LocationList::iterator tmp;
528 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
533 if (!(*i)->is_mark()) {
541 current_location = 0;
544 changed (); /* EMIT SIGNAL */
545 current_changed (0); /* EMIT SIGNAL */
549 Locations::add (Location *loc, bool make_current)
552 Glib::Mutex::Lock lm (lock);
553 locations.push_back (loc);
556 current_location = loc;
560 added (loc); /* EMIT SIGNAL */
563 current_changed (current_location); /* EMIT SIGNAL */
568 Locations::remove (Location *loc)
571 bool was_removed = false;
572 bool was_current = false;
573 LocationList::iterator i;
575 if (loc->is_session_range()) {
580 Glib::Mutex::Lock lm (lock);
582 for (i = locations.begin(); i != locations.end(); ++i) {
586 if (current_location == loc) {
587 current_location = 0;
597 removed (loc); /* EMIT SIGNAL */
600 current_changed (0); /* EMIT SIGNAL */
603 changed (); /* EMIT_SIGNAL */
608 Locations::location_changed (Location* /*loc*/)
610 changed (); /* EMIT SIGNAL */
614 Locations::get_state ()
616 XMLNode *node = new XMLNode ("Locations");
617 LocationList::iterator iter;
618 Glib::Mutex::Lock lm (lock);
620 for (iter = locations.begin(); iter != locations.end(); ++iter) {
621 node->add_child_nocopy ((*iter)->get_state ());
628 Locations::set_state (const XMLNode& node, int version)
630 if (node.name() != "Locations") {
631 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
635 XMLNodeList nlist = node.children();
638 current_location = 0;
640 Location* session_range_location = 0;
641 if (version < 3000) {
642 session_range_location = new Location (0, 0, _("session"), Location::IsSessionRange);
643 locations.push_back (session_range_location);
647 Glib::Mutex::Lock lm (lock);
649 XMLNodeConstIterator niter;
650 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
654 Location *loc = new Location (**niter);
658 if (version < 3000) {
659 /* look for old-style IsStart / IsEnd properties in this location;
660 if they are present, update the session_range_location accordingly
662 XMLProperty const * prop = (*niter)->property ("flags");
664 string v = prop->value ();
666 string::size_type const c = v.find_first_of (',');
667 string const s = v.substr (0, c);
668 if (s == X_("IsStart")) {
669 session_range_location->set_start (loc->start());
671 } else if (s == X_("IsEnd")) {
672 session_range_location->set_end (loc->start());
676 if (c == string::npos) {
680 v = v.substr (c + 1);
686 locations.push_back (loc);
690 catch (failed_constructor& err) {
691 error << _("could not load location from session file - ignored") << endmsg;
695 if (locations.size()) {
696 current_location = locations.front();
698 current_location = 0;
702 changed (); /* EMIT SIGNAL */
707 struct LocationStartEarlierComparison
709 bool operator() (Location *a, Location *b) {
710 return a->start() < b->start();
714 struct LocationStartLaterComparison
716 bool operator() (Location *a, Location *b) {
717 return a->start() > b->start();
722 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
727 Glib::Mutex::Lock lm (lock);
731 LocationStartLaterComparison cmp;
734 /* locs is now sorted latest..earliest */
736 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
737 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
740 if (!(*i)->is_hidden() && (*i)->start() < frame) {
749 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
754 Glib::Mutex::Lock lm (lock);
758 LocationStartEarlierComparison cmp;
761 /* locs is now sorted earliest..latest */
763 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
764 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
767 if (!(*i)->is_hidden() && (*i)->start() > frame) {
775 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
777 * @param frame Frame to look for.
778 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
779 * @param after Filled in with the position of the last `mark' after `frame' (or max_frames if none exists)
782 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
784 before = after = max_frames;
789 Glib::Mutex::Lock lm (lock);
793 std::list<nframes64_t> positions;
795 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
796 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
800 if (!(*i)->is_hidden()) {
801 if ((*i)->is_mark ()) {
802 positions.push_back ((*i)->start ());
804 positions.push_back ((*i)->start ());
805 positions.push_back ((*i)->end ());
810 if (positions.empty ()) {
816 std::list<nframes64_t>::iterator i = positions.begin ();
817 while (i != positions.end () && *i < frame) {
821 if (i == positions.end ()) {
822 /* run out of marks */
823 before = positions.back ();
829 if (i == positions.begin ()) {
839 Locations::session_range_location () const
841 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
842 if ((*i)->is_session_range()) {
843 return const_cast<Location*> (*i);
850 Locations::auto_loop_location () const
852 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
853 if ((*i)->is_auto_loop()) {
854 return const_cast<Location*> (*i);
861 Locations::auto_punch_location () const
863 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
864 if ((*i)->is_auto_punch()) {
865 return const_cast<Location*> (*i);
872 Locations::num_range_markers () const
875 Glib::Mutex::Lock lm (lock);
876 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
877 if ((*i)->is_range_marker()) {
885 Locations::get_location_by_id(PBD::ID id)
887 LocationList::iterator it;
888 for (it = locations.begin(); it != locations.end(); ++it)
889 if (id == (*it)->id())
896 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
898 Glib::Mutex::Lock lm (lock);
900 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
901 if ((flags == 0 || (*i)->matches (flags)) &&
902 ((*i)->start() >= start && (*i)->end() < end)) {