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_auto_punch (bool yn, void *src)
252 if (is_mark() || _start == _end) {
256 if (set_flag_internal (yn, IsAutoPunch)) {
257 FlagsChanged (this, src); /* EMIT SIGNAL */
262 Location::set_auto_loop (bool yn, void *src)
264 if (is_mark() || _start == _end) {
268 if (set_flag_internal (yn, IsAutoLoop)) {
269 FlagsChanged (this, src); /* EMIT SIGNAL */
274 Location::set_flag_internal (bool yn, Flags flag)
277 if (!(_flags & flag)) {
278 _flags = Flags (_flags | flag);
283 _flags = Flags (_flags & ~flag);
291 Location::set_mark (bool yn)
293 /* This function is private, and so does not emit signals */
295 if (_start != _end) {
299 set_flag_internal (yn, IsMark);
304 Location::cd_info_node(const string & name, const string & value)
306 XMLNode* root = new XMLNode("CD-Info");
308 root->add_property("name", name);
309 root->add_property("value", value);
316 Location::get_state (void)
318 XMLNode *node = new XMLNode ("Location");
321 typedef map<string, string>::const_iterator CI;
323 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
324 node->add_child_nocopy(cd_info_node(m->first, m->second));
327 id().print (buf, sizeof (buf));
328 node->add_property("id", buf);
329 node->add_property ("name", name());
330 snprintf (buf, sizeof (buf), "%u", start());
331 node->add_property ("start", buf);
332 snprintf (buf, sizeof (buf), "%u", end());
333 node->add_property ("end", buf);
334 node->add_property ("flags", enum_2_string (_flags));
335 node->add_property ("locked", (_locked ? "yes" : "no"));
341 Location::set_state (const XMLNode& node)
343 const XMLProperty *prop;
345 XMLNodeList cd_list = node.children();
346 XMLNodeConstIterator cd_iter;
352 if (node.name() != "Location") {
353 error << _("incorrect XML node passed to Location::set_state") << endmsg;
357 if ((prop = node.property ("id")) == 0) {
358 warning << _("XML node for Location has no ID information") << endmsg;
360 _id = prop->value ();
363 if ((prop = node.property ("name")) == 0) {
364 error << _("XML node for Location has no name information") << endmsg;
368 set_name (prop->value());
370 if ((prop = node.property ("start")) == 0) {
371 error << _("XML node for Location has no start information") << endmsg;
375 /* can't use set_start() here, because _end
376 may make the value of _start illegal.
379 _start = atoi (prop->value().c_str());
381 if ((prop = node.property ("end")) == 0) {
382 error << _("XML node for Location has no end information") << endmsg;
386 _end = atoi (prop->value().c_str());
388 if ((prop = node.property ("flags")) == 0) {
389 error << _("XML node for Location has no flags information") << endmsg;
393 _flags = Flags (string_2_enum (prop->value(), _flags));
395 if ((prop = node.property ("locked")) != 0) {
396 _locked = (prop->value() == "yes");
401 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
405 if (cd_node->name() != "CD-Info") {
409 if ((prop = cd_node->property ("name")) != 0) {
410 cd_name = prop->value();
412 throw failed_constructor ();
415 if ((prop = cd_node->property ("value")) != 0) {
416 cd_value = prop->value();
418 throw failed_constructor ();
422 cd_info[cd_name] = cd_value;
426 changed(this); /* EMIT SIGNAL */
431 /*---------------------------------------------------------------------- */
433 Locations::Locations ()
436 current_location = 0;
439 Locations::~Locations ()
441 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
442 LocationList::iterator tmp = i;
450 Locations::set_current (Location *loc, bool want_lock)
456 Glib::Mutex::Lock lm (lock);
457 ret = set_current_unlocked (loc);
459 ret = set_current_unlocked (loc);
463 current_changed (current_location); /* EMIT SIGNAL */
469 Locations::next_available_name(string& result,string base)
471 LocationList::iterator i;
477 bool available[SUFFIX_MAX+1];
480 for (int k=1; k<SUFFIX_MAX; k++) {
484 for (i = locations.begin(); i != locations.end(); ++i) {
486 temp = location->name();
487 if (l && !temp.find(base,0)) {
488 suffix = atoi(temp.substr(l,3).c_str());
489 if (suffix) available[suffix] = false;
492 for (int k=1; k<=SUFFIX_MAX; k++) {
494 snprintf (buf, 31, "%d", k);
503 Locations::set_current_unlocked (Location *loc)
505 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
506 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
510 current_location = loc;
518 Glib::Mutex::Lock lm (lock);
520 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
522 LocationList::iterator tmp = i;
525 if (!(*i)->is_end() && !(*i)->is_start()) {
532 current_location = 0;
535 changed (); /* EMIT SIGNAL */
536 current_changed (0); /* EMIT SIGNAL */
540 Locations::clear_markers ()
543 Glib::Mutex::Lock lm (lock);
544 LocationList::iterator tmp;
546 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
550 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
558 changed (); /* EMIT SIGNAL */
562 Locations::clear_ranges ()
565 Glib::Mutex::Lock lm (lock);
566 LocationList::iterator tmp;
568 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
573 if (!(*i)->is_mark()) {
581 current_location = 0;
584 changed (); /* EMIT SIGNAL */
585 current_changed (0); /* EMIT SIGNAL */
589 Locations::add (Location *loc, bool make_current)
592 Glib::Mutex::Lock lm (lock);
593 locations.push_back (loc);
596 current_location = loc;
600 added (loc); /* EMIT SIGNAL */
603 current_changed (current_location); /* EMIT SIGNAL */
608 Locations::remove (Location *loc)
611 bool was_removed = false;
612 bool was_current = false;
613 LocationList::iterator i;
615 if (loc->is_end() || loc->is_start()) {
620 Glib::Mutex::Lock lm (lock);
622 for (i = locations.begin(); i != locations.end(); ++i) {
626 if (current_location == loc) {
627 current_location = 0;
637 removed (loc); /* EMIT SIGNAL */
640 current_changed (0); /* EMIT SIGNAL */
643 changed (); /* EMIT_SIGNAL */
648 Locations::location_changed (Location* loc)
650 changed (); /* EMIT SIGNAL */
654 Locations::get_state ()
656 XMLNode *node = new XMLNode ("Locations");
657 LocationList::iterator iter;
658 Glib::Mutex::Lock lm (lock);
660 for (iter = locations.begin(); iter != locations.end(); ++iter) {
661 node->add_child_nocopy ((*iter)->get_state ());
668 Locations::set_state (const XMLNode& node)
671 XMLNodeConstIterator niter;
673 if (node.name() != "Locations") {
674 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
678 nlist = node.children();
681 current_location = 0;
684 Glib::Mutex::Lock lm (lock);
686 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
690 Location *loc = new Location (**niter);
691 locations.push_back (loc);
694 catch (failed_constructor& err) {
695 error << _("could not load location from session file - ignored") << endmsg;
699 if (locations.size()) {
701 current_location = locations.front();
703 current_location = 0;
707 changed (); /* EMIT SIGNAL */
712 struct LocationStartEarlierComparison
714 bool operator() (Location *a, Location *b) {
715 return a->start() < b->start();
719 struct LocationStartLaterComparison
721 bool operator() (Location *a, Location *b) {
722 return a->start() > b->start();
727 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
732 Glib::Mutex::Lock lm (lock);
736 LocationStartLaterComparison cmp;
739 /* locs is now sorted latest..earliest */
741 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
742 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
745 if (!(*i)->is_hidden() && (*i)->start() < frame) {
754 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
759 Glib::Mutex::Lock lm (lock);
763 LocationStartEarlierComparison cmp;
766 /* locs is now sorted earliest..latest */
768 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
769 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
772 if (!(*i)->is_hidden() && (*i)->start() > frame) {
781 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
786 Glib::Mutex::Lock lm (lock);
790 LocationStartLaterComparison cmp;
793 /* locs is now sorted latest..earliest */
795 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
796 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
799 if (!(*i)->is_hidden()) {
800 if ((*i)->is_mark()) {
801 /* MARK: start == end */
802 if ((*i)->start() < frame) {
803 return (*i)->start();
806 /* RANGE: start != end, compare start and end */
807 if ((*i)->end() < frame) {
810 if ((*i)->start () < frame) {
811 return (*i)->start();
821 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
826 Glib::Mutex::Lock lm (lock);
830 LocationStartEarlierComparison cmp;
833 /* locs is now sorted earliest..latest */
835 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
836 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
839 if (!(*i)->is_hidden()) {
840 if ((*i)->is_mark()) {
841 /* MARK, start == end so just compare start */
842 if ((*i)->start() > frame) {
843 return (*i)->start();
846 /* RANGE, start != end, compare start and end */
847 if ((*i)->start() > frame ) {
848 return (*i)->start ();
850 if ((*i)->end() > frame) {
861 Locations::end_location () const
863 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
864 if ((*i)->is_end()) {
865 return const_cast<Location*> (*i);
872 Locations::start_location () const
874 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
875 if ((*i)->is_start()) {
876 return const_cast<Location*> (*i);
883 Locations::auto_loop_location () const
885 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
886 if ((*i)->is_auto_loop()) {
887 return const_cast<Location*> (*i);
894 Locations::auto_punch_location () const
896 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
897 if ((*i)->is_auto_punch()) {
898 return const_cast<Location*> (*i);
905 Locations::num_range_markers () const
908 Glib::Mutex::Lock lm (lock);
909 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
910 if ((*i)->is_range_marker()) {
918 Locations::get_location_by_id(PBD::ID id)
920 LocationList::iterator it;
921 for (it = locations.begin(); it != locations.end(); it++)
922 if (id == (*it)->id())