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