2 Copyright (C) 2002-2007 Paul Davis
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.
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.
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.
20 #include "pbd/compose.h"
22 #include "canvas/canvas.h"
23 #include "canvas/debug.h"
25 #include "tempo_lines.h"
26 #include "public_editor.h"
27 #include "rgb_macros.h"
28 #include "ui_config.h"
32 TempoLines::TempoLines (ArdourCanvas::Container* group, double, ARDOUR::BeatsSamplesConverter* bfc)
33 : lines (group, ArdourCanvas::LineSet::Vertical)
36 lines.set_extent (ArdourCanvas::COORD_MAX);
39 TempoLines::~TempoLines ()
46 TempoLines::tempo_map_changed (samplepos_t new_origin)
49 _bfc->set_origin_b (new_origin);
65 TempoLines::draw_ticks (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
67 samplecnt_t leftmost_sample,
68 samplecnt_t sample_rate)
70 const uint32_t base = UIConfiguration::instance().color_mod("measure line beat", "measure line beat");
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) {
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();
85 if (f > leftmost_sample) {
86 lines.add (PublicEditor::instance().sample_to_pixel_unrounded (f), 1.0, c);
92 TempoLines::draw (std::vector<ARDOUR::TempoMap::BBTPoint>& grid,
94 samplecnt_t leftmost_sample,
95 samplecnt_t sample_rate)
97 std::vector<ARDOUR::TempoMap::BBTPoint>::const_iterator i;
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");
106 bool all_bars = false;
107 /* get the first bar spacing */
111 bars = (*i).bar - (*grid.begin()).bar;
115 if (bars < distance (grid.begin(), grid.end()) - 1) {
116 /* grid contains beats and bars */
117 beats = distance (grid.begin(), grid.end()) - bars;
119 /* grid contains only bars */
120 beats = distance (grid.begin(), grid.end());
122 if (i != grid.begin()) {
123 const int32_t last_bar = (*i).bar;
125 bar_mod = (last_bar - (*i).bar) * 4;
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));
137 beat_density = (beats * 10.0f) / (lines.canvas()->width() * canvas_width_used);
139 if (beat_density > 2.0f) {
140 /* if the lines are too close together, they become useless */
145 /* constrain divisions to a log2 factor to cap line density */
146 while (divisions > 3 && beat_density * divisions > 0.4) {
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;
155 draw_ticks (vec, divisions, leftmost_sample, sample_rate);
158 for (i = grid.begin(); i != grid.end(); ++i) {
161 /* keep all_bar beat density down */
162 if (all_bars && beat_density > 0.3 && ((*i).bar % bar_mod) != 1) {
168 if (beat_density > 0.3) {
169 continue; /* only draw beat lines if the gaps between beats are large. */
174 ArdourCanvas::Coord xpos = PublicEditor::instance().sample_to_pixel_unrounded ((*i).sample);
176 lines.add (xpos, 1.0, color);
178 if (beat_density <= 0.12 && !all_bars) {
179 /* draw subdivisions of this beat */
180 std::vector<ARDOUR::TempoMap::BBTPoint> vec;
182 draw_ticks (vec, divisions, leftmost_sample, sample_rate);