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 */
28 #include <sigc++/bind.h>
30 #include "pbd/stl_delete.h"
31 #include "pbd/xml++.h"
32 #include "pbd/enumwriter.h"
34 #include "ardour/location.h"
35 #include "ardour/session.h"
36 #include "ardour/audiofilesource.h"
43 using namespace ARDOUR;
47 Location::Location (const Location& other)
48 : StatefulDestructible(),
50 _start (other._start),
54 /* start and end flags can never be copied, because there can only ever be one of each */
56 _flags = Flags (_flags & ~IsStart);
57 _flags = Flags (_flags & ~IsEnd);
59 /* copy is not locked even if original was */
64 Location::Location (const XMLNode& node)
66 if (set_state (node, Stateful::loading_state_version)) {
67 throw failed_constructor ();
72 Location::operator= (const Location& other)
79 _start = other._start;
81 _flags = other._flags;
83 /* copy is not locked even if original was */
87 /* "changed" not emitted on purpose */
93 Location::set_start (nframes64_t s)
105 start_changed(this); /* EMIT SIGNAL */
106 end_changed(this); /* EMIT SIGNAL */
110 Session::StartTimeChanged (); /* EMIT SIGNAL */
111 AudioFileSource::set_header_position_offset ( s );
115 Session::EndTimeChanged (); /* EMIT SIGNAL */
121 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
127 start_changed(this); /* EMIT SIGNAL */
134 Location::set_end (nframes64_t e)
144 start_changed(this); /* EMIT SIGNAL */
145 end_changed(this); /* EMIT SIGNAL */
148 Session::StartTimeChanged (); /* EMIT SIGNAL */
152 Session::EndTimeChanged (); /* EMIT SIGNAL */
159 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
165 end_changed(this); /* EMIT SIGNAL */
171 Location::set (nframes64_t start, nframes64_t end)
177 if (is_mark() && start != end) {
179 } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
183 if (_start != start) {
185 start_changed(this); /* EMIT SIGNAL */
190 end_changed(this); /* EMIT SIGNAL */
196 Location::move_to (nframes64_t pos)
204 _end = _start + length();
206 changed (this); /* EMIT SIGNAL */
213 Location::set_hidden (bool yn, void *src)
215 if (set_flag_internal (yn, IsHidden)) {
216 FlagsChanged (this, src); /* EMIT SIGNAL */
221 Location::set_cd (bool yn, void *src)
223 // XXX this really needs to be session start
224 // but its not available here - leave to GUI
227 error << _("You cannot put a CD marker at this position") << endmsg;
231 if (set_flag_internal (yn, IsCDMarker)) {
232 FlagsChanged (this, src); /* EMIT SIGNAL */
237 Location::set_is_end (bool yn, void *src)
239 if (set_flag_internal (yn, IsEnd)) {
240 FlagsChanged (this, src); /* EMIT SIGNAL */
245 Location::set_is_start (bool yn, void *src)
247 if (set_flag_internal (yn, IsStart)) {
248 FlagsChanged (this, src); /* EMIT SIGNAL */
253 Location::set_is_range_marker (bool yn, void *src)
255 if (set_flag_internal (yn, IsRangeMarker)) {
256 FlagsChanged (this, src); /* EMIT SIGNAL */
261 Location::set_auto_punch (bool yn, void *src)
263 if (is_mark() || _start == _end) {
267 if (set_flag_internal (yn, IsAutoPunch)) {
268 FlagsChanged (this, src); /* EMIT SIGNAL */
273 Location::set_auto_loop (bool yn, void *src)
275 if (is_mark() || _start == _end) {
279 if (set_flag_internal (yn, IsAutoLoop)) {
280 FlagsChanged (this, src); /* EMIT SIGNAL */
285 Location::set_flag_internal (bool yn, Flags flag)
288 if (!(_flags & flag)) {
289 _flags = Flags (_flags | flag);
294 _flags = Flags (_flags & ~flag);
302 Location::set_mark (bool yn)
304 /* This function is private, and so does not emit signals */
306 if (_start != _end) {
310 set_flag_internal (yn, IsMark);
315 Location::cd_info_node(const string & name, const string & value)
317 XMLNode* root = new XMLNode("CD-Info");
319 root->add_property("name", name);
320 root->add_property("value", value);
327 Location::get_state (void)
329 XMLNode *node = new XMLNode ("Location");
332 typedef map<string, string>::const_iterator CI;
334 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
335 node->add_child_nocopy(cd_info_node(m->first, m->second));
338 id().print (buf, sizeof (buf));
339 node->add_property("id", buf);
340 node->add_property ("name", name());
341 snprintf (buf, sizeof (buf), "%" PRId64, start());
342 node->add_property ("start", buf);
343 snprintf (buf, sizeof (buf), "%" PRId64, end());
344 node->add_property ("end", buf);
345 node->add_property ("flags", enum_2_string (_flags));
346 node->add_property ("locked", (_locked ? "yes" : "no"));
352 Location::set_state (const XMLNode& node, int /*version*/)
354 const XMLProperty *prop;
356 XMLNodeList cd_list = node.children();
357 XMLNodeConstIterator cd_iter;
363 if (node.name() != "Location") {
364 error << _("incorrect XML node passed to Location::set_state") << endmsg;
368 if ((prop = node.property ("id")) == 0) {
369 warning << _("XML node for Location has no ID information") << endmsg;
371 _id = prop->value ();
374 if ((prop = node.property ("name")) == 0) {
375 error << _("XML node for Location has no name information") << endmsg;
379 set_name (prop->value());
381 if ((prop = node.property ("start")) == 0) {
382 error << _("XML node for Location has no start information") << endmsg;
386 /* can't use set_start() here, because _end
387 may make the value of _start illegal.
390 sscanf (prop->value().c_str(), "%" PRId64, &_start);
392 if ((prop = node.property ("end")) == 0) {
393 error << _("XML node for Location has no end information") << endmsg;
397 sscanf (prop->value().c_str(), "%" PRId64, &_end);
399 if ((prop = node.property ("flags")) == 0) {
400 error << _("XML node for Location has no flags information") << endmsg;
404 _flags = Flags (string_2_enum (prop->value(), _flags));
406 if ((prop = node.property ("locked")) != 0) {
407 _locked = string_is_affirmative (prop->value());
412 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
416 if (cd_node->name() != "CD-Info") {
420 if ((prop = cd_node->property ("name")) != 0) {
421 cd_name = prop->value();
423 throw failed_constructor ();
426 if ((prop = cd_node->property ("value")) != 0) {
427 cd_value = prop->value();
429 throw failed_constructor ();
433 cd_info[cd_name] = cd_value;
437 changed(this); /* EMIT SIGNAL */
442 /*---------------------------------------------------------------------- */
444 Locations::Locations ()
447 current_location = 0;
450 Locations::~Locations ()
452 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
453 LocationList::iterator tmp = i;
461 Locations::set_current (Location *loc, bool want_lock)
467 Glib::Mutex::Lock lm (lock);
468 ret = set_current_unlocked (loc);
470 ret = set_current_unlocked (loc);
474 current_changed (current_location); /* EMIT SIGNAL */
480 Locations::next_available_name(string& result,string base)
482 LocationList::iterator i;
488 bool available[SUFFIX_MAX+1];
491 for (int k=1; k<SUFFIX_MAX; k++) {
495 for (i = locations.begin(); i != locations.end(); ++i) {
497 temp = location->name();
498 if (l && !temp.find(base,0)) {
499 suffix = atoi(temp.substr(l,3).c_str());
500 if (suffix) available[suffix] = false;
503 for (int k=1; k<=SUFFIX_MAX; k++) {
505 snprintf (buf, 31, "%d", k);
514 Locations::set_current_unlocked (Location *loc)
516 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
517 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
521 current_location = loc;
529 Glib::Mutex::Lock lm (lock);
531 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
533 LocationList::iterator tmp = i;
536 if (!(*i)->is_end() && !(*i)->is_start()) {
543 current_location = 0;
546 changed (); /* EMIT SIGNAL */
547 current_changed (0); /* EMIT SIGNAL */
551 Locations::clear_markers ()
554 Glib::Mutex::Lock lm (lock);
555 LocationList::iterator tmp;
557 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
561 if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
569 changed (); /* EMIT SIGNAL */
573 Locations::clear_ranges ()
576 Glib::Mutex::Lock lm (lock);
577 LocationList::iterator tmp;
579 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
584 if (!(*i)->is_mark()) {
592 current_location = 0;
595 changed (); /* EMIT SIGNAL */
596 current_changed (0); /* EMIT SIGNAL */
600 Locations::add (Location *loc, bool make_current)
603 Glib::Mutex::Lock lm (lock);
604 locations.push_back (loc);
607 current_location = loc;
611 added (loc); /* EMIT SIGNAL */
614 current_changed (current_location); /* EMIT SIGNAL */
619 Locations::remove (Location *loc)
622 bool was_removed = false;
623 bool was_current = false;
624 LocationList::iterator i;
626 if (loc->is_end() || loc->is_start()) {
631 Glib::Mutex::Lock lm (lock);
633 for (i = locations.begin(); i != locations.end(); ++i) {
637 if (current_location == loc) {
638 current_location = 0;
648 removed (loc); /* EMIT SIGNAL */
651 current_changed (0); /* EMIT SIGNAL */
654 changed (); /* EMIT_SIGNAL */
659 Locations::location_changed (Location* /*loc*/)
661 changed (); /* EMIT SIGNAL */
665 Locations::get_state ()
667 XMLNode *node = new XMLNode ("Locations");
668 LocationList::iterator iter;
669 Glib::Mutex::Lock lm (lock);
671 for (iter = locations.begin(); iter != locations.end(); ++iter) {
672 node->add_child_nocopy ((*iter)->get_state ());
679 Locations::set_state (const XMLNode& node, int /*version*/)
682 XMLNodeConstIterator niter;
684 if (node.name() != "Locations") {
685 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
689 nlist = node.children();
692 current_location = 0;
695 Glib::Mutex::Lock lm (lock);
697 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
701 Location *loc = new Location (**niter);
702 locations.push_back (loc);
705 catch (failed_constructor& err) {
706 error << _("could not load location from session file - ignored") << endmsg;
710 if (locations.size()) {
712 current_location = locations.front();
714 current_location = 0;
718 changed (); /* EMIT SIGNAL */
723 struct LocationStartEarlierComparison
725 bool operator() (Location *a, Location *b) {
726 return a->start() < b->start();
730 struct LocationStartLaterComparison
732 bool operator() (Location *a, Location *b) {
733 return a->start() > b->start();
738 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
743 Glib::Mutex::Lock lm (lock);
747 LocationStartLaterComparison cmp;
750 /* locs is now sorted latest..earliest */
752 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
753 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
756 if (!(*i)->is_hidden() && (*i)->start() < frame) {
765 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
770 Glib::Mutex::Lock lm (lock);
774 LocationStartEarlierComparison cmp;
777 /* locs is now sorted earliest..latest */
779 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
780 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
783 if (!(*i)->is_hidden() && (*i)->start() > frame) {
791 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
793 * @param frame Frame to look for.
794 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
795 * @param after Filled in with the position of the last `mark' after `frame' (or max_frames if none exists)
798 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
800 before = after = max_frames;
805 Glib::Mutex::Lock lm (lock);
809 std::list<nframes64_t> positions;
811 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
812 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
816 if (!(*i)->is_hidden()) {
817 if ((*i)->is_mark ()) {
818 positions.push_back ((*i)->start ());
820 positions.push_back ((*i)->start ());
821 positions.push_back ((*i)->end ());
826 if (positions.empty ()) {
832 std::list<nframes64_t>::iterator i = positions.begin ();
833 while (i != positions.end () && *i < frame) {
837 if (i == positions.end ()) {
838 /* run out of marks */
839 before = positions.back ();
845 if (i == positions.begin ()) {
855 Locations::end_location () const
857 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
858 if ((*i)->is_end()) {
859 return const_cast<Location*> (*i);
866 Locations::start_location () const
868 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
869 if ((*i)->is_start()) {
870 return const_cast<Location*> (*i);
877 Locations::auto_loop_location () const
879 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
880 if ((*i)->is_auto_loop()) {
881 return const_cast<Location*> (*i);
888 Locations::auto_punch_location () const
890 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
891 if ((*i)->is_auto_punch()) {
892 return const_cast<Location*> (*i);
899 Locations::num_range_markers () const
902 Glib::Mutex::Lock lm (lock);
903 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
904 if ((*i)->is_range_marker()) {
912 Locations::get_location_by_id(PBD::ID id)
914 LocationList::iterator it;
915 for (it = locations.begin(); it != locations.end(); it++)
916 if (id == (*it)->id())
923 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
925 Glib::Mutex::Lock lm (lock);
927 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
928 if ((flags == 0 || (*i)->matches (flags)) &&
929 ((*i)->start() >= start && (*i)->end() < end)) {