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 */
26 #include <sigc++/bind.h>
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>
41 using namespace ARDOUR;
45 Location::Location (const Location& other)
46 : _name (other._name),
47 _start (other._start),
51 /* start and end flags can never be copied, because there can only ever be one of each */
53 _flags = Flags (_flags & ~IsStart);
54 _flags = Flags (_flags & ~IsEnd);
56 /* copy is not locked even if original was */
61 Location::Location (const XMLNode& node)
63 if (set_state (node)) {
64 throw failed_constructor ();
69 Location::operator= (const Location& other)
76 _start = other._start;
78 _flags = other._flags;
80 /* copy is not locked even if original was */
84 /* "changed" not emitted on purpose */
90 Location::set_start (nframes_t s)
102 start_changed(this); /* EMIT SIGNAL */
103 end_changed(this); /* EMIT SIGNAL */
107 Session::StartTimeChanged (); /* EMIT SIGNAL */
108 AudioFileSource::set_header_position_offset ( s );
112 Session::EndTimeChanged (); /* EMIT SIGNAL */
118 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
124 start_changed(this); /* EMIT SIGNAL */
131 Location::set_end (nframes_t e)
141 start_changed(this); /* EMIT SIGNAL */
142 end_changed(this); /* EMIT SIGNAL */
145 Session::StartTimeChanged (); /* EMIT SIGNAL */
149 Session::EndTimeChanged (); /* EMIT SIGNAL */
156 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
162 end_changed(this); /* EMIT SIGNAL */
168 Location::set (nframes_t start, nframes_t end)
174 if (is_mark() && start != end) {
176 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
180 if (_start != start) {
182 start_changed(this); /* EMIT SIGNAL */
187 end_changed(this); /* EMIT SIGNAL */
193 Location::move_to (nframes_t pos)
201 _end = _start + length();
203 changed (this); /* EMIT SIGNAL */
210 Location::set_hidden (bool yn, void *src)
212 if (set_flag_internal (yn, IsHidden)) {
213 FlagsChanged (this, src); /* EMIT SIGNAL */
218 Location::set_cd (bool yn, void *src)
220 // XXX this really needs to be session start
221 // but its not available here - leave to GUI
224 error << _("You cannot put a CD marker at this position") << endmsg;
228 if (set_flag_internal (yn, IsCDMarker)) {
229 FlagsChanged (this, src); /* EMIT SIGNAL */
234 Location::set_is_end (bool yn, void *src)
236 if (set_flag_internal (yn, IsEnd)) {
237 FlagsChanged (this, src); /* EMIT SIGNAL */
242 Location::set_is_start (bool yn, void *src)
244 if (set_flag_internal (yn, IsStart)) {
245 FlagsChanged (this, src); /* EMIT SIGNAL */
250 Location::set_is_range_marker (bool yn, void *src)
252 if (set_flag_internal (yn, IsRangeMarker)) {
253 FlagsChanged (this, src); /* EMIT SIGNAL */
258 Location::set_auto_punch (bool yn, void *src)
260 if (is_mark() || _start == _end) {
264 if (set_flag_internal (yn, IsAutoPunch)) {
265 FlagsChanged (this, src); /* EMIT SIGNAL */
270 Location::set_auto_loop (bool yn, void *src)
272 if (is_mark() || _start == _end) {
276 if (set_flag_internal (yn, IsAutoLoop)) {
277 FlagsChanged (this, src); /* EMIT SIGNAL */
282 Location::set_flag_internal (bool yn, Flags flag)
285 if (!(_flags & flag)) {
286 _flags = Flags (_flags | flag);
291 _flags = Flags (_flags & ~flag);
299 Location::set_mark (bool yn)
301 /* This function is private, and so does not emit signals */
303 if (_start != _end) {
307 set_flag_internal (yn, IsMark);
312 Location::cd_info_node(const string & name, const string & value)
314 XMLNode* root = new XMLNode("CD-Info");
316 root->add_property("name", name);
317 root->add_property("value", value);
324 Location::get_state (void)
326 XMLNode *node = new XMLNode ("Location");
329 typedef map<string, string>::const_iterator CI;
331 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
332 node->add_child_nocopy(cd_info_node(m->first, m->second));
335 id().print (buf, sizeof (buf));
336 node->add_property("id", buf);
337 node->add_property ("name", name());
338 snprintf (buf, sizeof (buf), "%u", start());
339 node->add_property ("start", buf);
340 snprintf (buf, sizeof (buf), "%u", end());
341 node->add_property ("end", buf);
342 node->add_property ("flags", enum_2_string (_flags));
343 node->add_property ("locked", (_locked ? "yes" : "no"));
349 Location::set_state (const XMLNode& node)
351 const XMLProperty *prop;
353 XMLNodeList cd_list = node.children();
354 XMLNodeConstIterator cd_iter;
360 if (node.name() != "Location") {
361 error << _("incorrect XML node passed to Location::set_state") << endmsg;
365 if ((prop = node.property ("id")) == 0) {
366 warning << _("XML node for Location has no ID information") << endmsg;
368 _id = prop->value ();
371 if ((prop = node.property ("name")) == 0) {
372 error << _("XML node for Location has no name information") << endmsg;
376 set_name (prop->value());
378 if ((prop = node.property ("start")) == 0) {
379 error << _("XML node for Location has no start information") << endmsg;
383 /* can't use set_start() here, because _end
384 may make the value of _start illegal.
387 _start = atoi (prop->value().c_str());
389 if ((prop = node.property ("end")) == 0) {
390 error << _("XML node for Location has no end information") << endmsg;
394 _end = atoi (prop->value().c_str());
396 if ((prop = node.property ("flags")) == 0) {
397 error << _("XML node for Location has no flags information") << endmsg;
401 _flags = Flags (string_2_enum (prop->value(), _flags));
403 if ((prop = node.property ("locked")) != 0) {
404 _locked = (prop->value() == "yes");
409 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
413 if (cd_node->name() != "CD-Info") {
417 if ((prop = cd_node->property ("name")) != 0) {
418 cd_name = prop->value();
420 throw failed_constructor ();
423 if ((prop = cd_node->property ("value")) != 0) {
424 cd_value = prop->value();
426 throw failed_constructor ();
430 cd_info[cd_name] = cd_value;
434 changed(this); /* EMIT SIGNAL */
439 /*---------------------------------------------------------------------- */
441 Locations::Locations ()
444 current_location = 0;
447 Locations::~Locations ()
449 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
450 LocationList::iterator tmp = i;
458 Locations::set_current (Location *loc, bool want_lock)
464 Glib::Mutex::Lock lm (lock);
465 ret = set_current_unlocked (loc);
467 ret = set_current_unlocked (loc);
471 current_changed (current_location); /* EMIT SIGNAL */
477 Locations::next_available_name(string& result,string base)
479 LocationList::iterator i;
485 bool available[SUFFIX_MAX+1];
488 for (int k=1; k<SUFFIX_MAX; k++) {
492 for (i = locations.begin(); i != locations.end(); ++i) {
494 temp = location->name();
495 if (l && !temp.find(base,0)) {
496 suffix = atoi(temp.substr(l,3).c_str());
497 if (suffix) available[suffix] = false;
500 for (int k=1; k<=SUFFIX_MAX; k++) {
502 snprintf (buf, 31, "%d", k);
511 Locations::set_current_unlocked (Location *loc)
513 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
514 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
518 current_location = loc;
526 Glib::Mutex::Lock lm (lock);
528 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
530 LocationList::iterator tmp = i;
533 if (!(*i)->is_end() && !(*i)->is_start()) {
540 current_location = 0;
543 changed (); /* EMIT SIGNAL */
544 current_changed (0); /* EMIT SIGNAL */
548 Locations::clear_markers ()
551 Glib::Mutex::Lock lm (lock);
552 LocationList::iterator tmp;
554 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
558 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
566 changed (); /* EMIT SIGNAL */
570 Locations::clear_ranges ()
573 Glib::Mutex::Lock lm (lock);
574 LocationList::iterator tmp;
576 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
581 if (!(*i)->is_mark()) {
589 current_location = 0;
592 changed (); /* EMIT SIGNAL */
593 current_changed (0); /* EMIT SIGNAL */
597 Locations::add (Location *loc, bool make_current)
600 Glib::Mutex::Lock lm (lock);
601 locations.push_back (loc);
604 current_location = loc;
608 added (loc); /* EMIT SIGNAL */
611 current_changed (current_location); /* EMIT SIGNAL */
616 Locations::remove (Location *loc)
619 bool was_removed = false;
620 bool was_current = false;
621 LocationList::iterator i;
623 if (loc->is_end() || loc->is_start()) {
628 Glib::Mutex::Lock lm (lock);
630 for (i = locations.begin(); i != locations.end(); ++i) {
634 if (current_location == loc) {
635 current_location = 0;
645 removed (loc); /* EMIT SIGNAL */
648 current_changed (0); /* EMIT SIGNAL */
651 changed (); /* EMIT_SIGNAL */
656 Locations::location_changed (Location* loc)
658 changed (); /* EMIT SIGNAL */
662 Locations::get_state ()
664 XMLNode *node = new XMLNode ("Locations");
665 LocationList::iterator iter;
666 Glib::Mutex::Lock lm (lock);
668 for (iter = locations.begin(); iter != locations.end(); ++iter) {
669 node->add_child_nocopy ((*iter)->get_state ());
676 Locations::set_state (const XMLNode& node)
679 XMLNodeConstIterator niter;
681 if (node.name() != "Locations") {
682 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
686 nlist = node.children();
689 current_location = 0;
692 Glib::Mutex::Lock lm (lock);
694 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
698 Location *loc = new Location (**niter);
699 locations.push_back (loc);
702 catch (failed_constructor& err) {
703 error << _("could not load location from session file - ignored") << endmsg;
707 if (locations.size()) {
709 current_location = locations.front();
711 current_location = 0;
715 changed (); /* EMIT SIGNAL */
720 struct LocationStartEarlierComparison
722 bool operator() (Location *a, Location *b) {
723 return a->start() < b->start();
727 struct LocationStartLaterComparison
729 bool operator() (Location *a, Location *b) {
730 return a->start() > b->start();
735 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
740 Glib::Mutex::Lock lm (lock);
744 LocationStartLaterComparison cmp;
747 /* locs is now sorted latest..earliest */
749 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
750 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
753 if (!(*i)->is_hidden() && (*i)->start() < frame) {
762 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
767 Glib::Mutex::Lock lm (lock);
771 LocationStartEarlierComparison cmp;
774 /* locs is now sorted earliest..latest */
776 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
777 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
780 if (!(*i)->is_hidden() && (*i)->start() > frame) {
789 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
794 Glib::Mutex::Lock lm (lock);
798 LocationStartLaterComparison cmp;
801 /* locs is now sorted latest..earliest */
803 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
804 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
807 if (!(*i)->is_hidden()) {
808 if ((*i)->is_mark()) {
809 /* MARK: start == end */
810 if ((*i)->start() < frame) {
811 return (*i)->start();
814 /* RANGE: start != end, compare start and end */
815 if ((*i)->end() < frame) {
818 if ((*i)->start () < frame) {
819 return (*i)->start();
829 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
834 Glib::Mutex::Lock lm (lock);
838 LocationStartEarlierComparison cmp;
841 /* locs is now sorted earliest..latest */
843 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
844 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
847 if (!(*i)->is_hidden()) {
848 if ((*i)->is_mark()) {
849 /* MARK, start == end so just compare start */
850 if ((*i)->start() > frame) {
851 return (*i)->start();
854 /* RANGE, start != end, compare start and end */
855 if ((*i)->start() > frame ) {
856 return (*i)->start ();
858 if ((*i)->end() > frame) {
869 Locations::end_location () const
871 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
872 if ((*i)->is_end()) {
873 return const_cast<Location*> (*i);
880 Locations::start_location () const
882 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
883 if ((*i)->is_start()) {
884 return const_cast<Location*> (*i);
891 Locations::auto_loop_location () const
893 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
894 if ((*i)->is_auto_loop()) {
895 return const_cast<Location*> (*i);
902 Locations::auto_punch_location () const
904 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
905 if ((*i)->is_auto_punch()) {
906 return const_cast<Location*> (*i);
913 Locations::num_range_markers () const
916 Glib::Mutex::Lock lm (lock);
917 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
918 if ((*i)->is_range_marker()) {
926 Locations::get_location_by_id(PBD::ID id)
928 LocationList::iterator it;
929 for (it = locations.begin(); it != locations.end(); it++)
930 if (id == (*it)->id())
937 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
939 Glib::Mutex::Lock lm (lock);
941 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
942 if ((flags == 0 || (*i)->matches (flags)) &&
943 ((*i)->start() >= start && (*i)->end() < end)) {