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)
159 Location::move_to (nframes64_t pos)
167 _end = _start + length();
169 changed (this); /* EMIT SIGNAL */
176 Location::set_hidden (bool yn, void *src)
178 if (set_flag_internal (yn, IsHidden)) {
179 FlagsChanged (this, src); /* EMIT SIGNAL */
184 Location::set_cd (bool yn, void *src)
186 // XXX this really needs to be session start
187 // but its not available here - leave to GUI
190 error << _("You cannot put a CD marker at this position") << endmsg;
194 if (set_flag_internal (yn, IsCDMarker)) {
195 FlagsChanged (this, src); /* EMIT SIGNAL */
200 Location::set_is_range_marker (bool yn, void *src)
202 if (set_flag_internal (yn, IsRangeMarker)) {
203 FlagsChanged (this, src); /* EMIT SIGNAL */
208 Location::set_auto_punch (bool yn, void *src)
210 if (is_mark() || _start == _end) {
214 if (set_flag_internal (yn, IsAutoPunch)) {
215 FlagsChanged (this, src); /* EMIT SIGNAL */
220 Location::set_auto_loop (bool yn, void *src)
222 if (is_mark() || _start == _end) {
226 if (set_flag_internal (yn, IsAutoLoop)) {
227 FlagsChanged (this, src); /* EMIT SIGNAL */
232 Location::set_flag_internal (bool yn, Flags flag)
235 if (!(_flags & flag)) {
236 _flags = Flags (_flags | flag);
241 _flags = Flags (_flags & ~flag);
249 Location::set_mark (bool yn)
251 /* This function is private, and so does not emit signals */
253 if (_start != _end) {
257 set_flag_internal (yn, IsMark);
262 Location::cd_info_node(const string & name, const string & value)
264 XMLNode* root = new XMLNode("CD-Info");
266 root->add_property("name", name);
267 root->add_property("value", value);
274 Location::get_state (void)
276 XMLNode *node = new XMLNode ("Location");
279 typedef map<string, string>::const_iterator CI;
281 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
282 node->add_child_nocopy(cd_info_node(m->first, m->second));
285 id().print (buf, sizeof (buf));
286 node->add_property("id", buf);
287 node->add_property ("name", name());
288 snprintf (buf, sizeof (buf), "%" PRId64, start());
289 node->add_property ("start", buf);
290 snprintf (buf, sizeof (buf), "%" PRId64, end());
291 node->add_property ("end", buf);
292 node->add_property ("flags", enum_2_string (_flags));
293 node->add_property ("locked", (_locked ? "yes" : "no"));
299 Location::set_state (const XMLNode& node, int /*version*/)
301 const XMLProperty *prop;
303 XMLNodeList cd_list = node.children();
304 XMLNodeConstIterator cd_iter;
310 if (node.name() != "Location") {
311 error << _("incorrect XML node passed to Location::set_state") << endmsg;
315 if ((prop = node.property ("id")) == 0) {
316 warning << _("XML node for Location has no ID information") << endmsg;
318 _id = prop->value ();
321 if ((prop = node.property ("name")) == 0) {
322 error << _("XML node for Location has no name information") << endmsg;
326 set_name (prop->value());
328 if ((prop = node.property ("start")) == 0) {
329 error << _("XML node for Location has no start information") << endmsg;
333 /* can't use set_start() here, because _end
334 may make the value of _start illegal.
337 sscanf (prop->value().c_str(), "%" PRId64, &_start);
339 if ((prop = node.property ("end")) == 0) {
340 error << _("XML node for Location has no end information") << endmsg;
344 sscanf (prop->value().c_str(), "%" PRId64, &_end);
346 if ((prop = node.property ("flags")) == 0) {
347 error << _("XML node for Location has no flags information") << endmsg;
351 _flags = Flags (string_2_enum (prop->value(), _flags));
353 if ((prop = node.property ("locked")) != 0) {
354 _locked = string_is_affirmative (prop->value());
359 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
363 if (cd_node->name() != "CD-Info") {
367 if ((prop = cd_node->property ("name")) != 0) {
368 cd_name = prop->value();
370 throw failed_constructor ();
373 if ((prop = cd_node->property ("value")) != 0) {
374 cd_value = prop->value();
376 throw failed_constructor ();
380 cd_info[cd_name] = cd_value;
384 changed(this); /* EMIT SIGNAL */
389 /*---------------------------------------------------------------------- */
391 Locations::Locations ()
394 current_location = 0;
397 Locations::~Locations ()
399 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
400 LocationList::iterator tmp = i;
408 Locations::set_current (Location *loc, bool want_lock)
414 Glib::Mutex::Lock lm (lock);
415 ret = set_current_unlocked (loc);
417 ret = set_current_unlocked (loc);
421 current_changed (current_location); /* EMIT SIGNAL */
427 Locations::next_available_name(string& result,string base)
429 LocationList::iterator i;
435 bool available[SUFFIX_MAX+1];
438 for (int k=1; k<SUFFIX_MAX; k++) {
442 for (i = locations.begin(); i != locations.end(); ++i) {
444 temp = location->name();
445 if (l && !temp.find(base,0)) {
446 suffix = atoi(temp.substr(l,3).c_str());
447 if (suffix) available[suffix] = false;
450 for (int k=1; k<=SUFFIX_MAX; k++) {
452 snprintf (buf, 31, "%d", k);
461 Locations::set_current_unlocked (Location *loc)
463 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
464 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
468 current_location = loc;
476 Glib::Mutex::Lock lm (lock);
478 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
480 LocationList::iterator tmp = i;
483 if (!(*i)->is_session_range()) {
490 current_location = 0;
493 changed (); /* EMIT SIGNAL */
494 current_changed (0); /* EMIT SIGNAL */
498 Locations::clear_markers ()
501 Glib::Mutex::Lock lm (lock);
502 LocationList::iterator tmp;
504 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
508 if ((*i)->is_mark() && !(*i)->is_session_range()) {
516 changed (); /* EMIT SIGNAL */
520 Locations::clear_ranges ()
523 Glib::Mutex::Lock lm (lock);
524 LocationList::iterator tmp;
526 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
531 if (!(*i)->is_mark()) {
539 current_location = 0;
542 changed (); /* EMIT SIGNAL */
543 current_changed (0); /* EMIT SIGNAL */
547 Locations::add (Location *loc, bool make_current)
550 Glib::Mutex::Lock lm (lock);
551 locations.push_back (loc);
554 current_location = loc;
558 added (loc); /* EMIT SIGNAL */
561 current_changed (current_location); /* EMIT SIGNAL */
566 Locations::remove (Location *loc)
569 bool was_removed = false;
570 bool was_current = false;
571 LocationList::iterator i;
573 if (loc->is_session_range()) {
578 Glib::Mutex::Lock lm (lock);
580 for (i = locations.begin(); i != locations.end(); ++i) {
584 if (current_location == loc) {
585 current_location = 0;
595 removed (loc); /* EMIT SIGNAL */
598 current_changed (0); /* EMIT SIGNAL */
601 changed (); /* EMIT_SIGNAL */
606 Locations::location_changed (Location* /*loc*/)
608 changed (); /* EMIT SIGNAL */
612 Locations::get_state ()
614 XMLNode *node = new XMLNode ("Locations");
615 LocationList::iterator iter;
616 Glib::Mutex::Lock lm (lock);
618 for (iter = locations.begin(); iter != locations.end(); ++iter) {
619 node->add_child_nocopy ((*iter)->get_state ());
626 Locations::set_state (const XMLNode& node, int version)
628 if (node.name() != "Locations") {
629 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
633 XMLNodeList nlist = node.children();
636 current_location = 0;
638 Location* session_range_location = 0;
639 if (version < 3000) {
640 session_range_location = new Location (0, 0, _("session"), Location::IsSessionRange);
641 locations.push_back (session_range_location);
645 Glib::Mutex::Lock lm (lock);
647 XMLNodeConstIterator niter;
648 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
652 Location *loc = new Location (**niter);
656 if (version < 3000) {
657 /* look for old-style IsStart / IsEnd properties in this location;
658 if they are present, update the session_range_location accordingly
660 XMLProperty const * prop = (*niter)->property ("flags");
662 string v = prop->value ();
664 string::size_type const c = v.find_first_of (',');
665 string const s = v.substr (0, c);
666 if (s == X_("IsStart")) {
667 session_range_location->set_start (loc->start());
669 } else if (s == X_("IsEnd")) {
670 session_range_location->set_end (loc->start());
674 if (c == string::npos) {
678 v = v.substr (c + 1);
684 locations.push_back (loc);
688 catch (failed_constructor& err) {
689 error << _("could not load location from session file - ignored") << endmsg;
693 if (locations.size()) {
694 current_location = locations.front();
696 current_location = 0;
700 changed (); /* EMIT SIGNAL */
705 struct LocationStartEarlierComparison
707 bool operator() (Location *a, Location *b) {
708 return a->start() < b->start();
712 struct LocationStartLaterComparison
714 bool operator() (Location *a, Location *b) {
715 return a->start() > b->start();
720 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
725 Glib::Mutex::Lock lm (lock);
729 LocationStartLaterComparison cmp;
732 /* locs is now sorted latest..earliest */
734 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
735 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
738 if (!(*i)->is_hidden() && (*i)->start() < frame) {
747 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
752 Glib::Mutex::Lock lm (lock);
756 LocationStartEarlierComparison cmp;
759 /* locs is now sorted earliest..latest */
761 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
762 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
765 if (!(*i)->is_hidden() && (*i)->start() > frame) {
773 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
775 * @param frame Frame to look for.
776 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
777 * @param after Filled in with the position of the last `mark' after `frame' (or max_frames if none exists)
780 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
782 before = after = max_frames;
787 Glib::Mutex::Lock lm (lock);
791 std::list<nframes64_t> positions;
793 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
794 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
798 if (!(*i)->is_hidden()) {
799 if ((*i)->is_mark ()) {
800 positions.push_back ((*i)->start ());
802 positions.push_back ((*i)->start ());
803 positions.push_back ((*i)->end ());
808 if (positions.empty ()) {
814 std::list<nframes64_t>::iterator i = positions.begin ();
815 while (i != positions.end () && *i < frame) {
819 if (i == positions.end ()) {
820 /* run out of marks */
821 before = positions.back ();
827 if (i == positions.begin ()) {
837 Locations::session_range_location () const
839 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
840 if ((*i)->is_session_range()) {
841 return const_cast<Location*> (*i);
848 Locations::auto_loop_location () const
850 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
851 if ((*i)->is_auto_loop()) {
852 return const_cast<Location*> (*i);
859 Locations::auto_punch_location () const
861 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
862 if ((*i)->is_auto_punch()) {
863 return const_cast<Location*> (*i);
870 Locations::num_range_markers () const
873 Glib::Mutex::Lock lm (lock);
874 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
875 if ((*i)->is_range_marker()) {
883 Locations::get_location_by_id(PBD::ID id)
885 LocationList::iterator it;
886 for (it = locations.begin(); it != locations.end(); ++it)
887 if (id == (*it)->id())
894 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
896 Glib::Mutex::Lock lm (lock);
898 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
899 if ((flags == 0 || (*i)->matches (flags)) &&
900 ((*i)->start() >= start && (*i)->end() < end)) {