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 */
106 Session::StartTimeChanged (); /* EMIT SIGNAL */
107 AudioFileSource::set_header_position_offset ( s );
111 Session::EndTimeChanged (); /* EMIT SIGNAL */
117 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
123 start_changed(this); /* EMIT SIGNAL */
130 Location::set_end (nframes_t e)
140 end_changed(this); /* EMIT SIGNAL */
145 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
151 end_changed(this); /* EMIT SIGNAL */
157 Location::set (nframes_t start, nframes_t end)
163 if (is_mark() && start != end) {
165 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
169 if (_start != start) {
171 start_changed(this); /* EMIT SIGNAL */
176 end_changed(this); /* EMIT SIGNAL */
182 Location::move_to (nframes_t pos)
190 _end = _start + length();
192 changed (this); /* EMIT SIGNAL */
199 Location::set_hidden (bool yn, void *src)
201 if (set_flag_internal (yn, IsHidden)) {
202 FlagsChanged (this, src); /* EMIT SIGNAL */
207 Location::set_cd (bool yn, void *src)
209 // XXX this really needs to be session start
210 // but its not available here - leave to GUI
213 error << _("You cannot put a CD marker at this position") << endmsg;
217 if (set_flag_internal (yn, IsCDMarker)) {
218 FlagsChanged (this, src); /* EMIT SIGNAL */
223 Location::set_is_end (bool yn, void *src)
225 if (set_flag_internal (yn, IsEnd)) {
226 FlagsChanged (this, src); /* EMIT SIGNAL */
231 Location::set_is_start (bool yn, void *src)
233 if (set_flag_internal (yn, IsStart)) {
234 FlagsChanged (this, src); /* EMIT SIGNAL */
239 Location::set_auto_punch (bool yn, void *src)
241 if (is_mark() || _start == _end) {
245 if (set_flag_internal (yn, IsAutoPunch)) {
246 FlagsChanged (this, src); /* EMIT SIGNAL */
251 Location::set_auto_loop (bool yn, void *src)
253 if (is_mark() || _start == _end) {
257 if (set_flag_internal (yn, IsAutoLoop)) {
258 FlagsChanged (this, src); /* EMIT SIGNAL */
263 Location::set_flag_internal (bool yn, Flags flag)
266 if (!(_flags & flag)) {
267 _flags = Flags (_flags | flag);
272 _flags = Flags (_flags & ~flag);
280 Location::set_mark (bool yn)
282 /* This function is private, and so does not emit signals */
284 if (_start != _end) {
288 set_flag_internal (yn, IsMark);
293 Location::cd_info_node(const string & name, const string & value)
295 XMLNode* root = new XMLNode("CD-Info");
297 root->add_property("name", name);
298 root->add_property("value", value);
305 Location::get_state (void)
307 XMLNode *node = new XMLNode ("Location");
310 typedef map<string, string>::const_iterator CI;
312 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
313 node->add_child_nocopy(cd_info_node(m->first, m->second));
316 id().print (buf, sizeof (buf));
317 node->add_property("id", buf);
318 node->add_property ("name", name());
319 snprintf (buf, sizeof (buf), "%u", start());
320 node->add_property ("start", buf);
321 snprintf (buf, sizeof (buf), "%u", end());
322 node->add_property ("end", buf);
323 node->add_property ("flags", enum_2_string (_flags));
324 node->add_property ("locked", (_locked ? "yes" : "no"));
330 Location::set_state (const XMLNode& node)
332 const XMLProperty *prop;
334 XMLNodeList cd_list = node.children();
335 XMLNodeConstIterator cd_iter;
341 if (node.name() != "Location") {
342 error << _("incorrect XML node passed to Location::set_state") << endmsg;
346 if ((prop = node.property ("id")) == 0) {
347 warning << _("XML node for Location has no ID information") << endmsg;
349 _id = prop->value ();
352 if ((prop = node.property ("name")) == 0) {
353 error << _("XML node for Location has no name information") << endmsg;
357 set_name (prop->value());
359 if ((prop = node.property ("start")) == 0) {
360 error << _("XML node for Location has no start information") << endmsg;
364 /* can't use set_start() here, because _end
365 may make the value of _start illegal.
368 _start = atoi (prop->value().c_str());
370 if ((prop = node.property ("end")) == 0) {
371 error << _("XML node for Location has no end information") << endmsg;
375 _end = atoi (prop->value().c_str());
377 if ((prop = node.property ("flags")) == 0) {
378 error << _("XML node for Location has no flags information") << endmsg;
382 _flags = Flags (string_2_enum (prop->value(), _flags));
384 if ((prop = node.property ("locked")) != 0) {
385 _locked = (prop->value() == "yes");
390 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
394 if (cd_node->name() != "CD-Info") {
398 if ((prop = cd_node->property ("name")) != 0) {
399 cd_name = prop->value();
401 throw failed_constructor ();
404 if ((prop = cd_node->property ("value")) != 0) {
405 cd_value = prop->value();
407 throw failed_constructor ();
411 cd_info[cd_name] = cd_value;
415 changed(this); /* EMIT SIGNAL */
420 /*---------------------------------------------------------------------- */
422 Locations::Locations ()
425 current_location = 0;
428 Locations::~Locations ()
430 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
431 LocationList::iterator tmp = i;
439 Locations::set_current (Location *loc, bool want_lock)
445 Glib::Mutex::Lock lm (lock);
446 ret = set_current_unlocked (loc);
448 ret = set_current_unlocked (loc);
452 current_changed (current_location); /* EMIT SIGNAL */
458 Locations::next_available_name(string& result,string base)
460 LocationList::iterator i;
466 bool available[SUFFIX_MAX+1];
469 for (int k=1; k<SUFFIX_MAX; k++) {
473 for (i = locations.begin(); i != locations.end(); ++i) {
475 temp = location->name();
476 if (l && !temp.find(base,0)) {
477 suffix = atoi(temp.substr(l,3).c_str());
478 if (suffix) available[suffix] = false;
481 for (int k=1; k<=SUFFIX_MAX; k++) {
483 snprintf (buf, 31, "%d", k);
492 Locations::set_current_unlocked (Location *loc)
494 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
495 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
499 current_location = loc;
507 Glib::Mutex::Lock lm (lock);
509 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
511 LocationList::iterator tmp = i;
514 if (!(*i)->is_end() && !(*i)->is_start()) {
521 current_location = 0;
524 changed (); /* EMIT SIGNAL */
525 current_changed (0); /* EMIT SIGNAL */
529 Locations::clear_markers ()
532 Glib::Mutex::Lock lm (lock);
533 LocationList::iterator tmp;
535 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
539 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
547 changed (); /* EMIT SIGNAL */
551 Locations::clear_ranges ()
554 Glib::Mutex::Lock lm (lock);
555 LocationList::iterator tmp;
557 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
562 if (!(*i)->is_mark()) {
570 current_location = 0;
573 changed (); /* EMIT SIGNAL */
574 current_changed (0); /* EMIT SIGNAL */
578 Locations::add (Location *loc, bool make_current)
581 Glib::Mutex::Lock lm (lock);
582 locations.push_back (loc);
585 current_location = loc;
589 added (loc); /* EMIT SIGNAL */
592 current_changed (current_location); /* EMIT SIGNAL */
597 Locations::remove (Location *loc)
600 bool was_removed = false;
601 bool was_current = false;
602 LocationList::iterator i;
604 if (loc->is_end() || loc->is_start()) {
609 Glib::Mutex::Lock lm (lock);
611 for (i = locations.begin(); i != locations.end(); ++i) {
615 if (current_location == loc) {
616 current_location = 0;
626 removed (loc); /* EMIT SIGNAL */
629 current_changed (0); /* EMIT SIGNAL */
632 changed (); /* EMIT_SIGNAL */
637 Locations::location_changed (Location* loc)
639 changed (); /* EMIT SIGNAL */
643 Locations::get_state ()
645 XMLNode *node = new XMLNode ("Locations");
646 LocationList::iterator iter;
647 Glib::Mutex::Lock lm (lock);
649 for (iter = locations.begin(); iter != locations.end(); ++iter) {
650 node->add_child_nocopy ((*iter)->get_state ());
657 Locations::set_state (const XMLNode& node)
660 XMLNodeConstIterator niter;
662 if (node.name() != "Locations") {
663 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
667 nlist = node.children();
670 current_location = 0;
673 Glib::Mutex::Lock lm (lock);
675 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
679 Location *loc = new Location (**niter);
680 locations.push_back (loc);
683 catch (failed_constructor& err) {
684 error << _("could not load location from session file - ignored") << endmsg;
688 if (locations.size()) {
690 current_location = locations.front();
692 current_location = 0;
696 changed (); /* EMIT SIGNAL */
701 struct LocationStartEarlierComparison
703 bool operator() (Location *a, Location *b) {
704 return a->start() < b->start();
708 struct LocationStartLaterComparison
710 bool operator() (Location *a, Location *b) {
711 return a->start() > b->start();
716 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
721 Glib::Mutex::Lock lm (lock);
725 LocationStartLaterComparison cmp;
728 /* locs is now sorted latest..earliest */
730 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
731 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
734 if (!(*i)->is_hidden() && (*i)->start() < frame) {
743 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
748 Glib::Mutex::Lock lm (lock);
752 LocationStartEarlierComparison cmp;
755 /* locs is now sorted earliest..latest */
757 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
758 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
761 if (!(*i)->is_hidden() && (*i)->start() > frame) {
770 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
775 Glib::Mutex::Lock lm (lock);
779 LocationStartLaterComparison cmp;
782 /* locs is now sorted latest..earliest */
784 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
785 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
788 if (!(*i)->is_hidden()) {
789 if ((*i)->is_mark()) {
790 /* MARK: start == end */
791 if ((*i)->start() < frame) {
792 return (*i)->start();
795 /* RANGE: start != end, compare start and end */
796 if ((*i)->end() < frame) {
799 if ((*i)->start () < frame) {
800 return (*i)->start();
810 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
815 Glib::Mutex::Lock lm (lock);
819 LocationStartEarlierComparison cmp;
822 /* locs is now sorted earliest..latest */
824 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
825 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
828 if (!(*i)->is_hidden()) {
829 if ((*i)->is_mark()) {
830 /* MARK, start == end so just compare start */
831 if ((*i)->start() > frame) {
832 return (*i)->start();
835 /* RANGE, start != end, compare start and end */
836 if ((*i)->start() > frame ) {
837 return (*i)->start ();
839 if ((*i)->end() > frame) {
850 Locations::end_location () const
852 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
853 if ((*i)->is_end()) {
854 return const_cast<Location*> (*i);
861 Locations::start_location () const
863 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
864 if ((*i)->is_start()) {
865 return const_cast<Location*> (*i);
872 Locations::auto_loop_location () const
874 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
875 if ((*i)->is_auto_loop()) {
876 return const_cast<Location*> (*i);
883 Locations::auto_punch_location () const
885 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
886 if ((*i)->is_auto_punch()) {
887 return const_cast<Location*> (*i);
894 Locations::num_range_markers () const
897 Glib::Mutex::Lock lm (lock);
898 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
899 if ((*i)->is_range_marker()) {
907 Locations::get_location_by_id(PBD::ID id)
909 LocationList::iterator it;
910 for (it = locations.begin(); it != locations.end(); it++)
911 if (id == (*it)->id())