initial commit
[goodguy/history.git] / cinelerra-5.0 / cinelerra / tracking.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2014 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 "arender.h"
23 #include "condition.h"
24 #include "cplayback.h"
25 #include "cwindow.h"
26 #include "cwindowgui.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "levelwindow.h"
30 #include "levelwindowgui.h"
31 #include "localsession.h"
32 #include "mainclock.h"
33 #include "meterpanel.h"
34 #include "mwindow.h"
35 #include "mwindowgui.h"
36 #include "tracking.h"
37 #include "patchbay.h"
38 #include "playbackengine.h"
39 #include "renderengine.h"
40 #include "mainsession.h"
41 #include "trackcanvas.h"
42
43
44
45 // States
46 #define PLAYING 0
47 #define DONE 1
48
49
50
51 Tracking::Tracking(MWindow *mwindow)
52  : Thread(1, 0, 0)
53 {
54         this->gui = mwindow->gui;
55         this->mwindow = mwindow; 
56         follow_loop = 0; 
57         visible = 0;
58         pixel = 0;
59         state = DONE;
60         startup_lock = new Condition(0, "Tracking::startup_lock");
61 }
62
63 Tracking::~Tracking()
64 {
65         if(state == PLAYING)
66         {
67 // Stop loop
68                 state = DONE;
69 // Not working in NPTL for some reason
70 //              Thread::cancel();
71                 Thread::join();
72         }
73
74
75         delete startup_lock;
76 }
77
78 void Tracking::create_objects()
79 {
80 //      start();
81 }
82
83 int Tracking::start_playback(double new_position)
84 {
85         if(state != PLAYING)
86         {
87                 last_position = new_position;
88                 state = PLAYING;
89                 draw();
90                 Thread::start();
91                 startup_lock->lock("Tracking::start_playback");
92         }
93         return 0;
94 }
95
96 int Tracking::stop_playback()
97 {
98         if(state != DONE)
99         {
100 // Stop loop
101                 state = DONE;
102 // Not working in NPTL for some reason
103 //              Thread::cancel();
104                 Thread::join();
105
106 // Final position is updated continuously during playback
107 // Get final position
108                 double position = get_tracking_position();
109 // Update cursor
110                 update_tracker(position);
111         
112                 stop_meters();
113                 state = DONE;
114         }
115         return 0;
116 }
117
118 PlaybackEngine* Tracking::get_playback_engine()
119 {
120         return mwindow->cwindow->playback_engine;
121 }
122
123 double Tracking::get_tracking_position()
124 {
125         return get_playback_engine()->get_tracking_position();
126 }
127
128 //int Tracking::get_pixel(double position)
129 //{
130 //      return (int64_t)((position - mwindow->edl->local_session->view_start) *
131 //              mwindow->edl->session->sample_rate / 
132 //              mwindow->edl->local_session->zoom_sample + 
133 //              0.5);
134 //}
135
136
137 void Tracking::update_meters(int64_t position)
138 {
139         double output_levels[MAXCHANNELS];
140         int do_audio = get_playback_engine()->get_output_levels(output_levels, position);
141
142         if(do_audio)
143         {
144                 module_levels.remove_all();
145                 get_playback_engine()->get_module_levels(&module_levels, position);
146
147                 mwindow->cwindow->gui->lock_window("Tracking::update_meters 1");
148                 mwindow->cwindow->gui->meters->update(output_levels);
149                 mwindow->cwindow->gui->unlock_window();
150
151                 mwindow->lwindow->gui->lock_window("Tracking::update_meters 2");
152                 mwindow->lwindow->gui->panel->update(output_levels);
153                 mwindow->lwindow->gui->unlock_window();
154
155                 mwindow->gui->lock_window("Tracking::update_meters 3");
156                 mwindow->gui->update_meters(&module_levels);
157                 mwindow->gui->unlock_window();
158         }
159 }
160
161 void Tracking::stop_meters()
162 {
163         mwindow->cwindow->gui->lock_window("Tracking::stop_meters 1");
164         mwindow->cwindow->gui->meters->stop_meters();
165         mwindow->cwindow->gui->unlock_window();
166
167         mwindow->gui->lock_window("Tracking::stop_meters 2");
168         mwindow->gui->stop_meters();
169         mwindow->gui->unlock_window();
170
171         mwindow->lwindow->gui->lock_window("Tracking::stop_meters 3");
172         mwindow->lwindow->gui->panel->stop_meters();
173         mwindow->lwindow->gui->unlock_window();
174 }
175
176
177
178
179 void Tracking::update_tracker(double position)
180 {
181 }
182
183 void Tracking::draw()
184 {
185 //      gui->lock_window("Tracking::draw");
186 //      if(!visible)
187 //      {
188 //              pixel = get_pixel(last_position);
189 //      }
190 // 
191 //      gui->canvas->set_color(GREEN);
192 //      gui->canvas->set_inverse();
193 //      gui->canvas->draw_line(pixel, 0, pixel, gui->canvas->get_h());
194 //      gui->canvas->set_opaque();
195 //      gui->canvas->flash(pixel, 0, pixel + 1, gui->canvas->get_h());
196 //      visible ^= 1;
197 //      gui->unlock_window();
198 }
199
200
201 void Tracking::run()
202 {
203         startup_lock->unlock();
204
205         double position;
206         while(state != DONE)
207         {
208                 Thread::enable_cancel();
209                 timer.delay(1000 / TRACKING_RATE);
210                 Thread::disable_cancel();
211
212                 if(state != DONE)
213                 {
214
215 // can be stopped during wait
216                         if(get_playback_engine()->tracking_active)
217                         {
218 // Get position of cursor
219                                 position = get_tracking_position();
220
221 // Update cursor
222                                 update_tracker(position);
223                         }
224                 }
225         }
226 }
227
228
229
230
231