Remove dead code.
[ardour.git] / libs / canvas / wave_view.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
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.
9
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.
14
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.
18
19 */
20
21 #include <cmath>
22 #include <cairomm/cairomm.h>
23
24 #include "gtkmm2ext/utils.h"
25
26 #include "pbd/compose.h"
27 #include "pbd/signals.h"
28 #include "pbd/stacktrace.h"
29
30 #include "ardour/types.h"
31 #include "ardour/dB.h"
32 #include "ardour/audioregion.h"
33
34 #include "canvas/wave_view.h"
35 #include "canvas/utils.h"
36 #include "canvas/canvas.h"
37 #include "canvas/colors.h"
38
39 #include <gdkmm/general.h>
40
41 using namespace std;
42 using namespace ARDOUR;
43 using namespace ArdourCanvas;
44
45 #define CACHE_HIGH_WATER (2)
46
47 std::map <boost::shared_ptr<AudioSource>, std::vector<WaveView::CacheEntry> >  WaveView::_image_cache;
48 double WaveView::_global_gradient_depth = 0.6;
49 bool WaveView::_global_logscaled = false;
50 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
51 bool WaveView::_global_show_waveform_clipping = true;
52 double WaveView::_clip_level = 0.98853;
53
54 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
55 PBD::Signal0<void> WaveView::ClipLevelChanged;
56
57 WaveView::WaveView (Canvas* c, boost::shared_ptr<ARDOUR::AudioRegion> region)
58         : Item (c)
59         , _region (region)
60         , _channel (0)
61         , _samples_per_pixel (0)
62         , _height (64)
63         , _show_zero (false)
64         , _zero_color (0xff0000ff)
65         , _clip_color (0xff0000ff)
66         , _logscaled (_global_logscaled)
67         , _shape (_global_shape)
68         , _gradient_depth (_global_gradient_depth)
69         , _shape_independent (false)
70         , _logscaled_independent (false)
71         , _gradient_depth_independent (false)
72         , _amplitude_above_axis (1.0)
73         , _region_amplitude (_region->scale_amplitude ())
74         , _region_start (region->start())
75 {
76         VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
77         ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
78 }
79
80 WaveView::WaveView (Item* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
81         : Item (parent)
82         , _region (region)
83         , _channel (0)
84         , _samples_per_pixel (0)
85         , _height (64)
86         , _show_zero (false)
87         , _zero_color (0xff0000ff)
88         , _clip_color (0xff0000ff)
89         , _logscaled (_global_logscaled)
90         , _shape (_global_shape)
91         , _gradient_depth (_global_gradient_depth)
92         , _shape_independent (false)
93         , _logscaled_independent (false)
94         , _gradient_depth_independent (false)
95         , _amplitude_above_axis (1.0)
96         , _region_amplitude (_region->scale_amplitude ())
97         , _region_start (region->start())
98 {
99         VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
100         ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
101 }
102
103 WaveView::~WaveView ()
104 {
105         invalidate_image_cache ();
106 }
107
108 void
109 WaveView::handle_visual_property_change ()
110 {
111         bool changed = false;
112
113         if (!_shape_independent && (_shape != global_shape())) {
114                 _shape = global_shape();
115                 changed = true;
116         }
117
118         if (!_logscaled_independent && (_logscaled != global_logscaled())) {
119                 _logscaled = global_logscaled();
120                 changed = true;
121         }
122
123         if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
124                 _gradient_depth = global_gradient_depth();
125                 changed = true;
126         }
127
128         if (changed) {
129                 begin_visual_change ();
130                 invalidate_image_cache ();
131                 end_visual_change ();
132         }
133 }
134
135 void
136 WaveView::handle_clip_level_change ()
137 {
138         begin_visual_change ();
139         invalidate_image_cache ();
140         end_visual_change ();
141 }
142
143 void
144 WaveView::set_fill_color (Color c)
145 {
146         if (c != _fill_color) {
147                 begin_visual_change ();
148                 invalidate_image_cache ();
149                 Fill::set_fill_color (c);
150                 end_visual_change ();
151         }
152 }
153
154 void
155 WaveView::set_outline_color (Color c)
156 {
157         if (c != _outline_color) {
158                 begin_visual_change ();
159                 invalidate_image_cache ();
160                 Outline::set_outline_color (c);
161                 end_visual_change ();
162         }
163 }
164
165 void
166 WaveView::set_samples_per_pixel (double samples_per_pixel)
167 {
168         if (samples_per_pixel != _samples_per_pixel) {
169                 begin_change ();
170
171                 invalidate_image_cache ();
172                 _samples_per_pixel = samples_per_pixel;
173                 _bounding_box_dirty = true;
174
175                 end_change ();
176         }
177 }
178
179 static inline double
180 window_to_image (double wave_origin, double image_start)
181 {
182         return image_start - wave_origin;
183 }
184
185 static inline float
186 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
187 {
188         return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
189 }
190
191 static inline float
192 alt_log_meter (float power)
193 {
194         return _log_meter (power, -192.0, 0.0, 8.0);
195 }
196
197 void
198 WaveView::set_clip_level (double dB)
199 {
200         const double clip_level = dB_to_coefficient (dB);
201         if (clip_level != _clip_level) {
202                 _clip_level = clip_level;
203                 ClipLevelChanged ();
204         }
205 }
206
207 void
208 WaveView::invalidate_image_cache ()
209 {
210         vector <uint32_t> deletion_list;
211         vector <CacheEntry> caches;
212
213         if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
214                 caches = _image_cache.find (_region->audio_source ())->second;
215         } else {
216                 return;
217         }
218
219         for (uint32_t i = 0; i < caches.size (); ++i) {
220
221                 if (_channel != caches[i].channel
222                     || _height != caches[i].height
223                     || _region_amplitude != caches[i].amplitude
224                     || _fill_color != caches[i].fill_color) {
225
226                         continue;
227                 }
228
229                 deletion_list.push_back (i);
230
231         }
232
233         while (deletion_list.size() > 0) {
234                 caches[deletion_list.back ()].image.clear ();
235                 caches.erase (caches.begin() + deletion_list.back());
236                 deletion_list.pop_back();
237         }
238
239         if (caches.size () == 0) {
240                 _image_cache.erase(_region->audio_source ());
241         } else {
242                 _image_cache[_region->audio_source ()] = caches;
243         }
244
245 }
246
247 void
248 WaveView::consolidate_image_cache () const
249 {
250         list <uint32_t> deletion_list;
251         vector <CacheEntry> caches;
252         uint32_t other_entries = 0;
253
254         if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
255                 caches  = _image_cache.find (_region->audio_source ())->second;
256         }
257
258         for (uint32_t i = 0; i < caches.size (); ++i) {
259
260                 if (_channel != caches[i].channel
261                     || _height != caches[i].height
262                     || _region_amplitude != caches[i].amplitude
263                     || _fill_color != caches[i].fill_color) {
264
265                         other_entries++;
266                         continue;
267                 }
268
269                 framepos_t segment_start = caches[i].start;
270                 framepos_t segment_end = caches[i].end;
271
272                 for (uint32_t j = i; j < caches.size (); ++j) {
273
274                         if (i == j || _channel != caches[j].channel
275                             || _height != caches[i].height
276                             || _region_amplitude != caches[i].amplitude
277                             || _fill_color != caches[i].fill_color) {
278
279                                 continue;
280                         }
281
282                         if (caches[j].start >= segment_start && caches[j].end <= segment_end) {
283
284                                 deletion_list.push_back (j);
285                         }
286                 }
287         }
288
289         deletion_list.sort ();
290         deletion_list.unique ();
291
292         while (deletion_list.size() > 0) {
293                 caches[deletion_list.back ()].image.clear ();
294                 caches.erase (caches.begin() + deletion_list.back ());
295                 deletion_list.pop_back();
296         }
297
298         /* We don't care if this channel/height/amplitude has anything in the cache - just drop the Last Added entries
299            until we reach a size where there is a maximum of CACHE_HIGH_WATER + other entries.
300         */
301
302         while (caches.size() > CACHE_HIGH_WATER + other_entries) {
303                 caches.front ().image.clear ();
304                 caches.erase(caches.begin ());
305         }
306
307         if (caches.size () == 0) {
308                 _image_cache.erase (_region->audio_source ());
309         } else {
310                 _image_cache[_region->audio_source ()] = caches;
311         }
312 }
313
314 Coord
315 WaveView::y_extent (double s, bool /*round_to_lower_edge*/) const
316 {
317         /* it is important that this returns an integral value, so that we
318          * can ensure correct single pixel behaviour.
319          *
320          * we need (_height - max(wave_line_width))
321          * wave_line_width == 1 IFF top==bottom (1 sample per pixel or flat line)
322          * wave_line_width == 2 otherwise
323          * then round away from the zero line, towards peak
324          */
325         if (_shape == Rectified) {
326                 // we only ever have 1 point and align to the bottom (not center)
327                 return floor ((1.0 - s) * (_height - 2.0));
328         } else {
329                 /* currently canvas rectangle is off-by-one and we
330                  * cannot draw a pixel at 0 (-.5 .. +.5) without it being
331                  * clipped. A value 1.0 (ideally one point at y=0) ends
332                  * up a pixel down. and a value of -1.0 (ideally y = _height-1)
333                  * currently is on the bottom separator line :(
334                  * So to make the complete waveform appear centered in
335                  * a region, we translate by +.5 (instead of -.5)
336                  * and waste two pixel of height: -4 (instad of -2)
337                  *
338                  * This needs fixing in canvas/rectangle the intersect
339                  * functions and probably a couple of other places as well...
340                  */
341                 Coord pos;
342                 if (s < 0) {
343                         pos = ceil  ((1.0 - s) * .5 * (_height - 4.0));
344                 } else {
345                         pos = floor ((1.0 - s) * .5 * (_height - 4.0));
346                 }
347                 return min (_height - 4.0, (max (0.0, pos)));
348         }
349 }
350
351 struct LineTips {
352         double top;
353         double bot;
354         double spread;
355         bool clip_max;
356         bool clip_min;
357
358         LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
359 };
360
361 struct ImageSet {
362         Cairo::RefPtr<Cairo::ImageSurface> wave;
363         Cairo::RefPtr<Cairo::ImageSurface> outline;
364         Cairo::RefPtr<Cairo::ImageSurface> clip;
365         Cairo::RefPtr<Cairo::ImageSurface> zero;
366
367         ImageSet() :
368                 wave (0), outline (0), clip (0), zero (0) {}
369 };
370
371 void
372 WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks) const
373 {
374
375         ImageSet images;
376
377         images.wave = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
378         images.outline = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
379         images.clip = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
380         images.zero = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
381
382         Cairo::RefPtr<Cairo::Context> wave_context = Cairo::Context::create (images.wave);
383         Cairo::RefPtr<Cairo::Context> outline_context = Cairo::Context::create (images.outline);
384         Cairo::RefPtr<Cairo::Context> clip_context = Cairo::Context::create (images.clip);
385         Cairo::RefPtr<Cairo::Context> zero_context = Cairo::Context::create (images.zero);
386
387         boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
388
389         /* Clip level nominally set to -0.9dBFS to account for inter-sample
390            interpolation possibly clipping (value may be too low).
391
392            We adjust by the region's own gain (but note: not by any gain
393            automation or its gain envelope) so that clip indicators are closer
394            to providing data about on-disk data. This multiplication is
395            needed because the data we get from AudioRegion::read_peaks()
396            has been scaled by scale_amplitude() already.
397         */
398
399         const double clip_level = _clip_level * _region_amplitude;
400
401         if (_shape == WaveView::Rectified) {
402
403                 /* each peak is a line from the bottom of the waveview
404                  * to a point determined by max (_peaks[i].max,
405                  * _peaks[i].min)
406                  */
407
408                 if (_logscaled) {
409                         for (int i = 0; i < n_peaks; ++i) {
410
411                                 tips[i].bot = height() - 1.0;
412                                 const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
413                                 tips[i].top = y_extent (p, false);
414                                 tips[i].spread = p * (_height - 1.0);
415
416                                 if (fabs (_peaks[i].max) >= clip_level) {
417                                         tips[i].clip_max = true;
418                                 }
419
420                                 if (fabs (_peaks[i].min) >= clip_level) {
421                                         tips[i].clip_max = true;
422                                 }
423                         }
424
425                 } else {for (int i = 0; i < n_peaks; ++i) {
426
427                                 tips[i].bot = height() - 1.0;
428                                 const double p = max(fabs (_peaks[i].max), fabs (_peaks[i].min));
429                                 tips[i].top = y_extent (p, false);
430                                 tips[i].spread = p * (_height - 2.0);
431                                 if (p >= clip_level) {
432                                         tips[i].clip_max = true;
433                                 }
434                         }
435
436                 }
437
438         } else {
439
440                 if (_logscaled) {
441                         for (int i = 0; i < n_peaks; ++i) {
442                                 double top = _peaks[i].max;
443                                 double bot = _peaks[i].min;
444
445                                 if (_peaks[i].max > 0 && _peaks[i].min > 0) {
446                                         if (fabs (_peaks[i].max) >= clip_level) {
447                                                 tips[i].clip_max = true;
448                                         }
449                                 }
450                                 else if (_peaks[i].max < 0 && _peaks[i].min < 0) {
451                                         if (fabs (_peaks[i].min) >= clip_level) {
452                                                 tips[i].clip_min = true;
453                                         }
454                                 } else {
455                                         if (fabs (_peaks[i].max) >= clip_level) {
456                                                 tips[i].clip_max = true;
457                                         }
458                                         if (fabs (_peaks[i].min) >= clip_level) {
459                                                 tips[i].clip_min = true;
460                                         }
461                                 }
462
463                                 if (top > 0.0) {
464                                         top = alt_log_meter (fast_coefficient_to_dB (top));
465                                 } else if (top < 0.0) {
466                                         top =-alt_log_meter (fast_coefficient_to_dB (-top));
467                                 } else {
468                                         top = 0.0;
469                                 }
470
471                                 if (bot > 0.0) {
472                                         bot = alt_log_meter (fast_coefficient_to_dB (bot));
473                                 } else if (bot < 0.0) {
474                                         bot = -alt_log_meter (fast_coefficient_to_dB (-bot));
475                                 } else {
476                                         bot = 0.0;
477                                 }
478
479                                 tips[i].top = y_extent (top, false);
480                                 tips[i].bot = y_extent (bot, true);
481                                 tips[i].spread = fabs (tips[i].top - tips[i].bot);
482                         }
483
484                 } else {
485                         for (int i = 0; i < n_peaks; ++i) {
486                                 if (_peaks[i].max > 0 && _peaks[i].min > 0) {
487                                         if (fabs (_peaks[i].max) >= clip_level) {
488                                                 tips[i].clip_max = true;
489                                         }
490                                 }
491                                 else if (_peaks[i].max < 0 && _peaks[i].min < 0) {
492                                         if (fabs (_peaks[i].min) >= clip_level) {
493                                                 tips[i].clip_min = true;
494                                         }
495                                 } else {
496                                         if (fabs (_peaks[i].max) >= clip_level) {
497                                                 tips[i].clip_max = true;
498                                         }
499                                         if (fabs (_peaks[i].min) >= clip_level) {
500                                                 tips[i].clip_min = true;
501                                         }
502                                 }
503
504                                 tips[i].top = y_extent (_peaks[i].max, false);
505                                 tips[i].bot = y_extent (_peaks[i].min, true);
506                                 tips[i].spread = fabs (tips[i].top - tips[i].bot);
507                         }
508
509                 }
510         }
511         Color alpha_one = rgba_to_color (0, 0, 0, 1.0);
512
513         set_source_rgba (wave_context, alpha_one);
514         set_source_rgba (outline_context, alpha_one);
515         set_source_rgba (clip_context, alpha_one);
516         set_source_rgba (zero_context, alpha_one);
517
518         /* ensure single-pixel lines */
519
520         wave_context->set_line_width (1.0);
521         wave_context->translate (0.5, +0.5);
522
523         outline_context->set_line_cap (Cairo::LINE_CAP_ROUND);
524         outline_context->set_line_width (1.0);
525         outline_context->translate (0.5, +0.5);
526
527         clip_context->set_line_width (1.0);
528         clip_context->translate (0.5, +0.5);
529
530         zero_context->set_line_width (1.0);
531         zero_context->translate (0.5, +0.5);
532
533         /* the height of the clip-indicator should be at most 7 pixels,
534          * or 5% of the height of the waveview item.
535          */
536
537         const double clip_height = min (7.0, ceil (_height * 0.05));
538
539         /* There are 3 possible components to draw at each x-axis position: the
540            waveform "line", the zero line and an outline/clip indicator.  We
541            have to decide which of the 3 to draw at each position, pixel by
542            pixel. This makes the rendering less efficient but it is the only
543            way I can see to do this correctly.
544
545            To avoid constant source swapping and stroking, we draw the components separately
546            onto four alpha only image surfaces for use as a mask.
547
548            With only 1 pixel of spread between the top and bottom of the line,
549            we just draw the upper outline/clip indicator.
550
551            With 2 pixels of spread, we draw the upper and lower outline clip
552            indicators.
553
554            With 3 pixels of spread we draw the upper and lower outline/clip
555            indicators and at least 1 pixel of the waveform line.
556
557            With 5 pixels of spread, we draw all components.
558
559            We can do rectified as two separate passes because we have a much
560            easier decision regarding whether to draw the waveform line. We
561            always draw the clip/outline indicators.
562         */
563
564         if (_shape == WaveView::Rectified) {
565
566                 for (int i = 0; i < n_peaks; ++i) {
567
568                         /* waveform line */
569
570                         if (tips[i].spread >= 1.0) {
571                                 wave_context->move_to (i, tips[i].top);
572                                 wave_context->line_to (i, tips[i].bot);
573                         }
574
575                         if (_global_show_waveform_clipping && (tips[i].clip_max)) {
576                                 clip_context->move_to (i, tips[i].top);
577                                 /* clip-indicating upper terminal line */
578                                 clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + .5)));
579                         } else {
580                                 outline_context->move_to (i, tips[i].top);
581                                 /* normal upper terminal dot */
582                                 outline_context->close_path ();
583                         }
584                 }
585
586                 wave_context->stroke ();
587                 clip_context->stroke ();
588                 outline_context->stroke ();
589
590         } else {
591                 const double height_2 = (_height - 4.0) * .5;
592
593                 for (int i = 0; i < n_peaks; ++i) {
594
595                         /* waveform line */
596
597                         if (tips[i].spread >= 2.0) {
598                                 wave_context->move_to (i, tips[i].top);
599                                 wave_context->line_to (i, tips[i].bot);
600                         }
601                         if (i > 0) {
602                                 if (tips[i-1].top + 2 < tips[i].bot) {
603                                         wave_context->move_to (i-1, tips[i-1].top);
604                                         wave_context->line_to (i, tips[i].bot);
605                                 }
606                                 else if (tips[i-1].bot > tips[i].top + 2) {
607                                         wave_context->move_to (i-1, tips[i-1].bot);
608                                         wave_context->line_to (i, tips[i].top);
609                                 }
610                         }
611
612                         /* zero line */
613
614                         if (tips[i].spread >= 5.0 && show_zero_line()) {
615                                 zero_context->move_to (i, floor(height_2));
616                                 zero_context->rel_line_to (1.0, 0);
617                         }
618
619                         if (tips[i].spread > 1.0) {
620                                 /* lower outline/clip indicator */
621                                 if (_global_show_waveform_clipping && tips[i].clip_min) {
622                                         clip_context->move_to (i, tips[i].bot);
623                                         /* clip-indicating lower terminal line */
624                                         const double sign = tips[i].bot > height_2 ? -1 : 1;
625                                         clip_context->rel_line_to (0, sign * min (clip_height, ceil (tips[i].spread + .5)));
626                                 } else {
627                                         outline_context->move_to (i, tips[i].bot);
628                                         /* normal lower terminal dot */
629                                         outline_context->close_path ();
630                                 }
631                         } else {
632                                 if (tips[i].clip_min) {
633                                         // make sure we draw the clip
634                                         tips[i].clip_max = true;
635                                 }
636                         }
637
638                         /* upper outline/clip indicator */
639                         if (_global_show_waveform_clipping && tips[i].clip_max) {
640                                 clip_context->move_to (i, tips[i].top);
641                                 /* clip-indicating upper terminal line */
642                                 const double sign = tips[i].top > height_2 ? -1 : 1;
643                                 clip_context->rel_line_to (0, sign * min(clip_height, ceil(tips[i].spread + .5)));
644                         } else {
645                                 outline_context->move_to (i, tips[i].top);
646                                 /* normal upper terminal dot */
647                                 outline_context->close_path ();
648                         }
649                 }
650
651                 wave_context->stroke ();
652                 outline_context->stroke ();
653                 clip_context->stroke ();
654                 zero_context->stroke ();
655         }
656
657         Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
658
659         /* Here we set a source colour and use the various components as a mask. */
660
661         if (gradient_depth() != 0.0) {
662
663                 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _height));
664
665                 double stops[3];
666
667                 double r, g, b, a;
668
669                 if (_shape == Rectified) {
670                         stops[0] = 0.1;
671                         stops[1] = 0.3;
672                         stops[2] = 0.9;
673                 } else {
674                         stops[0] = 0.1;
675                         stops[1] = 0.5;
676                         stops[2] = 0.9;
677                 }
678
679                 color_to_rgba (_fill_color, r, g, b, a);
680                 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
681                 /* generate a new color for the middle of the gradient */
682                 double h, s, v;
683                 color_to_hsv (_fill_color, h, s, v);
684                 /* change v towards white */
685                 v *= 1.0 - gradient_depth();
686                 Color center = hsv_to_color (h, s, v, a);
687                 color_to_rgba (center, r, g, b, a);
688
689                 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
690                 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
691
692                 context->set_source (gradient);
693         } else {
694                 set_source_rgba (context, _fill_color);
695         }
696
697         context->mask (images.wave, 0, 0);
698         context->fill ();
699
700         set_source_rgba (context, _outline_color);
701         context->mask (images.outline, 0, 0);
702         context->fill ();
703
704         set_source_rgba (context, _clip_color);
705         context->mask (images.clip, 0, 0);
706         context->fill ();
707
708         set_source_rgba (context, _zero_color);
709         context->mask (images.zero, 0, 0);
710         context->fill ();
711
712 }
713
714 void
715 WaveView::get_image (Cairo::RefPtr<Cairo::ImageSurface>& image, framepos_t start, framepos_t end, double& image_offset) const
716 {
717         vector <CacheEntry> caches;
718
719         if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) {
720
721                 caches = _image_cache.find (_region->audio_source ())->second;
722         }
723
724         /* Find a suitable ImageSurface.
725         */
726         for (uint32_t i = 0; i < caches.size (); ++i) {
727
728                 if (_channel != caches[i].channel
729                     || _height != caches[i].height
730                     || _region_amplitude != caches[i].amplitude
731                     || _fill_color != caches[i].fill_color) {
732
733                         continue;
734                 }
735
736                 framepos_t segment_start = caches[i].start;
737                 framepos_t segment_end = caches[i].end;
738
739                 if (end <= segment_end && start >= segment_start) {
740                         image_offset = (segment_start - _region_start) / _samples_per_pixel;
741                         image = caches[i].image;
742
743                         return;
744                 }
745         }
746
747         consolidate_image_cache ();
748
749         /* sample position is canonical here, and we want to generate
750          * an image that spans about twice the canvas width
751          */
752
753         const framepos_t center = start + ((end - start) / 2);
754         const framecnt_t canvas_samples = _canvas->visible_area().width() * _samples_per_pixel; /* one canvas width */
755
756         /* we can request data from anywhere in the Source, between 0 and its length
757          */
758
759         framepos_t sample_start = max ((framepos_t) 0, (center - canvas_samples));
760         framepos_t sample_end = min (center + canvas_samples, _region->source_length (0));
761
762         const int n_peaks = llrintf ((sample_end - sample_start)/ (double) _samples_per_pixel);
763
764         boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
765
766         _region->read_peaks (peaks.get(), n_peaks,
767                              sample_start, sample_end - sample_start,
768                              _channel,
769                              _samples_per_pixel);
770
771         image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, _height);
772
773         draw_image (image, peaks.get(), n_peaks);
774
775         _image_cache[_region->audio_source ()].push_back (CacheEntry (_channel, _height, _region_amplitude, _fill_color, sample_start,  sample_end, image));
776
777         image_offset = (sample_start - _region->start()) / _samples_per_pixel;
778
779         //cerr << "_image_cache size is : " << _image_cache.size() << " entries for this audiosource : " << _image_cache.find (_region->audio_source ())->second.size() << endl;
780
781         return;
782 }
783
784 void
785 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
786 {
787         assert (_samples_per_pixel != 0);
788
789         if (!_region) {
790                 return;
791         }
792
793         Rect self = item_to_window (Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height));
794         boost::optional<Rect> d = self.intersection (area);
795
796         if (!d) {
797                 return;
798         }
799
800         Rect draw = d.get();
801
802         /* window coordinates - pixels where x=0 is the left edge of the canvas
803          * window. We round down in case we were asked to
804          * draw "between" pixels at the start and/or end.
805          */
806
807         const double draw_start = floor (draw.x0);
808         const double draw_end = floor (draw.x1);
809
810         // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl;
811
812         /* image coordnates: pixels where x=0 is the start of this waveview,
813          * wherever it may be positioned. thus image_start=N means "an image
814          * that beings N pixels after the start of region that this waveview is
815          * representing.
816          */
817
818         const framepos_t image_start = window_to_image (self.x0, draw_start);
819         const framepos_t image_end = window_to_image (self.x0, draw_end);
820
821         // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
822
823         /* sample coordinates - note, these are not subject to rounding error */
824         framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
825         framepos_t sample_end   = _region_start + (image_end * _samples_per_pixel);
826
827         // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl;
828
829         Cairo::RefPtr<Cairo::ImageSurface> image;
830         double image_offset = 0;
831
832         get_image (image, sample_start, sample_end, image_offset);
833
834         // cerr << "Offset into image to place at zero: " << image_offset << endl;
835
836         context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height());
837
838         /* round image origin position to an exact pixel in device space to
839          * avoid blurring
840          */
841
842         double x  = self.x0 + image_offset;
843         double y  = self.y0;
844         context->user_to_device (x, y);
845         x = round (x);
846         y = round (y);
847         context->device_to_user (x, y);
848
849         context->set_source (image, x, y);
850         context->fill ();
851
852 }
853
854 void
855 WaveView::compute_bounding_box () const
856 {
857         if (_region) {
858                 _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height);
859         } else {
860                 _bounding_box = boost::optional<Rect> ();
861         }
862
863         _bounding_box_dirty = false;
864 }
865
866 void
867 WaveView::set_height (Distance height)
868 {
869         if (height != _height) {
870                 begin_change ();
871
872                 invalidate_image_cache ();
873                 _height = height;
874
875                 _bounding_box_dirty = true;
876                 end_change ();
877         }
878 }
879
880 void
881 WaveView::set_channel (int channel)
882 {
883         if (channel != _channel) {
884                 begin_change ();
885
886                 invalidate_image_cache ();
887                 _channel = channel;
888
889                 _bounding_box_dirty = true;
890                 end_change ();
891         }
892 }
893
894 void
895 WaveView::set_logscaled (bool yn)
896 {
897         if (_logscaled != yn) {
898                 begin_visual_change ();
899                 invalidate_image_cache ();
900                 _logscaled = yn;
901                 end_visual_change ();
902         }
903 }
904
905 void
906 WaveView::gain_changed ()
907 {
908         begin_visual_change ();
909         invalidate_image_cache ();
910         _region_amplitude = _region->scale_amplitude ();
911         end_visual_change ();
912 }
913
914 void
915 WaveView::set_zero_color (Color c)
916 {
917         if (_zero_color != c) {
918                 begin_visual_change ();
919                 invalidate_image_cache ();
920                 _zero_color = c;
921                 end_visual_change ();
922         }
923 }
924
925 void
926 WaveView::set_clip_color (Color c)
927 {
928         if (_clip_color != c) {
929                 begin_visual_change ();
930                 invalidate_image_cache ();
931                 _clip_color = c;
932                 end_visual_change ();
933         }
934 }
935
936 void
937 WaveView::set_show_zero_line (bool yn)
938 {
939         if (_show_zero != yn) {
940                 begin_visual_change ();
941                 invalidate_image_cache ();
942                 _show_zero = yn;
943                 end_visual_change ();
944         }
945 }
946
947 void
948 WaveView::set_shape (Shape s)
949 {
950         if (_shape != s) {
951                 begin_visual_change ();
952                 invalidate_image_cache ();
953                 _shape = s;
954                 end_visual_change ();
955         }
956 }
957
958 void
959 WaveView::set_amplitude_above_axis (double a)
960 {
961         if (_amplitude_above_axis != a) {
962                 begin_visual_change ();
963                 invalidate_image_cache ();
964                 _amplitude_above_axis = a;
965                 end_visual_change ();
966         }
967 }
968
969 void
970 WaveView::set_global_shape (Shape s)
971 {
972         if (_global_shape != s) {
973                 _global_shape = s;
974                 VisualPropertiesChanged (); /* EMIT SIGNAL */
975         }
976 }
977
978 void
979 WaveView::set_global_logscaled (bool yn)
980 {
981         if (_global_logscaled != yn) {
982                 _global_logscaled = yn;
983                 VisualPropertiesChanged (); /* EMIT SIGNAL */
984         }
985 }
986
987 void
988 WaveView::set_region_start (frameoffset_t start)
989 {
990         if (!_region) {
991                 return;
992         }
993
994         if (_region_start == start) {
995                 return;
996         }
997
998         begin_change ();
999         _region_start = start;
1000         _bounding_box_dirty = true;
1001         end_change ();
1002 }
1003
1004 void
1005 WaveView::region_resized ()
1006 {
1007         /* Called when the region start or end (thus length) has changed.
1008         */
1009
1010         if (!_region) {
1011                 return;
1012         }
1013
1014         begin_change ();
1015         _region_start = _region->start();
1016         _bounding_box_dirty = true;
1017         end_change ();
1018 }
1019
1020 void
1021 WaveView::set_global_gradient_depth (double depth)
1022 {
1023         if (_global_gradient_depth != depth) {
1024                 _global_gradient_depth = depth;
1025                 VisualPropertiesChanged (); /* EMIT SIGNAL */
1026         }
1027 }
1028
1029 void
1030 WaveView::set_global_show_waveform_clipping (bool yn)
1031 {
1032         if (_global_show_waveform_clipping != yn) {
1033                 _global_show_waveform_clipping = yn;
1034                 VisualPropertiesChanged (); /* EMIT SIGNAL */
1035         }
1036 }