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);
55 Location::operator= (const Location& other)
62 _start = other._start;
64 _flags = other._flags;
66 /* "changed" not emitted on purpose */
72 Location::set_start (jack_nframes_t s)
78 start_changed(this); /* EMIT SIGNAL */
80 AudioFileSource::set_header_position_offset ( s );
86 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
92 start_changed(this); /* EMIT SIGNAL */
99 Location::set_end (jack_nframes_t e)
105 end_changed(this); /* EMIT SIGNAL */
110 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
116 end_changed(this); /* EMIT SIGNAL */
122 Location::set (jack_nframes_t start, jack_nframes_t end)
124 if (is_mark() && start != end) {
126 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
130 if (_start != start) {
132 start_changed(this); /* EMIT SIGNAL */
137 end_changed(this); /* EMIT SIGNAL */
143 Location::set_hidden (bool yn, void *src)
145 if (set_flag_internal (yn, IsHidden)) {
146 FlagsChanged (this, src); /* EMIT SIGNAL */
151 Location::set_cd (bool yn, void *src)
153 if (set_flag_internal (yn, IsCDMarker)) {
154 FlagsChanged (this, src); /* EMIT SIGNAL */
159 Location::set_is_end (bool yn, void *src)
161 if (set_flag_internal (yn, IsEnd)) {
162 FlagsChanged (this, src); /* EMIT SIGNAL */
167 Location::set_is_start (bool yn, void *src)
169 if (set_flag_internal (yn, IsStart)) {
170 FlagsChanged (this, src); /* EMIT SIGNAL */
175 Location::set_auto_punch (bool yn, void *src)
177 if (is_mark() || _start == _end) {
181 if (set_flag_internal (yn, IsAutoPunch)) {
182 FlagsChanged (this, src); /* EMIT SIGNAL */
187 Location::set_auto_loop (bool yn, void *src)
189 if (is_mark() || _start == _end) {
193 if (set_flag_internal (yn, IsAutoLoop)) {
194 FlagsChanged (this, src); /* EMIT SIGNAL */
199 Location::set_flag_internal (bool yn, Flags flag)
202 if (!(_flags & flag)) {
216 Location::set_mark (bool yn)
218 /* This function is private, and so does not emit signals */
220 if (_start != _end) {
224 set_flag_internal (yn, IsMark);
229 Location::cd_info_node(const string & name, const string & value)
231 XMLNode* root = new XMLNode("CD-Info");
233 root->add_property("name", name);
234 root->add_property("value", value);
241 Location::get_state (void)
243 XMLNode *node = new XMLNode ("Location");
246 typedef map<string, string>::const_iterator CI;
247 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
248 node->add_child_nocopy(cd_info_node(m->first, m->second));
251 node->add_property ("name", name());
252 snprintf (buf, sizeof (buf), "%u", start());
253 node->add_property ("start", buf);
254 snprintf (buf, sizeof (buf), "%u", end());
255 node->add_property ("end", buf);
256 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
257 node->add_property ("flags", buf);
263 Location::set_state (const XMLNode& node)
265 XMLPropertyList plist;
266 const XMLProperty *prop;
268 XMLNodeList cd_list = node.children();
269 XMLNodeConstIterator cd_iter;
276 if (node.name() != "Location") {
277 error << _("incorrect XML node passed to Location::set_state") << endmsg;
281 plist = node.properties();
283 if ((prop = node.property ("name")) == 0) {
284 error << _("XML node for Location has no name information") << endmsg;
288 set_name (prop->value());
290 if ((prop = node.property ("start")) == 0) {
291 error << _("XML node for Location has no start information") << endmsg;
295 /* can't use set_start() here, because _end
296 may make the value of _start illegal.
299 _start = atoi (prop->value().c_str());
301 if ((prop = node.property ("end")) == 0) {
302 error << _("XML node for Location has no end information") << endmsg;
306 _end = atoi (prop->value().c_str());
310 if ((prop = node.property ("flags")) == 0) {
311 error << _("XML node for Location has no flags information") << endmsg;
315 _flags = Flags (atoi (prop->value().c_str()));
317 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
321 if (cd_node->name() != "CD-Info") {
325 if ((prop = cd_node->property ("name")) != 0) {
326 cd_name = prop->value();
328 throw failed_constructor ();
331 if ((prop = cd_node->property ("value")) != 0) {
332 cd_value = prop->value();
334 throw failed_constructor ();
338 cd_info[cd_name] = cd_value;
342 changed(this); /* EMIT SIGNAL */
347 /*---------------------------------------------------------------------- */
349 Locations::Locations ()
352 current_location = 0;
353 save_state (_("initial"));
356 Locations::~Locations ()
358 std::set<Location*> all_locations;
360 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
362 State* lstate = dynamic_cast<State*> (*siter);
364 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
365 all_locations.insert (*liter);
368 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
369 all_locations.insert (*siter);
373 set_delete (&all_locations);
377 Locations::set_current (Location *loc, bool want_lock)
383 Glib::Mutex::Lock lm (lock);
384 ret = set_current_unlocked (loc);
386 ret = set_current_unlocked (loc);
390 current_changed (current_location); /* EMIT SIGNAL */
396 Locations::set_current_unlocked (Location *loc)
398 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
399 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
403 current_location = loc;
411 Glib::Mutex::Lock lm (lock);
412 LocationList::iterator tmp;
413 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
416 if (!(*i)->is_end() && !(*i)->is_start()) {
423 current_location = 0;
426 save_state (_("clear"));
428 changed (); /* EMIT SIGNAL */
429 current_changed (0); /* EMIT SIGNAL */
433 Locations::clear_markers ()
436 Glib::Mutex::Lock lm (lock);
437 LocationList::iterator tmp;
439 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
443 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
451 save_state (_("clear markers"));
453 changed (); /* EMIT SIGNAL */
457 Locations::clear_ranges ()
460 Glib::Mutex::Lock lm (lock);
461 LocationList::iterator tmp;
463 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
468 if (!(*i)->is_mark()) {
476 current_location = 0;
479 save_state (_("clear ranges"));
481 changed (); /* EMIT SIGNAL */
482 current_changed (0); /* EMIT SIGNAL */
486 Locations::add (Location *loc, bool make_current)
489 Glib::Mutex::Lock lm (lock);
490 locations.push_back (loc);
493 current_location = loc;
497 save_state (_("add"));
499 added (loc); /* EMIT SIGNAL */
502 current_changed (current_location); /* EMIT SIGNAL */
507 Locations::remove (Location *loc)
510 bool was_removed = false;
511 bool was_current = false;
512 LocationList::iterator i;
514 if (loc->is_end() || loc->is_start()) {
519 Glib::Mutex::Lock lm (lock);
521 for (i = locations.begin(); i != locations.end(); ++i) {
525 if (current_location == loc) {
526 current_location = 0;
535 save_state (_("remove"));
537 removed (loc); /* EMIT SIGNAL */
540 current_changed (0); /* EMIT SIGNAL */
543 changed (); /* EMIT_SIGNAL */
548 Locations::location_changed (Location* loc)
550 save_state (X_("location changed"));
551 changed (); /* EMIT SIGNAL */
555 Locations::get_state ()
557 XMLNode *node = new XMLNode ("Locations");
558 LocationList::iterator iter;
559 Glib::Mutex::Lock lm (lock);
561 for (iter = locations.begin(); iter != locations.end(); ++iter) {
562 node->add_child_nocopy ((*iter)->get_state ());
569 Locations::set_state (const XMLNode& node)
572 XMLNodeConstIterator niter;
574 if (node.name() != "Locations") {
575 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
579 nlist = node.children();
582 Glib::Mutex::Lock lm (lock);
584 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
585 Location *loc = new Location;
587 if (loc->set_state (**niter)) {
590 locations.push_back (loc);
594 if (locations.size()) {
595 current_location = locations.front();
597 current_location = 0;
601 changed (); /* EMIT SIGNAL */
606 struct LocationStartEarlierComparison
608 bool operator() (Location *a, Location *b) {
609 return a->start() < b->start();
613 struct LocationStartLaterComparison
615 bool operator() (Location *a, Location *b) {
616 return a->start() > b->start();
621 Locations::first_location_before (jack_nframes_t frame)
626 Glib::Mutex::Lock lm (lock);
630 LocationStartLaterComparison cmp;
633 /* locs is now sorted latest..earliest */
635 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
636 if (!(*i)->is_hidden() && (*i)->start() < frame) {
645 Locations::first_location_after (jack_nframes_t frame)
650 Glib::Mutex::Lock lm (lock);
654 LocationStartEarlierComparison cmp;
657 /* locs is now sorted earliest..latest */
659 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
660 if (!(*i)->is_hidden() && (*i)->start() > frame) {
669 Locations::first_mark_before (jack_nframes_t frame)
674 Glib::Mutex::Lock lm (lock);
678 LocationStartLaterComparison cmp;
681 /* locs is now sorted latest..earliest */
683 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
684 if (!(*i)->is_hidden()) {
685 if ((*i)->is_mark()) {
686 /* MARK: start == end */
687 if ((*i)->start() < frame) {
688 return (*i)->start();
691 /* RANGE: start != end, compare start and end */
692 if ((*i)->end() < frame) {
695 if ((*i)->start () < frame) {
696 return (*i)->start();
706 Locations::first_mark_after (jack_nframes_t frame)
711 Glib::Mutex::Lock lm (lock);
715 LocationStartEarlierComparison cmp;
718 /* locs is now sorted earliest..latest */
720 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
721 if (!(*i)->is_hidden()) {
722 if ((*i)->is_mark()) {
723 /* MARK, start == end so just compare start */
724 if ((*i)->start() > frame) {
725 return (*i)->start();
728 /* RANGE, start != end, compare start and end */
729 if ((*i)->start() > frame ) {
730 return (*i)->start ();
732 if ((*i)->end() > frame) {
743 Locations::end_location () const
745 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
746 if ((*i)->is_end()) {
747 return const_cast<Location*> (*i);
754 Locations::start_location () const
756 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
757 if ((*i)->is_start()) {
758 return const_cast<Location*> (*i);
765 Locations::auto_loop_location () const
767 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
768 if ((*i)->is_auto_loop()) {
769 return const_cast<Location*> (*i);
776 Locations::auto_punch_location () const
778 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
779 if ((*i)->is_auto_punch()) {
780 return const_cast<Location*> (*i);
787 Locations::state_factory (std::string why) const
789 State* state = new State (why);
791 state->locations = locations;
793 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
794 state->states.push_back (new Location (**i));
801 Locations::restore_state (StateManager::State& state)
804 Glib::Mutex::Lock lm (lock);
805 State* lstate = dynamic_cast<State*> (&state);
807 locations = lstate->locations;
808 LocationList& states = lstate->states;
809 LocationList::iterator l, s;
811 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
820 Locations::get_memento () const
822 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
826 Locations::num_range_markers () const
829 Glib::Mutex::Lock lm (lock);
830 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
831 if ((*i)->is_range_marker()) {