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 )
239 return ((FloatAuto*)default_auto)->get_value();
240 if( next == previous )
241 return previous->get_value();
243 if( direction == PLAY_FORWARD) {
244 if( !previous ) return next->get_value(1);
245 if( !next ) return previous->get_value(0);
246 if( EQUIV(previous->get_value(0), next->get_value(1)) ) {
247 if( (previous->curve_mode == FloatAuto::LINEAR &&
248 next->curve_mode == FloatAuto::LINEAR) ||
249 (EQUIV(previous->get_control_out_value(), 0) &&
250 EQUIV(next->get_control_in_value(), 0)) ) {
251 return previous->get_value(0);
255 else if(direction == PLAY_REVERSE) {
256 if( !previous ) return next->get_value(0);
257 if( !next ) return previous->get_value(1);
258 if( EQUIV(previous->get_value(0), next->get_value(1)) ) {
259 if( (previous->curve_mode == FloatAuto::LINEAR &&
260 next->curve_mode == FloatAuto::LINEAR) ||
261 (EQUIV(previous->get_control_in_value(), 0) &&
262 EQUIV(next->get_control_out_value(), 0)) ) {
263 return previous->get_value(1);
267 // at this point: previous and next not NULL, positions differ, value not constant.
269 return calculate_bezier(previous, next, position, direction);
273 float FloatAutos::calculate_bezier(FloatAuto *previous, FloatAuto *next,
274 int64_t position, int direction)
276 int edge = direction == PLAY_FORWARD ? 0 : 1;
277 if( next->position == previous->position )
278 return previous->get_value(edge);
280 float y0 = previous->get_value(edge);
281 float y3 = next->get_value(1-edge);
284 float y1 = y0 + (direction == PLAY_FORWARD ?
285 previous->get_control_out_value() :
286 previous->get_control_in_value());
287 float y2 = y3 + (direction == PLAY_FORWARD ?
288 next->get_control_in_value() :
289 next->get_control_out_value());
290 float t = (float)(position - previous->position) /
291 (next->position - previous->position);
294 float tpow3 = t * t * t;
296 float invtpow2 = invt * invt;
297 float invtpow3 = invt * invt * invt;
299 float result = ( invtpow3 * y0
300 + 3 * t * invtpow2 * y1
301 + 3 * tpow2 * invt * y2
303 //printf("FloatAutos::get_value(t=%5.3f)->%6.2f (prev,pos,next)=(%d,%d,%d)\n", t, result, previous->position, position, next->position);
309 float FloatAutos::calculate_bezier_derivation(FloatAuto *previous, FloatAuto *next, int64_t position)
310 // calculate the slope of the interpolating bezier function at given position.
311 // computed slope is based on the actual position scale (in frames or samples)
313 float scale = next->position - previous->position;
315 if( !previous->get_control_out_position() )
317 return previous->get_control_out_value() / previous->get_control_out_position();
319 float y0 = previous->get_value(0);
320 float y3 = next->get_value(1);
323 float y1 = y0 + previous->get_control_out_value();
324 float y2 = y3 + next->get_control_in_value();
326 float t = (float)(position - previous->position) / scale;
330 float invtpow2 = invt * invt;
334 - invt * ( 2*t - invt ) * y1
335 + t * ( 2*invt - t ) * y2
339 return slope / scale;
344 void FloatAutos::get_extents(float *min,
346 int *coords_undefined,
352 printf("FloatAutos::get_extents edl == NULL\n");
358 printf("FloatAutos::get_extents track == NULL\n");
365 FloatAuto *current = (FloatAuto*)default_auto;
366 if(*coords_undefined)
368 *min = *max = current->get_value();
369 *coords_undefined = 0;
372 *min = MIN(current->get_value(), *min);
373 *max = MAX(current->get_value(), *max);
377 for(FloatAuto *current = (FloatAuto*)first; current; current = (FloatAuto*)NEXT)
379 if(current->position >= unit_start && current->position < unit_end)
381 if(*coords_undefined)
383 *min = *max = current->get_value();
384 *coords_undefined = 0;
387 *min = MIN(current->get_value(), *min);
388 *min = MIN(current->get_value() + current->get_control_in_value(), *min);
389 *min = MIN(current->get_value() + current->get_control_out_value(), *min);
391 *max = MAX(current->get_value(), *max);
392 *max = MAX(current->get_value() + current->get_control_in_value(), *max);
393 *max = MAX(current->get_value() + current->get_control_out_value(), *max);
397 // Test joining regions
400 int64_t unit_step = edl->local_session->zoom_sample;
401 if(track->data_type == TRACK_VIDEO)
402 unit_step = (int64_t)(unit_step *
403 edl->session->frame_rate /
404 edl->session->sample_rate);
405 unit_step = MAX(unit_step, 1);
406 for(int64_t position = unit_start;
408 position += unit_step)
410 float value = get_value(position,PLAY_FORWARD,prev,next);
411 if(*coords_undefined)
414 *coords_undefined = 0;
418 *min = MIN(value, *min);
419 *max = MAX(value, *max);
424 void FloatAutos::set_proxy(int orig_scale, int new_scale)
427 orig_value = ((FloatAuto*)default_auto)->value * orig_scale;
428 ((FloatAuto*)default_auto)->value = orig_value / new_scale;
429 orig_value = ((FloatAuto*)default_auto)->value1 * orig_scale;
430 ((FloatAuto*)default_auto)->value1 = orig_value / new_scale;
432 for( FloatAuto *current= (FloatAuto*)first; current; current=(FloatAuto*)NEXT ) {
433 orig_value = current->value * orig_scale;
434 current->value = orig_value / new_scale;
435 orig_value = current->value1 * orig_scale;
436 current->value1 = orig_value / new_scale;
437 orig_value = current->control_in_value * orig_scale;
438 current->control_in_value = orig_value / new_scale;
439 orig_value = current->control_out_value * orig_scale;
440 current->control_out_value = orig_value / new_scale;
444 void FloatAutos::dump()
446 printf(" FloatAutos::dump %p\n", this);
447 printf(" Default: position %jd value=%f\n",
448 default_auto->position, ((FloatAuto*)default_auto)->get_value());
449 for(Auto* current = first; current; current = NEXT)
451 printf(" position %jd value=%7.3f invalue=%7.3f outvalue=%7.3f %s\n",
453 ((FloatAuto*)current)->get_value(),
454 ((FloatAuto*)current)->get_control_in_value(),
455 ((FloatAuto*)current)->get_control_out_value(),
456 FloatAuto::curve_name(((FloatAuto*)current)->curve_mode));
460 double FloatAutos::automation_integral(int64_t start, int64_t length, int direction)
462 if( direction == PLAY_REVERSE )
469 int64_t pos = start, len = length, end = pos + len;
471 int64_t prev_pos = 0, next_pos = end;
472 Auto *zprev = 0, *znext = 0;
473 FloatAuto *prev = (FloatAuto*)get_prev_auto(pos, direction, zprev, 0);
474 if( prev ) prev_pos = prev->position;
475 FloatAuto *next = (FloatAuto*)get_next_auto(pos, direction, znext, 0);
476 if( next ) next_pos = next->position;
477 if( !prev && !next ) prev = (FloatAuto*)default_auto;
478 double dt = next_pos - prev_pos;
479 double t0 = (pos - prev_pos) / dt;
480 if( (pos = next_pos) > end ) pos = end;
481 double t1 = (pos - prev_pos) / dt;
483 double y0 = !prev ? next->get_value(1) : prev->get_value(0);
484 double y1 = y0 + (!prev ? 0 : prev->get_control_out_value());
485 double y3 = !next ? prev->get_value(0) : next->get_value(1);
486 double y2 = y3 + (!next ? 0 : next->get_control_in_value());
487 if( y0 != y1 || y1 != y2 || y2 != y3 ) {
488 // bezier definite integral t0..t1
489 double f4 = -y0/4 + 3*y1/4 - 3*y2/4 + y3/4;
490 double f3 = y0 - 2*y1 + y2;
491 double f2 = -3*y0/2 + 3*y1/2;
492 double f1 = y0, t = t0;
493 double t2 = t*t, t3 = t2*t, t4 = t3*t;
494 t0 = t4*f4 + t3*f3 + t2*f2 + t*f1;
495 t = t1; t2 = t*t; t3 = t2*t; t4 = t3*t;
496 t1 = t4*f4 + t3*f3 + t2*f2 + t*f1;
501 value += dt * (t1 - t0);
507 int64_t FloatAutos::speed_position(double pos)
509 double length = track->get_length();
510 int64_t l = -1, r = track->to_units(length, 1);
512 for( int i=32; --i >= 0 && automation_integral(0,r,PLAY_FORWARD) <= pos; r*=2 );
513 for( int i=64; --i >= 0 && (r-l)>1; ) {
514 int64_t m = (l + r) / 2;
515 double t = automation_integral(0,m,PLAY_FORWARD) - pos;
516 *(t >= 0 ? &r : &l) = m;