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>
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.
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.
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.
32 // 'std::isnan()' is not available in MSVC.
33 #define isnan_local(val) (bool)_isnan((double)val)
35 #define isnan_local std::isnan
41 #include "boost/shared_ptr.hpp"
43 #include "pbd/floating.h"
44 #include "pbd/memento_command.h"
45 #include "pbd/stl_delete.h"
46 #include "pbd/stacktrace.h"
48 #include "ardour/automation_list.h"
49 #include "ardour/dB.h"
50 #include "ardour/debug.h"
51 #include "ardour/parameter_types.h"
52 #include "ardour/tempo.h"
54 #include "evoral/Curve.hpp"
56 #include "canvas/debug.h"
58 #include "automation_line.h"
59 #include "control_point.h"
60 #include "gui_thread.h"
61 #include "rgb_macros.h"
62 #include "public_editor.h"
63 #include "selection.h"
64 #include "time_axis_view.h"
65 #include "point_selection.h"
66 #include "automation_time_axis.h"
67 #include "ui_config.h"
69 #include "ardour/event_type_map.h"
70 #include "ardour/session.h"
71 #include "ardour/value_as_string.h"
76 using namespace ARDOUR;
78 using namespace Editing;
80 /** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session samples.
81 * This will not be deleted by AutomationLine.
83 AutomationLine::AutomationLine (const string& name,
85 ArdourCanvas::Item& parent,
86 boost::shared_ptr<AutomationList> al,
87 const ParameterDescriptor& desc,
88 Evoral::TimeConverter<double, samplepos_t>* converter)
92 , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, samplepos_t>)
93 , _parent_group (parent)
95 , _maximum_time (max_samplepos)
100 _our_time_converter = false;
102 _our_time_converter = true;
107 update_pending = false;
108 have_timeout = false;
111 terminal_points_can_slide = true;
114 group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple(0, 1.5));
115 CANVAS_DEBUG_NAME (group, "region gain envelope group");
117 line = new ArdourCanvas::PolyLine (group);
118 CANVAS_DEBUG_NAME (line, "region gain envelope line");
119 line->set_data ("line", this);
120 line->set_outline_width (2.0);
121 line->set_covers_threshold (4.0);
123 line->Event.connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
125 trackview.session()->register_with_memento_command_factory(alist->id(), this);
127 interpolation_changed (alist->interpolation ());
132 AutomationLine::~AutomationLine ()
134 vector_delete (&control_points);
137 if (_our_time_converter) {
138 delete _time_converter;
143 AutomationLine::event_handler (GdkEvent* event)
145 return PublicEditor::instance().canvas_line_event (event, line, this);
149 AutomationLine::is_stepped() const
151 return (_desc.toggled ||
152 (alist && alist->interpolation() == AutomationList::Discrete));
156 AutomationLine::update_visibility ()
158 if (_visible & Line) {
159 /* Only show the line when there are some points, otherwise we may show an out-of-date line
160 when automation points have been removed (the line will still follow the shape of the
163 if (control_points.size() >= 2) {
169 if (_visible & ControlPoints) {
170 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
173 } else if (_visible & SelectedControlPoints) {
174 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
175 if ((*i)->selected()) {
182 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
189 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
190 if (_visible & ControlPoints) {
200 AutomationLine::get_uses_gain_mapping () const
202 switch (_desc.type) {
204 case EnvelopeAutomation:
213 AutomationLine::hide ()
215 /* leave control points setting unchanged, we are just hiding the
219 set_visibility (AutomationLine::VisibleAspects (_visible & ~Line));
223 AutomationLine::control_point_box_size ()
225 if (_height > TimeAxisView::preset_height (HeightLarger)) {
227 } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
234 AutomationLine::set_height (guint32 h)
239 double bsz = control_point_box_size();
241 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
242 (*i)->set_size (bsz);
246 line->set_fill_y1 (_height);
248 line->set_fill_y1 (0);
255 AutomationLine::set_line_color (uint32_t color)
258 line->set_outline_color (color);
260 Gtkmm2ext::SVAModifier mod = UIConfiguration::instance().modifier ("automation line fill");
262 line->set_fill_color ((color & 0xffffff00) + mod.a()*255);
266 AutomationLine::nth (uint32_t n)
268 if (n < control_points.size()) {
269 return control_points[n];
276 AutomationLine::nth (uint32_t n) const
278 if (n < control_points.size()) {
279 return control_points[n];
286 AutomationLine::modify_point_y (ControlPoint& cp, double y)
288 /* clamp y-coord appropriately. y is supposed to be a normalized fraction (0.0-1.0),
289 and needs to be converted to a canvas unit distance.
294 y = _height - (y * _height);
296 double const x = trackview.editor().sample_to_pixel_unrounded (_time_converter->to((*cp.model())->when) - _offset);
298 trackview.editor().begin_reversible_command (_("automation event move"));
299 trackview.editor().session()->add_command (
300 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
302 cp.move_to (x, y, ControlPoint::Full);
305 sync_model_with_view_point (cp);
308 reset_line_coords (cp);
310 if (line_points.size() > 1) {
311 line->set_steps (line_points, is_stepped());
314 update_pending = false;
316 trackview.editor().session()->add_command (
317 new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state()));
319 trackview.editor().commit_reversible_command ();
320 trackview.editor().session()->set_dirty ();
324 AutomationLine::reset_line_coords (ControlPoint& cp)
326 if (cp.view_index() < line_points.size()) {
327 line_points[cp.view_index()].x = cp.get_x ();
328 line_points[cp.view_index()].y = cp.get_y ();
333 AutomationLine::sync_model_with_view_points (list<ControlPoint*> cp)
335 update_pending = true;
338 for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
339 moved = sync_model_with_view_point (**i) || moved;
346 AutomationLine::get_verbose_cursor_string (double fraction) const
348 return fraction_to_string (fraction);
352 AutomationLine::get_verbose_cursor_relative_string (double fraction, double delta) const
354 std::string s = fraction_to_string (fraction);
355 std::string d = delta_to_string (delta);
356 return s + " (" + d + ")";
360 * @param fraction y fraction
361 * @return string representation of this value, using dB if appropriate.
364 AutomationLine::fraction_to_string (double fraction) const
366 view_to_model_coord_y (fraction);
367 return ARDOUR::value_as_string (_desc, fraction);
371 AutomationLine::delta_to_string (double delta) const
373 if (!get_uses_gain_mapping () && _desc.logarithmic) {
374 return "x " + ARDOUR::value_as_string (_desc, delta);
376 return "\u0394 " + ARDOUR::value_as_string (_desc, delta);
381 * @param s Value string in the form as returned by fraction_to_string.
382 * @return Corresponding y fraction.
385 AutomationLine::string_to_fraction (string const & s) const
388 sscanf (s.c_str(), "%lf", &v);
390 switch (_desc.type) {
392 case EnvelopeAutomation:
394 if (s == "-inf") { /* translation */
397 v = dB_to_coefficient (v);
403 model_to_view_coord_y (v);
407 /** Start dragging a single point, possibly adding others if the supplied point is selected and there
408 * are other selected points.
410 * @param cp Point to drag.
411 * @param x Initial x position (units).
412 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
415 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
417 trackview.editor().session()->add_command (
418 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
420 _drag_points.clear ();
421 _drag_points.push_back (cp);
423 if (cp->selected ()) {
424 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
425 if (*i != cp && (*i)->selected()) {
426 _drag_points.push_back (*i);
431 start_drag_common (x, fraction);
434 /** Start dragging a line vertically (with no change in x)
435 * @param i1 Control point index of the `left' point on the line.
436 * @param i2 Control point index of the `right' point on the line.
437 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
440 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
442 trackview.editor().session()->add_command (
443 new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0));
445 _drag_points.clear ();
447 for (uint32_t i = i1; i <= i2; i++) {
448 _drag_points.push_back (nth (i));
451 start_drag_common (0, fraction);
454 /** Start dragging multiple points (with no change in x)
455 * @param cp Points to drag.
456 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
459 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
461 trackview.editor().session()->add_command (
462 new MementoCommand<AutomationList> (memento_command_binder(), state, 0));
465 start_drag_common (0, fraction);
468 struct ControlPointSorter
470 bool operator() (ControlPoint const * a, ControlPoint const * b) const {
471 if (floateq (a->get_x(), b->get_x(), 1)) {
472 return a->view_index() < b->view_index();
474 return a->get_x() < b->get_x();
478 AutomationLine::ContiguousControlPoints::ContiguousControlPoints (AutomationLine& al)
479 : line (al), before_x (0), after_x (DBL_MAX)
484 AutomationLine::ContiguousControlPoints::compute_x_bounds (PublicEditor& e)
486 uint32_t sz = size();
488 if (sz > 0 && sz < line.npoints()) {
489 const TempoMap& map (e.session()->tempo_map());
491 /* determine the limits on x-axis motion for this
492 contiguous range of control points
495 if (front()->view_index() > 0) {
496 before_x = line.nth (front()->view_index() - 1)->get_x();
498 const samplepos_t pos = e.pixel_to_sample(before_x);
499 const Meter& meter = map.meter_at_sample (pos);
500 const samplecnt_t len = ceil (meter.samples_per_bar (map.tempo_at_sample (pos), e.session()->sample_rate())
501 / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()) );
502 const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
504 before_x += one_tick_in_pixels;
507 /* if our last point has a point after it in the line,
508 we have an "after" bound
511 if (back()->view_index() < (line.npoints() - 1)) {
512 after_x = line.nth (back()->view_index() + 1)->get_x();
514 const samplepos_t pos = e.pixel_to_sample(after_x);
515 const Meter& meter = map.meter_at_sample (pos);
516 const samplecnt_t len = ceil (meter.samples_per_bar (map.tempo_at_sample (pos), e.session()->sample_rate())
517 / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()));
518 const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
520 after_x -= one_tick_in_pixels;
526 AutomationLine::ContiguousControlPoints::clamp_dx (double dx)
532 /* get the maximum distance we can move any of these points along the x-axis
535 double tx; /* possible position a point would move to, given dx */
539 /* check the last point, since we're moving later in time */
542 /* check the first point, since we're moving earlier in time */
546 tx = cp->get_x() + dx; // new possible position if we just add the motion
547 tx = max (tx, before_x); // can't move later than following point
548 tx = min (tx, after_x); // can't move earlier than preceeding point
549 return tx - cp->get_x ();
553 AutomationLine::ContiguousControlPoints::move (double dx, double dvalue)
555 for (std::list<ControlPoint*>::iterator i = begin(); i != end(); ++i) {
556 // compute y-axis delta
557 double view_y = 1.0 - (*i)->get_y() / line.height();
558 line.view_to_model_coord_y (view_y);
559 line.apply_delta (view_y, dvalue);
560 line.model_to_view_coord_y (view_y);
561 view_y = (1.0 - view_y) * line.height();
563 (*i)->move_to ((*i)->get_x() + dx, view_y, ControlPoint::Full);
564 line.reset_line_coords (**i);
568 /** Common parts of starting a drag.
569 * @param x Starting x position in units, or 0 if x is being ignored.
570 * @param fraction Starting y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
573 AutomationLine::start_drag_common (double x, float fraction)
577 _last_drag_fraction = fraction;
578 _drag_had_movement = false;
581 /* they are probably ordered already, but we have to make sure */
583 _drag_points.sort (ControlPointSorter());
587 /** Should be called to indicate motion during a drag.
588 * @param x New x position of the drag in canvas units, or undefined if ignore_x == true.
589 * @param fraction New y fraction.
590 * @return x position and y fraction that were actually used (once clamped).
593 AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool with_push, uint32_t& final_index)
595 if (_drag_points.empty()) {
596 return pair<double,float> (fraction, _desc.is_linear () ? 0 : 1);
599 double dx = ignore_x ? 0 : (x - _drag_x);
600 double dy = fraction - _last_drag_fraction;
602 if (!_drag_had_movement) {
604 /* "first move" ... do some stuff that we don't want to do if
605 no motion ever took place, but need to do before we handle
609 /* partition the points we are dragging into (potentially several)
610 * set(s) of contiguous points. this will not happen with a normal
611 * drag, but if the user does a discontiguous selection, it can.
614 uint32_t expected_view_index = 0;
617 for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
618 if (i == _drag_points.begin() || (*i)->view_index() != expected_view_index) {
619 contig.reset (new ContiguousControlPoints (*this));
620 contiguous_points.push_back (contig);
622 contig->push_back (*i);
623 expected_view_index = (*i)->view_index() + 1;
626 if (contiguous_points.back()->empty()) {
627 contiguous_points.pop_back ();
630 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
631 (*ccp)->compute_x_bounds (trackview.editor());
633 _drag_had_movement = true;
636 /* OK, now on to the stuff related to *this* motion event. First, for
637 * each contiguous range, figure out the maximum x-axis motion we are
638 * allowed (because of neighbouring points that are not moving.
640 * if we are moving forwards with push, we don't need to do this,
641 * since all later points will move too.
644 if (dx < 0 || ((dx > 0) && !with_push)) {
645 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
646 double dxt = (*ccp)->clamp_dx (dx);
647 if (fabs (dxt) < fabs (dx)) {
653 /* compute deflection */
656 double value0 = _last_drag_fraction;
657 double value1 = _last_drag_fraction + dy;
658 view_to_model_coord_y (value0);
659 view_to_model_coord_y (value1);
660 delta_value = compute_delta (value0, value1);
663 /* special case -inf */
664 if (delta_value == 0 && dy > 0 && !_desc.is_linear ()) {
665 assert (_desc.lower == 0);
670 for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
671 double vy = 1.0 - (*i)->get_y() / _height;
672 view_to_model_coord_y (vy);
673 const double orig = vy;
674 apply_delta (vy, delta_value);
675 if (vy < _desc.lower) {
676 delta_value = compute_delta (orig, _desc.lower);
678 if (vy > _desc.upper) {
679 delta_value = compute_delta (orig, _desc.upper);
684 /* and now move each section */
685 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
686 (*ccp)->move (dx, delta_value);
690 final_index = contiguous_points.back()->back()->view_index () + 1;
692 uint32_t i = final_index;
693 while ((p = nth (i)) != 0 && p->can_slide()) {
694 p->move_to (p->get_x() + dx, p->get_y(), ControlPoint::Full);
695 reset_line_coords (*p);
700 /* update actual line coordinates (will queue a redraw) */
702 if (line_points.size() > 1) {
703 line->set_steps (line_points, is_stepped());
707 /* calculate effective delta */
708 ControlPoint* cp = _drag_points.front();
709 double vy = 1.0 - cp->get_y() / (double)_height;
710 view_to_model_coord_y (vy);
711 float val = (*(cp->model ()))->value;
712 float effective_delta = _desc.compute_delta (val, vy);
713 /* special case recovery from -inf */
714 if (val == 0 && effective_delta == 0 && vy > 0) {
715 assert (!_desc.is_linear ());
716 effective_delta = HUGE_VAL; // +Infinity
719 double const result_frac = _last_drag_fraction + dy;
720 _drag_distance += dx;
722 _last_drag_fraction = result_frac;
723 did_push = with_push;
725 return pair<float, float> (result_frac, effective_delta);
728 /** Should be called to indicate the end of a drag */
730 AutomationLine::end_drag (bool with_push, uint32_t final_index)
732 if (!_drag_had_movement) {
737 bool moved = sync_model_with_view_points (_drag_points);
741 uint32_t i = final_index;
742 while ((p = nth (i)) != 0 && p->can_slide()) {
743 moved = sync_model_with_view_point (*p) || moved;
750 update_pending = false;
753 /* A point has moved as a result of sync (clamped to integer or boolean
754 value), update line accordingly. */
755 line->set_steps (line_points, is_stepped());
758 trackview.editor().session()->add_command (
759 new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state()));
761 trackview.editor().session()->set_dirty ();
764 contiguous_points.clear ();
768 AutomationLine::sync_model_with_view_point (ControlPoint& cp)
770 /* find out where the visual control point is.
771 initial results are in canvas units. ask the
772 line to convert them to something relevant.
775 double view_x = cp.get_x();
776 double view_y = 1.0 - cp.get_y() / (double)_height;
778 /* if xval has not changed, set it directly from the model to avoid rounding errors */
780 if (view_x == trackview.editor().sample_to_pixel_unrounded (_time_converter->to ((*cp.model())->when)) - _offset) {
781 view_x = (*cp.model())->when - _offset;
783 view_x = trackview.editor().pixel_to_sample (view_x);
784 view_x = _time_converter->from (view_x + _offset);
787 update_pending = true;
789 view_to_model_coord_y (view_y);
791 alist->modify (cp.model(), view_x, view_y);
793 /* convert back from model to view y for clamping position (for integer/boolean/etc) */
794 model_to_view_coord_y (view_y);
795 const double point_y = _height - (view_y * _height);
796 if (point_y != cp.get_y()) {
797 cp.move_to (cp.get_x(), point_y, ControlPoint::Full);
798 reset_line_coords (cp);
806 AutomationLine::control_points_adjacent (double xval, uint32_t & before, uint32_t& after)
808 ControlPoint *bcp = 0;
809 ControlPoint *acp = 0;
812 unit_xval = trackview.editor().sample_to_pixel_unrounded (xval);
814 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
816 if ((*i)->get_x() <= unit_xval) {
818 if (!bcp || (*i)->get_x() > bcp->get_x()) {
820 before = bcp->view_index();
823 } else if ((*i)->get_x() > unit_xval) {
825 after = acp->view_index();
834 AutomationLine::is_last_point (ControlPoint& cp)
836 // If the list is not empty, and the point is the last point in the list
838 if (alist->empty()) {
842 AutomationList::const_iterator i = alist->end();
845 if (cp.model() == i) {
853 AutomationLine::is_first_point (ControlPoint& cp)
855 // If the list is not empty, and the point is the first point in the list
857 if (!alist->empty() && cp.model() == alist->begin()) {
864 // This is copied into AudioRegionGainLine
866 AutomationLine::remove_point (ControlPoint& cp)
868 trackview.editor().begin_reversible_command (_("remove control point"));
869 XMLNode &before = alist->get_state();
871 trackview.editor ().get_selection ().clear_points ();
872 alist->erase (cp.model());
874 trackview.editor().session()->add_command(
875 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
877 trackview.editor().commit_reversible_command ();
878 trackview.editor().session()->set_dirty ();
881 /** Get selectable points within an area.
882 * @param start Start position in session samples.
883 * @param end End position in session samples.
884 * @param bot Bottom y range, as a fraction of line height, where 0 is the bottom of the line.
885 * @param top Top y range, as a fraction of line height, where 0 is the bottom of the line.
886 * @param result Filled in with selectable things; in this case, ControlPoints.
889 AutomationLine::get_selectables (samplepos_t start, samplepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
891 /* convert fractions to display coordinates with 0 at the top of the track */
892 double const bot_track = (1 - topfrac) * trackview.current_height ();
893 double const top_track = (1 - botfrac) * trackview.current_height ();
895 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
896 double const model_when = (*(*i)->model())->when;
898 /* model_when is relative to the start of the source, so we just need to add on the origin_b here
899 (as it is the session sample position of the start of the source)
902 samplepos_t const session_samples_when = _time_converter->to (model_when) + _time_converter->origin_b ();
904 if (session_samples_when >= start && session_samples_when <= end && (*i)->get_y() >= bot_track && (*i)->get_y() <= top_track) {
905 results.push_back (*i);
911 AutomationLine::get_inverted_selectables (Selection&, list<Selectable*>& /*results*/)
917 AutomationLine::set_selected_points (PointSelection const & points)
919 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
920 (*i)->set_selected (false);
923 for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
924 (*i)->set_selected (true);
927 if (points.empty()) {
928 remove_visibility (SelectedControlPoints);
930 add_visibility (SelectedControlPoints);
937 AutomationLine::set_colors ()
939 set_line_color (UIConfiguration::instance().color ("automation line"));
940 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
946 AutomationLine::list_changed ()
948 DEBUG_TRACE (DEBUG::Automation, string_compose ("\tline changed, existing update pending? %1\n", update_pending));
950 if (!update_pending) {
951 update_pending = true;
952 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationLine::queue_reset, this));
957 AutomationLine::reset_callback (const Evoral::ControlList& events)
963 if (events.empty()) {
964 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
967 control_points.clear ();
972 /* hide all existing points, and the line */
974 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
981 Evoral::ControlList& e = const_cast<Evoral::ControlList&> (events);
983 for (AutomationList::iterator ai = e.begin(); ai != e.end(); ++ai, ++pi) {
985 double tx = (*ai)->when;
986 double ty = (*ai)->value;
988 /* convert from model coordinates to canonical view coordinates */
990 model_to_view_coord (tx, ty);
992 if (isnan_local (tx) || isnan_local (ty)) {
993 warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
998 if (tx >= max_samplepos || tx < 0 || tx >= _maximum_time) {
1002 /* convert x-coordinate to a canvas unit coordinate (this takes
1003 * zoom and scroll into account).
1006 tx = trackview.editor().sample_to_pixel_unrounded (tx);
1008 /* convert from canonical view height (0..1.0) to actual
1009 * height coordinates (using X11's top-left rooted system)
1012 ty = _height - (ty * _height);
1014 add_visible_control_point (vp, pi, tx, ty, ai, np);
1018 /* discard extra CP's to avoid confusing ourselves */
1020 while (control_points.size() > vp) {
1021 ControlPoint* cp = control_points.back();
1022 control_points.pop_back ();
1026 if (!terminal_points_can_slide) {
1027 control_points.back()->set_can_slide(false);
1032 /* reset the line coordinates given to the CanvasLine */
1034 while (line_points.size() < vp) {
1035 line_points.push_back (ArdourCanvas::Duple (0,0));
1038 while (line_points.size() > vp) {
1039 line_points.pop_back ();
1042 for (uint32_t n = 0; n < vp; ++n) {
1043 line_points[n].x = control_points[n]->get_x();
1044 line_points[n].y = control_points[n]->get_y();
1047 line->set_steps (line_points, is_stepped());
1049 update_visibility ();
1052 set_selected_points (trackview.editor().get_selection().points);
1056 AutomationLine::reset ()
1058 DEBUG_TRACE (DEBUG::Automation, "\t\tLINE RESET\n");
1059 update_pending = false;
1060 have_timeout = false;
1066 /* TODO: abort any drags in progress, e.g. draging points while writing automation
1067 * (the control-point model, used by AutomationLine::drag_motion, will be invalid).
1069 * Note: reset() may also be called from an aborted drag (LineDrag::aborted)
1070 * maybe abort in list_changed(), interpolation_changed() and ... ?
1074 alist->apply_to_points (*this, &AutomationLine::reset_callback);
1078 AutomationLine::queue_reset ()
1080 /* this must be called from the GUI thread */
1082 if (trackview.editor().session()->transport_rolling() && alist->automation_write()) {
1083 /* automation write pass ... defer to a timeout */
1084 /* redraw in 1/4 second */
1085 if (!have_timeout) {
1086 DEBUG_TRACE (DEBUG::Automation, "\tqueue timeout\n");
1087 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &AutomationLine::reset), false), 250);
1088 have_timeout = true;
1090 DEBUG_TRACE (DEBUG::Automation, "\ttimeout already queued, change ignored\n");
1098 AutomationLine::clear ()
1100 /* parent must create and commit command */
1101 XMLNode &before = alist->get_state();
1104 trackview.editor().session()->add_command (
1105 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
1109 AutomationLine::set_list (boost::shared_ptr<ARDOUR::AutomationList> list)
1117 AutomationLine::add_visibility (VisibleAspects va)
1119 VisibleAspects old = _visible;
1121 _visible = VisibleAspects (_visible | va);
1123 if (old != _visible) {
1124 update_visibility ();
1129 AutomationLine::set_visibility (VisibleAspects va)
1131 if (_visible != va) {
1133 update_visibility ();
1138 AutomationLine::remove_visibility (VisibleAspects va)
1140 VisibleAspects old = _visible;
1142 _visible = VisibleAspects (_visible & ~va);
1144 if (old != _visible) {
1145 update_visibility ();
1150 AutomationLine::track_entered()
1152 add_visibility (ControlPoints);
1156 AutomationLine::track_exited()
1158 remove_visibility (ControlPoints);
1162 AutomationLine::get_state (void)
1164 /* function as a proxy for the model */
1165 return alist->get_state();
1169 AutomationLine::set_state (const XMLNode &node, int version)
1171 /* function as a proxy for the model */
1172 return alist->set_state (node, version);
1176 AutomationLine::view_to_model_coord (double& x, double& y) const
1178 x = _time_converter->from (x);
1179 view_to_model_coord_y (y);
1183 AutomationLine::view_to_model_coord_y (double& y) const
1185 if (alist->default_interpolation () != alist->interpolation()) {
1186 switch (alist->interpolation()) {
1187 case AutomationList::Discrete:
1188 /* toggles and MIDI only -- see is_stepped() */
1189 assert (alist->default_interpolation () == AutomationList::Linear);
1191 case AutomationList::Linear:
1192 y = y * (_desc.upper - _desc.lower) + _desc.lower;
1195 /* types that default to linear, can't be use
1196 * Logarithmic or Exponential interpolation.
1197 * "Curved" is invalid for automation (only x-fads)
1203 y = _desc.from_interface (y);
1207 AutomationLine::compute_delta (double from, double to) const
1209 return _desc.compute_delta (from, to);
1213 AutomationLine::apply_delta (double& val, double delta) const
1215 if (val == 0 && !_desc.is_linear () && delta >= 1.0) {
1216 /* recover from -inf */
1217 val = 1.0 / _height;
1218 view_to_model_coord_y (val);
1221 val = _desc.apply_delta (val, delta);
1225 AutomationLine::model_to_view_coord_y (double& y) const
1227 if (alist->default_interpolation () != alist->interpolation()) {
1228 switch (alist->interpolation()) {
1229 case AutomationList::Discrete:
1230 /* toggles and MIDI only -- see is_stepped */
1231 assert (alist->default_interpolation () == AutomationList::Linear);
1233 case AutomationList::Linear:
1234 y = (y - _desc.lower) / (_desc.upper - _desc.lower);
1237 /* types that default to linear, can't be use
1238 * Logarithmic or Exponential interpolation.
1239 * "Curved" is invalid for automation (only x-fads)
1245 y = _desc.to_interface (y);
1249 AutomationLine::model_to_view_coord (double& x, double& y) const
1251 model_to_view_coord_y (y);
1252 x = _time_converter->to (x) - _offset;
1255 /** Called when our list has announced that its interpolation style has changed */
1257 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
1259 if (line_points.size() > 1) {
1261 line->set_steps(line_points, is_stepped());
1266 AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty,
1267 AutomationList::iterator model, uint32_t npoints)
1269 ControlPoint::ShapeType shape;
1271 if (view_index >= control_points.size()) {
1273 /* make sure we have enough control points */
1275 ControlPoint* ncp = new ControlPoint (*this);
1276 ncp->set_size (control_point_box_size ());
1278 control_points.push_back (ncp);
1281 if (!terminal_points_can_slide) {
1283 control_points[view_index]->set_can_slide (false);
1285 shape = ControlPoint::Start;
1287 shape = ControlPoint::Full;
1289 } else if (pi == npoints - 1) {
1290 control_points[view_index]->set_can_slide (false);
1291 shape = ControlPoint::End;
1293 control_points[view_index]->set_can_slide (true);
1294 shape = ControlPoint::Full;
1297 control_points[view_index]->set_can_slide (true);
1298 shape = ControlPoint::Full;
1301 control_points[view_index]->reset (tx, ty, model, view_index, shape);
1303 /* finally, control visibility */
1305 if (_visible & ControlPoints) {
1306 control_points[view_index]->show ();
1308 control_points[view_index]->hide ();
1313 AutomationLine::connect_to_list ()
1315 _list_connections.drop_connections ();
1317 alist->StateChanged.connect (_list_connections, invalidator (*this), boost::bind (&AutomationLine::list_changed, this), gui_context());
1319 alist->InterpolationChanged.connect (
1320 _list_connections, invalidator (*this), boost::bind (&AutomationLine::interpolation_changed, this, _1), gui_context());
1323 MementoCommandBinder<AutomationList>*
1324 AutomationLine::memento_command_binder ()
1326 return new SimpleMementoCommandBinder<AutomationList> (*alist.get());
1329 /** Set the maximum time that points on this line can be at, relative
1330 * to the start of the track or region that it is on.
1333 AutomationLine::set_maximum_time (samplecnt_t t)
1335 if (_maximum_time == t) {
1344 /** @return min and max x positions of points that are in the list, in session samples */
1345 pair<samplepos_t, samplepos_t>
1346 AutomationLine::get_point_x_range () const
1348 pair<samplepos_t, samplepos_t> r (max_samplepos, 0);
1350 for (AutomationList::const_iterator i = the_list()->begin(); i != the_list()->end(); ++i) {
1351 r.first = min (r.first, session_position (i));
1352 r.second = max (r.second, session_position (i));
1359 AutomationLine::session_position (AutomationList::const_iterator p) const
1361 return _time_converter->to ((*p)->when) + _offset + _time_converter->origin_b ();
1365 AutomationLine::set_offset (samplepos_t off)
1367 if (_offset == off) {