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