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 "grid_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().sample() < b->tempo().sample();
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->sample(), 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().sample(), (*tmp)->tempo().sample());
152 (*x)->set_position ((*x)->tempo().sample(), 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);
183 compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
185 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
186 update_tempo_based_rulers ();
188 maybe_draw_grid_lines ();
192 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
198 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
200 TempoSection* prev_ts = 0;
201 double max_tempo = 0.0;
202 double min_tempo = DBL_MAX;
204 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
205 TempoMarker* tempo_marker;
206 MeterMarker* meter_marker;
208 const MeterSection *ms;
210 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
211 if ((ts = &tempo_marker->tempo()) != 0) {
213 tempo_marker->set_position (ts->sample ());
215 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
216 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
218 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
221 max_tempo = max (max_tempo, ts->note_types_per_minute());
222 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
223 min_tempo = min (min_tempo, ts->note_types_per_minute());
224 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
229 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
230 if ((ms = &meter_marker->meter()) != 0) {
231 meter_marker->set_position (ms->sample ());
236 tempo_curves.sort (CurveComparator());
238 const double min_tempo_range = 5.0;
239 const double tempo_delta = fabs (max_tempo - min_tempo);
241 if (tempo_delta < min_tempo_range) {
242 max_tempo += min_tempo_range - tempo_delta;
243 min_tempo += tempo_delta - min_tempo_range;
246 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
247 Curves::iterator tmp = x;
248 (*x)->set_max_tempo (max_tempo);
249 (*x)->set_min_tempo (min_tempo);
251 if (tmp != tempo_curves.end()) {
252 (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
254 (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
257 if (!(*x)->tempo().active()) {
266 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
267 TempoMarker* tempo_marker;
268 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
269 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
273 compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
275 update_tempo_based_rulers ();
277 maybe_draw_grid_lines ();
281 Editor::redisplay_grid (bool immediate_redraw)
287 if (immediate_redraw) {
289 update_tempo_based_rulers ();
294 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_grid), true), false));
298 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
304 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
305 if (&(*x)->tempo() == ts) {
307 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
309 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
316 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
318 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, samplepos_t leftmost, samplepos_t rightmost)
324 /* prevent negative values of leftmost from creeping into tempomap
326 const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_sample (leftmost))) - 1.0;
327 switch (bbt_ruler_scale) {
329 case bbt_show_quarters:
330 case bbt_show_eighths:
331 case bbt_show_sixteenths:
332 case bbt_show_thirtyseconds:
333 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost);
337 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 1);
341 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 4);
345 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 16);
349 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 64);
354 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 128);
360 Editor::hide_grid_lines ()
368 Editor::maybe_draw_grid_lines ()
370 if ( _session == 0 ) {
374 if (grid_lines == 0) {
375 grid_lines = new GridLines (time_line_group, ArdourCanvas::LineSet::Vertical);
379 samplepos_t rightmost_sample = _leftmost_sample + current_page_samples();
381 if ( grid_musical() ) {
382 metric_get_bbt (grid_marks, _leftmost_sample, rightmost_sample, 12);
383 } else if (_grid_type== GridTypeTimecode) {
384 metric_get_timecode (grid_marks, _leftmost_sample, rightmost_sample, 12);
385 } else if (_grid_type == GridTypeCDFrame) {
386 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
387 } else if (_grid_type == GridTypeMinSec) {
388 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
391 grid_lines->draw ( grid_marks );
396 Editor::mouse_add_new_tempo_event (samplepos_t sample)
402 TempoMap& map(_session->tempo_map());
404 begin_reversible_command (_("add tempo mark"));
405 const double pulse = map.exact_qn_at_sample (sample, get_grid_music_divisions (0)) / 4.0;
408 XMLNode &before = map.get_state();
409 /* add music-locked ramped (?) tempo using the bpm/note type at sample*/
410 map.add_tempo (map.tempo_at_sample (sample), pulse, 0, MusicTime);
412 XMLNode &after = map.get_state();
413 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
414 commit_reversible_command ();
421 Editor::mouse_add_new_meter_event (samplepos_t sample)
428 TempoMap& map(_session->tempo_map());
429 MeterDialog meter_dialog (map, sample, _("add"));
431 switch (meter_dialog.run ()) {
432 case RESPONSE_ACCEPT:
438 double bpb = meter_dialog.get_bpb ();
439 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
441 double note_type = meter_dialog.get_note_type ();
443 Timecode::BBT_Time requested;
444 meter_dialog.get_bbt_time (requested);
446 const double al_sample = map.sample_at_bbt (requested);
447 begin_reversible_command (_("add meter mark"));
448 XMLNode &before = map.get_state();
450 if (meter_dialog.get_lock_style() == MusicTime) {
451 map.add_meter (Meter (bpb, note_type), requested, 0, MusicTime);
453 map.add_meter (Meter (bpb, note_type), requested, al_sample, AudioTime);
456 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
457 commit_reversible_command ();
463 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
465 ArdourMarker* marker;
466 TempoMarker* tempo_marker;
468 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
469 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
470 abort(); /*NOTREACHED*/
473 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
474 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
475 abort(); /*NOTREACHED*/
478 if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
479 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
484 Editor::edit_meter_section (MeterSection* section)
486 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
488 switch (meter_dialog.run()) {
489 case RESPONSE_ACCEPT:
495 double bpb = meter_dialog.get_bpb ();
496 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
498 double const note_type = meter_dialog.get_note_type ();
499 const Meter meter (bpb, note_type);
501 Timecode::BBT_Time when;
502 meter_dialog.get_bbt_time (when);
503 const samplepos_t sample = _session->tempo_map().sample_at_bbt (when);
504 const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
506 begin_reversible_command (_("replace meter mark"));
507 XMLNode &before = _session->tempo_map().get_state();
509 _session->tempo_map().replace_meter (*section, meter, when, sample, pls);
511 XMLNode &after = _session->tempo_map().get_state();
512 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
513 commit_reversible_command ();
517 Editor::edit_tempo_section (TempoSection* section)
519 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
521 switch (tempo_dialog.run ()) {
522 case RESPONSE_ACCEPT:
528 double bpm = tempo_dialog.get_bpm ();
529 double end_bpm = tempo_dialog.get_end_bpm ();
530 double nt = tempo_dialog.get_note_type ();
531 bpm = max (0.01, bpm);
532 const Tempo tempo (bpm, nt, end_bpm);
534 Timecode::BBT_Time when;
535 tempo_dialog.get_bbt_time (when);
537 begin_reversible_command (_("replace tempo mark"));
538 XMLNode &before = _session->tempo_map().get_state();
540 if (tempo_dialog.get_lock_style() == AudioTime) {
541 samplepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
542 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
544 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
545 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
548 XMLNode &after = _session->tempo_map().get_state();
549 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
550 commit_reversible_command ();
554 Editor::edit_tempo_marker (TempoMarker& tm)
556 edit_tempo_section (&tm.tempo());
560 Editor::edit_meter_marker (MeterMarker& mm)
562 edit_meter_section (&mm.meter());
566 Editor::real_remove_tempo_marker (TempoSection *section)
568 begin_reversible_command (_("remove tempo mark"));
569 XMLNode &before = _session->tempo_map().get_state();
570 _session->tempo_map().remove_tempo (*section, true);
571 XMLNode &after = _session->tempo_map().get_state();
572 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
573 commit_reversible_command ();
579 Editor::remove_meter_marker (ArdourCanvas::Item* item)
581 ArdourMarker* marker;
582 MeterMarker* meter_marker;
584 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
585 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
586 abort(); /*NOTREACHED*/
589 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
590 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
591 abort(); /*NOTREACHED*/
594 if (!meter_marker->meter().initial()) {
595 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
600 Editor::real_remove_meter_marker (MeterSection *section)
602 begin_reversible_command (_("remove tempo mark"));
603 XMLNode &before = _session->tempo_map().get_state();
604 _session->tempo_map().remove_meter (*section, true);
605 XMLNode &after = _session->tempo_map().get_state();
606 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
607 commit_reversible_command ();