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"
28 #include "pbd/stacktrace.h"
30 #include "ardour/types.h"
31 #include "ardour/dB.h"
32 #include "ardour/audioregion.h"
34 #include "canvas/wave_view.h"
35 #include "canvas/utils.h"
36 #include "canvas/canvas.h"
38 #include <gdkmm/general.h>
41 using namespace ARDOUR;
42 using namespace ArdourCanvas;
44 double WaveView::_global_gradient_depth = 0.6;
45 bool WaveView::_global_logscaled = false;
46 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
47 bool WaveView::_global_show_waveform_clipping = true;
49 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
51 WaveView::WaveView (Group* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
57 , _samples_per_pixel (0)
59 , _wave_color (0xffffffff)
61 , _zero_color (0xff0000ff)
62 , _clip_color (0xff0000ff)
63 , _logscaled (_global_logscaled)
64 , _shape (_global_shape)
65 , _gradient_depth (_global_gradient_depth)
66 , _shape_independent (false)
67 , _logscaled_independent (false)
68 , _gradient_depth_independent (false)
69 , _amplitude_above_axis (1.0)
70 , _region_start (region->start())
74 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
77 WaveView::~WaveView ()
82 WaveView::handle_visual_property_change ()
86 if (!_shape_independent && (_shape != global_shape())) {
87 _shape = global_shape();
91 if (!_logscaled_independent && (_logscaled != global_logscaled())) {
92 _logscaled = global_logscaled();
96 if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
97 _gradient_depth = global_gradient_depth();
107 WaveView::set_fill_color (Color c)
109 if (c != _fill_color) {
111 Fill::set_fill_color (c);
116 WaveView::set_outline_color (Color c)
118 if (c != _outline_color) {
120 Outline::set_outline_color (c);
125 WaveView::set_samples_per_pixel (double samples_per_pixel)
127 if (samples_per_pixel != _samples_per_pixel) {
130 _samples_per_pixel = samples_per_pixel;
132 _bounding_box_dirty = true;
141 image_to_window (double wave_origin, double image_start)
143 return wave_origin + image_start;
147 window_to_image (double wave_origin, double image_start)
149 return image_start - wave_origin;
153 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
155 return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
159 alt_log_meter (float power)
161 return _log_meter (power, -192.0, 0.0, 8.0);
170 LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
174 WaveView::draw_image (PeakData* _peaks, int n_peaks) const
176 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, _height);
178 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
180 boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
182 /* Clip level nominally set to -0.9dBFS to account for inter-sample
183 interpolation possibly clipping (value may be too low).
185 We adjust by the region's own gain (but note: not by any gain
186 automation or its gain envelope) so that clip indicators are closer
187 to providing data about on-disk data. This multiplication is
188 needed because the data we get from AudioRegion::read_peaks()
189 has been scaled by scale_amplitude() already.
192 const double clip_level = 0.98853 * _region->scale_amplitude();
194 if (_shape == WaveView::Rectified) {
196 /* each peak is a line from the bottom of the waveview
197 * to a point determined by max (_peaks[i].max,
202 for (int i = 0; i < n_peaks; ++i) {
203 tips[i].bot = height();
204 tips[i].top = position (alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min)))));
206 if (fabs (_peaks[i].max) >= clip_level) {
207 tips[i].clip_max = true;
210 if (fabs (_peaks[i].min) >= clip_level) {
211 tips[i].clip_min = true;
214 } else {for (int i = 0; i < n_peaks; ++i) {
215 tips[i].bot = height();
216 tips[i].top = position (max (fabs (_peaks[i].max), fabs (_peaks[i].min)));
218 if (fabs (_peaks[i].max) >= clip_level) {
219 tips[i].clip_max = true;
222 if (fabs (_peaks[i].min) >= clip_level) {
223 tips[i].clip_min = true;
231 for (int i = 0; i < n_peaks; ++i) {
232 Coord top = _peaks[i].min;
233 Coord bot = _peaks[i].max;
235 if (fabs (top) >= clip_level) {
236 tips[i].clip_max = true;
239 if (fabs (bot) >= clip_level) {
240 tips[i].clip_min = true;
244 top = position (alt_log_meter (fast_coefficient_to_dB (top)));
245 } else if (top < 0.0) {
246 top = position (-alt_log_meter (fast_coefficient_to_dB (-top)));
248 top = position (0.0);
252 bot = position (alt_log_meter (fast_coefficient_to_dB (bot)));
253 } else if (bot < 0.0) {
254 bot = position (-alt_log_meter (fast_coefficient_to_dB (-bot)));
256 bot = position (0.0);
264 for (int i = 0; i < n_peaks; ++i) {
266 if (fabs (_peaks[i].max) >= clip_level) {
267 tips[i].clip_max = true;
270 if (fabs (_peaks[i].min) >= clip_level) {
271 tips[i].clip_min = true;
274 tips[i].top = position (_peaks[i].min);
275 tips[i].bot = position (_peaks[i].max);
282 if (gradient_depth() != 0.0) {
284 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _height));
290 if (_shape == Rectified) {
300 color_to_rgba (_fill_color, r, g, b, a);
301 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
302 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
304 /* generate a new color for the middle of the gradient */
306 color_to_hsv (_fill_color, h, s, v);
307 /* change v towards white */
308 v *= 1.0 - gradient_depth();
309 Color center = hsv_to_color (h, s, v, a);
310 color_to_rgba (center, r, g, b, a);
311 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
313 context->set_source (gradient);
315 set_source_rgba (context, _fill_color);
318 /* ensure single-pixel lines */
320 context->set_line_width (0.5);
321 context->translate (0.5, 0.0);
325 if (_shape == WaveView::Rectified) {
326 for (int i = 0; i < n_peaks; ++i) {
327 context->move_to (i, tips[i].top); /* down 1 pixel */
328 context->line_to (i, tips[i].bot);
332 for (int i = 0; i < n_peaks; ++i) {
333 context->move_to (i, tips[i].top);
334 context->line_to (i, tips[i].bot);
339 /* now add dots to the top and bottom of each line (this is
340 * modelled on pyramix, except that we add clipping indicators.
343 if (_global_show_waveform_clipping) {
345 context->set_source_rgba (0, 0, 0, 1.0);
347 /* the height of the clip-indicator should be at most 7 pixels,
348 or 5% of the height of the waveview item.
350 const double clip_height = min (7.0, ceil (_height * 0.05));
352 for (int i = 0; i < n_peaks; ++i) {
353 context->move_to (i, tips[i].top);
355 bool show_top_clip = (_shape == WaveView::Rectified && (tips[i].clip_max || tips[i].clip_min)) ||
359 context->set_source_rgba (1.0, 0, 0, 1.0);
360 context->rel_line_to (0, clip_height);
362 context->set_source_rgba (0.0, 0, 0, 1.0);
364 context->rel_line_to (0, 1.0);
368 if (_shape != WaveView::Rectified) {
369 context->move_to (i, tips[i].bot);
370 if (tips[i].clip_min) {
371 context->set_source_rgba (1.0, 0, 0, 1.0);
372 context->rel_line_to (0, -clip_height);
374 context->set_source_rgba (0.0, 0, 0, 1.0);
376 context->rel_line_to (0, -1.0);
383 if (show_zero_line()) {
384 set_source_rgba (context, _zero_color);
385 context->move_to (0, position (0.0));
386 context->line_to (n_peaks, position (0.0));
392 WaveView::ensure_cache (framepos_t start, framepos_t end) const
394 if (_image && _sample_start <= start && _sample_end >= end) {
395 /* cache already covers required range, do nothing */
399 /* sample position is canonical here, and we want to generate
400 * an image that spans about twice the canvas width
403 const framepos_t center = start + ((end - start) / 2);
404 const framecnt_t canvas_samples = 2 * (_canvas->visible_area().width() * _samples_per_pixel);
406 /* we can request data from anywhere in the Source, between 0 and its length
409 _sample_start = max ((framepos_t) 0, (center - canvas_samples));
410 _sample_end = min (center + canvas_samples, _region->source_length (0));
412 double pixel_start = floor (_sample_start / (double) _samples_per_pixel);
413 double pixel_end = ceil (_sample_end / (double) _samples_per_pixel);
414 int n_peaks = llrintf (pixel_end - pixel_start);
416 boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
418 _region->read_peaks (peaks.get(), n_peaks,
419 _sample_start, _sample_end - _sample_start,
423 draw_image (peaks.get(), n_peaks);
427 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
429 assert (_samples_per_pixel != 0);
435 Rect self = item_to_window (Rect (0.0, 0.0, floor (_region->length() / _samples_per_pixel), _height));
436 boost::optional<Rect> d = self.intersection (area);
444 /* window coordinates - pixels where x=0 is the left edge of the canvas
445 * window. We round up and down in case we were asked to
446 * draw "between" pixels at the start and/or end.
449 const double draw_start = floor (draw.x0);
450 const double draw_end = ceil (draw.x1);
452 // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
454 /* image coordnates: pixels where x=0 is the start of this waveview,
455 * wherever it may be positioned. thus image_start=N means "an image
456 * that beings N pixels after the start of region that this waveview is
460 const framepos_t image_start = window_to_image (self.x0, draw_start);
461 const framepos_t image_end = window_to_image (self.x0, draw_end);
463 // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
465 /* sample coordinates - note, these are not subject to rounding error */
466 framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
467 framepos_t sample_end = _region_start + (image_end * _samples_per_pixel);
469 // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl;
471 ensure_cache (sample_start, sample_end);
473 // cerr << "Cache contains " << _cache->pixel_start() << " .. " << _cache->pixel_end() << " / "
474 // << _cache->sample_start() << " .. " << _cache->sample_end()
477 double image_offset = (_sample_start - _region->start()) / _samples_per_pixel;
479 // cerr << "Offset into image to place at zero: " << image_offset << endl;
481 context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
483 /* round image origin position to an exact pixel in device space to
487 double x = self.x0 + image_offset;
489 context->user_to_device (x, y);
492 context->device_to_user (x, y);
494 context->set_source (_image, x, y);
499 WaveView::compute_bounding_box () const
502 _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height);
504 _bounding_box = boost::optional<Rect> ();
507 _bounding_box_dirty = false;
511 WaveView::set_height (Distance height)
513 if (height != _height) {
518 _bounding_box_dirty = true;
526 WaveView::set_channel (int channel)
528 if (channel != _channel) {
533 _bounding_box_dirty = true;
541 WaveView::invalidate_image ()
543 begin_visual_change ();
549 end_visual_change ();
554 WaveView::set_logscaled (bool yn)
556 if (_logscaled != yn) {
563 WaveView::gain_changed ()
569 WaveView::set_zero_color (Color c)
571 if (_zero_color != c) {
578 WaveView::set_clip_color (Color c)
580 if (_clip_color != c) {
587 WaveView::set_show_zero_line (bool yn)
589 if (_show_zero != yn) {
596 WaveView::set_shape (Shape s)
605 WaveView::set_amplitude_above_axis (double a)
607 if (_amplitude_above_axis != a) {
608 _amplitude_above_axis = a;
614 WaveView::set_global_shape (Shape s)
616 if (_global_shape != s) {
618 VisualPropertiesChanged (); /* EMIT SIGNAL */
623 WaveView::set_global_logscaled (bool yn)
625 if (_global_logscaled != yn) {
626 _global_logscaled = yn;
627 VisualPropertiesChanged (); /* EMIT SIGNAL */
632 WaveView::region_resized ()
638 /* special: do not use _region->length() here to compute
639 bounding box because it will already have changed.
641 if we have a bounding box, use it.
644 _pre_change_bounding_box = _bounding_box;
646 _bounding_box_dirty = true;
647 compute_bounding_box ();
653 WaveView::position (double s) const
655 /* it is important that this returns an integral value, so that we
656 can ensure correct single pixel behaviour.
663 pos = floor (_height - (s * _height));
665 pos = floor ((1.0-s) * (_height / 2.0));
669 return min (_height, (max (0.0, pos)));
673 WaveView::set_global_gradient_depth (double depth)
675 if (_global_gradient_depth != depth) {
676 _global_gradient_depth = depth;
677 VisualPropertiesChanged (); /* EMIT SIGNAL */
682 WaveView::set_global_show_waveform_clipping (bool yn)
684 if (_global_show_waveform_clipping != yn) {
685 _global_show_waveform_clipping = yn;
686 VisualPropertiesChanged (); /* EMIT SIGNAL */