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
25 #include "edlsession.h"
28 #include "keyframes.h"
29 #include "localsession.h"
31 #include "transportque.inc"
33 KeyFrames::KeyFrames(EDL *edl, Track *track)
36 type = Autos::AUTOMATION_TYPE_PLUGIN;
39 KeyFrames::~KeyFrames()
44 KeyFrame* KeyFrames::get_prev_keyframe(int64_t position,
47 KeyFrame *current = 0;
49 // This doesn't work because edl->selectionstart doesn't change during
50 // playback at the same rate as PluginClient::source_position.
53 position = track->to_units(edl->local_session->get_selectionstart(1), 0);
56 // Get keyframe on or before current position
57 for(current = (KeyFrame*)last;
59 current = (KeyFrame*)PREVIOUS)
61 if(direction == PLAY_FORWARD && current->position <= position) break;
63 if(direction == PLAY_REVERSE && current->position < position) break;
66 // Nothing before current position
69 current = (KeyFrame*)first;
75 current = (KeyFrame*)default_auto;
81 KeyFrame* KeyFrames::get_keyframe()
83 int64_t pos = track->to_units(edl->local_session->get_selectionstart(1), 0);
84 // Search for keyframe on or before selection
85 KeyFrame *result = get_prev_keyframe(pos, PLAY_FORWARD);
86 if( edl->session->auto_keyframes ) {
87 if( !result || result->position != pos ||
88 result == (KeyFrame*)default_auto )
89 // generate keyframes while tweeking, and no keyframe found at pos
90 result = (KeyFrame*)insert_auto(pos);
96 void KeyFrames::update_parameter(KeyFrame *src)
98 // Create new keyframe if auto keyframes or replace entire keyframe.
99 double selection_start = edl->local_session->get_selectionstart(0);
100 double selection_end = edl->local_session->get_selectionend(0);
101 selection_start = edl->align_to_frame(selection_start, 0);
102 selection_end = edl->align_to_frame(selection_end, 0);
104 if( EQUIV(selection_start, selection_end) ) {
105 // Search for keyframe to write
106 KeyFrame *dst = get_keyframe();
110 // Replace changed parameter in all selected keyframes.
112 char *text = 0, *extra = 0;
113 // Search all keyframes in selection but don't create a new one.
114 int64_t start = track->to_units(selection_start, 0);
115 int64_t end = track->to_units(selection_end, 0);
116 KeyFrame *current = get_prev_keyframe(start, PLAY_FORWARD);
117 // The first one determines the changed parameters since it is the one displayed
118 current->get_diff(src, ¶ms, &text, &extra);
119 // Always update the first one
120 current->update_parameter(params, text, extra);
122 for( current = (KeyFrame*)NEXT; current; current = (KeyFrame*)NEXT ) {
123 if( current->position >= end ) break;
124 current->update_parameter(params, text, extra);
132 Auto* KeyFrames::new_auto()
134 return new KeyFrame(edl, this);
138 void KeyFrames::dump(FILE *fp)
140 fprintf(fp," DEFAULT_KEYFRAME\n");
141 ((KeyFrame*)default_auto)->dump(fp);
142 fprintf(fp," KEYFRAMES total=%d\n", total());
143 for(KeyFrame *current = (KeyFrame*)first;
145 current = (KeyFrame*)NEXT)