ardour
automation_line.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2003 Paul Davis
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #include <cmath>
21 
22 #ifdef COMPILER_MSVC
23 #include <float.h>
24 
25 // 'std::isnan()' is not available in MSVC.
26 #define isnan_local(val) (bool)_isnan((double)val)
27 #else
28 #define isnan_local std::isnan
29 #endif
30 
31 #include <climits>
32 #include <vector>
33 #include <fstream>
34 
35 #include "boost/shared_ptr.hpp"
36 
37 #include "pbd/floating.h"
38 #include "pbd/memento_command.h"
39 #include "pbd/stl_delete.h"
40 #include "pbd/stacktrace.h"
41 
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"
47 
48 #include "evoral/Curve.hpp"
49 
50 #include "canvas/debug.h"
51 
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"
62 
63 #include "ardour/event_type_map.h"
64 #include "ardour/session.h"
65 #include "ardour/value_as_string.h"
66 
67 #include "i18n.h"
68 
69 using namespace std;
70 using namespace ARDOUR;
71 using namespace PBD;
72 using namespace Editing;
73 
78  TimeAxisView& tv,
79  ArdourCanvas::Item& parent,
81  const ParameterDescriptor& desc,
83  : trackview (tv)
84  , _name (name)
85  , alist (al)
86  , _time_converter (converter ? converter : new Evoral::IdentityConverter<double, framepos_t>)
87  , _parent_group (parent)
88  , _offset (0)
89  , _maximum_time (max_framepos)
90  , _desc (desc)
91 {
92  if (converter) {
93  _our_time_converter = false;
94  } else {
95  _our_time_converter = true;
96  }
97 
98  _visible = Line;
99 
100  update_pending = false;
101  have_timeout = false;
102  _uses_gain_mapping = false;
103  no_draw = false;
104  _is_boolean = false;
106  _height = 0;
107 
108  group = new ArdourCanvas::Container (&parent, ArdourCanvas::Duple(0, 1.5));
109  CANVAS_DEBUG_NAME (group, "region gain envelope group");
110 
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);
116 
117  line->Event.connect (sigc::mem_fun (*this, &AutomationLine::event_handler));
118 
120 
121  if (alist->parameter().type() == GainAutomation ||
124  desc.unit == ParameterDescriptor::DB) {
125  set_uses_gain_mapping (true);
126  }
127 
129 
130  connect_to_list ();
131 }
132 
134 {
135  vector_delete (&control_points);
136  delete group;
137 
138  if (_our_time_converter) {
139  delete _time_converter;
140  }
141 }
142 
143 bool
145 {
146  return PublicEditor::instance().canvas_line_event (event, line, this);
147 }
148 
149 bool
151 {
152  return (_desc.toggled ||
153  (alist && alist->interpolation() == AutomationList::Discrete));
154 }
155 
156 void
158 {
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
162  old points).
163  */
164  if (control_points.size() >= 2) {
165  line->show();
166  } else {
167  line->hide ();
168  }
169 
170  if (_visible & ControlPoints) {
171  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
172  (*i)->show ();
173  }
174  } else if (_visible & SelectedControlPoints) {
175  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
176  if ((*i)->get_selected()) {
177  (*i)->show ();
178  } else {
179  (*i)->hide ();
180  }
181  }
182  } else {
183  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
184  (*i)->hide ();
185  }
186  }
187 
188  } else {
189  line->hide ();
190  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
191  if (_visible & ControlPoints) {
192  (*i)->show ();
193  } else {
194  (*i)->hide ();
195  }
196  }
197  }
198 
199 }
200 
201 void
203 {
204  /* leave control points setting unchanged, we are just hiding the
205  overall line
206  */
207 
209 }
210 
211 double
213 {
215  return 8.0;
216  } else if (_height > (guint32) TimeAxisView::preset_height (HeightNormal)) {
217  return 6.0;
218  }
219  return 4.0;
220 }
221 
222 void
224 {
225  if (h != _height) {
226  _height = h;
227 
228  double bsz = control_point_box_size();
229 
230  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
231  (*i)->set_size (bsz);
232  }
233 
234  reset ();
235  }
236 }
237 
238 void
240 {
241  _line_color = color;
242  line->set_outline_color (color);
243 }
244 
245 void
247 {
248  if (yn != _uses_gain_mapping) {
249  _uses_gain_mapping = yn;
250  reset ();
251  }
252 }
253 
256 {
257  if (n < control_points.size()) {
258  return control_points[n];
259  } else {
260  return 0;
261  }
262 }
263 
264 ControlPoint const *
265 AutomationLine::nth (uint32_t n) const
266 {
267  if (n < control_points.size()) {
268  return control_points[n];
269  } else {
270  return 0;
271  }
272 }
273 
274 void
276 {
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.
279  */
280 
281  y = max (0.0, y);
282  y = min (1.0, y);
283  y = _height - (y * _height);
284 
285  double const x = trackview.editor().sample_to_pixel_unrounded (_time_converter->to((*cp.model())->when) - _offset);
286 
287  trackview.editor().begin_reversible_command (_("automation event move"));
290 
291  cp.move_to (x, y, ControlPoint::Full);
292 
293  alist->freeze ();
295  alist->thaw ();
296 
297  reset_line_coords (cp);
298 
299  if (line_points.size() > 1) {
300  line->set_steps (line_points, is_stepped());
301  }
302 
303  update_pending = false;
304 
307 
310 }
311 
312 void
314 {
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 ();
318  }
319 }
320 
321 bool
323 {
324  update_pending = true;
325 
326  bool moved = false;
327  for (list<ControlPoint*>::iterator i = cp.begin(); i != cp.end(); ++i) {
328  moved = sync_model_with_view_point (**i) || moved;
329  }
330 
331  return moved;
332 }
333 
334 string
336 {
337  std::string s = fraction_to_string (fraction);
338  if (_uses_gain_mapping) {
339  s += " dB";
340  }
341 
342  return s;
343 }
344 
345 string
346 AutomationLine::get_verbose_cursor_relative_string (double original, double fraction) const
347 {
348  std::string s = fraction_to_string (fraction);
349  if (_uses_gain_mapping) {
350  s += " dB";
351  }
352 
353  std::string d = fraction_to_relative_string (original, fraction);
354 
355  if (!d.empty()) {
356 
357  s += " (\u0394";
358  s += d;
359 
360  if (_uses_gain_mapping) {
361  s += " dB";
362  }
363 
364  s += ')';
365  }
366 
367  return s;
368 }
369 
374 string
375 AutomationLine::fraction_to_string (double fraction) const
376 {
377  if (_uses_gain_mapping) {
378  char buf[32];
380  if (fraction == 0.0) {
381  snprintf (buf, sizeof (buf), "-inf");
382  } else {
383  snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (fraction, _desc.upper)));
384  }
385  } else {
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);
389  }
390  return buf;
391  } else {
392  view_to_model_coord_y (fraction);
393  return ARDOUR::value_as_string (_desc, fraction);
394  }
395 
396  return ""; /*NOTREACHED*/
397 }
398 
404 string
405 AutomationLine::fraction_to_relative_string (double original, double fraction) const
406 {
407  char buf[32];
408 
409  if (original == fraction) {
410  return "0";
411  }
412 
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.
417  */
418  return "";
419  } else if (fraction == 0.0) {
420  snprintf (buf, sizeof (buf), "-inf");
421  } else {
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);
425  }
426  } else {
427  view_to_model_coord_y (original);
428  view_to_model_coord_y (fraction);
429  return ARDOUR::value_as_string (_desc, fraction - original);
430  }
431 
432  return buf;
433 }
434 
439 double
440 AutomationLine::string_to_fraction (string const & s) const
441 {
442  if (s == "-inf") {
443  return 0;
444  }
445 
446  double v;
447  sscanf (s.c_str(), "%lf", &v);
448 
449  if (_uses_gain_mapping) {
450  v = gain_to_slider_position_with_max (dB_to_coefficient (v), Config->get_max_gain());
451  } else {
452  double dummy = 0.0;
453  model_to_view_coord (dummy, v);
454  }
455 
456  return v;
457 }
458 
466 void
467 AutomationLine::start_drag_single (ControlPoint* cp, double x, float fraction)
468 {
469  trackview.editor().begin_reversible_command (_("automation event move"));
472 
473  _drag_points.clear ();
474  _drag_points.push_back (cp);
475 
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);
480  }
481  }
482  }
483 
484  start_drag_common (x, fraction);
485 }
486 
492 void
493 AutomationLine::start_drag_line (uint32_t i1, uint32_t i2, float fraction)
494 {
495  trackview.editor().begin_reversible_command (_("automation range move"));
498 
499  _drag_points.clear ();
500 
501  for (uint32_t i = i1; i <= i2; i++) {
502  _drag_points.push_back (nth (i));
503  }
504 
505  start_drag_common (0, fraction);
506 }
507 
512 void
513 AutomationLine::start_drag_multiple (list<ControlPoint*> cp, float fraction, XMLNode* state)
514 {
515  trackview.editor().begin_reversible_command (_("automation range move"));
518 
519  _drag_points = cp;
520  start_drag_common (0, fraction);
521 }
522 
524 {
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();
528  }
529  return a->get_x() < b->get_x();
530  }
531 };
532 
534  : line (al), before_x (0), after_x (DBL_MAX)
535 {
536 }
537 
538 void
540 {
541  uint32_t sz = size();
542 
543  if (sz > 0 && sz < line.npoints()) {
544  const TempoMap& map (e.session()->tempo_map());
545 
546  /* determine the limits on x-axis motion for this
547  contiguous range of control points
548  */
549 
550  if (front()->view_index() > 0) {
551  before_x = line.nth (front()->view_index() - 1)->get_x();
552 
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);
558 
559  before_x += one_tick_in_pixels;
560  }
561 
562  /* if our last point has a point after it in the line,
563  we have an "after" bound
564  */
565 
566  if (back()->view_index() < (line.npoints() - 1)) {
567  after_x = line.nth (back()->view_index() + 1)->get_x();
568 
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);
574 
575  after_x -= one_tick_in_pixels;
576  }
577  }
578 }
579 
580 double
582 {
583  if (empty()) {
584  return dx;
585  }
586 
587  /* get the maximum distance we can move any of these points along the x-axis
588  */
589 
590  double tx; /* possible position a point would move to, given dx */
591  ControlPoint* cp;
592 
593  if (dx > 0) {
594  /* check the last point, since we're moving later in time */
595  cp = back();
596  } else {
597  /* check the first point, since we're moving earlier in time */
598  cp = front();
599  }
600 
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 ();
605 }
606 
607 void
609 {
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);
613  }
614 }
615 
620 void
621 AutomationLine::start_drag_common (double x, float fraction)
622 {
623  _drag_x = x;
624  _drag_distance = 0;
625  _last_drag_fraction = fraction;
626  _drag_had_movement = false;
627  did_push = false;
628 
629  /* they are probably ordered already, but we have to make sure */
630 
632 }
633 
634 
640 pair<double, float>
641 AutomationLine::drag_motion (double const x, float fraction, bool ignore_x, bool with_push, uint32_t& final_index)
642 {
643  if (_drag_points.empty()) {
644  return pair<double,float> (x,fraction);
645  }
646 
647  double dx = ignore_x ? 0 : (x - _drag_x);
648  double dy = fraction - _last_drag_fraction;
649 
650  if (!_drag_had_movement) {
651 
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
654  motion.
655  */
656 
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.
660  */
661 
662  uint32_t expected_view_index = 0;
663  CCP contig;
664 
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);
669  }
670  contig->push_back (*i);
671  expected_view_index = (*i)->view_index() + 1;
672  }
673 
674  if (contiguous_points.back()->empty()) {
675  contiguous_points.pop_back ();
676  }
677 
678  for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
679  (*ccp)->compute_x_bounds (trackview.editor());
680  }
681  }
682 
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.
686  *
687  * if we are moving forwards with push, we don't need to do this,
688  * since all later points will move too.
689  */
690 
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)) {
695  dx = dxt;
696  }
697  }
698  }
699 
700  /* clamp y */
701 
702  for (list<ControlPoint*>::iterator i = _drag_points.begin(); i != _drag_points.end(); ++i) {
703  double const y = ((_height - (*i)->get_y()) / _height) + dy;
704  if (y < 0) {
705  dy -= y;
706  }
707  if (y > 1) {
708  dy -= (y - 1);
709  }
710  }
711 
712  if (dx || dy) {
713 
714  /* and now move each section */
715 
716  for (vector<CCP>::iterator ccp = contiguous_points.begin(); ccp != contiguous_points.end(); ++ccp) {
717  (*ccp)->move (dx, dy);
718  }
719  if (with_push) {
720  final_index = contiguous_points.back()->back()->view_index () + 1;
721  ControlPoint* p;
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);
726  ++i;
727  }
728  }
729 
730  /* update actual line coordinates (will queue a redraw)
731  */
732 
733  if (line_points.size() > 1) {
734  line->set_steps (line_points, is_stepped());
735  }
736  }
737 
738  _drag_distance += dx;
739  _drag_x += dx;
740  _last_drag_fraction = fraction;
741  _drag_had_movement = true;
742  did_push = with_push;
743 
744  return pair<double, float> (_drag_x + dx, _last_drag_fraction + dy);
745 }
746 
748 void
749 AutomationLine::end_drag (bool with_push, uint32_t final_index)
750 {
751  if (!_drag_had_movement) {
752  return;
753  }
754 
755  alist->freeze ();
757 
758  if (with_push) {
759  ControlPoint* p;
760  uint32_t i = final_index;
761  while ((p = nth (i)) != 0 && p->can_slide()) {
762  moved = sync_model_with_view_point (*p) || moved;
763  ++i;
764  }
765  }
766 
767  alist->thaw ();
768 
769  update_pending = false;
770 
771  if (moved) {
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());
775  }
776 
779 
781  did_push = false;
782 
783  contiguous_points.clear ();
784 }
785 
786 bool
788 {
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.
792  */
793 
794  double view_x = cp.get_x();
795  double view_y = 1.0 - (cp.get_y() / _height);
796 
797  /* if xval has not changed, set it directly from the model to avoid rounding errors */
798 
799  if (view_x == trackview.editor().sample_to_pixel_unrounded (_time_converter->to ((*cp.model())->when)) - _offset) {
800  view_x = (*cp.model())->when - _offset;
801  } else {
802  view_x = trackview.editor().pixel_to_sample (view_x);
803  view_x = _time_converter->from (view_x + _offset);
804  }
805 
806  update_pending = true;
807 
808  view_to_model_coord_y (view_y);
809 
810  alist->modify (cp.model(), view_x, view_y);
811 
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);
818  return true;
819  }
820 
821  return false;
822 }
823 
824 bool
825 AutomationLine::control_points_adjacent (double xval, uint32_t & before, uint32_t& after)
826 {
827  ControlPoint *bcp = 0;
828  ControlPoint *acp = 0;
829  double unit_xval;
830 
831  unit_xval = trackview.editor().sample_to_pixel_unrounded (xval);
832 
833  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
834 
835  if ((*i)->get_x() <= unit_xval) {
836 
837  if (!bcp || (*i)->get_x() > bcp->get_x()) {
838  bcp = *i;
839  before = bcp->view_index();
840  }
841 
842  } else if ((*i)->get_x() > unit_xval) {
843  acp = *i;
844  after = acp->view_index();
845  break;
846  }
847  }
848 
849  return bcp && acp;
850 }
851 
852 bool
854 {
855  // If the list is not empty, and the point is the last point in the list
856 
857  if (alist->empty()) {
858  return false;
859  }
860 
861  AutomationList::const_iterator i = alist->end();
862  --i;
863 
864  if (cp.model() == i) {
865  return true;
866  }
867 
868  return false;
869 }
870 
871 bool
873 {
874  // If the list is not empty, and the point is the first point in the list
875 
876  if (!alist->empty() && cp.model() == alist->begin()) {
877  return true;
878  }
879 
880  return false;
881 }
882 
883 // This is copied into AudioRegionGainLine
884 void
886 {
887  trackview.editor().begin_reversible_command (_("remove control point"));
888  XMLNode &before = alist->get_state();
889 
890  alist->erase (cp.model());
891 
894 
897 }
898 
906 void
907 AutomationLine::get_selectables (framepos_t start, framepos_t end, double botfrac, double topfrac, list<Selectable*>& results)
908 {
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 ();
912 
913  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
914  double const model_when = (*(*i)->model())->when;
915 
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)
918  */
919 
920  framepos_t const session_frames_when = _time_converter->to (model_when) + _time_converter->origin_b ();
921 
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);
924  }
925  }
926 }
927 
928 void
929 AutomationLine::get_inverted_selectables (Selection&, list<Selectable*>& /*results*/)
930 {
931  // hmmm ....
932 }
933 
934 void
936 {
937  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
938  (*i)->set_selected (false);
939  }
940 
941  for (PointSelection::const_iterator i = points.begin(); i != points.end(); ++i) {
942  (*i)->set_selected (true);
943  }
944 
945  if (points.empty()) {
947  } else {
949  }
950 
951  set_colors ();
952 }
953 
955 {
956  set_line_color (ARDOUR_UI::config()->color ("automation line"));
957  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
958  (*i)->set_color ();
959  }
960 }
961 
962 void
964 {
965  DEBUG_TRACE (DEBUG::Automation, string_compose ("\tline changed, existing update pending? %1\n", update_pending));
966 
967  if (!update_pending) {
968  update_pending = true;
970  }
971 }
972 
973 void
975 {
976  uint32_t vp = 0;
977  uint32_t pi = 0;
978  uint32_t np;
979 
980  if (events.empty()) {
981  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
982  delete *i;
983  }
984  control_points.clear ();
985  line->hide();
986  return;
987  }
988 
989  /* hide all existing points, and the line */
990 
991  for (vector<ControlPoint*>::iterator i = control_points.begin(); i != control_points.end(); ++i) {
992  (*i)->hide();
993  }
994 
995  line->hide ();
996  np = events.size();
997 
998  Evoral::ControlList& e = const_cast<Evoral::ControlList&> (events);
999 
1000  for (AutomationList::iterator ai = e.begin(); ai != e.end(); ++ai, ++pi) {
1001 
1002  double tx = (*ai)->when;
1003  double ty = (*ai)->value;
1004 
1005  /* convert from model coordinates to canonical view coordinates */
1006 
1007  model_to_view_coord (tx, ty);
1008 
1009  if (isnan_local (tx) || isnan_local (ty)) {
1010  warning << string_compose (_("Ignoring illegal points on AutomationLine \"%1\""),
1011  _name) << endmsg;
1012  continue;
1013  }
1014 
1015  if (tx >= max_framepos || tx < 0 || tx >= _maximum_time) {
1016  continue;
1017  }
1018 
1019  /* convert x-coordinate to a canvas unit coordinate (this takes
1020  * zoom and scroll into account).
1021  */
1022 
1024 
1025  /* convert from canonical view height (0..1.0) to actual
1026  * height coordinates (using X11's top-left rooted system)
1027  */
1028 
1029  ty = _height - (ty * _height);
1030 
1031  add_visible_control_point (vp, pi, tx, ty, ai, np);
1032  vp++;
1033  }
1034 
1035  /* discard extra CP's to avoid confusing ourselves */
1036 
1037  while (control_points.size() > vp) {
1038  ControlPoint* cp = control_points.back();
1039  control_points.pop_back ();
1040  delete cp;
1041  }
1042 
1044  control_points.back()->set_can_slide(false);
1045  }
1046 
1047  if (vp > 1) {
1048 
1049  /* reset the line coordinates given to the CanvasLine */
1050 
1051  while (line_points.size() < vp) {
1052  line_points.push_back (ArdourCanvas::Duple (0,0));
1053  }
1054 
1055  while (line_points.size() > vp) {
1056  line_points.pop_back ();
1057  }
1058 
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();
1062  }
1063 
1064  line->set_steps (line_points, is_stepped());
1065 
1066  update_visibility ();
1067  }
1068 
1070 }
1071 
1072 void
1074 {
1075  DEBUG_TRACE (DEBUG::Automation, "\t\tLINE RESET\n");
1076  update_pending = false;
1077  have_timeout = false;
1078 
1079  if (no_draw) {
1080  return;
1081  }
1082 
1084 }
1085 
1086 void
1088 {
1089  /* this must be called from the GUI thread
1090  */
1091 
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;
1099  } else {
1100  DEBUG_TRACE (DEBUG::Automation, "\ttimeout already queued, change ignored\n");
1101  }
1102  } else {
1103  reset ();
1104  }
1105 }
1106 
1107 void
1109 {
1110  /* parent must create and commit command */
1111  XMLNode &before = alist->get_state();
1112  alist->clear();
1113 
1116 }
1117 
1118 void
1120 {
1121  alist = list;
1122  queue_reset ();
1123  connect_to_list ();
1124 }
1125 
1126 void
1128 {
1129  VisibleAspects old = _visible;
1130 
1131  _visible = VisibleAspects (_visible | va);
1132 
1133  if (old != _visible) {
1134  update_visibility ();
1135  }
1136 }
1137 
1138 void
1140 {
1141  if (_visible != va) {
1142  _visible = va;
1143  update_visibility ();
1144  }
1145 }
1146 
1147 void
1149 {
1150  VisibleAspects old = _visible;
1151 
1152  _visible = VisibleAspects (_visible & ~va);
1153 
1154  if (old != _visible) {
1155  update_visibility ();
1156  }
1157 }
1158 
1159 void
1161 {
1163 }
1164 
1165 void
1167 {
1169 }
1170 
1171 XMLNode &
1173 {
1174  /* function as a proxy for the model */
1175  return alist->get_state();
1176 }
1177 
1178 int
1179 AutomationLine::set_state (const XMLNode &node, int version)
1180 {
1181  /* function as a proxy for the model */
1182  return alist->set_state (node, version);
1183 }
1184 
1185 void
1186 AutomationLine::view_to_model_coord (double& x, double& y) const
1187 {
1188  x = _time_converter->from (x);
1190 }
1191 
1192 void
1194 {
1195  /* TODO: This should be more generic (use ParameterDescriptor)
1196  * or better yet: Controllable -> set_interface();
1197  */
1198  if ( alist->parameter().type() == GainAutomation
1200  || (_desc.unit == ParameterDescriptor::DB && _desc.lower == 0.)) {
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;
1208  y = max (0.0, y);
1209  y = min (1.0, y);
1210  y = dB_to_coefficient (lower_db + y * range_db);
1211  } else if (alist->parameter().type() == PanAzimuthAutomation ||
1213  y = 1.0 - y;
1214  } else if (alist->parameter().type() == PanWidthAutomation) {
1215  y = 2.0 * y - 1.0;
1216  } else {
1217  y = y * (double)(alist->get_max_y() - alist->get_min_y()) + alist->get_min_y();
1218  if (_desc.integer_step) {
1219  y = round(y);
1220  } else if (_desc.toggled) {
1221  y = (y > 0.5) ? 1.0 : 0.0;
1222  }
1223  }
1224 }
1225 
1226 void
1228 {
1229  /* TODO: This should be more generic (use ParameterDescriptor) */
1230  if ( alist->parameter().type() == GainAutomation
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 ||
1241  y = 1.0 - y;
1242  } else if (alist->parameter().type() == PanWidthAutomation) {
1243  y = .5 + y * .5;
1244  } else {
1245  y = (y - alist->get_min_y()) / (double)(alist->get_max_y() - alist->get_min_y());
1246  }
1247 }
1248 
1249 void
1250 AutomationLine::model_to_view_coord (double& x, double& y) const
1251 {
1253  x = _time_converter->to (x) - _offset;
1254 }
1255 
1257 void
1258 AutomationLine::interpolation_changed (AutomationList::InterpolationStyle style)
1259 {
1260  if (line_points.size() > 1) {
1261  line->set_steps(line_points, is_stepped());
1262  }
1263 }
1264 
1265 void
1266 AutomationLine::add_visible_control_point (uint32_t view_index, uint32_t pi, double tx, double ty,
1267  AutomationList::iterator model, uint32_t npoints)
1268 {
1270 
1271  if (view_index >= control_points.size()) {
1272 
1273  /* make sure we have enough control points */
1274 
1275  ControlPoint* ncp = new ControlPoint (*this);
1276  ncp->set_size (control_point_box_size ());
1277 
1278  control_points.push_back (ncp);
1279  }
1280 
1282  if (pi == 0) {
1283  control_points[view_index]->set_can_slide (false);
1284  if (tx == 0) {
1285  shape = ControlPoint::Start;
1286  } else {
1287  shape = ControlPoint::Full;
1288  }
1289  } else if (pi == npoints - 1) {
1290  control_points[view_index]->set_can_slide (false);
1291  shape = ControlPoint::End;
1292  } else {
1293  control_points[view_index]->set_can_slide (true);
1294  shape = ControlPoint::Full;
1295  }
1296  } else {
1297  control_points[view_index]->set_can_slide (true);
1298  shape = ControlPoint::Full;
1299  }
1300 
1301  control_points[view_index]->reset (tx, ty, model, view_index, shape);
1302 
1303  /* finally, control visibility */
1304 
1305  if (_visible & ControlPoints) {
1306  control_points[view_index]->show ();
1307  } else {
1308  control_points[view_index]->hide ();
1309  }
1310 }
1311 
1312 void
1314 {
1316 
1317  alist->StateChanged.connect (_list_connections, invalidator (*this), boost::bind (&AutomationLine::list_changed, this), gui_context());
1318 
1319  alist->InterpolationChanged.connect (
1320  _list_connections, invalidator (*this), boost::bind (&AutomationLine::interpolation_changed, this, _1), gui_context());
1321 }
1322 
1325 {
1327 }
1328 
1332 void
1334 {
1335  if (_maximum_time == t) {
1336  return;
1337  }
1338 
1339  _maximum_time = t;
1340  reset ();
1341 }
1342 
1343 
1345 pair<framepos_t, framepos_t>
1347 {
1348  pair<framepos_t, framepos_t> r (max_framepos, 0);
1349 
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));
1353  }
1354 
1355  return r;
1356 }
1357 
1358 framepos_t
1359 AutomationLine::session_position (AutomationList::const_iterator p) const
1360 {
1361  return _time_converter->to ((*p)->when) + _offset + _time_converter->origin_b ();
1362 }
1363 
1364 void
1366 {
1367  if (_offset == off) {
1368  return;
1369  }
1370 
1371  _offset = off;
1372  reset ();
1373 }
void interpolation_changed(ARDOUR::AutomationList::InterpolationStyle)
std::vector< CCP > contiguous_points
bool transport_rolling() const
Definition: session.h:592
VisibleAspects _visible
bool control_points_adjacent(double xval, uint32_t &before, uint32_t &after)
std::string _name
virtual bool canvas_line_event(GdkEvent *event, ArdourCanvas::Item *, AutomationLine *)=0
ControlPoint * nth(uint32_t)
bool is_stepped() const
void modify_point_y(ControlPoint &, double)
virtual A from(B b) const =0
float lower
Minimum value (in Hz, for frequencies)
PublicEditor & editor() const
bool can_slide() const
Definition: control_point.h:73
PBD::ScopedConnectionList _list_connections
double _last_drag_fraction
last y position of the drag, as a fraction
void add_visibility(VisibleAspects)
virtual void start_drag_single(ControlPoint *, double, float)
std::pair< ARDOUR::framepos_t, ARDOUR::framepos_t > get_point_x_range() const
virtual void end_drag(bool with_push, uint32_t final_index)
void view_to_model_coord(double &x, double &y) const
boost::shared_ptr< ARDOUR::AutomationList > the_list() const
LIBARDOUR_API double slider_position_to_gain_with_max(double g, double max_gain=2.0)
Definition: utils.cc:762
int set_state(const XMLNode &, int version)
std::string value_as_string(const ARDOUR::ParameterDescriptor &desc, double v)
Lists of selected things.
Definition: selection.h:66
void model_to_view_coord_y(double &) const
void add_command(Command *const cmd)
Definition: session.h:787
TempoMap & tempo_map()
Definition: session.h:596
Representation of the interface of the Editor class.
virtual void freeze()
uint32_t _line_color
virtual B to(A a) const =0
double get_min_y() const
Definition: Beats.hpp:239
std::vector< ControlPoint * > control_points
LIBPBD_API Transmitter warning
uint32_t npoints() const
int set_state(const XMLNode &, int version)
void set_uses_gain_mapping(bool yn)
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
void add_visible_control_point(uint32_t, uint32_t, double, double, ARDOUR::AutomationList::iterator, uint32_t)
void start_drag_common(double, float)
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
uint32_t current_height() const
uint32_t view_index() const
Definition: control_point.h:75
framecnt_t frame_rate() const
Definition: session.h:365
double get_x() const
Definition: control_point.h:59
void move_to(double x, double y, ShapeType)
static bool floateq(float a, float b, int max_ulps_diff)
Definition: floating.h:52
#define invalidator(x)
Definition: gui_thread.h:40
std::string fraction_to_relative_string(double, double) const
std::string get_verbose_cursor_relative_string(double, double) const
static float accurate_coefficient_to_dB(float coeff)
Definition: dB.h:38
static UI * instance()
Definition: gtk_ui.h:119
double control_point_box_size()
bool sync_model_with_view_points(std::list< ControlPoint * >)
#define _(Text)
Definition: i18n.h:11
const ARDOUR::ParameterDescriptor _desc
ArdourCanvas::PolyLine * line
LIBARDOUR_API uint64_t Automation
Definition: debug.cc:62
void modify(iterator, double, double)
int64_t framecnt_t
Definition: types.h:76
virtual void remove_point(ControlPoint &)
bool _drag_had_movement
true if the drag has seen movement, otherwise false
LIBARDOUR_API RCConfiguration * Config
Definition: globals.cc:119
void set_offset(ARDOUR::framecnt_t)
float upper
Maximum value (in Hz, for frequencies)
bool sync_model_with_view_point(ControlPoint &)
virtual std::string get_verbose_cursor_string(double) const
XMLNode & get_state(void)
ArdourCanvas::Container * group
Definition: amp.h:29
#define isnan_local
const PBD::ID & id() const
Definition: stateful.h:68
ARDOUR::Session * session() const
Definition: axis_view.h:52
virtual std::pair< double, float > drag_motion(double, float, bool, bool with_push, uint32_t &final_index)
virtual void begin_reversible_command(std::string cmd_name)=0
#define gui_context()
Definition: gui_thread.h:36
framepos_t session_position(ARDOUR::AutomationList::const_iterator) const
virtual void commit_reversible_command()=0
TimeAxisView & trackview
void set_size(double)
#define DEBUG_TRACE(bits, str)
Definition: debug.h:55
virtual ~AutomationLine()
int64_t framepos_t
Definition: types.h:66
virtual Selection & get_selection() const =0
Evoral::TimeConverter< double, ARDOUR::framepos_t > * _time_converter
bool automation_write() const
ARDOUR::framecnt_t _offset
void apply_to_points(T &obj, void(T::*method)(const ControlList &))
void view_to_model_coord_y(double &) const
bool get_selected() const
Definition: selectable.h:40
EventList::size_type size() const
void erase(iterator)
double get_max_y() const
PBD::Signal1< void, InterpolationStyle > InterpolationChanged
const Parameter & parameter() const
T * get() const
Definition: shared_ptr.hpp:268
void call_slot(EventLoop::InvalidationRecord *, const boost::function< void()> &)
Definition: abstract_ui.cc:368
virtual void start_drag_multiple(std::list< ControlPoint * >, float, XMLNode *)
double _drag_distance
total x movement of the drag, in canvas units
bool is_last_point(ControlPoint &)
static PublicEditor & instance()
double _drag_x
last x position of the drag, in units
PointSelection points
Definition: selection.h:86
std::string fraction_to_string(double) const
PBD::Signal0< void > StateChanged
LIBARDOUR_API double gain_to_slider_position_with_max(double g, double max_gain=2.0)
Definition: utils.cc:756
InterpolationStyle interpolation() const
const char * name
boost::shared_ptr< ARDOUR::AutomationList > alist
void model_to_view_coord(double &x, double &y) const
void reset_callback(const Evoral::ControlList &)
bool is_first_point(ControlPoint &)
AutomationLine(const std::string &name, TimeAxisView &tv, ArdourCanvas::Item &parent, boost::shared_ptr< ARDOUR::AutomationList > al, const ARDOUR::ParameterDescriptor &desc, Evoral::TimeConverter< double, ARDOUR::framepos_t > *converter=0)
virtual MementoCommandBinder< ARDOUR::AutomationList > * memento_command_binder()
bool toggled
True iff parameter is boolean.
void get_selectables(ARDOUR::framepos_t, ARDOUR::framepos_t, double, double, std::list< Selectable * > &)
Definition: xml++.h:95
static UIConfiguration * config()
Definition: ardour_ui.h:188
ARDOUR::AutomationList::iterator model() const
Definition: control_point.h:80
uint32_t type() const
Definition: Parameter.hpp:47
Definition: debug.h:30
double divisions_per_bar() const
Definition: tempo.h:70
void set_maximum_time(ARDOUR::framecnt_t)
virtual framepos_t pixel_to_sample(double pixel) const =0
bool operator()(ControlPoint const *a, ControlPoint const *b) const
void get_inverted_selectables(Selection &, std::list< Selectable * > &results)
static float dB_to_coefficient(float dB)
Definition: dB.h:30
void set_line_color(uint32_t)
static const framepos_t max_framepos
Definition: types.h:78
double frames_per_bar(const Tempo &, framecnt_t sr) const
Definition: tempo.cc:63
double get_y() const
Definition: control_point.h:60
void set_height(guint32)
static uint32_t preset_height(Height)
virtual bool event_handler(GdkEvent *)
ARDOUR::framecnt_t _maximum_time
virtual double sample_to_pixel_unrounded(framepos_t frame) const =0
double string_to_fraction(std::string const &) const
bool terminal_points_can_slide
void set_list(boost::shared_ptr< ARDOUR::AutomationList > list)
void remove_visibility(VisibleAspects)
ArdourCanvas::Points line_points
void register_with_memento_command_factory(PBD::ID, PBD::StatefulDestructible *)
void reset_line_coords(ControlPoint &)
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
virtual void start_drag_line(uint32_t, uint32_t, float)
void set_visibility(VisibleAspects)
bool empty() const
void set_selected_points(PointSelection const &)
std::list< ControlPoint * > _drag_points
points we are dragging
virtual ARDOUR::Session * session() const =0
LIBARDOUR_API PBD::PropertyDescriptor< bool > color
Definition: route_group.cc:50