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