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 #define CACHE_HIGH_WATER (2)
46 std::map <boost::shared_ptr<AudioSource>, std::vector<WaveView::CacheEntry> > WaveView::_image_cache;
47 double WaveView::_global_gradient_depth = 0.6;
48 bool WaveView::_global_logscaled = false;
49 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
50 bool WaveView::_global_show_waveform_clipping = true;
51 double WaveView::_clip_level = 0.98853;
53 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
54 PBD::Signal0<void> WaveView::ClipLevelChanged;
56 WaveView::WaveView (Canvas* c, boost::shared_ptr<ARDOUR::AudioRegion> region)
60 , _samples_per_pixel (0)
63 , _zero_color (0xff0000ff)
64 , _clip_color (0xff0000ff)
65 , _logscaled (_global_logscaled)
66 , _shape (_global_shape)
67 , _gradient_depth (_global_gradient_depth)
68 , _shape_independent (false)
69 , _logscaled_independent (false)
70 , _gradient_depth_independent (false)
71 , _amplitude_above_axis (1.0)
72 , _region_amplitude (_region->scale_amplitude ())
73 , _region_start (region->start())
75 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
76 ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
79 WaveView::WaveView (Item* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
83 , _samples_per_pixel (0)
86 , _zero_color (0xff0000ff)
87 , _clip_color (0xff0000ff)
88 , _logscaled (_global_logscaled)
89 , _shape (_global_shape)
90 , _gradient_depth (_global_gradient_depth)
91 , _shape_independent (false)
92 , _logscaled_independent (false)
93 , _gradient_depth_independent (false)
94 , _amplitude_above_axis (1.0)
95 , _region_amplitude (_region->scale_amplitude ())
96 , _region_start (region->start())
98 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
99 ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
102 WaveView::~WaveView ()
104 invalidate_image_cache ();
108 WaveView::handle_visual_property_change ()
110 bool changed = false;
112 if (!_shape_independent && (_shape != global_shape())) {
113 _shape = global_shape();
117 if (!_logscaled_independent && (_logscaled != global_logscaled())) {
118 _logscaled = global_logscaled();
122 if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
123 _gradient_depth = global_gradient_depth();
128 begin_visual_change ();
129 invalidate_image_cache ();
130 end_visual_change ();
135 WaveView::handle_clip_level_change ()
137 begin_visual_change ();
138 invalidate_image_cache ();
139 end_visual_change ();
143 WaveView::set_fill_color (Color c)
145 if (c != _fill_color) {
146 begin_visual_change ();
147 invalidate_image_cache ();
148 Fill::set_fill_color (c);
149 end_visual_change ();
154 WaveView::set_outline_color (Color c)
156 if (c != _outline_color) {
157 begin_visual_change ();
158 invalidate_image_cache ();
159 Outline::set_outline_color (c);
160 end_visual_change ();
165 WaveView::set_samples_per_pixel (double samples_per_pixel)
167 if (samples_per_pixel != _samples_per_pixel) {
170 invalidate_image_cache ();
171 _samples_per_pixel = samples_per_pixel;
172 _bounding_box_dirty = true;
179 image_to_window (double wave_origin, double image_start)
181 return wave_origin + image_start;
185 window_to_image (double wave_origin, double image_start)
187 return image_start - wave_origin;
191 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
193 return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
197 alt_log_meter (float power)
199 return _log_meter (power, -192.0, 0.0, 8.0);
203 WaveView::set_clip_level (double dB)
205 const double clip_level = dB_to_coefficient (dB);
206 if (clip_level != _clip_level) {
207 _clip_level = clip_level;
213 WaveView::invalidate_image_cache ()
215 vector <uint32_t> deletion_list;
216 vector <CacheEntry> caches;
218 if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
219 caches = _image_cache.find (_region->audio_source ())->second;
224 for (uint32_t i = 0; i < caches.size (); ++i) {
226 if (_channel != caches[i].channel
227 || _height != caches[i].height
228 || _region_amplitude != caches[i].amplitude
229 || _fill_color != caches[i].fill_color
230 || _outline_color != caches[i].outline_color) {
235 deletion_list.push_back (i);
239 while (deletion_list.size() > 0) {
240 caches[deletion_list.back ()].image.clear ();
241 caches.erase (caches.begin() + deletion_list.back());
242 deletion_list.pop_back();
245 if (caches.size () == 0) {
246 _image_cache.erase(_region->audio_source ());
248 _image_cache[_region->audio_source ()] = caches;
254 WaveView::consolidate_image_cache () const
256 list <uint32_t> deletion_list;
257 vector <CacheEntry> caches;
258 uint32_t other_entries = 0;
260 if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
261 caches = _image_cache.find (_region->audio_source ())->second;
264 for (uint32_t i = 0; i < caches.size (); ++i) {
266 if (_channel != caches[i].channel
267 || _height != caches[i].height
268 || _region_amplitude != caches[i].amplitude
269 || _fill_color != caches[i].fill_color
270 || _outline_color != caches[i].outline_color) {
276 framepos_t segment_start = caches[i].start;
277 framepos_t segment_end = caches[i].end;
279 for (uint32_t j = i; j < caches.size (); ++j) {
281 if (i == j || _channel != caches[j].channel
282 || _height != caches[i].height
283 || _region_amplitude != caches[i].amplitude
284 || _fill_color != caches[i].fill_color
285 || _outline_color != caches[i].outline_color) {
290 if (caches[j].start >= segment_start && caches[j].end <= segment_end) {
292 deletion_list.push_back (j);
297 deletion_list.sort ();
298 deletion_list.unique ();
300 while (deletion_list.size() > 0) {
301 caches[deletion_list.back ()].image.clear ();
302 caches.erase (caches.begin() + deletion_list.back ());
303 deletion_list.pop_back();
306 /* We don't care if this channel/height/amplitude has anything in the cache - just drop the Last Added entries
307 until we reach a size where there is a maximum of CACHE_HIGH_WATER + other entries.
310 while (caches.size() > CACHE_HIGH_WATER + other_entries) {
311 caches.front ().image.clear ();
312 caches.erase(caches.begin ());
315 if (caches.size () == 0) {
316 _image_cache.erase (_region->audio_source ());
318 _image_cache[_region->audio_source ()] = caches;
323 WaveView::y_extent (double s, bool round_to_lower_edge) const
325 /* it is important that this returns an integral value, so that we
326 can ensure correct single pixel behaviour.
333 if (round_to_lower_edge) {
334 pos = ceil (_height - (s * _height));
336 pos = floor (_height - (s * _height));
340 if (round_to_lower_edge) {
341 pos = ceil ((1.0-s) * (_height/2.0));
343 pos = floor ((1.0-s) * (_height/2.0));
348 return min (_height, (max (0.0, pos)));
358 LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
362 WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks) const
364 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
365 boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
367 /* Clip level nominally set to -0.9dBFS to account for inter-sample
368 interpolation possibly clipping (value may be too low).
370 We adjust by the region's own gain (but note: not by any gain
371 automation or its gain envelope) so that clip indicators are closer
372 to providing data about on-disk data. This multiplication is
373 needed because the data we get from AudioRegion::read_peaks()
374 has been scaled by scale_amplitude() already.
377 const double clip_level = _clip_level * _region_amplitude;
379 if (_shape == WaveView::Rectified) {
381 /* each peak is a line from the bottom of the waveview
382 * to a point determined by max (_peaks[i].max,
387 for (int i = 0; i < n_peaks; ++i) {
389 tips[i].bot = height();
390 const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
391 tips[i].top = y_extent (p, false);
392 tips[i].spread = (1.0 - p) * _height;
394 if (fabs (_peaks[i].max) >= clip_level) {
395 tips[i].clip_max = true;
398 if (fabs (_peaks[i].min) >= clip_level) {
399 tips[i].clip_min = true;
403 } else {for (int i = 0; i < n_peaks; ++i) {
405 tips[i].bot = height();
406 tips[i].top = y_extent (max (fabs (_peaks[i].max), fabs (_peaks[i].min)), true);
407 tips[i].spread = (1.0 - fabs(_peaks[i].max - _peaks[i].min)) * _height;
409 if (fabs (_peaks[i].max) >= clip_level) {
410 tips[i].clip_max = true;
413 if (fabs (_peaks[i].min) >= clip_level) {
414 tips[i].clip_min = true;
422 for (int i = 0; i < n_peaks; ++i) {
423 double top = _peaks[i].min;
424 double bot = _peaks[i].max;
426 if (fabs (top) >= clip_level) {
427 tips[i].clip_max = true;
430 if (fabs (top) >= clip_level) {
431 tips[i].clip_min = true;
435 top = alt_log_meter (fast_coefficient_to_dB (top));
436 } else if (top < 0.0) {
437 top =-alt_log_meter (fast_coefficient_to_dB (-top));
443 bot = alt_log_meter (fast_coefficient_to_dB (bot));
444 } else if (bot < 0.0) {
445 bot = -alt_log_meter (fast_coefficient_to_dB (-bot));
450 tips[i].spread = fabs (((1.0 - top) * (_height/2.0)) - ((1.0 - bot) * _height/2.0));
451 tips[i].top = y_extent (top, false);
452 tips[i].bot = y_extent (bot, true);
456 for (int i = 0; i < n_peaks; ++i) {
458 if (fabs (_peaks[i].max) >= clip_level) {
459 tips[i].clip_max = true;
462 if (fabs (_peaks[i].min) >= clip_level) {
463 tips[i].clip_min = true;
466 tips[i].top = y_extent (_peaks[i].min, false);
467 tips[i].bot = y_extent (_peaks[i].max, true);
468 tips[i].spread = fabs (((1.0 - _peaks[i].max) * (_height/2.0)) - ((1.0 - _peaks[i].min) * (_height/2.0)));
474 if (gradient_depth() != 0.0) {
476 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _height));
482 if (_shape == Rectified) {
492 color_to_rgba (_fill_color, r, g, b, a);
493 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
494 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
496 /* generate a new color for the middle of the gradient */
498 color_to_hsv (_fill_color, h, s, v);
499 /* change v towards white */
500 v *= 1.0 - gradient_depth();
501 Color center = hsv_to_color (h, s, v, a);
502 color_to_rgba (center, r, g, b, a);
503 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
505 context->set_source (gradient);
507 set_source_rgba (context, _fill_color);
510 /* ensure single-pixel lines */
512 context->set_line_width (0.5);
513 context->translate (0.5, 0.0);
515 /* the height of the clip-indicator should be at most 7 pixels,
516 * or 5% of the height of the waveview item.
519 const double clip_height = min (7.0, ceil (_height * 0.05));
521 /* There are 3 possible components to draw at each x-axis position: the
522 waveform "line", the zero line and an outline/clip indicator. We
523 have to decide which of the 3 to draw at each position, pixel by
524 pixel. This makes the rendering less efficient but it is the only
525 way I can see to do this correctly.
527 With only 1 pixel of spread between the top and bottom of the line,
528 we just draw the upper outline/clip indicator.
530 With 2 pixels of spread, we draw the upper and lower outline clip
533 With 3 pixels of spread we draw the upper and lower outline/clip
534 indicators and at least 1 pixel of the waveform line.
536 With 5 pixels of spread, we draw all components.
538 We can do rectified as two separate passes because we have a much
539 easier decision regarding whether to draw the waveform line. We
540 always draw the clip/outline indicators.
543 if (_shape == WaveView::Rectified) {
545 for (int i = 0; i < n_peaks; ++i) {
549 if (tips[i].spread >= 2.0) {
550 context->move_to (i, tips[i].top);
551 context->line_to (i, tips[i].bot);
557 /* outline/clip indicators */
559 set_source_rgba (context, _outline_color);
561 for (int i = 0; i < n_peaks; ++i) {
563 context->move_to (i, tips[i].top);
565 if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) {
566 /* clip-indicating upper terminal line */
567 set_source_rgba (context, _clip_color);
568 context->rel_line_to (0, min (clip_height, floor (tips[i].spread)));
570 set_source_rgba (context, _outline_color);
572 /* normal upper terminal dot */
573 context->rel_line_to (0, 1.0);
580 /* Note the use of cairo save/restore pairs to retain the drawing
581 context for the waveform lines, which is already set
582 correctly when we reach here.
585 for (int i = 0; i < n_peaks; ++i) {
589 if (tips[i].spread >= 3.0) {
590 context->move_to (i, tips[i].top);
591 context->line_to (i, tips[i].bot);
597 if (tips[i].spread >= 5.0 && show_zero_line()) {
599 set_source_rgba (context, _zero_color);
600 context->move_to (i, _height/2.0);
601 context->rel_line_to (0, 0.5);
608 /* upper outline/clip indicator */
610 context->move_to (i, tips[i].top);
611 if (_global_show_waveform_clipping && ((_shape == WaveView::Rectified && (tips[i].clip_max || tips[i].clip_min)) || tips[i].clip_max)) {
612 /* clip-indicating upper terminal line */
613 set_source_rgba (context, _clip_color);
614 context->rel_line_to (0, min (clip_height, floor (tips[i].spread)));
617 /* normal upper terminal dot */
618 set_source_rgba (context, _outline_color);
619 context->rel_line_to (0, 1.0);
625 if (tips[i].spread >= 2.0) {
627 /* lower outline/clip indicator */
630 context->move_to (i, tips[i].bot);
631 if (_global_show_waveform_clipping && _shape != WaveView::Rectified && tips[i].clip_min) {
632 /* clip-indicating lower terminal line */
633 set_source_rgba (context, _clip_color);
634 context->rel_line_to (0, -(min (clip_height, floor (tips[i].spread))));
637 /* normal lower terminal dot */
638 set_source_rgba (context, _outline_color);
639 context->rel_line_to (0, -1.0);
650 WaveView::get_image (Cairo::RefPtr<Cairo::ImageSurface>& image, framepos_t start, framepos_t end, double& image_offset) const
652 vector <CacheEntry> caches;
654 if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
656 caches = _image_cache.find (_region->audio_source ())->second;
659 /* Find a suitable ImageSurface.
661 for (uint32_t i = 0; i < caches.size (); ++i) {
663 if (_channel != caches[i].channel
664 || _height != caches[i].height
665 || _region_amplitude != caches[i].amplitude
666 || _fill_color != caches[i].fill_color
667 || _outline_color != caches[i].outline_color) {
672 framepos_t segment_start = caches[i].start;
673 framepos_t segment_end = caches[i].end;
675 if (end <= segment_end && start >= segment_start) {
676 image_offset = (segment_start - _region->start()) / _samples_per_pixel;
677 image = caches[i].image;
683 consolidate_image_cache ();
685 /* sample position is canonical here, and we want to generate
686 * an image that spans about twice the canvas width
689 const framepos_t center = start + ((end - start) / 2);
690 const framecnt_t canvas_samples = _canvas->visible_area().width() * _samples_per_pixel; /* one canvas width */
692 /* we can request data from anywhere in the Source, between 0 and its length
695 framepos_t sample_start = max ((framepos_t) 0, (center - canvas_samples));
696 framepos_t sample_end = min (center + canvas_samples, _region->source_length (0));
698 const int n_peaks = llrintf ((sample_end - sample_start)/ (double) _samples_per_pixel);
700 boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
702 _region->read_peaks (peaks.get(), n_peaks,
703 sample_start, sample_end - sample_start,
707 image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, ((double)(sample_end - sample_start)) / _samples_per_pixel, _height);
709 draw_image (image, peaks.get(), n_peaks);
711 _image_cache[_region->audio_source ()].push_back (CacheEntry (_channel, _height, _region_amplitude, _fill_color, _outline_color, sample_start, sample_end, image));
713 image_offset = (sample_start - _region->start()) / _samples_per_pixel;
715 //cerr << "_image_cache size is : " << _image_cache.size() << " entries for this audiosource : " << _image_cache.find (_region->audio_source ())->second.size() << endl;
721 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
723 assert (_samples_per_pixel != 0);
729 Rect self = item_to_window (Rect (0.5, 0.0, _region->length() / _samples_per_pixel, _height));
730 boost::optional<Rect> d = self.intersection (area);
738 /* window coordinates - pixels where x=0 is the left edge of the canvas
739 * window. We round down in case we were asked to
740 * draw "between" pixels at the start and/or end.
743 const double draw_start = floor (draw.x0);
744 const double draw_end = floor (draw.x1);
746 // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
748 /* image coordnates: pixels where x=0 is the start of this waveview,
749 * wherever it may be positioned. thus image_start=N means "an image
750 * that beings N pixels after the start of region that this waveview is
754 const framepos_t image_start = window_to_image (self.x0, draw_start);
755 const framepos_t image_end = window_to_image (self.x0, draw_end);
757 // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
759 /* sample coordinates - note, these are not subject to rounding error */
760 framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
761 framepos_t sample_end = _region_start + (image_end * _samples_per_pixel);
763 // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl;
765 Cairo::RefPtr<Cairo::ImageSurface> image;
766 double image_offset = 0;
768 get_image (image, sample_start, sample_end, image_offset);
770 // cerr << "Offset into image to place at zero: " << image_offset << endl;
772 context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
774 /* round image origin position to an exact pixel in device space to
778 double x = self.x0 + image_offset;
780 context->user_to_device (x, y);
783 context->device_to_user (x, y);
785 context->set_source (image, x, y);
790 WaveView::compute_bounding_box () const
793 _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height);
795 _bounding_box = boost::optional<Rect> ();
798 _bounding_box_dirty = false;
802 WaveView::set_height (Distance height)
804 if (height != _height) {
807 invalidate_image_cache ();
810 _bounding_box_dirty = true;
816 WaveView::set_channel (int channel)
818 if (channel != _channel) {
821 invalidate_image_cache ();
824 _bounding_box_dirty = true;
830 WaveView::set_logscaled (bool yn)
832 if (_logscaled != yn) {
833 begin_visual_change ();
834 invalidate_image_cache ();
836 end_visual_change ();
841 WaveView::gain_changed ()
843 begin_visual_change ();
844 invalidate_image_cache ();
845 _region_amplitude = _region->scale_amplitude ();
846 end_visual_change ();
850 WaveView::set_zero_color (Color c)
852 if (_zero_color != c) {
853 begin_visual_change ();
854 invalidate_image_cache ();
856 end_visual_change ();
861 WaveView::set_clip_color (Color c)
863 if (_clip_color != c) {
864 begin_visual_change ();
865 invalidate_image_cache ();
867 end_visual_change ();
872 WaveView::set_show_zero_line (bool yn)
874 if (_show_zero != yn) {
875 begin_visual_change ();
876 invalidate_image_cache ();
878 end_visual_change ();
883 WaveView::set_shape (Shape s)
886 begin_visual_change ();
887 invalidate_image_cache ();
889 end_visual_change ();
894 WaveView::set_amplitude_above_axis (double a)
896 if (_amplitude_above_axis != a) {
897 begin_visual_change ();
898 invalidate_image_cache ();
899 _amplitude_above_axis = a;
900 end_visual_change ();
905 WaveView::set_global_shape (Shape s)
907 if (_global_shape != s) {
909 VisualPropertiesChanged (); /* EMIT SIGNAL */
914 WaveView::set_global_logscaled (bool yn)
916 if (_global_logscaled != yn) {
917 _global_logscaled = yn;
918 VisualPropertiesChanged (); /* EMIT SIGNAL */
923 WaveView::set_region_start (frameoffset_t start)
929 if (_region_start == start) {
934 _region_start = start;
935 _bounding_box_dirty = true;
940 WaveView::region_resized ()
942 /* Called when the region start or end (thus length) has changed.
950 _region_start = _region->start();
951 _bounding_box_dirty = true;
956 WaveView::set_global_gradient_depth (double depth)
958 if (_global_gradient_depth != depth) {
959 _global_gradient_depth = depth;
960 VisualPropertiesChanged (); /* EMIT SIGNAL */
965 WaveView::set_global_show_waveform_clipping (bool yn)
967 if (_global_show_waveform_clipping != yn) {
968 _global_show_waveform_clipping = yn;
969 VisualPropertiesChanged (); /* EMIT SIGNAL */