Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / canvas / wave_view.h
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <boost/shared_ptr.hpp>
22 #include <boost/shared_array.hpp>
23
24 #include "pbd/properties.h"
25
26 #include "ardour/types.h"
27
28 #include <glibmm/refptr.h>
29
30 #include "canvas/item.h"
31 #include "canvas/fill.h"
32 #include "canvas/outline.h"
33
34 namespace ARDOUR {
35         class AudioRegion;
36 }
37
38 namespace Gdk {
39         class Pixbuf;
40 }
41
42 class WaveViewTest;
43         
44 namespace ArdourCanvas {
45
46 class WaveView : virtual public Item, public Outline, public Fill
47 {
48 public:
49         enum Shape { 
50                 Normal,
51                 Rectified,
52         };
53
54     /* Displays a single channel of waveform data for the given Region.
55
56        x = 0 in the waveview corresponds to the first waveform datum taken
57        from region->start() samples into the source data.
58
59        x = N in the waveview corresponds to the (N * spp)'th sample 
60        measured from region->start() into the source data.
61
62        when drawing, we will map the zeroth-pixel of the waveview
63        into a window. 
64
65        The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces
66        that cache sections of the display. This is filled on-demand and
67        never cleared until something explicitly marks the cache invalid
68        (such as a change in samples_per_pixel, the log scaling, rectified or
69        other view parameters).
70     */
71
72
73         WaveView (Group *, boost::shared_ptr<ARDOUR::AudioRegion>);
74        ~WaveView ();
75
76         void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
77         void compute_bounding_box () const;
78     
79         void set_samples_per_pixel (double);
80         void set_height (Distance);
81         void set_channel (int);
82         void set_region_start (ARDOUR::frameoffset_t);
83
84         void set_fill_color (Color);
85         void set_outline_color (Color);
86         
87         void region_resized ();
88         void gain_changed ();
89
90         void set_show_zero_line (bool);
91         bool show_zero_line() const { return _show_zero; }
92         void set_zero_color (Color);
93         void set_clip_color (Color);
94         void set_logscaled (bool);
95         void set_gradient_depth (double);
96         double gradient_depth() const { return _gradient_depth; }
97         void set_shape (Shape);
98
99         
100         /* currently missing because we don't need them (yet):
101            set_shape_independent();
102            set_logscaled_independent()
103         */
104
105         static void set_global_gradient_depth (double);
106         static void set_global_logscaled (bool);
107         static void set_global_shape (Shape);
108     
109         static double  global_gradient_depth()  { return _global_gradient_depth; }
110         static bool    global_logscaled()  { return _global_logscaled; }
111         static Shape   global_shape()  { return _global_shape; }
112
113         void set_amplitude_above_axis (double v);
114         double amplitude_above_axis () const { return _amplitude_above_axis; }
115
116 #ifdef CANVAS_COMPATIBILITY     
117         void*& property_gain_src () {
118                 return _foo_void;
119         }
120         void*& property_gain_function () {
121                 return _foo_void;
122         }
123 private:
124         void* _foo_void;
125
126 #endif
127
128     /** A cached, pre-rendered image of some section of a waveform.
129         
130         It spans a range given relative to the start of the source
131         of the waveform data, so a range from N..M corresponds
132         to the sample range N..M within the source.
133         
134         Invalidated by a changes to:
135
136                samples_per_pixel
137                colors
138                height
139
140     */
141
142         class CacheEntry
143         {
144           public:
145                 CacheEntry (WaveView const *, double, double, ARDOUR::framepos_t, ARDOUR::framepos_t);
146                 ~CacheEntry ();
147
148                 double pixel_start () const {
149                         return _pixel_start;
150                 }
151
152                 double pixel_end () const {
153                         return _pixel_end;
154                 }
155
156                 double sample_start () const {
157                         return _sample_start;
158                 }
159
160                 double sample_end () const {
161                         return _sample_end;
162                 }
163
164                 boost::shared_array<ARDOUR::PeakData> peaks () const {
165                         return _peaks;
166                 }
167
168                 Cairo::RefPtr<Cairo::ImageSurface> image();
169                 void clear_image ();
170
171         private:
172                 Coord position (Coord) const;
173                 
174                 WaveView const * _wave_view;
175             
176                 double     _pixel_start;
177                 double     _pixel_end;
178                 ARDOUR::framecnt_t _sample_start; 
179                 ARDOUR::framecnt_t _sample_end; 
180                 int        _n_peaks; 
181
182                 boost::shared_array<ARDOUR::PeakData> _peaks;
183                 Cairo::RefPtr<Cairo::ImageSurface> _image;
184         };
185
186         friend class CacheEntry;
187         friend class ::WaveViewTest;
188
189         void invalidate_whole_cache ();
190         void invalidate_image_cache ();
191
192         boost::shared_ptr<ARDOUR::AudioRegion> _region;
193         int    _channel;
194         double _samples_per_pixel;
195         Coord  _height;
196         Color  _wave_color;
197         bool   _show_zero;
198         Color  _zero_color;
199         Color  _clip_color;
200         bool   _logscaled;
201         Shape  _shape;
202         double _gradient_depth;
203         bool   _shape_independent;
204         bool   _logscaled_independent;
205         bool   _gradient_depth_independent;
206         double _amplitude_above_axis;
207
208         /** The `start' value to use for the region; we can't use the region's
209          *  value as the crossfade editor needs to alter it.
210          */
211         ARDOUR::frameoffset_t _region_start;
212     
213         mutable CacheEntry* _cache;
214        
215         PBD::ScopedConnection invalidation_connection;
216
217         static double _global_gradient_depth;
218         static bool   _global_logscaled;
219         static Shape  _global_shape;
220
221         static PBD::Signal0<void> VisualPropertiesChanged;
222
223         void handle_visual_property_change ();
224     void ensure_cache (ARDOUR::framecnt_t pixel_start, ARDOUR::framecnt_t pixel_end,
225                        ARDOUR::framepos_t sample_start, ARDOUR::framepos_t sample_end) const;
226 };
227
228 }