Fix 0006183 (waveview crash).
[ardour.git] / libs / canvas / wave_view.cc
index d67cf2c58f205b9ab140d7eb65ea6e43694ec84c..3521ef33cd20bc610a745ceb7196cf06c7baab64 100644 (file)
@@ -34,6 +34,7 @@
 #include "canvas/wave_view.h"
 #include "canvas/utils.h"
 #include "canvas/canvas.h"
+#include "canvas/colors.h"
 
 #include <gdkmm/general.h>
 
@@ -70,6 +71,7 @@ WaveView::WaveView (Canvas* c, boost::shared_ptr<ARDOUR::AudioRegion> region)
        , _gradient_depth_independent (false)
        , _amplitude_above_axis (1.0)
        , _region_amplitude (_region->scale_amplitude ())
+       , _start_shift (0.0)
        , _region_start (region->start())
 {
        VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
@@ -175,12 +177,6 @@ WaveView::set_samples_per_pixel (double samples_per_pixel)
        }
 }
 
-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)
 {
@@ -215,6 +211,21 @@ WaveView::invalidate_image_cache ()
        vector <uint32_t> deletion_list;
        vector <CacheEntry> caches;
 
+       /* The source may have disappeared in the case of rec regions.*/
+       if (_region->n_channels() == 0) {
+               std::map <boost::shared_ptr<ARDOUR::AudioSource>, std::vector <CacheEntry> >::iterator i;
+               for (i = _image_cache.begin(); i != _image_cache.end(); ++i) {
+                       if (i->first.unique()) {
+                               for (uint32_t n = 0; n < (*i).second.size (); ++n) {
+                                       (*i).second[n].image.clear ();
+                               }
+                               (*i).second.clear ();
+                               _image_cache.erase(i->first);
+                       }
+               }
+               return;
+       }
+
        if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
                caches = _image_cache.find (_region->audio_source ())->second;
        } else {
@@ -232,7 +243,6 @@ WaveView::invalidate_image_cache ()
                }
 
                deletion_list.push_back (i);
-
        }
 
        while (deletion_list.size() > 0) {
@@ -246,7 +256,6 @@ WaveView::invalidate_image_cache ()
        } else {
                _image_cache[_region->audio_source ()] = caches;
        }
-
 }
 
 void
@@ -353,6 +362,38 @@ WaveView::y_extent (double s, bool /*round_to_lower_edge*/) const
        }
 }
 
+void
+WaveView::draw_absent_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks) const
+{
+       Cairo::RefPtr<Cairo::ImageSurface> stripe = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
+
+       Cairo::RefPtr<Cairo::Context> stripe_context = Cairo::Context::create (stripe);
+       stripe_context->set_antialias (Cairo::ANTIALIAS_NONE);
+
+       uint32_t stripe_separation = 150;
+       double start = - floor (_height / stripe_separation) * stripe_separation;
+       int stripe_x = 0;
+
+       while (start < n_peaks) {
+
+               stripe_context->move_to (start, 0);
+               stripe_x = start + _height;
+               stripe_context->line_to (stripe_x, _height);
+               start += stripe_separation;
+       }
+
+       stripe_context->set_source_rgba (1.0, 1.0, 1.0, 1.0);
+       stripe_context->set_line_cap (Cairo::LINE_CAP_SQUARE);
+       stripe_context->set_line_width(50);
+       stripe_context->stroke();
+
+       Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
+
+       context->set_source_rgba (1.0, 1.0, 0.0, 0.3);
+       context->mask (stripe, 0, 0);
+       context->fill ();
+}
+
 struct LineTips {
        double top;
        double bot;
@@ -388,6 +429,10 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
        Cairo::RefPtr<Cairo::Context> outline_context = Cairo::Context::create (images.outline);
        Cairo::RefPtr<Cairo::Context> clip_context = Cairo::Context::create (images.clip);
        Cairo::RefPtr<Cairo::Context> zero_context = Cairo::Context::create (images.zero);
+       wave_context->set_antialias (Cairo::ANTIALIAS_NONE);
+       outline_context->set_antialias (Cairo::ANTIALIAS_NONE);
+       clip_context->set_antialias (Cairo::ANTIALIAS_NONE);
+       zero_context->set_antialias (Cairo::ANTIALIAS_NONE);
 
        boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
 
@@ -418,16 +463,17 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                                tips[i].top = y_extent (p, false);
                                tips[i].spread = p * (_height - 1.0);
 
-                               if (fabs (_peaks[i].max) >= clip_level) {
+                               if (_peaks[i].max >= clip_level) {
                                        tips[i].clip_max = true;
                                }
 
-                               if (fabs (_peaks[i].min) >= clip_level) {
-                                       tips[i].clip_max = true;
+                               if (-(_peaks[i].min) >= clip_level) {
+                                       tips[i].clip_min = true;
                                }
                        }
 
-               } else {for (int i = 0; i < n_peaks; ++i) {
+               } else {
+                       for (int i = 0; i < n_peaks; ++i) {
 
                                tips[i].bot = height() - 1.0;
                                const double p = max(fabs (_peaks[i].max), fabs (_peaks[i].min));
@@ -447,22 +493,11 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                                double top = _peaks[i].max;
                                double bot = _peaks[i].min;
 
-                               if (_peaks[i].max > 0 && _peaks[i].min > 0) {
-                                       if (fabs (_peaks[i].max) >= clip_level) {
+                               if (_peaks[i].max >= clip_level) {
                                                tips[i].clip_max = true;
-                                       }
                                }
-                               else if (_peaks[i].max < 0 && _peaks[i].min < 0) {
-                                       if (fabs (_peaks[i].min) >= clip_level) {
-                                               tips[i].clip_min = true;
-                                       }
-                               } else {
-                                       if (fabs (_peaks[i].max) >= clip_level) {
-                                               tips[i].clip_max = true;
-                                       }
-                                       if (fabs (_peaks[i].min) >= clip_level) {
-                                               tips[i].clip_min = true;
-                                       }
+                               if (-(_peaks[i].min) >= clip_level) {
+                                       tips[i].clip_min = true;
                                }
 
                                if (top > 0.0) {
@@ -483,32 +518,21 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
 
                                tips[i].top = y_extent (top, false);
                                tips[i].bot = y_extent (bot, true);
-                               tips[i].spread = fabs (tips[i].top - tips[i].bot);
+                               tips[i].spread = tips[i].bot - tips[i].top;
                        }
 
                } else {
                        for (int i = 0; i < n_peaks; ++i) {
-                               if (_peaks[i].max > 0 && _peaks[i].min > 0) {
-                                       if (fabs (_peaks[i].max) >= clip_level) {
-                                               tips[i].clip_max = true;
-                                       }
+                               if (_peaks[i].max >= clip_level) {
+                                       tips[i].clip_max = true;
                                }
-                               else if (_peaks[i].max < 0 && _peaks[i].min < 0) {
-                                       if (fabs (_peaks[i].min) >= clip_level) {
-                                               tips[i].clip_min = true;
-                                       }
-                               } else {
-                                       if (fabs (_peaks[i].max) >= clip_level) {
-                                               tips[i].clip_max = true;
-                                       }
-                                       if (fabs (_peaks[i].min) >= clip_level) {
-                                               tips[i].clip_min = true;
-                                       }
+                               if (-(_peaks[i].min) >= clip_level) {
+                                       tips[i].clip_min = true;
                                }
 
                                tips[i].top = y_extent (_peaks[i].max, false);
                                tips[i].bot = y_extent (_peaks[i].min, true);
-                               tips[i].spread = fabs (tips[i].top - tips[i].bot);
+                               tips[i].spread = tips[i].bot - tips[i].top;
                        }
 
                }
@@ -525,7 +549,6 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
        wave_context->set_line_width (1.0);
        wave_context->translate (0.5, +0.5);
 
-       outline_context->set_line_cap (Cairo::LINE_CAP_ROUND);
        outline_context->set_line_width (1.0);
        outline_context->translate (0.5, +0.5);
 
@@ -540,6 +563,7 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
         */
 
        const double clip_height = min (7.0, ceil (_height * 0.05));
+       bool draw_outline_as_wave = false;
 
        /* There are 3 possible components to draw at each x-axis position: the
           waveform "line", the zero line and an outline/clip indicator.  We
@@ -603,14 +627,18 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                                wave_context->move_to (i, tips[i].top);
                                wave_context->line_to (i, tips[i].bot);
                        }
+                       /* draw square waves and other discontiguous points clearly */
                        if (i > 0) {
-                               if (tips[i-1].top + 2 < tips[i].bot) {
+                               if (tips[i-1].top + 2 < tips[i].top) {
                                        wave_context->move_to (i-1, tips[i-1].top);
-                                       wave_context->line_to (i, tips[i].bot);
-                               }
-                               else if (tips[i-1].bot > tips[i].top + 2) {
-                                       wave_context->move_to (i-1, tips[i-1].bot);
+                                       wave_context->line_to (i-1, (tips[i].bot + tips[i-1].top)/2);
+                                       wave_context->move_to (i, (tips[i].bot + tips[i-1].top)/2);
                                        wave_context->line_to (i, tips[i].top);
+                               } else if (tips[i-1].bot > tips[i].bot + 2) {
+                                       wave_context->move_to (i-1, tips[i-1].bot);
+                                       wave_context->line_to (i-1, (tips[i].top + tips[i-1].bot)/2);
+                                       wave_context->move_to (i, (tips[i].top + tips[i-1].bot)/2);
+                                       wave_context->line_to (i, tips[i].bot);
                                }
                        }
 
@@ -622,6 +650,7 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                        }
 
                        if (tips[i].spread > 1.0) {
+                               draw_outline_as_wave = false;
                                /* lower outline/clip indicator */
                                if (_global_show_waveform_clipping && tips[i].clip_min) {
                                        clip_context->move_to (i, tips[i].bot);
@@ -629,11 +658,12 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                                        const double sign = tips[i].bot > height_2 ? -1 : 1;
                                        clip_context->rel_line_to (0, sign * min (clip_height, ceil (tips[i].spread + .5)));
                                } else {
-                                       outline_context->move_to (i, tips[i].bot);
+                                       outline_context->move_to (i, tips[i].bot + 0.5);
                                        /* normal lower terminal dot */
-                                       outline_context->close_path ();
+                                       outline_context->rel_line_to (0, -0.5);
                                }
                        } else {
+                               draw_outline_as_wave = true;
                                if (tips[i].clip_min) {
                                        // make sure we draw the clip
                                        tips[i].clip_max = true;
@@ -647,9 +677,17 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                                const double sign = tips[i].top > height_2 ? -1 : 1;
                                clip_context->rel_line_to (0, sign * min(clip_height, ceil(tips[i].spread + .5)));
                        } else {
-                               outline_context->move_to (i, tips[i].top);
-                               /* normal upper terminal dot */
-                               outline_context->close_path ();
+                               if (draw_outline_as_wave) {
+                                       wave_context->move_to (i, tips[i].top + 0.5);
+                                       /* special case where outline only is drawn.
+                                          is this correct? too short by 0.5?
+                                       */
+                                       wave_context->rel_line_to (0, -0.5);
+                               } else {
+                                       outline_context->move_to (i, tips[i].top + 0.5);
+                                       /* normal upper terminal dot */
+                                       outline_context->rel_line_to (0, -0.5);
+                               }
                        }
                }
 
@@ -688,7 +726,7 @@ WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peak
                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 center = hsva_to_color (h, s, v, a);
                color_to_rgba (center, r, g, b, a);
 
                gradient->add_color_stop_rgba (stops[0], r, g, b, a);
@@ -768,14 +806,19 @@ WaveView::get_image (Cairo::RefPtr<Cairo::ImageSurface>& image, framepos_t start
 
        boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
 
-       _region->read_peaks (peaks.get(), n_peaks,
+       framecnt_t peaks_read;
+       peaks_read = _region->read_peaks (peaks.get(), n_peaks,
                             sample_start, sample_end - sample_start,
                             _channel,
                             _samples_per_pixel);
 
        image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, _height);
 
-       draw_image (image, peaks.get(), n_peaks);
+       if (peaks_read > 0) {
+               draw_image (image, peaks.get(), n_peaks);
+       } else {
+               draw_absent_image (image, peaks.get(), n_peaks);
+       }
 
        _image_cache[_region->audio_source ()].push_back (CacheEntry (_channel, _height, _region_amplitude, _fill_color, sample_start,  sample_end, image));
 
@@ -795,7 +838,7 @@ WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) cons
                return;
        }
 
-       Rect self = item_to_window (Rect (0.5, 0.0, _region->length() / _samples_per_pixel, _height));
+       Rect self = item_to_window (Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height));
        boost::optional<Rect> d = self.intersection (area);
 
        if (!d) {
@@ -808,8 +851,8 @@ WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) cons
         * window. We round down in case we were asked to
         * draw "between" pixels at the start and/or end.
         */
-
-       const double draw_start = floor (draw.x0);
+       
+       double draw_start = floor (draw.x0);
        const double draw_end = floor (draw.x1);
 
        // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
@@ -828,7 +871,7 @@ WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) cons
        /* 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;
 
        Cairo::RefPtr<Cairo::ImageSurface> image;
@@ -838,6 +881,16 @@ WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) cons
 
        // cerr << "Offset into image to place at zero: " << image_offset << endl;
 
+       if (_start_shift && (sample_start == _region_start) && (self.x0 == draw.x0)) {
+               /* we are going to draw the first pixel for this region, but 
+                  we may not want this to overlap a border around the
+                  waveform. If so, _start_shift will be set.
+               */
+               //cerr << name.substr (23) << " ss = " << sample_start << " rs = " << _region_start << " sf = " << _start_shift << " ds = " << draw_start << " self = " << self << " draw = " << draw << endl;
+               //draw_start += _start_shift;
+               //image_offset += _start_shift;
+       }
+       
        context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
 
        /* round image origin position to an exact pixel in device space to
@@ -1036,6 +1089,19 @@ WaveView::set_global_show_waveform_clipping (bool yn)
 {
        if (_global_show_waveform_clipping != yn) {
                _global_show_waveform_clipping = yn;
-               VisualPropertiesChanged (); /* EMIT SIGNAL */
+               ClipLevelChanged ();
        }
 }
+
+void
+WaveView::set_start_shift (double pixels)
+{
+       if (pixels < 0) {
+               return;
+       }
+
+       begin_visual_change ();
+       _start_shift = pixels;
+       end_visual_change ();
+}
+