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