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"
37 #include "canvas/colors.h"
39 #include <gdkmm/general.h>
41 #include "gtk2_ardour/gui_thread.h"
44 using namespace ARDOUR;
45 using namespace ArdourCanvas;
47 #define CACHE_HIGH_WATER (2)
49 std::map <boost::shared_ptr<AudioSource>, std::vector<WaveView::CacheEntry> > WaveView::_image_cache;
50 double WaveView::_global_gradient_depth = 0.6;
51 bool WaveView::_global_logscaled = false;
52 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
53 bool WaveView::_global_show_waveform_clipping = true;
54 double WaveView::_clip_level = 0.98853;
56 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
57 PBD::Signal0<void> WaveView::ClipLevelChanged;
59 WaveView::WaveView (Canvas* c, boost::shared_ptr<ARDOUR::AudioRegion> region)
63 , _samples_per_pixel (0)
66 , _zero_color (0xff0000ff)
67 , _clip_color (0xff0000ff)
68 , _logscaled (_global_logscaled)
69 , _shape (_global_shape)
70 , _gradient_depth (_global_gradient_depth)
71 , _shape_independent (false)
72 , _logscaled_independent (false)
73 , _gradient_depth_independent (false)
74 , _amplitude_above_axis (1.0)
75 , _region_amplitude (_region->scale_amplitude ())
77 , _region_start (region->start())
79 _region->DropReferences.connect (_source_invalidated_connection, MISSING_INVALIDATOR,
80 boost::bind (&ArdourCanvas::WaveView::invalidate_source,
81 this, boost::weak_ptr<AudioSource>(_region->audio_source())), gui_context());
83 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
84 ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
87 WaveView::WaveView (Item* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
91 , _samples_per_pixel (0)
94 , _zero_color (0xff0000ff)
95 , _clip_color (0xff0000ff)
96 , _logscaled (_global_logscaled)
97 , _shape (_global_shape)
98 , _gradient_depth (_global_gradient_depth)
99 , _shape_independent (false)
100 , _logscaled_independent (false)
101 , _gradient_depth_independent (false)
102 , _amplitude_above_axis (1.0)
103 , _region_amplitude (_region->scale_amplitude ())
104 , _region_start (region->start())
106 _region->DropReferences.connect (_source_invalidated_connection, MISSING_INVALIDATOR,
107 boost::bind (&ArdourCanvas::WaveView::invalidate_source,
108 this, boost::weak_ptr<AudioSource>(_region->audio_source())), gui_context());
110 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
111 ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
114 WaveView::~WaveView ()
116 _source_invalidated_connection.disconnect();
117 invalidate_image_cache ();
121 WaveView::handle_visual_property_change ()
123 bool changed = false;
125 if (!_shape_independent && (_shape != global_shape())) {
126 _shape = global_shape();
130 if (!_logscaled_independent && (_logscaled != global_logscaled())) {
131 _logscaled = global_logscaled();
135 if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
136 _gradient_depth = global_gradient_depth();
141 begin_visual_change ();
142 invalidate_image_cache ();
143 end_visual_change ();
148 WaveView::handle_clip_level_change ()
150 begin_visual_change ();
151 invalidate_image_cache ();
152 end_visual_change ();
156 WaveView::set_fill_color (Color c)
158 if (c != _fill_color) {
159 begin_visual_change ();
160 invalidate_image_cache ();
161 Fill::set_fill_color (c);
162 end_visual_change ();
167 WaveView::set_outline_color (Color c)
169 if (c != _outline_color) {
170 begin_visual_change ();
171 invalidate_image_cache ();
172 Outline::set_outline_color (c);
173 end_visual_change ();
178 WaveView::set_samples_per_pixel (double samples_per_pixel)
180 if (samples_per_pixel != _samples_per_pixel) {
183 invalidate_image_cache ();
184 _samples_per_pixel = samples_per_pixel;
185 _bounding_box_dirty = true;
192 window_to_image (double wave_origin, double image_start)
194 return image_start - wave_origin;
198 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
200 return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
204 alt_log_meter (float power)
206 return _log_meter (power, -192.0, 0.0, 8.0);
210 WaveView::set_clip_level (double dB)
212 const double clip_level = dB_to_coefficient (dB);
213 if (clip_level != _clip_level) {
214 _clip_level = clip_level;
220 WaveView::invalidate_source (boost::weak_ptr<AudioSource> src)
222 if (boost::shared_ptr<AudioSource> source = src.lock()) {
224 std::map <boost::shared_ptr<ARDOUR::AudioSource>, std::vector <CacheEntry> >::iterator i;
225 for (i = _image_cache.begin (); i != _image_cache.end (); ++i) {
226 if (i->first == source) {
227 for (uint32_t n = 0; n < i->second.size (); ++n) {
228 i->second[n].image.clear ();
231 _image_cache.erase (i->first);
238 WaveView::invalidate_image_cache ()
240 vector <uint32_t> deletion_list;
241 vector <CacheEntry> caches;
243 /* The source may have disappeared.*/
245 if (_region->n_channels() == 0) {
249 if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
250 caches = _image_cache.find (_region->audio_source ())->second;
255 for (uint32_t i = 0; i < caches.size (); ++i) {
257 if (_channel != caches[i].channel
258 || _height != caches[i].height
259 || _region_amplitude != caches[i].amplitude
260 || _fill_color != caches[i].fill_color) {
265 deletion_list.push_back (i);
268 while (deletion_list.size() > 0) {
269 caches[deletion_list.back ()].image.clear ();
270 caches.erase (caches.begin() + deletion_list.back());
271 deletion_list.pop_back();
274 if (caches.size () == 0) {
275 _image_cache.erase(_region->audio_source ());
277 _image_cache[_region->audio_source ()] = caches;
282 WaveView::consolidate_image_cache () const
284 list <uint32_t> deletion_list;
285 vector <CacheEntry> caches;
286 uint32_t other_entries = 0;
288 if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
289 caches = _image_cache.find (_region->audio_source ())->second;
292 for (uint32_t i = 0; i < caches.size (); ++i) {
294 if (_channel != caches[i].channel
295 || _height != caches[i].height
296 || _region_amplitude != caches[i].amplitude
297 || _fill_color != caches[i].fill_color) {
303 framepos_t segment_start = caches[i].start;
304 framepos_t segment_end = caches[i].end;
306 for (uint32_t j = i; j < caches.size (); ++j) {
308 if (i == j || _channel != caches[j].channel
309 || _height != caches[i].height
310 || _region_amplitude != caches[i].amplitude
311 || _fill_color != caches[i].fill_color) {
316 if (caches[j].start >= segment_start && caches[j].end <= segment_end) {
318 deletion_list.push_back (j);
323 deletion_list.sort ();
324 deletion_list.unique ();
326 while (deletion_list.size() > 0) {
327 caches[deletion_list.back ()].image.clear ();
328 caches.erase (caches.begin() + deletion_list.back ());
329 deletion_list.pop_back();
332 /* We don't care if this channel/height/amplitude has anything in the cache - just drop the Last Added entries
333 until we reach a size where there is a maximum of CACHE_HIGH_WATER + other entries.
336 while (caches.size() > CACHE_HIGH_WATER + other_entries) {
337 caches.front ().image.clear ();
338 caches.erase(caches.begin ());
341 if (caches.size () == 0) {
342 _image_cache.erase (_region->audio_source ());
344 _image_cache[_region->audio_source ()] = caches;
349 WaveView::y_extent (double s, bool /*round_to_lower_edge*/) const
351 /* it is important that this returns an integral value, so that we
352 * can ensure correct single pixel behaviour.
354 * we need (_height - max(wave_line_width))
355 * wave_line_width == 1 IFF top==bottom (1 sample per pixel or flat line)
356 * wave_line_width == 2 otherwise
357 * then round away from the zero line, towards peak
359 if (_shape == Rectified) {
360 // we only ever have 1 point and align to the bottom (not center)
361 return floor ((1.0 - s) * (_height - 2.0));
363 /* currently canvas rectangle is off-by-one and we
364 * cannot draw a pixel at 0 (-.5 .. +.5) without it being
365 * clipped. A value 1.0 (ideally one point at y=0) ends
366 * up a pixel down. and a value of -1.0 (ideally y = _height-1)
367 * currently is on the bottom separator line :(
368 * So to make the complete waveform appear centered in
369 * a region, we translate by +.5 (instead of -.5)
370 * and waste two pixel of height: -4 (instad of -2)
372 * This needs fixing in canvas/rectangle the intersect
373 * functions and probably a couple of other places as well...
377 pos = ceil ((1.0 - s) * .5 * (_height - 4.0));
379 pos = floor ((1.0 - s) * .5 * (_height - 4.0));
381 return min (_height - 4.0, (max (0.0, pos)));
386 WaveView::draw_absent_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks) const
388 Cairo::RefPtr<Cairo::ImageSurface> stripe = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
390 Cairo::RefPtr<Cairo::Context> stripe_context = Cairo::Context::create (stripe);
391 stripe_context->set_antialias (Cairo::ANTIALIAS_NONE);
393 uint32_t stripe_separation = 150;
394 double start = - floor (_height / stripe_separation) * stripe_separation;
397 while (start < n_peaks) {
399 stripe_context->move_to (start, 0);
400 stripe_x = start + _height;
401 stripe_context->line_to (stripe_x, _height);
402 start += stripe_separation;
405 stripe_context->set_source_rgba (1.0, 1.0, 1.0, 1.0);
406 stripe_context->set_line_cap (Cairo::LINE_CAP_SQUARE);
407 stripe_context->set_line_width(50);
408 stripe_context->stroke();
410 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
412 context->set_source_rgba (1.0, 1.0, 0.0, 0.3);
413 context->mask (stripe, 0, 0);
424 LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
428 Cairo::RefPtr<Cairo::ImageSurface> wave;
429 Cairo::RefPtr<Cairo::ImageSurface> outline;
430 Cairo::RefPtr<Cairo::ImageSurface> clip;
431 Cairo::RefPtr<Cairo::ImageSurface> zero;
434 wave (0), outline (0), clip (0), zero (0) {}
438 WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks) const
443 images.wave = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
444 images.outline = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
445 images.clip = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
446 images.zero = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
448 Cairo::RefPtr<Cairo::Context> wave_context = Cairo::Context::create (images.wave);
449 Cairo::RefPtr<Cairo::Context> outline_context = Cairo::Context::create (images.outline);
450 Cairo::RefPtr<Cairo::Context> clip_context = Cairo::Context::create (images.clip);
451 Cairo::RefPtr<Cairo::Context> zero_context = Cairo::Context::create (images.zero);
452 wave_context->set_antialias (Cairo::ANTIALIAS_NONE);
453 outline_context->set_antialias (Cairo::ANTIALIAS_NONE);
454 clip_context->set_antialias (Cairo::ANTIALIAS_NONE);
455 zero_context->set_antialias (Cairo::ANTIALIAS_NONE);
457 boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
459 /* Clip level nominally set to -0.9dBFS to account for inter-sample
460 interpolation possibly clipping (value may be too low).
462 We adjust by the region's own gain (but note: not by any gain
463 automation or its gain envelope) so that clip indicators are closer
464 to providing data about on-disk data. This multiplication is
465 needed because the data we get from AudioRegion::read_peaks()
466 has been scaled by scale_amplitude() already.
469 const double clip_level = _clip_level * _region_amplitude;
471 if (_shape == WaveView::Rectified) {
473 /* each peak is a line from the bottom of the waveview
474 * to a point determined by max (_peaks[i].max,
479 for (int i = 0; i < n_peaks; ++i) {
481 tips[i].bot = height() - 1.0;
482 const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
483 tips[i].top = y_extent (p, false);
484 tips[i].spread = p * (_height - 1.0);
486 if (_peaks[i].max >= clip_level) {
487 tips[i].clip_max = true;
490 if (-(_peaks[i].min) >= clip_level) {
491 tips[i].clip_min = true;
496 for (int i = 0; i < n_peaks; ++i) {
498 tips[i].bot = height() - 1.0;
499 const double p = max(fabs (_peaks[i].max), fabs (_peaks[i].min));
500 tips[i].top = y_extent (p, false);
501 tips[i].spread = p * (_height - 2.0);
502 if (p >= clip_level) {
503 tips[i].clip_max = true;
512 for (int i = 0; i < n_peaks; ++i) {
513 double top = _peaks[i].max;
514 double bot = _peaks[i].min;
516 if (_peaks[i].max >= clip_level) {
517 tips[i].clip_max = true;
519 if (-(_peaks[i].min) >= clip_level) {
520 tips[i].clip_min = true;
524 top = alt_log_meter (fast_coefficient_to_dB (top));
525 } else if (top < 0.0) {
526 top =-alt_log_meter (fast_coefficient_to_dB (-top));
532 bot = alt_log_meter (fast_coefficient_to_dB (bot));
533 } else if (bot < 0.0) {
534 bot = -alt_log_meter (fast_coefficient_to_dB (-bot));
539 tips[i].top = y_extent (top, false);
540 tips[i].bot = y_extent (bot, true);
541 tips[i].spread = tips[i].bot - tips[i].top;
545 for (int i = 0; i < n_peaks; ++i) {
546 if (_peaks[i].max >= clip_level) {
547 tips[i].clip_max = true;
549 if (-(_peaks[i].min) >= clip_level) {
550 tips[i].clip_min = true;
553 tips[i].top = y_extent (_peaks[i].max, false);
554 tips[i].bot = y_extent (_peaks[i].min, true);
555 tips[i].spread = tips[i].bot - tips[i].top;
560 Color alpha_one = rgba_to_color (0, 0, 0, 1.0);
562 set_source_rgba (wave_context, alpha_one);
563 set_source_rgba (outline_context, alpha_one);
564 set_source_rgba (clip_context, alpha_one);
565 set_source_rgba (zero_context, alpha_one);
567 /* ensure single-pixel lines */
569 wave_context->set_line_width (1.0);
570 wave_context->translate (0.5, +0.5);
572 outline_context->set_line_width (1.0);
573 outline_context->translate (0.5, +0.5);
575 clip_context->set_line_width (1.0);
576 clip_context->translate (0.5, +0.5);
578 zero_context->set_line_width (1.0);
579 zero_context->translate (0.5, +0.5);
581 /* the height of the clip-indicator should be at most 7 pixels,
582 * or 5% of the height of the waveview item.
585 const double clip_height = min (7.0, ceil (_height * 0.05));
586 bool draw_outline_as_wave = false;
588 /* There are 3 possible components to draw at each x-axis position: the
589 waveform "line", the zero line and an outline/clip indicator. We
590 have to decide which of the 3 to draw at each position, pixel by
591 pixel. This makes the rendering less efficient but it is the only
592 way I can see to do this correctly.
594 To avoid constant source swapping and stroking, we draw the components separately
595 onto four alpha only image surfaces for use as a mask.
597 With only 1 pixel of spread between the top and bottom of the line,
598 we just draw the upper outline/clip indicator.
600 With 2 pixels of spread, we draw the upper and lower outline clip
603 With 3 pixels of spread we draw the upper and lower outline/clip
604 indicators and at least 1 pixel of the waveform line.
606 With 5 pixels of spread, we draw all components.
608 We can do rectified as two separate passes because we have a much
609 easier decision regarding whether to draw the waveform line. We
610 always draw the clip/outline indicators.
613 if (_shape == WaveView::Rectified) {
615 for (int i = 0; i < n_peaks; ++i) {
619 if (tips[i].spread >= 1.0) {
620 wave_context->move_to (i, tips[i].top);
621 wave_context->line_to (i, tips[i].bot);
624 if (_global_show_waveform_clipping && (tips[i].clip_max)) {
625 clip_context->move_to (i, tips[i].top);
626 /* clip-indicating upper terminal line */
627 clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + .5)));
629 outline_context->move_to (i, tips[i].top);
630 /* normal upper terminal dot */
631 outline_context->close_path ();
635 wave_context->stroke ();
636 clip_context->stroke ();
637 outline_context->stroke ();
640 const double height_2 = (_height - 4.0) * .5;
642 for (int i = 0; i < n_peaks; ++i) {
646 if (tips[i].spread >= 2.0) {
647 wave_context->move_to (i, tips[i].top);
648 wave_context->line_to (i, tips[i].bot);
650 /* draw square waves and other discontiguous points clearly */
652 if (tips[i-1].top + 2 < tips[i].top) {
653 wave_context->move_to (i-1, tips[i-1].top);
654 wave_context->line_to (i-1, (tips[i].bot + tips[i-1].top)/2);
655 wave_context->move_to (i, (tips[i].bot + tips[i-1].top)/2);
656 wave_context->line_to (i, tips[i].top);
657 } else if (tips[i-1].bot > tips[i].bot + 2) {
658 wave_context->move_to (i-1, tips[i-1].bot);
659 wave_context->line_to (i-1, (tips[i].top + tips[i-1].bot)/2);
660 wave_context->move_to (i, (tips[i].top + tips[i-1].bot)/2);
661 wave_context->line_to (i, tips[i].bot);
667 if (tips[i].spread >= 5.0 && show_zero_line()) {
668 zero_context->move_to (i, floor(height_2));
669 zero_context->rel_line_to (1.0, 0);
672 if (tips[i].spread > 1.0) {
673 draw_outline_as_wave = false;
674 /* lower outline/clip indicator */
675 if (_global_show_waveform_clipping && tips[i].clip_min) {
676 clip_context->move_to (i, tips[i].bot);
677 /* clip-indicating lower terminal line */
678 const double sign = tips[i].bot > height_2 ? -1 : 1;
679 clip_context->rel_line_to (0, sign * min (clip_height, ceil (tips[i].spread + .5)));
681 outline_context->move_to (i, tips[i].bot + 0.5);
682 /* normal lower terminal dot */
683 outline_context->rel_line_to (0, -0.5);
686 draw_outline_as_wave = true;
687 if (tips[i].clip_min) {
688 // make sure we draw the clip
689 tips[i].clip_max = true;
693 /* upper outline/clip indicator */
694 if (_global_show_waveform_clipping && tips[i].clip_max) {
695 clip_context->move_to (i, tips[i].top);
696 /* clip-indicating upper terminal line */
697 const double sign = tips[i].top > height_2 ? -1 : 1;
698 clip_context->rel_line_to (0, sign * min(clip_height, ceil(tips[i].spread + .5)));
700 if (draw_outline_as_wave) {
701 wave_context->move_to (i, tips[i].top + 0.5);
702 /* special case where outline only is drawn.
703 is this correct? too short by 0.5?
705 wave_context->rel_line_to (0, -0.5);
707 outline_context->move_to (i, tips[i].top + 0.5);
708 /* normal upper terminal dot */
709 outline_context->rel_line_to (0, -0.5);
714 wave_context->stroke ();
715 outline_context->stroke ();
716 clip_context->stroke ();
717 zero_context->stroke ();
720 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
722 /* Here we set a source colour and use the various components as a mask. */
724 if (gradient_depth() != 0.0) {
726 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _height));
732 if (_shape == Rectified) {
742 color_to_rgba (_fill_color, r, g, b, a);
743 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
744 /* generate a new color for the middle of the gradient */
746 color_to_hsv (_fill_color, h, s, v);
747 /* change v towards white */
748 v *= 1.0 - gradient_depth();
749 Color center = hsva_to_color (h, s, v, a);
750 color_to_rgba (center, r, g, b, a);
752 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
753 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
755 context->set_source (gradient);
757 set_source_rgba (context, _fill_color);
760 context->mask (images.wave, 0, 0);
763 set_source_rgba (context, _outline_color);
764 context->mask (images.outline, 0, 0);
767 set_source_rgba (context, _clip_color);
768 context->mask (images.clip, 0, 0);
771 set_source_rgba (context, _zero_color);
772 context->mask (images.zero, 0, 0);
778 WaveView::get_image (Cairo::RefPtr<Cairo::ImageSurface>& image, framepos_t start, framepos_t end, double& image_offset) const
780 vector <CacheEntry> caches;
782 if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
784 caches = _image_cache.find (_region->audio_source ())->second;
787 /* Find a suitable ImageSurface.
789 for (uint32_t i = 0; i < caches.size (); ++i) {
791 if (_channel != caches[i].channel
792 || _height != caches[i].height
793 || _region_amplitude != caches[i].amplitude
794 || _fill_color != caches[i].fill_color) {
799 framepos_t segment_start = caches[i].start;
800 framepos_t segment_end = caches[i].end;
802 if (end <= segment_end && start >= segment_start) {
803 image_offset = (segment_start - _region_start) / _samples_per_pixel;
804 image = caches[i].image;
810 consolidate_image_cache ();
812 /* sample position is canonical here, and we want to generate
813 * an image that spans about twice the canvas width
816 const framepos_t center = start + ((end - start) / 2);
817 const framecnt_t canvas_samples = _canvas->visible_area().width() * _samples_per_pixel; /* one canvas width */
819 /* we can request data from anywhere in the Source, between 0 and its length
822 framepos_t sample_start = max ((framepos_t) 0, (center - canvas_samples));
823 framepos_t sample_end = min (center + canvas_samples, _region->source_length (0));
825 const int n_peaks = llrintf ((sample_end - sample_start)/ (double) _samples_per_pixel);
827 boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
829 framecnt_t peaks_read;
830 peaks_read = _region->read_peaks (peaks.get(), n_peaks,
831 sample_start, sample_end - sample_start,
835 image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, _height);
837 if (peaks_read > 0) {
838 draw_image (image, peaks.get(), n_peaks);
840 draw_absent_image (image, peaks.get(), n_peaks);
843 _image_cache[_region->audio_source ()].push_back (CacheEntry (_channel, _height, _region_amplitude, _fill_color, sample_start, sample_end, image));
845 image_offset = (sample_start - _region->start()) / _samples_per_pixel;
847 //cerr << "_image_cache size is : " << _image_cache.size() << " entries for this audiosource : " << _image_cache.find (_region->audio_source ())->second.size() << endl;
853 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
855 assert (_samples_per_pixel != 0);
861 Rect self = item_to_window (Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height));
862 boost::optional<Rect> d = self.intersection (area);
870 /* window coordinates - pixels where x=0 is the left edge of the canvas
871 * window. We round down in case we were asked to
872 * draw "between" pixels at the start and/or end.
875 double draw_start = floor (draw.x0);
876 const double draw_end = floor (draw.x1);
878 // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
880 /* image coordnates: pixels where x=0 is the start of this waveview,
881 * wherever it may be positioned. thus image_start=N means "an image
882 * that beings N pixels after the start of region that this waveview is
886 const framepos_t image_start = window_to_image (self.x0, draw_start);
887 const framepos_t image_end = window_to_image (self.x0, draw_end);
889 // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
891 /* sample coordinates - note, these are not subject to rounding error */
892 framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
893 framepos_t sample_end = _region_start + (image_end * _samples_per_pixel);
895 // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl;
897 Cairo::RefPtr<Cairo::ImageSurface> image;
898 double image_offset = 0;
900 get_image (image, sample_start, sample_end, image_offset);
902 // cerr << "Offset into image to place at zero: " << image_offset << endl;
904 if (_start_shift && (sample_start == _region_start) && (self.x0 == draw.x0)) {
905 /* we are going to draw the first pixel for this region, but
906 we may not want this to overlap a border around the
907 waveform. If so, _start_shift will be set.
909 //cerr << name.substr (23) << " ss = " << sample_start << " rs = " << _region_start << " sf = " << _start_shift << " ds = " << draw_start << " self = " << self << " draw = " << draw << endl;
910 //draw_start += _start_shift;
911 //image_offset += _start_shift;
914 context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
916 /* round image origin position to an exact pixel in device space to
920 double x = self.x0 + image_offset;
922 context->user_to_device (x, y);
925 context->device_to_user (x, y);
927 context->set_source (image, x, y);
933 WaveView::compute_bounding_box () const
936 _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height);
938 _bounding_box = boost::optional<Rect> ();
941 _bounding_box_dirty = false;
945 WaveView::set_height (Distance height)
947 if (height != _height) {
950 invalidate_image_cache ();
953 _bounding_box_dirty = true;
959 WaveView::set_channel (int channel)
961 if (channel != _channel) {
964 invalidate_image_cache ();
967 _bounding_box_dirty = true;
973 WaveView::set_logscaled (bool yn)
975 if (_logscaled != yn) {
976 begin_visual_change ();
977 invalidate_image_cache ();
979 end_visual_change ();
984 WaveView::gain_changed ()
986 begin_visual_change ();
987 invalidate_image_cache ();
988 _region_amplitude = _region->scale_amplitude ();
989 end_visual_change ();
993 WaveView::set_zero_color (Color c)
995 if (_zero_color != c) {
996 begin_visual_change ();
997 invalidate_image_cache ();
999 end_visual_change ();
1004 WaveView::set_clip_color (Color c)
1006 if (_clip_color != c) {
1007 begin_visual_change ();
1008 invalidate_image_cache ();
1010 end_visual_change ();
1015 WaveView::set_show_zero_line (bool yn)
1017 if (_show_zero != yn) {
1018 begin_visual_change ();
1019 invalidate_image_cache ();
1021 end_visual_change ();
1026 WaveView::set_shape (Shape s)
1029 begin_visual_change ();
1030 invalidate_image_cache ();
1032 end_visual_change ();
1037 WaveView::set_amplitude_above_axis (double a)
1039 if (_amplitude_above_axis != a) {
1040 begin_visual_change ();
1041 invalidate_image_cache ();
1042 _amplitude_above_axis = a;
1043 end_visual_change ();
1048 WaveView::set_global_shape (Shape s)
1050 if (_global_shape != s) {
1052 VisualPropertiesChanged (); /* EMIT SIGNAL */
1057 WaveView::set_global_logscaled (bool yn)
1059 if (_global_logscaled != yn) {
1060 _global_logscaled = yn;
1061 VisualPropertiesChanged (); /* EMIT SIGNAL */
1066 WaveView::set_region_start (frameoffset_t start)
1072 if (_region_start == start) {
1077 _region_start = start;
1078 _bounding_box_dirty = true;
1083 WaveView::region_resized ()
1085 /* Called when the region start or end (thus length) has changed.
1093 _region_start = _region->start();
1094 _bounding_box_dirty = true;
1099 WaveView::set_global_gradient_depth (double depth)
1101 if (_global_gradient_depth != depth) {
1102 _global_gradient_depth = depth;
1103 VisualPropertiesChanged (); /* EMIT SIGNAL */
1108 WaveView::set_global_show_waveform_clipping (bool yn)
1110 if (_global_show_waveform_clipping != yn) {
1111 _global_show_waveform_clipping = yn;
1112 ClipLevelChanged ();
1117 WaveView::set_start_shift (double pixels)
1123 begin_visual_change ();
1124 _start_shift = pixels;
1125 end_visual_change ();