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 double max_tempo = 0.0;
88 double min_tempo = DBL_MAX;
90 remove_metric_marks (); // also clears tempo curves
92 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
93 const MeterSection *ms;
94 const TempoSection *ts;
96 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
97 snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
98 if (ms->position_lock_style() == MusicTime) {
99 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker music"), buf,
100 *(const_cast<MeterSection*>(ms))));
102 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
103 *(const_cast<MeterSection*>(ms))));
105 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
106 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
107 snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->note_types_per_minute(), ts->note_type());
109 snprintf (buf, sizeof (buf), "%.3f", ts->note_types_per_minute());
112 max_tempo = max (max_tempo, ts->note_types_per_minute());
113 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
114 min_tempo = min (min_tempo, ts->note_types_per_minute());
115 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
116 uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
118 tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
119 *(const_cast<TempoSection*>(ts)), ts->frame(), false));
121 if (ts->position_lock_style() == MusicTime) {
122 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), buf,
123 *(const_cast<TempoSection*>(ts))));
125 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), buf,
126 *(const_cast<TempoSection*>(ts))));
131 tempo_curves.sort (CurveComparator());
133 const double min_tempo_range = 5.0;
134 const double tempo_delta = fabs (max_tempo - min_tempo);
136 if (tempo_delta < min_tempo_range) {
137 max_tempo += min_tempo_range - tempo_delta;
138 min_tempo += tempo_delta - min_tempo_range;
141 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
142 Curves::iterator tmp = x;
143 (*x)->set_max_tempo (max_tempo);
144 (*x)->set_min_tempo (min_tempo);
146 if (tmp != tempo_curves.end()) {
147 (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
149 (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
152 if (!(*x)->tempo().active()) {
161 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
162 TempoMarker* tempo_marker;
164 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
165 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
172 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
178 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
181 tempo_lines->tempo_map_changed();
184 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
185 std::vector<TempoMap::BBTPoint> grid;
186 if (bbt_ruler_scale != bbt_show_many) {
187 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
189 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
190 draw_measures (grid);
191 update_tempo_based_rulers ();
195 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
201 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
204 tempo_lines->tempo_map_changed();
207 double max_tempo = 0.0;
208 double min_tempo = DBL_MAX;
210 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
211 TempoMarker* tempo_marker;
212 MeterMarker* meter_marker;
213 const TempoSection *ts;
214 const MeterSection *ms;
216 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
217 if ((ts = &tempo_marker->tempo()) != 0) {
218 tempo_marker->set_position (ts->frame ());
221 if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
222 snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->note_types_per_minute(), ts->note_type());
224 snprintf (buf, sizeof (buf), "%.3f", ts->note_types_per_minute());
227 tempo_marker->set_name (buf);
229 max_tempo = max (max_tempo, ts->note_types_per_minute());
230 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
231 min_tempo = min (min_tempo, ts->note_types_per_minute());
232 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
235 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
236 if ((ms = &meter_marker->meter()) != 0) {
237 meter_marker->set_position (ms->frame ());
242 tempo_curves.sort (CurveComparator());
244 const double min_tempo_range = 5.0;
245 const double tempo_delta = fabs (max_tempo - min_tempo);
247 if (tempo_delta < min_tempo_range) {
248 max_tempo += min_tempo_range - tempo_delta;
249 min_tempo += tempo_delta - min_tempo_range;
252 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
253 Curves::iterator tmp = x;
254 (*x)->set_max_tempo (max_tempo);
255 (*x)->set_min_tempo (min_tempo);
257 if (tmp != tempo_curves.end()) {
258 (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
260 (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
263 if (!(*x)->tempo().active()) {
272 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
273 TempoMarker* tempo_marker;
274 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
275 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
279 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
280 std::vector<TempoMap::BBTPoint> grid;
282 if (bbt_ruler_scale != bbt_show_many) {
283 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
286 draw_measures (grid);
287 update_tempo_based_rulers ();
291 Editor::redisplay_tempo (bool immediate_redraw)
297 if (immediate_redraw) {
298 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
299 std::vector<TempoMap::BBTPoint> grid;
301 if (bbt_ruler_scale != bbt_show_many) {
302 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
305 draw_measures (grid);
306 update_tempo_based_rulers (); // redraw rulers and measure lines
309 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
313 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
315 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
316 if (&(*x)->tempo() == ts && yn) {
317 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
319 } else if (&(*x)->tempo() == ts) {
320 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
326 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
328 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
334 /* prevent negative values of leftmost from creeping into tempomap
336 const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_frame (leftmost))) - 1.0;
337 switch (bbt_ruler_scale) {
341 case bbt_show_ticks_detail:
342 case bbt_show_ticks_super_detail:
343 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
347 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 1);
351 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 4);
355 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 16);
359 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 64);
364 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 128);
370 Editor::hide_measures ()
378 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
380 if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
384 if (tempo_lines == 0) {
385 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
388 const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
389 tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
393 Editor::mouse_add_new_tempo_event (framepos_t frame)
399 TempoMap& map(_session->tempo_map());
401 begin_reversible_command (_("add tempo mark"));
402 const double pulse = map.exact_qn_at_frame (frame, get_grid_music_divisions (0)) / 4.0;
405 XMLNode &before = map.get_state();
406 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
407 map.add_tempo (map.tempo_at_frame (frame), pulse, 0, MusicTime);
409 XMLNode &after = map.get_state();
410 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
411 commit_reversible_command ();
418 Editor::mouse_add_new_meter_event (framepos_t frame)
425 TempoMap& map(_session->tempo_map());
426 MeterDialog meter_dialog (map, frame, _("add"));
428 switch (meter_dialog.run ()) {
429 case RESPONSE_ACCEPT:
435 double bpb = meter_dialog.get_bpb ();
436 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
438 double note_type = meter_dialog.get_note_type ();
440 Timecode::BBT_Time requested;
441 meter_dialog.get_bbt_time (requested);
443 const double beat = map.beat_at_bbt (requested);
444 const double al_frame = map.frame_at_beat (beat);
445 begin_reversible_command (_("add meter mark"));
446 XMLNode &before = map.get_state();
448 if (meter_dialog.get_lock_style() == MusicTime) {
449 map.add_meter (Meter (bpb, note_type), beat, requested, 0, MusicTime);
451 map.add_meter (Meter (bpb, note_type), beat, requested, al_frame, AudioTime);
454 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
455 commit_reversible_command ();
461 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
463 ArdourMarker* marker;
464 TempoMarker* tempo_marker;
466 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
467 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
468 abort(); /*NOTREACHED*/
471 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
472 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
473 abort(); /*NOTREACHED*/
476 if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
477 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
482 Editor::edit_meter_section (MeterSection* section)
484 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
486 switch (meter_dialog.run()) {
487 case RESPONSE_ACCEPT:
493 double bpb = meter_dialog.get_bpb ();
494 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
496 double const note_type = meter_dialog.get_note_type ();
497 const Meter meter (bpb, note_type);
499 Timecode::BBT_Time when;
500 meter_dialog.get_bbt_time (when);
501 const framepos_t frame = _session->tempo_map().frame_at_bbt (when);
502 const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
504 begin_reversible_command (_("replace meter mark"));
505 XMLNode &before = _session->tempo_map().get_state();
507 _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
509 XMLNode &after = _session->tempo_map().get_state();
510 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
511 commit_reversible_command ();
515 Editor::edit_tempo_section (TempoSection* section)
517 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
519 switch (tempo_dialog.run ()) {
520 case RESPONSE_ACCEPT:
526 double bpm = tempo_dialog.get_bpm ();
527 double nt = tempo_dialog.get_note_type ();
528 bpm = max (0.01, bpm);
529 const Tempo tempo (bpm, nt);
531 Timecode::BBT_Time when;
532 tempo_dialog.get_bbt_time (when);
534 begin_reversible_command (_("replace tempo mark"));
535 XMLNode &before = _session->tempo_map().get_state();
537 if (tempo_dialog.get_lock_style() == AudioTime) {
538 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
539 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
541 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
542 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
545 XMLNode &after = _session->tempo_map().get_state();
546 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
547 commit_reversible_command ();
551 Editor::edit_tempo_marker (TempoMarker& tm)
553 edit_tempo_section (&tm.tempo());
557 Editor::edit_meter_marker (MeterMarker& mm)
559 edit_meter_section (&mm.meter());
563 Editor::real_remove_tempo_marker (TempoSection *section)
565 begin_reversible_command (_("remove tempo mark"));
566 XMLNode &before = _session->tempo_map().get_state();
567 _session->tempo_map().remove_tempo (*section, true);
568 XMLNode &after = _session->tempo_map().get_state();
569 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
570 commit_reversible_command ();
576 Editor::remove_meter_marker (ArdourCanvas::Item* item)
578 ArdourMarker* marker;
579 MeterMarker* meter_marker;
581 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
582 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
583 abort(); /*NOTREACHED*/
586 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
587 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
588 abort(); /*NOTREACHED*/
591 if (!meter_marker->meter().initial()) {
592 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
597 Editor::real_remove_meter_marker (MeterSection *section)
599 begin_reversible_command (_("remove tempo mark"));
600 XMLNode &before = _session->tempo_map().get_state();
601 _session->tempo_map().remove_meter (*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 ();