2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
5 * Copyright (C) 2006-2015 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
7 * Copyright (C) 2008-2011 Carl Hetherington <carl@carlh.net>
8 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
10 * Copyright (C) 2017-2018 Ben Loftis <ben@harrisonconsoles.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "gtk2ardour-config.h"
31 #include <cstdio> // for sprintf, grrr
37 #include "pbd/error.h"
38 #include "pbd/memento_command.h"
40 #include <gtkmm2ext/utils.h>
41 #include <gtkmm2ext/gtk_ui.h>
43 #include "ardour/session.h"
44 #include "ardour/tempo.h"
45 #include <gtkmm2ext/doi.h>
46 #include <gtkmm2ext/utils.h>
48 #include "canvas/canvas.h"
49 #include "canvas/item.h"
50 #include "canvas/line_set.h"
54 #include "tempo_dialog.h"
55 #include "rgb_macros.h"
56 #include "gui_thread.h"
57 #include "time_axis_view.h"
58 #include "grid_lines.h"
59 #include "ui_config.h"
64 using namespace ARDOUR;
67 using namespace Gtkmm2ext;
68 using namespace Editing;
71 Editor::remove_metric_marks ()
73 /* don't delete these while handling events, just punt till the GUI is idle */
75 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
76 delete_when_idle (*x);
78 metric_marks.clear ();
80 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
83 tempo_curves.clear ();
85 struct CurveComparator {
86 bool operator() (TempoCurve const * a, TempoCurve const * b) {
87 return a->tempo().sample() < b->tempo().sample();
91 Editor::draw_metric_marks (const Metrics& metrics)
94 TempoSection* prev_ts = 0;
95 double max_tempo = 0.0;
96 double min_tempo = DBL_MAX;
98 remove_metric_marks (); // also clears tempo curves
100 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
101 const MeterSection *ms;
104 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
105 snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
106 if (ms->position_lock_style() == MusicTime) {
107 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker music"), buf,
108 *(const_cast<MeterSection*>(ms))));
110 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
111 *(const_cast<MeterSection*>(ms))));
113 } else if ((ts = dynamic_cast<TempoSection*>(*i)) != 0) {
115 max_tempo = max (max_tempo, ts->note_types_per_minute());
116 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
117 min_tempo = min (min_tempo, ts->note_types_per_minute());
118 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
119 uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
121 tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
122 *(const_cast<TempoSection*>(ts)), ts->sample(), false));
124 const std::string tname (X_(""));
125 if (ts->position_lock_style() == MusicTime) {
126 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), tname,
127 *(const_cast<TempoSection*>(ts))));
129 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), tname,
130 *(const_cast<TempoSection*>(ts))));
132 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
133 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
135 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
141 tempo_curves.sort (CurveComparator());
143 const double min_tempo_range = 5.0;
144 const double tempo_delta = fabs (max_tempo - min_tempo);
146 if (tempo_delta < min_tempo_range) {
147 max_tempo += min_tempo_range - tempo_delta;
148 min_tempo += tempo_delta - min_tempo_range;
151 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
152 Curves::iterator tmp = x;
153 (*x)->set_max_tempo (max_tempo);
154 (*x)->set_min_tempo (min_tempo);
156 if (tmp != tempo_curves.end()) {
157 (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
159 (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
162 if (!(*x)->tempo().active()) {
171 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
172 TempoMarker* tempo_marker;
174 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
175 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
182 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
188 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
190 compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
192 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
193 update_tempo_based_rulers ();
195 maybe_draw_grid_lines ();
199 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
205 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
207 TempoSection* prev_ts = 0;
208 double max_tempo = 0.0;
209 double min_tempo = DBL_MAX;
211 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
212 TempoMarker* tempo_marker;
213 MeterMarker* meter_marker;
215 const MeterSection *ms;
217 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
218 if ((ts = &tempo_marker->tempo()) != 0) {
220 tempo_marker->set_position (ts->sample ());
222 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
223 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
225 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
228 max_tempo = max (max_tempo, ts->note_types_per_minute());
229 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
230 min_tempo = min (min_tempo, ts->note_types_per_minute());
231 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
236 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
237 if ((ms = &meter_marker->meter()) != 0) {
238 meter_marker->set_position (ms->sample ());
243 tempo_curves.sort (CurveComparator());
245 const double min_tempo_range = 5.0;
246 const double tempo_delta = fabs (max_tempo - min_tempo);
248 if (tempo_delta < min_tempo_range) {
249 max_tempo += min_tempo_range - tempo_delta;
250 min_tempo += tempo_delta - min_tempo_range;
253 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
254 Curves::iterator tmp = x;
255 (*x)->set_max_tempo (max_tempo);
256 (*x)->set_min_tempo (min_tempo);
258 if (tmp != tempo_curves.end()) {
259 (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
261 (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
264 if (!(*x)->tempo().active()) {
273 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
274 TempoMarker* tempo_marker;
275 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
276 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
280 compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
282 update_tempo_based_rulers ();
284 maybe_draw_grid_lines ();
288 Editor::redisplay_grid (bool immediate_redraw)
294 if (immediate_redraw) {
296 update_tempo_based_rulers ();
301 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_grid), true), false));
305 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
311 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
312 if (&(*x)->tempo() == ts) {
314 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
316 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
323 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
325 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, samplepos_t leftmost, samplepos_t rightmost)
331 /* prevent negative values of leftmost from creeping into tempomap
333 const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_sample (leftmost))) - 1.0;
334 switch (bbt_ruler_scale) {
336 case bbt_show_quarters:
337 case bbt_show_eighths:
338 case bbt_show_sixteenths:
339 case bbt_show_thirtyseconds:
340 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost);
344 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 1);
348 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 4);
352 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 16);
356 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 64);
361 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 128);
367 Editor::hide_grid_lines ()
375 Editor::maybe_draw_grid_lines ()
377 if ( _session == 0 ) {
381 if (grid_lines == 0) {
382 grid_lines = new GridLines (time_line_group, ArdourCanvas::LineSet::Vertical);
386 samplepos_t rightmost_sample = _leftmost_sample + current_page_samples();
388 if ( grid_musical() ) {
389 metric_get_bbt (grid_marks, _leftmost_sample, rightmost_sample, 12);
390 } else if (_grid_type== GridTypeTimecode) {
391 metric_get_timecode (grid_marks, _leftmost_sample, rightmost_sample, 12);
392 } else if (_grid_type == GridTypeCDFrame) {
393 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
394 } else if (_grid_type == GridTypeMinSec) {
395 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
398 grid_lines->draw ( grid_marks );
403 Editor::mouse_add_new_tempo_event (samplepos_t sample)
409 TempoMap& map(_session->tempo_map());
411 begin_reversible_command (_("add tempo mark"));
412 const double pulse = map.exact_qn_at_sample (sample, get_grid_music_divisions (0)) / 4.0;
415 XMLNode &before = map.get_state();
416 /* add music-locked ramped (?) tempo using the bpm/note type at sample*/
417 map.add_tempo (map.tempo_at_sample (sample), pulse, 0, MusicTime);
419 XMLNode &after = map.get_state();
420 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
421 commit_reversible_command ();
428 Editor::mouse_add_new_meter_event (samplepos_t sample)
435 TempoMap& map(_session->tempo_map());
436 MeterDialog meter_dialog (map, sample, _("add"));
438 switch (meter_dialog.run ()) {
439 case RESPONSE_ACCEPT:
445 double bpb = meter_dialog.get_bpb ();
446 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
448 double note_type = meter_dialog.get_note_type ();
450 Timecode::BBT_Time requested;
451 meter_dialog.get_bbt_time (requested);
453 const double al_sample = map.sample_at_bbt (requested);
454 begin_reversible_command (_("add meter mark"));
455 XMLNode &before = map.get_state();
457 if (meter_dialog.get_lock_style() == MusicTime) {
458 map.add_meter (Meter (bpb, note_type), requested, 0, MusicTime);
460 map.add_meter (Meter (bpb, note_type), requested, al_sample, AudioTime);
463 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
464 commit_reversible_command ();
470 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
472 ArdourMarker* marker;
473 TempoMarker* tempo_marker;
475 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
476 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
477 abort(); /*NOTREACHED*/
480 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
481 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
482 abort(); /*NOTREACHED*/
485 if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
486 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
491 Editor::edit_meter_section (MeterSection* section)
493 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
495 switch (meter_dialog.run()) {
496 case RESPONSE_ACCEPT:
502 double bpb = meter_dialog.get_bpb ();
503 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
505 double const note_type = meter_dialog.get_note_type ();
506 const Meter meter (bpb, note_type);
508 Timecode::BBT_Time when;
509 meter_dialog.get_bbt_time (when);
510 const samplepos_t sample = _session->tempo_map().sample_at_bbt (when);
511 const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
513 begin_reversible_command (_("replace meter mark"));
514 XMLNode &before = _session->tempo_map().get_state();
516 _session->tempo_map().replace_meter (*section, meter, when, sample, pls);
518 XMLNode &after = _session->tempo_map().get_state();
519 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
520 commit_reversible_command ();
524 Editor::edit_tempo_section (TempoSection* section)
526 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
528 switch (tempo_dialog.run ()) {
529 case RESPONSE_ACCEPT:
535 double bpm = tempo_dialog.get_bpm ();
536 double end_bpm = tempo_dialog.get_end_bpm ();
537 double nt = tempo_dialog.get_note_type ();
538 bpm = max (0.01, bpm);
539 const Tempo tempo (bpm, nt, end_bpm);
541 Timecode::BBT_Time when;
542 tempo_dialog.get_bbt_time (when);
544 begin_reversible_command (_("replace tempo mark"));
545 XMLNode &before = _session->tempo_map().get_state();
547 if (tempo_dialog.get_lock_style() == AudioTime) {
548 samplepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
549 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
551 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
552 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
555 XMLNode &after = _session->tempo_map().get_state();
556 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
557 commit_reversible_command ();
561 Editor::edit_tempo_marker (TempoMarker& tm)
563 edit_tempo_section (&tm.tempo());
567 Editor::edit_meter_marker (MeterMarker& mm)
569 edit_meter_section (&mm.meter());
573 Editor::real_remove_tempo_marker (TempoSection *section)
575 begin_reversible_command (_("remove tempo mark"));
576 XMLNode &before = _session->tempo_map().get_state();
577 _session->tempo_map().remove_tempo (*section, true);
578 XMLNode &after = _session->tempo_map().get_state();
579 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
580 commit_reversible_command ();
586 Editor::remove_meter_marker (ArdourCanvas::Item* item)
588 ArdourMarker* marker;
589 MeterMarker* meter_marker;
591 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
592 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
593 abort(); /*NOTREACHED*/
596 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
597 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
598 abort(); /*NOTREACHED*/
601 if (!meter_marker->meter().initial()) {
602 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
607 Editor::real_remove_meter_marker (MeterSection *section)
609 begin_reversible_command (_("remove tempo mark"));
610 XMLNode &before = _session->tempo_map().get_state();
611 _session->tempo_map().remove_meter (*section, true);
612 XMLNode &after = _session->tempo_map().get_state();
613 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
614 commit_reversible_command ();