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);
58 Location::Location (const XMLNode& node)
60 if (set_state (node)) {
61 throw failed_constructor ();
66 Location::operator= (const Location& other)
73 _start = other._start;
75 _flags = other._flags;
78 /* "changed" not emitted on purpose */
84 Location::set_start (nframes_t s)
96 start_changed(this); /* EMIT SIGNAL */
100 Session::StartTimeChanged (); /* EMIT SIGNAL */
101 AudioFileSource::set_header_position_offset ( s );
105 Session::EndTimeChanged (); /* EMIT SIGNAL */
111 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
117 start_changed(this); /* EMIT SIGNAL */
124 Location::set_end (nframes_t e)
134 end_changed(this); /* EMIT SIGNAL */
139 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
145 end_changed(this); /* EMIT SIGNAL */
151 Location::set (nframes_t start, nframes_t end)
157 if (is_mark() && start != end) {
159 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
163 if (_start != start) {
165 start_changed(this); /* EMIT SIGNAL */
170 end_changed(this); /* EMIT SIGNAL */
176 Location::move_to (nframes_t pos)
184 _end = _start + length();
186 changed (this); /* EMIT SIGNAL */
193 Location::set_hidden (bool yn, void *src)
195 if (set_flag_internal (yn, IsHidden)) {
196 FlagsChanged (this, src); /* EMIT SIGNAL */
201 Location::set_cd (bool yn, void *src)
203 // XXX this really needs to be session start
204 // but its not available here - leave to GUI
207 error << _("You cannot put a CD marker at this position") << endmsg;
211 if (set_flag_internal (yn, IsCDMarker)) {
212 FlagsChanged (this, src); /* EMIT SIGNAL */
217 Location::set_is_end (bool yn, void *src)
219 if (set_flag_internal (yn, IsEnd)) {
220 FlagsChanged (this, src); /* EMIT SIGNAL */
225 Location::set_is_start (bool yn, void *src)
227 if (set_flag_internal (yn, IsStart)) {
228 FlagsChanged (this, src); /* EMIT SIGNAL */
233 Location::set_auto_punch (bool yn, void *src)
235 if (is_mark() || _start == _end) {
239 if (set_flag_internal (yn, IsAutoPunch)) {
240 FlagsChanged (this, src); /* EMIT SIGNAL */
245 Location::set_auto_loop (bool yn, void *src)
247 if (is_mark() || _start == _end) {
251 if (set_flag_internal (yn, IsAutoLoop)) {
252 FlagsChanged (this, src); /* EMIT SIGNAL */
257 Location::set_flag_internal (bool yn, Flags flag)
260 if (!(_flags & flag)) {
261 _flags = Flags (_flags | flag);
266 _flags = Flags (_flags & ~flag);
274 Location::set_mark (bool yn)
276 /* This function is private, and so does not emit signals */
278 if (_start != _end) {
282 set_flag_internal (yn, IsMark);
287 Location::cd_info_node(const string & name, const string & value)
289 XMLNode* root = new XMLNode("CD-Info");
291 root->add_property("name", name);
292 root->add_property("value", value);
299 Location::get_state (void)
301 XMLNode *node = new XMLNode ("Location");
304 typedef map<string, string>::const_iterator CI;
306 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
307 node->add_child_nocopy(cd_info_node(m->first, m->second));
310 id().print (buf, sizeof (buf));
311 node->add_property("id", buf);
312 node->add_property ("name", name());
313 snprintf (buf, sizeof (buf), "%u", start());
314 node->add_property ("start", buf);
315 snprintf (buf, sizeof (buf), "%u", end());
316 node->add_property ("end", buf);
317 node->add_property ("flags", enum_2_string (_flags));
318 node->add_property ("locked", (_locked ? "yes" : "no"));
324 Location::set_state (const XMLNode& node)
326 const XMLProperty *prop;
328 XMLNodeList cd_list = node.children();
329 XMLNodeConstIterator cd_iter;
335 if (node.name() != "Location") {
336 error << _("incorrect XML node passed to Location::set_state") << endmsg;
340 if ((prop = node.property ("id")) == 0) {
341 warning << _("XML node for Location has no ID information") << endmsg;
343 _id = prop->value ();
346 if ((prop = node.property ("name")) == 0) {
347 error << _("XML node for Location has no name information") << endmsg;
351 set_name (prop->value());
353 if ((prop = node.property ("start")) == 0) {
354 error << _("XML node for Location has no start information") << endmsg;
358 /* can't use set_start() here, because _end
359 may make the value of _start illegal.
362 _start = atoi (prop->value().c_str());
364 if ((prop = node.property ("end")) == 0) {
365 error << _("XML node for Location has no end information") << endmsg;
369 _end = atoi (prop->value().c_str());
371 if ((prop = node.property ("flags")) == 0) {
372 error << _("XML node for Location has no flags information") << endmsg;
376 _flags = Flags (string_2_enum (prop->value(), _flags));
378 if ((prop = node.property ("locked")) != 0) {
379 _locked = (prop->value() == "yes");
384 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
388 if (cd_node->name() != "CD-Info") {
392 if ((prop = cd_node->property ("name")) != 0) {
393 cd_name = prop->value();
395 throw failed_constructor ();
398 if ((prop = cd_node->property ("value")) != 0) {
399 cd_value = prop->value();
401 throw failed_constructor ();
405 cd_info[cd_name] = cd_value;
409 changed(this); /* EMIT SIGNAL */
414 /*---------------------------------------------------------------------- */
416 Locations::Locations ()
419 current_location = 0;
422 Locations::~Locations ()
424 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
425 LocationList::iterator tmp = i;
433 Locations::set_current (Location *loc, bool want_lock)
439 Glib::Mutex::Lock lm (lock);
440 ret = set_current_unlocked (loc);
442 ret = set_current_unlocked (loc);
446 current_changed (current_location); /* EMIT SIGNAL */
452 Locations::next_available_name(string& result,string base)
454 LocationList::iterator i;
460 bool available[SUFFIX_MAX+1];
463 for (int k=1; k<SUFFIX_MAX; k++) {
467 for (i = locations.begin(); i != locations.end(); ++i) {
469 temp = location->name();
470 if (l && !temp.find(base,0)) {
471 suffix = atoi(temp.substr(l,3));
472 if (suffix) available[suffix] = false;
475 for (int k=1; k<=SUFFIX_MAX; k++) {
477 snprintf (buf, 31, "%d", k);
486 Locations::set_current_unlocked (Location *loc)
488 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
489 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
493 current_location = loc;
501 Glib::Mutex::Lock lm (lock);
503 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
505 LocationList::iterator tmp = i;
508 if (!(*i)->is_end() && !(*i)->is_start()) {
515 current_location = 0;
518 changed (); /* EMIT SIGNAL */
519 current_changed (0); /* EMIT SIGNAL */
523 Locations::clear_markers ()
526 Glib::Mutex::Lock lm (lock);
527 LocationList::iterator tmp;
529 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
533 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
541 changed (); /* EMIT SIGNAL */
545 Locations::clear_ranges ()
548 Glib::Mutex::Lock lm (lock);
549 LocationList::iterator tmp;
551 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
556 if (!(*i)->is_mark()) {
564 current_location = 0;
567 changed (); /* EMIT SIGNAL */
568 current_changed (0); /* EMIT SIGNAL */
572 Locations::add (Location *loc, bool make_current)
575 Glib::Mutex::Lock lm (lock);
576 locations.push_back (loc);
579 current_location = loc;
583 added (loc); /* EMIT SIGNAL */
586 current_changed (current_location); /* EMIT SIGNAL */
591 Locations::remove (Location *loc)
594 bool was_removed = false;
595 bool was_current = false;
596 LocationList::iterator i;
598 if (loc->is_end() || loc->is_start()) {
603 Glib::Mutex::Lock lm (lock);
605 for (i = locations.begin(); i != locations.end(); ++i) {
609 if (current_location == loc) {
610 current_location = 0;
620 removed (loc); /* EMIT SIGNAL */
623 current_changed (0); /* EMIT SIGNAL */
626 changed (); /* EMIT_SIGNAL */
631 Locations::location_changed (Location* loc)
633 changed (); /* EMIT SIGNAL */
637 Locations::get_state ()
639 XMLNode *node = new XMLNode ("Locations");
640 LocationList::iterator iter;
641 Glib::Mutex::Lock lm (lock);
643 for (iter = locations.begin(); iter != locations.end(); ++iter) {
644 node->add_child_nocopy ((*iter)->get_state ());
651 Locations::set_state (const XMLNode& node)
654 XMLNodeConstIterator niter;
656 if (node.name() != "Locations") {
657 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
661 nlist = node.children();
664 current_location = 0;
667 Glib::Mutex::Lock lm (lock);
669 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
673 Location *loc = new Location (**niter);
674 locations.push_back (loc);
677 catch (failed_constructor& err) {
678 error << _("could not load location from session file - ignored") << endmsg;
682 if (locations.size()) {
684 current_location = locations.front();
686 current_location = 0;
690 changed (); /* EMIT SIGNAL */
695 struct LocationStartEarlierComparison
697 bool operator() (Location *a, Location *b) {
698 return a->start() < b->start();
702 struct LocationStartLaterComparison
704 bool operator() (Location *a, Location *b) {
705 return a->start() > b->start();
710 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
715 Glib::Mutex::Lock lm (lock);
719 LocationStartLaterComparison cmp;
722 /* locs is now sorted latest..earliest */
724 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
725 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
728 if (!(*i)->is_hidden() && (*i)->start() < frame) {
737 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
742 Glib::Mutex::Lock lm (lock);
746 LocationStartEarlierComparison cmp;
749 /* locs is now sorted earliest..latest */
751 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
752 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
755 if (!(*i)->is_hidden() && (*i)->start() > frame) {
764 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
769 Glib::Mutex::Lock lm (lock);
773 LocationStartLaterComparison cmp;
776 /* locs is now sorted latest..earliest */
778 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
779 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
782 if (!(*i)->is_hidden()) {
783 if ((*i)->is_mark()) {
784 /* MARK: start == end */
785 if ((*i)->start() < frame) {
786 return (*i)->start();
789 /* RANGE: start != end, compare start and end */
790 if ((*i)->end() < frame) {
793 if ((*i)->start () < frame) {
794 return (*i)->start();
804 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
809 Glib::Mutex::Lock lm (lock);
813 LocationStartEarlierComparison cmp;
816 /* locs is now sorted earliest..latest */
818 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
819 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
822 if (!(*i)->is_hidden()) {
823 if ((*i)->is_mark()) {
824 /* MARK, start == end so just compare start */
825 if ((*i)->start() > frame) {
826 return (*i)->start();
829 /* RANGE, start != end, compare start and end */
830 if ((*i)->start() > frame ) {
831 return (*i)->start ();
833 if ((*i)->end() > frame) {
844 Locations::end_location () const
846 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
847 if ((*i)->is_end()) {
848 return const_cast<Location*> (*i);
855 Locations::start_location () const
857 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
858 if ((*i)->is_start()) {
859 return const_cast<Location*> (*i);
866 Locations::auto_loop_location () const
868 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
869 if ((*i)->is_auto_loop()) {
870 return const_cast<Location*> (*i);
877 Locations::auto_punch_location () const
879 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
880 if ((*i)->is_auto_punch()) {
881 return const_cast<Location*> (*i);
888 Locations::num_range_markers () const
891 Glib::Mutex::Lock lm (lock);
892 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
893 if ((*i)->is_range_marker()) {
901 Locations::get_location_by_id(PBD::ID id)
903 LocationList::iterator it;
904 for (it = locations.begin(); it != locations.end(); it++)
905 if (id == (*it)->id())