2 Copyright (C) 2002-2003 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 // 'std::isnan()' is not available in MSVC.
26 #define isnan_local(val) (bool)_isnan((double)val)
28 #define isnan_local std::isnan
35 #include "boost/shared_ptr.hpp"
37 #include "pbd/floating.h"
38 #include "pbd/memento_command.h"
39 #include "pbd/stl_delete.h"
40 #include "pbd/stacktrace.h"
42 #include "ardour/automation_list.h"
43 #include "ardour/dB.h"
44 #include "ardour/debug.h"
45 #include "ardour/parameter_types.h"
46 #include "ardour/tempo.h"
48 #include "evoral/Curve.hpp"
50 #include "canvas/debug.h"
52 #include "automation_line.h"
53 #include "control_point.h"
54 #include "gui_thread.h"
55 #include "rgb_macros.h"
56 #include "ardour_ui.h"
57 #include "public_editor.h"
58 #include "selection.h"
59 #include "time_axis_view.h"
60 #include "point_selection.h"
61 #include "automation_time_axis.h"
63 #include "ardour/event_type_map.h"
64 #include "ardour/session.h"
65 #include "ardour/value_as_string.h"
70 using namespace ARDOUR;
72 using namespace Editing;
74 /** @param converter A TimeConverter whose origin_b is the start time of the AutomationList in session frames.
75 * This will not be deleted by AutomationLine.
77 AutomationLine::AutomationLine (const string& name,
79 ArdourCanvas::Item& parent,
80 boost::shared_ptr<AutomationList> al,
81 const ParameterDescriptor& desc,
82 Evoral::TimeConverter<double, framepos_t>* converter)
86 , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, framepos_t>)
87 , _parent_group (parent)
89 , _maximum_time (max_framepos)
93 _our_time_converter = false;
95 _our_time_converter = true;
100 update_pending = false;
101 have_timeout = false;
102 _uses_gain_mapping = false;
105 terminal_points_can_slide = true;
108 group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple(0, 1.5));
109 CANVAS_DEBUG_NAME (group, "region gain envelope group");
111 line = new ArdourCanvas::PolyLine (group);
112 CANVAS_DEBUG_NAME (line, "region gain envelope line");
113 line->set_data ("line", this);
114 line->set_outline_width (2.0);
115 line->set_covers_threshold (4.0);
117 line->Event.connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
119 trackview.session()->register_with_memento_command_factory(alist->id(), this);
121 if (alist->parameter().type() == GainAutomation ||
122 alist->parameter().type() == TrimAutomation ||
123 alist->parameter().type() == EnvelopeAutomation ||
124 desc.unit == ParameterDescriptor::DB) {
125 set_uses_gain_mapping (true);
128 interpolation_changed (alist->interpolation ());
133 AutomationLine::~AutomationLine ()
135 vector_delete (&control_points);
138 if (_our_time_converter) {
139 delete _time_converter;
144 AutomationLine::event_handler (GdkEvent* event)
146 return PublicEditor::instance().canvas_line_event (event, line, this);
150 AutomationLine::is_stepped() const
152 return (_desc.toggled ||
153 (alist && alist->interpolation() == AutomationList::Discrete));
157 AutomationLine::update_visibility ()
159 if (_visible & Line) {
160 /* Only show the line when there are some points, otherwise we may show an out-of-date line
161 when automation points have been removed (the line will still follow the shape of the
164 if (control_points.size() >= 2) {
170 if (_visible & ControlPoints) {
171 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
174 } else if (_visible & SelectedControlPoints) {
175 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
176 if ((*i)->get_selected()) {
183 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
190 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
191 if (_visible & ControlPoints) {
202 AutomationLine::hide ()
204 /* leave control points setting unchanged, we are just hiding the
208 set_visibility (AutomationLine::VisibleAspects (_visible & ~Line));
212 AutomationLine::control_point_box_size ()
214 if (_height > TimeAxisView::preset_height (HeightLarger)) {
216 } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
223 AutomationLine::set_height (guint32 h)
228 double bsz = control_point_box_size();
230 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
231 (*i)->set_size (bsz);
239 AutomationLine::set_line_color (uint32_t color)
242 line->set_outline_color (color);
246 AutomationLine::set_uses_gain_mapping (bool yn)
248 if (yn != _uses_gain_mapping) {
249 _uses_gain_mapping = yn;
255 AutomationLine::nth (uint32_t n)
257 if (n < control_points.size()) {
258 return control_points[n];
265 AutomationLine::nth (uint32_t n) const
267 if (n < control_points.size()) {
268 return control_points[n];
275 AutomationLine::modify_point_y (ControlPoint& cp, double y)
277 /* clamp y-coord appropriately. y is supposed to be a normalized fraction (0.0-1.0),
278 and needs to be converted to a canvas unit distance.
283 y = _height - (y * _height);
285 double const x = trackview.editor().sample_to_pixel_unrounded (_time_converter->to((*cp.model())->when) - _offset);
287 trackview.editor().begin_reversible_command (_("automation event move"));
288 trackview.editor().session()->add_command (
289 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
291 cp.move_to (x, y, ControlPoint::Full);
294 sync_model_with_view_point (cp);
297 reset_line_coords (cp);
299 if (line_points.size() > 1) {
300 line->set_steps (line_points, is_stepped());
303 update_pending = false;
305 trackview.editor().session()->add_command (
306 new MementoCommand<AutomationList> (memento_command_binder(), 0, &alist->get_state()));
308 trackview.editor().commit_reversible_command ();
309 trackview.editor().session()->set_dirty ();
313 AutomationLine::reset_line_coords (ControlPoint& cp)
315 if (cp.view_index() < line_points.size()) {
316 line_points[cp.view_index()].x = cp.get_x ();
317 line_points[cp.view_index()].y = cp.get_y ();
322 AutomationLine::sync_model_with_view_points (list<ControlPoint*> cp)
324 update_pending = true;
327 for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
328 moved = sync_model_with_view_point (**i) || moved;
335 AutomationLine::get_verbose_cursor_string (double fraction) const
337 std::string s = fraction_to_string (fraction);
338 if (_uses_gain_mapping) {
346 AutomationLine::get_verbose_cursor_relative_string (double original, double fraction) const
348 std::string s = fraction_to_string (fraction);
349 if (_uses_gain_mapping) {
353 std::string d = fraction_to_relative_string (original, fraction);
360 if (_uses_gain_mapping) {
371 * @param fraction y fraction
372 * @return string representation of this value, using dB if appropriate.
375 AutomationLine::fraction_to_string (double fraction) const
377 if (_uses_gain_mapping) {
379 if (_desc.type == GainAutomation || _desc.type == EnvelopeAutomation || _desc.lower == 0) {
380 if (fraction == 0.0) {
381 snprintf (buf, sizeof (buf), "-inf");
383 snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, _desc.upper)));
386 const double lower_db = accurate_coefficient_to_dB (_desc.lower);
387 const double range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
388 snprintf (buf, sizeof (buf), "%.1f", lower_db + fraction * range_db);
392 view_to_model_coord_y (fraction);
393 return ARDOUR::value_as_string (_desc, fraction);
396 return ""; /*NOTREACHED*/
400 * @param original an old y-axis fraction
401 * @param fraction the new y fraction
402 * @return string representation of the difference between original and fraction, using dB if appropriate.
405 AutomationLine::fraction_to_relative_string (double original, double fraction) const
409 if (original == fraction) {
413 if (_uses_gain_mapping) {
414 if (original == 0.0) {
415 /* there is no sensible representation of a relative
416 change from -inf dB, so return an empty string.
419 } else if (fraction == 0.0) {
420 snprintf (buf, sizeof (buf), "-inf");
422 double old_db = accurate_coefficient_to_dB (slider_position_to_gain_with_max (original, Config->get_max_gain()));
423 double new_db = accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, Config->get_max_gain()));
424 snprintf (buf, sizeof (buf), "%.1f", new_db - old_db);
427 view_to_model_coord_y (original);
428 view_to_model_coord_y (fraction);
429 return ARDOUR::value_as_string (_desc, fraction - original);
436 * @param s Value string in the form as returned by fraction_to_string.
437 * @return Corresponding y fraction.
440 AutomationLine::string_to_fraction (string const & s) const
447 sscanf (s.c_str(), "%lf", &v);
449 if (_uses_gain_mapping) {
450 v = gain_to_slider_position_with_max (dB_to_coefficient (v), Config->get_max_gain());
453 model_to_view_coord (dummy, v);
459 /** Start dragging a single point, possibly adding others if the supplied point is selected and there
460 * are other selected points.
462 * @param cp Point to drag.
463 * @param x Initial x position (units).
464 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
467 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
469 trackview.editor().begin_reversible_command (_("automation event move"));
470 trackview.editor().session()->add_command (
471 new MementoCommand<AutomationList> (memento_command_binder(), &get_state(), 0));
473 _drag_points.clear ();
474 _drag_points.push_back (cp);
476 if (cp->get_selected ()) {
477 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
478 if (*i != cp && (*i)->get_selected()) {
479 _drag_points.push_back (*i);
484 start_drag_common (x, fraction);
487 /** Start dragging a line vertically (with no change in x)
488 * @param i1 Control point index of the `left' point on the line.
489 * @param i2 Control point index of the `right' point on the line.
490 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
493 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
495 trackview.editor().begin_reversible_command (_("automation range move"));
496 trackview.editor().session()->add_command (
497 new MementoCommand<AutomationList> (memento_command_binder (), &get_state(), 0));
499 _drag_points.clear ();
501 for (uint32_t i = i1; i <= i2; i++) {
502 _drag_points.push_back (nth (i));
505 start_drag_common (0, fraction);
508 /** Start dragging multiple points (with no change in x)
509 * @param cp Points to drag.
510 * @param fraction Initial y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
513 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
515 trackview.editor().begin_reversible_command (_("automation range move"));
516 trackview.editor().session()->add_command (
517 new MementoCommand<AutomationList> (memento_command_binder(), state, 0));
520 start_drag_common (0, fraction);
523 struct ControlPointSorter
525 bool operator() (ControlPoint const * a, ControlPoint const * b) const {
526 if (floateq (a->get_x(), b->get_x(), 1)) {
527 return a->view_index() < b->view_index();
529 return a->get_x() < b->get_x();
533 AutomationLine::ContiguousControlPoints::ContiguousControlPoints (AutomationLine& al)
534 : line (al), before_x (0), after_x (DBL_MAX)
539 AutomationLine::ContiguousControlPoints::compute_x_bounds (PublicEditor& e)
541 uint32_t sz = size();
543 if (sz > 0 && sz < line.npoints()) {
544 const TempoMap& map (e.session()->tempo_map());
546 /* determine the limits on x-axis motion for this
547 contiguous range of control points
550 if (front()->view_index() > 0) {
551 before_x = line.nth (front()->view_index() - 1)->get_x();
553 const framepos_t pos = e.pixel_to_sample(before_x);
554 const Meter& meter = map.meter_at (pos);
555 const framecnt_t len = ceil (meter.frames_per_bar (map.tempo_at (pos), e.session()->frame_rate())
556 / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()) );
557 const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
559 before_x += one_tick_in_pixels;
562 /* if our last point has a point after it in the line,
563 we have an "after" bound
566 if (back()->view_index() < (line.npoints() - 1)) {
567 after_x = line.nth (back()->view_index() + 1)->get_x();
569 const framepos_t pos = e.pixel_to_sample(after_x);
570 const Meter& meter = map.meter_at (pos);
571 const framecnt_t len = ceil (meter.frames_per_bar (map.tempo_at (pos), e.session()->frame_rate())
572 / (Timecode::BBT_Time::ticks_per_beat * meter.divisions_per_bar()));
573 const double one_tick_in_pixels = e.sample_to_pixel_unrounded (len);
575 after_x -= one_tick_in_pixels;
581 AutomationLine::ContiguousControlPoints::clamp_dx (double dx)
587 /* get the maximum distance we can move any of these points along the x-axis
590 double tx; /* possible position a point would move to, given dx */
594 /* check the last point, since we're moving later in time */
597 /* check the first point, since we're moving earlier in time */
601 tx = cp->get_x() + dx; // new possible position if we just add the motion
602 tx = max (tx, before_x); // can't move later than following point
603 tx = min (tx, after_x); // can't move earlier than preceeding point
604 return tx - cp->get_x ();
608 AutomationLine::ContiguousControlPoints::move (double dx, double dy)
610 for (std::list<ControlPoint*>::iterator i = begin(); i != end(); ++i) {
611 (*i)->move_to ((*i)->get_x() + dx, (*i)->get_y() - line.height() * dy, ControlPoint::Full);
612 line.reset_line_coords (**i);
616 /** Common parts of starting a drag.
617 * @param x Starting x position in units, or 0 if x is being ignored.
618 * @param fraction Starting y position (as a fraction of the track height, where 0 is the bottom and 1 the top)
621 AutomationLine::start_drag_common (double x, float fraction)
625 _last_drag_fraction = fraction;
626 _drag_had_movement = false;
629 /* they are probably ordered already, but we have to make sure */
631 _drag_points.sort (ControlPointSorter());
635 /** Should be called to indicate motion during a drag.
636 * @param x New x position of the drag in canvas units, or undefined if ignore_x == true.
637 * @param fraction New y fraction.
638 * @return x position and y fraction that were actually used (once clamped).
641 AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool with_push, uint32_t& final_index)
643 if (_drag_points.empty()) {
644 return pair<double,float> (x,fraction);
647 double dx = ignore_x ? 0 : (x - _drag_x);
648 double dy = fraction - _last_drag_fraction;
650 if (!_drag_had_movement) {
652 /* "first move" ... do some stuff that we don't want to do if
653 no motion ever took place, but need to do before we handle
657 /* partition the points we are dragging into (potentially several)
658 * set(s) of contiguous points. this will not happen with a normal
659 * drag, but if the user does a discontiguous selection, it can.
662 uint32_t expected_view_index = 0;
665 for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
666 if (i == _drag_points.begin() || (*i)->view_index() != expected_view_index) {
667 contig.reset (new ContiguousControlPoints (*this));
668 contiguous_points.push_back (contig);
670 contig->push_back (*i);
671 expected_view_index = (*i)->view_index() + 1;
674 if (contiguous_points.back()->empty()) {
675 contiguous_points.pop_back ();
678 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
679 (*ccp)->compute_x_bounds (trackview.editor());
683 /* OK, now on to the stuff related to *this* motion event. First, for
684 * each contiguous range, figure out the maximum x-axis motion we are
685 * allowed (because of neighbouring points that are not moving.
687 * if we are moving forwards with push, we don't need to do this,
688 * since all later points will move too.
691 if (dx < 0 || ((dx > 0) && !with_push)) {
692 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
693 double dxt = (*ccp)->clamp_dx (dx);
694 if (fabs (dxt) < fabs (dx)) {
702 for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
703 double const y = ((_height - (*i)->get_y()) / _height) + dy;
714 /* and now move each section */
716 for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
717 (*ccp)->move (dx, dy);
720 final_index = contiguous_points.back()->back()->view_index () + 1;
722 uint32_t i = final_index;
723 while ((p = nth (i)) != 0 && p->can_slide()) {
724 p->move_to (p->get_x() + dx, p->get_y(), ControlPoint::Full);
725 reset_line_coords (*p);
730 /* update actual line coordinates (will queue a redraw)
733 if (line_points.size() > 1) {
734 line->set_steps (line_points, is_stepped());
738 _drag_distance += dx;
740 _last_drag_fraction = fraction;
741 _drag_had_movement = true;
742 did_push = with_push;
744 return pair<double, float> (_drag_x + dx, _last_drag_fraction + dy);
747 /** Should be called to indicate the end of a drag */
749 AutomationLine::end_drag (bool with_push, uint32_t final_index)
751 if (!_drag_had_movement) {
756 bool moved = sync_model_with_view_points (_drag_points);
760 uint32_t i = final_index;
761 while ((p = nth (i)) != 0 && p->can_slide()) {
762 moved = sync_model_with_view_point (*p) || moved;
769 update_pending = false;
772 /* A point has moved as a result of sync (clamped to integer or boolean
773 value), update line accordingly. */
774 line->set_steps (line_points, is_stepped());
777 trackview.editor().session()->add_command (
778 new MementoCommand<AutomationList>(memento_command_binder (), 0, &alist->get_state()));
780 trackview.editor().session()->set_dirty ();
783 contiguous_points.clear ();
787 AutomationLine::sync_model_with_view_point (ControlPoint& cp)
789 /* find out where the visual control point is.
790 initial results are in canvas units. ask the
791 line to convert them to something relevant.
794 double view_x = cp.get_x();
795 double view_y = 1.0 - (cp.get_y() / _height);
797 /* if xval has not changed, set it directly from the model to avoid rounding errors */
799 if (view_x == trackview.editor().sample_to_pixel_unrounded (_time_converter->to ((*cp.model())->when)) - _offset) {
800 view_x = (*cp.model())->when - _offset;
802 view_x = trackview.editor().pixel_to_sample (view_x);
803 view_x = _time_converter->from (view_x + _offset);
806 update_pending = true;
808 view_to_model_coord_y (view_y);
810 alist->modify (cp.model(), view_x, view_y);
812 /* convert back from model to view y for clamping position (for integer/boolean/etc) */
813 model_to_view_coord_y (view_y);
814 const double point_y = _height - (view_y * _height);
815 if (point_y != cp.get_y()) {
816 cp.move_to (cp.get_x(), point_y, ControlPoint::Full);
817 reset_line_coords (cp);
825 AutomationLine::control_points_adjacent (double xval, uint32_t & before, uint32_t& after)
827 ControlPoint *bcp = 0;
828 ControlPoint *acp = 0;
831 unit_xval = trackview.editor().sample_to_pixel_unrounded (xval);
833 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
835 if ((*i)->get_x() <= unit_xval) {
837 if (!bcp || (*i)->get_x() > bcp->get_x()) {
839 before = bcp->view_index();
842 } else if ((*i)->get_x() > unit_xval) {
844 after = acp->view_index();
853 AutomationLine::is_last_point (ControlPoint& cp)
855 // If the list is not empty, and the point is the last point in the list
857 if (alist->empty()) {
861 AutomationList::const_iterator i = alist->end();
864 if (cp.model() == i) {
872 AutomationLine::is_first_point (ControlPoint& cp)
874 // If the list is not empty, and the point is the first point in the list
876 if (!alist->empty() && cp.model() == alist->begin()) {
883 // This is copied into AudioRegionGainLine
885 AutomationLine::remove_point (ControlPoint& cp)
887 trackview.editor().begin_reversible_command (_("remove control point"));
888 XMLNode &before = alist->get_state();
890 alist->erase (cp.model());
892 trackview.editor().session()->add_command(
893 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
895 trackview.editor().commit_reversible_command ();
896 trackview.editor().session()->set_dirty ();
899 /** Get selectable points within an area.
900 * @param start Start position in session frames.
901 * @param end End position in session frames.
902 * @param bot Bottom y range, as a fraction of line height, where 0 is the bottom of the line.
903 * @param top Top y range, as a fraction of line height, where 0 is the bottom of the line.
904 * @param result Filled in with selectable things; in this case, ControlPoints.
907 AutomationLine::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
909 /* convert fractions to display coordinates with 0 at the top of the track */
910 double const bot_track = (1 - topfrac) * trackview.current_height ();
911 double const top_track = (1 - botfrac) * trackview.current_height ();
913 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
914 double const model_when = (*(*i)->model())->when;
916 /* model_when is relative to the start of the source, so we just need to add on the origin_b here
917 (as it is the session frame position of the start of the source)
920 framepos_t const session_frames_when = _time_converter->to (model_when) + _time_converter->origin_b ();
922 if (session_frames_when >= start && session_frames_when <= end && (*i)->get_y() >= bot_track && (*i)->get_y() <= top_track) {
923 results.push_back (*i);
929 AutomationLine::get_inverted_selectables (Selection&, list<Selectable*>& /*results*/)
935 AutomationLine::set_selected_points (PointSelection const & points)
937 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
938 (*i)->set_selected (false);
941 for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
942 (*i)->set_selected (true);
945 if (points.empty()) {
946 remove_visibility (SelectedControlPoints);
948 add_visibility (SelectedControlPoints);
954 void AutomationLine::set_colors ()
956 set_line_color (ARDOUR_UI::config()->color ("automation line"));
957 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
963 AutomationLine::list_changed ()
965 DEBUG_TRACE (DEBUG::Automation, string_compose ("\tline changed, existing update pending? %1\n", update_pending));
967 if (!update_pending) {
968 update_pending = true;
969 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationLine::queue_reset, this));
974 AutomationLine::reset_callback (const Evoral::ControlList& events)
980 if (events.empty()) {
981 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
984 control_points.clear ();
989 /* hide all existing points, and the line */
991 for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
998 Evoral::ControlList& e = const_cast<Evoral::ControlList&> (events);
1000 for (AutomationList::iterator ai = e.begin(); ai != e.end(); ++ai, ++pi) {
1002 double tx = (*ai)->when;
1003 double ty = (*ai)->value;
1005 /* convert from model coordinates to canonical view coordinates */
1007 model_to_view_coord (tx, ty);
1009 if (isnan_local (tx) || isnan_local (ty)) {
1010 warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
1015 if (tx >= max_framepos || tx < 0 || tx >= _maximum_time) {
1019 /* convert x-coordinate to a canvas unit coordinate (this takes
1020 * zoom and scroll into account).
1023 tx = trackview.editor().sample_to_pixel_unrounded (tx);
1025 /* convert from canonical view height (0..1.0) to actual
1026 * height coordinates (using X11's top-left rooted system)
1029 ty = _height - (ty * _height);
1031 add_visible_control_point (vp, pi, tx, ty, ai, np);
1035 /* discard extra CP's to avoid confusing ourselves */
1037 while (control_points.size() > vp) {
1038 ControlPoint* cp = control_points.back();
1039 control_points.pop_back ();
1043 if (!terminal_points_can_slide) {
1044 control_points.back()->set_can_slide(false);
1049 /* reset the line coordinates given to the CanvasLine */
1051 while (line_points.size() < vp) {
1052 line_points.push_back (ArdourCanvas::Duple (0,0));
1055 while (line_points.size() > vp) {
1056 line_points.pop_back ();
1059 for (uint32_t n = 0; n < vp; ++n) {
1060 line_points[n].x = control_points[n]->get_x();
1061 line_points[n].y = control_points[n]->get_y();
1064 line->set_steps (line_points, is_stepped());
1066 update_visibility ();
1069 set_selected_points (trackview.editor().get_selection().points);
1073 AutomationLine::reset ()
1075 DEBUG_TRACE (DEBUG::Automation, "\t\tLINE RESET\n");
1076 update_pending = false;
1077 have_timeout = false;
1083 alist->apply_to_points (*this, &AutomationLine::reset_callback);
1087 AutomationLine::queue_reset ()
1089 /* this must be called from the GUI thread
1092 if (trackview.editor().session()->transport_rolling() && alist->automation_write()) {
1093 /* automation write pass ... defer to a timeout */
1094 /* redraw in 1/4 second */
1095 if (!have_timeout) {
1096 DEBUG_TRACE (DEBUG::Automation, "\tqueue timeout\n");
1097 Glib::signal_timeout().connect (sigc::bind_return (sigc::mem_fun (*this, &AutomationLine::reset), false), 250);
1098 have_timeout = true;
1100 DEBUG_TRACE (DEBUG::Automation, "\ttimeout already queued, change ignored\n");
1108 AutomationLine::clear ()
1110 /* parent must create and commit command */
1111 XMLNode &before = alist->get_state();
1114 trackview.editor().session()->add_command (
1115 new MementoCommand<AutomationList> (memento_command_binder (), &before, &alist->get_state()));
1119 AutomationLine::set_list (boost::shared_ptr<ARDOUR::AutomationList> list)
1127 AutomationLine::add_visibility (VisibleAspects va)
1129 VisibleAspects old = _visible;
1131 _visible = VisibleAspects (_visible | va);
1133 if (old != _visible) {
1134 update_visibility ();
1139 AutomationLine::set_visibility (VisibleAspects va)
1141 if (_visible != va) {
1143 update_visibility ();
1148 AutomationLine::remove_visibility (VisibleAspects va)
1150 VisibleAspects old = _visible;
1152 _visible = VisibleAspects (_visible & ~va);
1154 if (old != _visible) {
1155 update_visibility ();
1160 AutomationLine::track_entered()
1162 add_visibility (ControlPoints);
1166 AutomationLine::track_exited()
1168 remove_visibility (ControlPoints);
1172 AutomationLine::get_state (void)
1174 /* function as a proxy for the model */
1175 return alist->get_state();
1179 AutomationLine::set_state (const XMLNode &node, int version)
1181 /* function as a proxy for the model */
1182 return alist->set_state (node, version);
1186 AutomationLine::view_to_model_coord (double& x, double& y) const
1188 x = _time_converter->from (x);
1189 view_to_model_coord_y (y);
1193 AutomationLine::view_to_model_coord_y (double& y) const
1195 /* TODO: This should be more generic (use ParameterDescriptor)
1196 * or better yet: Controllable -> set_interface();
1198 if ( alist->parameter().type() == GainAutomation
1199 || alist->parameter().type() == EnvelopeAutomation
1200 || (_desc.unit == ParameterDescriptor::DB && _desc.lower == 0.)) {
1201 y = slider_position_to_gain_with_max (y, _desc.upper);
1202 y = max ((double)_desc.lower, y);
1203 y = min ((double)_desc.upper, y);
1204 } else if (alist->parameter().type() == TrimAutomation
1205 || (_desc.unit == ParameterDescriptor::DB && _desc.lower > 0 && _desc.upper > _desc.lower)) {
1206 const double lower_db = accurate_coefficient_to_dB (_desc.lower);
1207 const double range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
1210 y = dB_to_coefficient (lower_db + y * range_db);
1211 } else if (alist->parameter().type() == PanAzimuthAutomation ||
1212 alist->parameter().type() == PanElevationAutomation) {
1214 } else if (alist->parameter().type() == PanWidthAutomation) {
1217 y = y * (double)(alist->get_max_y() - alist->get_min_y()) + alist->get_min_y();
1218 if (_desc.integer_step) {
1220 } else if (_desc.toggled) {
1221 y = (y > 0.5) ? 1.0 : 0.0;
1227 AutomationLine::model_to_view_coord_y (double& y) const
1229 /* TODO: This should be more generic (use ParameterDescriptor) */
1230 if ( alist->parameter().type() == GainAutomation
1231 || alist->parameter().type() == EnvelopeAutomation
1232 || (_desc.unit == ParameterDescriptor::DB && _desc.lower == 0.)) {
1233 y = gain_to_slider_position_with_max (y, Config->get_max_gain());
1234 } else if (alist->parameter().type() == TrimAutomation
1235 || (_desc.unit == ParameterDescriptor::DB && _desc.lower > 0 && _desc.upper > _desc.lower)) {
1236 const double lower_db = accurate_coefficient_to_dB (_desc.lower);
1237 const double range_db = accurate_coefficient_to_dB (_desc.upper) - lower_db;
1238 y = (accurate_coefficient_to_dB (y) - lower_db) / range_db;
1239 } else if (alist->parameter().type() == PanAzimuthAutomation ||
1240 alist->parameter().type() == PanElevationAutomation) {
1242 } else if (alist->parameter().type() == PanWidthAutomation) {
1245 y = (y - alist->get_min_y()) / (double)(alist->get_max_y() - alist->get_min_y());
1250 AutomationLine::model_to_view_coord (double& x, double& y) const
1252 model_to_view_coord_y (y);
1253 x = _time_converter->to (x) - _offset;
1256 /** Called when our list has announced that its interpolation style has changed */
1258 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
1260 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 (framecnt_t t)
1335 if (_maximum_time == t) {
1344 /** @return min and max x positions of points that are in the list, in session frames */
1345 pair<framepos_t, framepos_t>
1346 AutomationLine::get_point_x_range () const
1348 pair<framepos_t, framepos_t> r (max_framepos, 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 (framepos_t off)
1367 if (_offset == off) {