r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / autos.C
blobc934b3dc78ac3c565a1a206ddaa0304b49a037c5
1 #include "autos.h"
2 #include "clip.h"
3 #include "edl.h"
4 #include "edlsession.h"
5 #include "localsession.h"
6 #include "filexml.h"
7 #include "track.h"
8 #include "transportque.inc"
10 #include <string.h>
13 Autos::Autos(EDL *edl, Track *track)
14  : List<Auto>()
16         this->edl = edl;
17         this->track = track;
18         type = -1;
23 Autos::~Autos()
25         while(last) delete last;
26         delete default_auto;
29 void Autos::create_objects()
31 // Default
32         default_auto = new_auto();
33         default_auto->is_default = 1;
36 int Autos::get_type()
38         return type;
41 Auto* Autos::append_auto()
43         return append(new_auto());
47 Auto* Autos::new_auto()
49         return new Auto(edl, this);
52 void Autos::resample(double old_rate, double new_rate)
54         for(Auto *current = first; current; current = NEXT)
55         {
56                 current->position = (int64_t)((double)current->position * 
57                         new_rate / 
58                         old_rate + 
59                         0.5);
60         }
63 void Autos::equivalent_output(Autos *autos, int64_t startproject, int64_t *result)
65 // Default keyframe differs
66         if(!total() && !(*default_auto == *autos->default_auto))
67         {
68                 if(*result < 0 || *result > startproject) *result = startproject;
69         }
70         else
71 // Search for difference
72         {
73                 for(Auto *current = first, *that_current = autos->first; 
74                         current || that_current; 
75                         current = NEXT,
76                         that_current = that_current->next)
77                 {
78 // Total differs
79                         if(current && !that_current)
80                         {
81                                 int64_t position1 = (autos->last ? autos->last->position : startproject);
82                                 int64_t position2 = current->position;
83                                 if(*result < 0 || *result > MIN(position1, position2))
84                                         *result = MIN(position1, position2);
85                                 break;
86                         }
87                         else
88                         if(!current && that_current)
89                         {
90                                 int64_t position1 = (last ? last->position : startproject);
91                                 int64_t position2 = that_current->position;
92                                 if(*result < 0 || *result > MIN(position1, position2))
93                                         *result = MIN(position1, position2);
94                                 break;
95                         }
96                         else
97 // Keyframes differ
98                         if(!(*current == *that_current) || 
99                                 current->position != that_current->position)
100                         {
101                                 int64_t position1 = (current->previous ? 
102                                         current->previous->position : 
103                                         startproject);
104                                 int64_t position2 = (that_current->previous ? 
105                                         that_current->previous->position : 
106                                         startproject);
107                                 if(*result < 0 || *result > MIN(position1, position2))
108                                         *result = MIN(position1, position2);
109                                 break;
110                         }
111                 }
112         }
115 void Autos::copy_from(Autos *autos)
117         Auto *current = autos->first, *this_current = first;
119         default_auto->copy_from(autos->default_auto);
121 // Detect common memory leak bug
122         if(autos->first && !autos->last)
123         {
124                 printf("Autos::copy_from inconsistent pointers\n");
125                 exit(1);
126         }
128         for(current = autos->first; current; current = NEXT)
129         {
130 //printf("Autos::copy_from 1 %p\n", current);
131 //sleep(1);
132                 if(!this_current)
133                 {
134                         append(this_current = new_auto());
135                 }
136                 this_current->copy_from(current);
137                 this_current = this_current->next;
138         }
140         for( ; this_current; )
141         {
142                 Auto *next_current = this_current->next;
143                 delete this_current;
144                 this_current = next_current;
145         }
149 // We don't replace it in pasting but
150 // when inserting the first EDL of a load operation we need to replace
151 // the default keyframe.
152 void Autos::insert_track(Autos *automation, 
153         int64_t start_unit, 
154         int64_t length_units,
155         int replace_default)
157 // Insert silence
158         insert(start_unit, start_unit + length_units);
160         if(replace_default) default_auto->copy_from(automation->default_auto);
161         for(Auto *current = automation->first; current; current = NEXT)
162         {
163                 Auto *new_auto = insert_auto(start_unit + current->position);
164                 new_auto->copy_from(current);
165 // Override copy_from
166                 new_auto->position = current->position + start_unit;
167         }
170 Auto* Autos::get_prev_auto(int64_t position, 
171         int direction, 
172         Auto* &current, 
173         int use_default)
175 // Get on or before position
176         if(direction == PLAY_FORWARD)
177         {
178 // Try existing result
179                 if(current)
180                 {
181                         while(current && current->position < position) current = NEXT;
182                         while(current && current->position > position) current = PREVIOUS;
183                 }
185                 if(!current)
186                 {
187                         for(current = last; 
188                                 current && current->position > position; 
189                                 current = PREVIOUS) ;
190                 }
191                 if(!current && use_default) current = (first ? first : default_auto);
192         }
193         else
194 // Get on or after position
195         if(direction == PLAY_REVERSE)
196         {
197                 if(current)
198                 {
199                         while(current && current->position > position) current = PREVIOUS;
200                         while(current && current->position < position) current = NEXT;
201                 }
203                 if(!current)
204                 {
205                         for(current = first; 
206                                 current && current->position < position; 
207                                 current = NEXT) ;
208                 }
210                 if(!current && use_default) current = (last ? last : default_auto);
211         }
213         return current;
216 Auto* Autos::get_prev_auto(int direction, Auto* &current)
218         double position_double = edl->local_session->get_selectionstart(1);
219         position_double = edl->align_to_frame(position_double, 0);
220         int64_t position = track->to_units(position_double, 0);
222         return get_prev_auto(position, direction, current);
224         return current;
227 int Autos::auto_exists_for_editing(double position)
229         int result = 0;
230         
231         if(edl->session->auto_keyframes)
232         {
233                 double unit_position = position;
234                 unit_position = edl->align_to_frame(unit_position, 0);
235                 if (get_auto_at_position(unit_position))
236                         result = 1;
237         }
238         else
239         {
240                 result = 1;
241         }
243         return result;
246 Auto* Autos::get_auto_at_position(double position)
248         int64_t unit_position = track->to_units(position, 0);
250         for(Auto *current = first; 
251                 current; 
252                 current = NEXT)
253         {
254                 if(edl->equivalent(current->position, unit_position))
255                 {
256                         return current;
257                 }
258         }
259         return 0;
263 Auto* Autos::get_auto_for_editing(double position)
265         if(position < 0)
266         {
267                 position = edl->local_session->get_selectionstart(1);
268         }
270         Auto *result = 0;
271         position = edl->align_to_frame(position, 0);
276 //printf("Autos::get_auto_for_editing %p %p\n", first, default_auto);
278         if(edl->session->auto_keyframes)
279         {
280                 result = insert_auto_for_editing(track->to_units(position, 0));
281         }
282         else
283                 result = get_prev_auto(track->to_units(position, 0), 
284                         PLAY_FORWARD, 
285                         result);
287 //printf("Autos::get_auto_for_editing %p %p %p\n", default_auto, first, result);
288         return result;
292 Auto* Autos::get_next_auto(int64_t position, int direction, Auto* &current, int use_default)
294         if(direction == PLAY_FORWARD)
295         {
296                 if(current)
297                 {
298                         while(current && current->position > position) current = PREVIOUS;
299                         while(current && current->position < position) current = NEXT;
300                 }
302                 if(!current)
303                 {
304                         for(current = first;
305                                 current && current->position <= position;
306                                 current = NEXT)
307                                 ;
308                 }
310                 if(!current && use_default) current = (last ? last : default_auto);
311         }
312         else
313         if(direction == PLAY_REVERSE)
314         {
315                 if(current)
316                 {
317                         while(current && current->position < position) current = NEXT;
318                         while(current && current->position > position) current = PREVIOUS;
319                 }
321                 if(!current)
322                 {
323                         for(current = last;
324                                 current && current->position > position;
325                                 current = PREVIOUS)
326                                 ;
327                 }
329                 if(!current && use_default) current = (first ? first : default_auto);
330         }
332         return current;
335 Auto* Autos::insert_auto(int64_t position)
337         Auto *current, *result;
339 // Test for existence
340         for(current = first; 
341                 current && !edl->equivalent(current->position, position); 
342                 current = NEXT)
343         {
344                 ;
345         }
347 // Insert new
348         if(!current)
349         {
350 // Get first one on or before as a template
351                 for(current = last; 
352                         current && current->position > position; 
353                         current = PREVIOUS)
354                 {
355                         ;
356                 }
358                 if(current)
359                 {
360                         insert_after(current, result = new_auto());
361                         result->copy_from(current);
362                 }
363                 else
364                 {
365                         current = first;
366                         if(!current) current = default_auto;
368                         insert_before(first, result = new_auto());
369                         if(current) result->copy_from(current);
370                 }
372                 result->position = position;
373         }
374         else
375         {
376                 result = current;
377         }
379         return result;
382 Auto* Autos::insert_auto_for_editing(int64_t position)
384         Auto *current, *result;
386 // Test for existence
387         for(current = first; 
388                 current && !edl->equivalent(current->position, position); 
389                 current = NEXT)
390         {
391                 ;
392         }
394 //printf("Autos::insert_auto_for_editing %p\n", current);
395 // Insert new
396         if(!current)
397         {
398 // Get first one on or before as a template
399                 for(current = last; 
400                         current && current->position > position; 
401                         current = PREVIOUS)
402                 {
403                         ;
404                 }
406                 if(current)
407                 {
408                         Auto *next = NEXT;
409                         insert_after(current, result = new_auto());
410                         result->interpolate_from(current, next, position);
411                 }
412                 else
413                 {
414                         current = first;
415                         if(!current) current = default_auto;
417                         insert_before(first, result = new_auto());
418                         if(current) result->copy_from(current);
419                 }
421                 result->position = position;
422         }
423         else
424         {
425                 result = current;
426         }
428         return result;
431 int Autos::clear_all()
433         Auto *current_, *current;
434         
435         for(current = first; current; current = current_)
436         {
437                 current_ = NEXT;
438                 remove(current);
439         }
440         append_auto();
441         return 0;
444 int Autos::insert(int64_t start, int64_t end)
446         int64_t length;
447         Auto *current = first;
449         for( ; current && current->position < start; current = NEXT)
450                 ;
452         length = end - start;
454         for(; current; current = NEXT)
455         {
456                 current->position += length;
457         }
458         return 0;
461 void Autos::paste(int64_t start, 
462         int64_t length, 
463         double scale, 
464         FileXML *file, 
465         int default_only)
467         int total = 0;
468         int result = 0;
470 //printf("Autos::paste %ld\n", start);
471         do{
472                 result = file->read_tag();
474                 if(!result && !file->tag.title_is("/AUTO"))
475                 {
476 // End of list
477                         if(/* strstr(file->tag.get_title(), "AUTOS") && */
478                                 file->tag.get_title()[0] == '/')
479                         {
480                                 result = 1;
481                         }
482                         else
483                         if(!strcmp(file->tag.get_title(), "AUTO"))
484                         {
485                                 Auto *current = 0;
487 // Paste first active auto into default                         
488                                 if(default_only)
489                                 {
490                                         if(total == 1)
491                                         {
492                                                 current = default_auto;
493                                         }
494                                 }
495                                 else
496 // Paste default auto into default
497                                 if(total == 0)
498                                         current = default_auto;
499                                 else
500                                 {
501                                         int64_t position = Units::to_int64(
502                                                 (double)file->tag.get_property("POSITION", 0) *
503                                                         scale + 
504                                                         start);
505 // Paste active auto into track
506                                         current = insert_auto(position);
507                                 }
509                                 if(current)
510                                 {
511                                         current->load(file);
512                                 }
513                                 total++;
514                         }
515                 }
516         }while(!result);
517         
521 int Autos::paste_silence(int64_t start, int64_t end)
523         insert(start, end);
524         return 0;
527 int Autos::copy(int64_t start, 
528         int64_t end, 
529         FileXML *file, 
530         int default_only,
531         int autos_only)
533 // First auto is always loaded into default even if it is discarded in a paste
534 // operation
535 //printf("Autos::copy 1 %d %d %p\n", default_only, start, autoof(start));
536         if(!autos_only)
537         {
538                 default_auto->copy(0, 0, file, default_only);
539         }
541 //printf("Autos::copy 10 %d %d %p\n", default_only, start, autoof(start));
542         if(!default_only)
543         {
544                 for(Auto* current = autoof(start); 
545                         current && current->position <= end; 
546                         current = NEXT)
547                 {
548 // Want to copy single keyframes by putting the cursor on them
549                         if(current->position >= start && current->position <= end)
550                         {
551                                 current->copy(start, end, file, default_only);
552                         }
553                 }
554         }
555 // Copy default auto again to make it the active auto on the clipboard
556         else
557         {
558 // Need to force position to 0 for the case of plugins
559 // and default status to 0.
560                 default_auto->copy(0, 0, file, default_only);
561         }
562 //printf("Autos::copy 20\n");
564         return 0;
567 // Remove 3 consecutive autos with the same value
568 // Remove autos which are out of order
569 void Autos::optimize()
571         int done = 0;
574 // Default auto should always be at 0
575         default_auto->position = 0;
576         while(!done)
577         {
578                 int consecutive = 0;
579                 done = 1;
580                 
581                 
582                 for(Auto *current = first; current; current = NEXT)
583                 {
584 // Get 3rd consecutive auto of equal value
585                         if(current != first)
586                         {
587                                 if(*current == *PREVIOUS)
588                                 {
589                                         consecutive++;
590                                         if(consecutive >= 3)
591                                         {
592                                                 delete PREVIOUS;
593                                                 break;
594                                         }
595                                 }
596                                 else
597                                         consecutive = 0;
598                                 
599                                 if(done && current->position <= PREVIOUS->position)
600                                 {
601                                         delete current;
602                                         break;
603                                 }
604                         }
605                 }
606         }
610 void Autos::remove_nonsequential(Auto *keyframe)
612         if((keyframe->next && keyframe->next->position <= keyframe->position) ||
613                 (keyframe->previous && keyframe->previous->position >= keyframe->position))
614         {
615                 delete keyframe;
616         }
620 void Autos::straighten(int64_t start, int64_t end)
624 void Autos::clear(int64_t start, 
625         int64_t end, 
626         int shift_autos)
628         int64_t length;
629         Auto *next, *current;
630         length = end - start;
633         current = autoof(start);
635 // If a range is selected don't delete the ending keyframe but do delete
636 // the beginning keyframe because shifting end handle forward shouldn't
637 // delete the first keyframe of the next edit.
639         while(current && 
640                 ((end != start && current->position < end) ||
641                 (end == start && current->position <= end)))
642         {
643                 next = NEXT;
644                 remove(current);
645                 current = next;
646         }
648         while(current && shift_autos)
649         {
650                 current->position -= length;
651                 current = NEXT;
652         }
655 int Autos::clear_auto(int64_t position)
657         Auto *current;
658         current = autoof(position);
659         if(current->position == position) remove(current);
663 int Autos::load(FileXML *file)
665         while(last)
666                 remove(last);    // remove any existing autos
668         int result = 0, first_auto = 1;
669         Auto *current;
670         
671         do{
672                 result = file->read_tag();
673                 
674                 if(!result && !file->tag.title_is("/AUTO"))
675                 {
676 // First tag with leading / is taken as end of autos
677                         if(/* strstr(file->tag.get_title(), "AUTOS") && */
679                                 file->tag.get_title()[0] == '/')
680                         {
681                                 result = 1;
682                         }
683                         else
684                         if(!strcmp(file->tag.get_title(), "AUTO"))
685                         {
686                                 if(first_auto)
687                                 {
688                                         default_auto->load(file);
689                                         default_auto->position = 0;
690                                         first_auto = 0;
691                                 }
692                                 else
693                                 {
694                                         current = append(new_auto());
695                                         current->position = file->tag.get_property("POSITION", (int64_t)0);
696                                         current->load(file);
697                                 }
698                         }
699                 }
700         }while(!result);
701         return 0;
709 int Autos::slope_adjustment(int64_t ax, double slope)
711         return (int)(ax * slope);
715 int Autos::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
717         Auto *current;
718         
719         for(current = first; current && scale_autos; current = NEXT)
720         {
721 //              if(current->position >= start && current->position <= end)
722 //              {
723                         current->position = (int64_t)((current->position - start) * rate_scale + start + 0.5);
724 //              }
725         }
726         return 0;
729 Auto* Autos::autoof(int64_t position)
731         Auto *current;
733         for(current = first; 
734                 current && current->position < position; 
735                 current = NEXT)
736         { 
737                 ;
738         }
739         return current;     // return 0 on failure
742 Auto* Autos::nearest_before(int64_t position)
744         Auto *current;
746         for(current = last; current && current->position >= position; current = PREVIOUS)
747         { ; }
750         return current;     // return 0 on failure
753 Auto* Autos::nearest_after(int64_t position)
755         Auto *current;
757         for(current = first; current && current->position <= position; current = NEXT)
758         { ; }
761         return current;     // return 0 on failure
764 int Autos::get_neighbors(int64_t start, int64_t end, Auto **before, Auto **after)
766         if(*before == 0) *before = first;
767         if(*after == 0) *after = last; 
769         while(*before && (*before)->next && (*before)->next->position <= start)
770                 *before = (*before)->next;
771         
772         while(*after && (*after)->previous && (*after)->previous->position >= end)
773                 *after = (*after)->previous;
775         while(*before && (*before)->position > start) *before = (*before)->previous;
776         
777         while(*after && (*after)->position < end) *after = (*after)->next;
778         return 0;
781 int Autos::automation_is_constant(int64_t start, int64_t end)
783         return 0;
786 double Autos::get_automation_constant(int64_t start, int64_t end)
788         return 0;
792 int Autos::init_automation(int64_t &buffer_position,
793                                 int64_t &input_start, 
794                                 int64_t &input_end, 
795                                 int &automate, 
796                                 double &constant, 
797                                 int64_t input_position,
798                                 int64_t buffer_len,
799                                 Auto **before, 
800                                 Auto **after,
801                                 int reverse)
803         buffer_position = 0;
805 // set start and end boundaries for automation info
806         input_start = reverse ? input_position - buffer_len : input_position;
807         input_end = reverse ? input_position : input_position + buffer_len;
809 // test automation for constant value
810 // and set up *before and *after
811         if(automate)
812         {
813                 if(automation_is_constant(input_start, input_end))
814                 {
815                         constant += get_automation_constant(input_start, input_end);
816                         automate = 0;
817                 }
818         }
819         return automate;
823 int Autos::init_slope(Auto **current_auto, 
824                                 double &slope_start, 
825                                 double &slope_value,
826                                 double &slope_position, 
827                                 int64_t &input_start, 
828                                 int64_t &input_end, 
829                                 Auto **before, 
830                                 Auto **after,
831                                 int reverse)
833 // apply automation
834         *current_auto = reverse ? *after : *before;
835 // no auto before start so use first auto in range
836 // already know there is an auto since automation isn't constant
837         if(!*current_auto)
838         {
839                 *current_auto = reverse ? last : first;
840 //              slope_value = (*current_auto)->value;
841                 slope_start = input_start;
842                 slope_position = 0;
843         }
844         else
845         {
846 // otherwise get the first slope point and advance auto
847 //              slope_value = (*current_auto)->value;
848                 slope_start = (*current_auto)->position;
849                 slope_position = reverse ? slope_start - input_end : input_start - slope_start;
850                 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
851         }
852         return 0;
856 int Autos::get_slope(Auto **current_auto, 
857                                 double &slope_start, 
858                                 double &slope_end, 
859                                 double &slope_value,
860                                 double &slope, 
861                                 int64_t buffer_len, 
862                                 int64_t buffer_position,
863                                 int reverse)
865 // get the slope
866         if(*current_auto)
867         {
868                 slope_end = reverse ? slope_start - (*current_auto)->position : (*current_auto)->position - slope_start;
869                 if(slope_end) 
870 //                      slope = ((*current_auto)->value - slope_value) / slope_end;
871 //              else
872                         slope = 0;
873         }
874         else
875         {
876                 slope = 0;
877                 slope_end = buffer_len - buffer_position;
878         }
879         return 0;
882 int Autos::advance_slope(Auto **current_auto, 
883                                 double &slope_start, 
884                                 double &slope_value,
885                                 double &slope_position, 
886                                 int reverse)
888         if(*current_auto) 
889         {
890                 slope_start = (*current_auto)->position;
891 //              slope_value = (*current_auto)->value;
892                 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
893                 slope_position = 0;
894         }
895         return 0;
898 int64_t Autos::get_length()
900         if(last) 
901                 return last->position + 1;
902         else
903                 return 0;
906 void Autos::get_extents(float *min, 
907         float *max,
908         int *coords_undefined,
909         int64_t unit_start,
910         int64_t unit_end)
912         
916 void Autos::dump()