1 /*
2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006-2015 David Robillard <d@drobilla.net>
5 * Copyright (C) 2006-2015 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
7 * Copyright (C) 2008-2011 Carl Hetherington <carl@carlh.net>
8 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2015-2017 Nick Mainsbridge <mainsbridge@gmail.com>
10 * Copyright (C) 2017-2018 Ben Loftis <ben@harrisonconsoles.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 #ifdef WAF_BUILD
28 #include "gtk2ardour-config.h"
29 #endif
30
31 #include <cstdio> // for sprintf, grrr
32 #include <cstdlib>
33 #include <cmath>
34 #include <string>
35 #include <climits>
36
37 #include "pbd/error.h"
38 #include "pbd/memento_command.h"
39
40 #include <gtkmm2ext/utils.h>
41 #include <gtkmm2ext/gtk_ui.h>
42
43 #include "ardour/session.h"
44 #include "ardour/tempo.h"
45 #include <gtkmm2ext/doi.h>
46 #include <gtkmm2ext/utils.h>
47
48 #include "canvas/canvas.h"
49 #include "canvas/item.h"
50 #include "canvas/line_set.h"
51
52 #include "editor.h"
53 #include "marker.h"
54 #include "tempo_dialog.h"
55 #include "rgb_macros.h"
56 #include "gui_thread.h"
57 #include "time_axis_view.h"
58 #include "grid_lines.h"
59 #include "ui_config.h"
60
61 #include "pbd/i18n.h"
62
63 using namespace std;
64 using namespace ARDOUR;
65 using namespace PBD;
66 using namespace Gtk;
67 using namespace Gtkmm2ext;
68 using namespace Editing;
69
70 void
remove_metric_marks()71 Editor::remove_metric_marks ()
72 {
73 /* don't delete these while handling events, just punt till the GUI is idle */
74
75 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
76 delete_when_idle (*x);
77 }
78 metric_marks.clear ();
79
80 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
81 delete (*x);
82 }
83 tempo_curves.clear ();
84 }
85 struct CurveComparator {
operator ()CurveComparator86 bool operator() (TempoCurve const * a, TempoCurve const * b) {
87 return a->tempo().sample() < b->tempo().sample();
88 }
89 };
90 void
draw_metric_marks(const Metrics & metrics)91 Editor::draw_metric_marks (const Metrics& metrics)
92 {
93 char buf[64];
94 TempoSection* prev_ts = 0;
95 double max_tempo = 0.0;
96 double min_tempo = DBL_MAX;
97
98 remove_metric_marks (); // also clears tempo curves
99
100 for (Metrics::const_iterator i = metrics.begin(); i != metrics.end(); ++i) {
101 const MeterSection *ms;
102 TempoSection *ts;
103
104 if ((ms = dynamic_cast<const MeterSection*>(*i)) != 0) {
105 snprintf (buf, sizeof(buf), "%g/%g", ms->divisions_per_bar(), ms->note_divisor ());
106 if (ms->position_lock_style() == MusicTime) {
107 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker music"), buf,
108 *(const_cast<MeterSection*>(ms))));
109 } else {
110 metric_marks.push_back (new MeterMarker (*this, *meter_group, UIConfiguration::instance().color ("meter marker"), buf,
111 *(const_cast<MeterSection*>(ms))));
112 }
113 } else if ((ts = dynamic_cast<TempoSection*>(*i)) != 0) {
114
115 max_tempo = max (max_tempo, ts->note_types_per_minute());
116 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
117 min_tempo = min (min_tempo, ts->note_types_per_minute());
118 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
119 uint32_t const tc_color = UIConfiguration::instance().color ("tempo curve");
120
121 tempo_curves.push_back (new TempoCurve (*this, *tempo_group, tc_color,
122 *(const_cast<TempoSection*>(ts)), ts->sample(), false));
123
124 const std::string tname (X_(""));
125 if (ts->position_lock_style() == MusicTime) {
126 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker music"), tname,
127 *(const_cast<TempoSection*>(ts))));
128 } else {
129 metric_marks.push_back (new TempoMarker (*this, *tempo_group, UIConfiguration::instance().color ("tempo marker"), tname,
130 *(const_cast<TempoSection*>(ts))));
131 }
132 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
133 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
134 } else {
135 metric_marks.back()->set_points_color (UIConfiguration::instance().color ("tempo marker"));
136 }
137 prev_ts = ts;
138 }
139
140 }
141 tempo_curves.sort (CurveComparator());
142
143 const double min_tempo_range = 5.0;
144 const double tempo_delta = fabs (max_tempo - min_tempo);
145
146 if (tempo_delta < min_tempo_range) {
147 max_tempo += min_tempo_range - tempo_delta;
148 min_tempo += tempo_delta - min_tempo_range;
149 }
150
151 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
152 Curves::iterator tmp = x;
153 (*x)->set_max_tempo (max_tempo);
154 (*x)->set_min_tempo (min_tempo);
155 ++tmp;
156 if (tmp != tempo_curves.end()) {
157 (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
158 } else {
159 (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
160 }
161
162 if (!(*x)->tempo().active()) {
163 (*x)->hide();
164 } else {
165 (*x)->show();
166 }
167
168 ++x;
169 }
170
171 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
172 TempoMarker* tempo_marker;
173
174 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
175 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (10.0, max_tempo - min_tempo));
176 }
177 }
178 }
179
180
181 void
tempo_map_changed(const PropertyChange &)182 Editor::tempo_map_changed (const PropertyChange& /*ignored*/)
183 {
184 if (!_session) {
185 return;
186 }
187
188 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed, ignored);
189
190 compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
191
192 _session->tempo_map().apply_with_metrics (*this, &Editor::draw_metric_marks); // redraw metric markers
193 update_tempo_based_rulers ();
194
195 maybe_draw_grid_lines ();
196 }
197
198 void
tempometric_position_changed(const PropertyChange &)199 Editor::tempometric_position_changed (const PropertyChange& /*ignored*/)
200 {
201 if (!_session) {
202 return;
203 }
204
205 ENSURE_GUI_THREAD (*this, &Editor::tempo_map_changed);
206
207 TempoSection* prev_ts = 0;
208 double max_tempo = 0.0;
209 double min_tempo = DBL_MAX;
210
211 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
212 TempoMarker* tempo_marker;
213 MeterMarker* meter_marker;
214 TempoSection *ts;
215 const MeterSection *ms;
216
217 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
218 if ((ts = &tempo_marker->tempo()) != 0) {
219
220 tempo_marker->set_position (ts->sample ());
221
222 if (prev_ts && abs (prev_ts->end_note_types_per_minute() - ts->note_types_per_minute()) < 1.0) {
223 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker music"));
224 } else {
225 tempo_marker->set_points_color (UIConfiguration::instance().color ("tempo marker"));
226 }
227
228 max_tempo = max (max_tempo, ts->note_types_per_minute());
229 max_tempo = max (max_tempo, ts->end_note_types_per_minute());
230 min_tempo = min (min_tempo, ts->note_types_per_minute());
231 min_tempo = min (min_tempo, ts->end_note_types_per_minute());
232
233 prev_ts = ts;
234 }
235 }
236 if ((meter_marker = dynamic_cast<MeterMarker*> (*x)) != 0) {
237 if ((ms = &meter_marker->meter()) != 0) {
238 meter_marker->set_position (ms->sample ());
239 }
240 }
241 }
242
243 tempo_curves.sort (CurveComparator());
244
245 const double min_tempo_range = 5.0;
246 const double tempo_delta = fabs (max_tempo - min_tempo);
247
248 if (tempo_delta < min_tempo_range) {
249 max_tempo += min_tempo_range - tempo_delta;
250 min_tempo += tempo_delta - min_tempo_range;
251 }
252
253 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ) {
254 Curves::iterator tmp = x;
255 (*x)->set_max_tempo (max_tempo);
256 (*x)->set_min_tempo (min_tempo);
257 ++tmp;
258 if (tmp != tempo_curves.end()) {
259 (*x)->set_position ((*x)->tempo().sample(), (*tmp)->tempo().sample());
260 } else {
261 (*x)->set_position ((*x)->tempo().sample(), UINT32_MAX);
262 }
263
264 if (!(*x)->tempo().active()) {
265 (*x)->hide();
266 } else {
267 (*x)->show();
268 }
269
270 ++x;
271 }
272
273 for (Marks::iterator x = metric_marks.begin(); x != metric_marks.end(); ++x) {
274 TempoMarker* tempo_marker;
275 if ((tempo_marker = dynamic_cast<TempoMarker*> (*x)) != 0) {
276 tempo_marker->update_height_mark ((tempo_marker->tempo().note_types_per_minute() - min_tempo) / max (max_tempo - min_tempo, 10.0));
277 }
278 }
279
280 compute_bbt_ruler_scale (_leftmost_sample, _leftmost_sample + current_page_samples());
281
282 update_tempo_based_rulers ();
283
284 maybe_draw_grid_lines ();
285 }
286
287 void
redisplay_grid(bool immediate_redraw)288 Editor::redisplay_grid (bool immediate_redraw)
289 {
290 if (!_session) {
291 return;
292 }
293
294 if (immediate_redraw) {
295
296 update_tempo_based_rulers ();
297
298 update_grid();
299
300 } else {
301 Glib::signal_idle().connect (sigc::bind_return (sigc::bind (sigc::mem_fun (*this, &Editor::redisplay_grid), true), false));
302 }
303 }
304 void
tempo_curve_selected(TempoSection * ts,bool yn)305 Editor::tempo_curve_selected (TempoSection* ts, bool yn)
306 {
307 if (ts == 0) {
308 return;
309 }
310
311 for (Curves::iterator x = tempo_curves.begin(); x != tempo_curves.end(); ++x) {
312 if (&(*x)->tempo() == ts) {
313 if (yn) {
314 (*x)->set_color_rgba (UIConfiguration::instance().color ("location marker"));
315 } else {
316 (*x)->set_color_rgba (UIConfiguration::instance().color ("tempo curve"));
317 }
318 break;
319 }
320 }
321 }
322
323 /* computes a grid starting a beat before and ending a beat after leftmost and rightmost respectively */
324 void
compute_current_bbt_points(std::vector<TempoMap::BBTPoint> & grid,samplepos_t leftmost,samplepos_t rightmost)325 Editor::compute_current_bbt_points (std::vector<TempoMap::BBTPoint>& grid, samplepos_t leftmost, samplepos_t rightmost)
326 {
327 if (!_session) {
328 return;
329 }
330
331 /* prevent negative values of leftmost from creeping into tempomap
332 */
333 const double lower_beat = floor (max (0.0, _session->tempo_map().beat_at_sample (leftmost))) - 1.0;
334 switch (bbt_ruler_scale) {
335
336 case bbt_show_quarters:
337 case bbt_show_eighths:
338 case bbt_show_sixteenths:
339 case bbt_show_thirtyseconds:
340 case bbt_show_sixtyfourths:
341 case bbt_show_onetwentyeighths:
342 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost);
343 break;
344
345 case bbt_show_1:
346 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 1);
347 break;
348
349 case bbt_show_4:
350 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 4);
351 break;
352
353 case bbt_show_16:
354 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 16);
355 break;
356
357 case bbt_show_64:
358 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 64);
359 break;
360
361 default:
362 /* bbt_show_many */
363 _session->tempo_map().get_grid (grid, max (_session->tempo_map().sample_at_beat (lower_beat), (samplepos_t) 0), rightmost, 128);
364 break;
365 }
366 }
367
368 void
hide_grid_lines()369 Editor::hide_grid_lines ()
370 {
371 if (grid_lines) {
372 grid_lines->hide();
373 }
374 }
375
376 void
maybe_draw_grid_lines()377 Editor::maybe_draw_grid_lines ()
378 {
379 if ( _session == 0 ) {
380 return;
381 }
382
383 if (grid_lines == 0) {
384 grid_lines = new GridLines (time_line_group, ArdourCanvas::LineSet::Vertical);
385 }
386
387 grid_marks.clear();
388 samplepos_t rightmost_sample = _leftmost_sample + current_page_samples();
389
390 if ( grid_musical() ) {
391 metric_get_bbt (grid_marks, _leftmost_sample, rightmost_sample, 12);
392 } else if (_grid_type== GridTypeTimecode) {
393 metric_get_timecode (grid_marks, _leftmost_sample, rightmost_sample, 12);
394 } else if (_grid_type == GridTypeCDFrame) {
395 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
396 } else if (_grid_type == GridTypeMinSec) {
397 metric_get_minsec (grid_marks, _leftmost_sample, rightmost_sample, 12);
398 }
399
400 grid_lines->draw ( grid_marks );
401 grid_lines->show();
402 }
403
404 void
mouse_add_new_tempo_event(samplepos_t sample)405 Editor::mouse_add_new_tempo_event (samplepos_t sample)
406 {
407 if (_session == 0) {
408 return;
409 }
410
411 TempoMap& map(_session->tempo_map());
412
413 begin_reversible_command (_("add tempo mark"));
414 const double pulse = map.exact_qn_at_sample (sample, get_grid_music_divisions (0)) / 4.0;
415
416 if (pulse > 0.0) {
417 XMLNode &before = map.get_state();
418 /* add music-locked ramped (?) tempo using the bpm/note type at sample*/
419 map.add_tempo (map.tempo_at_sample (sample), pulse, 0, MusicTime);
420
421 XMLNode &after = map.get_state();
422 _session->add_command(new MementoCommand<TempoMap>(map, &before, &after));
423 commit_reversible_command ();
424 }
425
426 //map.dump (cerr);
427 }
428
429 void
mouse_add_new_meter_event(samplepos_t sample)430 Editor::mouse_add_new_meter_event (samplepos_t sample)
431 {
432 if (_session == 0) {
433 return;
434 }
435
436
437 TempoMap& map(_session->tempo_map());
438 MeterDialog meter_dialog (map, sample, _("add"));
439
440 switch (meter_dialog.run ()) {
441 case RESPONSE_ACCEPT:
442 break;
443 default:
444 return;
445 }
446
447 double bpb = meter_dialog.get_bpb ();
448 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
449
450 double note_type = meter_dialog.get_note_type ();
451
452 Timecode::BBT_Time requested;
453 meter_dialog.get_bbt_time (requested);
454
455 const double al_sample = map.sample_at_bbt (requested);
456 begin_reversible_command (_("add meter mark"));
457 XMLNode &before = map.get_state();
458
459 if (meter_dialog.get_lock_style() == MusicTime) {
460 map.add_meter (Meter (bpb, note_type), requested, 0, MusicTime);
461 } else {
462 map.add_meter (Meter (bpb, note_type), requested, al_sample, AudioTime);
463 }
464
465 _session->add_command(new MementoCommand<TempoMap>(map, &before, &map.get_state()));
466 commit_reversible_command ();
467
468 //map.dump (cerr);
469 }
470
471 void
remove_tempo_marker(ArdourCanvas::Item * item)472 Editor::remove_tempo_marker (ArdourCanvas::Item* item)
473 {
474 ArdourMarker* marker;
475 TempoMarker* tempo_marker;
476
477 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
478 fatal << _("programming error: tempo marker canvas item has no marker object pointer!") << endmsg;
479 abort(); /*NOTREACHED*/
480 }
481
482 if ((tempo_marker = dynamic_cast<TempoMarker*> (marker)) == 0) {
483 fatal << _("programming error: marker for tempo is not a tempo marker!") << endmsg;
484 abort(); /*NOTREACHED*/
485 }
486
487 if (!tempo_marker->tempo().locked_to_meter() && tempo_marker->tempo().active()) {
488 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_tempo_marker), &tempo_marker->tempo()));
489 }
490 }
491
492 void
edit_meter_section(MeterSection * section)493 Editor::edit_meter_section (MeterSection* section)
494 {
495 MeterDialog meter_dialog (_session->tempo_map(), *section, _("done"));
496
497 switch (meter_dialog.run()) {
498 case RESPONSE_ACCEPT:
499 break;
500 default:
501 return;
502 }
503
504 double bpb = meter_dialog.get_bpb ();
505 bpb = max (1.0, bpb); // XXX is this a reasonable limit?
506
507 double const note_type = meter_dialog.get_note_type ();
508 const Meter meter (bpb, note_type);
509
510 Timecode::BBT_Time when;
511 meter_dialog.get_bbt_time (when);
512 const samplepos_t sample = _session->tempo_map().sample_at_bbt (when);
513 const PositionLockStyle pls = (meter_dialog.get_lock_style() == AudioTime) ? AudioTime : MusicTime;
514
515 begin_reversible_command (_("replace meter mark"));
516 XMLNode &before = _session->tempo_map().get_state();
517
518 _session->tempo_map().replace_meter (*section, meter, when, sample, pls);
519
520 XMLNode &after = _session->tempo_map().get_state();
521 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
522 commit_reversible_command ();
523 }
524
525 void
edit_tempo_section(TempoSection * section)526 Editor::edit_tempo_section (TempoSection* section)
527 {
528 TempoDialog tempo_dialog (_session->tempo_map(), *section, _("done"));
529
530 switch (tempo_dialog.run ()) {
531 case RESPONSE_ACCEPT:
532 break;
533 default:
534 return;
535 }
536
537 double bpm = tempo_dialog.get_bpm ();
538 double end_bpm = tempo_dialog.get_end_bpm ();
539 double nt = tempo_dialog.get_note_type ();
540 bpm = max (0.01, bpm);
541 const Tempo tempo (bpm, nt, end_bpm);
542
543 Timecode::BBT_Time when;
544 tempo_dialog.get_bbt_time (when);
545
546 begin_reversible_command (_("replace tempo mark"));
547 XMLNode &before = _session->tempo_map().get_state();
548
549 if (tempo_dialog.get_lock_style() == AudioTime) {
550 samplepos_t const f = _session->tempo_map().predict_tempo_position (section, when).second;
551 _session->tempo_map().replace_tempo (*section, tempo, 0.0, f, AudioTime);
552 } else {
553 double const p = _session->tempo_map().predict_tempo_position (section, when).first;
554 _session->tempo_map().replace_tempo (*section, tempo, p, 0, MusicTime);
555 }
556
557 XMLNode &after = _session->tempo_map().get_state();
558 _session->add_command (new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
559 commit_reversible_command ();
560 }
561
562 void
edit_tempo_marker(TempoMarker & tm)563 Editor::edit_tempo_marker (TempoMarker& tm)
564 {
565 edit_tempo_section (&tm.tempo());
566 }
567
568 void
edit_meter_marker(MeterMarker & mm)569 Editor::edit_meter_marker (MeterMarker& mm)
570 {
571 edit_meter_section (&mm.meter());
572 }
573
574 gint
real_remove_tempo_marker(TempoSection * section)575 Editor::real_remove_tempo_marker (TempoSection *section)
576 {
577 begin_reversible_command (_("remove tempo mark"));
578 XMLNode &before = _session->tempo_map().get_state();
579 _session->tempo_map().remove_tempo (*section, true);
580 XMLNode &after = _session->tempo_map().get_state();
581 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
582 commit_reversible_command ();
583
584 return FALSE;
585 }
586
587 void
remove_meter_marker(ArdourCanvas::Item * item)588 Editor::remove_meter_marker (ArdourCanvas::Item* item)
589 {
590 ArdourMarker* marker;
591 MeterMarker* meter_marker;
592
593 if ((marker = reinterpret_cast<ArdourMarker *> (item->get_data ("marker"))) == 0) {
594 fatal << _("programming error: meter marker canvas item has no marker object pointer!") << endmsg;
595 abort(); /*NOTREACHED*/
596 }
597
598 if ((meter_marker = dynamic_cast<MeterMarker*> (marker)) == 0) {
599 fatal << _("programming error: marker for meter is not a meter marker!") << endmsg;
600 abort(); /*NOTREACHED*/
601 }
602
603 if (!meter_marker->meter().initial()) {
604 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun(*this, &Editor::real_remove_meter_marker), &meter_marker->meter()));
605 }
606 }
607
608 gint
real_remove_meter_marker(MeterSection * section)609 Editor::real_remove_meter_marker (MeterSection *section)
610 {
611 begin_reversible_command (_("remove tempo mark"));
612 XMLNode &before = _session->tempo_map().get_state();
613 _session->tempo_map().remove_meter (*section, true);
614 XMLNode &after = _session->tempo_map().get_state();
615 _session->add_command(new MementoCommand<TempoMap>(_session->tempo_map(), &before, &after));
616 commit_reversible_command ();
617
618 return FALSE;
619 }
620