minor fixes/changes based on comparison with existing bindings
[ardour.git] / gtk2_ardour / canvas-waveview.c
index c8e9f613327aa0edea45d315b815f4d4ec9846f1..aab781081d57d124d887cb227ddb3b80069f2a3a 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <math.h>
 #include <libgnomecanvas/libgnomecanvas.h>
+#include <cairo.h>
 #include <string.h>
 #include <limits.h>
 
@@ -30,6 +31,7 @@
 #include "canvas-waveview.h"
 #include "rgb_macros.h"
 
+
 extern void c_stacktrace();
 
 enum {
@@ -49,7 +51,12 @@ enum {
         PROP_Y,
         PROP_HEIGHT,
         PROP_WAVE_COLOR,
+        PROP_CLIP_COLOR,
+        PROP_ZERO_COLOR,
+        PROP_FILL_COLOR,
+        PROP_FILLED,
         PROP_RECTIFIED,
+        PROP_ZERO_LINE,
         PROP_REGION_START,
         PROP_LOGSCALED,
 };
@@ -103,7 +110,7 @@ static void gnome_canvas_waveview_set_data_src   (GnomeCanvasWaveView *,
 static void gnome_canvas_waveview_set_channel    (GnomeCanvasWaveView *,
                                                   guint32);
 
-static gint32 gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview,
+static guint32 gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview,
                                                   gulong               start_sample,
                                                   gulong               end_sample);
 
@@ -249,6 +256,34 @@ gnome_canvas_waveview_class_init (GnomeCanvasWaveViewClass *class)
                                     0, G_MAXUINT, 0,
                                     (G_PARAM_READABLE | G_PARAM_WRITABLE)));
         
+        g_object_class_install_property
+                (gobject_class,
+                 PROP_CLIP_COLOR,
+                 g_param_spec_uint ("clip_color", NULL, NULL,
+                                    0, G_MAXUINT, 0,
+                                    (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+        
+        g_object_class_install_property
+                (gobject_class,
+                 PROP_ZERO_COLOR,
+                 g_param_spec_uint ("zero_color", NULL, NULL,
+                                    0, G_MAXUINT, 0,
+                                    (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+
+        g_object_class_install_property
+                (gobject_class,
+                 PROP_FILL_COLOR,
+                 g_param_spec_uint ("fill_color", NULL, NULL,
+                                    0, G_MAXUINT, 0,
+                                    (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+
+        g_object_class_install_property
+                (gobject_class,
+                 PROP_FILLED,
+                 g_param_spec_boolean ("filled", NULL, NULL,
+                                       FALSE,
+                                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+        
         g_object_class_install_property
                 (gobject_class,
                  PROP_RECTIFIED,
@@ -256,6 +291,13 @@ gnome_canvas_waveview_class_init (GnomeCanvasWaveViewClass *class)
                                        FALSE,
                                        (G_PARAM_READABLE | G_PARAM_WRITABLE)));
 
+        g_object_class_install_property
+                (gobject_class,
+                 PROP_ZERO_LINE,
+                 g_param_spec_boolean ("zero_line", NULL, NULL,
+                                       FALSE,
+                                       (G_PARAM_READABLE | G_PARAM_WRITABLE)));
+
         g_object_class_install_property
                 (gobject_class,
                  PROP_LOGSCALED,
@@ -318,6 +360,8 @@ gnome_canvas_waveview_init (GnomeCanvasWaveView *waveview)
        waveview->gain_src = NULL;
        waveview->rectified = FALSE;
        waveview->logscaled = FALSE;
+       waveview->filled = TRUE;
+       waveview->zero_line = FALSE;
        waveview->region_start = 0;
        waveview->samples_per_unit = 1.0;
        waveview->amplitude_above_axis = 1.0;
@@ -325,7 +369,10 @@ gnome_canvas_waveview_init (GnomeCanvasWaveView *waveview)
        waveview->screen_width = gdk_screen_width ();
        waveview->reload_cache_in_render = FALSE;
 
-       waveview->wave_color = RGBA_TO_UINT(44,35,126,255);
+       waveview->wave_color = 0;
+       waveview->clip_color = 0;
+       waveview->zero_color = 0;
+       waveview->fill_color = 0;
 }
 
 static void
@@ -345,7 +392,8 @@ gnome_canvas_waveview_destroy (GtkObject *object)
 #define DEBUG_CACHE 0
 #undef CACHE_MEMMOVE_OPTIMIZATION
 
-static gint32
+/** @return cache index of start_sample within the cache */
+static guint32
 gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview, gulong start_sample, gulong end_sample)
 {
        gulong required_cache_entries;
@@ -391,7 +439,7 @@ gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview, gulong start_
        /* Note the assumption that we have a 1:1 units:pixel ratio for the canvas. Its everywhere ... */
        
        half_width = (gulong) floor ((waveview->screen_width * waveview->samples_per_unit)/2.0 + 0.5);
-       
+
        if (start_sample < half_width) {
                new_cache_start = 0;
        } else {
@@ -617,9 +665,9 @@ gnome_canvas_waveview_ensure_cache (GnomeCanvasWaveView *waveview, gulong start_
   out:
 #if DEBUG_CACHE
        fprintf (stderr, "return cache index = %d\n", 
-                (gint32) floor ((((double) (start_sample - cache->start)) / waveview->samples_per_unit) + 0.5));
+                (guint32) floor ((((double) (start_sample - cache->start)) / waveview->samples_per_unit) + 0.5));
 #endif
-       return (gint32) floor ((((double) (start_sample - cache->start)) / waveview->samples_per_unit) + 0.5);
+       return (guint32) floor ((((double) (start_sample - cache->start)) / waveview->samples_per_unit) + 0.5);
 
 }
 
@@ -690,6 +738,8 @@ gnome_canvas_waveview_set_property (GObject      *object,
                                    GParamSpec   *pspec)
 
 {
+       (void) pspec;
+       
        GnomeCanvasItem *item;
        GnomeCanvasWaveView *waveview;
        int redraw = FALSE;
@@ -716,6 +766,7 @@ gnome_canvas_waveview_set_property (GObject      *object,
                waveview->length_function = g_value_get_pointer(value);
                redraw = TRUE;
                break;
+
        case PROP_SOURCEFILE_LENGTH_FUNCTION:
                waveview->sourcefile_length_function = g_value_get_pointer(value);
                redraw = TRUE;
@@ -797,12 +848,48 @@ gnome_canvas_waveview_set_property (GObject      *object,
                }
                break;
 
+       case PROP_CLIP_COLOR:
+               if (waveview->clip_color != g_value_get_uint(value)) {
+                       waveview->clip_color = g_value_get_uint(value);
+                       redraw = TRUE;
+               }
+               break;
+
+       case PROP_ZERO_COLOR:
+               if (waveview->zero_color != g_value_get_uint(value)) {
+                       waveview->zero_color = g_value_get_uint(value);
+                       redraw = TRUE;
+               }
+               break;
+
+       case PROP_FILL_COLOR:
+               if (waveview->fill_color != g_value_get_uint(value)) {
+                       waveview->fill_color = g_value_get_uint(value);
+                       redraw = TRUE;
+               }
+               break;
+
+       case PROP_FILLED:
+               if (waveview->filled != g_value_get_boolean(value)) {
+                       waveview->filled = g_value_get_boolean(value);
+                       redraw = TRUE;
+               }
+               break;
+
        case PROP_RECTIFIED:
                if (waveview->rectified != g_value_get_boolean(value)) {
                        waveview->rectified = g_value_get_boolean(value);
                        redraw = TRUE;
                }
                break;
+
+       case PROP_ZERO_LINE:
+               if (waveview->zero_line != g_value_get_boolean(value)) {
+                       waveview->zero_line = g_value_get_boolean(value);
+                       redraw = TRUE;
+               }
+               break;
+
        case PROP_LOGSCALED:
                if (waveview->logscaled != g_value_get_boolean(value)) {
                        waveview->logscaled = g_value_get_boolean(value);
@@ -836,61 +923,62 @@ gnome_canvas_waveview_set_property (GObject      *object,
 }
 
 static void
-gnome_canvas_waveview_get_property (GObject      *object,
-                                   guint         prop_id,
-                                   GValue       *value,
-                                   GParamSpec   *pspec)
+gnome_canvas_waveview_get_property (
+               GObject      *object,
+               guint         prop_id,
+               GValue       *value,
+               GParamSpec   *pspec)
 {
        
    
        g_return_if_fail (object != NULL);
-        g_return_if_fail (GNOME_IS_CANVAS_WAVEVIEW (object));
+       g_return_if_fail (GNOME_IS_CANVAS_WAVEVIEW (object));
 
        GnomeCanvasWaveView *waveview = GNOME_CANVAS_WAVEVIEW (object);
 
        switch (prop_id) {
        case PROP_DATA_SRC:
-               g_value_set_pointer(value, waveview->data_src);
+               g_value_set_pointer(value, waveview->data_src);
                break;
 
        case PROP_CHANNEL:
-               g_value_set_uint(value, waveview->channel);
+               g_value_set_uint(value, waveview->channel);
                break;
 
        case PROP_LENGTH_FUNCTION:
-               g_value_set_pointer(value, waveview->length_function);
+               g_value_set_pointer(value, waveview->length_function);
                break;
 
        case PROP_SOURCEFILE_LENGTH_FUNCTION:
-               g_value_set_pointer(value, waveview->sourcefile_length_function);
+               g_value_set_pointer(value, waveview->sourcefile_length_function);
                break;
 
        case PROP_PEAK_FUNCTION:
-               g_value_set_pointer(value, waveview->peak_function);
+               g_value_set_pointer(value, waveview->peak_function);
                break;
 
        case PROP_GAIN_FUNCTION:
-               g_value_set_pointer(value, waveview->gain_curve_function);
+               g_value_set_pointer(value, waveview->gain_curve_function);
                break;
 
        case PROP_GAIN_SRC:
-               g_value_set_pointer(value, waveview->gain_src);
+               g_value_set_pointer(value, waveview->gain_src);
                break;
 
        case PROP_CACHE:
-               g_value_set_pointer(value, waveview->cache);
+               g_value_set_pointer(value, waveview->cache);
                break;
 
        case PROP_CACHE_UPDATER:
-               g_value_set_boolean(value, waveview->cache_updater);
+               g_value_set_boolean(value, waveview->cache_updater);
                break;
 
        case PROP_SAMPLES_PER_UNIT:
-               g_value_set_double(value, waveview->samples_per_unit);
+               g_value_set_double(value, waveview->samples_per_unit);
                break;
 
        case PROP_AMPLITUDE_ABOVE_AXIS:
-               g_value_set_double(value, waveview->amplitude_above_axis);
+               g_value_set_double(value, waveview->amplitude_above_axis);
                break;
 
        case PROP_X:
@@ -909,10 +997,30 @@ gnome_canvas_waveview_get_property (GObject      *object,
                g_value_set_uint (value, waveview->wave_color);
                break;
 
+       case PROP_CLIP_COLOR:
+               g_value_set_uint (value, waveview->clip_color);
+               break;
+
+       case PROP_ZERO_COLOR:
+               g_value_set_uint (value, waveview->zero_color);
+               break;
+
+       case PROP_FILL_COLOR:
+               g_value_set_uint (value, waveview->fill_color);
+               break;
+
+       case PROP_FILLED:
+               g_value_set_boolean (value, waveview->filled);
+               break;
+
        case PROP_RECTIFIED:
                g_value_set_boolean (value, waveview->rectified);
                break;
 
+       case PROP_ZERO_LINE:
+               g_value_set_boolean (value, waveview->zero_line);
+               break;
+
        case PROP_LOGSCALED:
                g_value_set_boolean (value, waveview->logscaled);
                break;
@@ -969,9 +1077,13 @@ gnome_canvas_waveview_update (GnomeCanvasItem *item, double *affine, ArtSVP *cli
 
        UINT_TO_RGBA (waveview->wave_color, &waveview->wave_r, &waveview->wave_g, &waveview->wave_b,
                      &waveview->wave_a);
+       UINT_TO_RGBA (waveview->clip_color, &waveview->clip_r, &waveview->clip_g, &waveview->clip_b,
+                     &waveview->clip_a);
+       UINT_TO_RGBA (waveview->fill_color, &waveview->fill_r, &waveview->fill_g, &waveview->fill_b,
+                     &waveview->fill_a);
 
 //     check_cache (waveview, "end of update");
-}
+}                                 
 
 static void
 gnome_canvas_waveview_render (GnomeCanvasItem *item,
@@ -981,9 +1093,10 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
        gulong s1, s2;
        int clip_length = 0;
        int pymin, pymax;
-       int cache_index;
+       guint cache_index;
        double half_height;
-       int x, end, begin;
+       int x;
+       char rectify;
 
        waveview = GNOME_CANVAS_WAVEVIEW (item);
 
@@ -998,19 +1111,29 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
                buf->is_bg = FALSE;
        }
 
-       begin = MAX(waveview->bbox_ulx,buf->rect.x0);
+       /* a "unit" means a pixel */
 
-       if (waveview->bbox_lrx >= 0) {
-               end = MIN(waveview->bbox_lrx,buf->rect.x1);
-       } else {
-               end = buf->rect.x1;
-       }
+       /* begin: render start x (units) */
+       int const begin = MAX (waveview->bbox_ulx, buf->rect.x0);
+
+        /* zbegin: start x for zero line (units) */
+       int const zbegin = (begin == waveview->bbox_ulx) ? (begin + 1) : begin;
+
+       /* end: render end x (units) */
+       int const end = (waveview->bbox_lrx >= 0) ? MIN (waveview->bbox_lrx,buf->rect.x1) : buf->rect.x1;
+
+       /* zend: end x for zero-line (units) */
+       int const zend = (end == waveview->bbox_lrx) ? (end - 1) : end;
 
        if (begin == end) {
                return;
        }
 
-       s1 = floor ((begin - waveview->bbox_ulx) * waveview->samples_per_unit) ;
+       /* s1: start sample
+          s2: end sample
+       */
+
+       s1 = floor ((begin - waveview->bbox_ulx) * waveview->samples_per_unit);
 
        // fprintf (stderr, "0x%x begins at sample %f\n", waveview, waveview->bbox_ulx * waveview->samples_per_unit);
 
@@ -1051,10 +1174,18 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
                waveview->reload_cache_in_render = FALSE;
        }
 
-       cache_index = gnome_canvas_waveview_ensure_cache (waveview, s1, s2);
-
 //     check_cache (waveview, "post-ensure");
 
+       /* don't rectify at single-sample zoom */
+       if (waveview->rectified && waveview->samples_per_unit > 1) {
+               rectify = TRUE;
+       }
+       else {
+               rectify = FALSE;
+       }
+
+       clip_length = MIN(5,(waveview->height/4));
+
        /* 
           Now draw each line, clipping it appropriately. The clipping
           is done by the macros PAINT_FOO().
@@ -1063,103 +1194,648 @@ gnome_canvas_waveview_render (GnomeCanvasItem *item,
        half_height = waveview->half_height;
 
 /* this makes it slightly easier to comprehend whats going on */
-
 #define origin half_height
 
-       for (x = begin; x < end; x++) {
-
+       if (waveview->filled && !rectify) {
+               int prev_pymin = 1;
+               int prev_pymax = 0;
+               int last_pymin = 1;
+               int last_pymax = 0;
+               int next_pymin, next_pymax;
                double max, min;
-               int clip_max, clip_min;
-               
-               clip_max = 0;
-               clip_min = 0;
+               int next_clip_max = 0;
+               int next_clip_min = 0;
 
+               if (s1 < waveview->samples_per_unit) {
+                       /* we haven't got a prev vars to compare with, so outline the whole line here */
+                       prev_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+                       prev_pymin = prev_pymax;
+               }
+               else {
+                       s1 -= waveview->samples_per_unit;
+               }
+
+               if(end == waveview->bbox_lrx) {
+                       /* we don't have the NEXT vars for the last sample */
+                       last_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+                       last_pymin = last_pymax;
+               }
+               else {
+                       s2 += waveview->samples_per_unit;
+               }
+
+               cache_index = gnome_canvas_waveview_ensure_cache (waveview, s1, s2);
+
+               /*
+                * Compute the variables outside the rendering rect
+                */
+               if(prev_pymax != prev_pymin) {
+
+                       prev_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[cache_index].max, 1.0) * half_height) * item->canvas->pixels_per_unit);
+                       prev_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[cache_index].min, -1.0) * half_height) * item->canvas->pixels_per_unit);
+                       ++cache_index;
+               }
+               if(last_pymax != last_pymin) {
+                       /* take the index of one sample right of what we render */
+                       guint index = cache_index + (end - begin);
+
+                       if (index >= waveview->cache->data_size) {
+                               
+                               /* the data we want is off the end of the cache, which must mean its beyond
+                                  the end of the region's source; hence the peak values are 0 */
+                               last_pymax = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+                               last_pymin = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+                               
+                       } else {
+                               
+                               last_pymax = (int) rint ((item->y1 + origin - MIN(waveview->cache->data[index].max, 1.0) * half_height) * item->canvas->pixels_per_unit);
+                               last_pymin = (int) rint ((item->y1 + origin - MAX(waveview->cache->data[index].min, -1.0) * half_height) * item->canvas->pixels_per_unit);
+                               
+                       }
+                               
+               }
+
+               /* 
+                * initialize NEXT* variables for the first run, duplicated in the loop for speed
+                */
                max = waveview->cache->data[cache_index].max;
                min = waveview->cache->data[cache_index].min;
                
                if (max >= 1.0) {
                        max = 1.0;
-                       clip_max = 1;
+                       next_clip_max = 1;
                }
-
+               
                if (min <= -1.0) {
                        min = -1.0;
-                       clip_min = 1;
+                       next_clip_min = 1;
                }
+               
+               max *= half_height;
+               min *= half_height;
+               
+               next_pymax = (int) rint ((item->y1 + origin - max) * item->canvas->pixels_per_unit);
+               next_pymin = (int) rint ((item->y1 + origin - min) * item->canvas->pixels_per_unit);
+
+               /*
+                * And now the loop
+                */
+               for(x = begin; x < end; ++x) {
+                       int clip_max = next_clip_max;
+                       int clip_min = next_clip_min;
+                       int fill_max, fill_min;
+
+                       pymax = next_pymax;
+                       pymin = next_pymin;
+
+                       /* compute next */
+                       if(x == end - 1) {
+                               /*next is now the last column, which is outside the rendering rect, and possibly outside the region*/
+                               next_pymax = last_pymax;
+                               next_pymin = last_pymin;
+                       }
+                       else {
+                               ++cache_index;
+
+                               if (cache_index < waveview->cache->data_size) {
+                                       max = waveview->cache->data[cache_index].max;
+                                       min = waveview->cache->data[cache_index].min;
+                               } else {
+                                       max = min = 0;
+                               }
+
+                               next_clip_max = 0;
+                               next_clip_min = 0;
+
+                               if (max >= 1.0) {
+                                       max = 1.0;
+                                       next_clip_max = 1;
+                               }
+                               
+                               if (min <= -1.0) {
+                                       min = -1.0;
+                                       next_clip_min = 1;
+                               }
+
+                               max *= half_height;
+                               min *= half_height;
+                               
+                               next_pymax = (int) rint ((item->y1 + origin - max) * item->canvas->pixels_per_unit);
+                               next_pymin = (int) rint ((item->y1 + origin - min) * item->canvas->pixels_per_unit);
+                       }
+                       
+                       /* render */
+                       if (pymax == pymin) {
+                               PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymin);
+                       } else {
+                               if((prev_pymax < pymax && next_pymax < pymax) ||
+                                  (prev_pymax == pymax && next_pymax == pymax)) {
+                                       fill_max = pymax + 1;
+                                       PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax);
+                               }
+                               else {
+                                       fill_max = MAX(prev_pymax, next_pymax);
+                                       if(pymax == fill_max) {
+                                               PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax);
+                                               ++fill_max;
+                                       }
+                                       else {
+                                               PAINT_VERTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax, fill_max);
+                                       }
+                               }
+
+                               if((prev_pymin > pymin && next_pymin > pymin) ||
+                                  (prev_pymin == pymin && next_pymin == pymin)) {
+                                       fill_min = pymin - 1;
+                                       PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymin-1);
+                               }
+                               else {
+                                       fill_min = MIN(prev_pymin, next_pymin);
+                                       if(pymin == fill_min) {
+                                               PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymin);
+                                       }
+                                       else {
+                                               PAINT_VERTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, fill_min, pymin);
+                                       }
+                               }
+
+                               if(fill_max < fill_min) {
+                                       PAINT_VERTA(buf, waveview->fill_r, waveview->fill_g, waveview->fill_b, waveview->fill_a, x, fill_max, fill_min);
+                               }
+                               else if(fill_max == fill_min) {
+                                       PAINT_DOTA(buf, waveview->fill_r, waveview->fill_g, waveview->fill_b, waveview->fill_a, x, fill_max);
+                               }
+                       }
 
-               /* don't rectify at single-sample zoom */
+                       if (clip_max) {
+                               PAINT_VERTA(buf, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a, x, pymax, pymax+clip_length);
+                       }
+                       
+                       if (clip_min) {
+                               PAINT_VERTA(buf, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a, x, pymin-clip_length, pymin);
+                       }
 
-               if (waveview->rectified && waveview->samples_per_unit > 1) {
+                       prev_pymax = pymax;
+                       prev_pymin = pymin;
+               }
 
-                       if (fabs (min) > fabs (max)) {
-                               max = fabs (min);
-                       } 
+       } else if (waveview->filled && rectify) {
 
-                       max = max * waveview->height;
+               int prev_pymax = -1;
+               int last_pymax = -1;
+               int next_pymax;
+               double max, min;
+               int next_clip_max = 0;
+               int next_clip_min = 0;
 
-                       pymax = (int) rint ((item->y1 + waveview->height - max) * item->canvas->pixels_per_unit);
-                       pymin = (int) rint ((item->y1 + waveview->height) * item->canvas->pixels_per_unit);
+               // for rectified, this stays constant throughout the loop
+               pymin = (int) rint ((item->y1 + waveview->height) * item->canvas->pixels_per_unit);
 
-               } else {
-                       
-                       max = max * half_height;
-                       min = min * half_height;
-                       
-                       pymax = (int) rint ((item->y1 + origin - max) * item->canvas->pixels_per_unit);
-                       pymin = (int) rint ((item->y1 + origin - min) * item->canvas->pixels_per_unit);
+               if(s1 < waveview->samples_per_unit) {
+                       /* we haven't got a prev vars to compare with, so outline the whole line here */
+                       prev_pymax = pymin;
+               }
+               else {
+                       s1 -= waveview->samples_per_unit;
                }
 
-               /* OK, now fill the RGB buffer at x=i with a line between pymin and pymax,
-                  or, if samples_per_unit == 1, then a dot at each location.
-               */
+               if(end == waveview->bbox_lrx) {
+                       /* we don't have the NEXT vars for the last sample */
+                       last_pymax = pymin;
+               }
+               else {
+                       s2 += waveview->samples_per_unit;
+               }
 
-               if (pymax == pymin) {
-                       PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymin);
-               } else {
-                       PAINT_VERTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax, pymin);
+               cache_index = gnome_canvas_waveview_ensure_cache (waveview, s1, s2);
+
+               /*
+                * Compute the variables outside the rendering rect
+                */
+               if(prev_pymax < 0) {
+                       max = MIN(waveview->cache->data[cache_index].max, 1.0);
+                       min = MAX(waveview->cache->data[cache_index].min, -1.0);
+
+                       if (fabs (min) > fabs (max)) {
+                               max = fabs (min);
+                       }
+
+                       prev_pymax = (int) rint ((item->y1 + waveview->height - max * waveview->height) * item->canvas->pixels_per_unit);
+                       ++cache_index;
                }
-               
-               /* show clipped waveforms with small red lines */
+               if(last_pymax < 0) {
+                       /* take the index of one sample right of what we render */
+                       int index = cache_index + (end - begin);
+                       
+                       max = MIN(waveview->cache->data[index].max, 1.0);
+                       min = MAX(waveview->cache->data[index].min, -1.0);
 
-               if (clip_max || clip_min) {
-                       clip_length = MIN(5,(waveview->height/4));
+                       if (fabs (min) > fabs (max)) {
+                               max = fabs (min);
+                       }
+
+                       last_pymax = (int) rint ((item->y1 + waveview->height - max * waveview->height) * item->canvas->pixels_per_unit);
                }
 
-               if (clip_max) {
-                       PAINT_VERT(buf, 255, 0, 0, x, pymax, pymax+clip_length);
+               /* 
+                * initialize NEXT* variables for the first run, duplicated in the loop for speed
+                */
+               max = waveview->cache->data[cache_index].max;
+               min = waveview->cache->data[cache_index].min;
+               
+               if (max >= 1.0) {
+                       max = 1.0;
+                       next_clip_max = 1;
+               }
+               
+               if (min <= -1.0) {
+                       min = -1.0;
+                       next_clip_min = 1;
                }
+               
+               if (fabs (min) > fabs (max)) {
+                       max = fabs (min);
+               } 
+               
+               next_pymax = (int) rint ((item->y1 + waveview->height - max * waveview->height) * item->canvas->pixels_per_unit);
+
+               /*
+                * And now the loop
+                */
+               for(x = begin; x < end; ++x) {
+                       int clip_max = next_clip_max;
+                       int clip_min = next_clip_min;
+                       int fill_max;
+
+                       pymax = next_pymax;
+
+                       /* compute next */
+                       if(x == end - 1) {
+                               /*next is now the last column, which is outside the rendering rect, and possibly outside the region*/
+                               next_pymax = last_pymax;
+                       }
+                       else {
+                               ++cache_index;
 
-               if (clip_min) {
-                       PAINT_VERT(buf, 255, 0, 0, x, pymin-clip_length, pymin);
+                               max = waveview->cache->data[cache_index].max;
+                               min = waveview->cache->data[cache_index].min;
+                               
+                               if (max >= 1.0) {
+                                       max = 1.0;
+                                       next_clip_max = 1;
+                               }
+                               
+                               if (min <= -1.0) {
+                                       min = -1.0;
+                                       next_clip_min = 1;
+                               }
+                               
+                               if (fabs (min) > fabs (max)) {
+                                       max = fabs (min);
+                               } 
+                               
+                               next_pymax = (int) rint ((item->y1 + waveview->height - max * waveview->height) * item->canvas->pixels_per_unit);
+                       }
+                       
+                       /* render */
+                       if (pymax == pymin) {
+                               PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymin);
+                       } else {
+                               if((prev_pymax < pymax && next_pymax < pymax) ||
+                                  (prev_pymax == pymax && next_pymax == pymax)) {
+                                       fill_max = pymax + 1;
+                                       PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax);
+                               }
+                               else {
+                                       fill_max = MAX(prev_pymax, next_pymax);
+                                       if(pymax == fill_max) {
+                                               PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax);
+                                               ++fill_max;
+                                       }
+                                       else {
+                                               PAINT_VERTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax, fill_max);
+                                       }
+                               }
+
+                               if(fill_max < pymin) {
+                                       PAINT_VERTA(buf, waveview->fill_r, waveview->fill_g, waveview->fill_b, waveview->fill_a, x, fill_max, pymin);
+                               }
+                               else if(fill_max == pymin) {
+                                       PAINT_DOTA(buf, waveview->fill_r, waveview->fill_g, waveview->fill_b, waveview->fill_a, x, pymin);
+                               }
+                       }
+
+                       if (clip_max) {
+                               PAINT_VERTA(buf, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a, x, pymax, pymax+clip_length);
+                       }
+                       
+                       if (clip_min) {
+                               PAINT_VERTA(buf, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a, x, pymin-clip_length, pymin);
+                       }
+
+                       prev_pymax = pymax;
                }
+       }
+       else {
+               cache_index = gnome_canvas_waveview_ensure_cache (waveview, s1, s2);
+
+               for (x = begin; x < end; x++) {
+                       
+                       double max, min;
+                       int clip_max, clip_min;
+                       
+                       clip_max = 0;
+                       clip_min = 0;
+                       
+                       max = waveview->cache->data[cache_index].max;
+                       min = waveview->cache->data[cache_index].min;
+                       
+                       if (max >= 1.0) {
+                               max = 1.0;
+                               clip_max = 1;
+                       }
+                       
+                       if (min <= -1.0) {
+                               min = -1.0;
+                               clip_min = 1;
+                       }
+                       
+                       if (rectify) {
+                               
+                               if (fabs (min) > fabs (max)) {
+                                       max = fabs (min);
+                               } 
+                               
+                               max = max * waveview->height;
+                               
+                               pymax = (int) rint ((item->y1 + waveview->height - max) * item->canvas->pixels_per_unit);
+                               pymin = (int) rint ((item->y1 + waveview->height) * item->canvas->pixels_per_unit);
+                               
+                       } else {
+                               
+                               max = max * half_height;
+                               min = min * half_height;
+                               
+                               pymax = (int) rint ((item->y1 + origin - max) * item->canvas->pixels_per_unit);
+                               pymin = (int) rint ((item->y1 + origin - min) * item->canvas->pixels_per_unit);
+                       }
+                       
+                       /* OK, now fill the RGB buffer at x=i with a line between pymin and pymax,
+                          or, if samples_per_unit == 1, then a dot at each location.
+                       */
+                       
+                       if (pymax == pymin) {
+                               PAINT_DOTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymin);
+                       } else {
+                               PAINT_VERTA(buf, waveview->wave_r, waveview->wave_g, waveview->wave_b, waveview->wave_a, x, pymax, pymin);
+                       }
+                       
+                       /* show clipped waveforms with small red lines */
+                       
+                       if (clip_max) {
+                               PAINT_VERTA(buf, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a, x, pymax, pymax+clip_length);
+                       }
+                       
+                       if (clip_min) {
+                               PAINT_VERTA(buf, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a, x, pymin-clip_length, pymin);
+                       }
 
-               /* presto, we're done */
+                       /* presto, we're done */
                
-               cache_index++;
+                       cache_index++;
+               }
        }
 
+       if (!waveview->rectified && waveview->zero_line) {
+               // Paint zeroline.
+               //PAINT_HORIZA(buf, waveview->zero_r, waveview->zero_g, waveview->zero_b, waveview->zero_a, begin, endi-1, origin );
+               
+               unsigned char zero_r, zero_g, zero_b, zero_a;
+               UINT_TO_RGBA( waveview->zero_color, &zero_r, &zero_g, &zero_b, &zero_a );
+               int zeroline_y = (int) rint ((item->y1 + origin) * item->canvas->pixels_per_unit);
+               PAINT_HORIZA(buf, zero_r, zero_g, zero_b, zero_a, zbegin, zend, zeroline_y);
+       }
 #undef origin
 
 }
 
 static void
 gnome_canvas_waveview_draw (GnomeCanvasItem *item,
-                         GdkDrawable *drawable,
-                         int x, int y,
-                         int width, int height)
+                           GdkDrawable *drawable,
+                           int x, int y,
+                           int width, int height)
 {
        GnomeCanvasWaveView *waveview;
+       cairo_t* cr;
+       gulong s1, s2;
+       int cache_index;
+       gboolean rectify;
+       double origin;
+       double clip_length;
+       double xoff;
+       double yoff = 0.0;
+       double ulx;
+       double uly;
+       double lrx;
+       double lry;
 
        waveview = GNOME_CANVAS_WAVEVIEW (item);
 
-       if (parent_class->draw) {
-               (* parent_class->draw) (item, drawable, x, y, width, height);
+       /* compute intersection of Drawable area and waveview,
+          in canvas coordinate space
+       */
+
+       if (x > waveview->bbox_ulx) {
+               ulx = x;
+       } else {
+               ulx = waveview->bbox_ulx;
+       }
+
+       if (y > waveview->bbox_uly) {
+               uly = y;
+       } else {
+               uly = waveview->bbox_uly;
+       }
+
+       if (x + width > waveview->bbox_lrx) {
+               lrx = waveview->bbox_lrx;
+       } else {
+               lrx = x + width;
+       }
+
+       if (y + height > waveview->bbox_lry) {
+               lry = waveview->bbox_lry;
+       } else {
+               lry = y + height;
+       }
+
+       /* figure out which samples we need for the resulting intersection */
+
+       s1 = floor ((ulx - waveview->bbox_ulx) * waveview->samples_per_unit) ;
+
+       if (lrx == waveview->bbox_lrx) {
+               /* This avoids minor rounding errors when we have the
+                  entire region visible.
+               */
+               s2 = waveview->samples;
+       } else {
+               s2 = s1 + floor ((lrx - ulx) * waveview->samples_per_unit);
+       }
+
+       /* translate back to buffer coordinate space */
+
+       ulx -= x;
+       uly -= y;
+       lrx -= x;
+       lry -= y;
+
+       /* don't rectify at single-sample zoom */
+       if(waveview->rectified && waveview->samples_per_unit > 1.0) {
+               rectify = TRUE;
+       } else {
+               rectify = FALSE;
+       }
+
+       clip_length = MIN(5,(waveview->height/4));
+
+       cr = gdk_cairo_create (drawable);
+       cairo_set_line_width (cr, 0.5);
+
+       origin = waveview->bbox_uly - y + waveview->half_height;
+
+       cairo_rectangle (cr, ulx, uly, lrx - ulx, lry - uly);
+       cairo_clip (cr);
+
+       if (waveview->cache_updater && waveview->reload_cache_in_render) {
+               waveview->cache->start = 0;
+               waveview->cache->end = 0;
+               waveview->reload_cache_in_render = FALSE;
+       }
+
+       cache_index = gnome_canvas_waveview_ensure_cache (waveview, s1, s2);
+
+#if 0
+       printf ("%p r (%d,%d)(%d,%d)[%d x %d] bbox (%d,%d)(%d,%d)[%d x %d]"
+               " draw (%.1f,%.1f)(%.1f,%.1f)[%.1f x %.1f] s= %lu..%lu\n",
+               waveview,
+               x, y, 
+               x + width,
+               y + height,
+               width,
+               height,
+               waveview->bbox_ulx,
+               waveview->bbox_uly,
+               waveview->bbox_lrx,
+               waveview->bbox_lry,
+               waveview->bbox_lrx - waveview->bbox_ulx,
+               waveview->bbox_lry - waveview->bbox_uly,
+               ulx, uly,
+               lrx, lry,
+               lrx - ulx,
+               lry - uly,
+               s1, s2);
+#endif
+
+       /* draw the top half */
+       
+       for (xoff = ulx; xoff < lrx; xoff++) {
+               double max, min;
+
+               max = waveview->cache->data[cache_index].max;
+               min = waveview->cache->data[cache_index].min;
+
+               if (min <= -1.0) {
+                       min = -1.0;
+               }
+               
+               if (max >= 1.0) {
+                       max = 1.0;
+               }
+               
+               if (rectify) {
+                       if (fabs (min) > fabs (max)) {
+                               max = fabs (min);
+                       } 
+               } 
+               
+               yoff = origin - (waveview->half_height * max) + 0.5;
+               
+               if (xoff == ulx) {
+                       /* first point */
+                       cairo_move_to (cr, xoff+0.5, yoff);
+               } else {
+                       cairo_line_to (cr, xoff+0.5, yoff);
+               }
+
+               cache_index++;
        }
 
-       fprintf (stderr, "please don't use the CanvasWaveView item in a non-aa Canvas\n");
-       abort ();
+       /* from the final top point, move out of the clip zone */
+       
+       cairo_line_to (cr, xoff + 10, yoff);
+       
+       /* now draw the bottom half */
+
+       for (--xoff, --cache_index; xoff >= ulx; --xoff) {
+               double min;
+
+               min = waveview->cache->data[cache_index].min;
+
+               if (min <= -1.0) {
+                       min = -1.0;
+               }
+
+               yoff = origin - (waveview->half_height * min) + 0.5;
+
+               cairo_line_to (cr, xoff+0.5, yoff);
+               cache_index--;
+       }
+
+       /* from the final lower point, move out of the clip zone */
+
+       cairo_line_to (cr, xoff - 10, yoff);
+       
+       /* close path to fill */
+
+       cairo_close_path (cr);
+
+       /* fill and stroke */
+
+       cairo_set_source_rgba (cr, 
+                              (waveview->fill_r/255.0), 
+                              (waveview->fill_g/255.0), 
+                              (waveview->fill_b/255.0), 
+                              (waveview->fill_a/255.0));
+       cairo_fill_preserve (cr);
+       cairo_set_source_rgba (cr, 
+                              (waveview->wave_r/255.0), 
+                              (waveview->wave_g/255.0), 
+                              (waveview->wave_b/255.0), 
+                              (waveview->wave_a/255.0));
+       cairo_stroke (cr);
+
+       cairo_destroy (cr);
 }
 
+#if 0
+               if (clip_max || clip_min) {
+                       cairo_set_source_rgba (cr, waveview->clip_r, waveview->clip_g, waveview->clip_b, waveview->clip_a);
+               }
+
+               if (clip_max) {
+                       cairo_move_to (cr, xoff, yoff1);
+                       cairo_line_to (cr, xoff, yoff1 + clip_length);
+                       cairo_stroke (cr);
+               }
+               
+               if (clip_min) {
+                       cairo_move_to (cr, xoff, yoff2);
+                       cairo_line_to (cr, xoff, yoff2 - clip_length);
+                       cairo_stroke (cr);
+               }
+               
+#endif
+
 static void
 gnome_canvas_waveview_bounds (GnomeCanvasItem *item, double *x1, double *y1, double *x2, double *y2)
 {
@@ -1187,6 +1863,13 @@ gnome_canvas_waveview_bounds (GnomeCanvasItem *item, double *x1, double *y1, dou
 static double
 gnome_canvas_waveview_point (GnomeCanvasItem *item, double x, double y, int cx, int cy, GnomeCanvasItem **actual_item)
 {
+       (void) item;
+       (void) x;
+       (void) y;
+       (void) cx;
+       (void) cy;
+       (void) actual_item;
+       
        /* XXX for now, point is never inside the wave 
        GnomeCanvasWaveView *waveview;
        double x1, y1, x2, y2;