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