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) || (!is_mark() && 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 (OTHER); /* 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 (OTHER); /* 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 (OTHER); /* EMIT SIGNAL */
545 current_changed (0); /* EMIT SIGNAL */
549 Locations::add (Location *loc, bool make_current)
554 Glib::Mutex::Lock lm (lock);
555 locations.push_back (loc);
558 current_location = loc;
562 added (loc); /* EMIT SIGNAL */
565 current_changed (current_location); /* EMIT SIGNAL */
570 Locations::remove (Location *loc)
572 bool was_removed = false;
573 bool was_current = false;
574 LocationList::iterator i;
576 if (loc->is_session_range()) {
581 Glib::Mutex::Lock lm (lock);
583 for (i = locations.begin(); i != locations.end(); ++i) {
587 if (current_location == loc) {
588 current_location = 0;
598 removed (loc); /* EMIT SIGNAL */
601 current_changed (0); /* EMIT SIGNAL */
604 changed (REMOVAL); /* EMIT_SIGNAL */
609 Locations::location_changed (Location* /*loc*/)
611 changed (OTHER); /* EMIT SIGNAL */
615 Locations::get_state ()
617 XMLNode *node = new XMLNode ("Locations");
618 LocationList::iterator iter;
619 Glib::Mutex::Lock lm (lock);
621 for (iter = locations.begin(); iter != locations.end(); ++iter) {
622 node->add_child_nocopy ((*iter)->get_state ());
629 Locations::set_state (const XMLNode& node, int version)
631 if (node.name() != "Locations") {
632 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
636 XMLNodeList nlist = node.children();
639 current_location = 0;
641 Location* session_range_location = 0;
642 if (version < 3000) {
643 session_range_location = new Location (0, 0, _("session"), Location::IsSessionRange);
644 locations.push_back (session_range_location);
648 Glib::Mutex::Lock lm (lock);
650 XMLNodeConstIterator niter;
651 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
655 Location *loc = new Location (**niter);
659 if (version < 3000) {
660 /* look for old-style IsStart / IsEnd properties in this location;
661 if they are present, update the session_range_location accordingly
663 XMLProperty const * prop = (*niter)->property ("flags");
665 string v = prop->value ();
667 string::size_type const c = v.find_first_of (',');
668 string const s = v.substr (0, c);
669 if (s == X_("IsStart")) {
670 session_range_location->set_start (loc->start());
672 } else if (s == X_("IsEnd")) {
673 session_range_location->set_end (loc->start());
677 if (c == string::npos) {
681 v = v.substr (c + 1);
687 locations.push_back (loc);
691 catch (failed_constructor& err) {
692 error << _("could not load location from session file - ignored") << endmsg;
696 if (locations.size()) {
697 current_location = locations.front();
699 current_location = 0;
703 changed (OTHER); /* EMIT SIGNAL */
708 struct LocationStartEarlierComparison
710 bool operator() (Location *a, Location *b) {
711 return a->start() < b->start();
715 struct LocationStartLaterComparison
717 bool operator() (Location *a, Location *b) {
718 return a->start() > b->start();
723 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
728 Glib::Mutex::Lock lm (lock);
732 LocationStartLaterComparison cmp;
735 /* locs is now sorted latest..earliest */
737 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
738 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
741 if (!(*i)->is_hidden() && (*i)->start() < frame) {
750 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
755 Glib::Mutex::Lock lm (lock);
759 LocationStartEarlierComparison cmp;
762 /* locs is now sorted earliest..latest */
764 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
765 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
768 if (!(*i)->is_hidden() && (*i)->start() > frame) {
776 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
778 * @param frame Frame to look for.
779 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
780 * @param after Filled in with the position of the last `mark' after `frame' (or max_frames if none exists)
783 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
785 before = after = max_frames;
790 Glib::Mutex::Lock lm (lock);
794 std::list<nframes64_t> positions;
796 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
797 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
801 if (!(*i)->is_hidden()) {
802 if ((*i)->is_mark ()) {
803 positions.push_back ((*i)->start ());
805 positions.push_back ((*i)->start ());
806 positions.push_back ((*i)->end ());
811 if (positions.empty ()) {
817 std::list<nframes64_t>::iterator i = positions.begin ();
818 while (i != positions.end () && *i < frame) {
822 if (i == positions.end ()) {
823 /* run out of marks */
824 before = positions.back ();
830 if (i == positions.begin ()) {
840 Locations::session_range_location () const
842 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
843 if ((*i)->is_session_range()) {
844 return const_cast<Location*> (*i);
851 Locations::auto_loop_location () const
853 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
854 if ((*i)->is_auto_loop()) {
855 return const_cast<Location*> (*i);
862 Locations::auto_punch_location () const
864 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
865 if ((*i)->is_auto_punch()) {
866 return const_cast<Location*> (*i);
873 Locations::num_range_markers () const
876 Glib::Mutex::Lock lm (lock);
877 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
878 if ((*i)->is_range_marker()) {
886 Locations::get_location_by_id(PBD::ID id)
888 LocationList::iterator it;
889 for (it = locations.begin(); it != locations.end(); ++it)
890 if (id == (*it)->id())
897 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
899 Glib::Mutex::Lock lm (lock);
901 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
902 if ((flags == 0 || (*i)->matches (flags)) &&
903 ((*i)->start() >= start && (*i)->end() < end)) {