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 if (set_flag_internal (yn, IsCDMarker)) {
173 FlagsChanged (this, src); /* EMIT SIGNAL */
178 Location::set_is_end (bool yn, void *src)
180 if (set_flag_internal (yn, IsEnd)) {
181 FlagsChanged (this, src); /* EMIT SIGNAL */
186 Location::set_is_start (bool yn, void *src)
188 if (set_flag_internal (yn, IsStart)) {
189 FlagsChanged (this, src); /* EMIT SIGNAL */
194 Location::set_auto_punch (bool yn, void *src)
196 if (is_mark() || _start == _end) {
200 if (set_flag_internal (yn, IsAutoPunch)) {
201 FlagsChanged (this, src); /* EMIT SIGNAL */
206 Location::set_auto_loop (bool yn, void *src)
208 if (is_mark() || _start == _end) {
212 if (set_flag_internal (yn, IsAutoLoop)) {
213 FlagsChanged (this, src); /* EMIT SIGNAL */
218 Location::set_flag_internal (bool yn, Flags flag)
221 if (!(_flags & flag)) {
222 _flags = Flags (_flags | flag);
227 _flags = Flags (_flags & ~flag);
235 Location::set_mark (bool yn)
237 /* This function is private, and so does not emit signals */
239 if (_start != _end) {
243 set_flag_internal (yn, IsMark);
248 Location::cd_info_node(const string & name, const string & value)
250 XMLNode* root = new XMLNode("CD-Info");
252 root->add_property("name", name);
253 root->add_property("value", value);
260 Location::get_state (void)
262 XMLNode *node = new XMLNode ("Location");
265 typedef map<string, string>::const_iterator CI;
267 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
268 node->add_child_nocopy(cd_info_node(m->first, m->second));
271 id().print (buf, sizeof (buf));
272 node->add_property("id", buf);
273 node->add_property ("name", name());
274 snprintf (buf, sizeof (buf), "%u", start());
275 node->add_property ("start", buf);
276 snprintf (buf, sizeof (buf), "%u", end());
277 node->add_property ("end", buf);
278 node->add_property ("flags", enum_2_string (_flags));
284 Location::set_state (const XMLNode& node)
286 const XMLProperty *prop;
288 XMLNodeList cd_list = node.children();
289 XMLNodeConstIterator cd_iter;
295 if (node.name() != "Location") {
296 error << _("incorrect XML node passed to Location::set_state") << endmsg;
300 if ((prop = node.property ("id")) == 0) {
301 warning << _("XML node for Location has no ID information") << endmsg;
303 _id = prop->value ();
306 if ((prop = node.property ("name")) == 0) {
307 error << _("XML node for Location has no name information") << endmsg;
311 set_name (prop->value());
313 if ((prop = node.property ("start")) == 0) {
314 error << _("XML node for Location has no start information") << endmsg;
318 /* can't use set_start() here, because _end
319 may make the value of _start illegal.
322 _start = atoi (prop->value().c_str());
324 if ((prop = node.property ("end")) == 0) {
325 error << _("XML node for Location has no end information") << endmsg;
329 _end = atoi (prop->value().c_str());
331 if ((prop = node.property ("flags")) == 0) {
332 error << _("XML node for Location has no flags information") << endmsg;
336 _flags = Flags (string_2_enum (prop->value(), _flags));
338 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
342 if (cd_node->name() != "CD-Info") {
346 if ((prop = cd_node->property ("name")) != 0) {
347 cd_name = prop->value();
349 throw failed_constructor ();
352 if ((prop = cd_node->property ("value")) != 0) {
353 cd_value = prop->value();
355 throw failed_constructor ();
359 cd_info[cd_name] = cd_value;
363 changed(this); /* EMIT SIGNAL */
368 /*---------------------------------------------------------------------- */
370 Locations::Locations ()
373 current_location = 0;
376 Locations::~Locations ()
378 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
379 LocationList::iterator tmp = i;
387 Locations::set_current (Location *loc, bool want_lock)
393 Glib::Mutex::Lock lm (lock);
394 ret = set_current_unlocked (loc);
396 ret = set_current_unlocked (loc);
400 current_changed (current_location); /* EMIT SIGNAL */
406 Locations::next_available_name(string& result,string base)
408 LocationList::iterator i;
414 bool available[SUFFIX_MAX+1];
417 for (int k=1; k<SUFFIX_MAX; k++) {
421 for (i = locations.begin(); i != locations.end(); ++i) {
423 temp = location->name();
424 if (l && !temp.find(base,0)) {
425 suffix = atoi(temp.substr(l,3).c_str());
426 if (suffix) available[suffix] = false;
429 for (int k=1; k<=SUFFIX_MAX; k++) {
431 snprintf (buf, 31, "%d", k);
440 Locations::set_current_unlocked (Location *loc)
442 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
443 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
447 current_location = loc;
455 Glib::Mutex::Lock lm (lock);
457 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
459 LocationList::iterator tmp = i;
462 if (!(*i)->is_end() && !(*i)->is_start()) {
469 current_location = 0;
472 changed (); /* EMIT SIGNAL */
473 current_changed (0); /* EMIT SIGNAL */
477 Locations::clear_markers ()
480 Glib::Mutex::Lock lm (lock);
481 LocationList::iterator tmp;
483 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
487 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
495 changed (); /* EMIT SIGNAL */
499 Locations::clear_ranges ()
502 Glib::Mutex::Lock lm (lock);
503 LocationList::iterator tmp;
505 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
510 if (!(*i)->is_mark()) {
518 current_location = 0;
521 changed (); /* EMIT SIGNAL */
522 current_changed (0); /* EMIT SIGNAL */
526 Locations::add (Location *loc, bool make_current)
529 Glib::Mutex::Lock lm (lock);
530 locations.push_back (loc);
533 current_location = loc;
537 added (loc); /* EMIT SIGNAL */
540 current_changed (current_location); /* EMIT SIGNAL */
545 Locations::remove (Location *loc)
548 bool was_removed = false;
549 bool was_current = false;
550 LocationList::iterator i;
552 if (loc->is_end() || loc->is_start()) {
557 Glib::Mutex::Lock lm (lock);
559 for (i = locations.begin(); i != locations.end(); ++i) {
563 if (current_location == loc) {
564 current_location = 0;
574 removed (loc); /* EMIT SIGNAL */
577 current_changed (0); /* EMIT SIGNAL */
580 changed (); /* EMIT_SIGNAL */
585 Locations::location_changed (Location* loc)
587 changed (); /* EMIT SIGNAL */
591 Locations::get_state ()
593 XMLNode *node = new XMLNode ("Locations");
594 LocationList::iterator iter;
595 Glib::Mutex::Lock lm (lock);
597 for (iter = locations.begin(); iter != locations.end(); ++iter) {
598 node->add_child_nocopy ((*iter)->get_state ());
605 Locations::set_state (const XMLNode& node)
608 XMLNodeConstIterator niter;
610 if (node.name() != "Locations") {
611 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
615 nlist = node.children();
618 current_location = 0;
621 Glib::Mutex::Lock lm (lock);
623 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
627 Location *loc = new Location (**niter);
628 locations.push_back (loc);
631 catch (failed_constructor& err) {
632 error << _("could not load location from session file - ignored") << endmsg;
636 if (locations.size()) {
638 current_location = locations.front();
640 current_location = 0;
644 changed (); /* EMIT SIGNAL */
649 struct LocationStartEarlierComparison
651 bool operator() (Location *a, Location *b) {
652 return a->start() < b->start();
656 struct LocationStartLaterComparison
658 bool operator() (Location *a, Location *b) {
659 return a->start() > b->start();
664 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
669 Glib::Mutex::Lock lm (lock);
673 LocationStartLaterComparison cmp;
676 /* locs is now sorted latest..earliest */
678 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
679 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
682 if (!(*i)->is_hidden() && (*i)->start() < frame) {
691 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
696 Glib::Mutex::Lock lm (lock);
700 LocationStartEarlierComparison cmp;
703 /* locs is now sorted earliest..latest */
705 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
706 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
709 if (!(*i)->is_hidden() && (*i)->start() > frame) {
718 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
723 Glib::Mutex::Lock lm (lock);
727 LocationStartLaterComparison cmp;
730 /* locs is now sorted latest..earliest */
732 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
733 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
736 if (!(*i)->is_hidden()) {
737 if ((*i)->is_mark()) {
738 /* MARK: start == end */
739 if ((*i)->start() < frame) {
740 return (*i)->start();
743 /* RANGE: start != end, compare start and end */
744 if ((*i)->end() < frame) {
747 if ((*i)->start () < frame) {
748 return (*i)->start();
758 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
763 Glib::Mutex::Lock lm (lock);
767 LocationStartEarlierComparison cmp;
770 /* locs is now sorted earliest..latest */
772 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
773 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
776 if (!(*i)->is_hidden()) {
777 if ((*i)->is_mark()) {
778 /* MARK, start == end so just compare start */
779 if ((*i)->start() > frame) {
780 return (*i)->start();
783 /* RANGE, start != end, compare start and end */
784 if ((*i)->start() > frame ) {
785 return (*i)->start ();
787 if ((*i)->end() > frame) {
798 Locations::end_location () const
800 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
801 if ((*i)->is_end()) {
802 return const_cast<Location*> (*i);
809 Locations::start_location () const
811 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
812 if ((*i)->is_start()) {
813 return const_cast<Location*> (*i);
820 Locations::auto_loop_location () const
822 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
823 if ((*i)->is_auto_loop()) {
824 return const_cast<Location*> (*i);
831 Locations::auto_punch_location () const
833 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
834 if ((*i)->is_auto_punch()) {
835 return const_cast<Location*> (*i);
842 Locations::num_range_markers () const
845 Glib::Mutex::Lock lm (lock);
846 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
847 if ((*i)->is_range_marker()) {
855 Locations::get_location_by_id(PBD::ID id)
857 LocationList::iterator it;
858 for (it = locations.begin(); it != locations.end(); it++)
859 if (id == (*it)->id())