Tempo ramps - allow live updating of tempo markers.
[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
74 void
75 Editor::draw_metric_marks (const Metrics& metrics)
76 {
77
78         const MeterSection *ms;
79         const TempoSection *ts;
80         char buf[64];
81
82         remove_metric_marks ();
83
84         for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
85
86                 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
87                         snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
88                         metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
89                                                                  *(const_cast<MeterSection*>(ms))));
90                 } else if ((ts = dynamic_cast<const TempoSection*>(*i)) != 0) {
91                         if (UIConfiguration::instance().get_allow_non_quarter_pulse()) {
92                                 snprintf (buf, sizeof (buf), "%.2f/%.0f", ts->beats_per_minute(), ts->note_type());
93                         } else {
94                                 snprintf (buf, sizeof (buf), "%.2f", ts->beats_per_minute());
95                         }
96                         metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), buf,
97                                                                  *(const_cast<TempoSection*>(ts))));
98                 }
99
100         }
101
102 }
103
104
105 void
106 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
107 {
108         if (!_session) {
109                 return;
110         }
111
112         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
113
114         if (tempo_lines) {
115                 tempo_lines->tempo_map_changed();
116         }
117
118         std::vector<TempoMap::BBTPoint> grid;
119
120         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
121         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
122         draw_measures (grid);
123         update_tempo_based_rulers (grid);
124 }
125
126 void
127 Editor::marker_position_changed ()
128 {
129 // yes its identical...
130         if (!_session) {
131                 return;
132         }
133
134         ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
135
136         if (tempo_lines) {
137                 tempo_lines->tempo_map_changed();
138         }
139
140         std::vector<TempoMap::BBTPoint> grid;
141         compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
142         _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
143         draw_measures (grid);
144         update_tempo_based_rulers (grid);
145 }
146
147 void
148 Editor::redisplay_tempo (bool immediate_redraw)
149 {
150         if (!_session) {
151                 return;
152         }
153
154         if (immediate_redraw) {
155                 std::vector<TempoMap::BBTPoint> grid;
156
157                 compute_current_bbt_points (grid, leftmost_frame, leftmost_frame + current_page_samples());
158                 draw_measures (grid);
159                 update_tempo_based_rulers (grid); // redraw rulers and measure lines
160
161         } else {
162                 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_tempo), true), false));
163         }
164 }
165
166 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
167 void
168 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, framepos_t leftmost, framepos_t rightmost)
169 {
170         if (!_session) {
171                 return;
172         }
173
174         framecnt_t beat_before_lower_pos = _session->tempo_map().frame_at_beat (floor(_session->tempo_map().beat_at_frame (leftmost)));
175         framecnt_t beat_after_upper_pos = _session->tempo_map().frame_at_beat (floor (_session->tempo_map().beat_at_frame (rightmost)) + 1.0);
176
177         /* prevent negative values of leftmost from creeping into tempomap
178          */
179         _session->tempo_map().get_grid (grid, max (beat_before_lower_pos, (framepos_t) 0), beat_after_upper_pos);
180 }
181
182 void
183 Editor::hide_measures ()
184 {
185         if (tempo_lines) {
186                 tempo_lines->hide();
187         }
188 }
189
190 void
191 Editor::draw_measures (std::vector<ARDOUR::TempoMap::BBTPoint>& grid)
192 {
193         if (_session == 0 || _show_measures == false || distance (grid.begin(), grid.end()) == 0) {
194                 return;
195         }
196
197         if (tempo_lines == 0) {
198                 tempo_lines = new TempoLines (time_line_group, ArdourCanvas::LineSet::Vertical);
199         }
200
201         const unsigned divisions = get_grid_beat_divisions(leftmost_frame);
202         tempo_lines->draw (grid, divisions, leftmost_frame, _session->frame_rate());
203 }
204
205 void
206 Editor::mouse_add_new_tempo_event (framepos_t frame)
207 {
208         if (_session == 0) {
209                 return;
210         }
211
212         TempoMap& map(_session->tempo_map());
213         TempoDialog tempo_dialog (map, frame, _("add"));
214
215         //this causes compiz to display no border.
216         //tempo_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &tempo_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
217
218         switch (tempo_dialog.run()) {
219         case RESPONSE_ACCEPT:
220                 break;
221         default:
222                 return;
223         }
224
225         double bpm = 0;
226         Timecode::BBT_Time requested;
227
228         bpm = tempo_dialog.get_bpm ();
229         double nt = tempo_dialog.get_note_type();
230         bpm = max (0.01, bpm);
231
232         tempo_dialog.get_bbt_time (requested);
233
234         begin_reversible_command (_("add tempo mark"));
235         XMLNode &before = map.get_state();
236         map.add_tempo (Tempo (bpm,nt), requested, tempo_dialog.get_tempo_type());
237         XMLNode &after = map.get_state();
238         _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
239         commit_reversible_command ();
240
241         //map.dump (cerr);
242 }
243
244 void
245 Editor::mouse_add_new_meter_event (framepos_t frame)
246 {
247         if (_session == 0) {
248                 return;
249         }
250
251
252         TempoMap& map(_session->tempo_map());
253         MeterDialog meter_dialog (map, frame, _("add"));
254
255         //this causes compiz to display no border..
256         //meter_dialog.signal_realize().connect (sigc::bind (sigc::ptr_fun (set_decoration), &meter_dialog, Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH)));
257
258         switch (meter_dialog.run ()) {
259         case RESPONSE_ACCEPT:
260                 break;
261         default:
262                 return;
263         }
264
265         double bpb = meter_dialog.get_bpb ();
266         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
267
268         double note_type = meter_dialog.get_note_type ();
269         Timecode::BBT_Time requested;
270
271         meter_dialog.get_bbt_time (requested);
272
273         begin_reversible_command (_("add meter mark"));
274         XMLNode &before = map.get_state();
275         map.add_meter (Meter (bpb, note_type), requested);
276         _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
277         commit_reversible_command ();
278
279         //map.dump (cerr);
280 }
281
282 void
283 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
284 {
285         ArdourMarker* marker;
286         TempoMarker* tempo_marker;
287
288         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
289                 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
290                 abort(); /*NOTREACHED*/
291         }
292
293         if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
294                 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
295                 abort(); /*NOTREACHED*/
296         }
297
298         if (tempo_marker->tempo().movable()) {
299                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
300         }
301 }
302
303 void
304 Editor::edit_meter_section (MeterSection* section)
305 {
306         MeterDialog meter_dialog (*section, _("done"));
307
308         switch (meter_dialog.run()) {
309         case RESPONSE_ACCEPT:
310                 break;
311         default:
312                 return;
313         }
314
315         double bpb = meter_dialog.get_bpb ();
316         bpb = max (1.0, bpb); // XXX is this a reasonable limit?
317
318         double note_type = meter_dialog.get_note_type ();
319
320         Timecode::BBT_Time when;
321         meter_dialog.get_bbt_time(when);
322
323         begin_reversible_command (_("replace tempo mark"));
324         XMLNode &before = _session->tempo_map().get_state();
325         _session->tempo_map().replace_meter (*section, Meter (bpb, note_type), when);
326         XMLNode &after = _session->tempo_map().get_state();
327         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
328         commit_reversible_command ();
329 }
330
331 void
332 Editor::edit_tempo_section (TempoSection* section)
333 {
334         TempoDialog tempo_dialog (*section, _("done"));
335
336         switch (tempo_dialog.run ()) {
337         case RESPONSE_ACCEPT:
338                 break;
339         default:
340                 return;
341         }
342
343         double bpm = tempo_dialog.get_bpm ();
344         double nt = tempo_dialog.get_note_type ();
345         Timecode::BBT_Time when;
346         tempo_dialog.get_bbt_time(when);
347         bpm = max (0.01, bpm);
348
349         begin_reversible_command (_("replace tempo mark"));
350         XMLNode &before = _session->tempo_map().get_state();
351         _session->tempo_map().replace_tempo (*section, Tempo (bpm, nt), when, tempo_dialog.get_tempo_type());
352         XMLNode &after = _session->tempo_map().get_state();
353         _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
354         commit_reversible_command ();
355 }
356
357 void
358 Editor::edit_tempo_marker (TempoMarker& tm)
359 {
360         edit_tempo_section (&tm.tempo());
361 }
362
363 void
364 Editor::edit_meter_marker (MeterMarker& mm)
365 {
366         edit_meter_section (&mm.meter());
367 }
368
369 gint
370 Editor::real_remove_tempo_marker (TempoSection *section)
371 {
372         begin_reversible_command (_("remove tempo mark"));
373         XMLNode &before = _session->tempo_map().get_state();
374         _session->tempo_map().remove_tempo (*section, true);
375         XMLNode &after = _session->tempo_map().get_state();
376         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
377         commit_reversible_command ();
378
379         return FALSE;
380 }
381
382 void
383 Editor::remove_meter_marker (ArdourCanvas::Item* item)
384 {
385         ArdourMarker* marker;
386         MeterMarker* meter_marker;
387
388         if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
389                 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
390                 abort(); /*NOTREACHED*/
391         }
392
393         if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
394                 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
395                 abort(); /*NOTREACHED*/
396         }
397
398         if (meter_marker->meter().movable()) {
399           Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
400         }
401 }
402
403 gint
404 Editor::real_remove_meter_marker (MeterSection *section)
405 {
406         begin_reversible_command (_("remove tempo mark"));
407         XMLNode &before = _session->tempo_map().get_state();
408         _session->tempo_map().remove_meter (*section, true);
409         XMLNode &after = _session->tempo_map().get_state();
410         _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
411         commit_reversible_command ();
412
413         return FALSE;
414 }