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