persistent waveform shape setting.
[ardour.git] / gtk2_ardour / audio_region_view.cc
1 /*
2     Copyright (C) 2001-2006 Paul Davis
3
4     This program is free software; you can r>edistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <cassert>
21 #include <algorithm>
22 #include <vector>
23
24 #include <boost/scoped_array.hpp>
25
26 #include <gtkmm.h>
27
28 #include <gtkmm2ext/gtk_ui.h>
29
30 #include "ardour/playlist.h"
31 #include "ardour/audioregion.h"
32 #include "ardour/audiosource.h"
33 #include "ardour/profile.h"
34 #include "ardour/session.h"
35
36 #include "pbd/memento_command.h"
37 #include "pbd/stacktrace.h"
38
39 #include "evoral/Curve.hpp"
40
41 #include "canvas/rectangle.h"
42 #include "canvas/polygon.h"
43 #include "canvas/poly_line.h"
44 #include "canvas/line.h"
45 #include "canvas/text.h"
46 #include "canvas/curve.h"
47 #include "canvas/debug.h"
48 #include "canvas/utils.h"
49
50 #include "streamview.h"
51 #include "audio_region_view.h"
52 #include "audio_time_axis.h"
53 #include "public_editor.h"
54 #include "audio_region_editor.h"
55 #include "audio_streamview.h"
56 #include "region_gain_line.h"
57 #include "control_point.h"
58 #include "ghostregion.h"
59 #include "audio_time_axis.h"
60 #include "utils.h"
61 #include "rgb_macros.h"
62 #include "gui_thread.h"
63 #include "ardour_ui.h"
64
65 #include "i18n.h"
66
67 #define MUTED_ALPHA 48
68
69 using namespace std;
70 using namespace ARDOUR;
71 using namespace PBD;
72 using namespace Editing;
73 using namespace ArdourCanvas;
74
75 static const int32_t sync_mark_width = 9;
76 static double const handle_size = 15; /* height of fade handles */
77
78 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
79                                   Gdk::Color const & basic_color)
80         : RegionView (parent, tv, r, spu, basic_color)
81         , sync_mark(0)
82         , fade_in_handle(0)
83         , fade_out_handle(0)
84         , start_xfade_in (0)
85         , start_xfade_out (0)
86         , start_xfade_rect (0)
87         , _start_xfade_visible (false)
88         , end_xfade_in (0)
89         , end_xfade_out (0)
90         , end_xfade_rect (0)
91         , _end_xfade_visible (false)
92         , _amplitude_above_axis(1.0)
93         , fade_color(0)
94 {
95         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&AudioRegionView::parameter_changed, this, _1), gui_context());
96 }
97
98 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
99                                   Gdk::Color const & basic_color, bool recording, TimeAxisViewItem::Visibility visibility)
100         : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
101         , sync_mark(0)
102         , fade_in_handle(0)
103         , fade_out_handle(0)
104         , start_xfade_in (0)
105         , start_xfade_out (0)
106         , start_xfade_rect (0)
107         , _start_xfade_visible (false)
108         , end_xfade_in (0)
109         , end_xfade_out (0)
110         , end_xfade_rect (0)
111         , _end_xfade_visible (false)
112         , _amplitude_above_axis(1.0)
113         , fade_color(0)
114 {
115         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&AudioRegionView::parameter_changed, this, _1), gui_context());
116 }
117
118 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
119         : RegionView (other, boost::shared_ptr<Region> (other_region))
120         , fade_in_handle(0)
121         , fade_out_handle(0)
122         , start_xfade_in (0)
123         , start_xfade_out (0)
124         , start_xfade_rect (0)
125         , _start_xfade_visible (false)
126         , end_xfade_in (0)
127         , end_xfade_out (0)
128         , end_xfade_rect (0)
129         , _end_xfade_visible (false)
130         , _amplitude_above_axis (other._amplitude_above_axis)
131         , fade_color(0)
132 {
133         Gdk::Color c;
134         int r,g,b,a;
135
136         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
137         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
138
139         init (c, true);
140
141         Config->ParameterChanged.connect (*this, invalidator (*this), boost::bind (&AudioRegionView::parameter_changed, this, _1), gui_context());
142 }
143
144 void
145 AudioRegionView::init (Gdk::Color const & basic_color, bool wfd)
146 {
147         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
148         // where order is important and where it isn't...
149
150         RegionView::init (basic_color, wfd);
151
152         _amplitude_above_axis = 1.0;
153
154         compute_colors (basic_color);
155
156         create_waves ();
157
158         if (!_recregion) {
159                 fade_in_handle = new ArdourCanvas::Rectangle (group);
160                 CANVAS_DEBUG_NAME (fade_in_handle, string_compose ("fade in handle for %1", region()->name()));
161                 fade_in_handle->set_outline_color (RGBA_TO_UINT (0, 0, 0, 255));
162                 fade_in_handle->set_fill_color (ARDOUR_UI::config()->get_canvasvar_InactiveFadeHandle());
163                 fade_in_handle->set_data ("regionview", this);
164                 fade_in_handle->hide ();
165
166                 fade_out_handle = new ArdourCanvas::Rectangle (group);
167                 CANVAS_DEBUG_NAME (fade_out_handle, string_compose ("fade out handle for %1", region()->name()));
168                 fade_out_handle->set_outline_color (RGBA_TO_UINT (0, 0, 0, 255));
169                 fade_out_handle->set_fill_color (ARDOUR_UI::config()->get_canvasvar_InactiveFadeHandle());
170                 fade_out_handle->set_data ("regionview", this);
171                 fade_out_handle->hide ();
172         }
173
174         setup_fade_handle_positions ();
175
176         if (!trackview.session()->config.get_show_region_fades()) {
177                 set_fade_visibility (false);
178         }
179
180         const string line_name = _region->name() + ":gain";
181
182         if (!Profile->get_sae()) {
183                 gain_line.reset (new AudioRegionGainLine (line_name, *this, *group, audio_region()->envelope()));
184         }
185         
186         update_envelope_visibility ();
187         gain_line->reset ();
188
189         set_height (trackview.current_height());
190
191         region_muted ();
192         region_sync_changed ();
193
194         region_resized (ARDOUR::bounds_change);
195
196         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
197                 (*i)->set_duration (_region->length() / samples_per_pixel);
198         }
199
200         region_locked ();
201         envelope_active_changed ();
202         fade_in_active_changed ();
203         fade_out_active_changed ();
204
205         reset_width_dependent_items (_pixel_width);
206
207         if (fade_in_handle) {
208                 fade_in_handle->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
209         }
210
211         if (fade_out_handle) {
212                 fade_out_handle->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
213         }
214
215         set_colors ();
216
217         setup_waveform_visibility ();
218         setup_waveform_shape ();
219
220         if (frame_handle_start) {
221                 frame_handle_start->raise_to_top ();
222         }
223         if (frame_handle_end) {
224                 frame_handle_end->raise_to_top ();
225         }
226
227         /* XXX sync mark drag? */
228 }
229
230 AudioRegionView::~AudioRegionView ()
231 {
232         in_destructor = true;
233
234         RegionViewGoingAway (this); /* EMIT_SIGNAL */
235
236         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
237                 delete *i;
238         }
239
240         for (list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator i = feature_lines.begin(); i != feature_lines.end(); ++i) {
241                 delete ((*i).second);
242         }
243
244         /* all waveviews etc will be destroyed when the group is destroyed */
245 }
246
247 boost::shared_ptr<ARDOUR::AudioRegion>
248 AudioRegionView::audio_region() const
249 {
250         // "Guaranteed" to succeed...
251         return boost::dynamic_pointer_cast<AudioRegion>(_region);
252 }
253
254 void
255 AudioRegionView::region_changed (const PropertyChange& what_changed)
256 {
257         ENSURE_GUI_THREAD (*this, &AudioRegionView::region_changed, what_changed);
258
259         RegionView::region_changed (what_changed);
260
261         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
262                 region_scale_amplitude_changed ();
263         }
264         if (what_changed.contains (ARDOUR::Properties::fade_in)) {
265                 fade_in_changed ();
266         }
267         if (what_changed.contains (ARDOUR::Properties::fade_out)) {
268                 fade_out_changed ();
269         }
270         if (what_changed.contains (ARDOUR::Properties::fade_in_active)) {
271                 fade_in_active_changed ();
272         }
273         if (what_changed.contains (ARDOUR::Properties::fade_out_active)) {
274                 fade_out_active_changed ();
275         }
276         if (what_changed.contains (ARDOUR::Properties::envelope_active)) {
277                 envelope_active_changed ();
278         }
279         if (what_changed.contains (ARDOUR::Properties::valid_transients)) {
280                 transients_changed ();
281         }
282 }
283
284 void
285 AudioRegionView::fade_in_changed ()
286 {
287         reset_fade_in_shape ();
288 }
289
290 void
291 AudioRegionView::fade_out_changed ()
292 {
293         reset_fade_out_shape ();
294 }
295
296 void
297 AudioRegionView::fade_in_active_changed ()
298 {
299         if (start_xfade_rect) {
300                 if (audio_region()->fade_in_active()) {
301                         start_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
302                 } else {
303                         start_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_InactiveCrossfade());
304                 }
305         }
306 }
307
308 void
309 AudioRegionView::fade_out_active_changed ()
310 {
311         if (end_xfade_rect) {
312                 if (audio_region()->fade_out_active()) {
313                         end_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
314                 } else {        
315                         end_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_InactiveCrossfade());
316                 }
317         }
318 }
319
320
321 void
322 AudioRegionView::region_scale_amplitude_changed ()
323 {
324         for (uint32_t n = 0; n < waves.size(); ++n) {
325                 waves[n]->gain_changed ();
326         }
327 }
328
329 void
330 AudioRegionView::region_renamed ()
331 {
332         std::string str = RegionView::make_name ();
333
334         if (audio_region()->speed_mismatch (trackview.session()->frame_rate())) {
335                 str = string ("*") + str;
336         }
337
338         if (_region->muted()) {
339                 str = string ("!") + str;
340         }
341
342         set_item_name (str, this);
343         set_name_text (str);
344 }
345
346 void
347 AudioRegionView::region_resized (const PropertyChange& what_changed)
348 {
349         AudioGhostRegion* agr;
350
351         RegionView::region_resized(what_changed);
352         PropertyChange interesting_stuff;
353
354         interesting_stuff.add (ARDOUR::Properties::start);
355         interesting_stuff.add (ARDOUR::Properties::length);
356
357         if (what_changed.contains (interesting_stuff)) {
358                 
359                 for (uint32_t n = 0; n < waves.size(); ++n) {
360                         waves[n]->region_resized ();
361                 }
362
363                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
364                         if ((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
365
366                                 for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
367                                         (*w)->region_resized ();
368                                 }
369                         }
370                 }
371
372                 /* hide transient lines that extend beyond the region end */
373
374                 list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
375
376                 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
377                         if (l->first > _region->length() - 1) {
378                                 l->second->hide();
379                         } else {
380                                 l->second->show();
381                         }
382                 }
383         }
384 }
385
386 void
387 AudioRegionView::reset_width_dependent_items (double pixel_width)
388 {
389         RegionView::reset_width_dependent_items(pixel_width);
390         assert(_pixel_width == pixel_width);
391
392         if (fade_in_handle) {
393                 if (pixel_width <= 6.0 || _height < 5.0 || !trackview.session()->config.get_show_region_fades()) {
394                         fade_in_handle->hide();
395                         fade_out_handle->hide();
396                 } else {
397                         //fade_in_handle->show();
398                         //fade_out_handle->show();
399                 }
400         }
401
402         AnalysisFeatureList analysis_features = _region->transients();
403         AnalysisFeatureList::const_iterator i;
404
405         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
406
407         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
408
409                 float x_pos = trackview.editor().sample_to_pixel (*i);
410
411                 (*l).second->set (ArdourCanvas::Duple (x_pos, 2.0),
412                                   ArdourCanvas::Duple (x_pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
413
414                 (*l).first = *i;
415
416                 (*l).second->set (ArdourCanvas::Duple (x_pos, 2.0),
417                                   ArdourCanvas::Duple (x_pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
418         }
419
420         reset_fade_shapes ();
421 }
422
423 void
424 AudioRegionView::region_muted ()
425 {
426         RegionView::region_muted();
427         set_waveform_colors ();
428 }
429
430 void
431 AudioRegionView::setup_fade_handle_positions()
432 {
433         /* position of fade handle offset from the top of the region view */
434         double const handle_pos = 0.0;
435
436         if (fade_in_handle) {
437                 fade_in_handle->set_y0 (handle_pos);
438                 fade_in_handle->set_y1 (handle_pos + handle_size);
439         }
440
441         if (fade_out_handle) {
442                 fade_out_handle->set_y0 (handle_pos);
443                 fade_out_handle->set_y1 (handle_pos + handle_size);
444         }
445 }
446
447 void
448 AudioRegionView::set_height (gdouble height)
449 {
450         RegionView::set_height (height);
451
452         uint32_t wcnt = waves.size();
453
454         for (uint32_t n = 0; n < wcnt; ++n) {
455                 gdouble ht;
456
457                 if (height < NAME_HIGHLIGHT_THRESH) {
458                         ht = ((height - 2 * wcnt) / (double) wcnt);
459                 } else {
460                         ht = (((height - 2 * wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
461                 }
462
463                 gdouble yoff = n * (ht + 1);
464
465                 waves[n]->set_height (ht);
466                 waves[n]->set_y_position (yoff + 2);
467         }
468
469         if (gain_line) {
470
471                 if ((height/wcnt) < NAME_HIGHLIGHT_THRESH) {
472                         gain_line->hide ();
473                 } else {
474                         update_envelope_visibility ();
475                 }
476
477                 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE) - 2);
478         }
479
480         reset_fade_shapes ();
481
482         /* Update hights for any active feature lines */
483         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
484
485         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
486
487                 float pos_x = trackview.editor().sample_to_pixel((*l).first);
488
489                 if (height >= NAME_HIGHLIGHT_THRESH) {
490                         (*l).second->set (ArdourCanvas::Duple (pos_x, 2.0),
491                                           ArdourCanvas::Duple (pos_x, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
492                 } else {
493                         (*l).second->set (ArdourCanvas::Duple (pos_x, 2.0),
494                                           ArdourCanvas::Duple (pos_x, _height - 1));
495                 }
496         }
497
498         if (name_text) {
499                 name_text->raise_to_top();
500         }
501 }
502
503 void
504 AudioRegionView::reset_fade_shapes ()
505 {
506         reset_fade_in_shape ();
507         reset_fade_out_shape ();
508 }
509
510 void
511 AudioRegionView::reset_fade_in_shape ()
512 {
513         reset_fade_in_shape_width (audio_region(), (framecnt_t) audio_region()->fade_in()->back()->when);
514 }
515
516 void
517 AudioRegionView::reset_fade_in_shape_width (boost::shared_ptr<AudioRegion> ar, framecnt_t width)
518 {
519         if (fade_in_handle == 0) {
520                 return;
521         }
522
523         /* smallest size for a fade is 64 frames */
524
525         width = std::max ((framecnt_t) 64, width);
526
527         /* round here to prevent little visual glitches with sub-pixel placement */
528         double const pwidth = rint (width / samples_per_pixel);
529         double const handle_left = pwidth;
530
531         /* Put the fade in handle so that its left side is at the end-of-fade line */
532         fade_in_handle->set_x0 (handle_left);
533         fade_in_handle->set_x1 (handle_left + handle_size);
534
535         if (pwidth < 5) {
536                 hide_start_xfade();
537                 return;
538         }
539
540         if (!trackview.session()->config.get_show_region_fades()) {
541                 hide_start_xfade ();
542                 return;
543         }
544         
545         double effective_height;
546
547         if (_height >= NAME_HIGHLIGHT_THRESH) {
548                 effective_height = _height - NAME_HIGHLIGHT_SIZE;
549         } else {
550                 effective_height = _height;
551         }
552
553         /* points *MUST* be in anti-clockwise order */
554
555         Points points;
556         Points::size_type npoints;
557         Points::size_type pi;
558         boost::shared_ptr<const Evoral::ControlList> list (audio_region()->fade_in());
559         Evoral::ControlList::const_iterator x;
560         double length = list->length();
561         npoints = list->size();
562
563         points.assign (list->size(), Duple());
564
565         for (x = list->begin(), pi = 0; x != list->end(); ++x, ++pi) {
566                 points[pi].x = 1.0 + (pwidth * ((*x)->when/length));
567                 points[pi].y = effective_height - ((*x)->value * effective_height);
568         }
569
570         /* draw the line */
571
572         redraw_start_xfade_to (ar, width, points, effective_height, handle_left);
573
574         /* ensure trim handle stays on top */
575         if (frame_handle_start) {
576                 frame_handle_start->raise_to_top();
577         }
578 }
579
580 void
581 AudioRegionView::reset_fade_out_shape ()
582 {
583         reset_fade_out_shape_width (audio_region(), (framecnt_t) audio_region()->fade_out()->back()->when);
584 }
585
586 void
587 AudioRegionView::reset_fade_out_shape_width (boost::shared_ptr<AudioRegion> ar, framecnt_t width)
588 {
589         if (fade_out_handle == 0) {
590                 return;
591         }
592
593         /* smallest size for a fade is 64 frames */
594
595         width = std::max ((framecnt_t) 64, width);
596
597         double const pwidth = trackview.editor().sample_to_pixel (width);
598
599         /* the right edge should be right on the region frame is the pixel
600          * width is zero. Hence the additional + 1.0 at the end.
601          */
602
603         double const handle_right = trackview.editor().sample_to_pixel (_region->length()) + TimeAxisViewItem::RIGHT_EDGE_SHIFT - pwidth;
604
605         /* Put the fade out handle so that its right side is at the end-of-fade line;
606          */
607         fade_out_handle->set_x0 (handle_right - handle_size);
608         fade_out_handle->set_x1 (handle_right);
609
610         /* don't show shape if its too small */
611
612         if (pwidth < 5) {
613                 hide_end_xfade();
614                 return;
615         }
616
617         if (!trackview.session()->config.get_show_region_fades()) {
618                 hide_end_xfade();
619                 return;
620         }
621
622         double effective_height;
623
624         if (_height >= NAME_HIGHLIGHT_THRESH) {
625                 effective_height = _height - NAME_HIGHLIGHT_SIZE;
626         } else {
627                 effective_height = _height;
628         }
629
630         /* points *MUST* be in anti-clockwise order */
631         
632         Points points;
633         Points::size_type npoints;
634         Points::size_type pi;
635         boost::shared_ptr<const Evoral::ControlList> list (audio_region()->fade_out());
636         Evoral::ControlList::const_iterator x;
637         double length = list->length();
638         npoints = list->size();
639
640         points.assign (list->size(), Duple());
641
642         for (x = list->begin(), pi = 0; x != list->end(); ++x, ++pi) {
643                 points[pi].x = 1.0 + _pixel_width - pwidth + (pwidth * ((*x)->when/length));
644                 points[pi].y = effective_height - ((*x)->value * effective_height);
645         }
646
647         /* draw the line */
648
649         redraw_end_xfade_to (ar, width, points, effective_height, handle_right, pwidth);
650
651         /* ensure trim handle stays on top */
652         if (frame_handle_end) {
653                 frame_handle_end->raise_to_top();
654         }
655 }
656
657 framepos_t
658 AudioRegionView::get_fade_in_shape_width ()
659 {
660         return audio_region()->fade_in()->back()->when;
661 }
662
663 framepos_t
664 AudioRegionView::get_fade_out_shape_width ()
665 {
666         return audio_region()->fade_out()->back()->when;
667 }
668
669
670 void
671 AudioRegionView::redraw_start_xfade ()
672 {
673         boost::shared_ptr<AudioRegion> ar (audio_region());
674
675         if (!ar->fade_in() || ar->fade_in()->empty()) {
676                 return;
677         }
678
679         show_start_xfade();
680         reset_fade_in_shape_width (ar, ar->fade_in()->back()->when);
681 }
682
683 void
684 AudioRegionView::redraw_start_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t /*width*/, Points& points, double effective_height,
685                                         double rect_width)
686 {
687         if (points.size() < 2) {
688                 return;
689         }
690
691         if (!start_xfade_in) {
692                 start_xfade_in = new ArdourCanvas::Curve (group);
693                 CANVAS_DEBUG_NAME (start_xfade_in, string_compose ("xfade start in line for %1", region()->name()));
694                 start_xfade_in->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
695                 start_xfade_in->set_outline_width (1.5);
696                 start_xfade_in->set_ignore_events (true);
697         }
698
699         if (!start_xfade_out) {
700                 start_xfade_out = new ArdourCanvas::Curve (group);
701                 CANVAS_DEBUG_NAME (start_xfade_out, string_compose ("xfade start out line for %1", region()->name()));
702                 uint32_t col = UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine(), 128);
703                 start_xfade_out->set_outline_color (col);
704                 start_xfade_out->set_outline_width (2.0);
705                 start_xfade_out->set_ignore_events (true);
706         }
707
708         if (!start_xfade_rect) {
709                 start_xfade_rect = new ArdourCanvas::Rectangle (group);
710                 CANVAS_DEBUG_NAME (start_xfade_rect, string_compose ("xfade start rect for %1", region()->name()));
711                 start_xfade_rect->set_fill (true);
712                 start_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
713                 start_xfade_rect->set_outline (false);
714                 start_xfade_rect->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_start_xfade_event), start_xfade_rect, this));
715                 start_xfade_rect->set_data ("regionview", this);
716         }
717
718         start_xfade_rect->set (ArdourCanvas::Rect (1.0, 0.0, rect_width, effective_height));
719
720         start_xfade_in->set (points);
721
722         /* fade out line */
723
724         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_in ();
725         Points ipoints;
726         Points::size_type npoints;
727
728         if (!inverse) {
729
730                 /* there is no explicit inverse fade in curve, so take the
731                  * regular fade in curve given to use as "points" (already a
732                  * set of coordinates), and convert to the inverse shape.
733                  */
734
735                 npoints = points.size();
736                 ipoints.assign (npoints, Duple());
737
738                 for (Points::size_type i = 0, pci = 0; i < npoints; ++i, ++pci) {
739                         ArdourCanvas::Duple &p (ipoints[pci]);
740                         /* leave x-axis alone but invert with respect to y-axis */
741                         p.y = effective_height - points[pci].y;
742                 }
743
744         } else {
745
746                 /* there is an explicit inverse fade in curve. Grab the points
747                    and convert them into coordinates for the inverse fade in
748                    line.
749                 */
750
751                 npoints = inverse->size();
752                 ipoints.assign (npoints, Duple());
753                 
754                 Evoral::ControlList::const_iterator x;
755                 Points::size_type pi;
756                 double length = inverse->length();
757
758                 for (x = inverse->begin(), pi = 0; x != inverse->end(); ++x, ++pi) {
759                         ArdourCanvas::Duple& p (ipoints[pi]);
760                         p.x = 1.0 + (rect_width * ((*x)->when/length));
761                         p.y = effective_height - ((*x)->value * effective_height);
762                 }
763         }
764
765         start_xfade_out->set (ipoints);
766
767         show_start_xfade();
768 }
769
770 void
771 AudioRegionView::redraw_end_xfade ()
772 {
773         boost::shared_ptr<AudioRegion> ar (audio_region());
774
775         if (!ar->fade_out() || ar->fade_out()->empty()) {
776                 return;
777         }
778
779         show_end_xfade();
780         
781         reset_fade_out_shape_width (ar, ar->fade_out()->back()->when);
782 }
783
784 void
785 AudioRegionView::redraw_end_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t width, Points& points, double effective_height,
786                                       double rect_edge, double rect_width)
787 {
788         if (points.size() < 2) {
789                 return;
790         }
791
792         if (!end_xfade_in) {
793                 end_xfade_in = new ArdourCanvas::Curve (group);
794                 CANVAS_DEBUG_NAME (end_xfade_in, string_compose ("xfade end in line for %1", region()->name()));
795                 uint32_t col UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine(), 128);
796                 end_xfade_in->set_outline_color (col);
797                 end_xfade_in->set_outline_width (1.5);  
798                 end_xfade_in->set_ignore_events (true);
799 }
800
801         if (!end_xfade_out) {
802                 end_xfade_out = new ArdourCanvas::Curve (group);
803                 CANVAS_DEBUG_NAME (end_xfade_out, string_compose ("xfade end out line for %1", region()->name()));
804                 end_xfade_out->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
805                 end_xfade_out->set_outline_width (2.0);
806                 end_xfade_out->set_ignore_events (true);
807         }
808
809         if (!end_xfade_rect) {
810                 end_xfade_rect = new ArdourCanvas::Rectangle (group);
811                 CANVAS_DEBUG_NAME (end_xfade_rect, string_compose ("xfade end rect for %1", region()->name()));
812                 end_xfade_rect->set_fill (true);
813                 end_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
814                 end_xfade_rect->set_outline (0);
815                 end_xfade_rect->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_end_xfade_event), end_xfade_rect, this));
816                 end_xfade_rect->set_data ("regionview", this);
817         }
818
819         end_xfade_rect->set (ArdourCanvas::Rect (rect_edge, 0.0, rect_edge + rect_width + TimeAxisViewItem::RIGHT_EDGE_SHIFT, effective_height));
820
821         end_xfade_in->set (points);
822
823         /* fade in line */
824
825         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_out ();
826         Points ipoints;
827         Points::size_type npoints;
828
829         if (!inverse) {
830
831                 /* there is no explicit inverse fade out curve, so take the
832                  * regular fade out curve given to use as "points" (already a
833                  * set of coordinates), and convert to the inverse shape.
834                  */
835
836                 npoints = points.size();
837                 ipoints.assign (npoints, Duple());
838
839                 Points::size_type pci;
840
841                 for (pci = 0; pci < npoints; ++pci) {
842                         ArdourCanvas::Duple &p (ipoints[pci]);
843                         p.y = effective_height - points[pci].y;
844                 }
845
846         } else {
847
848                 /* there is an explicit inverse fade out curve. Grab the points
849                    and convert them into coordinates for the inverse fade out
850                    line.
851                 */
852
853                 npoints = inverse->size();
854                 ipoints.assign (npoints, Duple());
855                 
856                 const double rend = trackview.editor().sample_to_pixel (_region->length() - width);
857                 
858                 Evoral::ControlList::const_iterator x;
859                 Points::size_type i;
860                 Points::size_type pi;
861                 double length = inverse->length();
862
863                 for (x = inverse->begin(), i = 0, pi = 0; x != inverse->end(); ++x, ++pi, ++i) {
864                         ArdourCanvas::Duple& p (ipoints[pi]);
865                         p.x = 1.0 + (rect_width * ((*x)->when/length)) + rend;
866                         p.y = effective_height - ((*x)->value * effective_height);
867                 }
868         }
869
870         end_xfade_out->set (ipoints);
871
872         show_end_xfade();
873 }
874
875 void
876 AudioRegionView::hide_xfades ()
877 {
878         hide_start_xfade ();
879         hide_end_xfade ();
880 }
881
882 void
883 AudioRegionView::hide_start_xfade ()
884 {
885         if (start_xfade_in) {
886                 start_xfade_in->hide();
887         }
888         if (start_xfade_out) {
889                 start_xfade_out->hide();
890         }
891         if (start_xfade_rect) {
892                 start_xfade_rect->hide ();
893         }
894
895         _start_xfade_visible = false;
896 }
897
898 void
899 AudioRegionView::hide_end_xfade ()
900 {
901         if (end_xfade_in) {
902                 end_xfade_in->hide();
903         }
904         if (end_xfade_out) {
905                 end_xfade_out->hide();
906         }
907         if (end_xfade_rect) {
908                 end_xfade_rect->hide ();
909         }
910
911         _end_xfade_visible = false;
912 }
913
914 void
915 AudioRegionView::show_start_xfade ()
916 {
917         if (start_xfade_in) {
918                 start_xfade_in->show();
919         }
920         if (start_xfade_out) {
921                 start_xfade_out->show();
922         }
923         if (start_xfade_rect) {
924                 start_xfade_rect->show ();
925         }
926
927         _start_xfade_visible = true;
928 }
929
930 void
931 AudioRegionView::show_end_xfade ()
932 {
933         if (end_xfade_in) {
934                 end_xfade_in->show();
935         }
936         if (end_xfade_out) {
937                 end_xfade_out->show();
938         }
939         if (end_xfade_rect) {
940                 end_xfade_rect->show ();
941         }
942
943         _end_xfade_visible = true;
944 }
945
946 void
947 AudioRegionView::set_samples_per_pixel (gdouble fpp)
948 {
949         RegionView::set_samples_per_pixel (fpp);
950
951         if (Config->get_show_waveforms ()) {
952                 for (uint32_t n = 0; n < waves.size(); ++n) {
953                         waves[n]->set_samples_per_pixel (fpp);
954                 }
955         }
956
957         if (gain_line) {
958                 gain_line->reset ();
959         }
960
961         reset_fade_shapes ();
962 }
963
964 void
965 AudioRegionView::set_amplitude_above_axis (gdouble a)
966 {
967         for (uint32_t n=0; n < waves.size(); ++n) {
968                 waves[n]->set_amplitude_above_axis (a);
969         }
970 }
971
972 void
973 AudioRegionView::compute_colors (Gdk::Color const & basic_color)
974 {
975         RegionView::compute_colors (basic_color);
976
977         /* gain color computed in envelope_active_changed() */
978
979         fade_color = UINT_RGBA_CHANGE_A (fill_color, 120);
980 }
981
982 void
983 AudioRegionView::set_colors ()
984 {
985         RegionView::set_colors();
986
987         if (gain_line) {
988                 gain_line->set_line_color (audio_region()->envelope_active() ? 
989                                            ARDOUR_UI::config()->get_canvasvar_GainLine() : 
990                                            ARDOUR_UI::config()->get_canvasvar_GainLineInactive());
991         }
992
993         set_waveform_colors ();
994
995         if (start_xfade_in) {
996                 start_xfade_in->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
997         }
998         if (start_xfade_out) {
999                 uint32_t col UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine(), 128);
1000                 start_xfade_out->set_outline_color (col);
1001         }
1002         if (end_xfade_in) {
1003                 end_xfade_in->set_outline_color (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine());
1004         }
1005         if (end_xfade_out) {
1006                 uint32_t col UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->get_canvasvar_CrossfadeLine(), 128);
1007                 end_xfade_out->set_outline_color (col);
1008         }
1009
1010         if (start_xfade_rect) {
1011                 start_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
1012         }
1013         if (end_xfade_rect) {
1014                 end_xfade_rect->set_fill_color (ARDOUR_UI::config()->get_canvasvar_ActiveCrossfade());
1015         }
1016 }
1017
1018 void
1019 AudioRegionView::setup_waveform_visibility ()
1020 {
1021         if (Config->get_show_waveforms ()) {
1022                 for (uint32_t n = 0; n < waves.size(); ++n) {
1023                         /* make sure the zoom level is correct, since we don't update
1024                            this when waveforms are hidden.
1025                         */
1026                         // CAIROCANVAS
1027                         // waves[n]->set_samples_per_pixel (_samples_per_pixel);
1028                         waves[n]->show();
1029                 }
1030         } else {
1031                 for (uint32_t n = 0; n < waves.size(); ++n) {
1032                         waves[n]->hide();
1033                 }
1034         }
1035 }
1036
1037 void
1038 AudioRegionView::temporarily_hide_envelope ()
1039 {
1040         if (gain_line) {
1041                 gain_line->hide ();
1042         }
1043 }
1044
1045 void
1046 AudioRegionView::unhide_envelope ()
1047 {
1048         update_envelope_visibility ();
1049 }
1050
1051 void
1052 AudioRegionView::update_envelope_visibility ()
1053 {
1054         if (!gain_line) {
1055                 return;
1056         }
1057
1058         if (Config->get_show_region_gain() || trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1059                 gain_line->add_visibility (AutomationLine::Line);
1060         } else {
1061                 gain_line->hide ();
1062         }
1063 }
1064
1065 void
1066 AudioRegionView::create_waves ()
1067 {
1068         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
1069         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
1070
1071         if (!atv.track()) {
1072                 return;
1073         }
1074
1075         ChanCount nchans = atv.track()->n_channels();
1076
1077         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
1078         //              << " and channels = " << nchans.n_audio() << endl;
1079
1080         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
1081         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
1082                 tmp_waves.push_back (0);
1083         }
1084
1085         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
1086                 delete *i;
1087         }
1088
1089         _data_ready_connections.clear ();
1090
1091         for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
1092                 _data_ready_connections.push_back (0);
1093         }
1094
1095         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
1096
1097                 if (n >= audio_region()->n_channels()) {
1098                         break;
1099                 }
1100
1101                 // cerr << "\tchannel " << n << endl;
1102
1103                 if (wait_for_data) {
1104                         if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
1105                                 // cerr << "\tData is ready\n";
1106                                 create_one_wave (n, true);
1107                         } else {
1108                                 // cerr << "\tdata is not ready\n";
1109                                 // we'll get a PeaksReady signal from the source in the future
1110                                 // and will call create_one_wave(n) then.
1111                         }
1112
1113                 } else {
1114                         // cerr << "\tdon't delay, display today!\n";
1115                         create_one_wave (n, true);
1116                 }
1117
1118         }
1119 }
1120
1121 void
1122 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
1123 {
1124         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
1125         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
1126         uint32_t nchans = atv.track()->n_channels().n_audio();
1127         uint32_t n;
1128         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
1129         gdouble ht;
1130
1131         if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
1132                 ht = ((trackview.current_height()) / (double) nchans);
1133         } else {
1134                 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
1135         }
1136
1137         gdouble yoff = which * ht;
1138
1139         WaveView *wave = new WaveView (group, audio_region ());
1140         CANVAS_DEBUG_NAME (wave, string_compose ("wave view for chn %1 of %2", which, get_item_name()));
1141         
1142         wave->set_channel (which);
1143         wave->set_y_position (yoff);
1144         wave->set_height (ht);
1145         wave->set_samples_per_pixel (samples_per_pixel);
1146         wave->set_show_zero_line (true);
1147         
1148         switch (Config->get_waveform_shape()) {
1149         case Rectified:
1150                 wave->set_shape (WaveView::Rectified);
1151                 break;
1152         default:
1153                 wave->set_shape (WaveView::Normal);
1154         }
1155                 
1156         wave->set_logscaled (Config->get_waveform_scale() == Logarithmic);
1157
1158         if (!Config->get_show_waveforms ()) {
1159                 wave->hide();
1160         }
1161
1162         /* note: calling this function is serialized by the lock
1163            held in the peak building thread that signals that
1164            peaks are ready for use *or* by the fact that it is
1165            called one by one from the GUI thread.
1166         */
1167
1168         if (which < nchans) {
1169                 tmp_waves[which] = wave;
1170         } else {
1171                 /* n-channel track, >n-channel source */
1172         }
1173
1174         /* see if we're all ready */
1175
1176         for (n = 0; n < nchans; ++n) {
1177                 if (tmp_waves[n] == 0) {
1178                         break;
1179                 }
1180         }
1181
1182         if (n == nwaves && waves.empty()) {
1183                 /* all waves are ready */
1184                 tmp_waves.resize(nwaves);
1185
1186                 waves = tmp_waves;
1187                 tmp_waves.clear ();
1188
1189                 /* all waves created, don't hook into peaks ready anymore */
1190                 delete _data_ready_connections[which];
1191                 _data_ready_connections[which] = 0;
1192         }
1193 }
1194
1195 void
1196 AudioRegionView::peaks_ready_handler (uint32_t which)
1197 {
1198         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AudioRegionView::create_one_wave, this, which, false));
1199         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
1200 }
1201
1202 void
1203 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev, bool with_guard_points)
1204 {
1205         if (!gain_line) {
1206                 return;
1207         }
1208
1209         double x, y;
1210
1211         /* don't create points that can't be seen */
1212
1213         update_envelope_visibility ();
1214
1215         x = ev->button.x;
1216         y = ev->button.y;
1217
1218         item->canvas_to_item (x, y);
1219
1220         framepos_t fx = trackview.editor().pixel_to_sample (x);
1221
1222         if (fx > _region->length()) {
1223                 return;
1224         }
1225
1226         /* compute vertical fractional position */
1227
1228         y = 1.0 - (y / (_height - NAME_HIGHLIGHT_SIZE));
1229
1230         /* map using gain line */
1231
1232         gain_line->view_to_model_coord (x, y);
1233
1234         /* XXX STATEFUL: can't convert to stateful diff until we
1235            can represent automation data with it.
1236         */
1237
1238         trackview.session()->begin_reversible_command (_("add gain control point"));
1239         XMLNode &before = audio_region()->envelope()->get_state();
1240
1241         if (!audio_region()->envelope_active()) {
1242                 XMLNode &region_before = audio_region()->get_state();
1243                 audio_region()->set_envelope_active(true);
1244                 XMLNode &region_after = audio_region()->get_state();
1245                 trackview.session()->add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
1246         }
1247
1248         audio_region()->envelope()->add (fx, y, with_guard_points);
1249
1250         XMLNode &after = audio_region()->envelope()->get_state();
1251         trackview.session()->add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1252         trackview.session()->commit_reversible_command ();
1253 }
1254
1255 void
1256 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent* /*ev*/)
1257 {
1258         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1259         audio_region()->envelope()->erase (cp->model());
1260 }
1261
1262 void
1263 AudioRegionView::setup_waveform_shape ()
1264 {
1265         WaveView::Shape shape;
1266
1267         switch (Config->get_waveform_shape()) {
1268         case Rectified:
1269                 shape = WaveView::Rectified;
1270                 break;
1271         default:
1272                 shape = WaveView::Normal;
1273         }
1274         WaveView::set_global_shape (shape);
1275 }
1276
1277 void
1278 AudioRegionView::setup_waveform_scale ()
1279 {
1280         WaveView::set_global_logscaled (Config->get_waveform_scale() == Logarithmic);
1281 }
1282
1283 void
1284 AudioRegionView::setup_waveform_clipping ()
1285 {
1286         WaveView::set_global_show_waveform_clipping (ARDOUR_UI::config()->get_show_waveform_clipping());
1287 }
1288
1289 GhostRegion*
1290 AudioRegionView::add_ghost (TimeAxisView& tv)
1291 {
1292         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1293         assert(rtv);
1294
1295         double unit_position = _region->position () / samples_per_pixel;
1296         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1297         uint32_t nchans;
1298
1299         nchans = rtv->track()->n_channels().n_audio();
1300
1301         for (uint32_t n = 0; n < nchans; ++n) {
1302
1303                 if (n >= audio_region()->n_channels()) {
1304                         break;
1305                 }
1306
1307                 WaveView *wave = new WaveView (ghost->group, audio_region());
1308                 CANVAS_DEBUG_NAME (wave, string_compose ("ghost wave for %1", get_item_name()));
1309
1310                 wave->set_channel (n);
1311                 wave->set_samples_per_pixel (samples_per_pixel);
1312                 wave->set_amplitude_above_axis (_amplitude_above_axis);
1313
1314                 ghost->waves.push_back(wave);
1315         }
1316
1317         ghost->set_height ();
1318         ghost->set_duration (_region->length() / samples_per_pixel);
1319         ghost->set_colors();
1320         ghosts.push_back (ghost);
1321
1322         return ghost;
1323 }
1324
1325 void
1326 AudioRegionView::entered (bool internal_editing)
1327 {
1328         trackview.editor().set_current_trimmable (_region);
1329         trackview.editor().set_current_movable (_region);
1330         
1331         if (gain_line) {
1332                 /* these may or may not be visible depending on mouse mode */
1333                 gain_line->add_visibility (AutomationLine::ControlPoints);
1334         }
1335
1336         if (fade_in_handle && !internal_editing) {
1337                 fade_in_handle->show ();
1338                 fade_out_handle->show ();
1339                 fade_out_handle->raise_to_top ();
1340                 fade_in_handle->raise_to_top ();
1341         }
1342 }
1343
1344 void
1345 AudioRegionView::exited ()
1346 {
1347         trackview.editor().set_current_trimmable (boost::shared_ptr<Trimmable>());
1348         trackview.editor().set_current_movable (boost::shared_ptr<Movable>());
1349
1350         if (gain_line) {
1351                 gain_line->remove_visibility (AutomationLine::ControlPoints);
1352         }
1353
1354         if (fade_in_handle) {
1355                 fade_in_handle->hide ();
1356                 fade_out_handle->hide ();
1357         }
1358 }
1359
1360 void
1361 AudioRegionView::envelope_active_changed ()
1362 {
1363         if (gain_line) {
1364                 gain_line->set_line_color (audio_region()->envelope_active() ? 
1365                                            ARDOUR_UI::config()->get_canvasvar_GainLine() : 
1366                                            ARDOUR_UI::config()->get_canvasvar_GainLineInactive());
1367                 update_envelope_visibility ();
1368         }
1369 }
1370
1371 void
1372 AudioRegionView::color_handler ()
1373 {
1374         //case cMutedWaveForm:
1375         //case cWaveForm:
1376         //case cWaveFormClip:
1377         //case cZeroLine:
1378         set_colors ();
1379
1380         //case cGainLineInactive:
1381         //case cGainLine:
1382         envelope_active_changed();
1383
1384 }
1385
1386 void
1387 AudioRegionView::set_waveform_colors ()
1388 {
1389         for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1390                 set_one_waveform_color (*w);
1391         }
1392 }
1393
1394 void
1395 AudioRegionView::set_one_waveform_color (ArdourCanvas::WaveView* wave)
1396 {
1397         ArdourCanvas::Color fill;
1398         ArdourCanvas::Color outline;
1399         
1400         if (_selected) {
1401                 if (_region->muted()) {
1402                         /* hide outline with zero alpha */
1403                         outline = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_SelectedWaveForm(), 0);
1404                         fill = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_SelectedWaveFormFill(), MUTED_ALPHA);
1405                 } else {
1406                         outline = ARDOUR_UI::config()->get_canvasvar_SelectedWaveForm();
1407                         fill = ARDOUR_UI::config()->get_canvasvar_SelectedWaveFormFill();
1408                 }
1409         } else {
1410                 if (_recregion) {
1411                         outline = ARDOUR_UI::config()->get_canvasvar_RecWaveForm();
1412                         fill = ARDOUR_UI::config()->get_canvasvar_RecWaveFormFill();
1413                 } else {
1414                         if (_region->muted()) {
1415                                 /* hide outline with zero alpha */
1416                                 outline = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_WaveForm(), 0); 
1417                                 fill = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->get_canvasvar_WaveFormFill(), MUTED_ALPHA);
1418                         } else {
1419                                 outline = ARDOUR_UI::config()->get_canvasvar_WaveForm();
1420                                 fill = ARDOUR_UI::config()->get_canvasvar_WaveFormFill();
1421                         }
1422                 }
1423         }
1424
1425         if (ARDOUR_UI::config()->get_color_regions_using_track_color()) {
1426
1427                 /* just use a slightly transparent version of the selected
1428                  * color so that some of the track color bleeds through
1429                  */
1430
1431                 double r, g, b, a;
1432                 ArdourCanvas::color_to_rgba (fill, r, g, b, a);
1433                 fill = ArdourCanvas::rgba_to_color (r, g, b, 0.85); /* magic number, not user controllable */
1434                 outline = ARDOUR_UI::config()->get_canvasvar_WaveForm();
1435
1436                 if (!Config->get_show_name_highlight()) {
1437                         /* recolor name text because it needs to contrast with
1438                            the waveform background, not the name highlight.
1439                         */
1440                 }
1441         }
1442
1443         wave->set_fill_color (fill);
1444         wave->set_outline_color (outline);
1445         wave->set_clip_color (ARDOUR_UI::config()->get_canvasvar_WaveFormClip());
1446         wave->set_zero_color (ARDOUR_UI::config()->get_canvasvar_ZeroLine());
1447 }
1448
1449 void
1450 AudioRegionView::set_frame_color ()
1451 {
1452         if (!frame) {
1453                 return;
1454         }
1455
1456         if (_region->opaque()) {
1457                 fill_opacity = 130;
1458         } else {
1459                 fill_opacity = 0;
1460         }
1461
1462         TimeAxisViewItem::set_frame_color ();
1463
1464         set_waveform_colors ();
1465 }
1466
1467 void
1468 AudioRegionView::set_fade_visibility (bool yn)
1469 {
1470         if (yn) {
1471                 if (fade_in_handle) {
1472                         fade_in_handle->show ();
1473                 }
1474                 if (fade_out_handle) {
1475                         fade_out_handle->show ();
1476                 }
1477         } else {
1478                 if (fade_in_handle) {
1479                         fade_in_handle->hide ();
1480                 }
1481                 if (fade_out_handle) {
1482                         fade_out_handle->hide ();
1483                 }
1484         }
1485 }
1486
1487 void
1488 AudioRegionView::update_coverage_frames (LayerDisplay d)
1489 {
1490         RegionView::update_coverage_frames (d);
1491
1492         if (fade_in_handle) {
1493                 fade_in_handle->raise_to_top ();
1494                 fade_out_handle->raise_to_top ();
1495         }
1496 }
1497
1498 void
1499 AudioRegionView::show_region_editor ()
1500 {
1501         if (editor == 0) {
1502                 editor = new AudioRegionEditor (trackview.session(), audio_region());
1503         }
1504
1505         editor->present ();
1506         editor->show_all();
1507 }
1508
1509 void
1510 AudioRegionView::transients_changed ()
1511 {
1512         AnalysisFeatureList analysis_features = _region->transients();
1513
1514         while (feature_lines.size() < analysis_features.size()) {
1515
1516                 ArdourCanvas::Line* canvas_item = new ArdourCanvas::Line(group);
1517                 CANVAS_DEBUG_NAME (canvas_item, string_compose ("transient group for %1", region()->name()));
1518
1519                 canvas_item->set (ArdourCanvas::Duple (-1.0, 2.0),
1520                                   ArdourCanvas::Duple (1.0, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
1521
1522                 canvas_item->raise_to_top ();
1523                 canvas_item->show ();
1524
1525                 canvas_item->set_data ("regionview", this);
1526                 canvas_item->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), canvas_item, this));
1527
1528                 feature_lines.push_back (make_pair(0, canvas_item));
1529         }
1530
1531         while (feature_lines.size() > analysis_features.size()) {
1532                 ArdourCanvas::Line* line = feature_lines.back().second;
1533                 feature_lines.pop_back ();
1534                 delete line;
1535         }
1536
1537         AnalysisFeatureList::const_iterator i;
1538         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1539
1540         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1541
1542                 float *pos = new float;
1543                 *pos = trackview.editor().sample_to_pixel (*i);
1544
1545                 (*l).second->set (
1546                         ArdourCanvas::Duple (*pos, 2.0),
1547                         ArdourCanvas::Duple (*pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1)
1548                         );
1549
1550                 (*l).second->set_data ("position", pos);
1551                 (*l).first = *i;
1552         }
1553 }
1554
1555 void
1556 AudioRegionView::update_transient(float /*old_pos*/, float new_pos)
1557 {
1558         /* Find frame at old pos, calulate new frame then update region transients*/
1559         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1560
1561         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1562
1563                 /* Line has been updated in drag so we compare to new_pos */
1564
1565                 float* pos = (float*) (*l).second->get_data ("position");
1566
1567                 if (rint(new_pos) == rint(*pos)) {
1568
1569                     framepos_t old_frame = (*l).first;
1570                     framepos_t new_frame = trackview.editor().pixel_to_sample (new_pos);
1571
1572                     _region->update_transient (old_frame, new_frame);
1573
1574                     break;
1575                 }
1576         }
1577 }
1578
1579 void
1580 AudioRegionView::remove_transient(float pos)
1581 {
1582         /* Find frame at old pos, calulate new frame then update region transients*/
1583         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1584
1585         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1586
1587                 /* Line has been updated in drag so we compare to new_pos */
1588                 float *line_pos = (float*) (*l).second->get_data ("position");
1589
1590                 if (rint(pos) == rint(*line_pos)) {
1591                     _region->remove_transient ((*l).first);
1592                     break;
1593                 }
1594         }
1595 }
1596
1597 void
1598 AudioRegionView::thaw_after_trim ()
1599 {
1600         RegionView::thaw_after_trim ();
1601         unhide_envelope ();
1602         drag_end ();
1603 }
1604
1605
1606 void
1607 AudioRegionView::show_xfades ()
1608 {
1609         show_start_xfade ();
1610         show_end_xfade ();
1611 }
1612
1613 void
1614 AudioRegionView::drag_start ()
1615 {
1616         TimeAxisViewItem::drag_start ();
1617
1618         //we used to hide xfades here.  I don't see the point with the new model, but we can re-implement if needed
1619 }
1620
1621 void
1622 AudioRegionView::drag_end ()
1623 {
1624         TimeAxisViewItem::drag_end ();
1625
1626         //see comment for drag_start
1627 }
1628
1629 void
1630 AudioRegionView::parameter_changed (string const & p)
1631 {
1632         if (p == "show-waveforms") {
1633                 setup_waveform_visibility ();
1634         } else if (p == "waveform-scale") {
1635                 setup_waveform_scale ();
1636         } else if (p == "waveform-shape") {
1637                 setup_waveform_shape ();
1638         } else if (p == "show-waveform-clipping") {
1639                 setup_waveform_clipping ();
1640         }
1641 }