307528c685507e03d8cb2e3d2fff2e1f109c7497
[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 #include <boost/scoped_array.hpp>
24
25 #include "pbd/properties.h"
26
27 #include "ardour/types.h"
28
29 #include <glibmm/refptr.h>
30
31 #include "canvas/visibility.h"
32 #include "canvas/item.h"
33 #include "canvas/fill.h"
34 #include "canvas/outline.h"
35
36 namespace ARDOUR {
37         class AudioRegion;
38 }
39
40 namespace Gdk {
41         class Pixbuf;
42 }
43
44 class WaveViewTest;
45         
46 namespace ArdourCanvas {
47
48 class LIBCANVAS_API WaveView : virtual public Item, public Outline, public Fill
49 {
50 public:
51         enum Shape { 
52                 Normal,
53                 Rectified,
54         };
55
56     /* Displays a single channel of waveform data for the given Region.
57
58        x = 0 in the waveview corresponds to the first waveform datum taken
59        from region->start() samples into the source data.
60
61        x = N in the waveview corresponds to the (N * spp)'th sample 
62        measured from region->start() into the source data.
63
64        when drawing, we will map the zeroth-pixel of the waveview
65        into a window. 
66
67        The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces
68        that cache sections of the display. This is filled on-demand and
69        never cleared until something explicitly marks the cache invalid
70        (such as a change in samples_per_pixel, the log scaling, rectified or
71        other view parameters).
72     */
73
74
75         WaveView (Group *, boost::shared_ptr<ARDOUR::AudioRegion>);
76        ~WaveView ();
77
78         void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
79         void compute_bounding_box () const;
80     
81         void set_samples_per_pixel (double);
82         void set_height (Distance);
83         void set_channel (int);
84         void set_region_start (ARDOUR::frameoffset_t);
85
86         void set_fill_color (Color);
87         void set_outline_color (Color);
88         
89         void region_resized ();
90         void gain_changed ();
91
92         void set_show_zero_line (bool);
93         bool show_zero_line() const { return _show_zero; }
94         void set_zero_color (Color);
95         void set_clip_color (Color);
96         void set_logscaled (bool);
97         void set_gradient_depth (double);
98         double gradient_depth() const { return _gradient_depth; }
99         void set_shape (Shape);
100
101         /* currently missing because we don't need them (yet):
102            set_shape_independent();
103            set_logscaled_independent()
104         */
105
106         static void set_global_gradient_depth (double);
107         static void set_global_logscaled (bool);
108         static void set_global_shape (Shape);
109         static void set_global_show_waveform_clipping (bool);
110     
111         static double  global_gradient_depth()  { return _global_gradient_depth; }
112         static bool    global_logscaled()  { return _global_logscaled; }
113         static Shape   global_shape()  { return _global_shape; }
114
115         void set_amplitude_above_axis (double v);
116         double amplitude_above_axis () const { return _amplitude_above_axis; }
117
118         static void set_clip_level (double dB);
119         static PBD::Signal0<void> ClipLevelChanged;
120
121 #ifdef CANVAS_COMPATIBILITY     
122         void*& property_gain_src () {
123                 return _foo_void;
124         }
125         void*& property_gain_function () {
126                 return _foo_void;
127         }
128 private:
129         void* _foo_void;
130
131 #endif
132
133         friend class ::WaveViewTest;
134
135         void invalidate_image ();
136
137         boost::shared_ptr<ARDOUR::AudioRegion> _region;
138         int    _channel;
139         double _samples_per_pixel;
140         Coord  _height;
141         bool   _show_zero;
142         Color  _zero_color;
143         Color  _clip_color;
144         bool   _logscaled;
145         Shape  _shape;
146         double _gradient_depth;
147         bool   _shape_independent;
148         bool   _logscaled_independent;
149         bool   _gradient_depth_independent;
150         double _amplitude_above_axis;
151
152         /** The `start' value to use for the region; we can't use the region's
153          *  value as the crossfade editor needs to alter it.
154          */
155         ARDOUR::frameoffset_t _region_start;
156     
157     
158         mutable ARDOUR::framepos_t _sample_start; 
159         mutable ARDOUR::framepos_t _sample_end; 
160         mutable Cairo::RefPtr<Cairo::ImageSurface> _image;
161
162         PBD::ScopedConnectionList invalidation_connection;
163
164         static double _global_gradient_depth;
165         static bool   _global_logscaled;
166         static Shape  _global_shape;
167         static bool   _global_show_waveform_clipping;
168         static double _clip_level;
169
170         static PBD::Signal0<void> VisualPropertiesChanged;
171
172         void handle_visual_property_change ();
173         void handle_clip_level_change ();
174
175         void ensure_cache (ARDOUR::framepos_t sample_start, ARDOUR::framepos_t sample_end) const;
176         ArdourCanvas::Coord position (double) const;
177         void draw_image (ARDOUR::PeakData*, int npeaks) const;
178 };
179
180 }