Fix playhead smoothing when auditioning.
[ardour.git] / gtk2_ardour / automation_line.cc
index 8a11f199affa103206f2ff824a093a5484055c29..7e247044dde7b5c7ea39775d344376543406a4f0 100644 (file)
 
 */
 
+#include <cmath>
+
 #ifdef COMPILER_MSVC
 #include <float.h>
-/* isinf() & isnan() are C99 standards, which older MSVC doesn't provide */
-#define isinf(val) !((bool)_finite((double)val))
-#define isnan(val) (bool)_isnan((double)val)
+
+// 'std::isnan()' is not available in MSVC.
+#define isnan_local(val) (bool)_isnan((double)val)
+#else
+#define isnan_local std::isnan
 #endif
 
-#include <cmath>
 #include <climits>
 #include <vector>
-#include <fstream>
 
 #include "boost/shared_ptr.hpp"
 
@@ -39,6 +41,8 @@
 #include "ardour/automation_list.h"
 #include "ardour/dB.h"
 #include "ardour/debug.h"
+#include "ardour/parameter_types.h"
+#include "ardour/tempo.h"
 
 #include "evoral/Curve.hpp"
 
 #include "control_point.h"
 #include "gui_thread.h"
 #include "rgb_macros.h"
-#include "ardour_ui.h"
 #include "public_editor.h"
-#include "utils.h"
 #include "selection.h"
 #include "time_axis_view.h"
 #include "point_selection.h"
 #include "automation_time_axis.h"
+#include "ui_config.h"
 
 #include "ardour/event_type_map.h"
 #include "ardour/session.h"
+#include "ardour/value_as_string.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 using namespace Editing;
 
-/** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session frames.
+/** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session samples.
  *  This will not be deleted by AutomationLine.
  */
-AutomationLine::AutomationLine (const string& name, TimeAxisView& tv, ArdourCanvas::Group& parent,
-                                boost::shared_ptr<AutomationList> al,
-                                Evoral::TimeConverter<double, framepos_t>* converter)
+AutomationLine::AutomationLine (const string&                              name,
+                                TimeAxisView&                              tv,
+                                ArdourCanvas::Item&                        parent,
+                                boost::shared_ptr<AutomationList>          al,
+                                const ParameterDescriptor&                 desc,
+                                Evoral::TimeConverter<double, samplepos_t>* converter)
        : trackview (tv)
        , _name (name)
        , alist (al)
-       , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, framepos_t>)
+       , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, samplepos_t>)
        , _parent_group (parent)
        , _offset (0)
-       , _maximum_time (max_framepos)
+       , _maximum_time (max_samplepos)
+       , _fill (false)
+       , _desc (desc)
 {
        if (converter) {
-               _time_converter = converter;
                _our_time_converter = false;
        } else {
-               _time_converter = new Evoral::IdentityConverter<double, framepos_t>;
                _our_time_converter = true;
        }
-       
+
        _visible = Line;
 
        update_pending = false;
        have_timeout = false;
-       _uses_gain_mapping = false;
        no_draw = false;
        _is_boolean = false;
        terminal_points_can_slide = true;
        _height = 0;
 
-       group = new ArdourCanvas::Group (&parent);
+       group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple(0, 1.5));
        CANVAS_DEBUG_NAME (group, "region gain envelope group");
 
        line = new ArdourCanvas::PolyLine (group);
        CANVAS_DEBUG_NAME (line, "region gain envelope line");
        line->set_data ("line", this);
        line->set_outline_width (2.0);
+       line->set_covers_threshold (4.0);
 
        line->Event.connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
 
        trackview.session()->register_with_memento_command_factory(alist->id(), this);
 
-       if (alist->parameter().type() == GainAutomation ||
-           alist->parameter().type() == EnvelopeAutomation) {
-               set_uses_gain_mapping (true);
-       }
-
        interpolation_changed (alist->interpolation ());
 
        connect_to_list ();
@@ -136,65 +138,83 @@ AutomationLine::event_handler (GdkEvent* event)
        return PublicEditor::instance().canvas_line_event (event, line, this);
 }
 
+bool
+AutomationLine::is_stepped() const
+{
+       return (_desc.toggled ||
+               (alist && alist->interpolation() == AutomationList::Discrete));
+}
+
 void
-AutomationLine::show ()
+AutomationLine::update_visibility ()
 {
        if (_visible & Line) {
-               /* Only show the line there are some points, otherwise we may show an out-of-date line
+               /* Only show the line when there are some points, otherwise we may show an out-of-date line
                   when automation points have been removed (the line will still follow the shape of the
                   old points).
                */
-               if (alist->interpolation() != AutomationList::Discrete && control_points.size() >= 2) {
+               if (control_points.size() >= 2) {
                        line->show();
                } else {
                        line->hide ();
                }
-       } else {
-               line->hide();
-               /* if the line is not visible, then no control points should be visible */
-               for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       (*i)->hide ();
-               }
-               return;
-       }
 
-       if (_visible & ControlPoints) {
-               for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       (*i)->show ();
-               }
-       } else if (_visible & SelectedControlPoints) {
-               for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       if ((*i)->get_selected()) {
+               if (_visible & ControlPoints) {
+                       for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                                (*i)->show ();
-                       } else {
+                       }
+               } else if (_visible & SelectedControlPoints) {
+                       for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
+                               if ((*i)->selected()) {
+                                       (*i)->show ();
+                               } else {
+                                       (*i)->hide ();
+                               }
+                       }
+               } else {
+                       for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                                (*i)->hide ();
                        }
                }
+
        } else {
+               line->hide ();
                for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       (*i)->hide ();
+                       if (_visible & ControlPoints) {
+                               (*i)->show ();
+                       } else {
+                               (*i)->hide ();
+                       }
                }
        }
 }
 
+bool
+AutomationLine::get_uses_gain_mapping () const
+{
+       switch (_desc.type) {
+               case GainAutomation:
+               case EnvelopeAutomation:
+               case TrimAutomation:
+                       return true;
+               default:
+                       return false;
+       }
+}
+
 void
 AutomationLine::hide ()
 {
        /* leave control points setting unchanged, we are just hiding the
-          overall line 
+          overall line
        */
-       
+
        set_visibility (AutomationLine::VisibleAspects (_visible & ~Line));
 }
 
 double
 AutomationLine::control_point_box_size ()
 {
-       if (alist->interpolation() == AutomationList::Discrete) {
-               return max((_height*4.0) / (double)(alist->parameter().max() - alist->parameter().min()),
-                          4.0);
-       }
-
        if (_height > TimeAxisView::preset_height (HeightLarger)) {
                return 8.0;
        } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
@@ -215,6 +235,11 @@ AutomationLine::set_height (guint32 h)
                        (*i)->set_size (bsz);
                }
 
+               if (_fill) {
+                       line->set_fill_y1 (_height);
+               } else {
+                       line->set_fill_y1 (0);
+               }
                reset ();
        }
 }
@@ -224,15 +249,10 @@ AutomationLine::set_line_color (uint32_t color)
 {
        _line_color = color;
        line->set_outline_color (color);
-}
 
-void
-AutomationLine::set_uses_gain_mapping (bool yn)
-{
-       if (yn != _uses_gain_mapping) {
-               _uses_gain_mapping = yn;
-               reset ();
-       }
+       Gtkmm2ext::SVAModifier mod = UIConfiguration::instance().modifier ("automation line fill");
+
+       line->set_fill_color ((color & 0xffffff00) + mod.a()*255);
 }
 
 ControlPoint*
@@ -268,28 +288,28 @@ AutomationLine::modify_point_y (ControlPoint& cp, double y)
 
        double const x = trackview.editor().sample_to_pixel_unrounded (_time_converter->to((*cp.model())->when) - _offset);
 
-       trackview.editor().session()->begin_reversible_command (_("automation event move"));
+       trackview.editor().begin_reversible_command (_("automation event move"));
        trackview.editor().session()->add_command (
                new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
 
        cp.move_to (x, y, ControlPoint::Full);
 
+       alist->freeze ();
+       sync_model_with_view_point (cp);
+       alist->thaw ();
+
        reset_line_coords (cp);
 
        if (line_points.size() > 1) {
-               line->set (line_points);
+               line->set_steps (line_points, is_stepped());
        }
 
-       alist->freeze ();
-       sync_model_with_view_point (cp);
-       alist->thaw ();
-
        update_pending = false;
 
        trackview.editor().session()->add_command (
                new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state()));
 
-       trackview.editor().session()->commit_reversible_command ();
+       trackview.editor().commit_reversible_command ();
        trackview.editor().session()->set_dirty ();
 }
 
@@ -302,50 +322,31 @@ AutomationLine::reset_line_coords (ControlPoint& cp)
        }
 }
 
-void
+bool
 AutomationLine::sync_model_with_view_points (list<ControlPoint*> cp)
 {
        update_pending = true;
 
+       bool moved = false;
        for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
-               sync_model_with_view_point (**i);
+               moved = sync_model_with_view_point (**i) || moved;
        }
+
+       return moved;
 }
 
 string
 AutomationLine::get_verbose_cursor_string (double fraction) const
 {
-       std::string s = fraction_to_string (fraction);
-       if (_uses_gain_mapping) {
-               s += " dB";
-       }
-
-       return s;
+       return fraction_to_string (fraction);
 }
 
 string
-AutomationLine::get_verbose_cursor_relative_string (double original, double fraction) const
+AutomationLine::get_verbose_cursor_relative_string (double fraction, double delta) const
 {
        std::string s = fraction_to_string (fraction);
-       if (_uses_gain_mapping) {
-               s += " dB";
-       }
-
-       std::string d = fraction_to_relative_string (original, fraction);
-
-       if (!d.empty()) {
-
-               s += " (\u0394";
-               s += d;
-
-               if (_uses_gain_mapping) {
-                       s += " dB";
-               }
-
-               s += ')';
-       }
-
-       return s;
+       std::string d = delta_to_string (delta);
+       return s + " (" + d + ")";
 }
 
 /**
@@ -355,64 +356,18 @@ AutomationLine::get_verbose_cursor_relative_string (double original, double frac
 string
 AutomationLine::fraction_to_string (double fraction) const
 {
-       char buf[32];
-
-       if (_uses_gain_mapping) {
-               if (fraction == 0.0) {
-                       snprintf (buf, sizeof (buf), "-inf");
-               } else {
-                       snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, Config->get_max_gain())));
-               }
-       } else {
-               view_to_model_coord_y (fraction);
-               if (EventTypeMap::instance().is_integer (alist->parameter())) {
-                       snprintf (buf, sizeof (buf), "%d", (int)fraction);
-               } else {
-                       snprintf (buf, sizeof (buf), "%.2f", fraction);
-               }
-       }
-
-       return buf;
+       view_to_model_coord_y (fraction);
+       return ARDOUR::value_as_string (_desc, fraction);
 }
 
-/**
- *  @param original an old y-axis fraction
- *  @param fraction the new y fraction
- *  @return string representation of the difference between original and fraction, using dB if appropriate.
- */
 string
-AutomationLine::fraction_to_relative_string (double original, double fraction) const
+AutomationLine::delta_to_string (double delta) const
 {
-       char buf[32];
-
-       if (original == fraction) {
-               return "0";
-       }
-
-       if (_uses_gain_mapping) {
-               if (original == 0.0) {
-                       /* there is no sensible representation of a relative
-                          change from -inf dB, so return an empty string.
-                       */
-                       return "";
-               } else if (fraction == 0.0) {
-                       snprintf (buf, sizeof (buf), "-inf");
-               } else {
-                       double old_db = accurate_coefficient_to_dB (slider_position_to_gain_with_max (original, Config->get_max_gain()));
-                       double new_db = accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, Config->get_max_gain()));
-                       snprintf (buf, sizeof (buf), "%.1f", new_db - old_db);
-               }
+       if (!get_uses_gain_mapping () && _desc.logarithmic) {
+               return "x " + ARDOUR::value_as_string (_desc, delta);
        } else {
-               view_to_model_coord_y (original);
-               view_to_model_coord_y (fraction);
-               if (EventTypeMap::instance().is_integer (alist->parameter())) {
-                       snprintf (buf, sizeof (buf), "%d", (int)fraction - (int)original);
-               } else {
-                       snprintf (buf, sizeof (buf), "%.2f", fraction - original);
-               }
+               return "\u0394 " + ARDOUR::value_as_string (_desc, delta);
        }
-
-       return buf;
 }
 
 /**
@@ -422,20 +377,23 @@ AutomationLine::fraction_to_relative_string (double original, double fraction) c
 double
 AutomationLine::string_to_fraction (string const & s) const
 {
-       if (s == "-inf") {
-               return 0;
-       }
-
        double v;
        sscanf (s.c_str(), "%lf", &v);
 
-       if (_uses_gain_mapping) {
-               v = gain_to_slider_position_with_max (dB_to_coefficient (v), Config->get_max_gain());
-       } else {
-               double dummy = 0.0;
-               model_to_view_coord (dummy, v);
+       switch (_desc.type) {
+               case GainAutomation:
+               case EnvelopeAutomation:
+               case TrimAutomation:
+                       if (s == "-inf") { /* translation */
+                               v = 0;
+                       } else {
+                               v = dB_to_coefficient (v);
+                       }
+                       break;
+               default:
+                       break;
        }
-
+       model_to_view_coord_y (v);
        return v;
 }
 
@@ -449,16 +407,15 @@ AutomationLine::string_to_fraction (string const & s) const
 void
 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
 {
-       trackview.editor().session()->begin_reversible_command (_("automation event move"));
        trackview.editor().session()->add_command (
                new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
 
        _drag_points.clear ();
        _drag_points.push_back (cp);
 
-       if (cp->get_selected ()) {
+       if (cp->selected ()) {
                for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
-                       if (*i != cp && (*i)->get_selected()) {
+                       if (*i != cp && (*i)->selected()) {
                                _drag_points.push_back (*i);
                        }
                }
@@ -475,7 +432,6 @@ AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
 void
 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
 {
-       trackview.editor().session()->begin_reversible_command (_("automation range move"));
        trackview.editor().session()->add_command (
                new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0));
 
@@ -495,7 +451,6 @@ AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
 void
 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
 {
-       trackview.editor().session()->begin_reversible_command (_("automation range move"));
        trackview.editor().session()->add_command (
                new MementoCommand<AutomationList> (memento_command_binder(), state, 0));
 
@@ -508,43 +463,60 @@ struct ControlPointSorter
        bool operator() (ControlPoint const * a, ControlPoint const * b) const {
                if (floateq (a->get_x(), b->get_x(), 1)) {
                        return a->view_index() < b->view_index();
-               } 
+               }
                return a->get_x() < b->get_x();
        }
 };
 
 AutomationLine::ContiguousControlPoints::ContiguousControlPoints (AutomationLine& al)
-       : line (al), before_x (0), after_x (DBL_MAX) 
+       : line (al), before_x (0), after_x (DBL_MAX)
 {
 }
 
 void
-AutomationLine::ContiguousControlPoints::compute_x_bounds ()
+AutomationLine::ContiguousControlPoints::compute_x_bounds (PublicEditor& e)
 {
        uint32_t sz = size();
 
        if (sz > 0 && sz < line.npoints()) {
+               const TempoMap& map (e.session()->tempo_map());
 
-               /* determine the limits on x-axis motion for this 
+               /* determine the limits on x-axis motion for this
                   contiguous range of control points
                */
 
                if (front()->view_index() > 0) {
                        before_x = line.nth (front()->view_index() - 1)->get_x();
+
+                       const samplepos_t pos = e.pixel_to_sample(before_x);
+                       const Meter& meter = map.meter_at_sample (pos);
+                       const samplecnt_t len = ceil (meter.samples_per_bar (map.tempo_at_sample (pos), e.session()->sample_rate())
+                                       / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()) );
+                       const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
+
+                       before_x += one_tick_in_pixels;
                }
 
                /* if our last point has a point after it in the line,
                   we have an "after" bound
                */
 
-               if (back()->view_index() < (line.npoints() - 2)) {
+               if (back()->view_index() < (line.npoints() - 1)) {
                        after_x = line.nth (back()->view_index() + 1)->get_x();
+
+                       const samplepos_t pos = e.pixel_to_sample(after_x);
+                       const Meter& meter = map.meter_at_sample (pos);
+                       const samplecnt_t len = ceil (meter.samples_per_bar (map.tempo_at_sample (pos), e.session()->sample_rate())
+                                       / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()));
+                       const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
+
+                       after_x -= one_tick_in_pixels;
                }
        }
 }
 
-double 
-AutomationLine::ContiguousControlPoints::clamp_dx (double dx) 
+double
+AutomationLine::ContiguousControlPoints::clamp_dx (double dx)
 {
        if (empty()) {
                return dx;
@@ -552,10 +524,10 @@ AutomationLine::ContiguousControlPoints::clamp_dx (double dx)
 
        /* get the maximum distance we can move any of these points along the x-axis
         */
-       
+
        double tx; /* possible position a point would move to, given dx */
        ControlPoint* cp;
-       
+
        if (dx > 0) {
                /* check the last point, since we're moving later in time */
                cp = back();
@@ -564,17 +536,24 @@ AutomationLine::ContiguousControlPoints::clamp_dx (double dx)
                cp = front();
        }
 
-       tx = cp->get_x() + dx; // new possible position if we just add the motion 
+       tx = cp->get_x() + dx; // new possible position if we just add the motion
        tx = max (tx, before_x); // can't move later than following point
        tx = min (tx, after_x);  // can't move earlier than preceeding point
-       return  tx - cp->get_x (); 
+       return  tx - cp->get_x ();
 }
 
-void 
-AutomationLine::ContiguousControlPoints::move (double dx, double dy) 
+void
+AutomationLine::ContiguousControlPoints::move (double dx, double dvalue)
 {
        for (std::list<ControlPoint*>::iterator i = begin(); i != end(); ++i) {
-               (*i)->move_to ((*i)->get_x() + dx, (*i)->get_y() - line.height() * dy, ControlPoint::Full);
+               // compute y-axis delta
+               double view_y = 1.0 - (*i)->get_y() / line.height();
+               line.view_to_model_coord_y (view_y);
+               line.apply_delta (view_y, dvalue);
+               line.model_to_view_coord_y (view_y);
+               view_y = (1.0 - view_y) * line.height();
+
+               (*i)->move_to ((*i)->get_x() + dx, view_y, ControlPoint::Full);
                line.reset_line_coords (**i);
        }
 }
@@ -603,11 +582,11 @@ AutomationLine::start_drag_common (double x, float fraction)
  *  @param fraction New y fraction.
  *  @return x position and y fraction that were actually used (once clamped).
  */
-pair<double, float>
+pair<float, float>
 AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool with_push, uint32_t& final_index)
 {
        if (_drag_points.empty()) {
-               return pair<double,float> (x,fraction);
+               return pair<double,float> (fraction, _desc.is_linear () ? 0 : 1);
        }
 
        double dx = ignore_x ? 0 : (x - _drag_x);
@@ -615,19 +594,19 @@ AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool
 
        if (!_drag_had_movement) {
 
-               /* "first move" ... do some stuff that we don't want to do if 
+               /* "first move" ... do some stuff that we don't want to do if
                   no motion ever took place, but need to do before we handle
                   motion.
                */
-       
+
                /* partition the points we are dragging into (potentially several)
                 * set(s) of contiguous points. this will not happen with a normal
                 * drag, but if the user does a discontiguous selection, it can.
                 */
-               
+
                uint32_t expected_view_index = 0;
                CCP contig;
-               
+
                for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
                        if (i == _drag_points.begin() || (*i)->view_index() != expected_view_index) {
                                contig.reset (new ContiguousControlPoints (*this));
@@ -642,15 +621,16 @@ AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool
                }
 
                for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
-                       (*ccp)->compute_x_bounds ();
+                       (*ccp)->compute_x_bounds (trackview.editor());
                }
-       }       
+               _drag_had_movement = true;
+       }
 
        /* OK, now on to the stuff related to *this* motion event. First, for
         * each contiguous range, figure out the maximum x-axis motion we are
         * allowed (because of neighbouring points that are not moving.
-        * 
-        * if we are moving forwards with push, we don't need to do this, 
+        *
+        * if we are moving forwards with push, we don't need to do this,
         * since all later points will move too.
         */
 
@@ -663,25 +643,42 @@ AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool
                }
        }
 
+       /* compute deflection */
+       double delta_value;
+       {
+               double value0 = _last_drag_fraction;
+               double value1 = _last_drag_fraction + dy;
+               view_to_model_coord_y (value0);
+               view_to_model_coord_y (value1);
+               delta_value = compute_delta (value0, value1);
+       }
+
+       /* special case -inf */
+       if (delta_value == 0 && dy > 0 && !_desc.is_linear ()) {
+               assert (_desc.lower == 0);
+               delta_value = 1.0;
+       }
+
        /* clamp y */
-       
        for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
-               double const y = ((_height - (*i)->get_y()) / _height) + dy;
-               if (y < 0) {
-                       dy -= y;
+               double vy = 1.0 - (*i)->get_y() / _height;
+               view_to_model_coord_y (vy);
+               const double orig = vy;
+               apply_delta (vy, delta_value);
+               if (vy < _desc.lower) {
+                       delta_value = compute_delta (orig, _desc.lower);
                }
-               if (y > 1) {
-                       dy -= (y - 1);
+               if (vy > _desc.upper) {
+                       delta_value = compute_delta (orig, _desc.upper);
                }
        }
 
        if (dx || dy) {
-
                /* and now move each section */
-               
                for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
-                       (*ccp)->move (dx, dy);
+                       (*ccp)->move (dx, delta_value);
                }
+
                if (with_push) {
                        final_index = contiguous_points.back()->back()->view_index () + 1;
                        ControlPoint* p;
@@ -693,21 +690,32 @@ AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool
                        }
                }
 
-               /* update actual line coordinates (will queue a redraw)
-                */
+               /* update actual line coordinates (will queue a redraw) */
 
                if (line_points.size() > 1) {
-                       line->set (line_points);
+                       line->set_steps (line_points, is_stepped());
                }
        }
-       
+
+       /* calculate effective delta */
+       ControlPoint* cp = _drag_points.front();
+       double vy = 1.0 - cp->get_y() / (double)_height;
+       view_to_model_coord_y (vy);
+       float val = (*(cp->model ()))->value;
+       float effective_delta = _desc.compute_delta (val, vy);
+       /* special case recovery from -inf */
+       if (val == 0 && effective_delta == 0 && vy > 0) {
+               assert (!_desc.is_linear ());
+               effective_delta = HUGE_VAL; // +Infinity
+       }
+
+       double const result_frac = _last_drag_fraction + dy;
        _drag_distance += dx;
        _drag_x += dx;
-       _last_drag_fraction = fraction;
-       _drag_had_movement = true;
+       _last_drag_fraction = result_frac;
        did_push = with_push;
 
-       return pair<double, float> (_drag_x + dx, _last_drag_fraction + dy);
+       return pair<float, float> (result_frac, effective_delta);
 }
 
 /** Should be called to indicate the end of a drag */
@@ -719,13 +727,13 @@ AutomationLine::end_drag (bool with_push, uint32_t final_index)
        }
 
        alist->freeze ();
-       sync_model_with_view_points (_drag_points);
+       bool moved = sync_model_with_view_points (_drag_points);
 
        if (with_push) {
                ControlPoint* p;
                uint32_t i = final_index;
                while ((p = nth (i)) != 0 && p->can_slide()) {
-                       sync_model_with_view_point (*p);
+                       moved = sync_model_with_view_point (*p) || moved;
                        ++i;
                }
        }
@@ -734,6 +742,12 @@ AutomationLine::end_drag (bool with_push, uint32_t final_index)
 
        update_pending = false;
 
+       if (moved) {
+               /* A point has moved as a result of sync (clamped to integer or boolean
+                  value), update line accordingly. */
+               line->set_steps (line_points, is_stepped());
+       }
+
        trackview.editor().session()->add_command (
                new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state()));
 
@@ -743,7 +757,7 @@ AutomationLine::end_drag (bool with_push, uint32_t final_index)
        contiguous_points.clear ();
 }
 
-void
+bool
 AutomationLine::sync_model_with_view_point (ControlPoint& cp)
 {
        /* find out where the visual control point is.
@@ -752,7 +766,7 @@ AutomationLine::sync_model_with_view_point (ControlPoint& cp)
        */
 
        double view_x = cp.get_x();
-       double view_y = 1.0 - (cp.get_y() / _height);
+       double view_y = 1.0 - cp.get_y() / (double)_height;
 
        /* if xval has not changed, set it directly from the model to avoid rounding errors */
 
@@ -768,6 +782,17 @@ AutomationLine::sync_model_with_view_point (ControlPoint& cp)
        view_to_model_coord_y (view_y);
 
        alist->modify (cp.model(), view_x, view_y);
+
+       /* convert back from model to view y for clamping position (for integer/boolean/etc) */
+       model_to_view_coord_y (view_y);
+       const double point_y = _height - (view_y * _height);
+       if (point_y != cp.get_y()) {
+               cp.move_to (cp.get_x(), point_y, ControlPoint::Full);
+               reset_line_coords (cp);
+               return true;
+       }
+
+       return false;
 }
 
 bool
@@ -833,27 +858,28 @@ AutomationLine::is_first_point (ControlPoint& cp)
 void
 AutomationLine::remove_point (ControlPoint& cp)
 {
-       trackview.editor().session()->begin_reversible_command (_("remove control point"));
+       trackview.editor().begin_reversible_command (_("remove control point"));
        XMLNode &before = alist->get_state();
 
+       trackview.editor ().get_selection ().clear_points ();
        alist->erase (cp.model());
-       
+
        trackview.editor().session()->add_command(
                new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
 
-       trackview.editor().session()->commit_reversible_command ();
+       trackview.editor().commit_reversible_command ();
        trackview.editor().session()->set_dirty ();
 }
 
 /** Get selectable points within an area.
- *  @param start Start position in session frames.
- *  @param end End position in session frames.
+ *  @param start Start position in session samples.
+ *  @param end End position in session samples.
  *  @param bot Bottom y range, as a fraction of line height, where 0 is the bottom of the line.
  *  @param top Top y range, as a fraction of line height, where 0 is the bottom of the line.
  *  @param result Filled in with selectable things; in this case, ControlPoints.
  */
 void
-AutomationLine::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
+AutomationLine::get_selectables (samplepos_t start, samplepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
 {
        /* convert fractions to display coordinates with 0 at the top of the track */
        double const bot_track = (1 - topfrac) * trackview.current_height ();
@@ -863,12 +889,12 @@ AutomationLine::get_selectables (framepos_t start, framepos_t end, double botfra
                double const model_when = (*(*i)->model())->when;
 
                /* model_when is relative to the start of the source, so we just need to add on the origin_b here
-                  (as it is the session frame position of the start of the source)
+                  (as it is the session sample position of the start of the source)
                */
-               
-               framepos_t const session_frames_when = _time_converter->to (model_when) + _time_converter->origin_b ();
 
-               if (session_frames_when >= start && session_frames_when <= end && (*i)->get_y() >= bot_track && (*i)->get_y() <= top_track) {
+               samplepos_t const session_samples_when = _time_converter->to (model_when) + _time_converter->origin_b ();
+
+               if (session_samples_when >= start && session_samples_when <= end && (*i)->get_y() >= bot_track && (*i)->get_y() <= top_track) {
                        results.push_back (*i);
                }
        }
@@ -891,12 +917,19 @@ AutomationLine::set_selected_points (PointSelection const & points)
                (*i)->set_selected (true);
        }
 
+       if (points.empty()) {
+               remove_visibility (SelectedControlPoints);
+       } else {
+               add_visibility (SelectedControlPoints);
+       }
+
        set_colors ();
 }
 
-void AutomationLine::set_colors ()
+void
+AutomationLine::set_colors ()
 {
-       set_line_color (ARDOUR_UI::config()->get_canvasvar_AutomationLine());
+       set_line_color (UIConfiguration::instance().color ("automation line"));
        for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
                (*i)->set_color ();
        }
@@ -939,7 +972,7 @@ AutomationLine::reset_callback (const Evoral::ControlList& events)
        np = events.size();
 
        Evoral::ControlList& e = const_cast<Evoral::ControlList&> (events);
-       
+
        for (AutomationList::iterator ai = e.begin(); ai != e.end(); ++ai, ++pi) {
 
                double tx = (*ai)->when;
@@ -949,22 +982,22 @@ AutomationLine::reset_callback (const Evoral::ControlList& events)
 
                model_to_view_coord (tx, ty);
 
-               if (isnan (tx) || isnan (ty)) {
+               if (isnan_local (tx) || isnan_local (ty)) {
                        warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
                                                   _name) << endmsg;
                        continue;
                }
-               
-               if (tx >= max_framepos || tx < 0 || tx >= _maximum_time) {
+
+               if (tx >= max_samplepos || tx < 0 || tx >= _maximum_time) {
                        continue;
                }
-               
+
                /* convert x-coordinate to a canvas unit coordinate (this takes
                 * zoom and scroll into account).
                 */
-                       
+
                tx = trackview.editor().sample_to_pixel_unrounded (tx);
-               
+
                /* convert from canonical view height (0..1.0) to actual
                 * height coordinates (using X11's top-left rooted system)
                 */
@@ -1004,11 +1037,9 @@ AutomationLine::reset_callback (const Evoral::ControlList& events)
                        line_points[n].y = control_points[n]->get_y();
                }
 
-               line->set (line_points);
+               line->set_steps (line_points, is_stepped());
 
-               if (_visible && alist->interpolation() != AutomationList::Discrete) {
-                       line->show();
-               }
+               update_visibility ();
        }
 
        set_selected_points (trackview.editor().get_selection().points);
@@ -1025,14 +1056,21 @@ AutomationLine::reset ()
                return;
        }
 
+       /* TODO: abort any drags in progress, e.g. draging points while writing automation
+        * (the control-point model, used by AutomationLine::drag_motion, will be invalid).
+        *
+        * Note: reset() may also be called from an aborted drag (LineDrag::aborted)
+        * maybe abort in list_changed(), interpolation_changed() and ... ?
+        * XXX
+        */
+
        alist->apply_to_points (*this, &AutomationLine::reset_callback);
 }
 
 void
 AutomationLine::queue_reset ()
 {
-       /* this must be called from the GUI thread
-        */
+       /* this must be called from the GUI thread */
 
        if (trackview.editor().session()->transport_rolling() && alist->automation_write()) {
                /* automation write pass ... defer to a timeout */
@@ -1060,11 +1098,6 @@ AutomationLine::clear ()
                new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
 }
 
-void
-AutomationLine::change_model (AutomationList::iterator /*i*/, double /*x*/, double /*y*/)
-{
-}
-
 void
 AutomationLine::set_list (boost::shared_ptr<ARDOUR::AutomationList> list)
 {
@@ -1081,7 +1114,7 @@ AutomationLine::add_visibility (VisibleAspects va)
        _visible = VisibleAspects (_visible | va);
 
        if (old != _visible) {
-               show ();
+               update_visibility ();
        }
 }
 
@@ -1090,7 +1123,7 @@ AutomationLine::set_visibility (VisibleAspects va)
 {
        if (_visible != va) {
                _visible = va;
-               show ();
+               update_visibility ();
        }
 }
 
@@ -1102,24 +1135,20 @@ AutomationLine::remove_visibility (VisibleAspects va)
        _visible = VisibleAspects (_visible & ~va);
 
        if (old != _visible) {
-               show ();
+               update_visibility ();
        }
 }
 
 void
 AutomationLine::track_entered()
 {
-       if (alist->interpolation() != AutomationList::Discrete) {
-               add_visibility (ControlPoints);
-       }
+       add_visibility (ControlPoints);
 }
 
 void
 AutomationLine::track_exited()
 {
-       if (alist->interpolation() != AutomationList::Discrete) {
-               remove_visibility (ControlPoints);
-       }
+       remove_visibility (ControlPoints);
 }
 
 XMLNode &
@@ -1146,42 +1175,73 @@ AutomationLine::view_to_model_coord (double& x, double& y) const
 void
 AutomationLine::view_to_model_coord_y (double& y) const
 {
-       /* TODO: This should be more generic ... */
-       if (alist->parameter().type() == GainAutomation ||
-           alist->parameter().type() == EnvelopeAutomation) {
-               y = slider_position_to_gain_with_max (y, Config->get_max_gain());
-               y = max (0.0, y);
-               y = min (2.0, y);
-       } else if (alist->parameter().type() == PanAzimuthAutomation ||
-                  alist->parameter().type() == PanElevationAutomation) {
-               y = 1.0 - y;
-       } else if (alist->parameter().type() == PanWidthAutomation) {
-               y = 2.0 * y - 1.0;
-       } else if (alist->parameter().type() == PluginAutomation) {
-               y = y * (double)(alist->get_max_y()- alist->get_min_y()) + alist->get_min_y();
-       } else {
-               y = rint (y * alist->parameter().max());
+       if (alist->default_interpolation () != alist->interpolation()) {
+               switch (alist->interpolation()) {
+                       case AutomationList::Discrete:
+                               /* toggles and MIDI only -- see is_stepped() */
+                               assert (alist->default_interpolation () == AutomationList::Linear);
+                               break;
+                       case AutomationList::Linear:
+                               y = y * (_desc.upper - _desc.lower) + _desc.lower;
+                               return;
+                       default:
+                               /* types that default to linear, can't be use
+                                * Logarithmic or Exponential interpolation.
+                                * "Curved" is invalid for automation (only x-fads)
+                                */
+                               assert (0);
+                               break;
+               }
        }
+       y = _desc.from_interface (y);
+}
+
+double
+AutomationLine::compute_delta (double from, double to) const
+{
+       return _desc.compute_delta (from, to);
 }
 
 void
-AutomationLine::model_to_view_coord (double& x, double& y) const
+AutomationLine::apply_delta (double& val, double delta) const
 {
-       /* TODO: This should be more generic ... */
-       if (alist->parameter().type() == GainAutomation ||
-           alist->parameter().type() == EnvelopeAutomation) {
-               y = gain_to_slider_position_with_max (y, Config->get_max_gain());
-       } else if (alist->parameter().type() == PanAzimuthAutomation ||
-                  alist->parameter().type() == PanElevationAutomation) {
-               y = 1.0 - y;
-       } else if (alist->parameter().type() == PanWidthAutomation) {
-               y = .5 + y * .5;
-       } else if (alist->parameter().type() == PluginAutomation) {
-               y = (y - alist->get_min_y()) / (double)(alist->get_max_y()- alist->get_min_y());
-       } else {
-               y = y / (double)alist->parameter().max(); /* ... like this */
+       if (val == 0 && !_desc.is_linear () && delta >= 1.0) {
+               /* recover from -inf */
+               val = 1.0 / _height;
+               view_to_model_coord_y (val);
+               return;
        }
+       val = _desc.apply_delta (val, delta);
+}
 
+void
+AutomationLine::model_to_view_coord_y (double& y) const
+{
+       if (alist->default_interpolation () != alist->interpolation()) {
+               switch (alist->interpolation()) {
+                       case AutomationList::Discrete:
+                               /* toggles and MIDI only -- see is_stepped */
+                               assert (alist->default_interpolation () == AutomationList::Linear);
+                               break;
+                       case AutomationList::Linear:
+                               y = (y - _desc.lower) / (_desc.upper - _desc.lower);
+                               return;
+                       default:
+                               /* types that default to linear, can't be use
+                                * Logarithmic or Exponential interpolation.
+                                * "Curved" is invalid for automation (only x-fads)
+                                */
+                               assert (0);
+                               break;
+               }
+       }
+       y = _desc.to_interface (y);
+}
+
+void
+AutomationLine::model_to_view_coord (double& x, double& y) const
+{
+       model_to_view_coord_y (y);
        x = _time_converter->to (x) - _offset;
 }
 
@@ -1189,16 +1249,14 @@ AutomationLine::model_to_view_coord (double& x, double& y) const
 void
 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
 {
-       if (style == AutomationList::Discrete) {
-               set_visibility (ControlPoints);
-               line->hide();
-       } else {
-               set_visibility (Line);
+       if (line_points.size() > 1) {
+               reset ();
+               line->set_steps(line_points, is_stepped());
        }
 }
 
 void
-AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty, 
+AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty,
                                            AutomationList::iterator model, uint32_t npoints)
 {
        ControlPoint::ShapeType shape;
@@ -1265,7 +1323,7 @@ AutomationLine::memento_command_binder ()
  *  to the start of the track or region that it is on.
  */
 void
-AutomationLine::set_maximum_time (framecnt_t t)
+AutomationLine::set_maximum_time (samplecnt_t t)
 {
        if (_maximum_time == t) {
                return;
@@ -1276,11 +1334,11 @@ AutomationLine::set_maximum_time (framecnt_t t)
 }
 
 
-/** @return min and max x positions of points that are in the list, in session frames */
-pair<framepos_t, framepos_t>
+/** @return min and max x positions of points that are in the list, in session samples */
+pair<samplepos_t, samplepos_t>
 AutomationLine::get_point_x_range () const
 {
-       pair<framepos_t, framepos_t> r (max_framepos, 0);
+       pair<samplepos_t, samplepos_t> r (max_samplepos, 0);
 
        for (AutomationList::const_iterator i = the_list()->begin(); i != the_list()->end(); ++i) {
                r.first = min (r.first, session_position (i));
@@ -1290,14 +1348,14 @@ AutomationLine::get_point_x_range () const
        return r;
 }
 
-framepos_t
+samplepos_t
 AutomationLine::session_position (AutomationList::const_iterator p) const
 {
        return _time_converter->to ((*p)->when) + _offset + _time_converter->origin_b ();
 }
 
 void
-AutomationLine::set_offset (framepos_t off)
+AutomationLine::set_offset (samplepos_t off)
 {
        if (_offset == off) {
                return;