Tempo Marks : set point colour by discontinuity, display non-quarter pulse if we...
[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                         if (ts->note_type() != 4.0) {
109                                 snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->note_types_per_minute(), ts->note_type());
110                         } else {
111                                 snprintf (buf, sizeof (buf), "%.3f", ts->note_types_per_minute());
112                         }
113
114                         max_tempo = max (max_tempo, ts->note_types_per_minute());
115                         max_tempo = max (max_tempo, ts->end_note_types_per_minute());
116                         min_tempo = min (min_tempo, ts->note_types_per_minute());
117                         min_tempo = min (min_tempo, ts->end_note_types_per_minute());
118                         uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
119
120                         tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
121                                                                 *(const_cast<TempoSection*>(ts)), ts->frame(), false));
122
123                         if (ts->position_lock_style() == MusicTime) {
124                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), buf,
125                                                                  *(const_cast<TempoSection*>(ts))));
126                         } else {
127                                 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), buf,
128                                                                  *(const_cast<TempoSection*>(ts))));
129                         }
130                         if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
131                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
132                         } else {
133                                 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
134                         }
135                         prev_ts = ts;
136                 }
137
138         }
139         tempo_curves.sort (CurveComparator());
140
141         const double min_tempo_range = 5.0;
142         const double tempo_delta = fabs (max_tempo - min_tempo);
143
144         if (tempo_delta < min_tempo_range) {
145                 max_tempo += min_tempo_range - tempo_delta;
146                 min_tempo += tempo_delta - min_tempo_range;
147         }
148
149         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
150                 Curves::iterator tmp = x;
151                 (*x)->set_max_tempo (max_tempo);
152                 (*x)->set_min_tempo (min_tempo);
153                 ++tmp;
154                 if (tmp != tempo_curves.end()) {
155                         (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
156                 } else {
157                         (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
158                 }
159
160                 if (!(*x)->tempo().active()) {
161                         (*x)->hide();
162                 } else {
163                         (*x)->show();
164                 }
165
166                 ++x;
167         }
168
169         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
170                 TempoMarker* tempo_marker;
171
172                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
173                         tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
174                 }
175         }
176 }
177
178
179 void
180 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
181 {
182         if (!_session) {
183                 return;
184         }
185
186         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
187
188         if (tempo_lines) {
189                 tempo_lines->tempo_map_changed();
190         }
191
192         compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
193         std::vector<TempoMap::BBTPoint> grid;
194         if (bbt_ruler_scale != bbt_show_many) {
195                 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
196         }
197         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
198         draw_measures (grid);
199         update_tempo_based_rulers ();
200 }
201
202 void
203 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
204 {
205         if (!_session) {
206                 return;
207         }
208
209         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
210
211         if (tempo_lines) {
212                 tempo_lines->tempo_map_changed();
213         }
214
215         TempoSection* prev_ts = 0;
216         double max_tempo = 0.0;
217         double min_tempo = DBL_MAX;
218
219         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
220                 TempoMarker* tempo_marker;
221                 MeterMarker* meter_marker;
222                 TempoSection *ts;
223                 const MeterSection *ms;
224
225                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
226                         if ((ts = &tempo_marker->tempo()) != 0) {
227
228                                 tempo_marker->set_position (ts->frame ());
229                                 char buf[64];
230
231                                 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
232                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
233                                 } else {
234                                         tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
235                                 }
236
237                                 if (ts->note_type() != 4.0) {
238                                         snprintf (buf, sizeof (buf), "%.3f/%.0f", ts->note_types_per_minute(), ts->note_type());
239                                 } else {
240                                         snprintf (buf, sizeof (buf), "%.3f", ts->note_types_per_minute());
241                                 }
242
243                                 tempo_marker->set_name (buf);
244
245                                 max_tempo = max (max_tempo, ts->note_types_per_minute());
246                                 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
247                                 min_tempo = min (min_tempo, ts->note_types_per_minute());
248                                 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
249
250                                 prev_ts = ts;
251                         }
252                 }
253                 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
254                         if ((ms = &meter_marker->meter()) != 0) {
255                                 meter_marker->set_position (ms->frame ());
256                         }
257                 }
258         }
259
260         tempo_curves.sort (CurveComparator());
261
262         const double min_tempo_range = 5.0;
263         const double tempo_delta = fabs (max_tempo - min_tempo);
264
265         if (tempo_delta < min_tempo_range) {
266                 max_tempo += min_tempo_range - tempo_delta;
267                 min_tempo += tempo_delta - min_tempo_range;
268         }
269
270         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
271                 Curves::iterator tmp = x;
272                 (*x)->set_max_tempo (max_tempo);
273                 (*x)->set_min_tempo (min_tempo);
274                 ++tmp;
275                 if (tmp != tempo_curves.end()) {
276                         (*x)->set_position ((*x)->tempo().frame(), (*tmp)->tempo().frame());
277                 } else {
278                         (*x)->set_position ((*x)->tempo().frame(), UINT32_MAX);
279                 }
280
281                 if (!(*x)->tempo().active()) {
282                         (*x)->hide();
283                 } else {
284                         (*x)->show();
285                 }
286
287                 ++x;
288         }
289
290         for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
291                 TempoMarker* tempo_marker;
292                 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
293                         tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
294                 }
295         }
296
297         compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
298         std::vector<TempoMap::BBTPoint> grid;
299
300         if (bbt_ruler_scale != bbt_show_many) {
301                 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
302         }
303
304         draw_measures (grid);
305         update_tempo_based_rulers ();
306 }
307
308 void
309 Editor::redisplay_tempo (bool immediate_redraw)
310 {
311         if (!_session) {
312                 return;
313         }
314
315         if (immediate_redraw) {
316                 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
317                 std::vector<TempoMap::BBTPoint> grid;
318
319                 if (bbt_ruler_scale != bbt_show_many) {
320                         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
321                 }
322
323                 draw_measures (grid);
324                 update_tempo_based_rulers (); // redraw rulers and measure lines
325
326         } else {
327                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
328         }
329 }
330 void
331 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
332 {
333         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
334                 if (&(*x)->tempo() == ts) {
335                         (*x)->set_selected (yn);
336                         if (yn) {
337                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
338                         } else {
339                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
340                         }
341                         break;
342                 }
343         }
344 }
345
346 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
347 void
348 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
349 {
350         if (!_session) {
351                 return;
352         }
353
354         /* prevent negative values of leftmost from creeping into tempomap
355          */
356         const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_frame (leftmost))) - 1.0;
357         switch (bbt_ruler_scale) {
358
359         case bbt_show_beats:
360         case bbt_show_ticks:
361         case bbt_show_ticks_detail:
362         case bbt_show_ticks_super_detail:
363                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
364                 break;
365
366         case bbt_show_1:
367                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 1);
368                 break;
369
370         case bbt_show_4:
371                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 4);
372                 break;
373
374         case bbt_show_16:
375                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 16);
376                 break;
377
378         case bbt_show_64:
379                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 64);
380                 break;
381
382         default:
383                 /* bbt_show_many */
384                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 128);
385                 break;
386         }
387 }
388
389 void
390 Editor::hide_measures ()
391 {
392         if (tempo_lines) {
393                 tempo_lines->hide();
394         }
395 }
396
397 void
398 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
399 {
400         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
401                 return;
402         }
403
404         if (tempo_lines == 0) {
405                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
406         }
407
408         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
409         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
410 }
411
412 void
413 Editor::mouse_add_new_tempo_event (framepos_t frame)
414 {
415         if (_session == 0) {
416                 return;
417         }
418
419         TempoMap& map(_session->tempo_map());
420
421         begin_reversible_command (_("add tempo mark"));
422         const double pulse = map.exact_qn_at_frame (frame, get_grid_music_divisions (0)) / 4.0;
423
424         if (pulse > 0.0) {
425                 XMLNode &before = map.get_state();
426                 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
427                 map.add_tempo (map.tempo_at_frame (frame), pulse, 0, MusicTime);
428
429                 XMLNode &after = map.get_state();
430                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
431                 commit_reversible_command ();
432         }
433
434         //map.dump (cerr);
435 }
436
437 void
438 Editor::mouse_add_new_meter_event (framepos_t frame)
439 {
440         if (_session == 0) {
441                 return;
442         }
443
444
445         TempoMap& map(_session->tempo_map());
446         MeterDialog meter_dialog (map, frame, _("add"));
447
448         switch (meter_dialog.run ()) {
449         case RESPONSE_ACCEPT:
450                 break;
451         default:
452                 return;
453         }
454
455         double bpb = meter_dialog.get_bpb ();
456         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
457
458         double note_type = meter_dialog.get_note_type ();
459
460         Timecode::BBT_Time requested;
461         meter_dialog.get_bbt_time (requested);
462
463         const double beat = map.beat_at_bbt (requested);
464         const double al_frame = map.frame_at_beat (beat);
465         begin_reversible_command (_("add meter mark"));
466         XMLNode &before = map.get_state();
467
468         if (meter_dialog.get_lock_style() == MusicTime) {
469                 map.add_meter (Meter (bpb, note_type), beat, requested, 0, MusicTime);
470         } else {
471                 map.add_meter (Meter (bpb, note_type), beat, requested, al_frame, AudioTime);
472         }
473
474         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
475         commit_reversible_command ();
476
477         //map.dump (cerr);
478 }
479
480 void
481 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
482 {
483         ArdourMarker* marker;
484         TempoMarker* tempo_marker;
485
486         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
487                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
488                 abort(); /*NOTREACHED*/
489         }
490
491         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
492                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
493                 abort(); /*NOTREACHED*/
494         }
495
496         if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
497                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
498         }
499 }
500
501 void
502 Editor::edit_meter_section (MeterSection* section)
503 {
504         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
505
506         switch (meter_dialog.run()) {
507         case RESPONSE_ACCEPT:
508                 break;
509         default:
510                 return;
511         }
512
513         double bpb = meter_dialog.get_bpb ();
514         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
515
516         double const note_type = meter_dialog.get_note_type ();
517         const Meter meter (bpb, note_type);
518
519         Timecode::BBT_Time when;
520         meter_dialog.get_bbt_time (when);
521         const framepos_t frame = _session->tempo_map().frame_at_bbt (when);
522         const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
523
524         begin_reversible_command (_("replace meter mark"));
525         XMLNode &before = _session->tempo_map().get_state();
526
527         _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
528
529         XMLNode &after = _session->tempo_map().get_state();
530         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
531         commit_reversible_command ();
532 }
533
534 void
535 Editor::edit_tempo_section (TempoSection* section)
536 {
537         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
538
539         switch (tempo_dialog.run ()) {
540         case RESPONSE_ACCEPT:
541                 break;
542         default:
543                 return;
544         }
545
546         double bpm = tempo_dialog.get_bpm ();
547         double nt = tempo_dialog.get_note_type ();
548         bpm = max (0.01, bpm);
549         const Tempo tempo (bpm, nt);
550
551         Timecode::BBT_Time when;
552         tempo_dialog.get_bbt_time (when);
553
554         begin_reversible_command (_("replace tempo mark"));
555         XMLNode &before = _session->tempo_map().get_state();
556
557         if (tempo_dialog.get_lock_style() == AudioTime) {
558                 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
559                 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
560         } else {
561                 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
562                 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
563         }
564
565         XMLNode &after = _session->tempo_map().get_state();
566         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
567         commit_reversible_command ();
568 }
569
570 void
571 Editor::edit_tempo_marker (TempoMarker& tm)
572 {
573         edit_tempo_section (&tm.tempo());
574 }
575
576 void
577 Editor::edit_meter_marker (MeterMarker& mm)
578 {
579         edit_meter_section (&mm.meter());
580 }
581
582 gint
583 Editor::real_remove_tempo_marker (TempoSection *section)
584 {
585         begin_reversible_command (_("remove tempo mark"));
586         XMLNode &before = _session->tempo_map().get_state();
587         _session->tempo_map().remove_tempo (*section, true);
588         XMLNode &after = _session->tempo_map().get_state();
589         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
590         commit_reversible_command ();
591
592         return FALSE;
593 }
594
595 void
596 Editor::remove_meter_marker (ArdourCanvas::Item* item)
597 {
598         ArdourMarker* marker;
599         MeterMarker* meter_marker;
600
601         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
602                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
603                 abort(); /*NOTREACHED*/
604         }
605
606         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
607                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
608                 abort(); /*NOTREACHED*/
609         }
610
611         if (!meter_marker->meter().initial()) {
612           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
613         }
614 }
615
616 gint
617 Editor::real_remove_meter_marker (MeterSection *section)
618 {
619         begin_reversible_command (_("remove tempo mark"));
620         XMLNode &before = _session->tempo_map().get_state();
621         _session->tempo_map().remove_meter (*section, true);
622         XMLNode &after = _session->tempo_map().get_state();
623         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
624         commit_reversible_command ();
625
626         return FALSE;
627 }