r105: This commit was manufactured by cvs2svn to create tag
[cinelerra_cv/mob.git] / hvirtual / cinelerra / tracking.C
bloba1c30343eb0d92ce9be9dab683529e8fe4022802
1 #include "arender.h"
2 #include "cplayback.h"
3 #include "cwindow.h"
4 #include "cwindowgui.h"
5 #include "edl.h"
6 #include "edlsession.h"
7 #include "levelwindow.h"
8 #include "levelwindowgui.h"
9 #include "localsession.h"
10 #include "mainclock.h"
11 #include "meterpanel.h"
12 #include "mwindow.h"
13 #include "mwindowgui.h"
14 #include "tracking.h"
15 #include "patchbay.h"
16 #include "playbackengine.h"
17 #include "renderengine.h"
18 #include "mainsession.h"
19 #include "trackcanvas.h"
23 // States
24 #define PLAYING 0
25 #define PAUSED 1
26 #define DONE 2
30 Tracking::Tracking(MWindow *mwindow)
31  : Thread(1, 0, 0)
33         this->gui = mwindow->gui;
34         this->mwindow = mwindow; 
35         follow_loop = 0; 
36         visible = 0;
37         pixel = 0;
38         state = PAUSED;
41 Tracking::~Tracking()
43         state = DONE;
44         pause_lock.unlock();
45         Thread::join();
48 void Tracking::create_objects()
50         startup_lock.lock();
51         pause_lock.lock();
52         start();
55 int Tracking::start_playback(double new_position)
57         last_position = new_position;
58         state = PLAYING;
59         draw();
60         pause_lock.unlock();
61         return 0;
64 int Tracking::stop_playback()
66 // Wait to change state
67         loop_lock.lock();
68 // Stop loop
69         pause_lock.lock();
70         state = PAUSED;
71         loop_lock.unlock();
73 // Final position is updated continuously during playback
74 // Get final position
75         double position = get_tracking_position();
76 // Update cursor
77         update_tracker(position);
78 //printf("Tracking::stop_playback %ld\n", position);
79         
80         stop_meters();
81         return 0;
84 PlaybackEngine* Tracking::get_playback_engine()
86         return mwindow->cwindow->playback_engine;
89 double Tracking::get_tracking_position()
91 //printf("Tracking::get_tracking_position %ld\n", get_playback_engine()->get_tracking_position());
92         return get_playback_engine()->get_tracking_position();
95 int Tracking::get_pixel(double position)
97         return (int64_t)((position - mwindow->edl->local_session->view_start) *
98                 mwindow->edl->session->sample_rate / 
99                 mwindow->edl->local_session->zoom_sample + 
100                 0.5);
104 void Tracking::update_meters(int64_t position)
106         double output_levels[MAXCHANNELS];
107 //printf("Tracking::update_meters 1\n");
108         int do_audio = get_playback_engine()->get_output_levels(output_levels, position);
110         if(do_audio)
111         {
112                 module_levels.remove_all();
113                 get_playback_engine()->get_module_levels(&module_levels, position);
115                 mwindow->cwindow->gui->lock_window();
116                 mwindow->cwindow->gui->meters->update(output_levels);
117                 mwindow->cwindow->gui->unlock_window();
119                 mwindow->lwindow->gui->lock_window();
120                 mwindow->lwindow->gui->panel->update(output_levels);
121                 mwindow->lwindow->gui->unlock_window();
123 //for(int i = 0; i < module_levels.total; i++)
124 //      printf("Tracking::update_meters 2 %f\n", module_levels.values[i]);
126                 mwindow->gui->lock_window();
127                 mwindow->gui->patchbay->update_meters(&module_levels);
128                 mwindow->gui->unlock_window();
129         }
130 //printf("Tracking::update_meters 3\n");
133 void Tracking::stop_meters()
135 //printf("Tracking::stop_meters 1\n");
136         mwindow->cwindow->gui->lock_window();
137         mwindow->cwindow->gui->meters->stop_meters();
138         mwindow->cwindow->gui->unlock_window();
140         mwindow->gui->lock_window();
141         mwindow->gui->patchbay->stop_meters();
142         mwindow->gui->unlock_window();
144         mwindow->lwindow->gui->lock_window();
145         mwindow->lwindow->gui->panel->stop_meters();
146         mwindow->lwindow->gui->unlock_window();
147 //printf("Tracking::stop_meters 2\n");
153 void Tracking::update_tracker(double position)
157 void Tracking::draw()
159         gui->lock_window();
160         if(!visible)
161         {
162                 pixel = get_pixel(last_position);
163         }
165         gui->canvas->set_color(GREEN);
166         gui->canvas->set_inverse();
167         gui->canvas->draw_line(pixel, 0, pixel, gui->canvas->get_h());
168         gui->canvas->set_opaque();
169 //printf("Tracking::draw %d %d\n", pixel, gui->canvas->get_h());
170         gui->canvas->flash(pixel, 0, pixel + 1, gui->canvas->get_h());
171         visible ^= 1;
172         gui->unlock_window();
176 int Tracking::wait_for_startup()
178         startup_lock.lock();
179         startup_lock.unlock();
180         return 0;
183 void Tracking::run()
185         double position;
186         int64_t last_peak = -1;
187         int64_t last_peak_number = 0; // for zeroing peaks
188         int64_t *peak_samples;
189         int64_t current_peak = 0, starting_peak;
190         int total_peaks;
191         int i, j, pass;
192         int audio_on = 0;
194         startup_lock.unlock();
196 //printf("Tracking::run 1\n");
197         while(state != DONE)
198         {
199                 pause_lock.lock();
200                 pause_lock.unlock();
202 //printf("Tracking::run 2\n");
203                 loop_lock.lock();
204                 if(state != PAUSED && state != DONE)
205                         timer.delay(100);
207                 if(state != PAUSED && state != DONE)
208                 {
210 //printf("Tracking::run 3\n");
211 // can be stopped during wait
212                         if(get_playback_engine()->tracking_active)   
213                         {
214 // Get position of cursor
215                                 position = get_tracking_position();
217 //printf("Tracking::run 4 %ld\n", position);
218 // Update cursor
219                                 update_tracker(position);
221 //printf("Tracking::run 5\n");
222                         }
223                 }
224                 loop_lock.unlock();
225 //printf("Tracking::run 6\n");
226         }