2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <cairomm/cairomm.h>
23 #include "gtkmm2ext/utils.h"
25 #include "pbd/compose.h"
26 #include "pbd/signals.h"
28 #include "ardour/types.h"
29 #include "ardour/audioregion.h"
31 #include "canvas/wave_view.h"
32 #include "canvas/utils.h"
34 #include <gdkmm/general.h>
37 using namespace ARDOUR;
38 using namespace ArdourCanvas;
40 WaveView::WaveView (Group* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
46 , _samples_per_pixel (0)
48 , _wave_color (0xffffffff)
55 WaveView::set_samples_per_pixel (double samples_per_pixel)
59 _samples_per_pixel = samples_per_pixel;
61 _bounding_box_dirty = true;
64 invalidate_whole_cache ();
68 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
70 assert (_samples_per_pixel != 0);
76 /* p, start and end are offsets from the start of the source.
77 area is relative to the position of the region.
80 int const start = rint (area.x0 + _region_start / _samples_per_pixel);
81 int const end = rint (area.x1 + _region_start / _samples_per_pixel);
84 list<CacheEntry*>::iterator cache = _cache.begin ();
88 /* Step through cache entries that end at or before our current position, p */
89 while (cache != _cache.end() && (*cache)->end() <= p) {
94 1. we have run out of cache entries
95 2. the one we are looking at finishes after p but also starts after p.
96 3. the one we are looking at finishes after p and starts before p.
98 Set up a pointer to the cache entry that we will use on this iteration.
101 CacheEntry* render = 0;
103 if (cache == _cache.end ()) {
105 /* Case 1: we have run out of cache entries, so make a new one for
106 the whole required area and put it in the list.
109 CacheEntry* c = new CacheEntry (this, p, end);
110 _cache.push_back (c);
113 } else if ((*cache)->start() > p) {
115 /* Case 2: we have a cache entry, but it starts after p, so we
116 need another one for the missing bit.
119 CacheEntry* c = new CacheEntry (this, p, (*cache)->start());
120 cache = _cache.insert (cache, c);
126 /* Case 3: we have a cache entry that will do at least some of what
127 we have left, so render it.
135 int const this_end = min (end, render->end ());
137 Coord const left = p - _region_start / _samples_per_pixel;
138 Coord const right = this_end - _region_start / _samples_per_pixel;
142 context->rectangle (left, area.y0, right, area.height());
145 context->translate (left, 0);
147 context->set_source (render->image(), render->start() - p, 0);
152 p = min (end, render->end ());
157 WaveView::compute_bounding_box () const
160 _bounding_box = Rect (0, 0, _region->length() / _samples_per_pixel, _height);
162 _bounding_box = boost::optional<Rect> ();
165 _bounding_box_dirty = false;
169 WaveView::set_height (Distance height)
175 _bounding_box_dirty = true;
178 invalidate_image_cache ();
182 WaveView::set_channel (int channel)
188 _bounding_box_dirty = true;
191 invalidate_whole_cache ();
195 WaveView::invalidate_whole_cache ()
197 for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
205 WaveView::invalidate_image_cache ()
207 for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
208 (*i)->clear_image ();
213 WaveView::region_resized ()
215 _bounding_box_dirty = true;
219 WaveView::set_region_start (frameoffset_t start)
221 _region_start = start;
222 _bounding_box_dirty = true;
225 /** Construct a new CacheEntry with peak data between two offsets
228 WaveView::CacheEntry::CacheEntry (
229 WaveView const * wave_view,
233 : _wave_view (wave_view)
237 _n_peaks = _end - _start;
238 _peaks.reset (new PeakData[_n_peaks]);
240 _wave_view->_region->read_peaks (
243 _start * _wave_view->_samples_per_pixel,
244 (_end - _start) * _wave_view->_samples_per_pixel,
245 _wave_view->_channel,
246 _wave_view->_samples_per_pixel
250 WaveView::CacheEntry::~CacheEntry ()
254 Cairo::RefPtr<Cairo::ImageSurface>
255 WaveView::CacheEntry::image ()
259 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _n_peaks, _wave_view->_height);
260 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
262 _wave_view->setup_outline_context (context);
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].max));
269 context->move_to (0.5, position (_peaks[0].min));
270 for (int i = 1; i < _n_peaks; ++i) {
271 context->line_to (i + 0.5, position (_peaks[i].min));
275 set_source_rgba (context, _wave_view->_fill_color);
276 for (int i = 0; i < _n_peaks; ++i) {
277 context->move_to (i + 0.5, position (_peaks[i].max) - 1);
278 context->line_to (i + 0.5, position (_peaks[i].min) + 1);
288 WaveView::CacheEntry::position (float s) const
290 return (s + 1) * _wave_view->_height / 2;
294 WaveView::CacheEntry::clear_image ()