1 /*
2 * Copyright (C) 2005-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
4 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
5 * Copyright (C) 2006 Hans Fugal <hans@fugal.net>
6 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
7 * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
8 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
9 * Copyright (C) 2013-2017 Robin Gareus <robin@gareus.org>
10 * Copyright (C) 2014-2016 Nick Mainsbridge <mainsbridge@gmail.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 #include <cmath>
28
29 #ifdef COMPILER_MSVC
30 #include <float.h>
31
32 // 'std::isnan()' is not available in MSVC.
33 #define isnan_local(val) (bool)_isnan((double)val)
34 #else
35 #define isnan_local std::isnan
36 #endif
37
38 #include <climits>
39 #include <vector>
40
41 #include "boost/shared_ptr.hpp"
42
43 #include "pbd/floating.h"
44 #include "pbd/memento_command.h"
45 #include "pbd/stl_delete.h"
46
47 #include "ardour/automation_list.h"
48 #include "ardour/dB.h"
49 #include "ardour/debug.h"
50 #include "ardour/parameter_types.h"
51 #include "ardour/tempo.h"
52
53 #include "evoral/Curve.h"
54
55 #include "canvas/debug.h"
56
57 #include "automation_line.h"
58 #include "control_point.h"
59 #include "gui_thread.h"
60 #include "rgb_macros.h"
61 #include "public_editor.h"
62 #include "selection.h"
63 #include "time_axis_view.h"
64 #include "point_selection.h"
65 #include "automation_time_axis.h"
66 #include "ui_config.h"
67
68 #include "ardour/event_type_map.h"
69 #include "ardour/session.h"
70 #include "ardour/value_as_string.h"
71
72 #include "pbd/i18n.h"
73
74 using namespace std;
75 using namespace ARDOUR;
76 using namespace PBD;
77 using namespace Editing;
78
79 /** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session samples.
80 * This will not be deleted by AutomationLine.
81 */
AutomationLine(const string & name,TimeAxisView & tv,ArdourCanvas::Item & parent,boost::shared_ptr<AutomationList> al,const ParameterDescriptor & desc,Evoral::TimeConverter<double,samplepos_t> * converter)82 AutomationLine::AutomationLine (const string& name,
83 TimeAxisView& tv,
84 ArdourCanvas::Item& parent,
85 boost::shared_ptr<AutomationList> al,
86 const ParameterDescriptor& desc,
87 Evoral::TimeConverter<double, samplepos_t>* converter)
88 : trackview (tv)
89 , _name (name)
90 , alist (al)
91 , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, samplepos_t>)
92 , _parent_group (parent)
93 , _offset (0)
94 , _maximum_time (max_samplepos)
95 , _fill (false)
96 , _desc (desc)
97 {
98 if (converter) {
99 _our_time_converter = false;
100 } else {
101 _our_time_converter = true;
102 }
103
104 _visible = Line;
105
106 update_pending = false;
107 have_timeout = false;
108 no_draw = false;
109 _is_boolean = false;
110 terminal_points_can_slide = true;
111 _height = 0;
112
113 group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple(0, 1.5));
114 CANVAS_DEBUG_NAME (group, "region gain envelope group");
115
116 line = new ArdourCanvas::PolyLine (group);
117 CANVAS_DEBUG_NAME (line, "region gain envelope line");
118 line->set_data ("line", this);
119 line->set_outline_width (2.0);
120 line->set_covers_threshold (4.0);
121
122 line->Event.connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
123
124 trackview.session()->register_with_memento_command_factory(alist->id(), this);
125
126 interpolation_changed (alist->interpolation ());
127
128 connect_to_list ();
129 }
130
~AutomationLine()131 AutomationLine::~AutomationLine ()
132 {
133 delete group; // deletes child items
134
135 for (std::vector<ControlPoint *>::iterator i = control_points.begin(); i != control_points.end(); i++) {
136 (*i)->unset_item ();
137 delete *i;
138 }
139 control_points.clear ();
140
141 if (_our_time_converter) {
142 delete _time_converter;
143 }
144 }
145
146 bool
event_handler(GdkEvent * event)147 AutomationLine::event_handler (GdkEvent* event)
148 {
149 return PublicEditor::instance().canvas_line_event (event, line, this);
150 }
151
152 bool
is_stepped() const153 AutomationLine::is_stepped() const
154 {
155 return (_desc.toggled ||
156 (alist && alist->interpolation() == AutomationList::Discrete));
157 }
158
159 void
update_visibility()160 AutomationLine::update_visibility ()
161 {
162 if (_visible & Line) {
163 /* Only show the line when there are some points, otherwise we may show an out-of-date line
164 when automation points have been removed (the line will still follow the shape of the
165 old points).
166 */
167 if (control_points.size() >= 2) {
168 line->show();
169 } else {
170 line->hide ();
171 }
172
173 if (_visible & ControlPoints) {
174 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
175 (*i)->show ();
176 }
177 } else if (_visible & SelectedControlPoints) {
178 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
179 if ((*i)->selected()) {
180 (*i)->show ();
181 } else {
182 (*i)->hide ();
183 }
184 }
185 } else {
186 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
187 (*i)->hide ();
188 }
189 }
190
191 } else {
192 line->hide ();
193 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
194 if (_visible & ControlPoints) {
195 (*i)->show ();
196 } else {
197 (*i)->hide ();
198 }
199 }
200 }
201 }
202
203 bool
get_uses_gain_mapping() const204 AutomationLine::get_uses_gain_mapping () const
205 {
206 switch (_desc.type) {
207 case GainAutomation:
208 case BusSendLevel:
209 case EnvelopeAutomation:
210 case TrimAutomation:
211 return true;
212 default:
213 return false;
214 }
215 }
216
217 void
hide()218 AutomationLine::hide ()
219 {
220 /* leave control points setting unchanged, we are just hiding the
221 overall line
222 */
223
224 set_visibility (AutomationLine::VisibleAspects (_visible & ~Line));
225 }
226
227 double
control_point_box_size()228 AutomationLine::control_point_box_size ()
229 {
230 if (_height > TimeAxisView::preset_height (HeightLarger)) {
231 return 8.0;
232 } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
233 return 6.0;
234 }
235 return 4.0;
236 }
237
238 void
set_height(guint32 h)239 AutomationLine::set_height (guint32 h)
240 {
241 if (h != _height) {
242 _height = h;
243
244 double bsz = control_point_box_size();
245
246 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
247 (*i)->set_size (bsz);
248 }
249
250 if (_fill) {
251 line->set_fill_y1 (_height);
252 } else {
253 line->set_fill_y1 (0);
254 }
255 reset ();
256 }
257 }
258
259 void
set_line_color(uint32_t color)260 AutomationLine::set_line_color (uint32_t color)
261 {
262 _line_color = color;
263 line->set_outline_color (color);
264
265 Gtkmm2ext::SVAModifier mod = UIConfiguration::instance().modifier ("automation line fill");
266
267 line->set_fill_color ((color & 0xffffff00) + mod.a()*255);
268 }
269
270 ControlPoint*
nth(uint32_t n)271 AutomationLine::nth (uint32_t n)
272 {
273 if (n < control_points.size()) {
274 return control_points[n];
275 } else {
276 return 0;
277 }
278 }
279
280 ControlPoint const *
nth(uint32_t n) const281 AutomationLine::nth (uint32_t n) const
282 {
283 if (n < control_points.size()) {
284 return control_points[n];
285 } else {
286 return 0;
287 }
288 }
289
290 void
modify_point_y(ControlPoint & cp,double y)291 AutomationLine::modify_point_y (ControlPoint& cp, double y)
292 {
293 /* clamp y-coord appropriately. y is supposed to be a normalized fraction (0.0-1.0),
294 and needs to be converted to a canvas unit distance.
295 */
296
297 y = max (0.0, y);
298 y = min (1.0, y);
299 y = _height - (y * _height);
300
301 double const x = trackview.editor().sample_to_pixel_unrounded (_time_converter->to((*cp.model())->when) - _offset);
302
303 trackview.editor().begin_reversible_command (_("automation event move"));
304 trackview.editor().session()->add_command (
305 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
306
307 cp.move_to (x, y, ControlPoint::Full);
308
309 alist->freeze ();
310 sync_model_with_view_point (cp);
311 alist->thaw ();
312
313 reset_line_coords (cp);
314
315 if (line_points.size() > 1) {
316 line->set_steps (line_points, is_stepped());
317 }
318
319 update_pending = false;
320
321 trackview.editor().session()->add_command (
322 new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state()));
323
324 trackview.editor().commit_reversible_command ();
325 trackview.editor().session()->set_dirty ();
326 }
327
328 void
reset_line_coords(ControlPoint & cp)329 AutomationLine::reset_line_coords (ControlPoint& cp)
330 {
331 if (cp.view_index() < line_points.size()) {
332 line_points[cp.view_index()].x = cp.get_x ();
333 line_points[cp.view_index()].y = cp.get_y ();
334 }
335 }
336
337 bool
sync_model_with_view_points(list<ControlPoint * > cp)338 AutomationLine::sync_model_with_view_points (list<ControlPoint*> cp)
339 {
340 update_pending = true;
341
342 bool moved = false;
343 for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
344 moved = sync_model_with_view_point (**i) || moved;
345 }
346
347 return moved;
348 }
349
350 string
get_verbose_cursor_string(double fraction) const351 AutomationLine::get_verbose_cursor_string (double fraction) const
352 {
353 return fraction_to_string (fraction);
354 }
355
356 string
get_verbose_cursor_relative_string(double fraction,double delta) const357 AutomationLine::get_verbose_cursor_relative_string (double fraction, double delta) const
358 {
359 std::string s = fraction_to_string (fraction);
360 std::string d = delta_to_string (delta);
361 return s + " (" + d + ")";
362 }
363
364 /**
365 * @param fraction y fraction
366 * @return string representation of this value, using dB if appropriate.
367 */
368 string
fraction_to_string(double fraction) const369 AutomationLine::fraction_to_string (double fraction) const
370 {
371 view_to_model_coord_y (fraction);
372 return ARDOUR::value_as_string (_desc, fraction);
373 }
374
375 string
delta_to_string(double delta) const376 AutomationLine::delta_to_string (double delta) const
377 {
378 if (!get_uses_gain_mapping () && _desc.logarithmic) {
379 return "x " + ARDOUR::value_as_string (_desc, delta);
380 } else {
381 return "\u0394 " + ARDOUR::value_as_string (_desc, delta);
382 }
383 }
384
385 /**
386 * @param s Value string in the form as returned by fraction_to_string.
387 * @return Corresponding y fraction.
388 */
389 double
string_to_fraction(string const & s) const390 AutomationLine::string_to_fraction (string const & s) const
391 {
392 double v;
393 sscanf (s.c_str(), "%lf", &v);
394
395 switch (_desc.type) {
396 case GainAutomation:
397 case BusSendLevel:
398 case EnvelopeAutomation:
399 case TrimAutomation:
400 if (s == "-inf") { /* translation */
401 v = 0;
402 } else {
403 v = dB_to_coefficient (v);
404 }
405 break;
406 default:
407 break;
408 }
409 model_to_view_coord_y (v);
410 return v;
411 }
412
413 /** Start dragging a single point, possibly adding others if the supplied point is selected and there
414 * are other selected points.
415 *
416 * @param cp Point to drag.
417 * @param x Initial x position (units).
418 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
419 */
420 void
start_drag_single(ControlPoint * cp,double x,float fraction)421 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
422 {
423 trackview.editor().session()->add_command (
424 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
425
426 _drag_points.clear ();
427 _drag_points.push_back (cp);
428
429 if (cp->selected ()) {
430 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
431 if (*i != cp && (*i)->selected()) {
432 _drag_points.push_back (*i);
433 }
434 }
435 }
436
437 start_drag_common (x, fraction);
438 }
439
440 /** Start dragging a line vertically (with no change in x)
441 * @param i1 Control point index of the `left' point on the line.
442 * @param i2 Control point index of the `right' point on the line.
443 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
444 */
445 void
start_drag_line(uint32_t i1,uint32_t i2,float fraction)446 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
447 {
448 trackview.editor().session()->add_command (
449 new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0));
450
451 _drag_points.clear ();
452
453 for (uint32_t i = i1; i <= i2; i++) {
454 _drag_points.push_back (nth (i));
455 }
456
457 start_drag_common (0, fraction);
458 }
459
460 /** Start dragging multiple points (with no change in x)
461 * @param cp Points to drag.
462 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
463 */
464 void
start_drag_multiple(list<ControlPoint * > cp,float fraction,XMLNode * state)465 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
466 {
467 trackview.editor().session()->add_command (
468 new MementoCommand<AutomationList> (memento_command_binder(), state, 0));
469
470 _drag_points = cp;
471 start_drag_common (0, fraction);
472 }
473
474 struct ControlPointSorter
475 {
operator ()ControlPointSorter476 bool operator() (ControlPoint const * a, ControlPoint const * b) const {
477 if (floateq (a->get_x(), b->get_x(), 1)) {
478 return a->view_index() < b->view_index();
479 }
480 return a->get_x() < b->get_x();
481 }
482 };
483
ContiguousControlPoints(AutomationLine & al)484 AutomationLine::ContiguousControlPoints::ContiguousControlPoints (AutomationLine& al)
485 : line (al), before_x (0), after_x (DBL_MAX)
486 {
487 }
488
489 void
compute_x_bounds(PublicEditor & e)490 AutomationLine::ContiguousControlPoints::compute_x_bounds (PublicEditor& e)
491 {
492 uint32_t sz = size();
493
494 if (sz > 0 && sz < line.npoints()) {
495 const TempoMap& map (e.session()->tempo_map());
496
497 /* determine the limits on x-axis motion for this
498 contiguous range of control points
499 */
500
501 if (front()->view_index() > 0) {
502 before_x = line.nth (front()->view_index() - 1)->get_x();
503
504 const samplepos_t pos = e.pixel_to_sample(before_x);
505 const Meter& meter = map.meter_at_sample (pos);
506 const samplecnt_t len = ceil (meter.samples_per_bar (map.tempo_at_sample (pos), e.session()->sample_rate())
507 / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()) );
508 const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
509
510 before_x += one_tick_in_pixels;
511 }
512
513 /* if our last point has a point after it in the line,
514 we have an "after" bound
515 */
516
517 if (back()->view_index() < (line.npoints() - 1)) {
518 after_x = line.nth (back()->view_index() + 1)->get_x();
519
520 const samplepos_t pos = e.pixel_to_sample(after_x);
521 const Meter& meter = map.meter_at_sample (pos);
522 const samplecnt_t len = ceil (meter.samples_per_bar (map.tempo_at_sample (pos), e.session()->sample_rate())
523 / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()));
524 const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
525
526 after_x -= one_tick_in_pixels;
527 }
528 }
529 }
530
531 double
clamp_dx(double dx)532 AutomationLine::ContiguousControlPoints::clamp_dx (double dx)
533 {
534 if (empty()) {
535 return dx;
536 }
537
538 /* get the maximum distance we can move any of these points along the x-axis
539 */
540
541 double tx; /* possible position a point would move to, given dx */
542 ControlPoint* cp;
543
544 if (dx > 0) {
545 /* check the last point, since we're moving later in time */
546 cp = back();
547 } else {
548 /* check the first point, since we're moving earlier in time */
549 cp = front();
550 }
551
552 tx = cp->get_x() + dx; // new possible position if we just add the motion
553 tx = max (tx, before_x); // can't move later than following point
554 tx = min (tx, after_x); // can't move earlier than preceeding point
555 return tx - cp->get_x ();
556 }
557
558 void
move(double dx,double dvalue)559 AutomationLine::ContiguousControlPoints::move (double dx, double dvalue)
560 {
561 for (std::list<ControlPoint*>::iterator i = begin(); i != end(); ++i) {
562 // compute y-axis delta
563 double view_y = 1.0 - (*i)->get_y() / line.height();
564 line.view_to_model_coord_y (view_y);
565 line.apply_delta (view_y, dvalue);
566 line.model_to_view_coord_y (view_y);
567 view_y = (1.0 - view_y) * line.height();
568
569 (*i)->move_to ((*i)->get_x() + dx, view_y, ControlPoint::Full);
570 line.reset_line_coords (**i);
571 }
572 }
573
574 /** Common parts of starting a drag.
575 * @param x Starting x position in units, or 0 if x is being ignored.
576 * @param fraction Starting y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
577 */
578 void
start_drag_common(double x,float fraction)579 AutomationLine::start_drag_common (double x, float fraction)
580 {
581 _drag_x = x;
582 _drag_distance = 0;
583 _last_drag_fraction = fraction;
584 _drag_had_movement = false;
585 did_push = false;
586
587 /* they are probably ordered already, but we have to make sure */
588
589 _drag_points.sort (ControlPointSorter());
590 }
591
592
593 /** Should be called to indicate motion during a drag.
594 * @param x New x position of the drag in canvas units, or undefined if ignore_x == true.
595 * @param fraction New y fraction.
596 * @return x position and y fraction that were actually used (once clamped).
597 */
598 pair<float, float>
drag_motion(double const x,float fraction,bool ignore_x,bool with_push,uint32_t & final_index)599 AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool with_push, uint32_t& final_index)
600 {
601 if (_drag_points.empty()) {
602 return pair<double,float> (fraction, _desc.is_linear () ? 0 : 1);
603 }
604
605 double dx = ignore_x ? 0 : (x - _drag_x);
606 double dy = fraction - _last_drag_fraction;
607
608 if (!_drag_had_movement) {
609
610 /* "first move" ... do some stuff that we don't want to do if
611 no motion ever took place, but need to do before we handle
612 motion.
613 */
614
615 /* partition the points we are dragging into (potentially several)
616 * set(s) of contiguous points. this will not happen with a normal
617 * drag, but if the user does a discontiguous selection, it can.
618 */
619
620 uint32_t expected_view_index = 0;
621 CCP contig;
622
623 for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
624 if (i == _drag_points.begin() || (*i)->view_index() != expected_view_index) {
625 contig.reset (new ContiguousControlPoints (*this));
626 contiguous_points.push_back (contig);
627 }
628 contig->push_back (*i);
629 expected_view_index = (*i)->view_index() + 1;
630 }
631
632 if (contiguous_points.back()->empty()) {
633 contiguous_points.pop_back ();
634 }
635
636 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
637 (*ccp)->compute_x_bounds (trackview.editor());
638 }
639 _drag_had_movement = true;
640 }
641
642 /* OK, now on to the stuff related to *this* motion event. First, for
643 * each contiguous range, figure out the maximum x-axis motion we are
644 * allowed (because of neighbouring points that are not moving.
645 *
646 * if we are moving forwards with push, we don't need to do this,
647 * since all later points will move too.
648 */
649
650 if (dx < 0 || ((dx > 0) && !with_push)) {
651 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
652 double dxt = (*ccp)->clamp_dx (dx);
653 if (fabs (dxt) < fabs (dx)) {
654 dx = dxt;
655 }
656 }
657 }
658
659 /* compute deflection */
660 double delta_value;
661 {
662 double value0 = _last_drag_fraction;
663 double value1 = _last_drag_fraction + dy;
664 view_to_model_coord_y (value0);
665 view_to_model_coord_y (value1);
666 delta_value = compute_delta (value0, value1);
667 }
668
669 /* special case -inf */
670 if (delta_value == 0 && dy > 0 && !_desc.is_linear ()) {
671 assert (_desc.lower == 0);
672 delta_value = 1.0;
673 }
674
675 /* clamp y */
676 for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
677 double vy = 1.0 - (*i)->get_y() / _height;
678 view_to_model_coord_y (vy);
679 const double orig = vy;
680 apply_delta (vy, delta_value);
681 if (vy < _desc.lower) {
682 delta_value = compute_delta (orig, _desc.lower);
683 }
684 if (vy > _desc.upper) {
685 delta_value = compute_delta (orig, _desc.upper);
686 }
687 }
688
689 if (dx || dy) {
690 /* and now move each section */
691 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
692 (*ccp)->move (dx, delta_value);
693 }
694
695 if (with_push) {
696 final_index = contiguous_points.back()->back()->view_index () + 1;
697 ControlPoint* p;
698 uint32_t i = final_index;
699 while ((p = nth (i)) != 0 && p->can_slide()) {
700 p->move_to (p->get_x() + dx, p->get_y(), ControlPoint::Full);
701 reset_line_coords (*p);
702 ++i;
703 }
704 }
705
706 /* update actual line coordinates (will queue a redraw) */
707
708 if (line_points.size() > 1) {
709 line->set_steps (line_points, is_stepped());
710 }
711 }
712
713 /* calculate effective delta */
714 ControlPoint* cp = _drag_points.front();
715 double vy = 1.0 - cp->get_y() / (double)_height;
716 view_to_model_coord_y (vy);
717 float val = (*(cp->model ()))->value;
718 float effective_delta = _desc.compute_delta (val, vy);
719 /* special case recovery from -inf */
720 if (val == 0 && effective_delta == 0 && vy > 0) {
721 assert (!_desc.is_linear ());
722 effective_delta = HUGE_VAL; // +Infinity
723 }
724
725 double const result_frac = _last_drag_fraction + dy;
726 _drag_distance += dx;
727 _drag_x += dx;
728 _last_drag_fraction = result_frac;
729 did_push = with_push;
730
731 return pair<float, float> (result_frac, effective_delta);
732 }
733
734 /** Should be called to indicate the end of a drag */
735 void
end_drag(bool with_push,uint32_t final_index)736 AutomationLine::end_drag (bool with_push, uint32_t final_index)
737 {
738 if (!_drag_had_movement) {
739 return;
740 }
741
742 alist->freeze ();
743 bool moved = sync_model_with_view_points (_drag_points);
744
745 if (with_push) {
746 ControlPoint* p;
747 uint32_t i = final_index;
748 while ((p = nth (i)) != 0 && p->can_slide()) {
749 moved = sync_model_with_view_point (*p) || moved;
750 ++i;
751 }
752 }
753
754 alist->thaw ();
755
756 update_pending = false;
757
758 if (moved) {
759 /* A point has moved as a result of sync (clamped to integer or boolean
760 value), update line accordingly. */
761 line->set_steps (line_points, is_stepped());
762 }
763
764 trackview.editor().session()->add_command (
765 new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state()));
766
767 trackview.editor().session()->set_dirty ();
768 did_push = false;
769
770 contiguous_points.clear ();
771 }
772
773 bool
sync_model_with_view_point(ControlPoint & cp)774 AutomationLine::sync_model_with_view_point (ControlPoint& cp)
775 {
776 /* find out where the visual control point is.
777 initial results are in canvas units. ask the
778 line to convert them to something relevant.
779 */
780
781 double view_x = cp.get_x();
782 double view_y = 1.0 - cp.get_y() / (double)_height;
783
784 /* if xval has not changed, set it directly from the model to avoid rounding errors */
785
786 if (view_x == trackview.editor().sample_to_pixel_unrounded (_time_converter->to ((*cp.model())->when)) - _offset) {
787 view_x = (*cp.model())->when - _offset;
788 } else {
789 view_x = trackview.editor().pixel_to_sample (view_x);
790 view_x = _time_converter->from (view_x + _offset);
791 }
792
793 update_pending = true;
794
795 view_to_model_coord_y (view_y);
796
797 alist->modify (cp.model(), view_x, view_y);
798
799 /* convert back from model to view y for clamping position (for integer/boolean/etc) */
800 model_to_view_coord_y (view_y);
801 const double point_y = _height - (view_y * _height);
802 if (point_y != cp.get_y()) {
803 cp.move_to (cp.get_x(), point_y, ControlPoint::Full);
804 reset_line_coords (cp);
805 return true;
806 }
807
808 return false;
809 }
810
811 bool
control_points_adjacent(double xval,uint32_t & before,uint32_t & after)812 AutomationLine::control_points_adjacent (double xval, uint32_t & before, uint32_t& after)
813 {
814 ControlPoint *bcp = 0;
815 ControlPoint *acp = 0;
816 double unit_xval;
817
818 unit_xval = trackview.editor().sample_to_pixel_unrounded (xval);
819
820 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
821
822 if ((*i)->get_x() <= unit_xval) {
823
824 if (!bcp || (*i)->get_x() > bcp->get_x()) {
825 bcp = *i;
826 before = bcp->view_index();
827 }
828
829 } else if ((*i)->get_x() > unit_xval) {
830 acp = *i;
831 after = acp->view_index();
832 break;
833 }
834 }
835
836 return bcp && acp;
837 }
838
839 bool
is_last_point(ControlPoint & cp)840 AutomationLine::is_last_point (ControlPoint& cp)
841 {
842 // If the list is not empty, and the point is the last point in the list
843
844 if (alist->empty()) {
845 return false;
846 }
847
848 AutomationList::const_iterator i = alist->end();
849 --i;
850
851 if (cp.model() == i) {
852 return true;
853 }
854
855 return false;
856 }
857
858 bool
is_first_point(ControlPoint & cp)859 AutomationLine::is_first_point (ControlPoint& cp)
860 {
861 // If the list is not empty, and the point is the first point in the list
862
863 if (!alist->empty() && cp.model() == alist->begin()) {
864 return true;
865 }
866
867 return false;
868 }
869
870 // This is copied into AudioRegionGainLine
871 void
remove_point(ControlPoint & cp)872 AutomationLine::remove_point (ControlPoint& cp)
873 {
874 trackview.editor().begin_reversible_command (_("remove control point"));
875 XMLNode &before = alist->get_state();
876
877 trackview.editor ().get_selection ().clear_points ();
878 alist->erase (cp.model());
879
880 trackview.editor().session()->add_command(
881 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
882
883 trackview.editor().commit_reversible_command ();
884 trackview.editor().session()->set_dirty ();
885 }
886
887 /** Get selectable points within an area.
888 * @param start Start position in session samples.
889 * @param end End position in session samples.
890 * @param bot Bottom y range, as a fraction of line height, where 0 is the bottom of the line.
891 * @param top Top y range, as a fraction of line height, where 0 is the bottom of the line.
892 * @param result Filled in with selectable things; in this case, ControlPoints.
893 */
894 void
get_selectables(samplepos_t start,samplepos_t end,double botfrac,double topfrac,list<Selectable * > & results)895 AutomationLine::get_selectables (samplepos_t start, samplepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
896 {
897 /* convert fractions to display coordinates with 0 at the top of the track */
898 double const bot_track = (1 - topfrac) * trackview.current_height ();
899 double const top_track = (1 - botfrac) * trackview.current_height ();
900
901 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
902 double const model_when = (*(*i)->model())->when;
903
904 /* model_when is relative to the start of the source, so we just need to add on the origin_b here
905 (as it is the session sample position of the start of the source)
906 */
907
908 samplepos_t const session_samples_when = _time_converter->to (model_when) + _time_converter->origin_b ();
909
910 if (session_samples_when >= start && session_samples_when <= end && (*i)->get_y() >= bot_track && (*i)->get_y() <= top_track) {
911 results.push_back (*i);
912 }
913 }
914 }
915
916 void
get_inverted_selectables(Selection &,list<Selectable * > &)917 AutomationLine::get_inverted_selectables (Selection&, list<Selectable*>& /*results*/)
918 {
919 // hmmm ....
920 }
921
922 void
set_selected_points(PointSelection const & points)923 AutomationLine::set_selected_points (PointSelection const & points)
924 {
925 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
926 (*i)->set_selected (false);
927 }
928
929 for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
930 (*i)->set_selected (true);
931 }
932
933 if (points.empty()) {
934 remove_visibility (SelectedControlPoints);
935 } else {
936 add_visibility (SelectedControlPoints);
937 }
938
939 set_colors ();
940 }
941
942 void
set_colors()943 AutomationLine::set_colors ()
944 {
945 set_line_color (UIConfiguration::instance().color ("automation line"));
946 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
947 (*i)->set_color ();
948 }
949 }
950
951 void
list_changed()952 AutomationLine::list_changed ()
953 {
954 DEBUG_TRACE (DEBUG::Automation, string_compose ("\tline changed, existing update pending? %1\n", update_pending));
955
956 if (!update_pending) {
957 update_pending = true;
958 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationLine::queue_reset, this));
959 }
960 }
961
962 void
reset_callback(const Evoral::ControlList & events)963 AutomationLine::reset_callback (const Evoral::ControlList& events)
964 {
965 uint32_t vp = 0;
966 uint32_t pi = 0;
967 uint32_t np;
968
969 if (events.empty()) {
970 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
971 delete *i;
972 }
973 control_points.clear ();
974 line->hide();
975 return;
976 }
977
978 /* hide all existing points, and the line */
979
980 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
981 (*i)->hide();
982 }
983
984 line->hide ();
985 np = events.size();
986
987 Evoral::ControlList& e = const_cast<Evoral::ControlList&> (events);
988
989 for (AutomationList::iterator ai = e.begin(); ai != e.end(); ++ai, ++pi) {
990
991 double tx = (*ai)->when;
992 double ty = (*ai)->value;
993
994 /* convert from model coordinates to canonical view coordinates */
995
996 model_to_view_coord (tx, ty);
997
998 if (isnan_local (tx) || isnan_local (ty)) {
999 warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
1000 _name) << endmsg;
1001 continue;
1002 }
1003
1004 if (tx >= max_samplepos || tx < 0 || tx >= _maximum_time) {
1005 continue;
1006 }
1007
1008 /* convert x-coordinate to a canvas unit coordinate (this takes
1009 * zoom and scroll into account).
1010 */
1011
1012 tx = trackview.editor().sample_to_pixel_unrounded (tx);
1013
1014 /* convert from canonical view height (0..1.0) to actual
1015 * height coordinates (using X11's top-left rooted system)
1016 */
1017
1018 ty = _height - (ty * _height);
1019
1020 add_visible_control_point (vp, pi, tx, ty, ai, np);
1021 vp++;
1022 }
1023
1024 /* discard extra CP's to avoid confusing ourselves */
1025
1026 while (control_points.size() > vp) {
1027 ControlPoint* cp = control_points.back();
1028 control_points.pop_back ();
1029 delete cp;
1030 }
1031
1032 if (!terminal_points_can_slide) {
1033 control_points.back()->set_can_slide(false);
1034 }
1035
1036 if (vp > 1) {
1037
1038 /* reset the line coordinates given to the CanvasLine */
1039
1040 while (line_points.size() < vp) {
1041 line_points.push_back (ArdourCanvas::Duple (0,0));
1042 }
1043
1044 while (line_points.size() > vp) {
1045 line_points.pop_back ();
1046 }
1047
1048 for (uint32_t n = 0; n < vp; ++n) {
1049 line_points[n].x = control_points[n]->get_x();
1050 line_points[n].y = control_points[n]->get_y();
1051 }
1052
1053 line->set_steps (line_points, is_stepped());
1054
1055 update_visibility ();
1056 }
1057
1058 set_selected_points (trackview.editor().get_selection().points);
1059 }
1060
1061 void
reset()1062 AutomationLine::reset ()
1063 {
1064 DEBUG_TRACE (DEBUG::Automation, "\t\tLINE RESET\n");
1065 update_pending = false;
1066 have_timeout = false;
1067
1068 if (no_draw) {
1069 return;
1070 }
1071
1072 /* TODO: abort any drags in progress, e.g. draging points while writing automation
1073 * (the control-point model, used by AutomationLine::drag_motion, will be invalid).
1074 *
1075 * Note: reset() may also be called from an aborted drag (LineDrag::aborted)
1076 * maybe abort in list_changed(), interpolation_changed() and ... ?
1077 * XXX
1078 */
1079
1080 alist->apply_to_points (*this, &AutomationLine::reset_callback);
1081 }
1082
1083 void
queue_reset()1084 AutomationLine::queue_reset ()
1085 {
1086 /* this must be called from the GUI thread */
1087
1088 if (trackview.editor().session()->transport_rolling() && alist->automation_write()) {
1089 /* automation write pass ... defer to a timeout */
1090 /* redraw in 1/4 second */
1091 if (!have_timeout) {
1092 DEBUG_TRACE (DEBUG::Automation, "\tqueue timeout\n");
1093 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &AutomationLine::reset), false), 250);
1094 have_timeout = true;
1095 } else {
1096 DEBUG_TRACE (DEBUG::Automation, "\ttimeout already queued, change ignored\n");
1097 }
1098 } else {
1099 reset ();
1100 }
1101 }
1102
1103 void
clear()1104 AutomationLine::clear ()
1105 {
1106 /* parent must create and commit command */
1107 XMLNode &before = alist->get_state();
1108 alist->clear();
1109
1110 trackview.editor().session()->add_command (
1111 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
1112 }
1113
1114 void
set_list(boost::shared_ptr<ARDOUR::AutomationList> list)1115 AutomationLine::set_list (boost::shared_ptr<ARDOUR::AutomationList> list)
1116 {
1117 alist = list;
1118 queue_reset ();
1119 connect_to_list ();
1120 }
1121
1122 void
add_visibility(VisibleAspects va)1123 AutomationLine::add_visibility (VisibleAspects va)
1124 {
1125 VisibleAspects old = _visible;
1126
1127 _visible = VisibleAspects (_visible | va);
1128
1129 if (old != _visible) {
1130 update_visibility ();
1131 }
1132 }
1133
1134 void
set_visibility(VisibleAspects va)1135 AutomationLine::set_visibility (VisibleAspects va)
1136 {
1137 if (_visible != va) {
1138 _visible = va;
1139 update_visibility ();
1140 }
1141 }
1142
1143 void
remove_visibility(VisibleAspects va)1144 AutomationLine::remove_visibility (VisibleAspects va)
1145 {
1146 VisibleAspects old = _visible;
1147
1148 _visible = VisibleAspects (_visible & ~va);
1149
1150 if (old != _visible) {
1151 update_visibility ();
1152 }
1153 }
1154
1155 void
track_entered()1156 AutomationLine::track_entered()
1157 {
1158 add_visibility (ControlPoints);
1159 }
1160
1161 void
track_exited()1162 AutomationLine::track_exited()
1163 {
1164 remove_visibility (ControlPoints);
1165 }
1166
1167 XMLNode &
get_state(void)1168 AutomationLine::get_state (void)
1169 {
1170 /* function as a proxy for the model */
1171 return alist->get_state();
1172 }
1173
1174 int
set_state(const XMLNode & node,int version)1175 AutomationLine::set_state (const XMLNode &node, int version)
1176 {
1177 /* function as a proxy for the model */
1178 return alist->set_state (node, version);
1179 }
1180
1181 void
view_to_model_coord(double & x,double & y) const1182 AutomationLine::view_to_model_coord (double& x, double& y) const
1183 {
1184 x = _time_converter->from (x);
1185 view_to_model_coord_y (y);
1186 }
1187
1188 void
view_to_model_coord_y(double & y) const1189 AutomationLine::view_to_model_coord_y (double& y) const
1190 {
1191 if (alist->default_interpolation () != alist->interpolation()) {
1192 switch (alist->interpolation()) {
1193 case AutomationList::Discrete:
1194 /* toggles and MIDI only -- see is_stepped() */
1195 assert (alist->default_interpolation () == AutomationList::Linear);
1196 break;
1197 case AutomationList::Linear:
1198 y = y * (_desc.upper - _desc.lower) + _desc.lower;
1199 return;
1200 default:
1201 /* types that default to linear, can't be use
1202 * Logarithmic or Exponential interpolation.
1203 * "Curved" is invalid for automation (only x-fads)
1204 */
1205 assert (0);
1206 break;
1207 }
1208 }
1209 y = _desc.from_interface (y);
1210 }
1211
1212 double
compute_delta(double from,double to) const1213 AutomationLine::compute_delta (double from, double to) const
1214 {
1215 return _desc.compute_delta (from, to);
1216 }
1217
1218 void
apply_delta(double & val,double delta) const1219 AutomationLine::apply_delta (double& val, double delta) const
1220 {
1221 if (val == 0 && !_desc.is_linear () && delta >= 1.0) {
1222 /* recover from -inf */
1223 val = 1.0 / _height;
1224 view_to_model_coord_y (val);
1225 return;
1226 }
1227 val = _desc.apply_delta (val, delta);
1228 }
1229
1230 void
model_to_view_coord_y(double & y) const1231 AutomationLine::model_to_view_coord_y (double& y) const
1232 {
1233 if (alist->default_interpolation () != alist->interpolation()) {
1234 switch (alist->interpolation()) {
1235 case AutomationList::Discrete:
1236 /* toggles and MIDI only -- see is_stepped */
1237 assert (alist->default_interpolation () == AutomationList::Linear);
1238 break;
1239 case AutomationList::Linear:
1240 y = (y - _desc.lower) / (_desc.upper - _desc.lower);
1241 return;
1242 default:
1243 /* types that default to linear, can't be use
1244 * Logarithmic or Exponential interpolation.
1245 * "Curved" is invalid for automation (only x-fads)
1246 */
1247 assert (0);
1248 break;
1249 }
1250 }
1251 y = _desc.to_interface (y);
1252 }
1253
1254 void
model_to_view_coord(double & x,double & y) const1255 AutomationLine::model_to_view_coord (double& x, double& y) const
1256 {
1257 model_to_view_coord_y (y);
1258 x = _time_converter->to (x) - _offset;
1259 }
1260
1261 /** Called when our list has announced that its interpolation style has changed */
1262 void
interpolation_changed(AutomationList::InterpolationStyle style)1263 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
1264 {
1265 if (line_points.size() > 1) {
1266 reset ();
1267 line->set_steps(line_points, is_stepped());
1268 }
1269 }
1270
1271 void
add_visible_control_point(uint32_t view_index,uint32_t pi,double tx,double ty,AutomationList::iterator model,uint32_t npoints)1272 AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty,
1273 AutomationList::iterator model, uint32_t npoints)
1274 {
1275 ControlPoint::ShapeType shape;
1276
1277 if (view_index >= control_points.size()) {
1278
1279 /* make sure we have enough control points */
1280
1281 ControlPoint* ncp = new ControlPoint (*this);
1282 ncp->set_size (control_point_box_size ());
1283
1284 control_points.push_back (ncp);
1285 }
1286
1287 if (!terminal_points_can_slide) {
1288 if (pi == 0) {
1289 control_points[view_index]->set_can_slide (false);
1290 if (tx == 0) {
1291 shape = ControlPoint::Start;
1292 } else {
1293 shape = ControlPoint::Full;
1294 }
1295 } else if (pi == npoints - 1) {
1296 control_points[view_index]->set_can_slide (false);
1297 shape = ControlPoint::End;
1298 } else {
1299 control_points[view_index]->set_can_slide (true);
1300 shape = ControlPoint::Full;
1301 }
1302 } else {
1303 control_points[view_index]->set_can_slide (true);
1304 shape = ControlPoint::Full;
1305 }
1306
1307 control_points[view_index]->reset (tx, ty, model, view_index, shape);
1308
1309 /* finally, control visibility */
1310
1311 if (_visible & ControlPoints) {
1312 control_points[view_index]->show ();
1313 } else {
1314 control_points[view_index]->hide ();
1315 }
1316 }
1317
1318 void
connect_to_list()1319 AutomationLine::connect_to_list ()
1320 {
1321 _list_connections.drop_connections ();
1322
1323 alist->StateChanged.connect (_list_connections, invalidator (*this), boost::bind (&AutomationLine::list_changed, this), gui_context());
1324
1325 alist->InterpolationChanged.connect (
1326 _list_connections, invalidator (*this), boost::bind (&AutomationLine::interpolation_changed, this, _1), gui_context());
1327 }
1328
1329 MementoCommandBinder<AutomationList>*
memento_command_binder()1330 AutomationLine::memento_command_binder ()
1331 {
1332 return new SimpleMementoCommandBinder<AutomationList> (*alist.get());
1333 }
1334
1335 /** Set the maximum time that points on this line can be at, relative
1336 * to the start of the track or region that it is on.
1337 */
1338 void
set_maximum_time(samplecnt_t t)1339 AutomationLine::set_maximum_time (samplecnt_t t)
1340 {
1341 if (_maximum_time == t) {
1342 return;
1343 }
1344
1345 _maximum_time = t;
1346 reset ();
1347 }
1348
1349
1350 /** @return min and max x positions of points that are in the list, in session samples */
1351 pair<samplepos_t, samplepos_t>
get_point_x_range() const1352 AutomationLine::get_point_x_range () const
1353 {
1354 pair<samplepos_t, samplepos_t> r (max_samplepos, 0);
1355
1356 for (AutomationList::const_iterator i = the_list()->begin(); i != the_list()->end(); ++i) {
1357 r.first = min (r.first, session_position (i));
1358 r.second = max (r.second, session_position (i));
1359 }
1360
1361 return r;
1362 }
1363
1364 samplepos_t
session_position(AutomationList::const_iterator p) const1365 AutomationLine::session_position (AutomationList::const_iterator p) const
1366 {
1367 return _time_converter->to ((*p)->when) + _offset + _time_converter->origin_b ();
1368 }
1369
1370 void
set_offset(samplepos_t off)1371 AutomationLine::set_offset (samplepos_t off)
1372 {
1373 if (_offset == off) {
1374 return;
1375 }
1376
1377 _offset = off;
1378 reset ();
1379 }
1380