r827: Fix a crash when no audio output device can be opened.
[cinelerra_cv.git] / cinelerra / ctracking.C
blob59ed3c0ad04d6ab3c4668e2e70bec39818b24114
1 #include "bcsignals.h"
2 #include "cplayback.h"
3 #include "ctimebar.h"
4 #include "ctracking.h"
5 #include "cwindow.h"
6 #include "cwindowgui.h"
7 #include "edl.h"
8 #include "edlsession.h"
9 #include "localsession.h"
10 #include "mainclock.h"
11 #include "maincursor.h"
12 #include "mwindow.h"
13 #include "mwindowgui.h"
14 #include "patchbay.h"
15 #include "trackcanvas.h"
16 #include "transportque.h"
17 #include "zoombar.h"
19 CTracking::CTracking(MWindow *mwindow, CWindow *cwindow)
20  : Tracking(mwindow)
22         this->cwindow = cwindow;
25 CTracking::~CTracking()
29 PlaybackEngine* CTracking::get_playback_engine()
31         return cwindow->playback_engine;
34 int CTracking::start_playback(double new_position)
36         mwindow->gui->cursor->playing_back = 1;
38         Tracking::start_playback(new_position);
39         return 0;
42 int CTracking::stop_playback()
44         mwindow->gui->cursor->playing_back = 0;
47         Tracking::stop_playback();
48         return 0;
51 #define SCROLL_THRESHOLD 0
54 int CTracking::update_scroll(double position)
56         int updated_scroll = 0;
58         if(mwindow->edl->session->view_follows_playback)
59         {
60                 double seconds_per_pixel = (double)mwindow->edl->local_session->zoom_sample / 
61                         mwindow->edl->session->sample_rate;
62                 double half_canvas = seconds_per_pixel * 
63                         mwindow->gui->canvas->get_w() / 2;
64                 double midpoint = mwindow->edl->local_session->view_start * 
65                         seconds_per_pixel +
66                         half_canvas;
68                 if(get_playback_engine()->command->get_direction() == PLAY_FORWARD)
69                 {
70                         double left_boundary = midpoint + SCROLL_THRESHOLD * half_canvas;
71                         double right_boundary = midpoint + half_canvas;
73                         if(position > left_boundary &&
74                                 position < right_boundary)
75                         {
76                                 int pixels = Units::to_int64((position - midpoint) * 
77                                         mwindow->edl->session->sample_rate /
78                                         mwindow->edl->local_session->zoom_sample);
79                                 if(pixels) 
80                                 {
81                                         mwindow->move_right(pixels);
82 //printf("CTracking::update_scroll 1 %d\n", pixels);
83                                         updated_scroll = 1;
84                                 }
85                         }
86                 }
87                 else
88                 {
89                         double right_boundary = midpoint - SCROLL_THRESHOLD * half_canvas;
90                         double left_boundary = midpoint - half_canvas;
92                         if(position < right_boundary &&
93                                 position > left_boundary && 
94                                 mwindow->edl->local_session->view_start > 0)
95                         {
96                                 int pixels = Units::to_int64((midpoint - position) * 
97                                                 mwindow->edl->session->sample_rate /
98                                                 mwindow->edl->local_session->zoom_sample);
99                                 if(pixels) 
100                                 {
101                                         mwindow->move_left(pixels);
102                                         updated_scroll = 1;
103                                 }
104                         }
105                 }
106         }
108         return updated_scroll;
111 void CTracking::update_tracker(double position)
113         int updated_scroll = 0;
114 // Update cwindow slider
115         cwindow->gui->lock_window("CTracking::update_tracker 1");
116         cwindow->gui->slider->update(position);
118 // This is going to boost the latency but we need to update the timebar
119         cwindow->gui->timebar->draw_range();
120         cwindow->gui->timebar->flash();
121         cwindow->gui->unlock_window();
123 // Update mwindow cursor
124         mwindow->gui->lock_window("CTracking::update_tracker 2");
126         mwindow->edl->local_session->set_selectionstart(position);
127         mwindow->edl->local_session->set_selectionend(position);
129         updated_scroll = update_scroll(position);
131         mwindow->gui->mainclock->update(position);
132         mwindow->gui->patchbay->update();
134         if(!updated_scroll)
135         {
136                 mwindow->gui->cursor->update();
137                 mwindow->gui->zoombar->update();
140                 mwindow->gui->canvas->flash();
141                 mwindow->gui->flush();
142         }
143         mwindow->gui->unlock_window();
145 // Plugin GUI's hold lock on mwindow->gui here during user interface handlers.
146         mwindow->update_plugin_guis();
149         update_meters((int64_t)(position * mwindow->edl->session->sample_rate));
152 void CTracking::draw()