ca0a56972ecc8484ea77461016eb49fc5fe03bdf
[ardour.git] / libs / ardour / location.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <algorithm>
21 #include <set>
22 #include <cstdio> /* for sprintf */
23 #include <unistd.h>
24 #include <cerrno>
25 #include <ctime>
26 #include <sigc++/bind.h>
27
28 #include "pbd/stl_delete.h"
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
31
32 #include "ardour/location.h"
33 #include "ardour/session.h"
34 #include "ardour/audiofilesource.h"
35
36 #include "i18n.h"
37
38 #define SUFFIX_MAX 32
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace sigc;
43 using namespace PBD;
44
45 Location::Location (const Location& other)
46         : _name (other._name),
47           _start (other._start),
48           _end (other._end),
49           _flags (other._flags)
50 {
51         /* start and end flags can never be copied, because there can only ever be one of each */
52
53         _flags = Flags (_flags & ~IsStart);
54         _flags = Flags (_flags & ~IsEnd);
55
56         /* copy is not locked even if original was */
57
58         _locked = false;
59 }
60
61 Location::Location (const XMLNode& node)
62 {
63         if (set_state (node)) {
64                 throw failed_constructor ();
65         }
66 }
67
68 Location*
69 Location::operator= (const Location& other)
70 {
71         if (this == &other) {
72                 return this;
73         }
74
75         _name = other._name;
76         _start = other._start;
77         _end = other._end;
78         _flags = other._flags;
79
80         /* copy is not locked even if original was */
81
82         _locked = false;
83
84         /* "changed" not emitted on purpose */
85         
86         return this;
87 }
88
89 int
90 Location::set_start (nframes_t s)
91 {
92         if (_locked) {
93                 return -1;
94         }
95
96         if (is_mark()) {
97                 if (_start != s) {
98
99                         _start = s;
100                         _end = s;
101
102                         start_changed(this); /* EMIT SIGNAL */
103                         end_changed(this); /* EMIT SIGNAL */
104
105                         if ( is_start() ) {
106
107                                 Session::StartTimeChanged (); /* EMIT SIGNAL */
108                                 AudioFileSource::set_header_position_offset ( s );
109                         }
110
111                         if ( is_end() ) {
112                                 Session::EndTimeChanged (); /* EMIT SIGNAL */
113                         }
114                 }
115                 return 0;
116         }
117
118         if (((is_auto_punch() || is_auto_loop()) && s >= _end) || s > _end) {
119                 return -1;
120         }
121
122         if (s != _start) {
123                 _start = s; 
124                 start_changed(this); /* EMIT SIGNAL */
125         }
126
127         return 0;
128 }
129
130 int
131 Location::set_end (nframes_t e)
132 {
133         if (_locked) {
134                 return -1;
135         }
136
137         if (is_mark()) {
138                 if (_start != e) {
139                         _start = e;
140                         _end = e;
141                         start_changed(this); /* EMIT SIGNAL */
142                         end_changed(this); /* EMIT SIGNAL */
143
144                         if ( is_start() ) {
145                                 Session::StartTimeChanged (); /* EMIT SIGNAL */
146                         }
147
148                         if ( is_end() ) {
149                                 Session::EndTimeChanged (); /* EMIT SIGNAL */
150                         }
151
152                 }
153                 return 0;
154         }
155
156         if (((is_auto_punch() || is_auto_loop()) && e <= _start) || e < _start) {
157                 return -1;
158         }
159
160         if (e != _end) {
161                 _end = e; 
162                  end_changed(this); /* EMIT SIGNAL */
163         }
164         return 0;
165 }
166
167 int
168 Location::set (nframes_t start, nframes_t end)
169 {
170         if (_locked) {
171                 return -1;
172         }
173
174         if (is_mark() && start != end) {
175                 return -1;
176         } else if (((is_auto_punch() || is_auto_loop()) && start >= end) || (start > end)) {
177                 return -1;
178         }
179         
180         if (_start != start) {
181                 _start = start;
182                 start_changed(this); /* EMIT SIGNAL */
183         }
184
185         if (_end != end) {
186                 _end = end;
187                 end_changed(this); /* EMIT SIGNAL */
188         }
189         return 0;
190 }
191
192 int
193 Location::move_to (nframes_t pos) 
194 {
195         if (_locked) {
196                 return -1;
197         }
198
199         if (_start != pos) {
200                 _start = pos;
201                 _end = _start + length();
202                 
203                 changed (this); /* EMIT SIGNAL */
204         }
205         
206         return 0;
207 }
208
209 void
210 Location::set_hidden (bool yn, void *src)
211 {
212         if (set_flag_internal (yn, IsHidden)) {
213                  FlagsChanged (this, src); /* EMIT SIGNAL */
214         }
215 }
216
217 void
218 Location::set_cd (bool yn, void *src)
219 {
220         // XXX this really needs to be session start
221         // but its not available here - leave to GUI
222
223         if (_start == 0) {
224                 error << _("You cannot put a CD marker at this position") << endmsg;
225                 return;
226         }
227
228         if (set_flag_internal (yn, IsCDMarker)) {
229                  FlagsChanged (this, src); /* EMIT SIGNAL */
230         }
231 }
232
233 void
234 Location::set_is_end (bool yn, void *src)
235 {
236         if (set_flag_internal (yn, IsEnd)) {
237                  FlagsChanged (this, src); /* EMIT SIGNAL */
238         }
239 }
240
241 void
242 Location::set_is_start (bool yn, void *src)
243 {
244         if (set_flag_internal (yn, IsStart)) {
245                  FlagsChanged (this, src); /* EMIT SIGNAL */
246         }
247 }
248
249 void
250 Location::set_is_range_marker (bool yn, void *src)
251 {
252        if (set_flag_internal (yn, IsRangeMarker)) {
253                 FlagsChanged (this, src); /* EMIT SIGNAL */
254        }
255 }
256
257 void
258 Location::set_auto_punch (bool yn, void *src) 
259 {
260         if (is_mark() || _start == _end) {
261                 return;
262         }
263
264         if (set_flag_internal (yn, IsAutoPunch)) {
265                  FlagsChanged (this, src); /* EMIT SIGNAL */
266         }
267 }
268
269 void
270 Location::set_auto_loop (bool yn, void *src) 
271 {
272         if (is_mark() || _start == _end) {
273                 return;
274         }
275
276         if (set_flag_internal (yn, IsAutoLoop)) {
277                  FlagsChanged (this, src); /* EMIT SIGNAL */
278         }
279 }
280
281 bool
282 Location::set_flag_internal (bool yn, Flags flag)
283 {
284         if (yn) {
285                 if (!(_flags & flag)) {
286                         _flags = Flags (_flags | flag);
287                         return true;
288                 }
289         } else {
290                 if (_flags & flag) {
291                         _flags = Flags (_flags & ~flag);
292                         return true;
293                 }
294         }
295         return false;
296 }
297
298 void
299 Location::set_mark (bool yn)
300 {
301         /* This function is private, and so does not emit signals */
302
303         if (_start != _end) {
304                 return;
305         }
306         
307         set_flag_internal (yn, IsMark);
308 }
309
310
311 XMLNode&
312 Location::cd_info_node(const string & name, const string & value)
313 {
314         XMLNode* root = new XMLNode("CD-Info");
315
316         root->add_property("name", name);
317         root->add_property("value", value);
318         
319         return *root;
320 }
321
322  
323 XMLNode&
324 Location::get_state (void)
325 {
326         XMLNode *node = new XMLNode ("Location");
327         char buf[64];
328
329         typedef map<string, string>::const_iterator CI;
330
331         for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
332                 node->add_child_nocopy(cd_info_node(m->first, m->second));
333         }
334
335         id().print (buf, sizeof (buf));
336         node->add_property("id", buf);
337         node->add_property ("name", name());
338         snprintf (buf, sizeof (buf), "%u", start());
339         node->add_property ("start", buf);
340         snprintf (buf, sizeof (buf), "%u", end());
341         node->add_property ("end", buf);
342         node->add_property ("flags", enum_2_string (_flags));
343         node->add_property ("locked", (_locked ? "yes" : "no"));
344
345         return *node;
346 }
347
348 int
349 Location::set_state (const XMLNode& node)
350 {
351         const XMLProperty *prop;
352
353         XMLNodeList cd_list = node.children();
354         XMLNodeConstIterator cd_iter;
355         XMLNode *cd_node;
356         
357         string cd_name;
358         string cd_value;
359
360         if (node.name() != "Location") {
361                 error << _("incorrect XML node passed to Location::set_state") << endmsg;
362                 return -1;
363         }
364
365         if ((prop = node.property ("id")) == 0) {
366                 warning << _("XML node for Location has no ID information") << endmsg;
367         } else {
368                 _id = prop->value ();
369         }
370
371         if ((prop = node.property ("name")) == 0) {
372                 error << _("XML node for Location has no name information") << endmsg;
373                 return -1;
374         }
375                 
376         set_name (prop->value());
377                 
378         if ((prop = node.property ("start")) == 0) {
379                 error << _("XML node for Location has no start information") << endmsg; 
380                 return -1;
381         }
382                 
383                 /* can't use set_start() here, because _end
384                    may make the value of _start illegal.
385                 */
386                 
387         _start = atoi (prop->value().c_str());
388                 
389         if ((prop = node.property ("end")) == 0) {
390                   error << _("XML node for Location has no end information") << endmsg; 
391                   return -1;
392         }
393                 
394         _end = atoi (prop->value().c_str());
395                 
396         if ((prop = node.property ("flags")) == 0) {
397                   error << _("XML node for Location has no flags information") << endmsg; 
398                   return -1;
399         }
400                 
401         _flags = Flags (string_2_enum (prop->value(), _flags));
402
403         if ((prop = node.property ("locked")) != 0) {
404                 _locked = (prop->value() == "yes");
405         } else {
406                 _locked = false;
407         }
408
409         for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
410                   
411                   cd_node = *cd_iter;
412                   
413                   if (cd_node->name() != "CD-Info") {
414                     continue;
415                   }
416                   
417                   if ((prop = cd_node->property ("name")) != 0) {
418                     cd_name = prop->value();
419                   } else {
420                     throw failed_constructor ();
421                   }
422                   
423                   if ((prop = cd_node->property ("value")) != 0) {
424                     cd_value = prop->value();
425                   } else {
426                     throw failed_constructor ();
427                   }
428                   
429                   
430                   cd_info[cd_name] = cd_value;
431                   
432         }
433
434         changed(this); /* EMIT SIGNAL */
435                 
436         return 0;
437 }
438
439 /*---------------------------------------------------------------------- */
440
441 Locations::Locations ()
442
443 {
444         current_location = 0;
445 }
446
447 Locations::~Locations () 
448 {
449         for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
450                 LocationList::iterator tmp = i;
451                 ++tmp;
452                 delete *i;
453                 i = tmp;
454         }
455 }
456
457 int
458 Locations::set_current (Location *loc, bool want_lock)
459
460 {
461         int ret;
462
463         if (want_lock) {
464                 Glib::Mutex::Lock lm (lock);
465                 ret = set_current_unlocked (loc);
466         } else {
467                 ret = set_current_unlocked (loc);
468         }
469
470         if (ret == 0) {
471                  current_changed (current_location); /* EMIT SIGNAL */
472         }
473         return ret;
474 }
475
476 int
477 Locations::next_available_name(string& result,string base)
478 {
479         LocationList::iterator i;
480         Location* location;
481         string temp;
482         string::size_type l;
483         int suffix;
484         char buf[32];
485         bool available[SUFFIX_MAX+1];
486
487         result = base;
488         for (int k=1; k<SUFFIX_MAX; k++) {
489                 available[k] = true;
490         }
491         l = base.length();
492         for (i = locations.begin(); i != locations.end(); ++i) {
493                 location =* i;
494                 temp = location->name();
495                 if (l && !temp.find(base,0)) {
496                         suffix = atoi(temp.substr(l,3).c_str());
497                         if (suffix) available[suffix] = false;
498                 }
499         }
500         for (int k=1; k<=SUFFIX_MAX; k++) {
501                 if (available[k]) { 
502                         snprintf (buf, 31, "%d", k);
503                         result += buf;
504                         return 1;
505                 }
506         }
507         return 0;
508 }
509
510 int
511 Locations::set_current_unlocked (Location *loc)
512 {
513         if (find (locations.begin(), locations.end(), loc) == locations.end()) {
514                 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
515                 return -1;
516         }
517         
518         current_location = loc;
519         return 0;
520 }
521
522 void
523 Locations::clear ()
524 {
525         {
526                 Glib::Mutex::Lock lm (lock);
527
528                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
529
530                         LocationList::iterator tmp = i;
531                         ++tmp;
532
533                         if (!(*i)->is_end() && !(*i)->is_start()) {
534                                 locations.erase (i);
535                         }
536
537                         i = tmp;
538                 }
539
540                 current_location = 0;
541         }
542
543         changed (); /* EMIT SIGNAL */
544         current_changed (0); /* EMIT SIGNAL */
545 }       
546
547 void
548 Locations::clear_markers ()
549 {
550         {
551                 Glib::Mutex::Lock lm (lock);
552                 LocationList::iterator tmp;
553
554                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
555                         tmp = i;
556                         ++tmp;
557
558                         if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
559                                 locations.erase (i);
560                         }
561
562                         i = tmp;
563                 }
564         }
565
566         changed (); /* EMIT SIGNAL */
567 }       
568
569 void
570 Locations::clear_ranges ()
571 {
572         {
573                 Glib::Mutex::Lock lm (lock);
574                 LocationList::iterator tmp;
575                 
576                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
577
578                         tmp = i;
579                         ++tmp;
580
581                         if (!(*i)->is_mark()) {
582                                 locations.erase (i);
583
584                         }
585
586                         i = tmp;
587                 }
588
589                 current_location = 0;
590         }
591
592         changed (); /* EMIT SIGNAL */
593         current_changed (0); /* EMIT SIGNAL */
594 }       
595
596 void
597 Locations::add (Location *loc, bool make_current)
598 {
599         {
600                 Glib::Mutex::Lock lm (lock);
601                 locations.push_back (loc);
602
603                 if (make_current) {
604                         current_location = loc;
605                 }
606         }
607         
608         added (loc); /* EMIT SIGNAL */
609
610         if (make_current) {
611                  current_changed (current_location); /* EMIT SIGNAL */
612         } 
613 }
614
615 void
616 Locations::remove (Location *loc)
617
618 {
619         bool was_removed = false;
620         bool was_current = false;
621         LocationList::iterator i;
622
623         if (loc->is_end() || loc->is_start()) {
624                 return;
625         }
626
627         {
628                 Glib::Mutex::Lock lm (lock);
629
630                 for (i = locations.begin(); i != locations.end(); ++i) {
631                         if ((*i) == loc) {
632                                 locations.erase (i);
633                                 was_removed = true;
634                                 if (current_location == loc) {
635                                         current_location = 0;
636                                         was_current = true;
637                                 }
638                                 break;
639                         }
640                 }
641         }
642         
643         if (was_removed) {
644                 
645                 removed (loc); /* EMIT SIGNAL */
646
647                 if (was_current) {
648                          current_changed (0); /* EMIT SIGNAL */
649                 }
650
651                 changed (); /* EMIT_SIGNAL */
652         }
653 }
654
655 void
656 Locations::location_changed (Location* loc)
657 {
658         changed (); /* EMIT SIGNAL */
659 }
660
661 XMLNode&
662 Locations::get_state ()
663 {
664         XMLNode *node = new XMLNode ("Locations");
665         LocationList::iterator iter;
666         Glib::Mutex::Lock lm (lock);
667        
668         for (iter  = locations.begin(); iter != locations.end(); ++iter) {
669                 node->add_child_nocopy ((*iter)->get_state ());
670         }
671
672         return *node;
673 }       
674
675 int
676 Locations::set_state (const XMLNode& node)
677 {
678         XMLNodeList nlist;
679         XMLNodeConstIterator niter;
680
681         if (node.name() != "Locations") {
682                 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
683                 return -1;
684         }
685         
686         nlist = node.children();
687
688         locations.clear ();
689         current_location = 0;
690
691         {
692                 Glib::Mutex::Lock lm (lock);
693
694                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
695                         
696                         try {
697
698                                 Location *loc = new Location (**niter);
699                                 locations.push_back (loc);
700                         }
701
702                         catch (failed_constructor& err) {
703                                 error << _("could not load location from session file - ignored") << endmsg;
704                         }
705                 }
706                 
707                 if (locations.size()) {
708
709                         current_location = locations.front();
710                 } else {
711                         current_location = 0;
712                 }
713         }
714
715         changed (); /* EMIT SIGNAL */
716          
717         return 0;
718 }       
719
720 struct LocationStartEarlierComparison 
721 {
722     bool operator() (Location *a, Location *b) {
723         return a->start() < b->start();
724     }
725 };
726
727 struct LocationStartLaterComparison 
728 {
729     bool operator() (Location *a, Location *b) {
730         return a->start() > b->start();
731     }
732 };
733
734 Location *
735 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
736 {
737         LocationList locs;
738
739         {
740                 Glib::Mutex::Lock lm (lock);
741                 locs = locations;
742         }
743
744         LocationStartLaterComparison cmp;
745         locs.sort (cmp);
746
747         /* locs is now sorted latest..earliest */
748         
749         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
750                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
751                         continue;
752                 }
753                 if (!(*i)->is_hidden() && (*i)->start() < frame) {
754                         return (*i);
755                 }
756         }
757
758         return 0;
759 }
760
761 Location *
762 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
763 {
764         LocationList locs;
765
766         {
767                 Glib::Mutex::Lock lm (lock);
768                 locs = locations;
769         }
770
771         LocationStartEarlierComparison cmp;
772         locs.sort (cmp);
773
774         /* locs is now sorted earliest..latest */
775         
776         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
777                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
778                         continue;
779                 }
780                 if (!(*i)->is_hidden() && (*i)->start() > frame) {
781                         return (*i);
782                 }
783         }
784
785         return 0;
786 }
787
788 nframes_t
789 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
790 {
791         LocationList locs;
792
793         {
794         Glib::Mutex::Lock lm (lock);
795                 locs = locations;
796         }
797
798         LocationStartLaterComparison cmp;
799         locs.sort (cmp);
800
801         /* locs is now sorted latest..earliest */
802         
803         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
804                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
805                         continue;
806                 }
807                 if (!(*i)->is_hidden()) {
808                         if ((*i)->is_mark()) {
809                                 /* MARK: start == end */
810                                 if ((*i)->start() < frame) {
811                                         return (*i)->start();
812                                 }
813                         } else {
814                                 /* RANGE: start != end, compare start and end */
815                                 if ((*i)->end() < frame) {
816                                         return (*i)->end();
817                                 }
818                                 if ((*i)->start () < frame) {
819                                         return (*i)->start();
820                                 }
821                         }
822                 }
823         }
824
825         return 0;
826 }
827
828 nframes_t
829 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
830 {
831         LocationList locs;
832
833         {
834         Glib::Mutex::Lock lm (lock);
835                 locs = locations;
836         }
837
838         LocationStartEarlierComparison cmp;
839         locs.sort (cmp);
840
841         /* locs is now sorted earliest..latest */
842         
843         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
844                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
845                         continue;
846                 }
847                 if (!(*i)->is_hidden()) {
848                         if ((*i)->is_mark()) {
849                                 /* MARK, start == end so just compare start */
850                                 if ((*i)->start() > frame) {
851                                         return (*i)->start();
852                                 }
853                         } else {
854                                 /* RANGE, start != end, compare start and end */
855                                 if ((*i)->start() > frame ) {
856                                         return (*i)->start ();
857                                 }
858                                 if ((*i)->end() > frame) {
859                                         return (*i)->end ();
860                                 }
861                         }
862                 }
863         }
864
865         return max_frames;
866 }
867
868 Location*
869 Locations::end_location () const
870 {
871         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
872                 if ((*i)->is_end()) {
873                         return const_cast<Location*> (*i);
874                 }
875         }
876         return 0;
877 }       
878
879 Location*
880 Locations::start_location () const
881 {
882         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
883                 if ((*i)->is_start()) {
884                         return const_cast<Location*> (*i);
885                 }
886         }
887         return 0;
888 }       
889
890 Location*
891 Locations::auto_loop_location () const
892 {
893         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
894                 if ((*i)->is_auto_loop()) {
895                         return const_cast<Location*> (*i);
896                 }
897         }
898         return 0;
899 }       
900
901 Location*
902 Locations::auto_punch_location () const
903 {
904         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
905                 if ((*i)->is_auto_punch()) {
906                         return const_cast<Location*> (*i);
907                 }
908         }
909        return 0;
910 }       
911
912 uint32_t
913 Locations::num_range_markers () const
914 {
915         uint32_t cnt = 0;
916         Glib::Mutex::Lock lm (lock);
917         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
918                 if ((*i)->is_range_marker()) {
919                         ++cnt;
920                 }
921         }
922         return cnt;
923 }
924
925 Location *
926 Locations::get_location_by_id(PBD::ID id)
927 {
928     LocationList::iterator it;
929     for (it  = locations.begin(); it != locations.end(); it++)
930         if (id == (*it)->id())
931             return *it;
932
933     return 0;
934 }
935
936 void
937 Locations::find_all_between (nframes64_t start, nframes64_t end, LocationList& ll, Location::Flags flags)
938 {
939         Glib::Mutex::Lock lm (lock);
940
941         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
942                 if ((flags == 0 || (*i)->matches (flags)) && 
943                     ((*i)->start() >= start && (*i)->end() < end)) {
944                         ll.push_back (*i);
945                 }
946         }
947 }