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 <boost/scoped_array.hpp>
26 #include "gtkmm2ext/utils.h"
28 #include "pbd/compose.h"
29 #include "pbd/signals.h"
30 #include "pbd/stacktrace.h"
32 #include "ardour/types.h"
33 #include "ardour/dB.h"
34 #include "ardour/audioregion.h"
36 #include "canvas/wave_view.h"
37 #include "canvas/utils.h"
38 #include "canvas/canvas.h"
40 #include <gdkmm/general.h>
43 using namespace ARDOUR;
44 using namespace ArdourCanvas;
46 double WaveView::_global_gradient_depth = 0.6;
47 bool WaveView::_global_logscaled = false;
48 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
50 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
52 WaveView::WaveView (Group* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
58 , _samples_per_pixel (0)
60 , _wave_color (0xffffffff)
62 , _zero_color (0xff0000ff)
63 , _clip_color (0xff0000ff)
64 , _logscaled (_global_logscaled)
65 , _shape (_global_shape)
66 , _gradient_depth (_global_gradient_depth)
67 , _shape_independent (false)
68 , _logscaled_independent (false)
69 , _gradient_depth_independent (false)
70 , _amplitude_above_axis (1.0)
71 , _region_start (region->start())
74 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
77 WaveView::~WaveView ()
84 WaveView::handle_visual_property_change ()
88 if (!_shape_independent && (_shape != global_shape())) {
89 _shape = global_shape();
93 if (!_logscaled_independent && (_logscaled != global_logscaled())) {
94 _logscaled = global_logscaled();
98 if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
99 _gradient_depth = global_gradient_depth();
104 invalidate_image_cache ();
109 WaveView::set_fill_color (Color c)
111 if (c != _fill_color) {
112 invalidate_image_cache ();
113 Fill::set_fill_color (c);
118 WaveView::set_outline_color (Color c)
120 if (c != _outline_color) {
121 invalidate_image_cache ();
122 Outline::set_outline_color (c);
127 WaveView::set_samples_per_pixel (double samples_per_pixel)
129 if (samples_per_pixel != _samples_per_pixel) {
132 _samples_per_pixel = samples_per_pixel;
134 _bounding_box_dirty = true;
138 invalidate_whole_cache ();
143 image_to_window (double wave_origin, double image_start)
145 return wave_origin + image_start;
149 window_to_image (double wave_origin, double image_start)
151 return image_start - wave_origin;
155 WaveView::ensure_cache (framecnt_t start, framecnt_t end,
156 framepos_t sample_start, framepos_t sample_end) const
158 if (_cache && _cache->sample_start() <= sample_start && _cache->sample_end() >= sample_end) {
159 /* cache already covers required range, do nothing */
168 /* sample position is canonical here, and we want to generate
169 * an image that spans about twice the canvas width
172 const framepos_t center = sample_start + ((sample_end - sample_start) / 2);
173 const framecnt_t canvas_samples = 2 * (_canvas->visible_area().width() * _samples_per_pixel);
175 /* we can request data from anywhere in the Source, between 0 and its length
178 sample_start = max ((framepos_t) 0, (center - canvas_samples));
179 sample_end = min (center + canvas_samples, _region->source_length (0));
182 if (sample_end <= sample_start) {
183 cerr << "sample start = " << sample_start << endl;
184 cerr << "center+ = " << center<< endl;
185 cerr << "CS = " << canvas_samples << endl;
186 cerr << "pui = " << center + canvas_samples << endl;
187 cerr << "sl = " << _region->source_length (0) << endl;
188 cerr << "st = " << _region->start () << endl;
189 cerr << "END: " << sample_end << endl;
194 start = floor (sample_start / (double) _samples_per_pixel);
195 end = ceil (sample_end / (double) _samples_per_pixel);
197 assert (end > start);
199 // cerr << name << " cache miss - new CE, span " << start << " .. " << end << " (" << sample_start << " .. " << sample_end << ")\n";
201 _cache = new CacheEntry (this, start, end, sample_start, sample_end);
205 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
207 assert (_samples_per_pixel != 0);
213 Rect self = item_to_window (Rect (0.0, 0.0, floor (_region->length() / _samples_per_pixel), _height));
214 boost::optional<Rect> d = self.intersection (area);
222 /* window coordinates - pixels where x=0 is the left edge of the canvas
223 * window. We round up and down in case we were asked to
224 * draw "between" pixels at the start and/or end.
227 const double draw_start = floor (draw.x0);
228 const double draw_end = ceil (draw.x1);
230 // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
232 /* image coordnates: pixels where x=0 is the start of this waveview,
233 * wherever it may be positioned. thus image_start=N means "an image
234 * that beings N pixels after the start of region that this waveview is
238 const framepos_t image_start = window_to_image (self.x0, draw_start);
239 const framepos_t image_end = window_to_image (self.x0, draw_end);
241 // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
243 /* sample coordinates - note, these are not subject to rounding error */
244 framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
245 framepos_t sample_end = _region_start + (image_end * _samples_per_pixel);
247 // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl;
249 ensure_cache (image_start, image_end, sample_start, sample_end);
251 // cerr << "Cache contains " << _cache->pixel_start() << " .. " << _cache->pixel_end() << " / "
252 // << _cache->sample_start() << " .. " << _cache->sample_end()
255 double image_offset = (_cache->sample_start() - _region->start()) / _samples_per_pixel;
257 // cerr << "Offset into image to place at zero: " << image_offset << endl;
259 context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
261 /* round image origin position to an exact pixel in device space to
265 double x = self.x0 + image_offset;
267 context->user_to_device (x, y);
270 context->device_to_user (x, y);
272 context->set_source (_cache->image(), x, y);
277 WaveView::compute_bounding_box () const
280 _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height);
282 _bounding_box = boost::optional<Rect> ();
285 _bounding_box_dirty = false;
289 WaveView::set_height (Distance height)
291 if (height != _height) {
296 _bounding_box_dirty = true;
299 invalidate_image_cache ();
304 WaveView::set_channel (int channel)
306 if (channel != _channel) {
311 _bounding_box_dirty = true;
314 invalidate_whole_cache ();
319 WaveView::invalidate_whole_cache ()
321 begin_visual_change ();
324 end_visual_change ();
328 WaveView::invalidate_image_cache ()
330 invalidate_whole_cache ();
334 WaveView::set_logscaled (bool yn)
336 if (_logscaled != yn) {
338 invalidate_image_cache ();
343 WaveView::gain_changed ()
345 invalidate_whole_cache ();
349 WaveView::set_zero_color (Color c)
351 if (_zero_color != c) {
353 invalidate_image_cache ();
358 WaveView::set_clip_color (Color c)
360 if (_clip_color != c) {
362 invalidate_image_cache ();
367 WaveView::set_show_zero_line (bool yn)
369 if (_show_zero != yn) {
371 invalidate_image_cache ();
376 WaveView::set_shape (Shape s)
380 invalidate_image_cache ();
385 WaveView::set_amplitude_above_axis (double a)
387 if (_amplitude_above_axis != a) {
388 _amplitude_above_axis = a;
389 invalidate_image_cache ();
394 WaveView::set_global_shape (Shape s)
396 if (_global_shape != s) {
398 VisualPropertiesChanged (); /* EMIT SIGNAL */
403 WaveView::set_global_logscaled (bool yn)
405 if (_global_logscaled != yn) {
406 _global_logscaled = yn;
407 VisualPropertiesChanged (); /* EMIT SIGNAL */
412 WaveView::region_resized ()
418 /* special: do not use _region->length() here to compute
419 bounding box because it will already have changed.
421 if we have a bounding box, use it.
424 _pre_change_bounding_box = _bounding_box;
426 _bounding_box_dirty = true;
427 compute_bounding_box ();
432 WaveView::CacheEntry::CacheEntry (WaveView const * wave_view, double pixel_start, double pixel_end,
433 framepos_t sample_start,framepos_t sample_end)
434 : _wave_view (wave_view)
435 , _pixel_start (pixel_start)
436 , _pixel_end (pixel_end)
437 , _sample_start (sample_start)
438 , _sample_end (sample_end)
439 , _n_peaks (_pixel_end - _pixel_start)
441 _peaks.reset (new PeakData[_n_peaks]);
443 _wave_view->_region->read_peaks (_peaks.get(), _n_peaks,
444 _sample_start, _sample_end - _sample_start,
445 _wave_view->_channel,
446 _wave_view->_samples_per_pixel);
449 WaveView::CacheEntry::~CacheEntry ()
454 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
456 return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
460 alt_log_meter (float power)
462 return _log_meter (power, -192.0, 0.0, 8.0);
470 LineTips() : top (0.0), bot (0.0), clipped (false) {}
473 Cairo::RefPtr<Cairo::ImageSurface>
474 WaveView::CacheEntry::image ()
478 _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _n_peaks, _wave_view->_height);
479 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (_image);
481 #ifdef AREA_DRAW_AND_FILL
483 /* Draw the edge of the waveform, top half first, the loop back
484 * for the bottom half to create a clockwise path
487 context->begin_new_path();
489 if (_wave_view->_shape == WaveView::Rectified) {
491 /* top edge of waveform is based on max (fabs (peak_min, peak_max))
494 if (_wave_view->_logscaled) {
495 for (int i = 0; i < _n_peaks; ++i) {
496 context->line_to (i + 0.5, position (alt_log_meter (fast_coefficient_to_dB (
497 max (fabs (_peaks[i].max), fabs (_peaks[i].min))))));
500 for (int i = 0; i < _n_peaks; ++i) {
501 context->line_to (i + 0.5, position (max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
506 if (_wave_view->_logscaled) {
507 for (int i = 0; i < _n_peaks; ++i) {
508 Coord y = _peaks[i].max;
511 y = position (alt_log_meter (fast_coefficient_to_dB (y)));
512 } else if (y < 0.0) {
513 y = position (-alt_log_meter (fast_coefficient_to_dB (-y)));
517 context->line_to (i + 0.5, y);
520 for (int i = 0; i < _n_peaks; ++i) {
521 context->line_to (i + 0.5, position (_peaks[i].max));
526 /* from final top point, move out of the clip zone */
528 context->line_to (_n_peaks + 10, position (0.0));
530 /* bottom half, in reverse */
532 if (_wave_view->_shape == WaveView::Rectified) {
534 /* lower half: drop to the bottom, then a line back to
535 * beyond the left edge of the clip region
538 context->line_to (_n_peaks + 10, _wave_view->_height);
539 context->line_to (-10.0, _wave_view->_height);
543 if (_wave_view->_logscaled) {
544 for (int i = _n_peaks-1; i >= 0; --i) {
545 Coord y = _peaks[i].min;
547 context->line_to (i + 0.5, position (alt_log_meter (fast_coefficient_to_dB (y))));
548 } else if (y < 0.0) {
549 context->line_to (i + 0.5, position (-alt_log_meter (fast_coefficient_to_dB (-y))));
551 context->line_to (i + 0.5, position (0.0));
555 for (int i = _n_peaks-1; i >= 0; --i) {
556 context->line_to (i + 0.5, position (_peaks[i].min));
560 /* from final bottom point, move out of the clip zone */
562 context->line_to (-10.0, position (0.0));
565 context->close_path ();
567 if (_wave_view->gradient_depth() != 0.0) {
569 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _wave_view->_height));
575 if (_wave_view->_shape == Rectified) {
585 color_to_rgba (_wave_view->_fill_color, r, g, b, a);
586 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
587 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
589 /* generate a new color for the middle of the gradient */
591 color_to_hsv (_wave_view->_fill_color, h, s, v);
592 /* tone down the saturation */
593 s *= 1.0 - _wave_view->gradient_depth();
594 Color center = hsv_to_color (h, s, v, a);
595 color_to_rgba (center, r, g, b, a);
596 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
598 context->set_source (gradient);
600 set_source_rgba (context, _wave_view->_fill_color);
603 context->fill_preserve ();
604 _wave_view->setup_outline_context (context);
608 boost::scoped_array<LineTips> tips (new LineTips[_n_peaks]);
610 if (_wave_view->_shape == WaveView::Rectified) {
612 /* each peak is a line from the bottom of the waveview
613 * to a point determined by max (_peaks[i].max,
617 if (_wave_view->_logscaled) {
618 for (int i = 0; i < _n_peaks; ++i) {
619 tips[i].bot = _wave_view->height();
620 tips[i].top = position (alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min)))));
623 for (int i = 0; i < _n_peaks; ++i) {
624 tips[i].bot = _wave_view->height();
625 tips[i].top = position (max (fabs (_peaks[i].max), fabs (_peaks[i].min)));
631 if (_wave_view->_logscaled) {
632 for (int i = 0; i < _n_peaks; ++i) {
633 Coord top = _peaks[i].min;
634 Coord bot = _peaks[i].max;
637 top = position (alt_log_meter (fast_coefficient_to_dB (top)));
638 } else if (top < 0.0) {
639 top = position (-alt_log_meter (fast_coefficient_to_dB (-top)));
641 top = position (0.0);
645 bot = position (alt_log_meter (fast_coefficient_to_dB (bot)));
646 } else if (bot < 0.0) {
647 bot = position (-alt_log_meter (fast_coefficient_to_dB (-bot)));
649 bot = position (0.0);
658 for (int i = 0; i < _n_peaks; ++i) {
659 tips[i].top = position (_peaks[i].min);
660 tips[i].bot = position (_peaks[i].max);
665 if (_wave_view->gradient_depth() != 0.0) {
667 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _wave_view->_height));
673 if (_wave_view->_shape == Rectified) {
683 color_to_rgba (_wave_view->_fill_color, r, g, b, a);
684 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
685 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
687 /* generate a new color for the middle of the gradient */
689 color_to_hsv (_wave_view->_fill_color, h, s, v);
690 /* change v towards white */
691 v *= 1.0 - _wave_view->gradient_depth();
692 Color center = hsv_to_color (h, s, v, a);
693 color_to_rgba (center, r, g, b, a);
694 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
696 context->set_source (gradient);
698 set_source_rgba (context, _wave_view->_fill_color);
701 /* ensure single-pixel lines */
703 context->set_line_width (0.5);
704 context->translate (0.5, 0.0);
708 if (_wave_view->_shape == WaveView::Rectified) {
709 for (int i = 0; i < _n_peaks; ++i) {
710 context->move_to (i, tips[i].top); /* down 1 pixel */
711 context->line_to (i, tips[i].bot);
715 for (int i = 0; i < _n_peaks; ++i) {
716 context->move_to (i, tips[i].top);
717 context->line_to (i, tips[i].bot);
722 /* now add dots to the top and bottom of each line (this is
723 * modelled on pyramix, except that we add clipping indicators.
726 context->set_source_rgba (0, 0, 0, 1.0);
728 for (int i = 0; i < _n_peaks; ++i) {
729 context->move_to (i, tips[i].top);
730 context->rel_line_to (0, 1.0);
732 if (_wave_view->_shape != WaveView::Rectified) {
733 context->move_to (i, tips[i].bot);
734 context->rel_line_to (0, -1.0);
740 if (_wave_view->show_zero_line()) {
741 set_source_rgba (context, _wave_view->_zero_color);
742 context->move_to (0, position (0.0));
743 context->line_to (_n_peaks, position (0.0));
754 WaveView::CacheEntry::position (double s) const
756 /* it is important that this returns an integral value, so that we
757 can ensure correct single pixel behaviour.
760 switch (_wave_view->_shape) {
762 return floor (_wave_view->_height - (s * _wave_view->_height));
766 return floor ((1.0-s) * (_wave_view->_height / 2.0));
770 WaveView::CacheEntry::clear_image ()
776 WaveView::set_global_gradient_depth (double depth)
778 if (_global_gradient_depth != depth) {
779 _global_gradient_depth = depth;
780 VisualPropertiesChanged (); /* EMIT SIGNAL */