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/audiofilesource.h>
38 using namespace ARDOUR;
42 Location::Location (const Location& other)
43 : _name (other._name),
44 _start (other._start),
48 /* start and end flags can never be copied, because there can only ever be one of each */
50 _flags = Flags (_flags & ~IsStart);
51 _flags = Flags (_flags & ~IsEnd);
54 Location::Location (const XMLNode& node)
56 if (set_state (node)) {
57 throw failed_constructor ();
62 Location::operator= (const Location& other)
69 _start = other._start;
71 _flags = other._flags;
73 /* "changed" not emitted on purpose */
79 Location::set_start (jack_nframes_t s)
85 start_changed(this); /* EMIT SIGNAL */
87 AudioFileSource::set_header_position_offset ( s );
93 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
99 start_changed(this); /* EMIT SIGNAL */
106 Location::set_end (jack_nframes_t e)
112 end_changed(this); /* EMIT SIGNAL */
117 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
123 end_changed(this); /* EMIT SIGNAL */
129 Location::set (jack_nframes_t start, jack_nframes_t end)
131 if (is_mark() && start != end) {
133 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
137 if (_start != start) {
139 start_changed(this); /* EMIT SIGNAL */
144 end_changed(this); /* EMIT SIGNAL */
150 Location::set_hidden (bool yn, void *src)
152 if (set_flag_internal (yn, IsHidden)) {
153 FlagsChanged (this, src); /* EMIT SIGNAL */
158 Location::set_cd (bool yn, void *src)
160 if (set_flag_internal (yn, IsCDMarker)) {
161 FlagsChanged (this, src); /* EMIT SIGNAL */
166 Location::set_is_end (bool yn, void *src)
168 if (set_flag_internal (yn, IsEnd)) {
169 FlagsChanged (this, src); /* EMIT SIGNAL */
174 Location::set_is_start (bool yn, void *src)
176 if (set_flag_internal (yn, IsStart)) {
177 FlagsChanged (this, src); /* EMIT SIGNAL */
182 Location::set_auto_punch (bool yn, void *src)
184 if (is_mark() || _start == _end) {
188 if (set_flag_internal (yn, IsAutoPunch)) {
189 FlagsChanged (this, src); /* EMIT SIGNAL */
194 Location::set_auto_loop (bool yn, void *src)
196 if (is_mark() || _start == _end) {
200 if (set_flag_internal (yn, IsAutoLoop)) {
201 FlagsChanged (this, src); /* EMIT SIGNAL */
206 Location::set_flag_internal (bool yn, Flags flag)
209 if (!(_flags & flag)) {
223 Location::set_mark (bool yn)
225 /* This function is private, and so does not emit signals */
227 if (_start != _end) {
231 set_flag_internal (yn, IsMark);
236 Location::cd_info_node(const string & name, const string & value)
238 XMLNode* root = new XMLNode("CD-Info");
240 root->add_property("name", name);
241 root->add_property("value", value);
248 Location::get_state (void)
250 XMLNode *node = new XMLNode ("Location");
253 typedef map<string, string>::const_iterator CI;
255 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
256 node->add_child_nocopy(cd_info_node(m->first, m->second));
260 node->add_property("id", buf);
261 node->add_property ("name", name());
262 snprintf (buf, sizeof (buf), "%u", start());
263 node->add_property ("start", buf);
264 snprintf (buf, sizeof (buf), "%u", end());
265 node->add_property ("end", buf);
266 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
267 node->add_property ("flags", buf);
273 Location::set_state (const XMLNode& node)
275 const XMLProperty *prop;
277 XMLNodeList cd_list = node.children();
278 XMLNodeConstIterator cd_iter;
284 if (node.name() != "Location") {
285 error << _("incorrect XML node passed to Location::set_state") << endmsg;
289 if ((prop = node.property ("id")) == 0) {
290 warning << _("XML node for Location has no ID information") << endmsg;
292 _id = prop->value ();
295 if ((prop = node.property ("name")) == 0) {
296 error << _("XML node for Location has no name information") << endmsg;
300 set_name (prop->value());
302 if ((prop = node.property ("start")) == 0) {
303 error << _("XML node for Location has no start information") << endmsg;
307 /* can't use set_start() here, because _end
308 may make the value of _start illegal.
311 _start = atoi (prop->value().c_str());
313 if ((prop = node.property ("end")) == 0) {
314 error << _("XML node for Location has no end information") << endmsg;
318 _end = atoi (prop->value().c_str());
322 if ((prop = node.property ("flags")) == 0) {
323 error << _("XML node for Location has no flags information") << endmsg;
327 _flags = Flags (atoi (prop->value().c_str()));
329 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
333 if (cd_node->name() != "CD-Info") {
337 if ((prop = cd_node->property ("name")) != 0) {
338 cd_name = prop->value();
340 throw failed_constructor ();
343 if ((prop = cd_node->property ("value")) != 0) {
344 cd_value = prop->value();
346 throw failed_constructor ();
350 cd_info[cd_name] = cd_value;
354 changed(this); /* EMIT SIGNAL */
359 /*---------------------------------------------------------------------- */
361 Locations::Locations ()
364 current_location = 0;
365 save_state (_("initial"));
368 Locations::~Locations ()
370 std::set<Location*> all_locations;
372 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
374 State* lstate = dynamic_cast<State*> (*siter);
376 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
377 all_locations.insert (*liter);
380 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
381 all_locations.insert (*siter);
385 set_delete (&all_locations);
389 Locations::set_current (Location *loc, bool want_lock)
395 Glib::Mutex::Lock lm (lock);
396 ret = set_current_unlocked (loc);
398 ret = set_current_unlocked (loc);
402 current_changed (current_location); /* EMIT SIGNAL */
408 Locations::set_current_unlocked (Location *loc)
410 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
411 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
415 current_location = loc;
423 Glib::Mutex::Lock lm (lock);
424 LocationList::iterator tmp;
425 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
428 if (!(*i)->is_end() && !(*i)->is_start()) {
435 current_location = 0;
438 save_state (_("clear"));
440 changed (); /* EMIT SIGNAL */
441 current_changed (0); /* EMIT SIGNAL */
445 Locations::clear_markers ()
448 Glib::Mutex::Lock lm (lock);
449 LocationList::iterator tmp;
451 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
455 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
463 save_state (_("clear markers"));
465 changed (); /* EMIT SIGNAL */
469 Locations::clear_ranges ()
472 Glib::Mutex::Lock lm (lock);
473 LocationList::iterator tmp;
475 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
480 if (!(*i)->is_mark()) {
488 current_location = 0;
491 save_state (_("clear ranges"));
493 changed (); /* EMIT SIGNAL */
494 current_changed (0); /* EMIT SIGNAL */
498 Locations::add (Location *loc, bool make_current)
501 Glib::Mutex::Lock lm (lock);
502 locations.push_back (loc);
505 current_location = loc;
509 save_state (_("add"));
511 added (loc); /* EMIT SIGNAL */
514 current_changed (current_location); /* EMIT SIGNAL */
519 Locations::remove (Location *loc)
522 bool was_removed = false;
523 bool was_current = false;
524 LocationList::iterator i;
526 if (loc->is_end() || loc->is_start()) {
531 Glib::Mutex::Lock lm (lock);
533 for (i = locations.begin(); i != locations.end(); ++i) {
537 if (current_location == loc) {
538 current_location = 0;
547 save_state (_("remove"));
549 removed (loc); /* EMIT SIGNAL */
552 current_changed (0); /* EMIT SIGNAL */
555 changed (); /* EMIT_SIGNAL */
560 Locations::location_changed (Location* loc)
562 save_state (X_("location changed"));
563 changed (); /* EMIT SIGNAL */
567 Locations::get_state ()
569 XMLNode *node = new XMLNode ("Locations");
570 LocationList::iterator iter;
571 Glib::Mutex::Lock lm (lock);
573 for (iter = locations.begin(); iter != locations.end(); ++iter) {
574 node->add_child_nocopy ((*iter)->get_state ());
581 Locations::set_state (const XMLNode& node)
584 XMLNodeConstIterator niter;
586 if (node.name() != "Locations") {
587 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
591 nlist = node.children();
594 Glib::Mutex::Lock lm (lock);
596 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
600 Location *loc = new Location (**niter);
601 locations.push_back (loc);
604 catch (failed_constructor& err) {
605 error << _("could not load location from session file - ignored") << endmsg;
609 if (locations.size()) {
611 current_location = locations.front();
613 current_location = 0;
617 changed (); /* EMIT SIGNAL */
622 struct LocationStartEarlierComparison
624 bool operator() (Location *a, Location *b) {
625 return a->start() < b->start();
629 struct LocationStartLaterComparison
631 bool operator() (Location *a, Location *b) {
632 return a->start() > b->start();
637 Locations::first_location_before (jack_nframes_t frame)
642 Glib::Mutex::Lock lm (lock);
646 LocationStartLaterComparison cmp;
649 /* locs is now sorted latest..earliest */
651 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
652 if (!(*i)->is_hidden() && (*i)->start() < frame) {
661 Locations::first_location_after (jack_nframes_t frame)
666 Glib::Mutex::Lock lm (lock);
670 LocationStartEarlierComparison cmp;
673 /* locs is now sorted earliest..latest */
675 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
676 if (!(*i)->is_hidden() && (*i)->start() > frame) {
685 Locations::first_mark_before (jack_nframes_t frame)
690 Glib::Mutex::Lock lm (lock);
694 LocationStartLaterComparison cmp;
697 /* locs is now sorted latest..earliest */
699 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
700 if (!(*i)->is_hidden()) {
701 if ((*i)->is_mark()) {
702 /* MARK: start == end */
703 if ((*i)->start() < frame) {
704 return (*i)->start();
707 /* RANGE: start != end, compare start and end */
708 if ((*i)->end() < frame) {
711 if ((*i)->start () < frame) {
712 return (*i)->start();
722 Locations::first_mark_after (jack_nframes_t frame)
727 Glib::Mutex::Lock lm (lock);
731 LocationStartEarlierComparison cmp;
734 /* locs is now sorted earliest..latest */
736 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
737 if (!(*i)->is_hidden()) {
738 if ((*i)->is_mark()) {
739 /* MARK, start == end so just compare start */
740 if ((*i)->start() > frame) {
741 return (*i)->start();
744 /* RANGE, start != end, compare start and end */
745 if ((*i)->start() > frame ) {
746 return (*i)->start ();
748 if ((*i)->end() > frame) {
759 Locations::end_location () const
761 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
762 if ((*i)->is_end()) {
763 return const_cast<Location*> (*i);
770 Locations::start_location () const
772 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
773 if ((*i)->is_start()) {
774 return const_cast<Location*> (*i);
781 Locations::auto_loop_location () const
783 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
784 if ((*i)->is_auto_loop()) {
785 return const_cast<Location*> (*i);
792 Locations::auto_punch_location () const
794 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
795 if ((*i)->is_auto_punch()) {
796 return const_cast<Location*> (*i);
803 Locations::state_factory (std::string why) const
805 State* state = new State (why);
807 state->locations = locations;
809 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
810 state->states.push_back (new Location (**i));
817 Locations::restore_state (StateManager::State& state)
820 Glib::Mutex::Lock lm (lock);
821 State* lstate = dynamic_cast<State*> (&state);
823 locations = lstate->locations;
824 LocationList& states = lstate->states;
825 LocationList::iterator l, s;
827 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
836 Locations::get_memento () const
838 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
842 Locations::num_range_markers () const
845 Glib::Mutex::Lock lm (lock);
846 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
847 if ((*i)->is_range_marker()) {
855 Locations::get_location_by_id(PBD::ID id)
857 LocationList::iterator it;
858 for (it = locations.begin(); it != locations.end(); it++)
859 if (id == (*it)->id())