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),
51 Location::operator= (const Location& other)
58 _start = other._start;
60 _flags = other._flags;
62 /* "changed" not emitted on purpose */
68 Location::set_start (jack_nframes_t s)
74 start_changed(this); /* EMIT SIGNAL */
76 AudioFileSource::set_header_position_offset ( s );
82 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
88 start_changed(this); /* EMIT SIGNAL */
95 Location::set_end (jack_nframes_t e)
101 end_changed(this); /* EMIT SIGNAL */
106 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
112 end_changed(this); /* EMIT SIGNAL */
118 Location::set (jack_nframes_t start, jack_nframes_t end)
120 if (is_mark() && start != end) {
122 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
126 if (_start != start) {
128 start_changed(this); /* EMIT SIGNAL */
133 end_changed(this); /* EMIT SIGNAL */
139 Location::set_hidden (bool yn, void *src)
141 if (set_flag_internal (yn, IsHidden)) {
142 FlagsChanged (this, src); /* EMIT SIGNAL */
147 Location::set_cd (bool yn, void *src)
149 if (set_flag_internal (yn, IsCDMarker)) {
150 FlagsChanged (this, src); /* EMIT SIGNAL */
155 Location::set_is_end (bool yn, void *src)
157 if (set_flag_internal (yn, IsEnd)) {
158 FlagsChanged (this, src); /* EMIT SIGNAL */
163 Location::set_is_start (bool yn, void *src)
165 if (set_flag_internal (yn, IsStart)) {
166 FlagsChanged (this, src); /* EMIT SIGNAL */
171 Location::set_auto_punch (bool yn, void *src)
173 if (is_mark() || _start == _end) {
177 if (set_flag_internal (yn, IsAutoPunch)) {
178 FlagsChanged (this, src); /* EMIT SIGNAL */
183 Location::set_auto_loop (bool yn, void *src)
185 if (is_mark() || _start == _end) {
189 if (set_flag_internal (yn, IsAutoLoop)) {
190 FlagsChanged (this, src); /* EMIT SIGNAL */
195 Location::set_flag_internal (bool yn, Flags flag)
198 if (!(_flags & flag)) {
212 Location::set_mark (bool yn)
214 /* This function is private, and so does not emit signals */
216 if (_start != _end) {
220 set_flag_internal (yn, IsMark);
225 Location::cd_info_node(const string & name, const string & value)
227 XMLNode* root = new XMLNode("CD-Info");
229 root->add_property("name", name);
230 root->add_property("value", value);
237 Location::get_state (void)
239 XMLNode *node = new XMLNode ("Location");
242 typedef map<string, string>::const_iterator CI;
243 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
244 node->add_child_nocopy(cd_info_node(m->first, m->second));
247 node->add_property ("name", name());
248 snprintf (buf, sizeof (buf), "%u", start());
249 node->add_property ("start", buf);
250 snprintf (buf, sizeof (buf), "%u", end());
251 node->add_property ("end", buf);
252 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
253 node->add_property ("flags", buf);
259 Location::set_state (const XMLNode& node)
261 XMLPropertyList plist;
262 const XMLProperty *prop;
264 XMLNodeList cd_list = node.children();
265 XMLNodeConstIterator cd_iter;
272 if (node.name() != "Location") {
273 error << _("incorrect XML node passed to Location::set_state") << endmsg;
277 plist = node.properties();
279 if ((prop = node.property ("name")) == 0) {
280 error << _("XML node for Location has no name information") << endmsg;
284 set_name (prop->value());
286 if ((prop = node.property ("start")) == 0) {
287 error << _("XML node for Location has no start information") << endmsg;
291 /* can't use set_start() here, because _end
292 may make the value of _start illegal.
295 _start = atoi (prop->value().c_str());
297 if ((prop = node.property ("end")) == 0) {
298 error << _("XML node for Location has no end information") << endmsg;
302 _end = atoi (prop->value().c_str());
306 if ((prop = node.property ("flags")) == 0) {
307 error << _("XML node for Location has no flags information") << endmsg;
311 _flags = Flags (atoi (prop->value().c_str()));
313 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
317 if (cd_node->name() != "CD-Info") {
321 if ((prop = cd_node->property ("name")) != 0) {
322 cd_name = prop->value();
324 throw failed_constructor ();
327 if ((prop = cd_node->property ("value")) != 0) {
328 cd_value = prop->value();
330 throw failed_constructor ();
334 cd_info[cd_name] = cd_value;
338 changed(this); /* EMIT SIGNAL */
343 /*---------------------------------------------------------------------- */
345 Locations::Locations ()
348 current_location = 0;
349 save_state (_("initial"));
352 Locations::~Locations ()
354 std::set<Location*> all_locations;
356 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
358 State* lstate = dynamic_cast<State*> (*siter);
360 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
361 all_locations.insert (*liter);
364 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
365 all_locations.insert (*siter);
369 set_delete (&all_locations);
373 Locations::set_current (Location *loc, bool want_lock)
379 Glib::Mutex::Lock lm (lock);
380 ret = set_current_unlocked (loc);
382 ret = set_current_unlocked (loc);
386 current_changed (current_location); /* EMIT SIGNAL */
392 Locations::set_current_unlocked (Location *loc)
394 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
395 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
399 current_location = loc;
407 Glib::Mutex::Lock lm (lock);
408 LocationList::iterator tmp;
409 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
412 if (!(*i)->is_end() && !(*i)->is_start()) {
419 current_location = 0;
422 save_state (_("clear"));
424 changed (); /* EMIT SIGNAL */
425 current_changed (0); /* EMIT SIGNAL */
429 Locations::clear_markers ()
432 Glib::Mutex::Lock lm (lock);
433 LocationList::iterator tmp;
435 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
439 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
447 save_state (_("clear markers"));
449 changed (); /* EMIT SIGNAL */
453 Locations::clear_ranges ()
456 Glib::Mutex::Lock lm (lock);
457 LocationList::iterator tmp;
459 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
464 if (!(*i)->is_mark()) {
472 current_location = 0;
475 save_state (_("clear ranges"));
477 changed (); /* EMIT SIGNAL */
478 current_changed (0); /* EMIT SIGNAL */
482 Locations::add (Location *loc, bool make_current)
485 Glib::Mutex::Lock lm (lock);
486 locations.push_back (loc);
489 current_location = loc;
493 save_state (_("add"));
495 added (loc); /* EMIT SIGNAL */
498 current_changed (current_location); /* EMIT SIGNAL */
503 Locations::remove (Location *loc)
506 bool was_removed = false;
507 bool was_current = false;
508 LocationList::iterator i;
510 if (loc->is_end() || loc->is_start()) {
515 Glib::Mutex::Lock lm (lock);
517 for (i = locations.begin(); i != locations.end(); ++i) {
521 if (current_location == loc) {
522 current_location = 0;
531 save_state (_("remove"));
533 removed (loc); /* EMIT SIGNAL */
536 current_changed (0); /* EMIT SIGNAL */
539 changed (); /* EMIT_SIGNAL */
544 Locations::location_changed (Location* loc)
546 save_state (X_("location changed"));
547 changed (); /* EMIT SIGNAL */
551 Locations::get_state ()
553 XMLNode *node = new XMLNode ("Locations");
554 LocationList::iterator iter;
555 Glib::Mutex::Lock lm (lock);
557 for (iter = locations.begin(); iter != locations.end(); ++iter) {
558 node->add_child_nocopy ((*iter)->get_state ());
565 Locations::set_state (const XMLNode& node)
568 XMLNodeConstIterator niter;
570 if (node.name() != "Locations") {
571 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
575 nlist = node.children();
578 Glib::Mutex::Lock lm (lock);
580 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
581 Location *loc = new Location;
583 if (loc->set_state (**niter)) {
586 locations.push_back (loc);
590 if (locations.size()) {
591 current_location = locations.front();
593 current_location = 0;
597 changed (); /* EMIT SIGNAL */
602 struct LocationStartEarlierComparison
604 bool operator() (Location *a, Location *b) {
605 return a->start() < b->start();
609 struct LocationStartLaterComparison
611 bool operator() (Location *a, Location *b) {
612 return a->start() > b->start();
617 Locations::first_location_before (jack_nframes_t frame)
622 Glib::Mutex::Lock lm (lock);
626 LocationStartLaterComparison cmp;
629 /* locs is now sorted latest..earliest */
631 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
632 if (!(*i)->is_hidden() && (*i)->start() < frame) {
641 Locations::first_location_after (jack_nframes_t frame)
646 Glib::Mutex::Lock lm (lock);
650 LocationStartEarlierComparison cmp;
653 /* locs is now sorted earliest..latest */
655 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
656 if (!(*i)->is_hidden() && (*i)->start() > frame) {
665 Locations::first_mark_before (jack_nframes_t frame)
670 Glib::Mutex::Lock lm (lock);
674 LocationStartLaterComparison cmp;
677 /* locs is now sorted latest..earliest */
679 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
680 if (!(*i)->is_hidden()) {
681 if ((*i)->is_mark()) {
682 /* MARK: start == end */
683 if ((*i)->start() < frame) {
684 return (*i)->start();
687 /* RANGE: start != end, compare start and end */
688 if ((*i)->end() < frame) {
691 if ((*i)->start () < frame) {
692 return (*i)->start();
702 Locations::first_mark_after (jack_nframes_t frame)
707 Glib::Mutex::Lock lm (lock);
711 LocationStartEarlierComparison cmp;
714 /* locs is now sorted earliest..latest */
716 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
717 if (!(*i)->is_hidden()) {
718 if ((*i)->is_mark()) {
719 /* MARK, start == end so just compare start */
720 if ((*i)->start() > frame) {
721 return (*i)->start();
724 /* RANGE, start != end, compare start and end */
725 if ((*i)->start() > frame ) {
726 return (*i)->start ();
728 if ((*i)->end() > frame) {
739 Locations::end_location () const
741 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
742 if ((*i)->is_end()) {
743 return const_cast<Location*> (*i);
750 Locations::start_location () const
752 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
753 if ((*i)->is_start()) {
754 return const_cast<Location*> (*i);
761 Locations::auto_loop_location () const
763 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
764 if ((*i)->is_auto_loop()) {
765 return const_cast<Location*> (*i);
772 Locations::auto_punch_location () const
774 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
775 if ((*i)->is_auto_punch()) {
776 return const_cast<Location*> (*i);
783 Locations::state_factory (std::string why) const
785 State* state = new State (why);
787 state->locations = locations;
789 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
790 state->states.push_back (new Location (**i));
797 Locations::restore_state (StateManager::State& state)
800 Glib::Mutex::Lock lm (lock);
801 State* lstate = dynamic_cast<State*> (&state);
803 locations = lstate->locations;
804 LocationList& states = lstate->states;
805 LocationList::iterator l, s;
807 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
816 Locations::get_memento () const
818 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
822 Locations::num_range_markers () const
825 Glib::Mutex::Lock lm (lock);
826 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
827 if ((*i)->is_range_marker()) {