Merged with trunk R992.
[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         save_state (_("initial"));
376 }
377
378 Locations::~Locations () 
379 {
380         std::set<Location*> all_locations;
381         
382         for (StateMap::iterator siter = states.begin(); siter != states.end(); ++siter) {
383
384                 State* lstate = dynamic_cast<State*> (*siter);
385
386                 for (LocationList::iterator liter = lstate->locations.begin(); liter != lstate->locations.end(); ++liter) {
387                         all_locations.insert (*liter);
388                 }
389
390                 for (LocationList::iterator siter = lstate->states.begin(); siter != lstate->states.end(); ++siter) {
391                         all_locations.insert (*siter);
392                 }
393         }
394
395         set_delete (&all_locations);
396 }
397
398 int
399 Locations::set_current (Location *loc, bool want_lock)
400
401 {
402         int ret;
403
404         if (want_lock) {
405                 Glib::Mutex::Lock lm (lock);
406                 ret = set_current_unlocked (loc);
407         } else {
408                 ret = set_current_unlocked (loc);
409         }
410
411         if (ret == 0) {
412                  current_changed (current_location); /* EMIT SIGNAL */
413         }
414         return ret;
415 }
416
417 int
418 Locations::set_current_unlocked (Location *loc)
419 {
420         if (find (locations.begin(), locations.end(), loc) == locations.end()) {
421                 error << _("Locations: attempt to use unknown location as selected location") << endmsg;
422                 return -1;
423         }
424         
425         current_location = loc;
426         return 0;
427 }
428
429 void
430 Locations::clear ()
431 {
432         {
433                 Glib::Mutex::Lock lm (lock);
434                 LocationList::iterator tmp;
435                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
436                         tmp = i;
437                         ++tmp;
438                         if (!(*i)->is_end() && !(*i)->is_start()) {
439                                 locations.erase (i);
440                         }
441                         i = tmp;
442                 }
443
444                 locations.clear ();
445                 current_location = 0;
446         }
447
448         save_state (_("clear"));
449         
450         changed (); /* EMIT SIGNAL */
451         current_changed (0); /* EMIT SIGNAL */
452 }       
453
454 void
455 Locations::clear_markers ()
456 {
457         {
458                 Glib::Mutex::Lock lm (lock);
459                 LocationList::iterator tmp;
460
461                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
462                         tmp = i;
463                         ++tmp;
464
465                         if ((*i)->is_mark() && !(*i)->is_end() && !(*i)->is_start()) {
466                                 locations.erase (i);
467                         }
468
469                         i = tmp;
470                 }
471         }
472
473         save_state (_("clear markers"));
474         
475         changed (); /* EMIT SIGNAL */
476 }       
477
478 void
479 Locations::clear_ranges ()
480 {
481         {
482                 Glib::Mutex::Lock lm (lock);
483                 LocationList::iterator tmp;
484                 
485                 for (LocationList::iterator i = locations.begin(); i != locations.end(); ) {
486
487                         tmp = i;
488                         ++tmp;
489
490                         if (!(*i)->is_mark()) {
491                                 locations.erase (i);
492
493                         }
494
495                         i = tmp;
496                 }
497
498                 current_location = 0;
499         }
500
501         save_state (_("clear ranges"));
502
503         changed (); /* EMIT SIGNAL */
504         current_changed (0); /* EMIT SIGNAL */
505 }       
506
507 void
508 Locations::add (Location *loc, bool make_current)
509 {
510         {
511                 Glib::Mutex::Lock lm (lock);
512                 locations.push_back (loc);
513
514                 if (make_current) {
515                         current_location = loc;
516                 }
517         }
518         
519         save_state (_("add"));
520
521         added (loc); /* EMIT SIGNAL */
522
523         if (make_current) {
524                  current_changed (current_location); /* EMIT SIGNAL */
525         } 
526 }
527
528 void
529 Locations::remove (Location *loc)
530
531 {
532         bool was_removed = false;
533         bool was_current = false;
534         LocationList::iterator i;
535
536         if (loc->is_end() || loc->is_start()) {
537                 return;
538         }
539
540         {
541                 Glib::Mutex::Lock lm (lock);
542
543                 for (i = locations.begin(); i != locations.end(); ++i) {
544                         if ((*i) == loc) {
545                                 locations.erase (i);
546                                 was_removed = true;
547                                 if (current_location == loc) {
548                                         current_location = 0;
549                                         was_current = true;
550                                 }
551                                 break;
552                         }
553                 }
554         }
555         
556         if (was_removed) {
557                 save_state (_("remove"));
558
559                  removed (loc); /* EMIT SIGNAL */
560
561                 if (was_current) {
562                          current_changed (0); /* EMIT SIGNAL */
563                 }
564
565                 changed (); /* EMIT_SIGNAL */
566         }
567 }
568
569 void
570 Locations::location_changed (Location* loc)
571 {
572         save_state (X_("location changed"));
573         changed (); /* EMIT SIGNAL */
574 }
575
576 XMLNode&
577 Locations::get_state ()
578 {
579         XMLNode *node = new XMLNode ("Locations");
580         LocationList::iterator iter;
581         Glib::Mutex::Lock lm (lock);
582        
583         for (iter  = locations.begin(); iter != locations.end(); ++iter) {
584                 node->add_child_nocopy ((*iter)->get_state ());
585         }
586
587         return *node;
588 }       
589
590 int
591 Locations::set_state (const XMLNode& node)
592 {
593         XMLNodeList nlist;
594         XMLNodeConstIterator niter;
595
596         if (node.name() != "Locations") {
597                 error << _("incorrect XML mode passed to Locations::set_state") << endmsg;
598                 return -1;
599         }
600         
601         nlist = node.children();
602         
603         {
604                 Glib::Mutex::Lock lm (lock);
605
606                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
607                         
608                         try {
609
610                                 Location *loc = new Location (**niter);
611                                 locations.push_back (loc);
612                         }
613
614                         catch (failed_constructor& err) {
615                                 error << _("could not load location from session file - ignored") << endmsg;
616                         }
617                 }
618                 
619                 if (locations.size()) {
620
621                         current_location = locations.front();
622                 } else {
623                         current_location = 0;
624                 }
625         }
626
627         changed (); /* EMIT SIGNAL */
628          
629         return 0;
630 }       
631
632 struct LocationStartEarlierComparison 
633 {
634     bool operator() (Location *a, Location *b) {
635         return a->start() < b->start();
636     }
637 };
638
639 struct LocationStartLaterComparison 
640 {
641     bool operator() (Location *a, Location *b) {
642         return a->start() > b->start();
643     }
644 };
645
646 Location *
647 Locations::first_location_before (nframes_t frame)
648 {
649         LocationList locs;
650
651         {
652                 Glib::Mutex::Lock lm (lock);
653                 locs = locations;
654         }
655
656         LocationStartLaterComparison cmp;
657         locs.sort (cmp);
658
659         /* locs is now sorted latest..earliest */
660         
661         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
662                 if (!(*i)->is_hidden() && (*i)->start() < frame) {
663                         return (*i);
664                 }
665         }
666
667         return 0;
668 }
669
670 Location *
671 Locations::first_location_after (nframes_t frame)
672 {
673         LocationList locs;
674
675         {
676                 Glib::Mutex::Lock lm (lock);
677                 locs = locations;
678         }
679
680         LocationStartEarlierComparison cmp;
681         locs.sort (cmp);
682
683         /* locs is now sorted earliest..latest */
684         
685         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
686                 if (!(*i)->is_hidden() && (*i)->start() > frame) {
687                         return (*i);
688                 }
689         }
690
691         return 0;
692 }
693
694 nframes_t
695 Locations::first_mark_before (nframes_t frame)
696 {
697         LocationList locs;
698
699         {
700         Glib::Mutex::Lock lm (lock);
701                 locs = locations;
702         }
703
704         LocationStartLaterComparison cmp;
705         locs.sort (cmp);
706
707         /* locs is now sorted latest..earliest */
708         
709         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
710                 if (!(*i)->is_hidden()) {
711                         if ((*i)->is_mark()) {
712                                 /* MARK: start == end */
713                                 if ((*i)->start() < frame) {
714                                         return (*i)->start();
715                                 }
716                         } else {
717                                 /* RANGE: start != end, compare start and end */
718                                 if ((*i)->end() < frame) {
719                                         return (*i)->end();
720                                 }
721                                 if ((*i)->start () < frame) {
722                                         return (*i)->start();
723                                 }
724                         }
725                 }
726         }
727
728         return 0;
729 }
730
731 nframes_t
732 Locations::first_mark_after (nframes_t frame)
733 {
734         LocationList locs;
735
736         {
737         Glib::Mutex::Lock lm (lock);
738                 locs = locations;
739         }
740
741         LocationStartEarlierComparison cmp;
742         locs.sort (cmp);
743
744         /* locs is now sorted earliest..latest */
745         
746         for (LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
747                 if (!(*i)->is_hidden()) {
748                         if ((*i)->is_mark()) {
749                                 /* MARK, start == end so just compare start */
750                                 if ((*i)->start() > frame) {
751                                         return (*i)->start();
752                                 }
753                         } else {
754                                 /* RANGE, start != end, compare start and end */
755                                 if ((*i)->start() > frame ) {
756                                         return (*i)->start ();
757                                 }
758                                 if ((*i)->end() > frame) {
759                                         return (*i)->end ();
760                                 }
761                         }
762                 }
763         }
764
765         return max_frames;
766 }
767
768 Location*
769 Locations::end_location () const
770 {
771         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
772                 if ((*i)->is_end()) {
773                         return const_cast<Location*> (*i);
774                 }
775         }
776         return 0;
777 }       
778
779 Location*
780 Locations::start_location () const
781 {
782         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
783                 if ((*i)->is_start()) {
784                         return const_cast<Location*> (*i);
785                 }
786         }
787         return 0;
788 }       
789
790 Location*
791 Locations::auto_loop_location () const
792 {
793         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
794                 if ((*i)->is_auto_loop()) {
795                         return const_cast<Location*> (*i);
796                 }
797         }
798         return 0;
799 }       
800
801 Location*
802 Locations::auto_punch_location () const
803 {
804         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
805                 if ((*i)->is_auto_punch()) {
806                         return const_cast<Location*> (*i);
807                 }
808         }
809        return 0;
810 }       
811
812 StateManager::State*
813 Locations::state_factory (std::string why) const
814 {
815         State* state = new State (why);
816
817         state->locations = locations;
818         
819         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
820                 state->states.push_back (new Location (**i));
821         }
822
823         return state;
824 }
825
826 Change
827 Locations::restore_state (StateManager::State& state) 
828 {
829         {
830                 Glib::Mutex::Lock lm (lock);
831                 State* lstate = dynamic_cast<State*> (&state);
832
833                 locations = lstate->locations;
834                 LocationList& states = lstate->states;
835                 LocationList::iterator l, s;
836
837                 for (l = locations.begin(), s = states.begin(); s != states.end(); ++s, ++l) {
838                         (*l) = (*s);
839                 }
840         }
841
842         return Change (0);
843 }
844
845 UndoAction
846 Locations::get_memento () const
847 {
848   return sigc::bind (mem_fun (*(const_cast<Locations*> (this)), &StateManager::use_state), _current_state_id);
849 }
850
851 uint32_t
852 Locations::num_range_markers () const
853 {
854         uint32_t cnt = 0;
855         Glib::Mutex::Lock lm (lock);
856         for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) {
857                 if ((*i)->is_range_marker()) {
858                         ++cnt;
859                 }
860         }
861         return cnt;
862 }
863
864 Location *
865 Locations::get_location_by_id(PBD::ID id)
866 {
867     LocationList::iterator it;
868     for (it  = locations.begin(); it != locations.end(); it++)
869         if (id == (*it)->id())
870             return *it;
871
872     return 0;
873 }