merge with master
[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         WaveView (Group *, boost::shared_ptr<ARDOUR::AudioRegion>);
55
56         void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
57         void compute_bounding_box () const;
58     
59         void set_samples_per_pixel (double);
60         void set_height (Distance);
61         void set_channel (int);
62         void set_region_start (ARDOUR::frameoffset_t);
63
64         void set_fill_color (Color);
65         void set_outline_color (Color);
66         
67         void region_resized ();
68         void gain_changed ();
69
70         void set_show_zero_line (bool);
71         bool show_zero_line() const { return _show_zero; }
72         void set_zero_color (Color);
73         void set_clip_color (Color);
74         void set_logscaled (bool);
75         void set_gradient_depth (double);
76         double gradient_depth() const { return _gradient_depth; }
77         void set_shape (Shape);
78
79         
80         /* currently missing because we don't need them (yet):
81            set_shape_independent();
82            set_logscaled_independent()
83         */
84
85         static void set_global_gradient_depth (double);
86         static void set_global_logscaled (bool);
87         static void set_global_shape (Shape);
88     
89         static double  global_gradient_depth()  { return _global_gradient_depth; }
90         static bool    global_logscaled()  { return _global_logscaled; }
91         static Shape   global_shape()  { return _global_shape; }
92
93         void set_amplitude_above_axis (double v);
94         double amplitude_above_axis () const { return _amplitude_above_axis; }
95
96 #ifdef CANVAS_COMPATIBILITY     
97         void*& property_gain_src () {
98                 return _foo_void;
99         }
100         void*& property_gain_function () {
101                 return _foo_void;
102         }
103 private:
104         void* _foo_void;
105
106 #endif
107
108     /** A cached, pre-rendered image of some section of a waveform.
109         
110         It spans a range given relative to the start of the source
111         of the waveform data, so a range from N..M corresponds
112         to the sample range N..M within the source.
113         
114         Invalidated by a changes to:
115
116                samples_per_pixel
117                colors
118                height
119
120     */
121
122         class CacheEntry
123         {
124           public:
125                 CacheEntry (WaveView const *, double, double, int);
126                 ~CacheEntry ();
127
128                 double start () const {
129                         return _start;
130                 }
131
132                 double end () const {
133                         return _end;
134                 }
135
136                 boost::shared_array<ARDOUR::PeakData> peaks () const {
137                         return _peaks;
138                 }
139
140                 Cairo::RefPtr<Cairo::ImageSurface> image();
141                 void clear_image ();
142
143         private:
144                 Coord position (Coord) const;
145                 
146                 WaveView const * _wave_view;
147                 
148                 double _start;
149                 double _end;
150                 int _n_peaks; 
151
152                 boost::shared_array<ARDOUR::PeakData> _peaks;
153                 Cairo::RefPtr<Cairo::ImageSurface> _image;
154         };
155
156         friend class CacheEntry;
157         friend class ::WaveViewTest;
158
159         void invalidate_whole_cache ();
160         void invalidate_image_cache ();
161
162         boost::shared_ptr<ARDOUR::AudioRegion> _region;
163         int    _channel;
164         double _samples_per_pixel;
165         Coord  _height;
166         Color  _wave_color;
167         bool   _show_zero;
168         Color  _zero_color;
169         Color  _clip_color;
170         bool   _logscaled;
171         Shape  _shape;
172         double _gradient_depth;
173         bool   _shape_independent;
174         bool   _logscaled_independent;
175         bool   _gradient_depth_independent;
176         double _amplitude_above_axis;
177
178         /** The `start' value to use for the region; we can't use the region's
179          *  value as the crossfade editor needs to alter it.
180          */
181         ARDOUR::frameoffset_t _region_start;
182
183         mutable std::list<CacheEntry*> _cache;
184        
185         PBD::ScopedConnection invalidation_connection;
186
187         static double _global_gradient_depth;
188         static bool   _global_logscaled;
189         static Shape  _global_shape;
190
191         static PBD::Signal0<void> VisualPropertiesChanged;
192
193         void handle_visual_property_change ();
194 };
195
196 }