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 (jack_nframes_t s)
86 start_changed(this); /* EMIT SIGNAL */
88 Session::StartTimeChanged (); /* EMIT SIGNAL */
89 AudioFileSource::set_header_position_offset ( s );
92 Session::EndTimeChanged (); /* EMIT SIGNAL */
98 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
104 start_changed(this); /* EMIT SIGNAL */
111 Location::set_end (jack_nframes_t e)
117 end_changed(this); /* EMIT SIGNAL */
122 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
128 end_changed(this); /* EMIT SIGNAL */
134 Location::set (jack_nframes_t start, jack_nframes_t end)
136 if (is_mark() && start != end) {
138 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
142 if (_start != start) {
144 start_changed(this); /* EMIT SIGNAL */
149 end_changed(this); /* EMIT SIGNAL */
155 Location::set_hidden (bool yn, void *src)
157 if (set_flag_internal (yn, IsHidden)) {
158 FlagsChanged (this, src); /* EMIT SIGNAL */
163 Location::set_cd (bool yn, void *src)
165 if (set_flag_internal (yn, IsCDMarker)) {
166 FlagsChanged (this, src); /* EMIT SIGNAL */
171 Location::set_is_end (bool yn, void *src)
173 if (set_flag_internal (yn, IsEnd)) {
174 FlagsChanged (this, src); /* EMIT SIGNAL */
179 Location::set_is_start (bool yn, void *src)
181 if (set_flag_internal (yn, IsStart)) {
182 FlagsChanged (this, src); /* EMIT SIGNAL */
187 Location::set_auto_punch (bool yn, void *src)
189 if (is_mark() || _start == _end) {
193 if (set_flag_internal (yn, IsAutoPunch)) {
194 FlagsChanged (this, src); /* EMIT SIGNAL */
199 Location::set_auto_loop (bool yn, void *src)
201 if (is_mark() || _start == _end) {
205 if (set_flag_internal (yn, IsAutoLoop)) {
206 FlagsChanged (this, src); /* EMIT SIGNAL */
211 Location::set_flag_internal (bool yn, Flags flag)
214 if (!(_flags & flag)) {
228 Location::set_mark (bool yn)
230 /* This function is private, and so does not emit signals */
232 if (_start != _end) {
236 set_flag_internal (yn, IsMark);
241 Location::cd_info_node(const string & name, const string & value)
243 XMLNode* root = new XMLNode("CD-Info");
245 root->add_property("name", name);
246 root->add_property("value", value);
253 Location::get_state (void)
255 XMLNode *node = new XMLNode ("Location");
258 typedef map<string, string>::const_iterator CI;
260 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
261 node->add_child_nocopy(cd_info_node(m->first, m->second));
265 node->add_property("id", buf);
266 node->add_property ("name", name());
267 snprintf (buf, sizeof (buf), "%u", start());
268 node->add_property ("start", buf);
269 snprintf (buf, sizeof (buf), "%u", end());
270 node->add_property ("end", buf);
271 snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
272 node->add_property ("flags", buf);
278 Location::set_state (const XMLNode& node)
280 const XMLProperty *prop;
282 XMLNodeList cd_list = node.children();
283 XMLNodeConstIterator cd_iter;
289 if (node.name() != "Location") {
290 error << _("incorrect XML node passed to Location::set_state") << endmsg;
294 if ((prop = node.property ("id")) == 0) {
295 warning << _("XML node for Location has no ID information") << endmsg;
297 _id = prop->value ();
300 if ((prop = node.property ("name")) == 0) {
301 error << _("XML node for Location has no name information") << endmsg;
305 set_name (prop->value());
307 if ((prop = node.property ("start")) == 0) {
308 error << _("XML node for Location has no start information") << endmsg;
312 /* can't use set_start() here, because _end
313 may make the value of _start illegal.
316 _start = atoi (prop->value().c_str());
318 if ((prop = node.property ("end")) == 0) {
319 error << _("XML node for Location has no end information") << endmsg;
323 _end = atoi (prop->value().c_str());
327 if ((prop = node.property ("flags")) == 0) {
328 error << _("XML node for Location has no flags information") << endmsg;
332 _flags = Flags (atoi (prop->value().c_str()));
334 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
338 if (cd_node->name() != "CD-Info") {
342 if ((prop = cd_node->property ("name")) != 0) {
343 cd_name = prop->value();
345 throw failed_constructor ();
348 if ((prop = cd_node->property ("value")) != 0) {
349 cd_value = prop->value();
351 throw failed_constructor ();
355 cd_info[cd_name] = cd_value;
359 changed(this); /* EMIT SIGNAL */
364 /*---------------------------------------------------------------------- */
366 Locations::Locations ()
369 current_location = 0;
370 save_state (_("initial"));
373 Locations::~Locations ()
375 std::set<Location*> all_locations;
377 for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
379 State* lstate = dynamic_cast<State*> (*siter);
381 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
382 all_locations.insert (*liter);
385 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
386 all_locations.insert (*siter);
390 set_delete (&all_locations);
394 Locations::set_current (Location *loc, bool want_lock)
400 Glib::Mutex::Lock lm (lock);
401 ret = set_current_unlocked (loc);
403 ret = set_current_unlocked (loc);
407 current_changed (current_location); /* EMIT SIGNAL */
413 Locations::set_current_unlocked (Location *loc)
415 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
416 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
420 current_location = loc;
428 Glib::Mutex::Lock lm (lock);
429 LocationList::iterator tmp;
430 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
433 if (!(*i)->is_end() && !(*i)->is_start()) {
440 current_location = 0;
443 save_state (_("clear"));
445 changed (); /* EMIT SIGNAL */
446 current_changed (0); /* EMIT SIGNAL */
450 Locations::clear_markers ()
453 Glib::Mutex::Lock lm (lock);
454 LocationList::iterator tmp;
456 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
460 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
468 save_state (_("clear markers"));
470 changed (); /* EMIT SIGNAL */
474 Locations::clear_ranges ()
477 Glib::Mutex::Lock lm (lock);
478 LocationList::iterator tmp;
480 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
485 if (!(*i)->is_mark()) {
493 current_location = 0;
496 save_state (_("clear ranges"));
498 changed (); /* EMIT SIGNAL */
499 current_changed (0); /* EMIT SIGNAL */
503 Locations::add (Location *loc, bool make_current)
506 Glib::Mutex::Lock lm (lock);
507 locations.push_back (loc);
510 current_location = loc;
514 save_state (_("add"));
516 added (loc); /* EMIT SIGNAL */
519 current_changed (current_location); /* EMIT SIGNAL */
524 Locations::remove (Location *loc)
527 bool was_removed = false;
528 bool was_current = false;
529 LocationList::iterator i;
531 if (loc->is_end() || loc->is_start()) {
536 Glib::Mutex::Lock lm (lock);
538 for (i = locations.begin(); i != locations.end(); ++i) {
542 if (current_location == loc) {
543 current_location = 0;
552 save_state (_("remove"));
554 removed (loc); /* EMIT SIGNAL */
557 current_changed (0); /* EMIT SIGNAL */
560 changed (); /* EMIT_SIGNAL */
565 Locations::location_changed (Location* loc)
567 save_state (X_("location changed"));
568 changed (); /* EMIT SIGNAL */
572 Locations::get_state ()
574 XMLNode *node = new XMLNode ("Locations");
575 LocationList::iterator iter;
576 Glib::Mutex::Lock lm (lock);
578 for (iter = locations.begin(); iter != locations.end(); ++iter) {
579 node->add_child_nocopy ((*iter)->get_state ());
586 Locations::set_state (const XMLNode& node)
589 XMLNodeConstIterator niter;
591 if (node.name() != "Locations") {
592 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
596 nlist = node.children();
599 Glib::Mutex::Lock lm (lock);
601 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
605 Location *loc = new Location (**niter);
606 locations.push_back (loc);
609 catch (failed_constructor& err) {
610 error << _("could not load location from session file - ignored") << endmsg;
614 if (locations.size()) {
616 current_location = locations.front();
618 current_location = 0;
622 changed (); /* EMIT SIGNAL */
627 struct LocationStartEarlierComparison
629 bool operator() (Location *a, Location *b) {
630 return a->start() < b->start();
634 struct LocationStartLaterComparison
636 bool operator() (Location *a, Location *b) {
637 return a->start() > b->start();
642 Locations::first_location_before (jack_nframes_t frame)
647 Glib::Mutex::Lock lm (lock);
651 LocationStartLaterComparison cmp;
654 /* locs is now sorted latest..earliest */
656 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
657 if (!(*i)->is_hidden() && (*i)->start() < frame) {
666 Locations::first_location_after (jack_nframes_t frame)
671 Glib::Mutex::Lock lm (lock);
675 LocationStartEarlierComparison cmp;
678 /* locs is now sorted earliest..latest */
680 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
681 if (!(*i)->is_hidden() && (*i)->start() > frame) {
690 Locations::first_mark_before (jack_nframes_t frame)
695 Glib::Mutex::Lock lm (lock);
699 LocationStartLaterComparison cmp;
702 /* locs is now sorted latest..earliest */
704 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
705 if (!(*i)->is_hidden()) {
706 if ((*i)->is_mark()) {
707 /* MARK: start == end */
708 if ((*i)->start() < frame) {
709 return (*i)->start();
712 /* RANGE: start != end, compare start and end */
713 if ((*i)->end() < frame) {
716 if ((*i)->start () < frame) {
717 return (*i)->start();
727 Locations::first_mark_after (jack_nframes_t frame)
732 Glib::Mutex::Lock lm (lock);
736 LocationStartEarlierComparison cmp;
739 /* locs is now sorted earliest..latest */
741 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
742 if (!(*i)->is_hidden()) {
743 if ((*i)->is_mark()) {
744 /* MARK, start == end so just compare start */
745 if ((*i)->start() > frame) {
746 return (*i)->start();
749 /* RANGE, start != end, compare start and end */
750 if ((*i)->start() > frame ) {
751 return (*i)->start ();
753 if ((*i)->end() > frame) {
764 Locations::end_location () const
766 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
767 if ((*i)->is_end()) {
768 return const_cast<Location*> (*i);
775 Locations::start_location () const
777 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
778 if ((*i)->is_start()) {
779 return const_cast<Location*> (*i);
786 Locations::auto_loop_location () const
788 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
789 if ((*i)->is_auto_loop()) {
790 return const_cast<Location*> (*i);
797 Locations::auto_punch_location () const
799 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
800 if ((*i)->is_auto_punch()) {
801 return const_cast<Location*> (*i);
808 Locations::state_factory (std::string why) const
810 State* state = new State (why);
812 state->locations = locations;
814 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
815 state->states.push_back (new Location (**i));
822 Locations::restore_state (StateManager::State& state)
825 Glib::Mutex::Lock lm (lock);
826 State* lstate = dynamic_cast<State*> (&state);
828 locations = lstate->locations;
829 LocationList& states = lstate->states;
830 LocationList::iterator l, s;
832 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
841 Locations::get_memento () const
843 return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
847 Locations::num_range_markers () const
850 Glib::Mutex::Lock lm (lock);
851 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
852 if ((*i)->is_range_marker()) {
860 Locations::get_location_by_id(PBD::ID id)
862 LocationList::iterator it;
863 for (it = locations.begin(); it != locations.end(); it++)
864 if (id == (*it)->id())