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.
22 #include <cairomm/cairomm.h>
24 #include "gtkmm2ext/utils.h"
26 #include "pbd/compose.h"
27 #include "pbd/signals.h"
29 #include "ardour/types.h"
30 #include "ardour/dB.h"
31 #include "ardour/audioregion.h"
33 #include "canvas/wave_view.h"
34 #include "canvas/utils.h"
35 #include "canvas/canvas.h"
37 #include <gdkmm/general.h>
40 using namespace ARDOUR;
41 using namespace ArdourCanvas;
43 double WaveView::_global_gradient_depth = 0.6;
44 bool WaveView::_global_logscaled = false;
45 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
47 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
49 WaveView::WaveView (Group* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
55 , _samples_per_pixel (0)
57 , _wave_color (0xffffffff)
59 , _zero_color (0xff0000ff)
60 , _clip_color (0xff0000ff)
61 , _logscaled (_global_logscaled)
62 , _shape (_global_shape)
63 , _gradient_depth (_global_gradient_depth)
64 , _shape_independent (false)
65 , _logscaled_independent (false)
66 , _gradient_depth_independent (false)
67 , _amplitude_above_axis (1.0)
70 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
74 WaveView::handle_visual_property_change ()
78 if (!_shape_independent && (_shape != global_shape())) {
79 _shape = global_shape();
83 if (!_logscaled_independent && (_logscaled != global_logscaled())) {
84 _logscaled = global_logscaled();
88 if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
89 _gradient_depth = global_gradient_depth();
94 invalidate_image_cache ();
99 WaveView::set_fill_color (Color c)
101 invalidate_image_cache ();
102 Fill::set_fill_color (c);
106 WaveView::set_outline_color (Color c)
108 invalidate_image_cache ();
109 Outline::set_outline_color (c);
113 WaveView::set_samples_per_pixel (double samples_per_pixel)
117 _samples_per_pixel = samples_per_pixel;
119 _bounding_box_dirty = true;
122 invalidate_whole_cache ();
126 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
128 assert (_samples_per_pixel != 0);
134 /* p, start and end are offsets from the start of the source.
135 area is relative to the position of the region.
138 int const start = rint (area.x0 + _region_start / _samples_per_pixel);
139 int const end = rint (area.x1 + _region_start / _samples_per_pixel);
142 list<CacheEntry*>::iterator cache = _cache.begin ();
146 /* Step through cache entries that end at or before our current position, p */
147 while (cache != _cache.end() && (*cache)->end() <= p) {
152 1. we have run out of cache entries
153 2. the one we are looking at finishes after p but also starts after p.
154 3. the one we are looking at finishes after p and starts before p.
156 Set up a pointer to the cache entry that we will use on this iteration.
159 CacheEntry* render = 0;
161 if (cache == _cache.end ()) {
163 /* Case 1: we have run out of cache entries, so make a new one for
164 the whole required area and put it in the list.
167 CacheEntry* c = new CacheEntry (this, p, end);
168 _cache.push_back (c);
171 } else if ((*cache)->start() > p) {
173 /* Case 2: we have a cache entry, but it starts after p, so we
174 need another one for the missing bit.
177 CacheEntry* c = new CacheEntry (this, p, min (end, (*cache)->start()));
178 cache = _cache.insert (cache, c);
184 /* Case 3: we have a cache entry that will do at least some of what
185 we have left, so render it.
193 int const this_end = min (end, render->end ());
195 Coord const left = p - _region_start / _samples_per_pixel;
196 Coord const right = this_end - _region_start / _samples_per_pixel;
200 context->rectangle (left, area.y0, right, area.height());
203 context->translate (left, 0);
205 context->set_source (render->image(), render->start() - p, 0);
210 p = min (end, render->end ());
215 WaveView::compute_bounding_box () const
218 _bounding_box = Rect (0, 0, _region->length() / _samples_per_pixel, _height);
220 _bounding_box = boost::optional<Rect> ();
223 _bounding_box_dirty = false;
227 WaveView::set_height (Distance height)
233 _bounding_box_dirty = true;
236 invalidate_image_cache ();
240 WaveView::set_channel (int channel)
246 _bounding_box_dirty = true;
249 invalidate_whole_cache ();
253 WaveView::invalidate_whole_cache ()
255 begin_visual_change ();
256 for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
262 end_visual_change ();
266 WaveView::invalidate_image_cache ()
268 begin_visual_change ();
270 for (list<CacheEntry*>::iterator i = _cache.begin(); i != _cache.end(); ++i) {
271 (*i)->clear_image ();
274 end_visual_change ();
278 WaveView::region_resized ()
280 _bounding_box_dirty = true;
284 WaveView::set_logscaled (bool yn)
286 if (_logscaled != yn) {
288 invalidate_image_cache ();
293 WaveView::gain_changed ()
295 invalidate_whole_cache ();
299 WaveView::set_zero_color (Color c)
301 if (_zero_color != c) {
303 invalidate_image_cache ();
308 WaveView::set_clip_color (Color c)
310 if (_clip_color != c) {
312 invalidate_image_cache ();
317 WaveView::set_show_zero_line (bool yn)
319 if (_show_zero != yn) {
321 invalidate_image_cache ();
326 WaveView::set_shape (Shape s)
330 invalidate_image_cache ();
335 WaveView::set_amplitude_above_axis (double a)
337 if (_amplitude_above_axis != a) {
338 _amplitude_above_axis = a;
339 invalidate_image_cache ();
344 WaveView::set_global_shape (Shape s)
346 if (_global_shape != s) {
348 VisualPropertiesChanged (); /* EMIT SIGNAL */
353 WaveView::set_global_logscaled (bool yn)
355 if (_global_logscaled != yn) {
356 _global_logscaled = yn;
357 VisualPropertiesChanged (); /* EMIT SIGNAL */
363 WaveView::set_region_start (frameoffset_t start)
365 _region_start = start;
366 _bounding_box_dirty = true;
369 /** Construct a new CacheEntry with peak data between two offsets
372 WaveView::CacheEntry::CacheEntry (WaveView const * wave_view, int start, int end)
373 : _wave_view (wave_view)
377 _n_peaks = _end - _start;
378 _peaks.reset (new PeakData[_n_peaks]);
380 _wave_view->_region->read_peaks (
383 _start * _wave_view->_samples_per_pixel,
384 (_end - _start) * _wave_view->_samples_per_pixel,
385 _wave_view->_channel,
386 _wave_view->_samples_per_pixel
390 WaveView::CacheEntry::~CacheEntry ()
395 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
397 return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
401 alt_log_meter (float power)
403 return _log_meter (power, -192.0, 0.0, 8.0);
406 Cairo::RefPtr<Cairo::ImageSurface>
407 WaveView::CacheEntry::image ()
411 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _n_peaks, _wave_view->_height);
412 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
414 /* Draw the edge of the waveform, top half first, the loop back
415 * for the bottom half to create a clockwise path
418 context->begin_new_path();
420 if (_wave_view->_shape == WaveView::Rectified) {
422 /* top edge of waveform is based on max (fabs (peak_min, peak_max))
425 if (_wave_view->_logscaled) {
426 for (int i = 0; i < _n_peaks; ++i) {
427 context->line_to (i + 0.5, position (alt_log_meter (fast_coefficient_to_dB (
428 max (fabs (_peaks[i].max), fabs (_peaks[i].min))))));
431 for (int i = 0; i < _n_peaks; ++i) {
432 context->line_to (i + 0.5, position (max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
437 if (_wave_view->_logscaled) {
438 for (int i = 0; i < _n_peaks; ++i) {
439 Coord y = _peaks[i].max;
441 context->line_to (i + 0.5, position (alt_log_meter (fast_coefficient_to_dB (y))));
442 } else if (y < 0.0) {
443 context->line_to (i + 0.5, position (-alt_log_meter (fast_coefficient_to_dB (-y))));
445 context->line_to (i + 0.5, position (0.0));
449 for (int i = 0; i < _n_peaks; ++i) {
450 context->line_to (i + 0.5, position (_peaks[i].max));
455 /* from final top point, move out of the clip zone */
457 context->line_to (_n_peaks + 10, position (0.0));
460 /* bottom half, in reverse */
462 if (_wave_view->_shape == WaveView::Rectified) {
464 /* lower half: drop to the bottom, then a line back to
465 * beyond the left edge of the clip region
468 context->line_to (_n_peaks + 10, _wave_view->_height);
469 context->line_to (-10.0, _wave_view->_height);
473 if (_wave_view->_logscaled) {
474 for (int i = _n_peaks-1; i >= 0; --i) {
475 Coord y = _peaks[i].min;
477 context->line_to (i + 0.5, position (alt_log_meter (fast_coefficient_to_dB (y))));
478 } else if (y < 0.0) {
479 context->line_to (i + 0.5, position (-alt_log_meter (fast_coefficient_to_dB (-y))));
481 context->line_to (i + 0.5, position (0.0));
485 for (int i = _n_peaks-1; i >= 0; --i) {
486 context->line_to (i + 0.5, position (_peaks[i].min));
490 /* from final bottom point, move out of the clip zone */
492 context->line_to (-10.0, position (0.0));
495 context->close_path ();
497 if (_wave_view->gradient_depth() != 0.0) {
499 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _wave_view->_height));
505 if (_wave_view->_shape == Rectified) {
515 color_to_rgba (_wave_view->_fill_color, r, g, b, a);
516 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
517 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
519 /* generate a new color for the middle of the gradient */
521 color_to_hsv (_wave_view->_fill_color, h, s, v);
522 /* tone down the saturation */
523 s *= 1.0 - _wave_view->gradient_depth();
524 Color center = hsv_to_color (h, s, v, a);
525 color_to_rgba (center, r, g, b, a);
526 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
528 context->set_source (gradient);
530 set_source_rgba (context, _wave_view->_fill_color);
533 context->fill_preserve ();
534 _wave_view->setup_outline_context (context);
537 if (_wave_view->show_zero_line()) {
538 set_source_rgba (context, _wave_view->_zero_color);
539 context->move_to (0, position (0.0));
540 context->line_to (_n_peaks, position (0.0));
550 WaveView::CacheEntry::position (double s) const
552 switch (_wave_view->_shape) {
554 return _wave_view->_height - (s * _wave_view->_height);
558 return (1.0-s) * (_wave_view->_height / 2.0);
562 WaveView::CacheEntry::clear_image ()
568 WaveView::set_global_gradient_depth (double depth)
570 if (_global_gradient_depth != depth) {
571 _global_gradient_depth = depth;
572 VisualPropertiesChanged (); /* EMIT SIGNAL */