::find_next_event() does not need to be a pure virtual in ControlSet (it can originat...
[ardour.git] / gtk2_ardour / editor_tempodisplay.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdio> // for sprintf, grrr
25 #include <cstdlib>
26 #include <cmath>
27 #include <string>
28 #include <climits>
29
30 #include "pbd/error.h"
31 #include "pbd/memento_command.h"
32
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/gtk_ui.h>
35
36 #include "ardour/session.h"
37 #include "ardour/tempo.h"
38 #include <gtkmm2ext/doi.h>
39 #include <gtkmm2ext/utils.h>
40
41 #include "canvas/canvas.h"
42 #include "canvas/item.h"
43 #include "canvas/line_set.h"
44
45 #include "editor.h"
46 #include "marker.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"
53
54 #include "pbd/i18n.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Gtk;
60 using namespace Gtkmm2ext;
61 using namespace Editing;
62
63 void
64 Editor::remove_metric_marks ()
65 {
66         /* don't delete these while handling events, just punt till the GUI is idle */
67
68         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
69                 delete_when_idle (*x);
70         }
71         metric_marks.clear ();
72
73         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
74                 delete (*x);
75         }
76         tempo_curves.clear ();
77 }
78 struct CurveComparator {
79         bool operator() (TempoCurve const * a, TempoCurve const * b) {
80                 return a->tempo().frame() < b->tempo().frame();
81         }
82 };
83 void
84 Editor::draw_metric_marks (const Metrics& metrics)
85 {
86         char buf[64];
87         TempoSection* prev_ts = 0;
88         double max_tempo = 0.0;
89         double min_tempo = DBL_MAX;
90
91         remove_metric_marks (); // also clears tempo curves
92
93         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
94                 const MeterSection *ms;
95                 TempoSection *ts;
96
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))));
102                         } else {
103                                 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
104                                                                          *(const_cast<MeterSection*>(ms))));
105                         }
106                 } else if ((ts = dynamic_cast<TempoSection*>(*i)) != 0) {
107
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");
113
114                         tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
115                                                                 *(const_cast<TempoSection*>(ts)), ts->frame(), false));
116
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))));
121                         } else {
122                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), tname,
123                                                                  *(const_cast<TempoSection*>(ts))));
124                         }
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"));
127                         } else {
128                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
129                         }
130                         prev_ts = ts;
131                 }
132
133         }
134         tempo_curves.sort (CurveComparator());
135
136         const double min_tempo_range = 5.0;
137         const double tempo_delta = fabs (max_tempo - min_tempo);
138
139         if (tempo_delta < min_tempo_range) {
140                 max_tempo += min_tempo_range - tempo_delta;
141                 min_tempo += tempo_delta - min_tempo_range;
142         }
143
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);
148                 ++tmp;
149                 if (tmp != tempo_curves.end()) {
150                         (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
151                 } else {
152                         (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
153                 }
154
155                 if (!(*x)->tempo().active()) {
156                         (*x)->hide();
157                 } else {
158                         (*x)->show();
159                 }
160
161                 ++x;
162         }
163
164         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
165                 TempoMarker* tempo_marker;
166
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));
169                 }
170         }
171 }
172
173
174 void
175 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
176 {
177         if (!_session) {
178                 return;
179         }
180
181         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
182
183         if (tempo_lines) {
184                 tempo_lines->tempo_map_changed(_session->tempo_map().music_origin());
185         }
186
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());
191         }
192         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
193         draw_measures (grid);
194         update_tempo_based_rulers ();
195 }
196
197 void
198 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
199 {
200         if (!_session) {
201                 return;
202         }
203
204         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
205
206         if (tempo_lines) {
207                 tempo_lines->tempo_map_changed(_session->tempo_map().music_origin());
208         }
209
210         TempoSection* prev_ts = 0;
211         double max_tempo = 0.0;
212         double min_tempo = DBL_MAX;
213
214         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
215                 TempoMarker* tempo_marker;
216                 MeterMarker* meter_marker;
217                 TempoSection *ts;
218                 const MeterSection *ms;
219
220                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
221                         if ((ts = &tempo_marker->tempo()) != 0) {
222
223                                 tempo_marker->set_position (ts->frame ());
224
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"));
227                                 } else {
228                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
229                                 }
230
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());
235
236                                 prev_ts = ts;
237                         }
238                 }
239                 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
240                         if ((ms = &meter_marker->meter()) != 0) {
241                                 meter_marker->set_position (ms->frame ());
242                         }
243                 }
244         }
245
246         tempo_curves.sort (CurveComparator());
247
248         const double min_tempo_range = 5.0;
249         const double tempo_delta = fabs (max_tempo - min_tempo);
250
251         if (tempo_delta < min_tempo_range) {
252                 max_tempo += min_tempo_range - tempo_delta;
253                 min_tempo += tempo_delta - min_tempo_range;
254         }
255
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);
260                 ++tmp;
261                 if (tmp != tempo_curves.end()) {
262                         (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
263                 } else {
264                         (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
265                 }
266
267                 if (!(*x)->tempo().active()) {
268                         (*x)->hide();
269                 } else {
270                         (*x)->show();
271                 }
272
273                 ++x;
274         }
275
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));
280                 }
281         }
282
283         compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
284         std::vector<TempoMap::BBTPoint> grid;
285
286         if (bbt_ruler_scale != bbt_show_many) {
287                 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
288         }
289
290         draw_measures (grid);
291         update_tempo_based_rulers ();
292 }
293
294 void
295 Editor::redisplay_tempo (bool immediate_redraw)
296 {
297         if (!_session) {
298                 return;
299         }
300
301         if (immediate_redraw) {
302
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());
305
306                 std::vector<TempoMap::BBTPoint> grid;
307
308                 if (bbt_ruler_scale != bbt_show_many) {
309                         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
310                 }
311
312                 draw_measures (grid);
313                 update_tempo_based_rulers (); // redraw rulers and measure lines
314
315         } else {
316                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
317         }
318 }
319 void
320 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
321 {
322         if (ts == 0) {
323                 return;
324         }
325
326         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
327                 if (&(*x)->tempo() == ts) {
328                         if (yn) {
329                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
330                         } else {
331                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
332                         }
333                         break;
334                 }
335         }
336 }
337
338 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
339 void
340 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
341 {
342         if (!_session) {
343                 return;
344         }
345
346         /* prevent negative values of leftmost from creeping into tempomap
347          */
348         const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_frame (leftmost))) - 1.0;
349         switch (bbt_ruler_scale) {
350
351         case bbt_show_beats:
352         case bbt_show_ticks:
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);
356                 break;
357
358         case bbt_show_1:
359                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 1);
360                 break;
361
362         case bbt_show_4:
363                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 4);
364                 break;
365
366         case bbt_show_16:
367                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 16);
368                 break;
369
370         case bbt_show_64:
371                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 64);
372                 break;
373
374         default:
375                 /* bbt_show_many */
376                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 128);
377                 break;
378         }
379 }
380
381 void
382 Editor::hide_measures ()
383 {
384         if (tempo_lines) {
385                 tempo_lines->hide();
386         }
387 }
388
389 void
390 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
391 {
392         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
393                 return;
394         }
395
396         if (tempo_lines == 0) {
397                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical, new BeatsFramesConverter (_session->tempo_map(), _session->tempo_map().music_origin()));
398         }
399
400         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
401         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
402 }
403
404 void
405 Editor::mouse_add_new_tempo_event (framepos_t frame)
406 {
407         if (_session == 0) {
408                 return;
409         }
410
411         TempoMap& map(_session->tempo_map());
412
413         begin_reversible_command (_("add tempo mark"));
414         const double pulse = map.exact_qn_at_frame (frame, get_grid_music_divisions (0)) / 4.0;
415
416         if (pulse > 0.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);
420
421                 XMLNode &after = map.get_state();
422                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
423                 commit_reversible_command ();
424         }
425
426         //map.dump (cerr);
427 }
428
429 void
430 Editor::mouse_add_new_meter_event (framepos_t frame)
431 {
432         if (_session == 0) {
433                 return;
434         }
435
436
437         TempoMap& map(_session->tempo_map());
438         MeterDialog meter_dialog (map, frame, _("add"));
439
440         switch (meter_dialog.run ()) {
441         case RESPONSE_ACCEPT:
442                 break;
443         default:
444                 return;
445         }
446
447         double bpb = meter_dialog.get_bpb ();
448         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
449
450         double note_type = meter_dialog.get_note_type ();
451
452         Timecode::BBT_Time requested;
453         meter_dialog.get_bbt_time (requested);
454
455         const double al_frame = map.frame_at_bbt (requested);
456         begin_reversible_command (_("add meter mark"));
457         XMLNode &before = map.get_state();
458
459         if (meter_dialog.get_lock_style() == MusicTime) {
460                 map.add_meter (Meter (bpb, note_type), requested, 0, MusicTime);
461         } else {
462                 map.add_meter (Meter (bpb, note_type), requested, al_frame, AudioTime);
463         }
464
465         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
466         commit_reversible_command ();
467
468         //map.dump (cerr);
469 }
470
471 void
472 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
473 {
474         ArdourMarker* marker;
475         TempoMarker* tempo_marker;
476
477         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
478                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
479                 abort(); /*NOTREACHED*/
480         }
481
482         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
483                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
484                 abort(); /*NOTREACHED*/
485         }
486
487         if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
488                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
489         }
490 }
491
492 void
493 Editor::edit_meter_section (MeterSection* section)
494 {
495         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
496
497         switch (meter_dialog.run()) {
498         case RESPONSE_ACCEPT:
499                 break;
500         default:
501                 return;
502         }
503
504         double bpb = meter_dialog.get_bpb ();
505         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
506
507         double const note_type = meter_dialog.get_note_type ();
508         const Meter meter (bpb, note_type);
509
510         Timecode::BBT_Time when;
511         meter_dialog.get_bbt_time (when);
512         const framepos_t frame = _session->tempo_map().frame_at_bbt (when);
513         const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
514
515         begin_reversible_command (_("replace meter mark"));
516         XMLNode &before = _session->tempo_map().get_state();
517
518         _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
519
520         XMLNode &after = _session->tempo_map().get_state();
521         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
522         commit_reversible_command ();
523 }
524
525 void
526 Editor::edit_tempo_section (TempoSection* section)
527 {
528         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
529
530         switch (tempo_dialog.run ()) {
531         case RESPONSE_ACCEPT:
532                 break;
533         default:
534                 return;
535         }
536
537         double bpm = tempo_dialog.get_bpm ();
538         double end_bpm = tempo_dialog.get_end_bpm ();
539         double nt = tempo_dialog.get_note_type ();
540         bpm = max (0.01, bpm);
541         const Tempo tempo (bpm, nt, end_bpm);
542
543         Timecode::BBT_Time when;
544         tempo_dialog.get_bbt_time (when);
545
546         begin_reversible_command (_("replace tempo mark"));
547         XMLNode &before = _session->tempo_map().get_state();
548
549         if (tempo_dialog.get_lock_style() == AudioTime) {
550                 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
551                 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
552         } else {
553                 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
554                 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
555         }
556
557         XMLNode &after = _session->tempo_map().get_state();
558         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
559         commit_reversible_command ();
560 }
561
562 void
563 Editor::edit_tempo_marker (TempoMarker& tm)
564 {
565         edit_tempo_section (&tm.tempo());
566 }
567
568 void
569 Editor::edit_meter_marker (MeterMarker& mm)
570 {
571         edit_meter_section (&mm.meter());
572 }
573
574 gint
575 Editor::real_remove_tempo_marker (TempoSection *section)
576 {
577         begin_reversible_command (_("remove tempo mark"));
578         XMLNode &before = _session->tempo_map().get_state();
579         _session->tempo_map().remove_tempo (*section, true);
580         XMLNode &after = _session->tempo_map().get_state();
581         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
582         commit_reversible_command ();
583
584         return FALSE;
585 }
586
587 void
588 Editor::remove_meter_marker (ArdourCanvas::Item* item)
589 {
590         ArdourMarker* marker;
591         MeterMarker* meter_marker;
592
593         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
594                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
595                 abort(); /*NOTREACHED*/
596         }
597
598         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
599                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
600                 abort(); /*NOTREACHED*/
601         }
602
603         if (!meter_marker->meter().initial()) {
604           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
605         }
606 }
607
608 gint
609 Editor::real_remove_meter_marker (MeterSection *section)
610 {
611         begin_reversible_command (_("remove tempo mark"));
612         XMLNode &before = _session->tempo_map().get_state();
613         _session->tempo_map().remove_meter (*section, true);
614         XMLNode &after = _session->tempo_map().get_state();
615         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
616         commit_reversible_command ();
617
618         return FALSE;
619 }