36abbd7dc756041d1d1cb1a356f24bb312508a70
[goodguy/history.git] / cinelerra-5.1 / cinelerra / autos.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "autos.h"
23 #include "clip.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "floatauto.h"
27 #include "localsession.h"
28 #include "filexml.h"
29 #include "track.h"
30 #include "transportque.inc"
31
32
33 Autos::Autos(EDL *edl, Track *track)
34  : List<Auto>()
35 {
36         this->edl = edl;
37         this->track = track;
38         type = -1;
39         autoidx = -1;
40         autogrouptype = -1;
41 }
42
43
44
45 Autos::~Autos()
46 {
47         while(last) delete last;
48         delete default_auto;
49 }
50
51 void Autos::create_objects()
52 {
53 // Default
54         default_auto = new_auto();
55         default_auto->is_default = 1;
56 }
57
58 int Autos::get_type()
59 {
60         return type;
61 }
62
63 Auto* Autos::append_auto()
64 {
65         return append(new_auto());
66 }
67
68
69 Auto* Autos::new_auto()
70 {
71         return new Auto(edl, this);
72 }
73
74 void Autos::resample(double old_rate, double new_rate)
75 {
76         for(Auto *current = first; current; current = NEXT)
77         {
78                 current->position = (int64_t)((double)current->position *
79                         new_rate /
80                         old_rate +
81                         0.5);
82         }
83 }
84
85 void Autos::equivalent_output(Autos *autos, int64_t startproject, int64_t *result)
86 {
87 // Default keyframe differs
88         if(!total() && !(*default_auto == *autos->default_auto))
89         {
90                 if(*result < 0 || *result > startproject) *result = startproject;
91         }
92         else
93 // Search for difference
94         {
95                 for(Auto *current = first, *that_current = autos->first;
96                         current || that_current;
97                         current = NEXT,
98                         that_current = that_current->next)
99                 {
100 // Total differs
101                         if(current && !that_current)
102                         {
103                                 int64_t position1 = (autos->last ? autos->last->position : startproject);
104                                 int64_t position2 = current->position;
105                                 if(*result < 0 || *result > MIN(position1, position2))
106                                         *result = MIN(position1, position2);
107                                 break;
108                         }
109                         else
110                         if(!current && that_current)
111                         {
112                                 int64_t position1 = (last ? last->position : startproject);
113                                 int64_t position2 = that_current->position;
114                                 if(*result < 0 || *result > MIN(position1, position2))
115                                         *result = MIN(position1, position2);
116                                 break;
117                         }
118                         else
119 // Keyframes differ
120                         if(!(*current == *that_current) ||
121                                 current->position != that_current->position)
122                         {
123                                 int64_t position1 = (current->previous ?
124                                         current->previous->position :
125                                         startproject);
126                                 int64_t position2 = (that_current->previous ?
127                                         that_current->previous->position :
128                                         startproject);
129                                 if(*result < 0 || *result > MIN(position1, position2))
130                                         *result = MIN(position1, position2);
131                                 break;
132                         }
133                 }
134         }
135 }
136
137 void Autos::copy_from(Autos *autos)
138 {
139         Auto *current = autos->first, *this_current = first;
140
141         default_auto->copy_from(autos->default_auto);
142
143 // Detect common memory leak bug
144         if(autos->first && !autos->last)
145         {
146                 printf("Autos::copy_from inconsistent pointers\n");
147                 exit(1);
148         }
149
150         for(current = autos->first; current; current = NEXT)
151         {
152 //printf("Autos::copy_from 1 %p\n", current);
153 //sleep(1);
154                 if(!this_current)
155                 {
156                         append(this_current = new_auto());
157                 }
158                 this_current->copy_from(current);
159                 this_current = this_current->next;
160         }
161
162         for( ; this_current; )
163         {
164                 Auto *next_current = this_current->next;
165                 delete this_current;
166                 this_current = next_current;
167         }
168 }
169
170
171 // We don't replace it in pasting but
172 // when inserting the first EDL of a load operation we need to replace
173 // the default keyframe.
174 void Autos::insert_track(Autos *automation,
175         int64_t start_unit,
176         int64_t length_units,
177         int replace_default)
178 {
179 // Insert silence
180         insert(start_unit, start_unit + length_units);
181
182         if(replace_default) default_auto->copy_from(automation->default_auto);
183         for(Auto *current = automation->first; current; current = NEXT)
184         {
185 // fill new auto with values from current (template), interpolate values if possible
186                 Auto *new_auto = insert_auto(start_unit + current->position, current);
187 // Override copy_from
188                 new_auto->position = current->position + start_unit;
189         }
190 }
191
192 Auto* Autos::get_prev_auto(int64_t position,
193         int direction,
194         Auto* &current,
195         int use_default)
196 {
197 // Get on or before position
198         if(direction == PLAY_FORWARD)
199         {
200 // Try existing result
201                 if(current)
202                 {
203                         while(current && current->position < position) current = NEXT;
204                         while(current && current->position > position) current = PREVIOUS;
205                 }
206
207                 if(!current)
208                 {
209                         for(current = last;
210                                 current && current->position > position;
211                                 current = PREVIOUS) ;
212                 }
213                 if(!current && use_default) current = (first ? first : default_auto);
214         }
215         else
216 // Get on or after position
217         if(direction == PLAY_REVERSE)
218         {
219                 if(current)
220                 {
221                         while(current && current->position > position) current = PREVIOUS;
222                         while(current && current->position < position) current = NEXT;
223                 }
224
225                 if(!current)
226                 {
227                         for(current = first;
228                                 current && current->position < position;
229                                 current = NEXT) ;
230                 }
231
232                 if(!current && use_default) current = (last ? last : default_auto);
233         }
234
235         return current;
236 }
237
238 Auto* Autos::get_prev_auto(int direction, Auto* &current)
239 {
240         double position_double = edl->local_session->get_selectionstart(1);
241         position_double = edl->align_to_frame(position_double, 0);
242         int64_t position = track->to_units(position_double, 0);
243
244         return get_prev_auto(position, direction, current);
245 }
246
247 int Autos::auto_exists_for_editing(double position)
248 {
249         int result = 0;
250
251         if(edl->session->auto_keyframes)
252         {
253                 double unit_position = position;
254                 unit_position = edl->align_to_frame(unit_position, 0);
255                 if (get_auto_at_position(unit_position))
256                         result = 1;
257         }
258         else
259         {
260                 result = 1;
261         }
262
263         return result;
264 }
265
266 Auto* Autos::get_auto_at_position(double position)
267 {
268         int64_t unit_position = track->to_units(position, 0);
269
270         for(Auto *current = first;
271                 current;
272                 current = NEXT)
273         {
274                 if(edl->equivalent(current->position, unit_position))
275                 {
276                         return current;
277                 }
278         }
279         return 0;
280 }
281
282
283 Auto* Autos::get_auto_for_editing(double position)
284 {
285         if(position < 0)
286         {
287                 position = edl->local_session->get_selectionstart(1);
288         }
289
290         Auto *result = 0;
291         position = edl->align_to_frame(position, 0);
292
293
294
295
296 //printf("Autos::get_auto_for_editing %p %p\n", first, default_auto);
297
298         if(edl->session->auto_keyframes)
299         {
300                 result = insert_auto(track->to_units(position, 0));
301         }
302         else
303                 result = get_prev_auto(track->to_units(position, 0),
304                         PLAY_FORWARD,
305                         result);
306
307 //printf("Autos::get_auto_for_editing %p %p %p\n", default_auto, first, result);
308         return result;
309 }
310
311
312 Auto* Autos::get_next_auto(int64_t position, int direction, Auto* &current, int use_default)
313 {
314         if(direction == PLAY_FORWARD)
315         {
316                 if(current)
317                 {
318                         while(current && current->position > position) current = PREVIOUS;
319                         while(current && current->position < position) current = NEXT;
320                 }
321
322                 if(!current)
323                 {
324                         for(current = first;
325                                 current && current->position <= position;
326                                 current = NEXT)
327                                 ;
328                 }
329
330                 if(!current && use_default) current = (last ? last : default_auto);
331         }
332         else
333         if(direction == PLAY_REVERSE)
334         {
335                 if(current)
336                 {
337                         while(current && current->position < position) current = NEXT;
338                         while(current && current->position > position) current = PREVIOUS;
339                 }
340
341                 if(!current)
342                 {
343                         for(current = last;
344                                 current && current->position > position;
345                                 current = PREVIOUS)
346                                 ;
347                 }
348
349                 if(!current && use_default) current = (first ? first : default_auto);
350         }
351
352         return current;
353 }
354 Auto* Autos::insert_auto(int64_t position, Auto *templ)
355 {
356         Auto *current, *result;
357
358 // Test for existence
359         for(current = first;
360                 current && !edl->equivalent(current->position, position);
361                 current = NEXT)
362         {
363                 ;
364         }
365
366 // Insert new
367         if(!current)
368         {
369 // Get first one on or before as a template
370                 for(current = last;
371                         current && current->position > position;
372                         current = PREVIOUS)
373                 {
374                         ;
375                 }
376
377                 if(current)
378                 {
379                         insert_after(current, result = new_auto());
380                 }
381                 else
382                 {
383                         current = first;
384                         if(!current) current = default_auto;
385
386                         insert_before(first, result = new_auto());
387                 }
388
389 // interpolate if possible, else copy from template
390                 result->interpolate_from(0, 0, position, templ);
391 // Set curve mode
392                 if( !templ && result->is_floatauto() ) {
393                         FloatAuto *floatauto = (FloatAuto *)result;
394                         floatauto->curve_mode =
395                                 edl->local_session->playback_start >= 0 &&
396                                 edl->local_session->playback_end < 0 ? FloatAuto::SMOOTH :
397                                         (FloatAuto::t_mode) edl->local_session->floatauto_type;
398                 }
399         }
400         else
401         {
402                 result = current;
403         }
404
405         return result;
406 }
407
408 int Autos::clear_all()
409 {
410         Auto *current_, *current;
411
412         for(current = first; current; current = current_)
413         {
414                 current_ = NEXT;
415                 remove(current);
416         }
417         append_auto();
418         return 0;
419 }
420
421 int Autos::insert(int64_t start, int64_t end)
422 {
423         int64_t length;
424         Auto *current = first;
425
426         for( ; current && current->position < start; current = NEXT)
427                 ;
428
429         length = end - start;
430
431         for(; current; current = NEXT)
432         {
433                 current->position += length;
434         }
435         return 0;
436 }
437
438 void Autos::paste(int64_t start,
439         int64_t length,
440         double scale,
441         FileXML *file,
442         int default_only,
443         int active_only)
444 {
445         int total = 0;
446         int result = 0;
447
448 //printf("Autos::paste %d start=%jd\n", __LINE__, start);
449         do{
450                 result = file->read_tag();
451
452                 if(!result && !file->tag.title_is("/AUTO"))
453                 {
454 // End of list
455                         if(file->tag.get_title()[0] == '/')
456                         {
457                                 result = 1;
458                         }
459                         else
460                         if(!strcmp(file->tag.get_title(), "AUTO"))
461                         {
462                                 Auto *current = 0;
463
464 // Paste first auto into default
465                                 if(default_only && total == 0)
466                                 {
467                                         current = default_auto;
468                                 }
469                                 else
470 // Paste default auto into default
471                                 if(!default_only)
472                                 {
473                                         int64_t position = Units::to_int64(
474                                                 (double)file->tag.get_property("POSITION", 0) *
475                                                         scale +
476                                                         start);
477 // Paste active auto into track
478                                         current = insert_auto(position);
479                                 }
480
481                                 if(current)
482                                 {
483                                         current->load(file);
484                                 }
485                                 total++;
486                         }
487                 }
488         } while( !result );
489 }
490
491
492 int Autos::paste_silence(int64_t start, int64_t end)
493 {
494         insert(start, end);
495         return 0;
496 }
497
498 int Autos::copy(int64_t start,
499         int64_t end,
500         FileXML *file,
501         int default_only,
502         int active_only)
503 {
504 // First auto always loaded with default
505 //printf("Autos::copy %d %d %d\n", __LINE__, default_only, active_only);
506         if(default_only || (!active_only && !default_only))
507         {
508                 default_auto->copy(0, 0, file, default_only);
509         }
510
511 //printf("Autos::copy 10 %d %d %p\n", default_only, start, autoof(start));
512         if(active_only || (!default_only && !active_only))
513         {
514                 for(Auto* current = autoof(start);
515                         current && current->position <= end;
516                         current = NEXT)
517                 {
518 // Want to copy single keyframes by putting the cursor on them
519                         if(current->position >= start && current->position <= end)
520                         {
521                                 current->copy(start, end, file, default_only);
522                         }
523                 }
524         }
525 // Copy default auto again to make it the active auto on the clipboard
526 //      else
527 //      {
528 // Need to force position to 0 for the case of plugins
529 // and default status to 0.
530 //              default_auto->copy(0, 0, file, default_only);
531 //      }
532 //printf("Autos::copy 20\n");
533
534         return 0;
535 }
536
537 // Remove 3 consecutive autos with the same value
538 // Remove autos which are out of order
539 void Autos::optimize()
540 {
541         int done = 0;
542
543
544 // Default auto should always be at 0
545         default_auto->position = 0;
546         while(!done)
547         {
548                 int consecutive = 0;
549                 done = 1;
550
551
552                 for(Auto *current = first; current; current = NEXT)
553                 {
554 // Get 3rd consecutive auto of equal value
555                         if(current != first)
556                         {
557                                 if(*current == *PREVIOUS)
558                                 {
559                                         consecutive++;
560                                         if(consecutive >= 3)
561                                         {
562                                                 delete PREVIOUS;
563                                                 break;
564                                         }
565                                 }
566                                 else
567                                         consecutive = 0;
568
569                                 if(done && current->position <= PREVIOUS->position)
570                                 {
571                                         delete current;
572                                         break;
573                                 }
574                         }
575                 }
576         }
577 }
578
579
580 void Autos::remove_nonsequential(Auto *keyframe)
581 {
582         if((keyframe->next && keyframe->next->position <= keyframe->position) ||
583                 (keyframe->previous && keyframe->previous->position >= keyframe->position))
584         {
585                 delete keyframe;
586         }
587 }
588
589
590 void Autos::set_automation_mode(int64_t start, int64_t end, int mode)
591 {
592 }
593
594 void Autos::clear(int64_t start,
595         int64_t end,
596         int shift_autos)
597 {
598         int64_t length;
599         Auto *next, *current;
600         length = end - start;
601
602
603         current = autoof(start);
604
605 // If a range is selected don't delete the ending keyframe but do delete
606 // the beginning keyframe because shifting end handle forward shouldn't
607 // delete the first keyframe of the next edit.
608
609         while(current &&
610                 ((end != start && current->position < end) ||
611                 (end == start && current->position <= end)))
612         {
613                 next = NEXT;
614                 remove(current);
615                 current = next;
616         }
617
618         while(current && shift_autos)
619         {
620                 current->position -= length;
621                 current = NEXT;
622         }
623 }
624
625 int Autos::clear_auto(int64_t position)
626 {
627         Auto *current;
628         current = autoof(position);
629         return current->position==position ? (remove(current), 1) : 0;
630 }
631
632
633 int Autos::load(FileXML *file)
634 {
635         while(last)
636                 remove(last);    // remove any existing autos
637
638         int result = 0, first_auto = 1;
639         Auto *current;
640
641         do{
642                 result = file->read_tag();
643
644                 if(!result && !file->tag.title_is("/AUTO"))
645                 {
646 // First tag with leading / is taken as end of autos
647                         if(/* strstr(file->tag.get_title(), "AUTOS") && */
648
649                                 file->tag.get_title()[0] == '/')
650                         {
651                                 result = 1;
652                         }
653                         else
654                         if(!strcmp(file->tag.get_title(), "AUTO"))
655                         {
656                                 if(first_auto)
657                                 {
658                                         default_auto->load(file);
659                                         default_auto->position = 0;
660                                         first_auto = 0;
661                                 }
662                                 else
663                                 {
664                                         current = append(new_auto());
665                                         current->position = file->tag.get_property("POSITION", (int64_t)0);
666                                         current->load(file);
667                                 }
668                         }
669                 }
670         } while( !result );
671         return 0;
672 }
673
674
675
676
677
678
679 int Autos::slope_adjustment(int64_t ax, double slope)
680 {
681         return (int)(ax * slope);
682 }
683
684
685 int Autos::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
686 {
687         Auto *current;
688
689         for(current = first; current && scale_autos; current = NEXT)
690         {
691 //              if(current->position >= start && current->position <= end)
692 //              {
693                         current->position = (int64_t)((current->position - start) * rate_scale + start + 0.5);
694 //              }
695         }
696         return 0;
697 }
698
699 Auto* Autos::autoof(int64_t position)
700 {
701         Auto *current;
702
703         for(current = first;
704                 current && current->position < position;
705                 current = NEXT)
706         {
707                 ;
708         }
709         return current;     // return 0 on failure
710 }
711
712 Auto* Autos::nearest_before(int64_t position)
713 {
714         Auto *current;
715
716         for(current = last; current && current->position >= position; current = PREVIOUS)
717         { ; }
718
719
720         return current;     // return 0 on failure
721 }
722
723 Auto* Autos::nearest_after(int64_t position)
724 {
725         Auto *current;
726
727         for(current = first; current && current->position <= position; current = NEXT)
728         { ; }
729
730
731         return current;     // return 0 on failure
732 }
733
734 int Autos::get_neighbors(int64_t start, int64_t end, Auto **before, Auto **after)
735 {
736         if(*before == 0) *before = first;
737         if(*after == 0) *after = last;
738
739         while(*before && (*before)->next && (*before)->next->position <= start)
740                 *before = (*before)->next;
741
742         while(*after && (*after)->previous && (*after)->previous->position >= end)
743                 *after = (*after)->previous;
744
745         while(*before && (*before)->position > start) *before = (*before)->previous;
746
747         while(*after && (*after)->position < end) *after = (*after)->next;
748         return 0;
749 }
750
751 int Autos::automation_is_constant(int64_t start, int64_t end)
752 {
753         return 0;
754 }
755
756 double Autos::get_automation_constant(int64_t start, int64_t end)
757 {
758         return 0;
759 }
760
761
762 int Autos::init_automation(int64_t &buffer_position,
763                                 int64_t &input_start,
764                                 int64_t &input_end,
765                                 int &automate,
766                                 double &constant,
767                                 int64_t input_position,
768                                 int64_t buffer_len,
769                                 Auto **before,
770                                 Auto **after,
771                                 int reverse)
772 {
773         buffer_position = 0;
774
775 // set start and end boundaries for automation info
776         input_start = reverse ? input_position - buffer_len : input_position;
777         input_end = reverse ? input_position : input_position + buffer_len;
778
779 // test automation for constant value
780 // and set up *before and *after
781         if(automate)
782         {
783                 if(automation_is_constant(input_start, input_end))
784                 {
785                         constant += get_automation_constant(input_start, input_end);
786                         automate = 0;
787                 }
788         }
789         return automate;
790 }
791
792
793 int Autos::init_slope(Auto **current_auto,
794                                 double &slope_start,
795                                 double &slope_value,
796                                 double &slope_position,
797                                 int64_t &input_start,
798                                 int64_t &input_end,
799                                 Auto **before,
800                                 Auto **after,
801                                 int reverse)
802 {
803 // apply automation
804         *current_auto = reverse ? *after : *before;
805 // no auto before start so use first auto in range
806 // already know there is an auto since automation isn't constant
807         if(!*current_auto)
808         {
809                 *current_auto = reverse ? last : first;
810 //              slope_value = (*current_auto)->value;
811                 slope_start = input_start;
812                 slope_position = 0;
813         }
814         else
815         {
816 // otherwise get the first slope point and advance auto
817 //              slope_value = (*current_auto)->value;
818                 slope_start = (*current_auto)->position;
819                 slope_position = reverse ? slope_start - input_end : input_start - slope_start;
820                 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
821         }
822         return 0;
823 }
824
825
826 int Autos::get_slope(Auto **current_auto,
827                                 double &slope_start,
828                                 double &slope_end,
829                                 double &slope_value,
830                                 double &slope,
831                                 int64_t buffer_len,
832                                 int64_t buffer_position,
833                                 int reverse)
834 {
835 // get the slope
836         if(*current_auto)
837         {
838                 slope_end = reverse ? slope_start - (*current_auto)->position : (*current_auto)->position - slope_start;
839                 if(slope_end)
840 //                      slope = ((*current_auto)->value - slope_value) / slope_end;
841 //              else
842                         slope = 0;
843         }
844         else
845         {
846                 slope = 0;
847                 slope_end = buffer_len - buffer_position;
848         }
849         return 0;
850 }
851
852 int Autos::advance_slope(Auto **current_auto,
853                                 double &slope_start,
854                                 double &slope_value,
855                                 double &slope_position,
856                                 int reverse)
857 {
858         if(*current_auto)
859         {
860                 slope_start = (*current_auto)->position;
861 //              slope_value = (*current_auto)->value;
862                 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
863                 slope_position = 0;
864         }
865         return 0;
866 }
867
868 int64_t Autos::get_length()
869 {
870         if(last)
871                 return last->position + 1;
872         else
873                 return 0;
874 }
875
876 void Autos::get_extents(float *min,
877         float *max,
878         int *coords_undefined,
879         int64_t unit_start,
880         int64_t unit_end)
881 {
882
883 }
884
885
886 void Autos::dump(FILE *fp)
887 {
888 }
889
890
891
892
893
894
895
896
897
898