initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / keyframes.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  * 
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.
10  * 
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.
15  * 
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
19  * 
20  */
21
22 #include "bchash.h"
23 #include "clip.h"
24 #include "format.inc"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "filexml.h"
28 #include "keyframe.h"
29 #include "keyframes.h"
30 #include "localsession.h"
31 #include "track.h"
32 #include "transportque.inc"
33
34 KeyFrames::KeyFrames(EDL *edl, Track *track)
35  : Autos(edl, track)
36 {
37         type = Autos::AUTOMATION_TYPE_PLUGIN;
38 }
39
40 KeyFrames::~KeyFrames()
41 {
42 }
43
44
45 KeyFrame* KeyFrames::get_prev_keyframe(int64_t position,
46         int direction)
47 {
48         KeyFrame *current = 0;
49
50 // This doesn't work because edl->selectionstart doesn't change during
51 // playback at the same rate as PluginClient::source_position.
52         if(position < 0)
53         {
54                 position = track->to_units(edl->local_session->get_selectionstart(1), 0);
55         }
56
57 // Get keyframe on or before current position
58         for(current = (KeyFrame*)last;
59                 current;
60                 current = (KeyFrame*)PREVIOUS)
61         {
62                 if(direction == PLAY_FORWARD && current->position <= position) break;
63                 else
64                 if(direction == PLAY_REVERSE && current->position < position) break;
65         }
66
67 // Nothing before current position
68         if(!current && first)
69         {
70                 current = (KeyFrame*)first;
71         }
72         else
73 // No keyframes
74         if(!current)
75         {
76                 current = (KeyFrame*)default_auto;
77         }
78
79         return current;
80 }
81
82 KeyFrame* KeyFrames::get_keyframe()
83 {
84 // Search for keyframe on or before selection
85         KeyFrame *result = 
86                 get_prev_keyframe(
87                         track->to_units(edl->local_session->get_selectionstart(1), 0), 
88                         PLAY_FORWARD);
89
90 // Return nearest keyframe if not in automatic keyframe generation
91         if(!edl->session->auto_keyframes)
92         {
93                 return result;
94         }
95         else
96 // Return new keyframe
97         if(result == (KeyFrame*)default_auto || 
98                 result->position != track->to_units(edl->local_session->get_selectionstart(1), 0))
99         {
100                 return (KeyFrame*)insert_auto(track->to_units(edl->local_session->get_selectionstart(1), 0));
101         }
102         else
103 // Return existing keyframe
104         {
105                 return result;
106         }
107
108         return 0;
109 }
110
111
112 void KeyFrames::update_parameter(KeyFrame *src)
113 {
114         const int debug = 0;
115 // Create new keyframe if auto keyframes or replace entire keyframe.
116         double selection_start = edl->local_session->get_selectionstart(0);
117         double selection_end = edl->local_session->get_selectionend(0);
118         selection_start = edl->align_to_frame(selection_start, 0);
119         selection_end = edl->align_to_frame(selection_end, 0);
120
121         if(EQUIV(selection_start, selection_end))
122         {
123 // Search for keyframe to write to
124                 KeyFrame *dst = get_keyframe();
125                 
126                 dst->copy_data(src);
127         }
128         else
129 // Replace changed parameter in all selected keyframes.
130         {
131                 BC_Hash *params = 0;
132                 char *text = 0;
133                 char *extra = 0;
134
135
136 // Search all keyframes in selection but don't create a new one.
137                 int64_t start = track->to_units(selection_start, 0);
138                 int64_t end = track->to_units(selection_end, 0);
139                 KeyFrame *current = get_prev_keyframe(
140                                 start, 
141                                 PLAY_FORWARD);
142
143 // The first one determines the changed parameters since it is the one displayed
144 // in the GUI.
145                 current->get_diff(src,
146                         &params, 
147                         &text,
148                         &extra);
149
150
151 if(debug) printf("KeyFrames::update_parameter %d params=%p position=" _LD " start=" _LD "\n", 
152   __LINE__,  params, current->position, track->to_units(start, 0));
153
154 if(debug && params)
155 {
156 for(int i = 0; i < params->size(); i++)
157 printf("KeyFrames::update_parameter %d changed=%s %s\n", 
158 __LINE__,  
159 params->get_key(i),
160 params->get_value(i));
161 }
162
163 // Always update the first one
164                 current->update_parameter(params, 
165                         text,
166                         extra);
167                 for(current = (KeyFrame*)NEXT ; current && current->position < end; current = (KeyFrame*)NEXT)
168                 {
169 if(debug) printf("KeyFrames::update_parameter %d position=" _LD "\n", 
170   __LINE__, current->position);
171                         current->update_parameter(params, 
172                                 text,
173                                 extra);
174                 }
175                 
176                 delete params;
177                 delete [] text,
178                 delete [] extra;
179         }
180 }
181
182 Auto* KeyFrames::new_auto()
183 {
184         return new KeyFrame(edl, this);
185 }
186
187
188 void KeyFrames::dump(FILE *fp)
189 {
190         fprintf(fp,"    DEFAULT_KEYFRAME\n");
191         ((KeyFrame*)default_auto)->dump(fp);
192         fprintf(fp,"    KEYFRAMES total=%d\n", total());
193         for(KeyFrame *current = (KeyFrame*)first;
194                 current;
195                 current = (KeyFrame*)NEXT)
196         {
197                 current->dump(fp);
198         }
199 }
200