4 #include "edlsession.h"
5 #include "localsession.h"
8 #include "transportque.inc"
13 Autos::Autos(EDL *edl, Track *track)
25 while(last) delete last;
29 void Autos::create_objects()
32 default_auto = new_auto();
33 default_auto->is_default = 1;
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)
56 current->position = (int64_t)((double)current->position *
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))
68 if(*result < 0 || *result > startproject) *result = startproject;
71 // Search for difference
73 for(Auto *current = first, *that_current = autos->first;
74 current || that_current;
76 that_current = that_current->next)
79 if(current && !that_current)
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);
88 if(!current && that_current)
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);
98 if(!(*current == *that_current) ||
99 current->position != that_current->position)
101 int64_t position1 = (current->previous ?
102 current->previous->position :
104 int64_t position2 = (that_current->previous ?
105 that_current->previous->position :
107 if(*result < 0 || *result > MIN(position1, position2))
108 *result = MIN(position1, position2);
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)
124 printf("Autos::copy_from inconsistent pointers\n");
128 for(current = autos->first; current; current = NEXT)
130 //printf("Autos::copy_from 1 %p\n", current);
134 append(this_current = new_auto());
136 this_current->copy_from(current);
137 this_current = this_current->next;
140 for( ; this_current; )
142 Auto *next_current = this_current->next;
144 this_current = next_current;
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,
154 int64_t length_units,
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)
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;
170 Auto* Autos::get_prev_auto(int64_t position,
175 // Get on or before position
176 if(direction == PLAY_FORWARD)
178 // Try existing result
181 while(current && current->position < position) current = NEXT;
182 while(current && current->position > position) current = PREVIOUS;
188 current && current->position > position;
189 current = PREVIOUS) ;
191 if(!current && use_default) current = (first ? first : default_auto);
194 // Get on or after position
195 if(direction == PLAY_REVERSE)
199 while(current && current->position > position) current = PREVIOUS;
200 while(current && current->position < position) current = NEXT;
206 current && current->position < position;
210 if(!current && use_default) current = (last ? last : default_auto);
216 Auto* Autos::get_prev_auto(int direction, Auto* ¤t)
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);
227 int Autos::auto_exists_for_editing(double position)
231 if(edl->session->auto_keyframes)
233 double unit_position = position;
234 unit_position = edl->align_to_frame(unit_position, 0);
235 if (get_auto_at_position(unit_position))
246 Auto* Autos::get_auto_at_position(double position)
248 int64_t unit_position = track->to_units(position, 0);
250 for(Auto *current = first;
254 if(edl->equivalent(current->position, unit_position))
263 Auto* Autos::get_auto_for_editing(double position)
267 position = edl->local_session->get_selectionstart(1);
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)
280 result = insert_auto_for_editing(track->to_units(position, 0));
283 result = get_prev_auto(track->to_units(position, 0),
287 //printf("Autos::get_auto_for_editing %p %p %p\n", default_auto, first, result);
292 Auto* Autos::get_next_auto(int64_t position, int direction, Auto* ¤t, int use_default)
294 if(direction == PLAY_FORWARD)
298 while(current && current->position > position) current = PREVIOUS;
299 while(current && current->position < position) current = NEXT;
305 current && current->position <= position;
310 if(!current && use_default) current = (last ? last : default_auto);
313 if(direction == PLAY_REVERSE)
317 while(current && current->position < position) current = NEXT;
318 while(current && current->position > position) current = PREVIOUS;
324 current && current->position > position;
329 if(!current && use_default) current = (first ? first : default_auto);
335 Auto* Autos::insert_auto(int64_t position)
337 Auto *current, *result;
339 // Test for existence
341 current && !edl->equivalent(current->position, position);
347 //printf("Autos::insert_auto %p\n", current);
351 // Get first one on or before as a template
353 current && current->position > position;
361 insert_after(current, result = new_auto());
362 result->copy_from(current);
367 if(!current) current = default_auto;
369 insert_before(first, result = new_auto());
370 if(current) result->copy_from(current);
373 result->position = position;
383 Auto* Autos::insert_auto_for_editing(int64_t position)
385 Auto *current, *result;
387 // Test for existence
389 current && !edl->equivalent(current->position, position);
395 //printf("Autos::insert_auto_for_editing %p\n", current);
399 // Get first one on or before as a template
401 current && current->position > position;
410 insert_after(current, result = new_auto());
411 result->interpolate_from(current, next, position);
416 if(!current) current = default_auto;
418 insert_before(first, result = new_auto());
419 if(current) result->copy_from(current);
422 result->position = position;
432 int Autos::clear_all()
434 Auto *current_, *current;
436 for(current = first; current; current = current_)
445 int Autos::insert(int64_t start, int64_t end)
448 Auto *current = first;
450 for( ; current && current->position < start; current = NEXT)
453 length = end - start;
455 for(; current; current = NEXT)
457 current->position += length;
462 void Autos::paste(int64_t start,
471 //printf("Autos::paste %ld\n", start);
473 result = file->read_tag();
475 if(!result && !file->tag.title_is("/AUTO"))
478 if(/* strstr(file->tag.get_title(), "AUTOS") && */
479 file->tag.get_title()[0] == '/')
484 if(!strcmp(file->tag.get_title(), "AUTO"))
488 // Paste first active auto into default
493 current = default_auto;
497 // Paste default auto into default
499 current = default_auto;
502 int64_t position = Units::to_int64(
503 (double)file->tag.get_property("POSITION", 0) *
506 // Paste active auto into track
507 current = insert_auto(position);
522 int Autos::paste_silence(int64_t start, int64_t end)
528 int Autos::copy(int64_t start,
534 // First auto is always loaded into default even if it is discarded in a paste
536 //printf("Autos::copy 1 %d %d %p\n", default_only, start, autoof(start));
539 default_auto->copy(0, 0, file, default_only);
542 //printf("Autos::copy 10 %d %d %p\n", default_only, start, autoof(start));
545 for(Auto* current = autoof(start);
546 current && current->position <= end;
549 // Want to copy single keyframes by putting the cursor on them
550 if(current->position >= start && current->position <= end)
552 current->copy(start, end, file, default_only);
556 // Copy default auto again to make it the active auto on the clipboard
559 // Need to force position to 0 for the case of plugins
560 // and default status to 0.
561 default_auto->copy(0, 0, file, default_only);
563 //printf("Autos::copy 20\n");
568 // Remove 3 consecutive autos with the same value
569 // Remove autos which are out of order
570 void Autos::optimize()
575 // Default auto should always be at 0
576 default_auto->position = 0;
583 for(Auto *current = first; current; current = NEXT)
585 // Get 3rd consecutive auto of equal value
588 if(*current == *PREVIOUS)
600 if(done && current->position <= PREVIOUS->position)
611 void Autos::remove_nonsequential(Auto *keyframe)
613 if((keyframe->next && keyframe->next->position <= keyframe->position) ||
614 (keyframe->previous && keyframe->previous->position >= keyframe->position))
623 void Autos::clear(int64_t start,
628 Auto *next, *current;
629 length = end - start;
632 current = autoof(start);
634 // If a range is selected don't delete the ending keyframe but do delete
635 // the beginning keyframe because shifting end handle forward shouldn't
636 // delete the first keyframe of the next edit.
639 ((end != start && current->position < end) ||
640 (end == start && current->position <= end)))
647 while(current && shift_autos)
649 current->position -= length;
654 int Autos::clear_auto(int64_t position)
657 current = autoof(position);
658 if(current->position == position) remove(current);
662 int Autos::load(FileXML *file)
665 remove(last); // remove any existing autos
667 int result = 0, first_auto = 1;
671 result = file->read_tag();
673 if(!result && !file->tag.title_is("/AUTO"))
675 // First tag with leading / is taken as end of autos
676 if(/* strstr(file->tag.get_title(), "AUTOS") && */
678 file->tag.get_title()[0] == '/')
683 if(!strcmp(file->tag.get_title(), "AUTO"))
687 default_auto->load(file);
688 default_auto->position = 0;
693 current = append(new_auto());
694 current->position = file->tag.get_property("POSITION", (int64_t)0);
708 int Autos::slope_adjustment(int64_t ax, double slope)
710 return (int)(ax * slope);
714 int Autos::scale_time(float rate_scale, int scale_edits, int scale_autos, int64_t start, int64_t end)
718 for(current = first; current && scale_autos; current = NEXT)
720 // if(current->position >= start && current->position <= end)
722 current->position = (int64_t)((current->position - start) * rate_scale + start + 0.5);
728 Auto* Autos::autoof(int64_t position)
733 current && current->position < position;
738 return current; // return 0 on failure
741 Auto* Autos::nearest_before(int64_t position)
745 for(current = last; current && current->position >= position; current = PREVIOUS)
749 return current; // return 0 on failure
752 Auto* Autos::nearest_after(int64_t position)
756 for(current = first; current && current->position <= position; current = NEXT)
760 return current; // return 0 on failure
763 int Autos::get_neighbors(int64_t start, int64_t end, Auto **before, Auto **after)
765 if(*before == 0) *before = first;
766 if(*after == 0) *after = last;
768 while(*before && (*before)->next && (*before)->next->position <= start)
769 *before = (*before)->next;
771 while(*after && (*after)->previous && (*after)->previous->position >= end)
772 *after = (*after)->previous;
774 while(*before && (*before)->position > start) *before = (*before)->previous;
776 while(*after && (*after)->position < end) *after = (*after)->next;
780 int Autos::automation_is_constant(int64_t start, int64_t end)
785 double Autos::get_automation_constant(int64_t start, int64_t end)
791 int Autos::init_automation(int64_t &buffer_position,
792 int64_t &input_start,
796 int64_t input_position,
804 // set start and end boundaries for automation info
805 input_start = reverse ? input_position - buffer_len : input_position;
806 input_end = reverse ? input_position : input_position + buffer_len;
808 // test automation for constant value
809 // and set up *before and *after
812 if(automation_is_constant(input_start, input_end))
814 constant += get_automation_constant(input_start, input_end);
822 int Autos::init_slope(Auto **current_auto,
825 double &slope_position,
826 int64_t &input_start,
833 *current_auto = reverse ? *after : *before;
834 // no auto before start so use first auto in range
835 // already know there is an auto since automation isn't constant
838 *current_auto = reverse ? last : first;
839 // slope_value = (*current_auto)->value;
840 slope_start = input_start;
845 // otherwise get the first slope point and advance auto
846 // slope_value = (*current_auto)->value;
847 slope_start = (*current_auto)->position;
848 slope_position = reverse ? slope_start - input_end : input_start - slope_start;
849 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
855 int Autos::get_slope(Auto **current_auto,
861 int64_t buffer_position,
867 slope_end = reverse ? slope_start - (*current_auto)->position : (*current_auto)->position - slope_start;
869 // slope = ((*current_auto)->value - slope_value) / slope_end;
876 slope_end = buffer_len - buffer_position;
881 int Autos::advance_slope(Auto **current_auto,
884 double &slope_position,
889 slope_start = (*current_auto)->position;
890 // slope_value = (*current_auto)->value;
891 (*current_auto) = reverse ? (*current_auto)->previous : (*current_auto)->next;
897 int64_t Autos::get_length()
900 return last->position + 1;
905 void Autos::get_extents(float *min,
907 int *coords_undefined,