Optimize automation-event process splitting
[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, ARDOUR::BeatsSamplesConverter* bfc)
33         : lines (group, ArdourCanvas::LineSet::Vertical)
34         , _bfc (bfc)
35 {
36         lines.set_extent (ArdourCanvas::COORD_MAX);
37 }
38
39 TempoLines::~TempoLines ()
40 {
41         delete _bfc;
42         _bfc = 0;
43 }
44
45 void
46 TempoLines::tempo_map_changed (samplepos_t new_origin)
47 {
48         lines.clear ();
49         _bfc->set_origin_b (new_origin);
50 }
51
52 void
53 TempoLines::show ()
54 {
55         lines.show ();
56 }
57
58 void
59 TempoLines::hide ()
60 {
61         lines.hide ();
62 }
63
64 void
65 TempoLines::draw_ticks (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
66                         unsigned                                              divisions,
67                         samplecnt_t                                            leftmost_sample,
68                         samplecnt_t                                            sample_rate)
69 {
70         const uint32_t base = UIConfiguration::instance().color_mod("measure line beat", "measure line beat");
71
72         for (unsigned l = 1; l < divisions; ++l) {
73                 /* find the coarsest division level this tick falls on */
74                 unsigned level = divisions;
75                 for (unsigned d = divisions; d >= 4; d /= 2) {
76                         if (l % (divisions / d) == 0) {
77                                 level = d;
78                         }
79                 }
80                 /* draw line with alpha corresponding to coarsest level */
81                 const uint8_t    a = max(8, (int)rint(UINT_RGBA_A(base) / (0.8 * log2(level))));
82                 const uint32_t   c = UINT_RGBA_CHANGE_A(base, a);
83                 const samplepos_t f = _bfc->to (Temporal::Beats (grid.begin()->qn + (l / (double) divisions))) + _bfc->origin_b();
84
85                 if (f > leftmost_sample) {
86                         lines.add (PublicEditor::instance().sample_to_pixel_unrounded (f), 1.0, c);
87                 }
88         }
89 }
90
91 void
92 TempoLines::draw (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
93                   unsigned                                              divisions,
94                   samplecnt_t                                            leftmost_sample,
95                   samplecnt_t                                            sample_rate)
96 {
97         std::vector<ARDOUR::TempoMap::BBTPoint>::const_iterator i;
98         double  beat_density;
99
100         uint32_t beats = 0;
101         uint32_t bars = 0;
102         const uint32_t bar_color = UIConfiguration::instance().color ("measure line bar");
103         const uint32_t beat_color = UIConfiguration::instance().color_mod ("measure line beat", "measure line beat");
104         uint32_t color;
105
106         bool all_bars = false;
107         /* get the first bar spacing */
108
109         i = grid.end();
110         i--;
111         bars = (*i).bar - (*grid.begin()).bar;
112
113         int32_t bar_mod = 4;
114
115         if (bars < distance (grid.begin(), grid.end()) - 1) {
116                 /* grid contains beats and bars */
117                 beats = distance (grid.begin(), grid.end()) - bars;
118         } else {
119                 /* grid contains only bars */
120                 beats = distance (grid.begin(), grid.end());
121
122                 if (i != grid.begin()) {
123                         const int32_t last_bar = (*i).bar;
124                         i--;
125                         bar_mod = (last_bar - (*i).bar) * 4;
126                 }
127
128                 all_bars = true;
129         }
130
131         double canvas_width_used = 1.0;
132         if (leftmost_sample < grid.front().sample) {
133                 const samplecnt_t sample_distance = max ((samplecnt_t) 1, grid.back().sample - grid.front().sample);
134                 canvas_width_used = 1.0 - ((grid.front().sample - leftmost_sample) / (double) (sample_distance + grid.front().sample));
135         }
136
137         beat_density = (beats * 10.0f) / (lines.canvas()->width() * canvas_width_used);
138
139         if (beat_density > 2.0f) {
140                 /* if the lines are too close together, they become useless */
141                 lines.clear ();
142                 return;
143         }
144
145         /* constrain divisions to a log2 factor to cap line density */
146         while (divisions > 3 && beat_density * divisions > 0.4) {
147                 divisions /= 2;
148         }
149
150         lines.clear ();
151         if (beat_density <= 0.12 && grid.begin() != grid.end() && grid.begin()->sample > 0 && !all_bars) {
152                 /* draw subdivisions of the beat before the first visible beat line XX this shouldn't happen now */
153                 std::vector<ARDOUR::TempoMap::BBTPoint> vec;
154                 vec.push_back (*i);
155                 draw_ticks (vec, divisions, leftmost_sample, sample_rate);
156         }
157
158         for (i = grid.begin(); i != grid.end(); ++i) {
159
160                 if ((*i).is_bar()) {
161                         /* keep all_bar beat density down */
162                         if (all_bars && beat_density > 0.3 && ((*i).bar % bar_mod) != 1) {
163                                 continue;
164                         }
165
166                         color = bar_color;
167                 } else {
168                         if (beat_density > 0.3) {
169                                 continue; /* only draw beat lines if the gaps between beats are large. */
170                         }
171                         color = beat_color;
172                 }
173
174                 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).sample);
175
176                 lines.add (xpos, 1.0, color);
177
178                 if (beat_density <= 0.12 && !all_bars) {
179                         /* draw subdivisions of this beat */
180                         std::vector<ARDOUR::TempoMap::BBTPoint> vec;
181                         vec.push_back (*i);
182                         draw_ticks (vec, divisions, leftmost_sample, sample_rate);
183                 }
184         }
185 }
186