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