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 max_tempo = max (max_tempo, ts->note_types_per_minute());
109 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
110 min_tempo = min (min_tempo, ts->note_types_per_minute());
111 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
112 uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
114 tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
115 *(const_cast<TempoSection*>(ts)), ts->frame(), false));
117 const std::string tname (X_(""));
118 if (ts->position_lock_style() == MusicTime) {
119 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), tname,
120 *(const_cast<TempoSection*>(ts))));
122 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), tname,
123 *(const_cast<TempoSection*>(ts))));
125 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
126 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
128 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
134 tempo_curves.sort (CurveComparator());
136 const double min_tempo_range = 5.0;
137 const double tempo_delta = fabs (max_tempo - min_tempo);
139 if (tempo_delta < min_tempo_range) {
140 max_tempo += min_tempo_range - tempo_delta;
141 min_tempo += tempo_delta - min_tempo_range;
144 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
145 Curves::iterator tmp = x;
146 (*x)->set_max_tempo (max_tempo);
147 (*x)->set_min_tempo (min_tempo);
149 if (tmp != tempo_curves.end()) {
150 (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
152 (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
155 if (!(*x)->tempo().active()) {
164 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
165 TempoMarker* tempo_marker;
167 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
168 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
175 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
181 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
184 tempo_lines->tempo_map_changed();
187 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
188 std::vector<TempoMap::BBTPoint> grid;
189 if (bbt_ruler_scale != bbt_show_many) {
190 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
192 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
193 draw_measures (grid);
194 update_tempo_based_rulers ();
198 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
204 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
207 tempo_lines->tempo_map_changed();
210 TempoSection* prev_ts = 0;
211 double max_tempo = 0.0;
212 double min_tempo = DBL_MAX;
214 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
215 TempoMarker* tempo_marker;
216 MeterMarker* meter_marker;
218 const MeterSection *ms;
220 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
221 if ((ts = &tempo_marker->tempo()) != 0) {
223 tempo_marker->set_position (ts->frame ());
225 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
226 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
228 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
231 max_tempo = max (max_tempo, ts->note_types_per_minute());
232 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
233 min_tempo = min (min_tempo, ts->note_types_per_minute());
234 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
239 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
240 if ((ms = &meter_marker->meter()) != 0) {
241 meter_marker->set_position (ms->frame ());
246 tempo_curves.sort (CurveComparator());
248 const double min_tempo_range = 5.0;
249 const double tempo_delta = fabs (max_tempo - min_tempo);
251 if (tempo_delta < min_tempo_range) {
252 max_tempo += min_tempo_range - tempo_delta;
253 min_tempo += tempo_delta - min_tempo_range;
256 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
257 Curves::iterator tmp = x;
258 (*x)->set_max_tempo (max_tempo);
259 (*x)->set_min_tempo (min_tempo);
261 if (tmp != tempo_curves.end()) {
262 (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
264 (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
267 if (!(*x)->tempo().active()) {
276 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
277 TempoMarker* tempo_marker;
278 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
279 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
283 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
284 std::vector<TempoMap::BBTPoint> grid;
286 if (bbt_ruler_scale != bbt_show_many) {
287 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
290 draw_measures (grid);
291 update_tempo_based_rulers ();
295 Editor::redisplay_tempo (bool immediate_redraw)
301 if (immediate_redraw) {
303 //only recalculate bbt_ruler_scale on a zoom or snap-change; not every redraw; if a case is found where this is necessary, uncomment this line.
304 // compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
306 std::vector<TempoMap::BBTPoint> grid;
308 if (bbt_ruler_scale != bbt_show_many) {
309 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
312 draw_measures (grid);
313 update_tempo_based_rulers (); // redraw rulers and measure lines
316 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
320 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
326 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
327 if (&(*x)->tempo() == ts) {
329 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
331 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
338 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
340 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
346 /* prevent negative values of leftmost from creeping into tempomap
348 const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_frame (leftmost))) - 1.0;
349 switch (bbt_ruler_scale) {
353 case bbt_show_ticks_detail:
354 case bbt_show_ticks_super_detail:
355 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
359 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 1);
363 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 4);
367 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 16);
371 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 64);
376 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 128);
382 Editor::hide_measures ()
390 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
392 if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
396 if (tempo_lines == 0) {
397 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
400 const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
401 tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
405 Editor::mouse_add_new_tempo_event (framepos_t frame)
411 TempoMap& map(_session->tempo_map());
413 begin_reversible_command (_("add tempo mark"));
414 const double pulse = map.exact_qn_at_frame (frame, get_grid_music_divisions (0)) / 4.0;
417 XMLNode &before = map.get_state();
418 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
419 map.add_tempo (map.tempo_at_frame (frame), pulse, 0, MusicTime);
421 XMLNode &after = map.get_state();
422 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
423 commit_reversible_command ();
430 Editor::mouse_add_new_meter_event (framepos_t frame)
437 TempoMap& map(_session->tempo_map());
438 MeterDialog meter_dialog (map, frame, _("add"));
440 switch (meter_dialog.run ()) {
441 case RESPONSE_ACCEPT:
447 double bpb = meter_dialog.get_bpb ();
448 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
450 double note_type = meter_dialog.get_note_type ();
452 Timecode::BBT_Time requested;
453 meter_dialog.get_bbt_time (requested);
455 const double beat = map.beat_at_bbt (requested);
456 const double al_frame = map.frame_at_beat (beat);
457 begin_reversible_command (_("add meter mark"));
458 XMLNode &before = map.get_state();
460 if (meter_dialog.get_lock_style() == MusicTime) {
461 map.add_meter (Meter (bpb, note_type), beat, requested, 0, MusicTime);
463 map.add_meter (Meter (bpb, note_type), beat, requested, al_frame, AudioTime);
466 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
467 commit_reversible_command ();
473 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
475 ArdourMarker* marker;
476 TempoMarker* tempo_marker;
478 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
479 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
480 abort(); /*NOTREACHED*/
483 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
484 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
485 abort(); /*NOTREACHED*/
488 if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
489 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
494 Editor::edit_meter_section (MeterSection* section)
496 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
498 switch (meter_dialog.run()) {
499 case RESPONSE_ACCEPT:
505 double bpb = meter_dialog.get_bpb ();
506 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
508 double const note_type = meter_dialog.get_note_type ();
509 const Meter meter (bpb, note_type);
511 Timecode::BBT_Time when;
512 meter_dialog.get_bbt_time (when);
513 const framepos_t frame = _session->tempo_map().frame_at_bbt (when);
514 const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
516 begin_reversible_command (_("replace meter mark"));
517 XMLNode &before = _session->tempo_map().get_state();
519 _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
521 XMLNode &after = _session->tempo_map().get_state();
522 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
523 commit_reversible_command ();
527 Editor::edit_tempo_section (TempoSection* section)
529 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
531 switch (tempo_dialog.run ()) {
532 case RESPONSE_ACCEPT:
538 double bpm = tempo_dialog.get_bpm ();
539 double end_bpm = tempo_dialog.get_end_bpm ();
540 double nt = tempo_dialog.get_note_type ();
541 bpm = max (0.01, bpm);
542 const Tempo tempo (bpm, nt, end_bpm);
544 Timecode::BBT_Time when;
545 tempo_dialog.get_bbt_time (when);
547 begin_reversible_command (_("replace tempo mark"));
548 XMLNode &before = _session->tempo_map().get_state();
550 if (tempo_dialog.get_lock_style() == AudioTime) {
551 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
552 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
554 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
555 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
558 XMLNode &after = _session->tempo_map().get_state();
559 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
560 commit_reversible_command ();
564 Editor::edit_tempo_marker (TempoMarker& tm)
566 edit_tempo_section (&tm.tempo());
570 Editor::edit_meter_marker (MeterMarker& mm)
572 edit_meter_section (&mm.meter());
576 Editor::real_remove_tempo_marker (TempoSection *section)
578 begin_reversible_command (_("remove tempo mark"));
579 XMLNode &before = _session->tempo_map().get_state();
580 _session->tempo_map().remove_tempo (*section, true);
581 XMLNode &after = _session->tempo_map().get_state();
582 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
583 commit_reversible_command ();
589 Editor::remove_meter_marker (ArdourCanvas::Item* item)
591 ArdourMarker* marker;
592 MeterMarker* meter_marker;
594 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
595 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
596 abort(); /*NOTREACHED*/
599 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
600 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
601 abort(); /*NOTREACHED*/
604 if (!meter_marker->meter().initial()) {
605 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
610 Editor::real_remove_meter_marker (MeterSection *section)
612 begin_reversible_command (_("remove tempo mark"));
613 XMLNode &before = _session->tempo_map().get_state();
614 _session->tempo_map().remove_meter (*section, true);
615 XMLNode &after = _session->tempo_map().get_state();
616 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
617 commit_reversible_command ();