Tempo ramps - more bbt dragging work.
[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         const double lower_beat = floor (_session->tempo_map().beat_at_frame (leftmost));
248         _session->tempo_map().get_grid (grid, max (_session->tempo_map().frame_at_beat (lower_beat), (framepos_t) 0), rightmost);
249 }
250
251 void
252 Editor::hide_measures ()
253 {
254         if (tempo_lines) {
255                 tempo_lines->hide();
256         }
257 }
258
259 void
260 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
261 {
262         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
263                 return;
264         }
265
266         if (tempo_lines == 0) {
267                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
268         }
269
270         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
271         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
272 }
273
274 void
275 Editor::mouse_add_new_tempo_event (framepos_t frame)
276 {
277         if (_session == 0) {
278                 return;
279         }
280
281         TempoMap& map(_session->tempo_map());
282
283         begin_reversible_command (_("add tempo mark"));
284         const double pulse = map.pulse_at_frame (frame);
285
286         if (pulse > 0.0) {
287                 XMLNode &before = map.get_state();
288                 /* add music-locked ramped (?) tempo using the bpm/note type at frame*/
289                 map.add_tempo (map.tempo_at (frame), pulse, TempoSection::Ramp);
290
291                 XMLNode &after = map.get_state();
292                 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
293                 commit_reversible_command ();
294         }
295
296         //map.dump (cerr);
297 }
298
299 void
300 Editor::mouse_add_new_meter_event (framepos_t frame)
301 {
302         if (_session == 0) {
303                 return;
304         }
305
306
307         TempoMap& map(_session->tempo_map());
308         MeterDialog meter_dialog (map, frame, _("add"));
309
310         switch (meter_dialog.run ()) {
311         case RESPONSE_ACCEPT:
312                 break;
313         default:
314                 return;
315         }
316
317         double bpb = meter_dialog.get_bpb ();
318         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
319
320         double note_type = meter_dialog.get_note_type ();
321         Timecode::BBT_Time requested;
322
323         meter_dialog.get_bbt_time (requested);
324
325         begin_reversible_command (_("add meter mark"));
326         XMLNode &before = map.get_state();
327
328         if (meter_dialog.get_lock_style() == MusicTime) {
329                 map.add_meter (Meter (bpb, note_type), map.bbt_to_beats (requested), requested);
330         } else {
331                 map.add_meter (Meter (bpb, note_type), map.frame_time (requested), map.bbt_to_beats (requested), requested);
332         }
333
334         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
335         commit_reversible_command ();
336
337         //map.dump (cerr);
338 }
339
340 void
341 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
342 {
343         ArdourMarker* marker;
344         TempoMarker* tempo_marker;
345
346         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
347                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
348                 abort(); /*NOTREACHED*/
349         }
350
351         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
352                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
353                 abort(); /*NOTREACHED*/
354         }
355
356         if (tempo_marker->tempo().movable()) {
357                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
358         }
359 }
360
361 void
362 Editor::edit_meter_section (MeterSection* section)
363 {
364         MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
365
366         switch (meter_dialog.run()) {
367         case RESPONSE_ACCEPT:
368                 break;
369         default:
370                 return;
371         }
372
373         double bpb = meter_dialog.get_bpb ();
374         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
375
376         double const note_type = meter_dialog.get_note_type ();
377         Timecode::BBT_Time when;
378         meter_dialog.get_bbt_time(when);
379         framepos_t const frame = _session->tempo_map().frame_at_beat (_session->tempo_map().bbt_to_beats (when));
380
381         begin_reversible_command (_("replace meter mark"));
382         XMLNode &before = _session->tempo_map().get_state();
383         section->set_position_lock_style (meter_dialog.get_lock_style());
384         if (meter_dialog.get_lock_style() == MusicTime) {
385                 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
386         } else {
387                 _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), frame);
388         }
389         XMLNode &after = _session->tempo_map().get_state();
390         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
391         commit_reversible_command ();
392 }
393
394 void
395 Editor::edit_tempo_section (TempoSection* section)
396 {
397         TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
398
399         switch (tempo_dialog.run ()) {
400         case RESPONSE_ACCEPT:
401                 break;
402         default:
403                 return;
404         }
405
406         double bpm = tempo_dialog.get_bpm ();
407         double nt = tempo_dialog.get_note_type ();
408         Timecode::BBT_Time when;
409
410         tempo_dialog.get_bbt_time (when);
411
412         bpm = max (0.01, bpm);
413
414         begin_reversible_command (_("replace tempo mark"));
415         XMLNode &before = _session->tempo_map().get_state();
416
417         if (tempo_dialog.get_lock_style() == MusicTime) {
418                 section->set_position_lock_style (MusicTime);
419                 framepos_t const f = _session->tempo_map().predict_tempo_frame (section, when);
420                 double const p = _session->tempo_map().predict_tempo_pulse (section, f);
421                 _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), p, tempo_dialog.get_tempo_type());
422         } else {
423                 framepos_t const f = _session->tempo_map().predict_tempo_frame (section, when);
424                 _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), f, tempo_dialog.get_tempo_type());
425         }
426
427         XMLNode &after = _session->tempo_map().get_state();
428         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
429         commit_reversible_command ();
430 }
431
432 void
433 Editor::edit_tempo_marker (TempoMarker& tm)
434 {
435         edit_tempo_section (&tm.tempo());
436 }
437
438 void
439 Editor::edit_meter_marker (MeterMarker& mm)
440 {
441         edit_meter_section (&mm.meter());
442 }
443
444 gint
445 Editor::real_remove_tempo_marker (TempoSection *section)
446 {
447         begin_reversible_command (_("remove tempo mark"));
448         XMLNode &before = _session->tempo_map().get_state();
449         _session->tempo_map().remove_tempo (*section, true);
450         XMLNode &after = _session->tempo_map().get_state();
451         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
452         commit_reversible_command ();
453
454         return FALSE;
455 }
456
457 void
458 Editor::remove_meter_marker (ArdourCanvas::Item* item)
459 {
460         ArdourMarker* marker;
461         MeterMarker* meter_marker;
462
463         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
464                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
465                 abort(); /*NOTREACHED*/
466         }
467
468         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
469                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
470                 abort(); /*NOTREACHED*/
471         }
472
473         if (meter_marker->meter().movable()) {
474           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
475         }
476 }
477
478 gint
479 Editor::real_remove_meter_marker (MeterSection *section)
480 {
481         begin_reversible_command (_("remove tempo mark"));
482         XMLNode &before = _session->tempo_map().get_state();
483         _session->tempo_map().remove_meter (*section, true);
484         XMLNode &after = _session->tempo_map().get_state();
485         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
486         commit_reversible_command ();
487
488         return FALSE;
489 }