Stop showing bar lines sooner.
[ardour.git] / gtk2_ardour / tempo_lines.cc
1 /*
2     Copyright (C) 2002-2007 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/compose.h"
21
22 #include "canvas/canvas.h"
23 #include "canvas/debug.h"
24
25 #include "ardour_ui.h"
26 #include "public_editor.h"
27 #include "rgb_macros.h"
28 #include "tempo_lines.h"
29
30 using namespace std;
31
32 TempoLines::TempoLines (ArdourCanvas::Container* group, double)
33         : lines (group, ArdourCanvas::LineSet::Vertical)
34 {
35         lines.set_extent (ArdourCanvas::COORD_MAX);
36 }
37
38 void
39 TempoLines::tempo_map_changed()
40 {
41         lines.clear ();
42 }
43
44 void
45 TempoLines::show ()
46 {
47         lines.show ();
48 }
49
50 void
51 TempoLines::hide ()
52 {
53         lines.hide ();
54 }
55
56 void
57 TempoLines::draw_ticks (const ARDOUR::TempoMap::BBTPointList::const_iterator& b,
58                         unsigned                                              divisions,
59                         framecnt_t                                            leftmost_frame,
60                         framecnt_t                                            frame_rate)
61 {
62         const double   fpb  = b->tempo->frames_per_beat(frame_rate);
63         const uint32_t base = ARDOUR_UI::config()->color_mod("measure line beat", "measure line beat");
64
65         for (unsigned l = 1; l < divisions; ++l) {
66                 /* find the coarsest division level this tick falls on */
67                 unsigned level = divisions;
68                 for (unsigned d = divisions; d >= 4; d /= 2) {
69                         if (l % (divisions / d) == 0) {
70                                 level = d;
71                         }
72                 }
73
74                 /* draw line with alpha corresponding to coarsest level */
75                 const uint8_t    a = max(0, (int)rint(UINT_RGBA_A(base) / (0.75 * log2(level))));
76                 const uint32_t   c = UINT_RGBA_CHANGE_A(base, a);
77                 const framepos_t f = b->frame + (l * (fpb / (double)divisions));
78                 if (f > leftmost_frame) {
79                         lines.add (PublicEditor::instance().sample_to_pixel_unrounded (f), 1.0, c);
80                 }
81         }
82 }
83
84 void
85 TempoLines::draw (const ARDOUR::TempoMap::BBTPointList::const_iterator& begin,
86                   const ARDOUR::TempoMap::BBTPointList::const_iterator& end,
87                   unsigned                                              divisions,
88                   framecnt_t                                            leftmost_frame,
89                   framecnt_t                                            frame_rate)
90 {
91         ARDOUR::TempoMap::BBTPointList::const_iterator i;
92         double  beat_density;
93
94         uint32_t beats = 0;
95         uint32_t bars = 0;
96         uint32_t color;
97
98         /* get the first bar spacing */
99
100         i = end;
101         i--;
102         bars = (*i).bar - (*begin).bar; 
103         beats = distance (begin, end) - bars;
104
105         beat_density = (beats * 10.0f) / lines.canvas()->width();
106
107         if (beat_density > 2.0f) {
108                 /* if the lines are too close together, they become useless */
109                 lines.clear ();
110                 return;
111         }
112
113         /* constrain divisions to a log2 factor to cap line density */
114         while (divisions > 3 && beat_density * divisions > 0.4) {
115                 divisions /= 2;
116         }
117
118         lines.clear ();
119
120         if (beat_density < 0.1 && begin != end && begin->frame > 0) {
121                 /* draw subdivisions of the beat before the first visible beat line */
122                 ARDOUR::TempoMap::BBTPointList::const_iterator prev = begin;
123                 --prev;
124                 draw_ticks(prev, divisions, leftmost_frame, frame_rate);
125         }
126
127         for (i = begin; i != end; ++i) {
128
129                 if ((*i).is_bar()) {
130                         color = ARDOUR_UI::config()->color ("measure line bar");
131                 } else {
132                         if (beat_density > 0.3) {
133                                 continue; /* only draw beat lines if the gaps between beats are large. */
134                         }
135                         color = ARDOUR_UI::config()->color_mod ("measure line beat", "measure line beat");
136                 }
137
138                 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).frame);
139
140                 lines.add (xpos, 1.0, color);
141
142                 if (beat_density < 0.1) {
143                         /* draw subdivisions of this beat */
144                         draw_ticks(i, divisions, leftmost_frame, frame_rate);
145                 }
146         }
147 }
148