Credit Andrew fix Alpha Bug of BT558 + small cleanup
[goodguy/cinelerra.git] / cinelerra-5.1 / 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->mwindow = mwindow;
55         state = DONE;
56         startup_lock = new Condition(0, "Tracking::startup_lock");
57 }
58
59 Tracking::~Tracking()
60 {
61         if(state == PLAYING)
62         {
63 // Stop loop
64                 state = DONE;
65 // Not working in NPTL for some reason
66 //              Thread::cancel();
67         }
68         Thread::join();
69
70         delete startup_lock;
71 }
72
73 void Tracking::create_objects()
74 {
75 //      start();
76 }
77
78 int Tracking::start_playback(double new_position)
79 {
80         if(state != PLAYING)
81         {
82                 last_position = new_position;
83                 state = PLAYING;
84                 draw();
85                 Thread::start();
86                 startup_lock->lock("Tracking::start_playback");
87         }
88         return 0;
89 }
90
91 int Tracking::stop_playback()
92 {
93         if(state != DONE)
94         {
95 // Stop loop
96                 state = DONE;
97 // Not working in NPTL for some reason
98 //              Thread::cancel();
99                 Thread::join();
100                 stop_meters();
101                 stop_mixers();
102 // tracking_position is updated continuously during playback
103 //  and when render ends, update cursor with final position
104                 update_tracker(get_tracking_position());
105         }
106         return 0;
107 }
108
109 PlaybackEngine* Tracking::get_playback_engine()
110 {
111         return mwindow->cwindow->playback_engine;
112 }
113
114 double Tracking::get_tracking_position()
115 {
116         return get_playback_engine()->get_tracking_position();
117 }
118
119 void Tracking::update_meters(int64_t position)
120 {
121         double output_levels[MAXCHANNELS];
122         int do_audio = get_playback_engine()->get_output_levels(output_levels, position);
123
124         if(do_audio)
125         {
126                 module_levels.remove_all();
127                 get_playback_engine()->get_module_levels(&module_levels, position);
128
129                 mwindow->cwindow->gui->lock_window("Tracking::update_meters 1");
130                 mwindow->cwindow->gui->meters->update(output_levels);
131                 mwindow->cwindow->gui->unlock_window();
132
133                 mwindow->lwindow->gui->lock_window("Tracking::update_meters 2");
134                 mwindow->lwindow->gui->panel->update(output_levels);
135                 mwindow->lwindow->gui->unlock_window();
136
137                 mwindow->gui->lock_window("Tracking::update_meters 3");
138                 mwindow->gui->update_meters(&module_levels);
139                 mwindow->gui->unlock_window();
140         }
141 }
142
143 void Tracking::stop_meters()
144 {
145         mwindow->cwindow->gui->lock_window("Tracking::stop_meters 1");
146         mwindow->cwindow->gui->meters->stop_meters();
147         mwindow->cwindow->gui->unlock_window();
148
149         mwindow->gui->lock_window("Tracking::stop_meters 2");
150         mwindow->gui->stop_meters();
151         mwindow->gui->unlock_window();
152
153         mwindow->lwindow->gui->lock_window("Tracking::stop_meters 3");
154         mwindow->lwindow->gui->panel->stop_meters();
155         mwindow->lwindow->gui->unlock_window();
156 }
157
158 void Tracking::stop_mixers()
159 {
160         mwindow->stop_mixers();
161 }
162
163
164 void Tracking::update_tracker(double position)
165 {
166 }
167
168 void Tracking::draw()
169 {
170 //      gui->lock_window("Tracking::draw");
171 //      if(!visible)
172 //      {
173 //              pixel = get_pixel(last_position);
174 //      }
175 //
176 //      gui->canvas->set_color(GREEN);
177 //      gui->canvas->set_inverse();
178 //      gui->canvas->draw_line(pixel, 0, pixel, gui->canvas->get_h());
179 //      gui->canvas->set_opaque();
180 //      gui->canvas->flash(pixel, 0, pixel + 1, gui->canvas->get_h());
181 //      visible ^= 1;
182 //      gui->unlock_window();
183 }
184
185
186 void Tracking::run()
187 {
188         startup_lock->unlock();
189
190         double position;
191         while(state != DONE)
192         {
193                 Thread::enable_cancel();
194                 timer.delay(1000 / TRACKING_RATE);
195                 Thread::disable_cancel();
196
197                 if(state != DONE)
198                 {
199
200 // can be stopped during wait
201                         if(get_playback_engine()->tracking_active)
202                         {
203 // Get position of cursor
204                                 position = get_tracking_position();
205
206 // Update cursor
207                                 update_tracker(position);
208                         }
209                 }
210         }
211 }
212
213
214
215
216