Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / wave_view.cc
1 #include <cairomm/cairomm.h>
2
3 #include "gtkmm2ext/utils.h"
4
5 #include "pbd/compose.h"
6 #include "pbd/signals.h"
7
8 #include "ardour/types.h"
9 #include "ardour/audioregion.h"
10
11 #include "canvas/wave_view.h"
12 #include "canvas/utils.h"
13
14 #include <gdkmm/general.h>
15
16 using namespace std;
17 using namespace ARDOUR;
18 using namespace ArdourCanvas;
19
20 WaveView::WaveView (Group* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
21         : Item (parent)
22         , Outline (parent)
23         , Fill (parent)
24         , _region (region)
25         , _channel (0)
26         , _frames_per_pixel (0)
27         , _height (64)
28         , _wave_color (0xffffffff)
29         , _region_start (0)
30 {
31         
32 }
33
34 void
35 WaveView::set_frames_per_pixel (double frames_per_pixel)
36 {
37         begin_change ();
38         
39         _frames_per_pixel = frames_per_pixel;
40
41         _bounding_box_dirty = true;
42         end_change ();
43
44         invalidate_whole_cache ();
45 }
46
47 void
48 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
49 {
50         assert (_frames_per_pixel != 0);
51
52         if (!_region) {
53                 return;
54         }
55
56         /* p, start and end are offsets from the start of the source.
57            area is relative to the position of the region.
58          */
59         
60         int const start = rint (area.x0 + _region_start / _frames_per_pixel);
61         int const end   = rint (area.x1 + _region_start / _frames_per_pixel);
62
63         int p = start;
64         list<CacheEntry*>::iterator cache = _cache.begin ();
65
66         while (p < end) {
67
68                 /* Step through cache entries that end at or before our current position, p */
69                 while (cache != _cache.end() && (*cache)->end() <= p) {
70                         ++cache;
71                 }
72
73                 /* Now either:
74                    1. we have run out of cache entries
75                    2. the one we are looking at finishes after p but also starts after p.
76                    3. the one we are looking at finishes after p and starts before p.
77
78                    Set up a pointer to the cache entry that we will use on this iteration.
79                 */
80
81                 CacheEntry* render = 0;
82
83                 if (cache == _cache.end ()) {
84
85                         /* Case 1: we have run out of cache entries, so make a new one for
86                            the whole required area and put it in the list.
87                         */
88                         
89                         CacheEntry* c = new CacheEntry (this, p, end);
90                         _cache.push_back (c);
91                         render = c;
92
93                 } else if ((*cache)->start() > p) {
94
95                         /* Case 2: we have a cache entry, but it starts after p, so we
96                            need another one for the missing bit.
97                         */
98
99                         CacheEntry* c = new CacheEntry (this, p, (*cache)->start());
100                         cache = _cache.insert (cache, c);
101                         ++cache;
102                         render = c;
103
104                 } else {
105
106                         /* Case 3: we have a cache entry that will do at least some of what
107                            we have left, so render it.
108                         */
109
110                         render = *cache;
111                         ++cache;
112
113                 }
114
115                 int const this_end = min (end, render->end ());
116                 
117                 Coord const left  =        p - _region_start / _frames_per_pixel;
118                 Coord const right = this_end - _region_start / _frames_per_pixel;
119                 
120                 context->save ();
121                 
122                 context->rectangle (left, area.y0, right, area.height());
123                 context->clip ();
124                 
125                 context->translate (left, 0);
126
127                 Gdk::Cairo::set_source_pixbuf (context, render->pixbuf (), render->start() - p, 0);
128                 context->paint ();
129                 
130                 context->restore ();
131
132                 p = min (end, render->end ());
133         }
134 }
135
136 void
137 WaveView::compute_bounding_box () const
138 {
139         if (_region) {
140                 _bounding_box = Rect (0, 0, _region->length() / _frames_per_pixel, _height);
141         } else {
142                 _bounding_box = boost::optional<Rect> ();
143         }
144         
145         _bounding_box_dirty = false;
146 }
147         
148 XMLNode *
149 WaveView::get_state () const
150 {
151         /* XXX */
152         return new XMLNode ("WaveView");
153 }
154
155 void
156 WaveView::set_state (XMLNode const * /*node*/)
157 {
158         /* XXX */
159 }
160
161 void
162 WaveView::set_height (Distance height)
163 {
164         begin_change ();
165
166         _height = height;
167
168         _bounding_box_dirty = true;
169         end_change ();
170
171         invalidate_pixbuf_cache ();
172 }
173
174 void
175 WaveView::set_channel (int channel)
176 {
177         begin_change ();
178         
179         _channel = channel;
180
181         _bounding_box_dirty = true;
182         end_change ();
183
184         invalidate_whole_cache ();
185 }
186
187 void
188 WaveView::invalidate_whole_cache ()
189 {
190         for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
191                 delete *i;
192         }
193
194         _cache.clear ();
195 }
196
197 void
198 WaveView::invalidate_pixbuf_cache ()
199 {
200         for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
201                 (*i)->clear_pixbuf ();
202         }
203 }
204
205 void
206 WaveView::region_resized ()
207 {
208         _bounding_box_dirty = true;
209 }
210
211 void
212 WaveView::set_region_start (frameoffset_t start)
213 {
214         _region_start = start;
215         _bounding_box_dirty = true;
216 }
217
218 /** Construct a new CacheEntry with peak data between two offsets
219  *  in the source.
220  */
221 WaveView::CacheEntry::CacheEntry (
222         WaveView const * wave_view,
223         int start,
224         int end
225         )
226         : _wave_view (wave_view)
227         , _start (start)
228         , _end (end)
229 {
230         _n_peaks = _end - _start;
231         _peaks = new PeakData[_n_peaks];
232
233         _wave_view->_region->read_peaks (
234                 _peaks,
235                 _n_peaks,
236                 _start * _wave_view->_frames_per_pixel,
237                 (_end - _start) * _wave_view->_frames_per_pixel,
238                 _wave_view->_channel,
239                 _wave_view->_frames_per_pixel
240                 );
241 }
242
243 WaveView::CacheEntry::~CacheEntry ()
244 {
245         delete[] _peaks;
246 }
247
248 Glib::RefPtr<Gdk::Pixbuf>
249 WaveView::CacheEntry::pixbuf ()
250 {
251         if (!_pixbuf) {
252                 _pixbuf = Gdk::Pixbuf::create (Gdk::COLORSPACE_RGB, true, 8, _n_peaks, _wave_view->_height);
253                 Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _n_peaks, _wave_view->_height);
254                 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (surface);
255
256                 _wave_view->setup_outline_context (context);
257                 context->move_to (0.5, position (_peaks[0].min));
258                 for (int i = 1; i < _n_peaks; ++i) {
259                         context->line_to (i + 0.5, position (_peaks[i].max));
260                 }
261                 context->stroke ();
262                 
263                 context->move_to (0.5, position (_peaks[0].min));
264                 for (int i = 1; i < _n_peaks; ++i) {
265                         context->line_to (i + 0.5, position (_peaks[i].min));
266                 }
267                 context->stroke ();
268
269                 set_source_rgba (context, _wave_view->_fill_color);
270                 for (int i = 0; i < _n_peaks; ++i) {
271                         context->move_to (i + 0.5, position (_peaks[i].max) - 1);
272                         context->line_to (i + 0.5, position (_peaks[i].min) + 1);
273                         context->stroke ();
274                 }
275
276                 Gtkmm2ext::convert_bgra_to_rgba (surface->get_data(), _pixbuf->get_pixels(), _n_peaks, _wave_view->_height);
277         }
278
279         return _pixbuf;
280 }
281
282
283 Coord
284 WaveView::CacheEntry::position (float s) const
285 {
286         return (s + 1) * _wave_view->_height / 2;
287 }
288
289 void
290 WaveView::CacheEntry::clear_pixbuf ()
291 {
292         _pixbuf.reset ();
293 }
294
295
296