tempo editing updates
[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();
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();
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                 compute_bbt_ruler_scale (leftmost_frame, leftmost_frame + current_page_samples());
303                 std::vector<TempoMap::BBTPoint> grid;
304
305                 if (bbt_ruler_scale != bbt_show_many) {
306                         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
307                 }
308
309                 draw_measures (grid);
310                 update_tempo_based_rulers (); // redraw rulers and measure lines
311
312         } else {
313                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
314         }
315 }
316 void
317 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
318 {
319         if (ts == 0) {
320                 return;
321         }
322
323         for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
324                 if (&(*x)->tempo() == ts) {
325                         if (yn) {
326                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
327                         } else {
328                                 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
329                         }
330                         break;
331                 }
332         }
333 }
334
335 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
336 void
337 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
338 {
339         if (!_session) {
340                 return;
341         }
342
343         /* prevent negative values of leftmost from creeping into tempomap
344          */
345         const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_frame (leftmost))) - 1.0;
346         switch (bbt_ruler_scale) {
347
348         case bbt_show_beats:
349         case bbt_show_ticks:
350         case bbt_show_ticks_detail:
351         case bbt_show_ticks_super_detail:
352                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
353                 break;
354
355         case bbt_show_1:
356                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 1);
357                 break;
358
359         case bbt_show_4:
360                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 4);
361                 break;
362
363         case bbt_show_16:
364                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 16);
365                 break;
366
367         case bbt_show_64:
368                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 64);
369                 break;
370
371         default:
372                 /* bbt_show_many */
373                 _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost, 128);
374                 break;
375         }
376 }
377
378 void
379 Editor::hide_measures ()
380 {
381         if (tempo_lines) {
382                 tempo_lines->hide();
383         }
384 }
385
386 void
387 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
388 {
389         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
390                 return;
391         }
392
393         if (tempo_lines == 0) {
394                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
395         }
396
397         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
398         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
399 }
400
401 void
402 Editor::mouse_add_new_tempo_event (framepos_t frame)
403 {
404         if (_session == 0) {
405                 return;
406         }
407
408         TempoMap& map(_session->tempo_map());
409
410         begin_reversible_command (_("add tempo mark"));
411         const double pulse = map.exact_qn_at_frame (frame, get_grid_music_divisions (0)) / 4.0;
412
413         if (pulse > 0.0) {
414                 XMLNode &before = map.get_state();
415                 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
416                 map.add_tempo (map.tempo_at_frame (frame), pulse, 0, MusicTime);
417
418                 XMLNode &after = map.get_state();
419                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
420                 commit_reversible_command ();
421         }
422
423         //map.dump (cerr);
424 }
425
426 void
427 Editor::mouse_add_new_meter_event (framepos_t frame)
428 {
429         if (_session == 0) {
430                 return;
431         }
432
433
434         TempoMap& map(_session->tempo_map());
435         MeterDialog meter_dialog (map, frame, _("add"));
436
437         switch (meter_dialog.run ()) {
438         case RESPONSE_ACCEPT:
439                 break;
440         default:
441                 return;
442         }
443
444         double bpb = meter_dialog.get_bpb ();
445         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
446
447         double note_type = meter_dialog.get_note_type ();
448
449         Timecode::BBT_Time requested;
450         meter_dialog.get_bbt_time (requested);
451
452         const double beat = map.beat_at_bbt (requested);
453         const double al_frame = map.frame_at_beat (beat);
454         begin_reversible_command (_("add meter mark"));
455         XMLNode &before = map.get_state();
456
457         if (meter_dialog.get_lock_style() == MusicTime) {
458                 map.add_meter (Meter (bpb, note_type), beat, requested, 0, MusicTime);
459         } else {
460                 map.add_meter (Meter (bpb, note_type), beat, requested, al_frame, AudioTime);
461         }
462
463         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
464         commit_reversible_command ();
465
466         //map.dump (cerr);
467 }
468
469 void
470 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
471 {
472         ArdourMarker* marker;
473         TempoMarker* tempo_marker;
474
475         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
476                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
477                 abort(); /*NOTREACHED*/
478         }
479
480         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
481                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
482                 abort(); /*NOTREACHED*/
483         }
484
485         if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
486                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
487         }
488 }
489
490 void
491 Editor::edit_meter_section (MeterSection* section)
492 {
493         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
494
495         switch (meter_dialog.run()) {
496         case RESPONSE_ACCEPT:
497                 break;
498         default:
499                 return;
500         }
501
502         double bpb = meter_dialog.get_bpb ();
503         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
504
505         double const note_type = meter_dialog.get_note_type ();
506         const Meter meter (bpb, note_type);
507
508         Timecode::BBT_Time when;
509         meter_dialog.get_bbt_time (when);
510         const framepos_t frame = _session->tempo_map().frame_at_bbt (when);
511         const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
512
513         begin_reversible_command (_("replace meter mark"));
514         XMLNode &before = _session->tempo_map().get_state();
515
516         _session->tempo_map().replace_meter (*section, meter, when, frame, pls);
517
518         XMLNode &after = _session->tempo_map().get_state();
519         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
520         commit_reversible_command ();
521 }
522
523 void
524 Editor::edit_tempo_section (TempoSection* section)
525 {
526         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
527
528         switch (tempo_dialog.run ()) {
529         case RESPONSE_ACCEPT:
530                 break;
531         default:
532                 return;
533         }
534
535         double bpm = tempo_dialog.get_bpm ();
536         double nt = tempo_dialog.get_note_type ();
537         bpm = max (0.01, bpm);
538         const Tempo tempo (bpm, nt);
539
540         Timecode::BBT_Time when;
541         tempo_dialog.get_bbt_time (when);
542
543         begin_reversible_command (_("replace tempo mark"));
544         XMLNode &before = _session->tempo_map().get_state();
545
546         if (tempo_dialog.get_lock_style() == AudioTime) {
547                 framepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
548                 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
549         } else {
550                 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
551                 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
552         }
553
554         XMLNode &after = _session->tempo_map().get_state();
555         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
556         commit_reversible_command ();
557 }
558
559 void
560 Editor::edit_tempo_marker (TempoMarker& tm)
561 {
562         edit_tempo_section (&tm.tempo());
563 }
564
565 void
566 Editor::edit_meter_marker (MeterMarker& mm)
567 {
568         edit_meter_section (&mm.meter());
569 }
570
571 gint
572 Editor::real_remove_tempo_marker (TempoSection *section)
573 {
574         begin_reversible_command (_("remove tempo mark"));
575         XMLNode &before = _session->tempo_map().get_state();
576         _session->tempo_map().remove_tempo (*section, true);
577         XMLNode &after = _session->tempo_map().get_state();
578         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
579         commit_reversible_command ();
580
581         return FALSE;
582 }
583
584 void
585 Editor::remove_meter_marker (ArdourCanvas::Item* item)
586 {
587         ArdourMarker* marker;
588         MeterMarker* meter_marker;
589
590         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
591                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
592                 abort(); /*NOTREACHED*/
593         }
594
595         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
596                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
597                 abort(); /*NOTREACHED*/
598         }
599
600         if (!meter_marker->meter().initial()) {
601           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
602         }
603 }
604
605 gint
606 Editor::real_remove_meter_marker (MeterSection *section)
607 {
608         begin_reversible_command (_("remove tempo mark"));
609         XMLNode &before = _session->tempo_map().get_state();
610         _session->tempo_map().remove_meter (*section, true);
611         XMLNode &after = _session->tempo_map().get_state();
612         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
613         commit_reversible_command ();
614
615         return FALSE;
616 }