4 #include "edlsession.h"
5 #include "localsession.h"
8 #include "transportque.inc"
13 Autos::Autos(EDL *edl, Track *track)
27 while(last) delete last;
31 void Autos::create_objects()
34 default_auto = new_auto();
35 default_auto->is_default = 1;
43 Auto* Autos::append_auto()
45 return append(new_auto());
49 Auto* Autos::new_auto()
51 return new Auto(edl, this);
54 void Autos::resample(double old_rate, double new_rate)
56 for(Auto *current = first; current; current = NEXT)
58 current->position = (int64_t)((double)current->position *
65 void Autos::equivalent_output(Autos *autos, int64_t startproject, int64_t *result)
67 // Default keyframe differs
68 if(!total() && !(*default_auto == *autos->default_auto))
70 if(*result < 0 || *result > startproject) *result = startproject;
73 // Search for difference
75 for(Auto *current = first, *that_current = autos->first;
76 current || that_current;
78 that_current = that_current->next)
81 if(current && !that_current)
83 int64_t position1 = (autos->last ? autos->last->position : startproject);
84 int64_t position2 = current->position;
85 if(*result < 0 || *result > MIN(position1, position2))
86 *result = MIN(position1, position2);
90 if(!current && that_current)
92 int64_t position1 = (last ? last->position : startproject);
93 int64_t position2 = that_current->position;
94 if(*result < 0 || *result > MIN(position1, position2))
95 *result = MIN(position1, position2);
100 if(!(*current == *that_current) ||
101 current->position != that_current->position)
103 int64_t position1 = (current->previous ?
104 current->previous->position :
106 int64_t position2 = (that_current->previous ?
107 that_current->previous->position :
109 if(*result < 0 || *result > MIN(position1, position2))
110 *result = MIN(position1, position2);
117 void Autos::copy_from(Autos *autos)
119 Auto *current = autos->first, *this_current = first;
121 default_auto->copy_from(autos->default_auto);
123 // Detect common memory leak bug
124 if(autos->first && !autos->last)
126 printf("Autos::copy_from inconsistent pointers\n");
130 for(current = autos->first; current; current = NEXT)
132 //printf("Autos::copy_from 1 %p\n", current);
136 append(this_current = new_auto());
138 this_current->copy_from(current);
139 this_current = this_current->next;
142 for( ; this_current; )
144 Auto *next_current = this_current->next;
146 this_current = next_current;
151 // We don't replace it in pasting but
152 // when inserting the first EDL of a load operation we need to replace
153 // the default keyframe.
154 void Autos::insert_track(Autos *automation,
156 int64_t length_units,
160 insert(start_unit, start_unit + length_units);
162 if(replace_default) default_auto->copy_from(automation->default_auto);
163 for(Auto *current = automation->first; current; current = NEXT)
165 Auto *new_auto = insert_auto(start_unit + current->position);
166 new_auto->copy_from(current);
167 // Override copy_from
168 new_auto->position = current->position + start_unit;
172 Auto* Autos::get_prev_auto(int64_t position,
177 // Get on or before position
178 if(direction == PLAY_FORWARD)
180 // Try existing result
183 while(current && current->position < position) current = NEXT;
184 while(current && current->position > position) current = PREVIOUS;
190 current && current->position > position;
191 current = PREVIOUS) ;
193 if(!current && use_default) current = (first ? first : default_auto);
196 // Get on or after position
197 if(direction == PLAY_REVERSE)
201 while(current && current->position > position) current = PREVIOUS;
202 while(current && current->position < position) current = NEXT;
208 current && current->position < position;
212 if(!current && use_default) current = (last ? last : default_auto);
218 Auto* Autos::get_prev_auto(int direction, Auto* ¤t)
220 double position_double = edl->local_session->get_selectionstart(1);
221 position_double = edl->align_to_frame(position_double, 0);
222 int64_t position = track->to_units(position_double, 0);
224 return get_prev_auto(position, direction, current);
229 int Autos::auto_exists_for_editing(double position)
233 if(edl->session->auto_keyframes)
235 double unit_position = position;
236 unit_position = edl->align_to_frame(unit_position, 0);
237 if (get_auto_at_position(unit_position))
248 Auto* Autos::get_auto_at_position(double position)
250 int64_t unit_position = track->to_units(position, 0);
252 for(Auto *current = first;
256 if(edl->equivalent(current->position, unit_position))
265 Auto* Autos::get_auto_for_editing(double position)
269 position = edl->local_session->get_selectionstart(1);
273 position = edl->align_to_frame(position, 0);
278 //printf("Autos::get_auto_for_editing %p %p\n", first, default_auto);
280 if(edl->session->auto_keyframes)
282 result = insert_auto_for_editing(track->to_units(position, 0));
285 result = get_prev_auto(track->to_units(position, 0),
289 //printf("Autos::get_auto_for_editing %p %p %p\n", default_auto, first, result);
294 Auto* Autos::get_next_auto(int64_t position, int direction, Auto* ¤t, int use_default)
296 if(direction == PLAY_FORWARD)
300 while(current && current->position > position) current = PREVIOUS;
301 while(current && current->position < position) current = NEXT;
307 current && current->position <= position;
312 if(!current && use_default) current = (last ? last : default_auto);
315 if(direction == PLAY_REVERSE)
319 while(current && current->position < position) current = NEXT;
320 while(current && current->position > position) current = PREVIOUS;
326 current && current->position > position;
331 if(!current && use_default) current = (first ? first : default_auto);
337 Auto* Autos::insert_auto(int64_t position)
339 Auto *current, *result;
341 // Test for existence
343 current && !edl->equivalent(current->position, position);
352 // Get first one on or before as a template
354 current && current->position > position;
362 insert_after(current, result = new_auto());
363 result->copy_from(current);
368 if(!current) current = default_auto;
370 insert_before(first, result = new_auto());
371 if(current) result->copy_from(current);
374 result->position = position;
384 Auto* Autos::insert_auto_for_editing(int64_t position)
386 Auto *current, *result;
388 // Test for existence
390 current && !edl->equivalent(current->position, position);
396 //printf("Autos::insert_auto_for_editing %p\n", current);
400 // Get first one on or before as a template
402 current && current->position > position;
411 insert_after(current, result = new_auto());
412 result->interpolate_from(current, next, position);
417 if(!current) current = default_auto;
419 insert_before(first, result = new_auto());
420 if(current) result->copy_from(current);
423 result->position = position;
433 int Autos::clear_all()
435 Auto *current_, *current;
437 for(current = first; current; current = current_)
446 int Autos::insert(int64_t start, int64_t end)
449 Auto *current = first;
451 for( ; current && current->position < start; current = NEXT)
454 length = end - start;
456 for(; current; current = NEXT)
458 current->position += length;
463 void Autos::paste(int64_t start,
472 //printf("Autos::paste %ld\n", start);
474 result = file->read_tag();
476 if(!result && !file->tag.title_is("/AUTO"))
479 if(/* strstr(file->tag.get_title(), "AUTOS") && */
480 file->tag.get_title()[0] == '/')
485 if(!strcmp(file->tag.get_title(), "AUTO"))
489 // Paste first active auto into default
494 current = default_auto;
498 // Paste default auto into default
500 current = default_auto;
503 int64_t position = Units::to_int64(
504 (double)file->tag.get_property("POSITION", 0) *
507 // Paste active auto into track
508 current = insert_auto(position);
523 int Autos::paste_silence(int64_t start, int64_t end)
529 int Autos::copy(int64_t start,
535 // First auto is always loaded into default even if it is discarded in a paste
537 //printf("Autos::copy 1 %d %d %p\n", default_only, start, autoof(start));
540 default_auto->copy(0, 0, file, default_only);
543 //printf("Autos::copy 10 %d %d %p\n", default_only, start, autoof(start));
546 for(Auto* current = autoof(start);
547 current && current->position <= end;
550 // Want to copy single keyframes by putting the cursor on them
551 if(current->position >= start && current->position <= end)
553 current->copy(start, end, file, default_only);
557 // Copy default auto again to make it the active auto on the clipboard
560 // Need to force position to 0 for the case of plugins
561 // and default status to 0.
562 default_auto->copy(0, 0, file, default_only);
564 //printf("Autos::copy 20\n");
569 // Remove 3 consecutive autos with the same value
570 // Remove autos which are out of order
571 void Autos::optimize()
576 // Default auto should always be at 0
577 default_auto->position = 0;
584 for(Auto *current = first; current; current = NEXT)
586 // Get 3rd consecutive auto of equal value
589 if(*current == *PREVIOUS)
601 if(done && current->position <= PREVIOUS->position)
612 void Autos::remove_nonsequential(Auto *keyframe)
614 if((keyframe->next && keyframe->next->position <= keyframe->position) ||
615 (keyframe->previous && keyframe->previous->position >= keyframe->position))
622 void Autos::straighten(int64_t start, int64_t end)
626 void Autos::clear(int64_t start,
631 Auto *next, *current;
632 length = end - start;
635 current = autoof(start);
637 // If a range is selected don't delete the ending keyframe but do delete
638 // the beginning keyframe because shifting end handle forward shouldn't
639 // delete the first keyframe of the next edit.
642 ((end != start && current->position < end) ||
643 (end == start && current->position <= end)))
650 while(current && shift_autos)
652 current->position -= length;
657 int Autos::clear_auto(int64_t position)
660 current = autoof(position);
661 if(current->position == position) remove(current);
665 int Autos::load(FileXML *file)
668 remove(last); // remove any existing autos
670 int result = 0, first_auto = 1;
674 result = file->read_tag();
676 if(!result && !file->tag.title_is("/AUTO"))
678 // First tag with leading / is taken as end of autos
679 if(/* strstr(file->tag.get_title(), "AUTOS") && */
681 file->tag.get_title()[0] == '/')
686 if(!strcmp(file->tag.get_title(), "AUTO"))
690 default_auto->load(file);
691 default_auto->position = 0;
696 current = append(new_auto());
697 current->position = file->tag.get_property("POSITION", (int64_t)0);
711 int Autos::slope_adjustment(int64_t ax, double slope)
713 return (int)(ax * slope);
717 int Autos::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
721 for(current = first; current && scale_autos; current = NEXT)
723 // if(current->position >= start && current->position <= end)
725 current->position = (int64_t)((current->position - start) * rate_scale + start + 0.5);
731 Auto* Autos::autoof(int64_t position)
736 current && current->position < position;
741 return current; // return 0 on failure
744 Auto* Autos::nearest_before(int64_t position)
748 for(current = last; current && current->position >= position; current = PREVIOUS)
752 return current; // return 0 on failure
755 Auto* Autos::nearest_after(int64_t position)
759 for(current = first; current && current->position <= position; current = NEXT)
763 return current; // return 0 on failure
766 int Autos::get_neighbors(int64_t start, int64_t end, Auto **before, Auto **after)
768 if(*before == 0) *before = first;
769 if(*after == 0) *after = last;
771 while(*before && (*before)->next && (*before)->next->position <= start)
772 *before = (*before)->next;
774 while(*after && (*after)->previous && (*after)->previous->position >= end)
775 *after = (*after)->previous;
777 while(*before && (*before)->position > start) *before = (*before)->previous;
779 while(*after && (*after)->position < end) *after = (*after)->next;
783 int Autos::automation_is_constant(int64_t start, int64_t end)
788 double Autos::get_automation_constant(int64_t start, int64_t end)
794 int Autos::init_automation(int64_t &buffer_position,
795 int64_t &input_start,
799 int64_t input_position,
807 // set start and end boundaries for automation info
808 input_start = reverse ? input_position - buffer_len : input_position;
809 input_end = reverse ? input_position : input_position + buffer_len;
811 // test automation for constant value
812 // and set up *before and *after
815 if(automation_is_constant(input_start, input_end))
817 constant += get_automation_constant(input_start, input_end);
825 int Autos::init_slope(Auto **current_auto,
828 double &slope_position,
829 int64_t &input_start,
836 *current_auto = reverse ? *after : *before;
837 // no auto before start so use first auto in range
838 // already know there is an auto since automation isn't constant
841 *current_auto = reverse ? last : first;
842 // slope_value = (*current_auto)->value;
843 slope_start = input_start;
848 // otherwise get the first slope point and advance auto
849 // slope_value = (*current_auto)->value;
850 slope_start = (*current_auto)->position;
851 slope_position = reverse ? slope_start - input_end : input_start - slope_start;
852 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
858 int Autos::get_slope(Auto **current_auto,
864 int64_t buffer_position,
870 slope_end = reverse ? slope_start - (*current_auto)->position : (*current_auto)->position - slope_start;
872 // slope = ((*current_auto)->value - slope_value) / slope_end;
879 slope_end = buffer_len - buffer_position;
884 int Autos::advance_slope(Auto **current_auto,
887 double &slope_position,
892 slope_start = (*current_auto)->position;
893 // slope_value = (*current_auto)->value;
894 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
900 int64_t Autos::get_length()
903 return last->position + 1;
908 void Autos::get_extents(float *min,
910 int *coords_undefined,
934 // c-file-style: "linux"