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.
23 #include <cstdio> /* for sprintf */
27 #include <sigc++/bind.h>
29 #include <pbd/stl_delete.h>
30 #include <pbd/xml++.h>
32 #include <ardour/location.h>
33 #include <ardour/session.h>
34 #include <ardour/audiofilesource.h>
39 using namespace ARDOUR;
43 Location::Location (const Location& other)
44 : _name (other._name),
45 _start (other._start),
49 /* start and end flags can never be copied, because there can only ever be one of each */
51 _flags = Flags (_flags & ~IsStart);
52 _flags = Flags (_flags & ~IsEnd);
55 Location::Location (const XMLNode& node)
57 if (set_state (node)) {
58 throw failed_constructor ();
63 Location::operator= (const Location& other)
70 _start = other._start;
72 _flags = other._flags;
74 /* "changed" not emitted on purpose */
80 Location::set_start (nframes_t s)
88 start_changed(this); /* EMIT SIGNAL */
92 Session::StartTimeChanged (); /* EMIT SIGNAL */
93 AudioFileSource::set_header_position_offset ( s );
97 Session::EndTimeChanged (); /* EMIT SIGNAL */
103 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
109 start_changed(this); /* EMIT SIGNAL */
116 Location::set_end (nframes_t e)
122 end_changed(this); /* EMIT SIGNAL */
127 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
133 end_changed(this); /* EMIT SIGNAL */
139 Location::set (nframes_t start, nframes_t end)
141 if (is_mark() && start != end) {
143 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
147 if (_start != start) {
149 start_changed(this); /* EMIT SIGNAL */
154 end_changed(this); /* EMIT SIGNAL */
160 Location::set_hidden (bool yn, void *src)
162 if (set_flag_internal (yn, IsHidden)) {
163 FlagsChanged (this, src); /* EMIT SIGNAL */
168 Location::set_cd (bool yn, void *src)
170 if (set_flag_internal (yn, IsCDMarker)) {
171 FlagsChanged (this, src); /* EMIT SIGNAL */
176 Location::set_is_end (bool yn, void *src)
178 if (set_flag_internal (yn, IsEnd)) {
179 FlagsChanged (this, src); /* EMIT SIGNAL */
184 Location::set_is_start (bool yn, void *src)
186 if (set_flag_internal (yn, IsStart)) {
187 FlagsChanged (this, src); /* EMIT SIGNAL */
192 Location::set_auto_punch (bool yn, void *src)
194 if (is_mark() || _start == _end) {
198 if (set_flag_internal (yn, IsAutoPunch)) {
199 FlagsChanged (this, src); /* EMIT SIGNAL */
204 Location::set_auto_loop (bool yn, void *src)
206 if (is_mark() || _start == _end) {
210 if (set_flag_internal (yn, IsAutoLoop)) {
211 FlagsChanged (this, src); /* EMIT SIGNAL */
216 Location::set_flag_internal (bool yn, Flags flag)
219 if (!(_flags & flag)) {
233 Location::set_mark (bool yn)
235 /* This function is private, and so does not emit signals */
237 if (_start != _end) {
241 set_flag_internal (yn, IsMark);
246 Location::cd_info_node(const string & name, const string & value)
248 XMLNode* root = new XMLNode("CD-Info");
250 root->add_property("name", name);
251 root->add_property("value", value);
258 Location::get_state (void)
260 XMLNode *node = new XMLNode ("Location");
263 typedef map<string, string>::const_iterator CI;
265 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
266 node->add_child_nocopy(cd_info_node(m->first, m->second));
269 id().print (buf, sizeof (buf));
270 node->add_property("id", buf);
271 node->add_property ("name", name());
272 snprintf (buf, sizeof (buf), "%u", start());
273 node->add_property ("start", buf);
274 snprintf (buf, sizeof (buf), "%u", end());
275 node->add_property ("end", buf);
276 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
277 node->add_property ("flags", buf);
283 Location::set_state (const XMLNode& node)
285 const XMLProperty *prop;
287 XMLNodeList cd_list = node.children();
288 XMLNodeConstIterator cd_iter;
294 if (node.name() != "Location") {
295 error << _("incorrect XML node passed to Location::set_state") << endmsg;
299 if ((prop = node.property ("id")) == 0) {
300 warning << _("XML node for Location has no ID information") << endmsg;
302 _id = prop->value ();
305 if ((prop = node.property ("name")) == 0) {
306 error << _("XML node for Location has no name information") << endmsg;
310 set_name (prop->value());
312 if ((prop = node.property ("start")) == 0) {
313 error << _("XML node for Location has no start information") << endmsg;
317 /* can't use set_start() here, because _end
318 may make the value of _start illegal.
321 _start = atoi (prop->value().c_str());
323 if ((prop = node.property ("end")) == 0) {
324 error << _("XML node for Location has no end information") << endmsg;
328 _end = atoi (prop->value().c_str());
332 if ((prop = node.property ("flags")) == 0) {
333 error << _("XML node for Location has no flags information") << endmsg;
337 _flags = Flags (atoi (prop->value().c_str()));
339 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
343 if (cd_node->name() != "CD-Info") {
347 if ((prop = cd_node->property ("name")) != 0) {
348 cd_name = prop->value();
350 throw failed_constructor ();
353 if ((prop = cd_node->property ("value")) != 0) {
354 cd_value = prop->value();
356 throw failed_constructor ();
360 cd_info[cd_name] = cd_value;
364 changed(this); /* EMIT SIGNAL */
369 /*---------------------------------------------------------------------- */
371 Locations::Locations ()
374 current_location = 0;
376 save_state (_("initial"));
380 Locations::~Locations ()
382 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
383 LocationList::iterator tmp = i;
391 std::set<Location*> all_locations;
394 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
396 State* lstate = dynamic_cast<State*> (*siter);
398 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
399 all_locations.insert (*liter);
402 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
403 all_locations.insert (*siter);
407 set_delete (&all_locations);
412 Locations::set_current (Location *loc, bool want_lock)
418 Glib::Mutex::Lock lm (lock);
419 ret = set_current_unlocked (loc);
421 ret = set_current_unlocked (loc);
425 current_changed (current_location); /* EMIT SIGNAL */
431 Locations::set_current_unlocked (Location *loc)
433 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
434 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
438 current_location = loc;
446 Glib::Mutex::Lock lm (lock);
447 LocationList::iterator tmp;
448 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
451 if (!(*i)->is_end() && !(*i)->is_start()) {
458 current_location = 0;
462 save_state (_("clear"));
465 changed (); /* EMIT SIGNAL */
466 current_changed (0); /* EMIT SIGNAL */
470 Locations::clear_markers ()
473 Glib::Mutex::Lock lm (lock);
474 LocationList::iterator tmp;
476 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
480 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
489 save_state (_("clear markers"));
492 changed (); /* EMIT SIGNAL */
496 Locations::clear_ranges ()
499 Glib::Mutex::Lock lm (lock);
500 LocationList::iterator tmp;
502 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
507 if (!(*i)->is_mark()) {
515 current_location = 0;
519 save_state (_("clear ranges"));
522 changed (); /* EMIT SIGNAL */
523 current_changed (0); /* EMIT SIGNAL */
527 Locations::add (Location *loc, bool make_current)
530 Glib::Mutex::Lock lm (lock);
531 locations.push_back (loc);
534 current_location = loc;
539 save_state (_("add"));
542 added (loc); /* EMIT SIGNAL */
545 current_changed (current_location); /* EMIT SIGNAL */
550 Locations::remove (Location *loc)
553 bool was_removed = false;
554 bool was_current = false;
555 LocationList::iterator i;
557 if (loc->is_end() || loc->is_start()) {
562 Glib::Mutex::Lock lm (lock);
564 for (i = locations.begin(); i != locations.end(); ++i) {
568 if (current_location == loc) {
569 current_location = 0;
579 save_state (_("remove"));
582 removed (loc); /* EMIT SIGNAL */
585 current_changed (0); /* EMIT SIGNAL */
588 changed (); /* EMIT_SIGNAL */
593 Locations::location_changed (Location* loc)
596 save_state (X_("location changed"));
598 changed (); /* EMIT SIGNAL */
602 Locations::get_state ()
604 XMLNode *node = new XMLNode ("Locations");
605 LocationList::iterator iter;
606 Glib::Mutex::Lock lm (lock);
608 for (iter = locations.begin(); iter != locations.end(); ++iter) {
609 node->add_child_nocopy ((*iter)->get_state ());
616 Locations::set_state (const XMLNode& node)
619 XMLNodeConstIterator niter;
621 if (node.name() != "Locations") {
622 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
626 nlist = node.children();
629 Glib::Mutex::Lock lm (lock);
631 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
635 Location *loc = new Location (**niter);
636 locations.push_back (loc);
639 catch (failed_constructor& err) {
640 error << _("could not load location from session file - ignored") << endmsg;
644 if (locations.size()) {
646 current_location = locations.front();
648 current_location = 0;
652 changed (); /* EMIT SIGNAL */
657 struct LocationStartEarlierComparison
659 bool operator() (Location *a, Location *b) {
660 return a->start() < b->start();
664 struct LocationStartLaterComparison
666 bool operator() (Location *a, Location *b) {
667 return a->start() > b->start();
672 Locations::first_location_before (nframes_t frame)
677 Glib::Mutex::Lock lm (lock);
681 LocationStartLaterComparison cmp;
684 /* locs is now sorted latest..earliest */
686 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
687 if (!(*i)->is_hidden() && (*i)->start() < frame) {
696 Locations::first_location_after (nframes_t frame)
701 Glib::Mutex::Lock lm (lock);
705 LocationStartEarlierComparison cmp;
708 /* locs is now sorted earliest..latest */
710 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
711 if (!(*i)->is_hidden() && (*i)->start() > frame) {
720 Locations::first_mark_before (nframes_t frame)
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 (!(*i)->is_hidden()) {
736 if ((*i)->is_mark()) {
737 /* MARK: start == end */
738 if ((*i)->start() < frame) {
739 return (*i)->start();
742 /* RANGE: start != end, compare start and end */
743 if ((*i)->end() < frame) {
746 if ((*i)->start () < frame) {
747 return (*i)->start();
757 Locations::first_mark_after (nframes_t frame)
762 Glib::Mutex::Lock lm (lock);
766 LocationStartEarlierComparison cmp;
769 /* locs is now sorted earliest..latest */
771 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
772 if (!(*i)->is_hidden()) {
773 if ((*i)->is_mark()) {
774 /* MARK, start == end so just compare start */
775 if ((*i)->start() > frame) {
776 return (*i)->start();
779 /* RANGE, start != end, compare start and end */
780 if ((*i)->start() > frame ) {
781 return (*i)->start ();
783 if ((*i)->end() > frame) {
794 Locations::end_location () const
796 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
797 if ((*i)->is_end()) {
798 return const_cast<Location*> (*i);
805 Locations::start_location () const
807 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
808 if ((*i)->is_start()) {
809 return const_cast<Location*> (*i);
816 Locations::auto_loop_location () const
818 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
819 if ((*i)->is_auto_loop()) {
820 return const_cast<Location*> (*i);
827 Locations::auto_punch_location () const
829 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
830 if ((*i)->is_auto_punch()) {
831 return const_cast<Location*> (*i);
839 Locations::state_factory (std::string why) const
841 State* state = new State (why);
843 state->locations = locations;
845 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
846 state->states.push_back (new Location (**i));
853 Locations::restore_state (StateManager::State& state)
856 Glib::Mutex::Lock lm (lock);
857 State* lstate = dynamic_cast<State*> (&state);
859 locations = lstate->locations;
860 LocationList& states = lstate->states;
861 LocationList::iterator l, s;
863 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
872 Locations::get_memento () const
874 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
879 Locations::num_range_markers () const
882 Glib::Mutex::Lock lm (lock);
883 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
884 if ((*i)->is_range_marker()) {
892 Locations::get_location_by_id(PBD::ID id)
894 LocationList::iterator it;
895 for (it = locations.begin(); it != locations.end(); it++)
896 if (id == (*it)->id())