Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / wave_view.cc
index f420c73980583cb611903f4ebc8bb5052278ecc0..3467672b88accf5d04700b432cf4c47dd8ccfcd8 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "pbd/compose.h"
 #include "pbd/signals.h"
+#include "pbd/stacktrace.h"
 
 #include "ardour/types.h"
 #include "ardour/dB.h"
@@ -40,9 +41,10 @@ using namespace std;
 using namespace ARDOUR;
 using namespace ArdourCanvas;
 
-bool WaveView::_gradient_waveforms = true;
+double WaveView::_global_gradient_depth = 0.6;
 bool WaveView::_global_logscaled = false;
 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
+bool WaveView::_global_show_waveform_clipping = true;
 
 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
 
@@ -60,14 +62,22 @@ WaveView::WaveView (Group* parent, boost::shared_ptr<ARDOUR::AudioRegion> region
        , _clip_color (0xff0000ff)
        , _logscaled (_global_logscaled)
        , _shape (_global_shape)
-       , _amplitude (1.0)
+       , _gradient_depth (_global_gradient_depth)
        , _shape_independent (false)
        , _logscaled_independent (false)
-       , _region_start (0)
+       , _gradient_depth_independent (false)
+       , _amplitude_above_axis (1.0)
+       , _region_start (region->start())
+       , _sample_start (-1)
+       , _sample_end (-1)
 {
        VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
 }
 
+WaveView::~WaveView ()
+{
+}
+
 void
 WaveView::handle_visual_property_change ()
 {
@@ -83,207 +93,476 @@ WaveView::handle_visual_property_change ()
                changed = true;
        }
 
+       if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
+               _gradient_depth = global_gradient_depth();
+               changed = true;
+       }
+       
        if (changed) {
-               invalidate_image_cache ();
+               invalidate_image ();
        }
 }
 
 void
 WaveView::set_fill_color (Color c)
 {
-       invalidate_image_cache ();
-       Fill::set_fill_color (c);
+       if (c != _fill_color) {
+               invalidate_image ();
+               Fill::set_fill_color (c);
+       }
 }
 
 void
 WaveView::set_outline_color (Color c)
 {
-       invalidate_image_cache ();
-       Outline::set_outline_color (c);
+       if (c != _outline_color) {
+               invalidate_image ();
+               Outline::set_outline_color (c);
+       }
 }
 
 void
 WaveView::set_samples_per_pixel (double samples_per_pixel)
 {
-       begin_change ();
-       
-       _samples_per_pixel = samples_per_pixel;
+       if (samples_per_pixel != _samples_per_pixel) {
+               begin_change ();
 
-       _bounding_box_dirty = true;
-       end_change ();
+               _samples_per_pixel = samples_per_pixel;
+               
+               _bounding_box_dirty = true;
+               
+               end_change ();
+               
+               invalidate_image ();
+       }
+}
+
+static inline double
+image_to_window (double wave_origin, double image_start)
+{
+       return wave_origin + image_start;
+}
+
+static inline double
+window_to_image (double wave_origin, double image_start)
+{
+       return image_start - wave_origin;
+}
 
-       invalidate_whole_cache ();
+static inline float
+_log_meter (float power, double lower_db, double upper_db, double non_linearity)
+{
+       return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
 }
 
+static inline float
+alt_log_meter (float power)
+{
+       return _log_meter (power, -192.0, 0.0, 8.0);
+}
+
+struct LineTips {
+    double top;
+    double bot;
+    bool clip_max;
+    bool clip_min;
+    
+    LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
+};
+
 void
-WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
+WaveView::draw_image (PeakData* _peaks, int n_peaks) const
 {
-       assert (_samples_per_pixel != 0);
+       _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, _height);
 
-       if (!_region) {
-               return;
-       }
+       Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
 
-       /* p, start and end are offsets from the start of the source.
-          area is relative to the position of the region.
-        */
-       
-       int const start = rint (area.x0 + _region_start / _samples_per_pixel);
-       int const end   = rint (area.x1 + _region_start / _samples_per_pixel);
+       boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
 
-       int p = start;
-       list<CacheEntry*>::iterator cache = _cache.begin ();
+       /* Clip level nominally set to -0.9dBFS to account for inter-sample
+          interpolation possibly clipping (value may be too low).
 
-       while (p < end) {
+          We adjust by the region's own gain (but note: not by any gain
+          automation or its gain envelope) so that clip indicators are closer
+          to providing data about on-disk data. This multiplication is
+          needed because the data we get from AudioRegion::read_peaks()
+          has been scaled by scale_amplitude() already.
+       */
 
-               /* Step through cache entries that end at or before our current position, p */
-               while (cache != _cache.end() && (*cache)->end() <= p) {
-                       ++cache;
-               }
+       const double clip_level = 0.98853 * _region->scale_amplitude();
 
-               /* Now either:
-                  1. we have run out of cache entries
-                  2. the one we are looking at finishes after p but also starts after p.
-                  3. the one we are looking at finishes after p and starts before p.
+       if (_shape == WaveView::Rectified) {
 
-                  Set up a pointer to the cache entry that we will use on this iteration.
-               */
+               /* each peak is a line from the bottom of the waveview
+                * to a point determined by max (_peaks[i].max,
+                * _peaks[i].min)
+                */
 
-               CacheEntry* render = 0;
+               if (_logscaled) {
+                       for (int i = 0; i < n_peaks; ++i) {
+                               tips[i].bot = height();
+                               tips[i].top = position (alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min)))));
 
-               if (cache == _cache.end ()) {
+                               if (fabs (_peaks[i].max) >= clip_level) {
+                                       tips[i].clip_max = true;
+                               }
 
-                       /* Case 1: we have run out of cache entries, so make a new one for
-                          the whole required area and put it in the list.
-                       */
-                       
-                       CacheEntry* c = new CacheEntry (this, p, end);
-                       _cache.push_back (c);
-                       render = c;
+                               if (fabs (_peaks[i].min) >= clip_level) {
+                                       tips[i].clip_min = true;
+                               }
+                       }
+               } else {for (int i = 0; i < n_peaks; ++i) {
+                               tips[i].bot = height();
+                               tips[i].top = position (max (fabs (_peaks[i].max), fabs (_peaks[i].min)));
+
+                               if (fabs (_peaks[i].max) >= clip_level) {
+                                       tips[i].clip_max = true;
+                               }
+
+                               if (fabs (_peaks[i].min) >= clip_level) {
+                                       tips[i].clip_min = true;
+                               }
+                       }
+               }
 
-               } else if ((*cache)->start() > p) {
+       } else {
 
-                       /* Case 2: we have a cache entry, but it starts after p, so we
-                          need another one for the missing bit.
-                       */
+               if (_logscaled) {
+                       for (int i = 0; i < n_peaks; ++i) {
+                               Coord top = _peaks[i].min;
+                               Coord bot = _peaks[i].max;
 
-                       CacheEntry* c = new CacheEntry (this, p, (*cache)->start());
-                       cache = _cache.insert (cache, c);
-                       ++cache;
-                       render = c;
+                               if (fabs (top) >= clip_level) {
+                                       tips[i].clip_max = true;
+                               }
+
+                               if (fabs (bot) >= clip_level) {
+                                       tips[i].clip_min = true;
+                               }
+
+                               if (top > 0.0) {
+                                       top = position (alt_log_meter (fast_coefficient_to_dB (top)));
+                               } else if (top < 0.0) {
+                                       top = position (-alt_log_meter (fast_coefficient_to_dB (-top)));
+                               } else {
+                                       top = position (0.0);
+                               }
+
+                               if (bot > 0.0) {
+                                       bot = position (alt_log_meter (fast_coefficient_to_dB (bot)));
+                               } else if (bot < 0.0) {
+                                       bot = position (-alt_log_meter (fast_coefficient_to_dB (-bot)));
+                               } else {
+                                       bot = position (0.0);
+                               }
+
+                               tips[i].top = top;
+                               tips[i].bot = bot;
+                       } 
 
                } else {
+                       for (int i = 0; i < n_peaks; ++i) {
+
+                               if (fabs (_peaks[i].max) >= clip_level) {
+                                       tips[i].clip_max = true;
+                               }
 
-                       /* Case 3: we have a cache entry that will do at least some of what
-                          we have left, so render it.
-                       */
+                               if (fabs (_peaks[i].min) >= clip_level) {
+                                       tips[i].clip_min = true;
+                               }
+
+                               tips[i].top = position (_peaks[i].min);
+                               tips[i].bot = position (_peaks[i].max);
 
-                       render = *cache;
-                       ++cache;
 
+                       }
                }
+       }
 
-               int const this_end = min (end, render->end ());
+       if (gradient_depth() != 0.0) {
+                       
+               Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _height));
+                       
+               double stops[3];
+                       
+               double r, g, b, a;
+
+               if (_shape == Rectified) {
+                       stops[0] = 0.1;
+                       stops[0] = 0.3;
+                       stops[0] = 0.9;
+               } else {
+                       stops[0] = 0.1;
+                       stops[1] = 0.5;
+                       stops[2] = 0.9;
+               }
+
+               color_to_rgba (_fill_color, r, g, b, a);
+               gradient->add_color_stop_rgba (stops[0], r, g, b, a);
+               gradient->add_color_stop_rgba (stops[2], r, g, b, a);
+
+               /* generate a new color for the middle of the gradient */
+               double h, s, v;
+               color_to_hsv (_fill_color, h, s, v);
+               /* change v towards white */
+               v *= 1.0 - gradient_depth();
+               Color center = hsv_to_color (h, s, v, a);
+               color_to_rgba (center, r, g, b, a);
+               gradient->add_color_stop_rgba (stops[1], r, g, b, a);
+                       
+               context->set_source (gradient);
+       } else {
+               set_source_rgba (context, _fill_color);
+       }
+
+       /* ensure single-pixel lines */
                
-               Coord const left  =        p - _region_start / _samples_per_pixel;
-               Coord const right = this_end - _region_start / _samples_per_pixel;
+       context->set_line_width (0.5);
+       context->translate (0.5, 0.0);
+
+       /* draw the lines */
+
+       if (_shape == WaveView::Rectified) {
+               for (int i = 0; i < n_peaks; ++i) {
+                       context->move_to (i, tips[i].top); /* down 1 pixel */
+                       context->line_to (i, tips[i].bot);
+                       context->stroke ();
+               }
+       } else {
+               for (int i = 0; i < n_peaks; ++i) {
+                       context->move_to (i, tips[i].top);
+                       context->line_to (i, tips[i].bot);
+                       context->stroke ();
+               }
+       }
+
+       /* now add dots to the top and bottom of each line (this is
+        * modelled on pyramix, except that we add clipping indicators.
+        */
+
+       if (_global_show_waveform_clipping) {
                
-               context->save ();
+               context->set_source_rgba (0, 0, 0, 1.0);
                
-               context->rectangle (left, area.y0, right, area.height());
-               context->clip ();
+               /* the height of the clip-indicator should be at most 7 pixels,
+                  or 5% of the height of the waveview item.
+               */
+               const double clip_height = min (7.0, ceil (_height * 0.05));
                
-               context->translate (left, 0);
+               for (int i = 0; i < n_peaks; ++i) {
+                       context->move_to (i, tips[i].top);
+                       
+                       bool show_top_clip = (_shape == WaveView::Rectified && (tips[i].clip_max || tips[i].clip_min)) ||
+                               tips[i].clip_max;
+                       
+                       if (show_top_clip) {
+                               context->set_source_rgba (1.0, 0, 0, 1.0);
+                               context->rel_line_to (0, clip_height);
+                               context->stroke ();
+                               context->set_source_rgba (0.0, 0, 0, 1.0);
+                       } else {
+                               context->rel_line_to (0, 1.0);
+                               context->stroke ();
+                       }
 
-               context->set_source (render->image(), render->start() - p, 0);
-               context->paint ();
-               
-               context->restore ();
+                       if (_shape != WaveView::Rectified) {
+                               context->move_to (i, tips[i].bot);
+                               if (tips[i].clip_min) {
+                                       context->set_source_rgba (1.0, 0, 0, 1.0);
+                                       context->rel_line_to (0, -clip_height);
+                                       context->stroke ();
+                                       context->set_source_rgba (0.0, 0, 0, 1.0);
+                               } else {
+                                       context->rel_line_to (0, -1.0);
+                                       context->stroke ();
+                               }
+                       }
+               }
+       }
 
-               p = min (end, render->end ());
+       if (show_zero_line()) {
+               set_source_rgba (context, _zero_color);
+               context->move_to (0, position (0.0));
+               context->line_to (n_peaks, position (0.0));
+               context->stroke ();
        }
 }
 
 void
-WaveView::compute_bounding_box () const
+WaveView::ensure_cache (framepos_t start, framepos_t end) const
 {
-       if (_region) {
-               _bounding_box = Rect (0, 0, _region->length() / _samples_per_pixel, _height);
-       } else {
-               _bounding_box = boost::optional<Rect> ();
+       if (_image && _sample_start <= start && _sample_end >= end) {
+               /* cache already covers required range, do nothing */
+               return;
        }
+
+       /* sample position is canonical here, and we want to generate
+        * an image that spans about twice the canvas width 
+        */
        
-       _bounding_box_dirty = false;
-}
-       
-void
-WaveView::set_height (Distance height)
-{
-       begin_change ();
+       const framepos_t center = start + ((end - start) / 2);
+       const framecnt_t canvas_samples = 2 * (_canvas->visible_area().width() * _samples_per_pixel);
+
+       /* we can request data from anywhere in the Source, between 0 and its length
+        */
 
-       _height = height;
+       _sample_start = max ((framepos_t) 0, (center - canvas_samples));
+       _sample_end = min (center + canvas_samples, _region->source_length (0));
 
-       _bounding_box_dirty = true;
-       end_change ();
+       double pixel_start = floor (_sample_start / (double) _samples_per_pixel);
+       double pixel_end = ceil (_sample_end / (double) _samples_per_pixel);
+       int n_peaks = llrintf (pixel_end - pixel_start);
+
+       boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
 
-       invalidate_image_cache ();
+       _region->read_peaks (peaks.get(), n_peaks, 
+                            _sample_start, _sample_end - _sample_start,
+                            _channel, 
+                            _samples_per_pixel);
+
+       draw_image (peaks.get(), n_peaks);
 }
 
 void
-WaveView::set_channel (int channel)
+WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
 {
-       begin_change ();
+       assert (_samples_per_pixel != 0);
+
+       if (!_region) {
+               return;
+       }
+
+       Rect self = item_to_window (Rect (0.0, 0.0, floor (_region->length() / _samples_per_pixel), _height));
+       boost::optional<Rect> d = self.intersection (area);
+
+       if (!d) {
+               return;
+       }
        
-       _channel = channel;
+       Rect draw = d.get();
 
-       _bounding_box_dirty = true;
-       end_change ();
+       /* window coordinates - pixels where x=0 is the left edge of the canvas
+        * window. We round up and down in case we were asked to
+        * draw "between" pixels at the start and/or end.
+        */
+
+       const double draw_start = floor (draw.x0);
+       const double draw_end = ceil (draw.x1);
 
-       invalidate_whole_cache ();
+       // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
+       
+       /* image coordnates: pixels where x=0 is the start of this waveview,
+        * wherever it may be positioned. thus image_start=N means "an image
+        * that beings N pixels after the start of region that this waveview is
+        * representing. 
+        */
+
+       const framepos_t image_start = window_to_image (self.x0, draw_start);
+       const framepos_t image_end = window_to_image (self.x0, draw_end);
+
+       // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
+
+       /* sample coordinates - note, these are not subject to rounding error */
+       framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
+       framepos_t sample_end   = _region_start + (image_end * _samples_per_pixel);
+
+       // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl;
+
+       ensure_cache (sample_start, sample_end);
+
+       // cerr << "Cache contains " << _cache->pixel_start() << " .. " << _cache->pixel_end() << " / " 
+       // << _cache->sample_start() << " .. " << _cache->sample_end()
+       // << endl;
+
+       double image_offset = (_sample_start - _region->start()) / _samples_per_pixel;
+
+       // cerr << "Offset into image to place at zero: " << image_offset << endl;
+
+       context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
+
+       /* round image origin position to an exact pixel in device space to
+        * avoid blurring
+        */
+
+       double x  = self.x0 + image_offset;
+       double y  = self.y0;
+       context->user_to_device (x, y);
+       x = round (x);
+       y = round (y);
+       context->device_to_user (x, y);
+
+       context->set_source (_image, x, y);
+       context->fill ();
 }
 
 void
-WaveView::invalidate_whole_cache ()
+WaveView::compute_bounding_box () const
+{
+       if (_region) {
+               _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height);
+       } else {
+               _bounding_box = boost::optional<Rect> ();
+       }
+       
+       _bounding_box_dirty = false;
+}
+       
+void
+WaveView::set_height (Distance height)
 {
-       for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
-               delete *i;
+       if (height != _height) {
+               begin_change ();
+               
+               _height = height;
+               
+               _bounding_box_dirty = true;
+               end_change ();
+               
+               invalidate_image ();
        }
-
-       _cache.clear ();
-       _canvas->item_visual_property_changed (this);
 }
 
 void
-WaveView::invalidate_image_cache ()
+WaveView::set_channel (int channel)
 {
-       for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
-               (*i)->clear_image ();
+       if (channel != _channel) {
+               begin_change ();
+               
+               _channel = channel;
+               
+               _bounding_box_dirty = true;
+               end_change ();
+               
+               invalidate_image ();
        }
-       _canvas->item_visual_property_changed (this);
 }
 
 void
-WaveView::region_resized ()
+WaveView::invalidate_image ()
 {
-       _bounding_box_dirty = true;
+       begin_visual_change ();
+
+       _image.clear ();
+       _sample_start  = -1;
+       _sample_end = -1;
+       
+       end_visual_change ();
 }
 
+
 void
 WaveView::set_logscaled (bool yn)
 {
        if (_logscaled != yn) {
                _logscaled = yn;
-               invalidate_image_cache ();
+               invalidate_image ();
        }
 }
 
 void
-WaveView::set_amplitude (double a)
+WaveView::gain_changed ()
 {
-       if (_amplitude != a) {
-               _amplitude = a;
-               invalidate_image_cache ();
-       }
+       invalidate_image ();
 }
 
 void
@@ -291,7 +570,7 @@ WaveView::set_zero_color (Color c)
 {
        if (_zero_color != c) {
                _zero_color = c;
-               invalidate_image_cache ();
+               invalidate_image ();
        }
 }
 
@@ -300,7 +579,7 @@ WaveView::set_clip_color (Color c)
 {
        if (_clip_color != c) {
                _clip_color = c;
-               invalidate_image_cache ();
+               invalidate_image ();
        }
 }
 
@@ -309,7 +588,7 @@ WaveView::set_show_zero_line (bool yn)
 {
        if (_show_zero != yn) {
                _show_zero = yn;
-               invalidate_image_cache ();
+               invalidate_image ();
        }
 }
 
@@ -318,7 +597,16 @@ WaveView::set_shape (Shape s)
 {
        if (_shape != s) {
                _shape = s;
-               invalidate_image_cache ();
+               invalidate_image ();
+       }
+}
+
+void
+WaveView::set_amplitude_above_axis (double a)
+{
+       if (_amplitude_above_axis != a) {
+               _amplitude_above_axis = a;
+               invalidate_image ();
        }
 }
 
@@ -337,226 +625,65 @@ WaveView::set_global_logscaled (bool yn)
        if (_global_logscaled != yn) {
                _global_logscaled = yn;
                VisualPropertiesChanged (); /* EMIT SIGNAL */
-               
        }
 }
 
 void
-WaveView::set_region_start (frameoffset_t start)
+WaveView::region_resized ()
 {
-       _region_start = start;
-       _bounding_box_dirty = true;
-}
+       if (!_region) {
+               return;
+       }
 
-/** Construct a new CacheEntry with peak data between two offsets
- *  in the source.
- */
-WaveView::CacheEntry::CacheEntry (
-       WaveView const * wave_view,
-       int start,
-       int end
-       )
-       : _wave_view (wave_view)
-       , _start (start)
-       , _end (end)
-{
-       _n_peaks = _end - _start;
-       _peaks.reset (new PeakData[_n_peaks]);
-
-       _wave_view->_region->read_peaks (
-               _peaks.get(),
-               _n_peaks,
-               _start * _wave_view->_samples_per_pixel,
-               (_end - _start) * _wave_view->_samples_per_pixel,
-               _wave_view->_channel,
-               _wave_view->_samples_per_pixel
-               );
-}
+       /* special: do not use _region->length() here to compute
+          bounding box because it will already have changed.
+          
+          if we have a bounding box, use it.
+       */
 
-WaveView::CacheEntry::~CacheEntry ()
-{
-}
+       _pre_change_bounding_box = _bounding_box;
 
-static inline float
-_log_meter (float power, double lower_db, double upper_db, double non_linearity)
-{
-       return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
-}
+       _bounding_box_dirty = true;
+       compute_bounding_box ();
 
-static inline float
-alt_log_meter (float power)
-{
-       return _log_meter (power, -192.0, 0.0, 8.0);
+       end_change ();
 }
 
-Cairo::RefPtr<Cairo::ImageSurface>
-WaveView::CacheEntry::image ()
+Coord
+WaveView::position (double s) const
 {
-       if (!_image) {
-
-               _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _n_peaks, _wave_view->_height);
-               Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
-
-               /* Draw the edge of the waveform, top half first, the loop back
-                * for the bottom half to create a clockwise path
-                */
-
-               context->begin_new_path();
-
-
-               if (_wave_view->_shape == WaveView::Rectified) {
-
-                       /* top edge of waveform is based on max (fabs (peak_min, peak_max))
-                        */
-
-                       if (_wave_view->_logscaled) {
-                               for (int i = 0; i < _n_peaks; ++i) {
-                                       context->line_to (i + 0.5, position (_wave_view->amplitude() * 
-                                                                            alt_log_meter (fast_coefficient_to_dB (
-                                                                                                   max (fabs (_peaks[i].max), fabs (_peaks[i].min))))));
-                               }
-                       } else {
-                               for (int i = 0; i < _n_peaks; ++i) {
-                                       context->line_to (i + 0.5, position (_wave_view->amplitude() * max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
-                               }
-                       }
-
-               } else {
-                       if (_wave_view->_logscaled) {
-                               for (int i = 0; i < _n_peaks; ++i) {
-                                       Coord y = _peaks[i].max;
-                                       if (y > 0.0) {
-                                               context->line_to (i + 0.5, position (_wave_view->amplitude() * alt_log_meter (fast_coefficient_to_dB (y))));
-                                       } else if (y < 0.0) {
-                                               context->line_to (i + 0.5, position (_wave_view->amplitude() * -alt_log_meter (fast_coefficient_to_dB (-y))));
-                                       } else {
-                                               context->line_to (i + 0.5, position (0.0));
-                                       }
-                               } 
-                       } else {
-                               for (int i = 0; i < _n_peaks; ++i) {
-                                       context->line_to (i + 0.5, position (_wave_view->amplitude() * _peaks[i].max));
-                               }
-                       }
-               }
-
-               /* from final top point, move out of the clip zone */
-
-               context->line_to (_n_peaks + 10, position (0.0));
-
-       
-               /* bottom half, in reverse */
-       
-               if (_wave_view->_shape == WaveView::Rectified) {
-                       
-                       /* lower half: drop to the bottom, then a line back to
-                        * beyond the left edge of the clip region 
-                        */
-
-                       context->line_to (_n_peaks + 10, _wave_view->_height);
-                       context->line_to (-10.0, _wave_view->_height);
-
-               } else {
-
-                       if (_wave_view->_logscaled) {
-                               for (int i = _n_peaks-1; i >= 0; --i) {
-                                       Coord y = _peaks[i].min;
-                                       if (y > 0.0) {
-                                               context->line_to (i + 0.5, position (_wave_view->amplitude() * alt_log_meter (fast_coefficient_to_dB (y))));
-                                       } else if (y < 0.0) {
-                                               context->line_to (i + 0.5, position (_wave_view->amplitude() * -alt_log_meter (fast_coefficient_to_dB (-y))));
-                                       } else {
-                                               context->line_to (i + 0.5, position (0.0));
-                                       }
-                               } 
-                       } else {
-                               for (int i = _n_peaks-1; i >= 0; --i) {
-                                       context->line_to (i + 0.5, position (_wave_view->amplitude() * _peaks[i].min));
-                               }
-                       }
-               
-                       /* from final bottom point, move out of the clip zone */
-                       
-                       context->line_to (-10.0, position (0.0));
-               }
-
-               context->close_path ();
-
-               if (WaveView::gradient_waveforms()) {
-
-                       Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _wave_view->_height));
-                       
-                       double stops[3];
-                       
-                       double r, g, b, a;
-
-                       if (_wave_view->_shape == Rectified) {
-                               stops[0] = 0.1;
-                               stops[0] = 0.3;
-                               stops[0] = 0.9;
-                       } else {
-                               stops[0] = 0.1;
-                               stops[1] = 0.5;
-                               stops[2] = 0.9;
-                       }
-
-                       color_to_rgba (_wave_view->_fill_color, r, g, b, a);
-                       gradient->add_color_stop_rgba (stops[0], r, g, b, a);
-                       gradient->add_color_stop_rgba (stops[2], r, g, b, a);
-                       
-                       /* generate a new color for the middle of the gradient */
-                       double h, s, v;
-                       color_to_hsv (_wave_view->_fill_color, h, s, v);
-                       /* tone down the saturation */
-                       s *= 0.60;
-                       Color center = hsv_to_color (h, s, v, a);
-                       color_to_rgba (center, r, g, b, a);
-                       gradient->add_color_stop_rgba (stops[1], r, g, b, a);
-                       
-                       context->set_source (gradient);
-               } else {
-                       set_source_rgba (context, _wave_view->_fill_color);
-               }
-
-               context->fill_preserve ();
-               _wave_view->setup_outline_context (context);
-               context->stroke ();
-
-               if (_wave_view->show_zero_line()) {
-                       set_source_rgba (context, _wave_view->_zero_color);
-                       context->move_to (0, position (0.0));
-                       context->line_to (_n_peaks, position (0.0));
-                       context->stroke ();
-               }
-       }
-
-       return _image;
-}
+       /* it is important that this returns an integral value, so that we 
+          can ensure correct single pixel behaviour.
+        */
 
+       Coord pos;
 
-Coord
-WaveView::CacheEntry::position (double s) const
-{
-       switch (_wave_view->_shape) {
+       switch (_shape) {
        case Rectified:
-               return _wave_view->_height - (s * _wave_view->_height);
+               pos = floor (_height - (s * _height));
        default:
+               pos = floor ((1.0-s) * (_height / 2.0));
                break;
        }
-       return (s+1.0) * (_wave_view->_height / 2.0);
+
+       return min (_height, (max (0.0, pos)));
 }
 
 void
-WaveView::CacheEntry::clear_image ()
+WaveView::set_global_gradient_depth (double depth)
 {
-       _image.clear ();
+       if (_global_gradient_depth != depth) {
+               _global_gradient_depth = depth;
+               VisualPropertiesChanged (); /* EMIT SIGNAL */
+       }
 }
-           
+
 void
-WaveView::set_gradient_waveforms (bool yn)
+WaveView::set_global_show_waveform_clipping (bool yn)
 {
-       if (_gradient_waveforms != yn) {
-               _gradient_waveforms = yn;
+       if (_global_show_waveform_clipping != yn) {
+               _global_show_waveform_clipping = yn;
                VisualPropertiesChanged (); /* EMIT SIGNAL */
        }
 }
+