2 Copyright (C) 2002 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.
21 #include "gtk2ardour-config.h"
24 #include <cstdio> // for sprintf, grrr
30 #include "pbd/error.h"
31 #include "pbd/memento_command.h"
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/gtk_ui.h>
36 #include "ardour/session.h"
37 #include "ardour/tempo.h"
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/utils.h>
41 #include "canvas/canvas.h"
42 #include "canvas/item.h"
43 #include "canvas/line_set.h"
47 #include "tempo_dialog.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50 #include "time_axis_view.h"
51 #include "tempo_lines.h"
52 #include "ui_config.h"
57 using namespace ARDOUR;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
64 Editor::remove_metric_marks ()
66 /* don't delete these while handling events, just punt till the GUI is idle */
68 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
69 delete_when_idle (*x);
71 metric_marks.clear ();
73 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
76 tempo_curves.clear ();
78 struct CurveComparator {
79 bool operator() (TempoCurve const * a, TempoCurve const * b) {
80 return a->tempo().frame() < b->tempo().frame();
84 Editor::draw_metric_marks (const Metrics& metrics)
87 TempoSection* prev_ts = 0;
88 double max_tempo = 0.0;
89 double min_tempo = DBL_MAX;
91 remove_metric_marks (); // also clears tempo curves
93 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
94 const MeterSection *ms;
97 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
98 snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
99 if (ms->position_lock_style() == MusicTime) {
100 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker music"), buf,
101 *(const_cast<MeterSection*>(ms))));
103 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
104 *(const_cast<MeterSection*>(ms))));
106 } else if ((ts = dynamic_cast<TempoSection*>(*i)) != 0) {
108 if (ts->type() == TempoSection::Constant) {
109 if (ts->note_type() != 4.0) {
110 snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->note_types_per_minute(), ts->note_type());
112 snprintf (buf, sizeof (buf), "%.3f", ts->note_types_per_minute());
115 if (ts->note_type() != 4.0) {
116 snprintf (buf, sizeof (buf), "%.3f/%.0f>%.3f", ts->note_types_per_minute(), ts->note_type(), ts->end_note_types_per_minute());
118 snprintf (buf, sizeof (buf), "%.3f>%.3f", ts->note_types_per_minute(), ts->end_note_types_per_minute());
121 max_tempo = max (max_tempo, ts->note_types_per_minute());
122 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
123 min_tempo = min (min_tempo, ts->note_types_per_minute());
124 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
125 uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
127 tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
128 *(const_cast<TempoSection*>(ts)), ts->frame(), false));
130 if (ts->position_lock_style() == MusicTime) {
131 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), buf,
132 *(const_cast<TempoSection*>(ts))));
134 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), buf,
135 *(const_cast<TempoSection*>(ts))));
137 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
138 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
140 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
146 tempo_curves.sort (CurveComparator());
148 const double min_tempo_range = 5.0;
149 const double tempo_delta = fabs (max_tempo - min_tempo);
151 if (tempo_delta < min_tempo_range) {
152 max_tempo += min_tempo_range - tempo_delta;
153 min_tempo += tempo_delta - min_tempo_range;
156 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
157 Curves::iterator tmp = x;
158 (*x)->set_max_tempo (max_tempo);
159 (*x)->set_min_tempo (min_tempo);
161 if (tmp != tempo_curves.end()) {
162 (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
164 (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
167 if (!(*x)->tempo().active()) {
176 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
177 TempoMarker* tempo_marker;
179 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
180 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
187 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
193 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
196 tempo_lines->tempo_map_changed();
199 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
200 std::vector<TempoMap::BBTPoint> grid;
201 if (bbt_ruler_scale != bbt_show_many) {
202 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
204 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
205 draw_measures (grid);
206 update_tempo_based_rulers ();
210 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
216 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
219 tempo_lines->tempo_map_changed();
222 TempoSection* prev_ts = 0;
223 double max_tempo = 0.0;
224 double min_tempo = DBL_MAX;
226 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
227 TempoMarker* tempo_marker;
228 MeterMarker* meter_marker;
230 const MeterSection *ms;
232 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
233 if ((ts = &tempo_marker->tempo()) != 0) {
235 tempo_marker->set_position (ts->frame ());
238 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
239 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
241 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
244 if (ts->type() == TempoSection::Constant) {
245 if (ts->note_type() != 4.0) {
246 snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->note_types_per_minute(), ts->note_type());
248 snprintf (buf, sizeof (buf), "%.3f", ts->note_types_per_minute());
251 if (ts->note_type() != 4.0) {
252 snprintf (buf, sizeof (buf), "%.3f/%.0f>%.3f", ts->note_types_per_minute(), ts->note_type(), ts->end_note_types_per_minute());
254 snprintf (buf, sizeof (buf), "%.3f>%.3f", ts->note_types_per_minute(), ts->end_note_types_per_minute());
258 tempo_marker->set_name (buf);
260 max_tempo = max (max_tempo, ts->note_types_per_minute());
261 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
262 min_tempo = min (min_tempo, ts->note_types_per_minute());
263 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
268 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
269 if ((ms = &meter_marker->meter()) != 0) {
270 meter_marker->set_position (ms->frame ());
275 tempo_curves.sort (CurveComparator());
277 const double min_tempo_range = 5.0;
278 const double tempo_delta = fabs (max_tempo - min_tempo);
280 if (tempo_delta < min_tempo_range) {
281 max_tempo += min_tempo_range - tempo_delta;
282 min_tempo += tempo_delta - min_tempo_range;
285 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
286 Curves::iterator tmp = x;
287 (*x)->set_max_tempo (max_tempo);
288 (*x)->set_min_tempo (min_tempo);
290 if (tmp != tempo_curves.end()) {
291 (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
293 (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
296 if (!(*x)->tempo().active()) {
305 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
306 TempoMarker* tempo_marker;
307 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
308 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
312 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
313 std::vector<TempoMap::BBTPoint> grid;
315 if (bbt_ruler_scale != bbt_show_many) {
316 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
319 draw_measures (grid);
320 update_tempo_based_rulers ();
324 Editor::redisplay_tempo (bool immediate_redraw)
330 if (immediate_redraw) {
331 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
332 std::vector<TempoMap::BBTPoint> grid;
334 if (bbt_ruler_scale != bbt_show_many) {
335 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
338 draw_measures (grid);
339 update_tempo_based_rulers (); // redraw rulers and measure lines
342 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
346 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
348 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
349 if (&(*x)->tempo() == ts) {
351 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
353 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
360 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
362 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
368 /* prevent negative values of leftmost from creeping into tempomap
370 const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_frame (leftmost))) - 1.0;
371 switch (bbt_ruler_scale) {
375 case bbt_show_ticks_detail:
376 case bbt_show_ticks_super_detail:
377 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
381 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 1);
385 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 4);
389 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 16);
393 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 64);
398 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 128);
404 Editor::hide_measures ()
412 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
414 if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
418 if (tempo_lines == 0) {
419 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
422 const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
423 tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
427 Editor::mouse_add_new_tempo_event (framepos_t frame)
433 TempoMap& map(_session->tempo_map());
435 begin_reversible_command (_("add tempo mark"));
436 const double pulse = map.exact_qn_at_frame (frame, get_grid_music_divisions (0)) / 4.0;
439 XMLNode &before = map.get_state();
440 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
441 map.add_tempo (map.tempo_at_frame (frame), pulse, 0, MusicTime);
443 XMLNode &after = map.get_state();
444 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
445 commit_reversible_command ();
452 Editor::mouse_add_new_meter_event (framepos_t frame)
459 TempoMap& map(_session->tempo_map());
460 MeterDialog meter_dialog (map, frame, _("add"));
462 switch (meter_dialog.run ()) {
463 case RESPONSE_ACCEPT:
469 double bpb = meter_dialog.get_bpb ();
470 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
472 double note_type = meter_dialog.get_note_type ();
474 Timecode::BBT_Time requested;
475 meter_dialog.get_bbt_time (requested);
477 const double beat = map.beat_at_bbt (requested);
478 const double al_frame = map.frame_at_beat (beat);
479 begin_reversible_command (_("add meter mark"));
480 XMLNode &before = map.get_state();
482 if (meter_dialog.get_lock_style() == MusicTime) {
483 map.add_meter (Meter (bpb, note_type), beat, requested, 0, MusicTime);
485 map.add_meter (Meter (bpb, note_type), beat, requested, al_frame, AudioTime);
488 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
489 commit_reversible_command ();
495 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
497 ArdourMarker* marker;
498 TempoMarker* tempo_marker;
500 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
501 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
502 abort(); /*NOTREACHED*/
505 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
506 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
507 abort(); /*NOTREACHED*/
510 if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
511 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
516 Editor::edit_meter_section (MeterSection* section)
518 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
520 switch (meter_dialog.run()) {
521 case RESPONSE_ACCEPT:
527 double bpb = meter_dialog.get_bpb ();
528 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
530 double const note_type = meter_dialog.get_note_type ();
531 const Meter meter (bpb, note_type);
533 Timecode::BBT_Time when;
534 meter_dialog.get_bbt_time (when);
535 const framepos_t frame = _session->tempo_map().frame_at_bbt (when);
536 const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
538 begin_reversible_command (_("replace meter mark"));
539 XMLNode &before = _session->tempo_map().get_state();
541 _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
543 XMLNode &after = _session->tempo_map().get_state();
544 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
545 commit_reversible_command ();
549 Editor::edit_tempo_section (TempoSection* section)
551 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
553 switch (tempo_dialog.run ()) {
554 case RESPONSE_ACCEPT:
560 double bpm = tempo_dialog.get_bpm ();
561 double nt = tempo_dialog.get_note_type ();
562 bpm = max (0.01, bpm);
563 const Tempo tempo (bpm, nt);
565 Timecode::BBT_Time when;
566 tempo_dialog.get_bbt_time (when);
568 begin_reversible_command (_("replace tempo mark"));
569 XMLNode &before = _session->tempo_map().get_state();
571 if (tempo_dialog.get_lock_style() == AudioTime) {
572 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
573 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
575 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
576 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
579 XMLNode &after = _session->tempo_map().get_state();
580 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
581 commit_reversible_command ();
585 Editor::edit_tempo_marker (TempoMarker& tm)
587 edit_tempo_section (&tm.tempo());
591 Editor::edit_meter_marker (MeterMarker& mm)
593 edit_meter_section (&mm.meter());
597 Editor::real_remove_tempo_marker (TempoSection *section)
599 begin_reversible_command (_("remove tempo mark"));
600 XMLNode &before = _session->tempo_map().get_state();
601 _session->tempo_map().remove_tempo (*section, true);
602 XMLNode &after = _session->tempo_map().get_state();
603 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
604 commit_reversible_command ();
610 Editor::remove_meter_marker (ArdourCanvas::Item* item)
612 ArdourMarker* marker;
613 MeterMarker* meter_marker;
615 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
616 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
617 abort(); /*NOTREACHED*/
620 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
621 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
622 abort(); /*NOTREACHED*/
625 if (!meter_marker->meter().initial()) {
626 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
631 Editor::real_remove_meter_marker (MeterSection *section)
633 begin_reversible_command (_("remove tempo mark"));
634 XMLNode &before = _session->tempo_map().get_state();
635 _session->tempo_map().remove_meter (*section, true);
636 XMLNode &after = _session->tempo_map().get_state();
637 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
638 commit_reversible_command ();