fix up file renaming code a little bit
[ArdourMidi.git] / libs / ardour / location.cc
blob3f62d8949d255b103bc0c0d387b875839ef0df7c
1 /*
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.
20 #include <algorithm>
21 #include <set>
22 #include <cstdio> /* for sprintf */
23 #include <unistd.h>
24 #include <cerrno>
25 #include <ctime>
26 #include <list>
29 #include "pbd/stl_delete.h"
30 #include "pbd/xml++.h"
31 #include "pbd/enumwriter.h"
33 #include "ardour/location.h"
34 #include "ardour/session.h"
35 #include "ardour/audiofilesource.h"
37 #include "i18n.h"
39 #define SUFFIX_MAX 32
41 using namespace std;
42 using namespace ARDOUR;
43 using namespace PBD;
45 Location::Location (const Location& other)
46 : StatefulDestructible(),
47 _name (other._name),
48 _start (other._start),
49 _end (other._end),
50 _flags (other._flags)
52 /* copy is not locked even if original was */
54 _locked = false;
57 Location::Location (const XMLNode& node)
59 if (set_state (node, Stateful::loading_state_version)) {
60 throw failed_constructor ();
64 Location*
65 Location::operator= (const Location& other)
67 if (this == &other) {
68 return this;
71 _name = other._name;
72 _start = other._start;
73 _end = other._end;
74 _flags = other._flags;
76 /* copy is not locked even if original was */
78 _locked = false;
80 /* "changed" not emitted on purpose */
82 return this;
85 /** Set start position.
86 * @param s New start.
87 * @param force true to force setting, even if the given new start is after the current end.
89 int
90 Location::set_start (nframes64_t s, bool force)
92 if (_locked) {
93 return -1;
96 if (!force) {
97 if (((is_auto_punch() || is_auto_loop()) && s >= _end) || (!is_mark() && s > _end)) {
98 return -1;
102 if (is_mark()) {
103 if (_start != s) {
104 _start = s;
105 _end = s;
106 start_changed (this); /* EMIT SIGNAL */
107 end_changed (this); /* EMIT SIGNAL */
109 return 0;
112 if (s != _start) {
113 _start = s;
114 start_changed (this); /* EMIT SIGNAL */
115 if (is_session_range ()) {
116 Session::StartTimeChanged (); /* EMIT SIGNAL */
117 AudioFileSource::set_header_position_offset (s);
121 return 0;
124 /** Set end position.
125 * @param s New end.
126 * @param force true to force setting, even if the given new start is after the current end.
129 Location::set_end (nframes64_t e, bool force)
131 if (_locked) {
132 return -1;
135 if (!force) {
136 if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
137 return -1;
141 if (is_mark()) {
142 if (_start != e) {
143 _start = e;
144 _end = e;
145 start_changed (this); /* EMIT SIGNAL */
146 end_changed (this); /* EMIT SIGNAL */
148 return 0;
151 if (e != _end) {
152 _end = e;
153 end_changed(this); /* EMIT SIGNAL */
155 if (is_session_range()) {
156 Session::EndTimeChanged (); /* EMIT SIGNAL */
160 return 0;
164 Location::set (nframes64_t start, nframes64_t end)
166 /* check validity */
167 if (((is_auto_punch() || is_auto_loop()) && start >= end) || (!is_mark() && start > end)) {
168 return -1;
171 /* now we know these values are ok, so force-set them */
172 int const s = set_start (start, true);
173 int const e = set_end (end, true);
175 return (s == 0 && e == 0) ? 0 : -1;
179 Location::move_to (nframes64_t pos)
181 if (_locked) {
182 return -1;
185 if (_start != pos) {
186 _start = pos;
187 _end = _start + length();
189 changed (this); /* EMIT SIGNAL */
192 return 0;
195 void
196 Location::set_hidden (bool yn, void *src)
198 if (set_flag_internal (yn, IsHidden)) {
199 FlagsChanged (this, src); /* EMIT SIGNAL */
203 void
204 Location::set_cd (bool yn, void *src)
206 // XXX this really needs to be session start
207 // but its not available here - leave to GUI
209 if (_start == 0) {
210 error << _("You cannot put a CD marker at this position") << endmsg;
211 return;
214 if (set_flag_internal (yn, IsCDMarker)) {
215 FlagsChanged (this, src); /* EMIT SIGNAL */
219 void
220 Location::set_is_range_marker (bool yn, void *src)
222 if (set_flag_internal (yn, IsRangeMarker)) {
223 FlagsChanged (this, src); /* EMIT SIGNAL */
227 void
228 Location::set_auto_punch (bool yn, void *src)
230 if (is_mark() || _start == _end) {
231 return;
234 if (set_flag_internal (yn, IsAutoPunch)) {
235 FlagsChanged (this, src); /* EMIT SIGNAL */
239 void
240 Location::set_auto_loop (bool yn, void *src)
242 if (is_mark() || _start == _end) {
243 return;
246 if (set_flag_internal (yn, IsAutoLoop)) {
247 FlagsChanged (this, src); /* EMIT SIGNAL */
251 bool
252 Location::set_flag_internal (bool yn, Flags flag)
254 if (yn) {
255 if (!(_flags & flag)) {
256 _flags = Flags (_flags | flag);
257 return true;
259 } else {
260 if (_flags & flag) {
261 _flags = Flags (_flags & ~flag);
262 return true;
265 return false;
268 void
269 Location::set_mark (bool yn)
271 /* This function is private, and so does not emit signals */
273 if (_start != _end) {
274 return;
277 set_flag_internal (yn, IsMark);
281 XMLNode&
282 Location::cd_info_node(const string & name, const string & value)
284 XMLNode* root = new XMLNode("CD-Info");
286 root->add_property("name", name);
287 root->add_property("value", value);
289 return *root;
293 XMLNode&
294 Location::get_state (void)
296 XMLNode *node = new XMLNode ("Location");
297 char buf[64];
299 typedef map<string, string>::const_iterator CI;
301 for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
302 node->add_child_nocopy(cd_info_node(m->first, m->second));
305 id().print (buf, sizeof (buf));
306 node->add_property("id", buf);
307 node->add_property ("name", name());
308 snprintf (buf, sizeof (buf), "%" PRId64, start());
309 node->add_property ("start", buf);
310 snprintf (buf, sizeof (buf), "%" PRId64, end());
311 node->add_property ("end", buf);
312 node->add_property ("flags", enum_2_string (_flags));
313 node->add_property ("locked", (_locked ? "yes" : "no"));
315 return *node;
319 Location::set_state (const XMLNode& node, int /*version*/)
321 const XMLProperty *prop;
323 XMLNodeList cd_list = node.children();
324 XMLNodeConstIterator cd_iter;
325 XMLNode *cd_node;
327 string cd_name;
328 string cd_value;
330 if (node.name() != "Location") {
331 error << _("incorrect XML node passed to Location::set_state") << endmsg;
332 return -1;
335 if ((prop = node.property ("id")) == 0) {
336 warning << _("XML node for Location has no ID information") << endmsg;
337 } else {
338 _id = prop->value ();
341 if ((prop = node.property ("name")) == 0) {
342 error << _("XML node for Location has no name information") << endmsg;
343 return -1;
346 set_name (prop->value());
348 if ((prop = node.property ("start")) == 0) {
349 error << _("XML node for Location has no start information") << endmsg;
350 return -1;
353 /* can't use set_start() here, because _end
354 may make the value of _start illegal.
357 sscanf (prop->value().c_str(), "%" PRId64, &_start);
359 if ((prop = node.property ("end")) == 0) {
360 error << _("XML node for Location has no end information") << endmsg;
361 return -1;
364 sscanf (prop->value().c_str(), "%" PRId64, &_end);
366 if ((prop = node.property ("flags")) == 0) {
367 error << _("XML node for Location has no flags information") << endmsg;
368 return -1;
371 _flags = Flags (string_2_enum (prop->value(), _flags));
373 if ((prop = node.property ("locked")) != 0) {
374 _locked = string_is_affirmative (prop->value());
375 } else {
376 _locked = false;
379 for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
381 cd_node = *cd_iter;
383 if (cd_node->name() != "CD-Info") {
384 continue;
387 if ((prop = cd_node->property ("name")) != 0) {
388 cd_name = prop->value();
389 } else {
390 throw failed_constructor ();
393 if ((prop = cd_node->property ("value")) != 0) {
394 cd_value = prop->value();
395 } else {
396 throw failed_constructor ();
400 cd_info[cd_name] = cd_value;
404 changed(this); /* EMIT SIGNAL */
406 return 0;
409 /*---------------------------------------------------------------------- */
411 Locations::Locations ()
414 current_location = 0;
417 Locations::~Locations ()
419 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
420 LocationList::iterator tmp = i;
421 ++tmp;
422 delete *i;
423 i = tmp;
428 Locations::set_current (Location *loc, bool want_lock)
431 int ret;
433 if (want_lock) {
434 Glib::Mutex::Lock lm (lock);
435 ret = set_current_unlocked (loc);
436 } else {
437 ret = set_current_unlocked (loc);
440 if (ret == 0) {
441 current_changed (current_location); /* EMIT SIGNAL */
443 return ret;
447 Locations::next_available_name(string& result,string base)
449 LocationList::iterator i;
450 Location* location;
451 string temp;
452 string::size_type l;
453 int suffix;
454 char buf[32];
455 bool available[SUFFIX_MAX+1];
457 result = base;
458 for (int k=1; k<SUFFIX_MAX; k++) {
459 available[k] = true;
461 l = base.length();
462 for (i = locations.begin(); i != locations.end(); ++i) {
463 location =* i;
464 temp = location->name();
465 if (l && !temp.find(base,0)) {
466 suffix = atoi(temp.substr(l,3).c_str());
467 if (suffix) available[suffix] = false;
470 for (int k=1; k<=SUFFIX_MAX; k++) {
471 if (available[k]) {
472 snprintf (buf, 31, "%d", k);
473 result += buf;
474 return 1;
477 return 0;
481 Locations::set_current_unlocked (Location *loc)
483 if (find (locations.begin(), locations.end(), loc) == locations.end()) {
484 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
485 return -1;
488 current_location = loc;
489 return 0;
492 void
493 Locations::clear ()
496 Glib::Mutex::Lock lm (lock);
498 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
500 LocationList::iterator tmp = i;
501 ++tmp;
503 if (!(*i)->is_session_range()) {
504 locations.erase (i);
507 i = tmp;
510 current_location = 0;
513 changed (OTHER); /* EMIT SIGNAL */
514 current_changed (0); /* EMIT SIGNAL */
517 void
518 Locations::clear_markers ()
521 Glib::Mutex::Lock lm (lock);
522 LocationList::iterator tmp;
524 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
525 tmp = i;
526 ++tmp;
528 if ((*i)->is_mark() && !(*i)->is_session_range()) {
529 locations.erase (i);
532 i = tmp;
536 changed (OTHER); /* EMIT SIGNAL */
539 void
540 Locations::clear_ranges ()
543 Glib::Mutex::Lock lm (lock);
544 LocationList::iterator tmp;
546 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
548 tmp = i;
549 ++tmp;
551 if (!(*i)->is_mark()) {
552 locations.erase (i);
556 i = tmp;
559 current_location = 0;
562 changed (OTHER); /* EMIT SIGNAL */
563 current_changed (0); /* EMIT SIGNAL */
566 void
567 Locations::add (Location *loc, bool make_current)
569 assert (loc);
572 Glib::Mutex::Lock lm (lock);
573 locations.push_back (loc);
575 if (make_current) {
576 current_location = loc;
580 added (loc); /* EMIT SIGNAL */
582 if (make_current) {
583 current_changed (current_location); /* EMIT SIGNAL */
587 void
588 Locations::remove (Location *loc)
590 bool was_removed = false;
591 bool was_current = false;
592 LocationList::iterator i;
594 if (loc->is_session_range()) {
595 return;
599 Glib::Mutex::Lock lm (lock);
601 for (i = locations.begin(); i != locations.end(); ++i) {
602 if ((*i) == loc) {
603 locations.erase (i);
604 was_removed = true;
605 if (current_location == loc) {
606 current_location = 0;
607 was_current = true;
609 break;
614 if (was_removed) {
616 removed (loc); /* EMIT SIGNAL */
618 if (was_current) {
619 current_changed (0); /* EMIT SIGNAL */
622 changed (REMOVAL); /* EMIT_SIGNAL */
626 void
627 Locations::location_changed (Location* /*loc*/)
629 changed (OTHER); /* EMIT SIGNAL */
632 XMLNode&
633 Locations::get_state ()
635 XMLNode *node = new XMLNode ("Locations");
636 LocationList::iterator iter;
637 Glib::Mutex::Lock lm (lock);
639 for (iter = locations.begin(); iter != locations.end(); ++iter) {
640 node->add_child_nocopy ((*iter)->get_state ());
643 return *node;
647 Locations::set_state (const XMLNode& node, int version)
649 if (node.name() != "Locations") {
650 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
651 return -1;
654 XMLNodeList nlist = node.children();
656 locations.clear ();
657 current_location = 0;
659 Location* session_range_location = 0;
660 if (version < 3000) {
661 session_range_location = new Location (0, 0, _("session"), Location::IsSessionRange);
662 locations.push_back (session_range_location);
666 Glib::Mutex::Lock lm (lock);
668 XMLNodeConstIterator niter;
669 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
671 try {
673 Location *loc = new Location (**niter);
675 bool add = true;
677 if (version < 3000) {
678 /* look for old-style IsStart / IsEnd properties in this location;
679 if they are present, update the session_range_location accordingly
681 XMLProperty const * prop = (*niter)->property ("flags");
682 if (prop) {
683 string v = prop->value ();
684 while (1) {
685 string::size_type const c = v.find_first_of (',');
686 string const s = v.substr (0, c);
687 if (s == X_("IsStart")) {
688 session_range_location->set_start (loc->start());
689 add = false;
690 } else if (s == X_("IsEnd")) {
691 session_range_location->set_end (loc->start());
692 add = false;
695 if (c == string::npos) {
696 break;
699 v = v.substr (c + 1);
704 if (add) {
705 locations.push_back (loc);
709 catch (failed_constructor& err) {
710 error << _("could not load location from session file - ignored") << endmsg;
714 if (locations.size()) {
715 current_location = locations.front();
716 } else {
717 current_location = 0;
721 changed (OTHER); /* EMIT SIGNAL */
723 return 0;
726 struct LocationStartEarlierComparison
728 bool operator() (Location *a, Location *b) {
729 return a->start() < b->start();
733 struct LocationStartLaterComparison
735 bool operator() (Location *a, Location *b) {
736 return a->start() > b->start();
740 Location *
741 Locations::first_location_before (nframes64_t frame, bool include_special_ranges)
743 LocationList locs;
746 Glib::Mutex::Lock lm (lock);
747 locs = locations;
750 LocationStartLaterComparison cmp;
751 locs.sort (cmp);
753 /* locs is now sorted latest..earliest */
755 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
756 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
757 continue;
759 if (!(*i)->is_hidden() && (*i)->start() < frame) {
760 return (*i);
764 return 0;
767 Location *
768 Locations::first_location_after (nframes64_t frame, bool include_special_ranges)
770 LocationList locs;
773 Glib::Mutex::Lock lm (lock);
774 locs = locations;
777 LocationStartEarlierComparison cmp;
778 locs.sort (cmp);
780 /* locs is now sorted earliest..latest */
782 for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
783 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
784 continue;
786 if (!(*i)->is_hidden() && (*i)->start() > frame) {
787 return (*i);
791 return 0;
794 /** Look for the `marks' (either locations which are marks, or start/end points of range markers) either
795 * side of a frame.
796 * @param frame Frame to look for.
797 * @param before Filled in with the position of the last `mark' before `frame' (or max_frames if none exists)
798 * @param after Filled in with the position of the last `mark' after `frame' (or max_frames if none exists)
800 void
801 Locations::marks_either_side (nframes64_t const frame, nframes64_t& before, nframes64_t& after) const
803 before = after = max_frames;
805 LocationList locs;
808 Glib::Mutex::Lock lm (lock);
809 locs = locations;
812 std::list<nframes64_t> positions;
814 for (LocationList::const_iterator i = locs.begin(); i != locs.end(); ++i) {
815 if (((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
816 continue;
819 if (!(*i)->is_hidden()) {
820 if ((*i)->is_mark ()) {
821 positions.push_back ((*i)->start ());
822 } else {
823 positions.push_back ((*i)->start ());
824 positions.push_back ((*i)->end ());
829 if (positions.empty ()) {
830 return;
833 positions.sort ();
835 std::list<nframes64_t>::iterator i = positions.begin ();
836 while (i != positions.end () && *i < frame) {
837 ++i;
840 if (i == positions.end ()) {
841 /* run out of marks */
842 before = positions.back ();
843 return;
846 after = *i;
848 if (i == positions.begin ()) {
849 /* none before */
850 return;
853 --i;
854 before = *i;
857 Location*
858 Locations::session_range_location () const
860 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
861 if ((*i)->is_session_range()) {
862 return const_cast<Location*> (*i);
865 return 0;
868 Location*
869 Locations::auto_loop_location () const
871 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
872 if ((*i)->is_auto_loop()) {
873 return const_cast<Location*> (*i);
876 return 0;
879 Location*
880 Locations::auto_punch_location () const
882 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
883 if ((*i)->is_auto_punch()) {
884 return const_cast<Location*> (*i);
887 return 0;
890 uint32_t
891 Locations::num_range_markers () const
893 uint32_t cnt = 0;
894 Glib::Mutex::Lock lm (lock);
895 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
896 if ((*i)->is_range_marker()) {
897 ++cnt;
900 return cnt;
903 Location *
904 Locations::get_location_by_id(PBD::ID id)
906 LocationList::iterator it;
907 for (it = locations.begin(); it != locations.end(); ++it)
908 if (id == (*it)->id())
909 return *it;
911 return 0;
914 void
915 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
917 Glib::Mutex::Lock lm (lock);
919 for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
920 if ((flags == 0 || (*i)->matches (flags)) &&
921 ((*i)->start() >= start && (*i)->end() < end)) {
922 ll.push_back (*i);