4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
22 #include "automation.inc"
25 #include "edlsession.h"
27 #include "floatauto.h"
28 #include "floatautos.h"
30 #include "localsession.h"
31 #include "transportque.inc"
33 FloatAutos::FloatAutos(EDL *edl,
38 this->default_ = default_;
39 type = AUTOMATION_TYPE_FLOAT;
44 FloatAutos::~FloatAutos()
48 void FloatAutos::set_automation_mode(int64_t start, int64_t end, int mode)
50 FloatAuto *current = (FloatAuto*)first;
53 // Is current auto in range?
54 if(current->position >= start && current->position < end)
56 current->change_curve_mode((FloatAuto::t_mode)mode);
58 current = (FloatAuto*)NEXT;
62 void FloatAutos::draw_joining_line(BC_SubWindow *canvas, int vertical, int center_pixel, int x1, int y1, int x2, int y2)
65 canvas->draw_line(center_pixel - y1, x1, center_pixel - y2, x2);
67 canvas->draw_line(x1, center_pixel + y1, x2, center_pixel + y2);
70 Auto* FloatAutos::new_auto()
72 FloatAuto *result = new FloatAuto(edl, this);
73 result->set_value(default_);
77 int FloatAutos::get_testy(float slope, int cursor_x, int ax, int ay)
79 return (int)(slope * (cursor_x - ax)) + ay;
82 int FloatAutos::automation_is_constant(int64_t start,
87 int total_autos = total();
89 if(direction == PLAY_FORWARD)
100 // No keyframes on track
103 constant = ((FloatAuto*)default_auto)->get_value();
107 // Only one keyframe on track.
110 constant = ((FloatAuto*)first)->get_value();
114 // Last keyframe is before region
115 if(last->position <= start)
117 constant = ((FloatAuto*)last)->get_value();
121 // First keyframe is after region
122 if(first->position > end)
124 constant = ((FloatAuto*)first)->get_value();
129 int64_t prev_position = -1;
130 for(Auto *current = first; current; current = NEXT)
132 int test_current_next = 0;
133 int test_previous_current = 0;
134 FloatAuto *float_current = (FloatAuto*)current;
136 // keyframes before and after region but not in region
137 if(prev_position >= 0 &&
138 prev_position < start &&
139 current->position >= end)
141 // Get value now in case change doesn't occur
142 constant = float_current->get_value();
143 test_previous_current = 1;
145 prev_position = current->position;
147 // Keyframe occurs in the region
148 if(!test_previous_current &&
149 current->position < end &&
150 current->position >= start)
153 // Get value now in case change doesn't occur
154 constant = float_current->get_value();
156 // Keyframe has neighbor
157 if(current->previous)
159 test_previous_current = 1;
164 test_current_next = 1;
168 if(test_current_next)
170 //printf("FloatAutos::automation_is_constant 1 %d\n", start);
171 FloatAuto *float_next = (FloatAuto*)current->next;
173 // Change occurs between keyframes
174 if( !EQUIV(float_current->get_value(), float_next->get_value()) ||
175 !EQUIV(float_current->get_control_out_value(), 0) ||
176 !EQUIV(float_next->get_control_in_value(), 0))
182 if(test_previous_current)
184 FloatAuto *float_previous = (FloatAuto*)current->previous;
186 // Change occurs between keyframes
187 if(!EQUIV(float_current->get_value(), float_previous->get_value()) ||
188 !EQUIV(float_current->get_control_in_value(), 0) ||
189 !EQUIV(float_previous->get_control_out_value(), 0))
191 // printf("FloatAutos::automation_is_constant %d %d %d %f %f %f %f\n",
193 // float_previous->position,
194 // float_current->position,
195 // float_previous->get_value(),
196 // float_current->get_value(),
197 // float_previous->get_control_out_value(),
198 // float_current->get_control_in_value());
204 // Got nothing that changes in the region.
208 double FloatAutos::get_automation_constant(int64_t start, int64_t end)
210 Auto *current_auto, *before = 0, *after = 0;
212 // quickly get autos just outside range
213 get_neighbors(start, end, &before, &after);
215 // no auto before range so use first
217 current_auto = before;
219 current_auto = first;
221 // no autos at all so use default value
222 if(!current_auto) current_auto = default_auto;
224 return ((FloatAuto*)current_auto)->get_value();
228 float FloatAutos::get_value(int64_t position,
230 FloatAuto* &previous,
233 // Calculate bezier equation at position
234 previous = (FloatAuto*)get_prev_auto(position, direction, (Auto* &)previous, 0);
235 next = (FloatAuto*)get_next_auto(position, direction, (Auto* &)next, 0);
238 if(!next && !previous) return ((FloatAuto*)default_auto)->get_value();
239 if(!previous) return next->get_value();
240 if(!next) return previous->get_value();
241 if(next == previous) return previous->get_value();
243 if(direction == PLAY_FORWARD)
245 if(EQUIV(previous->get_value(), next->get_value())) {
246 if( (previous->curve_mode == FloatAuto::LINEAR &&
247 next->curve_mode == FloatAuto::LINEAR) ||
248 (EQUIV(previous->get_control_out_value(), 0) &&
249 EQUIV(next->get_control_in_value(), 0))) {
250 return previous->get_value();
254 else if(direction == PLAY_REVERSE) {
255 if(EQUIV(previous->get_value(), next->get_value())) {
256 if( (previous->curve_mode == FloatAuto::LINEAR &&
257 next->curve_mode == FloatAuto::LINEAR) ||
258 (EQUIV(previous->get_control_in_value(), 0) &&
259 EQUIV(next->get_control_out_value(), 0))) {
260 return previous->get_value();
264 // at this point: previous and next not NULL, positions differ, value not constant.
266 return calculate_bezier(previous, next, position);
270 float FloatAutos::calculate_bezier(FloatAuto *previous, FloatAuto *next, int64_t position)
272 if(next->position - previous->position == 0) return previous->get_value();
274 float y0 = previous->get_value();
275 float y3 = next->get_value();
278 float y1 = previous->get_value() + previous->get_control_out_value();
279 float y2 = next->get_value() + next->get_control_in_value();
280 float t = (float)(position - previous->position) /
281 (next->position - previous->position);
284 float tpow3 = t * t * t;
286 float invtpow2 = invt * invt;
287 float invtpow3 = invt * invt * invt;
289 float result = ( invtpow3 * y0
290 + 3 * t * invtpow2 * y1
291 + 3 * tpow2 * invt * y2
293 //printf("FloatAutos::get_value(t=%5.3f)->%6.2f (prev,pos,next)=(%d,%d,%d)\n", t, result, previous->position, position, next->position);
299 float FloatAutos::calculate_bezier_derivation(FloatAuto *previous, FloatAuto *next, int64_t position)
300 // calculate the slope of the interpolating bezier function at given position.
301 // computed slope is based on the actual position scale (in frames or samples)
303 float scale = next->position - previous->position;
305 if( !previous->get_control_out_position() )
307 return previous->get_control_out_value() / previous->get_control_out_position();
309 float y0 = previous->get_value();
310 float y3 = next->get_value();
313 float y1 = previous->get_value() + previous->get_control_out_value();
314 float y2 = next->get_value() + next->get_control_in_value();
316 float t = (float)(position - previous->position) / scale;
320 float invtpow2 = invt * invt;
324 - invt * ( 2*t - invt ) * y1
325 + t * ( 2*invt - t ) * y2
329 return slope / scale;
334 void FloatAutos::get_extents(float *min,
336 int *coords_undefined,
342 printf("FloatAutos::get_extents edl == NULL\n");
348 printf("FloatAutos::get_extents track == NULL\n");
355 FloatAuto *current = (FloatAuto*)default_auto;
356 if(*coords_undefined)
358 *min = *max = current->get_value();
359 *coords_undefined = 0;
362 *min = MIN(current->get_value(), *min);
363 *max = MAX(current->get_value(), *max);
367 for(FloatAuto *current = (FloatAuto*)first; current; current = (FloatAuto*)NEXT)
369 if(current->position >= unit_start && current->position < unit_end)
371 if(*coords_undefined)
373 *min = *max = current->get_value();
374 *coords_undefined = 0;
377 *min = MIN(current->get_value(), *min);
378 *min = MIN(current->get_value() + current->get_control_in_value(), *min);
379 *min = MIN(current->get_value() + current->get_control_out_value(), *min);
381 *max = MAX(current->get_value(), *max);
382 *max = MAX(current->get_value() + current->get_control_in_value(), *max);
383 *max = MAX(current->get_value() + current->get_control_out_value(), *max);
387 // Test joining regions
390 int64_t unit_step = edl->local_session->zoom_sample;
391 if(track->data_type == TRACK_VIDEO)
392 unit_step = (int64_t)(unit_step *
393 edl->session->frame_rate /
394 edl->session->sample_rate);
395 unit_step = MAX(unit_step, 1);
396 for(int64_t position = unit_start;
398 position += unit_step)
400 float value = get_value(position,PLAY_FORWARD,prev,next);
401 if(*coords_undefined)
404 *coords_undefined = 0;
408 *min = MIN(value, *min);
409 *max = MAX(value, *max);
414 void FloatAutos::set_proxy(int orig_scale, int new_scale)
417 orig_value = ((FloatAuto*)default_auto)->value * orig_scale;
418 ((FloatAuto*)default_auto)->value = orig_value / new_scale;
420 for( FloatAuto *current= (FloatAuto*)first; current; current=(FloatAuto*)NEXT ) {
421 orig_value = current->value * orig_scale;
422 current->value = orig_value / new_scale;
423 orig_value = current->control_in_value * orig_scale;
424 current->control_in_value = orig_value / new_scale;
425 orig_value = current->control_out_value * orig_scale;
426 current->control_out_value = orig_value / new_scale;
430 void FloatAutos::dump()
432 printf(" FloatAutos::dump %p\n", this);
433 printf(" Default: position %jd value=%f\n",
434 default_auto->position, ((FloatAuto*)default_auto)->get_value());
435 for(Auto* current = first; current; current = NEXT)
437 printf(" position %jd value=%7.3f invalue=%7.3f outvalue=%7.3f %s\n",
439 ((FloatAuto*)current)->get_value(),
440 ((FloatAuto*)current)->get_control_in_value(),
441 ((FloatAuto*)current)->get_control_out_value(),
442 FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
446 double FloatAutos::automation_integral(int64_t start, int64_t length, int direction)
448 if( direction == PLAY_REVERSE )
455 int64_t pos = start, len = length, end = pos + len;
457 int64_t prev_pos = 0, next_pos = end;
458 Auto *zprev = 0, *znext = 0;
459 FloatAuto *prev = (FloatAuto*)get_prev_auto(pos, direction, zprev, 0);
460 if( prev ) prev_pos = prev->position;
461 FloatAuto *next = (FloatAuto*)get_next_auto(pos, direction, znext, 0);
462 if( next ) next_pos = next->position;
463 if( !prev && !next ) prev = next = (FloatAuto*)default_auto;
464 else if( !prev ) prev = next;
465 else if( !next ) next = prev;
467 double dt = next_pos - prev_pos;
468 double t0 = (pos - prev_pos) / dt;
469 if( (pos = next_pos) > end ) pos = end;
470 double t1 = (pos - prev_pos) / dt;
472 double y0 = prev->get_value(), y1 = y0 + prev->get_control_out_value();
473 double y3 = next->get_value(), y2 = y3 + next->get_control_in_value();
474 if( y0 != y1 || y1 != y2 || y2 != y3 ) {
475 // bezier definite integral t0..t1
476 double f4 = -y0/4 + 3*y1/4 - 3*y2/4 + y3/4;
477 double f3 = y0 - 2*y1 + y2;
478 double f2 = -3*y0/2 + 3*y1/2;
479 double f1 = y0, t = t0;
480 double t2 = t*t, t3 = t2*t, t4 = t3*t;
481 t0 = t4*f4 + t3*f3 + t2*f2 + t*f1;
482 t = t1; t2 = t*t; t3 = t2*t; t4 = t3*t;
483 t1 = t4*f4 + t3*f3 + t2*f2 + t*f1;
488 value += dt * (t1 - t0);
494 int64_t FloatAutos::speed_position(double pos)
496 double length = track->get_length();
497 int64_t l = -1, r = track->to_units(length, 1);
499 for( int i=32; --i >= 0 && automation_integral(0,r,PLAY_FORWARD) <= pos; r*=2 );
500 for( int i=64; --i >= 0 && (r-l)>1; ) {
501 int64_t m = (l + r) / 2;
502 double t = automation_integral(0,m,PLAY_FORWARD) - pos;
503 *(t >= 0 ? &r : &l) = m;