rename Tempo _beats_per_minute to _note_types_per_minute, provide pulse helpers.
[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 "tempo_lines.h"
26 #include "public_editor.h"
27 #include "rgb_macros.h"
28 #include "ui_config.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 (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
58                         unsigned                                              divisions,
59                         framecnt_t                                            leftmost_frame,
60                         framecnt_t                                            frame_rate)
61 {
62         const uint32_t base = UIConfiguration::instance().color_mod("measure line beat", "measure line beat");
63
64         for (unsigned l = 1; l < divisions; ++l) {
65                 /* find the coarsest division level this tick falls on */
66                 unsigned level = divisions;
67                 for (unsigned d = divisions; d >= 4; d /= 2) {
68                         if (l % (divisions / d) == 0) {
69                                 level = d;
70                         }
71                 }
72                 /* draw line with alpha corresponding to coarsest level */
73                 const uint8_t    a = max(8, (int)rint(UINT_RGBA_A(base) / (0.8 * log2(level))));
74                 const uint32_t   c = UINT_RGBA_CHANGE_A(base, a);
75                 framepos_t f = 0;
76
77                 if (grid.begin()->c != 0.0) {
78                         const double beat_divisions = (l / ((double) divisions)) * (grid.begin()->tempo.note_type() / grid.begin()->meter.note_divisor());
79                         const double time_at_division = log (((grid.begin()->c * (beat_divisions)) /
80                                                            grid.begin()->tempo.note_types_per_minute()) + 1) / grid.begin()->c;
81
82                         f = grid.begin()->frame + (framecnt_t) floor ((time_at_division * 60.0 * frame_rate) + 0.5);
83                 } else {
84                         const double fpb  = grid.begin()->tempo.frames_per_note_type (frame_rate)
85                                 * (grid.begin()->tempo.note_type() / grid.begin()->meter.note_divisor());
86
87                         f = grid.begin()->frame + (l * (fpb / (double) divisions));
88                 }
89                 if (f > leftmost_frame) {
90                         lines.add (PublicEditor::instance().sample_to_pixel_unrounded (f), 1.0, c);
91                 }
92         }
93 }
94
95 void
96 TempoLines::draw (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
97                   unsigned                                              divisions,
98                   framecnt_t                                            leftmost_frame,
99                   framecnt_t                                            frame_rate)
100 {
101         std::vector<ARDOUR::TempoMap::BBTPoint>::const_iterator i;
102         double  beat_density;
103
104         uint32_t beats = 0;
105         uint32_t bars = 0;
106         const uint32_t bar_color = UIConfiguration::instance().color ("measure line bar");
107         const uint32_t beat_color = UIConfiguration::instance().color_mod ("measure line beat", "measure line beat");
108         uint32_t color;
109
110         bool all_bars = false;
111         /* get the first bar spacing */
112
113         i = grid.end();
114         i--;
115         bars = (*i).bar - (*grid.begin()).bar;
116
117         int32_t bar_mod = 4;
118
119         if (bars < distance (grid.begin(), grid.end()) - 1) {
120                 /* grid contains beats and bars */
121                 beats = distance (grid.begin(), grid.end()) - bars;
122         } else {
123                 /* grid contains only bars */
124                 beats = distance (grid.begin(), grid.end());
125
126                 if (i != grid.begin()) {
127                         const int32_t last_bar = (*i).bar;
128                         i--;
129                         bar_mod = (last_bar - (*i).bar) * 4;
130                 }
131
132                 all_bars = true;
133         }
134
135         double canvas_width_used = 1.0;
136         if (leftmost_frame < grid.front().frame) {
137                 const framecnt_t frame_distance = max ((framecnt_t) 1, grid.back().frame - grid.front().frame);
138                 canvas_width_used = 1.0 - ((grid.front().frame - leftmost_frame) / (double) (frame_distance + grid.front().frame));
139         }
140
141         beat_density = (beats * 10.0f) / (lines.canvas()->width() * canvas_width_used);
142
143         if (beat_density > 2.0f) {
144                 /* if the lines are too close together, they become useless */
145                 lines.clear ();
146                 return;
147         }
148
149         /* constrain divisions to a log2 factor to cap line density */
150         while (divisions > 3 && beat_density * divisions > 0.4) {
151                 divisions /= 2;
152         }
153
154         lines.clear ();
155         if (beat_density <= 0.12 && grid.begin() != grid.end() && grid.begin()->frame > 0 && !all_bars) {
156                 /* draw subdivisions of the beat before the first visible beat line XX this shouldn't happen now */
157                 std::vector<ARDOUR::TempoMap::BBTPoint> vec;
158                 vec.push_back (*i);
159                 draw_ticks (vec, divisions, leftmost_frame, frame_rate);
160         }
161
162         for (i = grid.begin(); i != grid.end(); ++i) {
163
164                 if ((*i).is_bar()) {
165                         /* keep all_bar beat density down */
166                         if (all_bars && beat_density > 0.3 && ((*i).bar % bar_mod) != 1) {
167                                 continue;
168                         }
169
170                         color = bar_color;
171                 } else {
172                         if (beat_density > 0.3) {
173                                 continue; /* only draw beat lines if the gaps between beats are large. */
174                         }
175                         color = beat_color;
176                 }
177
178                 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).frame);
179
180                 lines.add (xpos, 1.0, color);
181
182                 if (beat_density <= 0.12 && !all_bars) {
183                         /* draw subdivisions of this beat */
184                         std::vector<ARDOUR::TempoMap::BBTPoint> vec;
185                         vec.push_back (*i);
186                         draw_ticks (vec, divisions, leftmost_frame, frame_rate);
187                 }
188         }
189 }
190