merge from 2.0-ongoing @ 3581
[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_auto_punch (bool yn, void *src) 
251 {
252         if (is_mark() || _start == _end) {
253                 return;
254         }
255
256         if (set_flag_internal (yn, IsAutoPunch)) {
257                  FlagsChanged (this, src); /* EMIT SIGNAL */
258         }
259 }
260
261 void
262 Location::set_auto_loop (bool yn, void *src) 
263 {
264         if (is_mark() || _start == _end) {
265                 return;
266         }
267
268         if (set_flag_internal (yn, IsAutoLoop)) {
269                  FlagsChanged (this, src); /* EMIT SIGNAL */
270         }
271 }
272
273 bool
274 Location::set_flag_internal (bool yn, Flags flag)
275 {
276         if (yn) {
277                 if (!(_flags & flag)) {
278                         _flags = Flags (_flags | flag);
279                         return true;
280                 }
281         } else {
282                 if (_flags & flag) {
283                         _flags = Flags (_flags & ~flag);
284                         return true;
285                 }
286         }
287         return false;
288 }
289
290 void
291 Location::set_mark (bool yn)
292 {
293         /* This function is private, and so does not emit signals */
294
295         if (_start != _end) {
296                 return;
297         }
298         
299         set_flag_internal (yn, IsMark);
300 }
301
302
303 XMLNode&
304 Location::cd_info_node(const string & name, const string & value)
305 {
306         XMLNode* root = new XMLNode("CD-Info");
307
308         root->add_property("name", name);
309         root->add_property("value", value);
310         
311         return *root;
312 }
313
314  
315 XMLNode&
316 Location::get_state (void)
317 {
318         XMLNode *node = new XMLNode ("Location");
319         char buf[64];
320
321         typedef map<string, string>::const_iterator CI;
322
323         for(CI m = cd_info.begin(); m != cd_info.end(); ++m){
324                 node->add_child_nocopy(cd_info_node(m->first, m->second));
325         }
326
327         id().print (buf, sizeof (buf));
328         node->add_property("id", buf);
329         node->add_property ("name", name());
330         snprintf (buf, sizeof (buf), "%u", start());
331         node->add_property ("start", buf);
332         snprintf (buf, sizeof (buf), "%u", end());
333         node->add_property ("end", buf);
334         node->add_property ("flags", enum_2_string (_flags));
335         node->add_property ("locked", (_locked ? "yes" : "no"));
336
337         return *node;
338 }
339
340 int
341 Location::set_state (const XMLNode& node)
342 {
343         const XMLProperty *prop;
344
345         XMLNodeList cd_list = node.children();
346         XMLNodeConstIterator cd_iter;
347         XMLNode *cd_node;
348         
349         string cd_name;
350         string cd_value;
351
352         if (node.name() != "Location") {
353                 error << _("incorrect XML node passed to Location::set_state") << endmsg;
354                 return -1;
355         }
356
357         if ((prop = node.property ("id")) == 0) {
358                 warning << _("XML node for Location has no ID information") << endmsg;
359         } else {
360                 _id = prop->value ();
361         }
362
363         if ((prop = node.property ("name")) == 0) {
364                 error << _("XML node for Location has no name information") << endmsg;
365                 return -1;
366         }
367                 
368         set_name (prop->value());
369                 
370         if ((prop = node.property ("start")) == 0) {
371                 error << _("XML node for Location has no start information") << endmsg; 
372                 return -1;
373         }
374                 
375                 /* can't use set_start() here, because _end
376                    may make the value of _start illegal.
377                 */
378                 
379         _start = atoi (prop->value().c_str());
380                 
381         if ((prop = node.property ("end")) == 0) {
382                   error << _("XML node for Location has no end information") << endmsg; 
383                   return -1;
384         }
385                 
386         _end = atoi (prop->value().c_str());
387                 
388         if ((prop = node.property ("flags")) == 0) {
389                   error << _("XML node for Location has no flags information") << endmsg; 
390                   return -1;
391         }
392                 
393         _flags = Flags (string_2_enum (prop->value(), _flags));
394
395         if ((prop = node.property ("locked")) != 0) {
396                 _locked = (prop->value() == "yes");
397         } else {
398                 _locked = false;
399         }
400
401         for (cd_iter = cd_list.begin(); cd_iter != cd_list.end(); ++cd_iter) {
402                   
403                   cd_node = *cd_iter;
404                   
405                   if (cd_node->name() != "CD-Info") {
406                     continue;
407                   }
408                   
409                   if ((prop = cd_node->property ("name")) != 0) {
410                     cd_name = prop->value();
411                   } else {
412                     throw failed_constructor ();
413                   }
414                   
415                   if ((prop = cd_node->property ("value")) != 0) {
416                     cd_value = prop->value();
417                   } else {
418                     throw failed_constructor ();
419                   }
420                   
421                   
422                   cd_info[cd_name] = cd_value;
423                   
424         }
425
426         changed(this); /* EMIT SIGNAL */
427                 
428         return 0;
429 }
430
431 /*---------------------------------------------------------------------- */
432
433 Locations::Locations ()
434
435 {
436         current_location = 0;
437 }
438
439 Locations::~Locations () 
440 {
441         for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
442                 LocationList::iterator tmp = i;
443                 ++tmp;
444                 delete *i;
445                 i = tmp;
446         }
447 }
448
449 int
450 Locations::set_current (Location *loc, bool want_lock)
451
452 {
453         int ret;
454
455         if (want_lock) {
456                 Glib::Mutex::Lock lm (lock);
457                 ret = set_current_unlocked (loc);
458         } else {
459                 ret = set_current_unlocked (loc);
460         }
461
462         if (ret == 0) {
463                  current_changed (current_location); /* EMIT SIGNAL */
464         }
465         return ret;
466 }
467
468 int
469 Locations::next_available_name(string& result,string base)
470 {
471         LocationList::iterator i;
472         Location* location;
473         string temp;
474         string::size_type l;
475         int suffix;
476         char buf[32];
477         bool available[SUFFIX_MAX+1];
478
479         result = base;
480         for (int k=1; k<SUFFIX_MAX; k++) {
481                 available[k] = true;
482         }
483         l = base.length();
484         for (i = locations.begin(); i != locations.end(); ++i) {
485                 location =* i;
486                 temp = location->name();
487                 if (l && !temp.find(base,0)) {
488                         suffix = atoi(temp.substr(l,3).c_str());
489                         if (suffix) available[suffix] = false;
490                 }
491         }
492         for (int k=1; k<=SUFFIX_MAX; k++) {
493                 if (available[k]) { 
494                         snprintf (buf, 31, "%d", k);
495                         result += buf;
496                         return 1;
497                 }
498         }
499         return 0;
500 }
501
502 int
503 Locations::set_current_unlocked (Location *loc)
504 {
505         if (find (locations.begin(), locations.end(), loc) == locations.end()) {
506                 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
507                 return -1;
508         }
509         
510         current_location = loc;
511         return 0;
512 }
513
514 void
515 Locations::clear ()
516 {
517         {
518                 Glib::Mutex::Lock lm (lock);
519
520                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
521
522                         LocationList::iterator tmp = i;
523                         ++tmp;
524
525                         if (!(*i)->is_end() && !(*i)->is_start()) {
526                                 locations.erase (i);
527                         }
528
529                         i = tmp;
530                 }
531
532                 current_location = 0;
533         }
534
535         changed (); /* EMIT SIGNAL */
536         current_changed (0); /* EMIT SIGNAL */
537 }       
538
539 void
540 Locations::clear_markers ()
541 {
542         {
543                 Glib::Mutex::Lock lm (lock);
544                 LocationList::iterator tmp;
545
546                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
547                         tmp = i;
548                         ++tmp;
549
550                         if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
551                                 locations.erase (i);
552                         }
553
554                         i = tmp;
555                 }
556         }
557
558         changed (); /* EMIT SIGNAL */
559 }       
560
561 void
562 Locations::clear_ranges ()
563 {
564         {
565                 Glib::Mutex::Lock lm (lock);
566                 LocationList::iterator tmp;
567                 
568                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
569
570                         tmp = i;
571                         ++tmp;
572
573                         if (!(*i)->is_mark()) {
574                                 locations.erase (i);
575
576                         }
577
578                         i = tmp;
579                 }
580
581                 current_location = 0;
582         }
583
584         changed (); /* EMIT SIGNAL */
585         current_changed (0); /* EMIT SIGNAL */
586 }       
587
588 void
589 Locations::add (Location *loc, bool make_current)
590 {
591         {
592                 Glib::Mutex::Lock lm (lock);
593                 locations.push_back (loc);
594
595                 if (make_current) {
596                         current_location = loc;
597                 }
598         }
599         
600         added (loc); /* EMIT SIGNAL */
601
602         if (make_current) {
603                  current_changed (current_location); /* EMIT SIGNAL */
604         } 
605 }
606
607 void
608 Locations::remove (Location *loc)
609
610 {
611         bool was_removed = false;
612         bool was_current = false;
613         LocationList::iterator i;
614
615         if (loc->is_end() || loc->is_start()) {
616                 return;
617         }
618
619         {
620                 Glib::Mutex::Lock lm (lock);
621
622                 for (i = locations.begin(); i != locations.end(); ++i) {
623                         if ((*i) == loc) {
624                                 locations.erase (i);
625                                 was_removed = true;
626                                 if (current_location == loc) {
627                                         current_location = 0;
628                                         was_current = true;
629                                 }
630                                 break;
631                         }
632                 }
633         }
634         
635         if (was_removed) {
636                 
637                 removed (loc); /* EMIT SIGNAL */
638
639                 if (was_current) {
640                          current_changed (0); /* EMIT SIGNAL */
641                 }
642
643                 changed (); /* EMIT_SIGNAL */
644         }
645 }
646
647 void
648 Locations::location_changed (Location* loc)
649 {
650         changed (); /* EMIT SIGNAL */
651 }
652
653 XMLNode&
654 Locations::get_state ()
655 {
656         XMLNode *node = new XMLNode ("Locations");
657         LocationList::iterator iter;
658         Glib::Mutex::Lock lm (lock);
659        
660         for (iter  = locations.begin(); iter != locations.end(); ++iter) {
661                 node->add_child_nocopy ((*iter)->get_state ());
662         }
663
664         return *node;
665 }       
666
667 int
668 Locations::set_state (const XMLNode& node)
669 {
670         XMLNodeList nlist;
671         XMLNodeConstIterator niter;
672
673         if (node.name() != "Locations") {
674                 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
675                 return -1;
676         }
677         
678         nlist = node.children();
679
680         locations.clear ();
681         current_location = 0;
682
683         {
684                 Glib::Mutex::Lock lm (lock);
685
686                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
687                         
688                         try {
689
690                                 Location *loc = new Location (**niter);
691                                 locations.push_back (loc);
692                         }
693
694                         catch (failed_constructor& err) {
695                                 error << _("could not load location from session file - ignored") << endmsg;
696                         }
697                 }
698                 
699                 if (locations.size()) {
700
701                         current_location = locations.front();
702                 } else {
703                         current_location = 0;
704                 }
705         }
706
707         changed (); /* EMIT SIGNAL */
708          
709         return 0;
710 }       
711
712 struct LocationStartEarlierComparison 
713 {
714     bool operator() (Location *a, Location *b) {
715         return a->start() < b->start();
716     }
717 };
718
719 struct LocationStartLaterComparison 
720 {
721     bool operator() (Location *a, Location *b) {
722         return a->start() > b->start();
723     }
724 };
725
726 Location *
727 Locations::first_location_before (nframes_t frame, bool include_special_ranges)
728 {
729         LocationList locs;
730
731         {
732                 Glib::Mutex::Lock lm (lock);
733                 locs = locations;
734         }
735
736         LocationStartLaterComparison cmp;
737         locs.sort (cmp);
738
739         /* locs is now sorted latest..earliest */
740         
741         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
742                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
743                         continue;
744                 }
745                 if (!(*i)->is_hidden() && (*i)->start() < frame) {
746                         return (*i);
747                 }
748         }
749
750         return 0;
751 }
752
753 Location *
754 Locations::first_location_after (nframes_t frame, bool include_special_ranges)
755 {
756         LocationList locs;
757
758         {
759                 Glib::Mutex::Lock lm (lock);
760                 locs = locations;
761         }
762
763         LocationStartEarlierComparison cmp;
764         locs.sort (cmp);
765
766         /* locs is now sorted earliest..latest */
767         
768         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
769                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
770                         continue;
771                 }
772                 if (!(*i)->is_hidden() && (*i)->start() > frame) {
773                         return (*i);
774                 }
775         }
776
777         return 0;
778 }
779
780 nframes_t
781 Locations::first_mark_before (nframes_t frame, bool include_special_ranges)
782 {
783         LocationList locs;
784
785         {
786         Glib::Mutex::Lock lm (lock);
787                 locs = locations;
788         }
789
790         LocationStartLaterComparison cmp;
791         locs.sort (cmp);
792
793         /* locs is now sorted latest..earliest */
794         
795         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
796                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
797                         continue;
798                 }
799                 if (!(*i)->is_hidden()) {
800                         if ((*i)->is_mark()) {
801                                 /* MARK: start == end */
802                                 if ((*i)->start() < frame) {
803                                         return (*i)->start();
804                                 }
805                         } else {
806                                 /* RANGE: start != end, compare start and end */
807                                 if ((*i)->end() < frame) {
808                                         return (*i)->end();
809                                 }
810                                 if ((*i)->start () < frame) {
811                                         return (*i)->start();
812                                 }
813                         }
814                 }
815         }
816
817         return 0;
818 }
819
820 nframes_t
821 Locations::first_mark_after (nframes_t frame, bool include_special_ranges)
822 {
823         LocationList locs;
824
825         {
826         Glib::Mutex::Lock lm (lock);
827                 locs = locations;
828         }
829
830         LocationStartEarlierComparison cmp;
831         locs.sort (cmp);
832
833         /* locs is now sorted earliest..latest */
834         
835         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
836                 if (!include_special_ranges && ((*i)->is_auto_loop() || (*i)->is_auto_punch())) {
837                         continue;
838                 }
839                 if (!(*i)->is_hidden()) {
840                         if ((*i)->is_mark()) {
841                                 /* MARK, start == end so just compare start */
842                                 if ((*i)->start() > frame) {
843                                         return (*i)->start();
844                                 }
845                         } else {
846                                 /* RANGE, start != end, compare start and end */
847                                 if ((*i)->start() > frame ) {
848                                         return (*i)->start ();
849                                 }
850                                 if ((*i)->end() > frame) {
851                                         return (*i)->end ();
852                                 }
853                         }
854                 }
855         }
856
857         return max_frames;
858 }
859
860 Location*
861 Locations::end_location () const
862 {
863         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
864                 if ((*i)->is_end()) {
865                         return const_cast<Location*> (*i);
866                 }
867         }
868         return 0;
869 }       
870
871 Location*
872 Locations::start_location () const
873 {
874         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
875                 if ((*i)->is_start()) {
876                         return const_cast<Location*> (*i);
877                 }
878         }
879         return 0;
880 }       
881
882 Location*
883 Locations::auto_loop_location () const
884 {
885         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
886                 if ((*i)->is_auto_loop()) {
887                         return const_cast<Location*> (*i);
888                 }
889         }
890         return 0;
891 }       
892
893 Location*
894 Locations::auto_punch_location () const
895 {
896         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
897                 if ((*i)->is_auto_punch()) {
898                         return const_cast<Location*> (*i);
899                 }
900         }
901        return 0;
902 }       
903
904 uint32_t
905 Locations::num_range_markers () const
906 {
907         uint32_t cnt = 0;
908         Glib::Mutex::Lock lm (lock);
909         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
910                 if ((*i)->is_range_marker()) {
911                         ++cnt;
912                 }
913         }
914         return cnt;
915 }
916
917 Location *
918 Locations::get_location_by_id(PBD::ID id)
919 {
920     LocationList::iterator it;
921     for (it  = locations.begin(); it != locations.end(); it++)
922         if (id == (*it)->id())
923             return *it;
924
925     return 0;
926 }