Fix broken logic and possible NULL pointer dereference for Bundle XML.
[ardour.git] / libs / canvas / canvas / wave_view.h
index 4fd45cff5daca8bc544cab5e3aa683b81ed42da7..f26c3a5c104dfeb8c1ee6d31d973406b303943ad 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <boost/shared_ptr.hpp>
 #include <boost/shared_array.hpp>
+#include <boost/scoped_array.hpp>
 
 #include "pbd/properties.h"
 
@@ -27,6 +28,7 @@
 
 #include <glibmm/refptr.h>
 
+#include "canvas/visibility.h"
 #include "canvas/item.h"
 #include "canvas/fill.h"
 #include "canvas/outline.h"
@@ -43,15 +45,55 @@ class WaveViewTest;
        
 namespace ArdourCanvas {
 
-class WaveView : virtual public Item, public Outline, public Fill
+class LIBCANVAS_API WaveView : public Item
 {
 public:
+
         enum Shape { 
                Normal,
-               Rectified,
+               Rectified
         };
 
-       WaveView (Group *, boost::shared_ptr<ARDOUR::AudioRegion>);
+       struct CacheEntry {
+               int channel;
+               Coord height;
+               float amplitude;
+               Color fill_color;
+               framepos_t start;
+               framepos_t end;
+               Cairo::RefPtr<Cairo::ImageSurface> image;
+
+       CacheEntry(int chan, Coord hght, float amp, Color fcl, framepos_t strt, framepos_t ed, Cairo::RefPtr<Cairo::ImageSurface> img)  :
+               
+               channel (chan), height (hght), amplitude (amp), fill_color (fcl), 
+                       start (strt), end (ed), image (img) {} 
+       };
+
+       /* final ImageSurface rendered with colours */
+       Cairo::RefPtr<Cairo::ImageSurface> _image;
+       
+    /* Displays a single channel of waveform data for the given Region.
+
+       x = 0 in the waveview corresponds to the first waveform datum taken
+       from region->start() samples into the source data.
+
+       x = N in the waveview corresponds to the (N * spp)'th sample 
+       measured from region->start() into the source data.
+
+       when drawing, we will map the zeroth-pixel of the waveview
+       into a window. 
+
+       The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces
+       that cache sections of the display. This is filled on-demand and
+       never cleared until something explicitly marks the cache invalid
+       (such as a change in samples_per_pixel, the log scaling, rectified or
+       other view parameters).
+    */
+
+
+       WaveView (Canvas *, boost::shared_ptr<ARDOUR::AudioRegion>);
+       WaveView (Item*, boost::shared_ptr<ARDOUR::AudioRegion>);
+       ~WaveView ();
 
        void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
        void compute_bounding_box () const;
@@ -65,18 +107,17 @@ public:
         void set_outline_color (Color);
        
        void region_resized ();
+        void gain_changed ();
 
         void set_show_zero_line (bool);
         bool show_zero_line() const { return _show_zero; }
         void set_zero_color (Color);
         void set_clip_color (Color);
-        void set_amplitude (double);
         void set_logscaled (bool);
         void set_gradient_depth (double);
         double gradient_depth() const { return _gradient_depth; }
         void set_shape (Shape);
-        double amplitude() const { return _amplitude; }
-        
+
         /* currently missing because we don't need them (yet):
           set_shape_independent();
           set_logscaled_independent()
@@ -85,11 +126,18 @@ public:
         static void set_global_gradient_depth (double);
         static void set_global_logscaled (bool);
         static void set_global_shape (Shape);
+        static void set_global_show_waveform_clipping (bool);
     
         static double  global_gradient_depth()  { return _global_gradient_depth; }
         static bool    global_logscaled()  { return _global_logscaled; }
         static Shape   global_shape()  { return _global_shape; }
 
+        void set_amplitude_above_axis (double v);
+        double amplitude_above_axis () const { return _amplitude_above_axis; }
+
+       static void set_clip_level (double dB);
+       static PBD::Signal0<void> ClipLevelChanged;
+
 #ifdef CANVAS_COMPATIBILITY    
        void*& property_gain_src () {
                return _foo_void;
@@ -97,87 +145,55 @@ public:
        void*& property_gain_function () {
                return _foo_void;
        }
-       double& property_amplitude_above_axis () {
-               return _foo_double;
-       }
 private:
        void* _foo_void;
-       bool _foo_bool;
-       int _foo_int;
-       Color _foo_uint;
-       double _foo_double;
-#endif
-
-       class CacheEntry
-       {
-       public:
-               CacheEntry (WaveView const *, int, int);
-               ~CacheEntry ();
-
-               int start () const {
-                       return _start;
-               }
-
-               int end () const {
-                       return _end;
-               }
-
-               boost::shared_array<ARDOUR::PeakData> peaks () const {
-                       return _peaks;
-               }
-
-                Cairo::RefPtr<Cairo::ImageSurface> image();
-               void clear_image ();
 
-       private:
-               Coord position (Coord) const;
-               
-               WaveView const * _wave_view;
-               int _start;
-               int _end;
-               int _n_peaks;
-               boost::shared_array<ARDOUR::PeakData> _peaks;
-               Cairo::RefPtr<Cairo::ImageSurface> _image;
-       };
+#endif
 
-       friend class CacheEntry;
-       friend class ::WaveViewTest;
+        friend class ::WaveViewTest;
 
-       void invalidate_whole_cache ();
+       static std::map <boost::shared_ptr<ARDOUR::AudioSource>, std::vector <CacheEntry> > _image_cache;
+       void consolidate_image_cache () const;
        void invalidate_image_cache ();
 
        boost::shared_ptr<ARDOUR::AudioRegion> _region;
        int    _channel;
        double _samples_per_pixel;
        Coord  _height;
-       Color  _wave_color;
         bool   _show_zero;
         Color  _zero_color;
         Color  _clip_color;
         bool   _logscaled;
         Shape  _shape;
         double _gradient_depth;
-        double _amplitude;
         bool   _shape_independent;
         bool   _logscaled_independent;
         bool   _gradient_depth_independent;
+        double _amplitude_above_axis;
+       float  _region_amplitude;
 
        /** The `start' value to use for the region; we can't use the region's
         *  value as the crossfade editor needs to alter it.
         */
        ARDOUR::frameoffset_t _region_start;
-       
-       mutable std::list<CacheEntry*> _cache;
-       
-        PBD::ScopedConnection invalidation_connection;
+
+        PBD::ScopedConnectionList invalidation_connection;
 
         static double _global_gradient_depth;
         static bool   _global_logscaled;
         static Shape  _global_shape;
+        static bool   _global_show_waveform_clipping;
+       static double _clip_level;
 
         static PBD::Signal0<void> VisualPropertiesChanged;
 
         void handle_visual_property_change ();
+        void handle_clip_level_change ();
+
+       void get_image (Cairo::RefPtr<Cairo::ImageSurface>& image, framepos_t start, framepos_t end, double& image_offset) const;
+
+        ArdourCanvas::Coord y_extent (double, bool) const;
+       void draw_image (Cairo::RefPtr<Cairo::ImageSurface>&, ARDOUR::PeakData*, int) const;
 };
 
 }