shuttle and transportque reworks, new shdmp, titler font textbox tweak
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / transportque.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 "bcsignals.h"
23 #include "clip.h"
24 #include "condition.h"
25 #include "edl.h"
26 #include "edlsession.h"
27 #include "localsession.h"
28 #include "playbackengine.h"
29 #include "tracks.h"
30 #include "transportque.h"
31
32 TransportCommand::TransportCommand()
33 {
34 // In rendering we want a master EDL so settings don't get clobbered
35 // in the middle of a job.
36         edl = new EDL;
37         edl->create_objects();
38         command = 0;
39         change_type = 0;
40         reset();
41 }
42
43 TransportCommand::~TransportCommand()
44 {
45         edl->Garbage::remove_user();
46 }
47
48 void TransportCommand::reset()
49 {
50         playbackstart = 0;
51         start_position = 0;
52         end_position = 0;
53         infinite = 0;
54         realtime = 0;
55         resume = 0;
56         toggle_audio = 0;
57         loop_play = 0;
58         displacement = 0;
59         speed = 0;
60 // Don't reset the change type for commands which don't perform the change
61         if(command != STOP) change_type = 0;
62         command = COMMAND_NONE;
63 }
64
65 EDL* TransportCommand::get_edl()
66 {
67         return edl;
68 }
69
70 void TransportCommand::delete_edl()
71 {
72         edl->Garbage::remove_user();
73         edl = 0;
74 }
75
76 void TransportCommand::new_edl()
77 {
78         edl = new EDL;
79         edl->create_objects();
80 }
81
82
83 void TransportCommand::copy_from(TransportCommand *command)
84 {
85         this->command = command->command;
86         this->change_type = command->change_type;
87         this->edl->copy_all(command->edl);
88         this->start_position = command->start_position;
89         this->end_position = command->end_position;
90         this->playbackstart = command->playbackstart;
91         this->realtime = command->realtime;
92         this->resume = command->resume;
93         this->toggle_audio = command->toggle_audio;
94         this->loop_play = command->loop_play;
95         this->displacement = command->displacement;
96         this->speed = command->speed;
97 }
98
99 TransportCommand& TransportCommand::operator=(TransportCommand &command)
100 {
101         copy_from(&command);
102         return *this;
103 }
104
105 int TransportCommand::single_frame(int command)
106 {
107         return (command == SINGLE_FRAME_FWD || command == SINGLE_FRAME_REWIND ||
108                 command == CURRENT_FRAME || command == LAST_FRAME);
109 }
110
111 int TransportCommand::get_direction(int command)
112 {
113         switch(command) {
114         case SINGLE_FRAME_FWD:
115         case NORMAL_FWD:
116         case FAST_FWD:
117         case SLOW_FWD:
118         case CURRENT_FRAME:
119                 return PLAY_FORWARD;
120
121         case SINGLE_FRAME_REWIND:
122         case NORMAL_REWIND:
123         case FAST_REWIND:
124         case SLOW_REWIND:
125         case LAST_FRAME:
126                 return PLAY_REVERSE;
127
128         default:
129                 break;
130         }
131         return PLAY_FORWARD;
132 }
133
134 float TransportCommand::get_speed(int command, float speed)
135 {
136         switch(command) {
137         case SLOW_FWD:
138         case SLOW_REWIND:
139                 return speed ? speed : 0.5;
140
141         case NORMAL_FWD:
142         case NORMAL_REWIND:
143         case SINGLE_FRAME_FWD:
144         case SINGLE_FRAME_REWIND:
145         case CURRENT_FRAME:
146         case LAST_FRAME:
147                 return 1.;
148
149         case FAST_FWD:
150         case FAST_REWIND:
151                 return speed ? speed : 2.;
152         }
153
154         return 0.;
155 }
156
157 // Assume starting without pause
158 void TransportCommand::set_playback_range(EDL *edl, int use_inout, int do_displacement)
159 {
160         if( !edl ) edl = this->edl;
161         double length = edl->tracks->total_playable_length();
162         double frame_period = 1.0 / edl->session->frame_rate;
163         displacement = 0;
164
165         start_position = use_inout && edl->local_session->inpoint_valid() ?
166                 edl->local_session->get_inpoint() :
167                 !loop_play ? edl->local_session->get_selectionstart(1) : 0;
168         end_position = use_inout && edl->local_session->outpoint_valid() ?
169                 edl->local_session->get_outpoint() :
170                 !loop_play ? edl->local_session->get_selectionend(1) : length;
171
172         if( !use_inout && EQUIV(start_position, end_position) ) {
173 // starting play at or past end_position, play to end_position of media (for mixers)
174                 if( start_position >= length )
175                         length = edl->tracks->total_length();
176                 switch( command ) {
177                 case SLOW_FWD:
178                 case FAST_FWD:
179                 case NORMAL_FWD: {
180                         end_position = length;
181 // this prevents a crash if start_position position is after the loop when playing forwards
182                         if( edl->local_session->loop_playback &&
183                             start_position > edl->local_session->loop_end ) {
184                                 start_position = edl->local_session->loop_start;
185                         }
186                         break; }
187
188                 case SLOW_REWIND:
189                 case FAST_REWIND:
190                 case NORMAL_REWIND:
191                         start_position = 0;
192 // this prevents a crash if start_position position is before the loop when playing backwards
193                         if( edl->local_session->loop_playback &&
194                             end_position <= edl->local_session->loop_start ) {
195                                         end_position = edl->local_session->loop_end;
196                         }
197                         break;
198
199                 case CURRENT_FRAME:
200                 case LAST_FRAME:
201                 case SINGLE_FRAME_FWD:
202                         end_position = start_position + frame_period;
203                         break;
204
205                 case SINGLE_FRAME_REWIND:
206                         start_position = end_position - frame_period;
207                         break;
208                 }
209
210                 if( realtime && do_displacement ) {
211                         if( (command != CURRENT_FRAME && get_direction() == PLAY_FORWARD ) ||
212                             (command != LAST_FRAME    && get_direction() == PLAY_REVERSE ) ) {
213                                 start_position += frame_period;
214                                 end_position += frame_period;
215                                 displacement = 1;
216                         }
217                 }
218         }
219
220 //      if( start_position < 0 )
221 //              start_position = 0;
222 //      if( end_position > length )
223 //              end_position = length;
224         if( end_position < start_position )
225                 end_position = start_position;
226
227         playbackstart = get_direction() == PLAY_FORWARD ?
228                 start_position : end_position;
229 }
230
231 void TransportCommand::playback_range_adjust_inout()
232 {
233         if(edl->local_session->inpoint_valid() ||
234                 edl->local_session->outpoint_valid())
235         {
236                 playback_range_inout();
237         }
238 }
239
240 void TransportCommand::playback_range_inout()
241 {
242         if(edl->local_session->inpoint_valid())
243                 start_position = edl->local_session->get_inpoint();
244         else
245                 start_position = 0;
246
247         if(edl->local_session->outpoint_valid())
248                 end_position = edl->local_session->get_outpoint();
249         else {
250                 end_position = edl->tracks->total_playable_length();
251                 if( start_position >= end_position )
252                         end_position = edl->tracks->total_length();
253         }
254 }
255
256 void TransportCommand::playback_range_project()
257 {
258         start_position = 0;
259         end_position = edl->tracks->total_playable_length();
260 }
261
262 void TransportCommand::playback_range_1frame()
263 {
264         start_position = end_position = edl->local_session->get_selectionstart(1);
265         if( edl->session->frame_rate > 0 ) end_position += 1./edl->session->frame_rate;
266 }
267
268