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)
98 start_changed(this); /* EMIT SIGNAL */
99 end_changed(this); /* EMIT SIGNAL */
105 if (is_session_range()) {
106 start_changed (this); /* EMIT SIGNAL */
107 Session::StartTimeChanged (); /* EMIT SIGNAL */
108 AudioFileSource::set_header_position_offset (s);
111 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
117 start_changed(this); /* EMIT SIGNAL */
124 Location::set_end (nframes64_t e)
134 start_changed(this); /* EMIT SIGNAL */
135 end_changed(this); /* EMIT SIGNAL */
140 if (is_session_range()) {
142 end_changed (this); /* EMIT SIGNAL */
143 Session::EndTimeChanged (); /* EMIT SIGNAL */
146 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
152 end_changed(this); /* EMIT SIGNAL */
158 Location::set (nframes64_t start, nframes64_t end)
164 if (is_mark() && start != end) {
166 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
170 if (_start != start) {
172 start_changed(this); /* EMIT SIGNAL */
177 end_changed(this); /* EMIT SIGNAL */
183 Location::move_to (nframes64_t pos)
191 _end = _start + length();
193 changed (this); /* EMIT SIGNAL */
200 Location::set_hidden (bool yn, void *src)
202 if (set_flag_internal (yn, IsHidden)) {
203 FlagsChanged (this, src); /* EMIT SIGNAL */
208 Location::set_cd (bool yn, void *src)
210 // XXX this really needs to be session start
211 // but its not available here - leave to GUI
214 error << _("You cannot put a CD marker at this position") << endmsg;
218 if (set_flag_internal (yn, IsCDMarker)) {
219 FlagsChanged (this, src); /* EMIT SIGNAL */
224 Location::set_is_session_range (bool yn, void *src)
226 if (set_flag_internal (yn, IsSessionRange)) {
227 FlagsChanged (this, src); /* EMIT SIGNAL */
232 Location::set_is_range_marker (bool yn, void *src)
234 if (set_flag_internal (yn, IsRangeMarker)) {
235 FlagsChanged (this, src); /* EMIT SIGNAL */
240 Location::set_auto_punch (bool yn, void *src)
242 if (is_mark() || _start == _end) {
246 if (set_flag_internal (yn, IsAutoPunch)) {
247 FlagsChanged (this, src); /* EMIT SIGNAL */
252 Location::set_auto_loop (bool yn, void *src)
254 if (is_mark() || _start == _end) {
258 if (set_flag_internal (yn, IsAutoLoop)) {
259 FlagsChanged (this, src); /* EMIT SIGNAL */
264 Location::set_flag_internal (bool yn, Flags flag)
267 if (!(_flags & flag)) {
268 _flags = Flags (_flags | flag);
273 _flags = Flags (_flags & ~flag);
281 Location::set_mark (bool yn)
283 /* This function is private, and so does not emit signals */
285 if (_start != _end) {
289 set_flag_internal (yn, IsMark);
294 Location::cd_info_node(const string & name, const string & value)
296 XMLNode* root = new XMLNode("CD-Info");
298 root->add_property("name", name);
299 root->add_property("value", value);
306 Location::get_state (void)
308 XMLNode *node = new XMLNode ("Location");
311 typedef map<string, string>::const_iterator CI;
313 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
314 node->add_child_nocopy(cd_info_node(m->first, m->second));
317 id().print (buf, sizeof (buf));
318 node->add_property("id", buf);
319 node->add_property ("name", name());
320 snprintf (buf, sizeof (buf), "%" PRId64, start());
321 node->add_property ("start", buf);
322 snprintf (buf, sizeof (buf), "%" PRId64, end());
323 node->add_property ("end", buf);
324 node->add_property ("flags", enum_2_string (_flags));
325 node->add_property ("locked", (_locked ? "yes" : "no"));
331 Location::set_state (const XMLNode& node, int /*version*/)
333 const XMLProperty *prop;
335 XMLNodeList cd_list = node.children();
336 XMLNodeConstIterator cd_iter;
342 if (node.name() != "Location") {
343 error << _("incorrect XML node passed to Location::set_state") << endmsg;
347 if ((prop = node.property ("id")) == 0) {
348 warning << _("XML node for Location has no ID information") << endmsg;
350 _id = prop->value ();
353 if ((prop = node.property ("name")) == 0) {
354 error << _("XML node for Location has no name information") << endmsg;
358 set_name (prop->value());
360 if ((prop = node.property ("start")) == 0) {
361 error << _("XML node for Location has no start information") << endmsg;
365 /* can't use set_start() here, because _end
366 may make the value of _start illegal.
369 sscanf (prop->value().c_str(), "%" PRId64, &_start);
371 if ((prop = node.property ("end")) == 0) {
372 error << _("XML node for Location has no end information") << endmsg;
376 sscanf (prop->value().c_str(), "%" PRId64, &_end);
378 if ((prop = node.property ("flags")) == 0) {
379 error << _("XML node for Location has no flags information") << endmsg;
383 _flags = Flags (string_2_enum (prop->value(), _flags));
385 if ((prop = node.property ("locked")) != 0) {
386 _locked = string_is_affirmative (prop->value());
391 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
395 if (cd_node->name() != "CD-Info") {
399 if ((prop = cd_node->property ("name")) != 0) {
400 cd_name = prop->value();
402 throw failed_constructor ();
405 if ((prop = cd_node->property ("value")) != 0) {
406 cd_value = prop->value();
408 throw failed_constructor ();
412 cd_info[cd_name] = cd_value;
416 changed(this); /* EMIT SIGNAL */
421 /*---------------------------------------------------------------------- */
423 Locations::Locations ()
426 current_location = 0;
429 Locations::~Locations ()
431 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
432 LocationList::iterator tmp = i;
440 Locations::set_current (Location *loc, bool want_lock)
446 Glib::Mutex::Lock lm (lock);
447 ret = set_current_unlocked (loc);
449 ret = set_current_unlocked (loc);
453 current_changed (current_location); /* EMIT SIGNAL */
459 Locations::next_available_name(string& result,string base)
461 LocationList::iterator i;
467 bool available[SUFFIX_MAX+1];
470 for (int k=1; k<SUFFIX_MAX; k++) {
474 for (i = locations.begin(); i != locations.end(); ++i) {
476 temp = location->name();
477 if (l && !temp.find(base,0)) {
478 suffix = atoi(temp.substr(l,3).c_str());
479 if (suffix) available[suffix] = false;
482 for (int k=1; k<=SUFFIX_MAX; k++) {
484 snprintf (buf, 31, "%d", k);
493 Locations::set_current_unlocked (Location *loc)
495 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
496 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
500 current_location = loc;
508 Glib::Mutex::Lock lm (lock);
510 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
512 LocationList::iterator tmp = i;
515 if (!(*i)->is_session_range()) {
522 current_location = 0;
525 changed (); /* EMIT SIGNAL */
526 current_changed (0); /* EMIT SIGNAL */
530 Locations::clear_markers ()
533 Glib::Mutex::Lock lm (lock);
534 LocationList::iterator tmp;
536 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
540 if ((*i)->is_mark() && !(*i)->is_session_range()) {
548 changed (); /* EMIT SIGNAL */
552 Locations::clear_ranges ()
555 Glib::Mutex::Lock lm (lock);
556 LocationList::iterator tmp;
558 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
563 if (!(*i)->is_mark()) {
571 current_location = 0;
574 changed (); /* EMIT SIGNAL */
575 current_changed (0); /* EMIT SIGNAL */
579 Locations::add (Location *loc, bool make_current)
582 Glib::Mutex::Lock lm (lock);
583 locations.push_back (loc);
586 current_location = loc;
590 added (loc); /* EMIT SIGNAL */
593 current_changed (current_location); /* EMIT SIGNAL */
598 Locations::remove (Location *loc)
601 bool was_removed = false;
602 bool was_current = false;
603 LocationList::iterator i;
605 if (loc->is_session_range()) {
610 Glib::Mutex::Lock lm (lock);
612 for (i = locations.begin(); i != locations.end(); ++i) {
616 if (current_location == loc) {
617 current_location = 0;
627 removed (loc); /* EMIT SIGNAL */
630 current_changed (0); /* EMIT SIGNAL */
633 changed (); /* EMIT_SIGNAL */
638 Locations::location_changed (Location* /*loc*/)
640 changed (); /* EMIT SIGNAL */
644 Locations::get_state ()
646 XMLNode *node = new XMLNode ("Locations");
647 LocationList::iterator iter;
648 Glib::Mutex::Lock lm (lock);
650 for (iter = locations.begin(); iter != locations.end(); ++iter) {
651 node->add_child_nocopy ((*iter)->get_state ());
658 Locations::set_state (const XMLNode& node, int /*version*/)
660 if (node.name() != "Locations") {
661 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
665 XMLNodeList nlist = node.children();
668 current_location = 0;
671 Glib::Mutex::Lock lm (lock);
673 XMLNodeConstIterator niter;
674 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
678 Location *loc = new Location (**niter);
679 locations.push_back (loc);
682 catch (failed_constructor& err) {
683 error << _("could not load location from session file - ignored") << endmsg;
687 if (locations.size()) {
689 current_location = locations.front();
691 current_location = 0;
695 changed (); /* EMIT SIGNAL */
700 struct LocationStartEarlierComparison
702 bool operator() (Location *a, Location *b) {
703 return a->start() < b->start();
707 struct LocationStartLaterComparison
709 bool operator() (Location *a, Location *b) {
710 return a->start() > b->start();
715 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
720 Glib::Mutex::Lock lm (lock);
724 LocationStartLaterComparison cmp;
727 /* locs is now sorted latest..earliest */
729 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
730 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
733 if (!(*i)->is_hidden() && (*i)->start() < frame) {
742 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
747 Glib::Mutex::Lock lm (lock);
751 LocationStartEarlierComparison cmp;
754 /* locs is now sorted earliest..latest */
756 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
757 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
760 if (!(*i)->is_hidden() && (*i)->start() > frame) {
768 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
770 * @param frame Frame to look for.
771 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
772 * @param after Filled in with the position of the last `mark' after `frame' (or max_frames if none exists)
775 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
777 before = after = max_frames;
782 Glib::Mutex::Lock lm (lock);
786 std::list<nframes64_t> positions;
788 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
789 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
793 if (!(*i)->is_hidden()) {
794 if ((*i)->is_mark ()) {
795 positions.push_back ((*i)->start ());
797 positions.push_back ((*i)->start ());
798 positions.push_back ((*i)->end ());
803 if (positions.empty ()) {
809 std::list<nframes64_t>::iterator i = positions.begin ();
810 while (i != positions.end () && *i < frame) {
814 if (i == positions.end ()) {
815 /* run out of marks */
816 before = positions.back ();
822 if (i == positions.begin ()) {
832 Locations::session_range_location () const
834 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
835 if ((*i)->is_session_range()) {
836 return const_cast<Location*> (*i);
843 Locations::auto_loop_location () const
845 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
846 if ((*i)->is_auto_loop()) {
847 return const_cast<Location*> (*i);
854 Locations::auto_punch_location () const
856 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
857 if ((*i)->is_auto_punch()) {
858 return const_cast<Location*> (*i);
865 Locations::num_range_markers () const
868 Glib::Mutex::Lock lm (lock);
869 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
870 if ((*i)->is_range_marker()) {
878 Locations::get_location_by_id(PBD::ID id)
880 LocationList::iterator it;
881 for (it = locations.begin(); it != locations.end(); ++it)
882 if (id == (*it)->id())
889 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
891 Glib::Mutex::Lock lm (lock);
893 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
894 if ((flags == 0 || (*i)->matches (flags)) &&
895 ((*i)->start() >= start && (*i)->end() < end)) {