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