d08aae57aef58e609e4962bea2ac48ebc76c69f9
[goodguy/cinelerra.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 && first && first->position <= position)
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 && last && last->position >= position)
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, 1);
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, int create)
284 {
285         if(position < 0) {
286                 position = edl->local_session->get_selectionstart(1);
287         }
288
289         Auto *result = 0;
290         get_prev_auto(track->to_units(position, 0), PLAY_FORWARD, result);
291         if( create > 0 ) create = edl->session->auto_keyframes;
292         if( create && (!result || result->is_default ||
293             !EQUIV(track->from_units(result->position), position)) ) {
294 //printf("Autos::get_auto_for_editing %p %p %p\n", default_auto, first, result);
295                 position = edl->align_to_frame(position, 0);
296                 result = insert_auto(track->to_units(position, 0));
297         }
298 //printf("Autos::get_auto_for_editing %p %p\n", first, default_auto);
299
300         return result;
301 }
302
303
304 Auto* Autos::get_next_auto(int64_t position, int direction, Auto* &current, int use_default)
305 {
306         if(direction == PLAY_FORWARD)
307         {
308                 if(current)
309                 {
310                         while(current && current->position > position) current = PREVIOUS;
311                         while(current && current->position < position) current = NEXT;
312                 }
313
314                 if(!current && last && last->position > position)
315                 {
316                         for(current = first;
317                                 current && current->position <= position;
318                                 current = NEXT)
319                                 ;
320                 }
321
322                 if(!current && use_default) current = (last ? last : default_auto);
323         }
324         else
325         if(direction == PLAY_REVERSE)
326         {
327                 if(current)
328                 {
329                         while(current && current->position < position) current = NEXT;
330                         while(current && current->position > position) current = PREVIOUS;
331                 }
332
333                 if(!current && first && first->position <= position)
334                 {
335                         for(current = last;
336                                 current && current->position > position;
337                                 current = PREVIOUS)
338                                 ;
339                 }
340
341                 if(!current && use_default) current = (first ? first : default_auto);
342         }
343
344         return current;
345 }
346 Auto* Autos::insert_auto(int64_t position, Auto *templ)
347 {
348         Auto *current, *result;
349
350 // Test for existence
351         for(current = first;
352                 current && !edl->equivalent(current->position, position);
353                 current = NEXT)
354         {
355                 ;
356         }
357
358 // Insert new
359         if(!current)
360         {
361 // Get first one on or before as a template
362                 for(current = last;
363                         current && current->position > position;
364                         current = PREVIOUS)
365                 {
366                         ;
367                 }
368
369                 if(current)
370                 {
371                         insert_after(current, result = new_auto());
372                 }
373                 else
374                 {
375                         current = first;
376                         if(!current) current = default_auto;
377
378                         insert_before(first, result = new_auto());
379                 }
380
381 // interpolate if possible, else copy from template
382                 result->interpolate_from(0, 0, position, templ);
383 // Set curve mode
384                 if( !templ && result->is_floatauto() ) {
385                         FloatAuto *floatauto = (FloatAuto *)result;
386                         FloatAuto::t_mode new_mode =
387                                 edl->local_session->playback_start >= 0 &&
388                                 edl->local_session->playback_end < 0 ? FloatAuto::SMOOTH :
389                                         (FloatAuto::t_mode) edl->local_session->floatauto_type;
390                         floatauto->change_curve_mode(new_mode, 0);
391                 }
392         }
393         else
394         {
395                 result = current;
396         }
397
398         return result;
399 }
400
401 void Autos::clear_all()
402 {
403         while( last ) delete last;
404         delete default_auto;
405         create_objects();
406 }
407
408 int Autos::insert(int64_t start, int64_t end)
409 {
410         int64_t length;
411         Auto *current = first;
412
413         for( ; current && current->position < start; current = NEXT)
414                 ;
415
416         length = end - start;
417
418         for(; current; current = NEXT)
419         {
420                 current->position += length;
421         }
422         return 0;
423 }
424
425 void Autos::paste(int64_t start,
426         int64_t length,
427         double scale,
428         FileXML *file,
429         int default_only,
430         int active_only)
431 {
432         int total = 0;
433         int result = 0;
434
435 //printf("Autos::paste %d start=%jd\n", __LINE__, start);
436         do{
437                 result = file->read_tag();
438
439                 if(!result && !file->tag.title_is("/AUTO"))
440                 {
441 // End of list
442                         if(file->tag.get_title()[0] == '/')
443                         {
444                                 result = 1;
445                         }
446                         else
447                         if(!strcmp(file->tag.get_title(), "AUTO"))
448                         {
449                                 Auto *current = 0;
450
451 // Paste first auto into default
452                                 if(default_only && total == 0)
453                                 {
454                                         current = default_auto;
455                                 }
456                                 else
457 // Paste default auto into default
458                                 if(!default_only)
459                                 {
460                                         int64_t position = Units::to_int64(
461                                                 (double)file->tag.get_property("POSITION", 0) *
462                                                         scale +
463                                                         start);
464 // Paste active auto into track
465                                         current = insert_auto(position);
466                                 }
467
468                                 if(current)
469                                 {
470                                         current->load(file);
471                                 }
472                                 total++;
473                         }
474                 }
475         } while( !result );
476 }
477
478
479 int Autos::paste_silence(int64_t start, int64_t end)
480 {
481         insert(start, end);
482         return 0;
483 }
484
485 int Autos::copy(int64_t start,
486         int64_t end,
487         FileXML *file,
488         int default_only,
489         int active_only)
490 {
491 // First auto always loaded with default
492 //printf("Autos::copy %d %d %d\n", __LINE__, default_only, active_only);
493         if(default_only || (!active_only && !default_only))
494         {
495                 default_auto->copy(0, 0, file, default_only);
496         }
497
498 //printf("Autos::copy 10 %d %d %p\n", default_only, start, autoof(start));
499         if(active_only || (!default_only && !active_only))
500         {
501                 Auto *current = autoof(start);
502
503                 while( current && current->position <= end ) {
504 // Want to copy single keyframes by putting the cursor on them
505                         if( current->position >= start && current->position <= end ) {
506                                 current->copy(start, end, file, default_only);
507                         }
508                         current = NEXT;
509                 }
510         }
511 // Copy default auto again to make it the active auto on the clipboard
512 //      else
513 //      {
514 // Need to force position to 0 for the case of plugins
515 // and default status to 0.
516 //              default_auto->copy(0, 0, file, default_only);
517 //      }
518 //printf("Autos::copy 20\n");
519
520         return 0;
521 }
522
523 // Remove 3 consecutive autos with the same value
524 // Remove autos which are out of order
525 void Autos::optimize()
526 {
527         int done = 0;
528
529
530 // Default auto should always be at 0
531         default_auto->position = 0;
532         while(!done)
533         {
534                 int consecutive = 0;
535                 done = 1;
536
537
538                 for(Auto *current = first; current; current = NEXT)
539                 {
540 // Get 3rd consecutive auto of equal value
541                         if(current != first)
542                         {
543                                 if(*current == *PREVIOUS)
544                                 {
545                                         consecutive++;
546                                         if(consecutive >= 3)
547                                         {
548                                                 delete PREVIOUS;
549                                                 break;
550                                         }
551                                 }
552                                 else
553                                         consecutive = 0;
554
555                                 if(done && current->position <= PREVIOUS->position)
556                                 {
557                                         delete current;
558                                         break;
559                                 }
560                         }
561                 }
562         }
563 }
564
565
566 void Autos::remove_nonsequential(Auto *keyframe)
567 {
568         if((keyframe->next && keyframe->next->position <= keyframe->position) ||
569                 (keyframe->previous && keyframe->previous->position >= keyframe->position))
570         {
571                 delete keyframe;
572         }
573 }
574
575
576 void Autos::set_automation_mode(int64_t start, int64_t end, int mode)
577 {
578 }
579
580 void Autos::clear(int64_t start,
581         int64_t end,
582         int shift_autos)
583 {
584         int64_t length;
585         Auto *next, *current;
586         length = end - start;
587
588
589         current = autoof(start);
590
591 // If a range is selected don't delete the ending keyframe but do delete
592 // the beginning keyframe because shifting end handle forward shouldn't
593 // delete the first keyframe of the next edit.
594
595         while(current &&
596                 ((end != start && current->position < end) ||
597                 (end == start && current->position <= end)))
598         {
599                 next = NEXT;
600                 remove(current);
601                 current = next;
602         }
603
604         while(current && shift_autos)
605         {
606                 current->position -= length;
607                 current = NEXT;
608         }
609 }
610
611 int Autos::clear_auto(int64_t position)
612 {
613         Auto *current;
614         current = autoof(position);
615         return current->position==position ? (remove(current), 1) : 0;
616 }
617
618
619 int Autos::load(FileXML *file)
620 {
621         while(last)
622                 remove(last);    // remove any existing autos
623
624         int result = 0, first_auto = 1;
625         Auto *current;
626
627         do{
628                 result = file->read_tag();
629
630                 if(!result && !file->tag.title_is("/AUTO"))
631                 {
632 // First tag with leading / is taken as end of autos
633                         if(/* strstr(file->tag.get_title(), "AUTOS") && */
634
635                                 file->tag.get_title()[0] == '/')
636                         {
637                                 result = 1;
638                         }
639                         else
640                         if(!strcmp(file->tag.get_title(), "AUTO"))
641                         {
642                                 if(first_auto)
643                                 {
644                                         default_auto->load(file);
645                                         default_auto->position = 0;
646                                         first_auto = 0;
647                                 }
648                                 else
649                                 {
650                                         current = append(new_auto());
651                                         current->position = file->tag.get_property("POSITION", (int64_t)0);
652                                         current->load(file);
653                                 }
654                         }
655                 }
656         } while( !result );
657         return 0;
658 }
659
660
661
662
663
664
665 int Autos::slope_adjustment(int64_t ax, double slope)
666 {
667         return (int)(ax * slope);
668 }
669
670
671 int Autos::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
672 {
673         Auto *current;
674
675         for(current = first; current && scale_autos; current = NEXT)
676         {
677 //              if(current->position >= start && current->position <= end)
678 //              {
679                         current->position = (int64_t)((current->position - start) * rate_scale + start + 0.5);
680 //              }
681         }
682         return 0;
683 }
684
685 Auto* Autos::autoof(int64_t position)
686 {
687         Auto *current;
688
689         for(current = first;
690                 current && current->position < position;
691                 current = NEXT)
692         {
693                 ;
694         }
695         return current;     // return 0 on failure
696 }
697
698 Auto* Autos::nearest_before(int64_t position)
699 {
700         Auto *current;
701         for(current = last; current && current->position >= position; current = PREVIOUS);
702         return current;     // return 0 on failure
703 }
704
705 Auto* Autos::nearest_after(int64_t position)
706 {
707         Auto *current;
708         for(current = first; current && current->position <= position; current = NEXT);
709         return current;     // return 0 on failure
710 }
711
712 int Autos::get_neighbors(int64_t start, int64_t end, Auto **before, Auto **after)
713 {
714         if(*before == 0) *before = first;
715         if(*after == 0) *after = last;
716
717         while(*before && (*before)->next && (*before)->next->position <= start)
718                 *before = (*before)->next;
719
720         while(*after && (*after)->previous && (*after)->previous->position >= end)
721                 *after = (*after)->previous;
722
723         while(*before && (*before)->position > start) *before = (*before)->previous;
724
725         while(*after && (*after)->position < end) *after = (*after)->next;
726         return 0;
727 }
728
729 int Autos::automation_is_constant(int64_t start, int64_t end)
730 {
731         return 0;
732 }
733
734 double Autos::get_automation_constant(int64_t start, int64_t end)
735 {
736         return 0;
737 }
738
739
740 int Autos::init_automation(int64_t &buffer_position,
741                                 int64_t &input_start,
742                                 int64_t &input_end,
743                                 int &automate,
744                                 double &constant,
745                                 int64_t input_position,
746                                 int64_t buffer_len,
747                                 Auto **before,
748                                 Auto **after,
749                                 int reverse)
750 {
751         buffer_position = 0;
752
753 // set start and end boundaries for automation info
754         input_start = reverse ? input_position - buffer_len : input_position;
755         input_end = reverse ? input_position : input_position + buffer_len;
756
757 // test automation for constant value
758 // and set up *before and *after
759         if(automate)
760         {
761                 if(automation_is_constant(input_start, input_end))
762                 {
763                         constant += get_automation_constant(input_start, input_end);
764                         automate = 0;
765                 }
766         }
767         return automate;
768 }
769
770
771 int Autos::init_slope(Auto **current_auto,
772                                 double &slope_start,
773                                 double &slope_value,
774                                 double &slope_position,
775                                 int64_t &input_start,
776                                 int64_t &input_end,
777                                 Auto **before,
778                                 Auto **after,
779                                 int reverse)
780 {
781 // apply automation
782         *current_auto = reverse ? *after : *before;
783 // no auto before start so use first auto in range
784 // already know there is an auto since automation isn't constant
785         if(!*current_auto)
786         {
787                 *current_auto = reverse ? last : first;
788 //              slope_value = (*current_auto)->value;
789                 slope_start = input_start;
790                 slope_position = 0;
791         }
792         else
793         {
794 // otherwise get the first slope point and advance auto
795 //              slope_value = (*current_auto)->value;
796                 slope_start = (*current_auto)->position;
797                 slope_position = reverse ? slope_start - input_end : input_start - slope_start;
798                 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
799         }
800         return 0;
801 }
802
803
804 int Autos::get_slope(Auto **current_auto,
805                                 double &slope_start,
806                                 double &slope_end,
807                                 double &slope_value,
808                                 double &slope,
809                                 int64_t buffer_len,
810                                 int64_t buffer_position,
811                                 int reverse)
812 {
813 // get the slope
814         if(*current_auto)
815         {
816                 slope_end = reverse ? slope_start - (*current_auto)->position : (*current_auto)->position - slope_start;
817                 if(slope_end)
818 //                      slope = ((*current_auto)->value - slope_value) / slope_end;
819 //              else
820                         slope = 0;
821         }
822         else
823         {
824                 slope = 0;
825                 slope_end = buffer_len - buffer_position;
826         }
827         return 0;
828 }
829
830 int Autos::advance_slope(Auto **current_auto,
831                                 double &slope_start,
832                                 double &slope_value,
833                                 double &slope_position,
834                                 int reverse)
835 {
836         if(*current_auto)
837         {
838                 slope_start = (*current_auto)->position;
839 //              slope_value = (*current_auto)->value;
840                 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
841                 slope_position = 0;
842         }
843         return 0;
844 }
845
846 int64_t Autos::get_length()
847 {
848         if(last)
849                 return last->position + 1;
850         else
851                 return 0;
852 }
853
854 void Autos::get_extents(float *min,
855         float *max,
856         int *coords_undefined,
857         int64_t unit_start,
858         int64_t unit_end)
859 {
860
861 }
862
863
864 void Autos::dump(FILE *fp)
865 {
866 }
867
868
869
870
871
872
873
874
875
876