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.
22 #include <cstdio> /* for sprintf */
26 #include <sigc++/bind.h>
28 #include <pbd/stl_delete.h>
29 #include <pbd/xml++.h>
30 #include <pbd/enumwriter.h>
32 #include <ardour/location.h>
33 #include <ardour/session.h>
34 #include <ardour/audiofilesource.h>
41 using namespace ARDOUR;
45 Location::Location (const Location& other)
46 : _name (other._name),
47 _start (other._start),
51 /* start and end flags can never be copied, because there can only ever be one of each */
53 _flags = Flags (_flags & ~IsStart);
54 _flags = Flags (_flags & ~IsEnd);
57 Location::Location (const XMLNode& node)
59 if (set_state (node)) {
60 throw failed_constructor ();
65 Location::operator= (const Location& other)
72 _start = other._start;
74 _flags = other._flags;
76 /* "changed" not emitted on purpose */
82 Location::set_start (nframes_t s)
90 start_changed(this); /* EMIT SIGNAL */
94 Session::StartTimeChanged (); /* EMIT SIGNAL */
95 AudioFileSource::set_header_position_offset ( s );
99 Session::EndTimeChanged (); /* EMIT SIGNAL */
105 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
111 start_changed(this); /* EMIT SIGNAL */
118 Location::set_end (nframes_t e)
124 end_changed(this); /* EMIT SIGNAL */
129 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
135 end_changed(this); /* EMIT SIGNAL */
141 Location::set (nframes_t start, nframes_t end)
143 if (is_mark() && start != end) {
145 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
149 if (_start != start) {
151 start_changed(this); /* EMIT SIGNAL */
156 end_changed(this); /* EMIT SIGNAL */
162 Location::set_hidden (bool yn, void *src)
164 if (set_flag_internal (yn, IsHidden)) {
165 FlagsChanged (this, src); /* EMIT SIGNAL */
170 Location::set_cd (bool yn, void *src)
172 // XXX this really needs to be session start
173 // but its not available here - leave to GUI
176 error << _("You cannot put a CD marker at this position") << endmsg;
180 if (set_flag_internal (yn, IsCDMarker)) {
181 FlagsChanged (this, src); /* EMIT SIGNAL */
186 Location::set_is_end (bool yn, void *src)
188 if (set_flag_internal (yn, IsEnd)) {
189 FlagsChanged (this, src); /* EMIT SIGNAL */
194 Location::set_is_start (bool yn, void *src)
196 if (set_flag_internal (yn, IsStart)) {
197 FlagsChanged (this, src); /* EMIT SIGNAL */
202 Location::set_auto_punch (bool yn, void *src)
204 if (is_mark() || _start == _end) {
208 if (set_flag_internal (yn, IsAutoPunch)) {
209 FlagsChanged (this, src); /* EMIT SIGNAL */
214 Location::set_auto_loop (bool yn, void *src)
216 if (is_mark() || _start == _end) {
220 if (set_flag_internal (yn, IsAutoLoop)) {
221 FlagsChanged (this, src); /* EMIT SIGNAL */
226 Location::set_flag_internal (bool yn, Flags flag)
229 if (!(_flags & flag)) {
230 _flags = Flags (_flags | flag);
235 _flags = Flags (_flags & ~flag);
243 Location::set_mark (bool yn)
245 /* This function is private, and so does not emit signals */
247 if (_start != _end) {
251 set_flag_internal (yn, IsMark);
256 Location::cd_info_node(const string & name, const string & value)
258 XMLNode* root = new XMLNode("CD-Info");
260 root->add_property("name", name);
261 root->add_property("value", value);
268 Location::get_state (void)
270 XMLNode *node = new XMLNode ("Location");
273 typedef map<string, string>::const_iterator CI;
275 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
276 node->add_child_nocopy(cd_info_node(m->first, m->second));
279 id().print (buf, sizeof (buf));
280 node->add_property("id", buf);
281 node->add_property ("name", name());
282 snprintf (buf, sizeof (buf), "%u", start());
283 node->add_property ("start", buf);
284 snprintf (buf, sizeof (buf), "%u", end());
285 node->add_property ("end", buf);
286 node->add_property ("flags", enum_2_string (_flags));
292 Location::set_state (const XMLNode& node)
294 const XMLProperty *prop;
296 XMLNodeList cd_list = node.children();
297 XMLNodeConstIterator cd_iter;
303 if (node.name() != "Location") {
304 error << _("incorrect XML node passed to Location::set_state") << endmsg;
308 if ((prop = node.property ("id")) == 0) {
309 warning << _("XML node for Location has no ID information") << endmsg;
311 _id = prop->value ();
314 if ((prop = node.property ("name")) == 0) {
315 error << _("XML node for Location has no name information") << endmsg;
319 set_name (prop->value());
321 if ((prop = node.property ("start")) == 0) {
322 error << _("XML node for Location has no start information") << endmsg;
326 /* can't use set_start() here, because _end
327 may make the value of _start illegal.
330 _start = atoi (prop->value().c_str());
332 if ((prop = node.property ("end")) == 0) {
333 error << _("XML node for Location has no end information") << endmsg;
337 _end = atoi (prop->value().c_str());
339 if ((prop = node.property ("flags")) == 0) {
340 error << _("XML node for Location has no flags information") << endmsg;
344 _flags = Flags (string_2_enum (prop->value(), _flags));
346 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
350 if (cd_node->name() != "CD-Info") {
354 if ((prop = cd_node->property ("name")) != 0) {
355 cd_name = prop->value();
357 throw failed_constructor ();
360 if ((prop = cd_node->property ("value")) != 0) {
361 cd_value = prop->value();
363 throw failed_constructor ();
367 cd_info[cd_name] = cd_value;
371 changed(this); /* EMIT SIGNAL */
376 /*---------------------------------------------------------------------- */
378 Locations::Locations ()
381 current_location = 0;
384 Locations::~Locations ()
386 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
387 LocationList::iterator tmp = i;
395 Locations::set_current (Location *loc, bool want_lock)
401 Glib::Mutex::Lock lm (lock);
402 ret = set_current_unlocked (loc);
404 ret = set_current_unlocked (loc);
408 current_changed (current_location); /* EMIT SIGNAL */
414 Locations::next_available_name(string& result,string base)
416 LocationList::iterator i;
422 bool available[SUFFIX_MAX+1];
425 for (int k=1; k<SUFFIX_MAX; k++) {
429 for (i = locations.begin(); i != locations.end(); ++i) {
431 temp = location->name();
432 if (l && !temp.find(base,0)) {
433 suffix = atoi(temp.substr(l,3).c_str());
434 if (suffix) available[suffix] = false;
437 for (int k=1; k<=SUFFIX_MAX; k++) {
439 snprintf (buf, 31, "%d", k);
448 Locations::set_current_unlocked (Location *loc)
450 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
451 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
455 current_location = loc;
463 Glib::Mutex::Lock lm (lock);
465 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
467 LocationList::iterator tmp = i;
470 if (!(*i)->is_end() && !(*i)->is_start()) {
477 current_location = 0;
480 changed (); /* EMIT SIGNAL */
481 current_changed (0); /* EMIT SIGNAL */
485 Locations::clear_markers ()
488 Glib::Mutex::Lock lm (lock);
489 LocationList::iterator tmp;
491 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
495 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
503 changed (); /* EMIT SIGNAL */
507 Locations::clear_ranges ()
510 Glib::Mutex::Lock lm (lock);
511 LocationList::iterator tmp;
513 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
518 if (!(*i)->is_mark()) {
526 current_location = 0;
529 changed (); /* EMIT SIGNAL */
530 current_changed (0); /* EMIT SIGNAL */
534 Locations::add (Location *loc, bool make_current)
537 Glib::Mutex::Lock lm (lock);
538 locations.push_back (loc);
541 current_location = loc;
545 added (loc); /* EMIT SIGNAL */
548 current_changed (current_location); /* EMIT SIGNAL */
553 Locations::remove (Location *loc)
556 bool was_removed = false;
557 bool was_current = false;
558 LocationList::iterator i;
560 if (loc->is_end() || loc->is_start()) {
565 Glib::Mutex::Lock lm (lock);
567 for (i = locations.begin(); i != locations.end(); ++i) {
571 if (current_location == loc) {
572 current_location = 0;
582 removed (loc); /* EMIT SIGNAL */
585 current_changed (0); /* EMIT SIGNAL */
588 changed (); /* EMIT_SIGNAL */
593 Locations::location_changed (Location* loc)
595 changed (); /* EMIT SIGNAL */
599 Locations::get_state ()
601 XMLNode *node = new XMLNode ("Locations");
602 LocationList::iterator iter;
603 Glib::Mutex::Lock lm (lock);
605 for (iter = locations.begin(); iter != locations.end(); ++iter) {
606 node->add_child_nocopy ((*iter)->get_state ());
613 Locations::set_state (const XMLNode& node)
616 XMLNodeConstIterator niter;
618 if (node.name() != "Locations") {
619 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
623 nlist = node.children();
626 current_location = 0;
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, bool include_special_ranges)
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 (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
690 if (!(*i)->is_hidden() && (*i)->start() < frame) {
699 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
704 Glib::Mutex::Lock lm (lock);
708 LocationStartEarlierComparison cmp;
711 /* locs is now sorted earliest..latest */
713 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
714 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
717 if (!(*i)->is_hidden() && (*i)->start() > frame) {
726 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
731 Glib::Mutex::Lock lm (lock);
735 LocationStartLaterComparison cmp;
738 /* locs is now sorted latest..earliest */
740 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
741 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
744 if (!(*i)->is_hidden()) {
745 if ((*i)->is_mark()) {
746 /* MARK: start == end */
747 if ((*i)->start() < frame) {
748 return (*i)->start();
751 /* RANGE: start != end, compare start and end */
752 if ((*i)->end() < frame) {
755 if ((*i)->start () < frame) {
756 return (*i)->start();
766 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
771 Glib::Mutex::Lock lm (lock);
775 LocationStartEarlierComparison cmp;
778 /* locs is now sorted earliest..latest */
780 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
781 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
784 if (!(*i)->is_hidden()) {
785 if ((*i)->is_mark()) {
786 /* MARK, start == end so just compare start */
787 if ((*i)->start() > frame) {
788 return (*i)->start();
791 /* RANGE, start != end, compare start and end */
792 if ((*i)->start() > frame ) {
793 return (*i)->start ();
795 if ((*i)->end() > frame) {
806 Locations::end_location () const
808 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
809 if ((*i)->is_end()) {
810 return const_cast<Location*> (*i);
817 Locations::start_location () const
819 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
820 if ((*i)->is_start()) {
821 return const_cast<Location*> (*i);
828 Locations::auto_loop_location () const
830 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
831 if ((*i)->is_auto_loop()) {
832 return const_cast<Location*> (*i);
839 Locations::auto_punch_location () const
841 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
842 if ((*i)->is_auto_punch()) {
843 return const_cast<Location*> (*i);
850 Locations::num_range_markers () const
853 Glib::Mutex::Lock lm (lock);
854 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
855 if ((*i)->is_range_marker()) {
863 Locations::get_location_by_id(PBD::ID id)
865 LocationList::iterator it;
866 for (it = locations.begin(); it != locations.end(); it++)
867 if (id == (*it)->id())