e1aaf2a7a78f2d3629e5e139f4c218f15654acb9
[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
23 #include <gtkmm.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include "ardour/playlist.h"
28 #include "ardour/audioregion.h"
29 #include "ardour/audiosource.h"
30 #include "ardour/profile.h"
31 #include "ardour/session.h"
32
33 #include "pbd/memento_command.h"
34 #include "pbd/stacktrace.h"
35
36 #include "evoral/Curve.hpp"
37
38 #include "streamview.h"
39 #include "audio_region_view.h"
40 #include "audio_time_axis.h"
41 #include "simplerect.h"
42 #include "simpleline.h"
43 #include "waveview.h"
44 #include "public_editor.h"
45 #include "audio_region_editor.h"
46 #include "region_gain_line.h"
47 #include "control_point.h"
48 #include "ghostregion.h"
49 #include "audio_time_axis.h"
50 #include "utils.h"
51 #include "rgb_macros.h"
52 #include "gui_thread.h"
53 #include "ardour_ui.h"
54
55 #include "i18n.h"
56
57 #define MUTED_ALPHA 10
58
59 using namespace std;
60 using namespace ARDOUR;
61 using namespace PBD;
62 using namespace Editing;
63 using namespace ArdourCanvas;
64
65 static const int32_t sync_mark_width = 9;
66
67 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
68                                   Gdk::Color const & basic_color)
69         : RegionView (parent, tv, r, spu, basic_color)
70         , sync_mark(0)
71         , zero_line(0)
72         , fade_in_shape(0)
73         , fade_out_shape(0)
74         , fade_in_handle(0)
75         , fade_out_handle(0)
76         , gain_line(0)
77         , _amplitude_above_axis(1.0)
78         , _flags(0)
79         , fade_color(0)
80 {
81 }
82
83
84 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
85                                   Gdk::Color const & basic_color, bool recording, TimeAxisViewItem::Visibility visibility)
86         : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
87         , sync_mark(0)
88         , zero_line(0)
89         , fade_in_shape(0)
90         , fade_out_shape(0)
91         , fade_in_handle(0)
92         , fade_out_handle(0)
93         , gain_line(0)
94         , _amplitude_above_axis(1.0)
95         , _flags(0)
96         , fade_color(0)
97 {
98 }
99
100
101 AudioRegionView::AudioRegionView (const AudioRegionView& other)
102         : sigc::trackable(other)
103         , RegionView (other)
104         , zero_line(0)
105         , fade_in_shape(0)
106         , fade_out_shape(0)
107         , fade_in_handle(0)
108         , fade_out_handle(0)
109         , gain_line(0)
110         , _amplitude_above_axis(1.0)
111         , _flags(0)
112         , fade_color(0)
113 {
114         Gdk::Color c;
115         int r,g,b,a;
116
117         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
118         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
119
120         init (c, false);
121 }
122
123 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
124         : RegionView (other, boost::shared_ptr<Region> (other_region))
125         , zero_line(0)
126         , fade_in_shape(0)
127         , fade_out_shape(0)
128         , fade_in_handle(0)
129         , fade_out_handle(0)
130         , gain_line(0)
131         , _amplitude_above_axis(1.0)
132         , _flags(0)
133         , fade_color(0)
134 {
135         Gdk::Color c;
136         int r,g,b,a;
137
138         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
139         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
140
141         init (c, true);
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         XMLNode *node;
153
154         _amplitude_above_axis = 1.0;
155         zero_line             = 0;
156         _flags                = 0;
157
158         if ((node = _region->extra_xml ("GUI")) != 0) {
159                 set_flags (node);
160         } else {
161                 _flags = WaveformVisible;
162                 store_flags ();
163         }
164
165         /* make envelope visible if it has anything interesting in it */
166         if (audio_region()->envelope()->size() > 2) {
167                 _flags |= EnvelopeVisible;
168         }
169
170         compute_colors (basic_color);
171
172         create_waves ();
173
174         fade_in_shape = new ArdourCanvas::Polygon (*group);
175         fade_in_shape->property_fill_color_rgba() = fade_color;
176         fade_in_shape->set_data ("regionview", this);
177
178         fade_out_shape = new ArdourCanvas::Polygon (*group);
179         fade_out_shape->property_fill_color_rgba() = fade_color;
180         fade_out_shape->set_data ("regionview", this);
181
182         {
183                 uint32_t r,g,b,a;
184                 UINT_TO_RGBA(fill_color,&r,&g,&b,&a);
185
186                 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
187                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
188                 fade_in_handle->property_outline_pixels() = 0;
189
190                 fade_in_handle->set_data ("regionview", this);
191
192                 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
193                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
194                 fade_out_handle->property_outline_pixels() = 0;
195
196                 fade_out_handle->set_data ("regionview", this);
197         }
198
199         setup_fade_handle_positions ();
200
201         if (!trackview.session()->config.get_show_region_fades()) {
202                 set_fade_visibility (false);
203         }
204
205         const string line_name = _region->name() + ":gain";
206
207         if (!Profile->get_sae()) {
208                 gain_line = new AudioRegionGainLine (line_name, *this, *group, audio_region()->envelope());
209         }
210
211         if (!(_flags & EnvelopeVisible)) {
212                 gain_line->hide ();
213         } else {
214                 gain_line->show ();
215         }
216
217         gain_line->reset ();
218
219         set_height (trackview.current_height());
220
221         region_muted ();
222         region_sync_changed ();
223
224         region_resized (ARDOUR::bounds_change);
225         set_waveview_data_src();
226         region_locked ();
227         envelope_active_changed ();
228         fade_in_active_changed ();
229         fade_out_active_changed ();
230
231         reset_width_dependent_items (_pixel_width);
232
233         fade_in_shape->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_event), fade_in_shape, this));
234         fade_in_handle->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
235         fade_out_shape->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_event), fade_out_shape, this));
236         fade_out_handle->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
237
238         set_colors ();
239
240         /* XXX sync mark drag? */
241 }
242
243 AudioRegionView::~AudioRegionView ()
244 {
245         in_destructor = true;
246
247         RegionViewGoingAway (this); /* EMIT_SIGNAL */
248
249         for (vector<GnomeCanvasWaveViewCache *>::iterator cache = wave_caches.begin(); cache != wave_caches.end() ; ++cache) {
250                 gnome_canvas_waveview_cache_destroy (*cache);
251         }
252
253         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
254                 delete *i;
255         }
256
257         for (list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator i = feature_lines.begin(); i != feature_lines.end(); ++i) {
258                 delete ((*i).second);
259         }
260         
261         /* all waveviews etc will be destroyed when the group is destroyed */
262
263         delete gain_line;
264 }
265
266 boost::shared_ptr<ARDOUR::AudioRegion>
267 AudioRegionView::audio_region() const
268 {
269         // "Guaranteed" to succeed...
270         return boost::dynamic_pointer_cast<AudioRegion>(_region);
271 }
272
273 void
274 AudioRegionView::region_changed (const PropertyChange& what_changed)
275 {
276         ENSURE_GUI_THREAD (*this, &AudioRegionView::region_changed, what_changed);
277         // cerr << "AudioRegionView::region_changed() called" << endl;
278
279         RegionView::region_changed (what_changed);
280
281         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
282                 region_scale_amplitude_changed ();
283         }
284         if (what_changed.contains (ARDOUR::Properties::fade_in)) {
285                 fade_in_changed ();
286         }
287         if (what_changed.contains (ARDOUR::Properties::fade_out)) {
288                 fade_out_changed ();
289         }
290         if (what_changed.contains (ARDOUR::Properties::fade_in_active)) {
291                 fade_in_active_changed ();
292         }
293         if (what_changed.contains (ARDOUR::Properties::fade_out_active)) {
294                 fade_out_active_changed ();
295         }
296         if (what_changed.contains (ARDOUR::Properties::envelope_active)) {
297                 envelope_active_changed ();
298         }
299         if (what_changed.contains (ARDOUR::Properties::valid_transients)) {
300                 transients_changed ();
301         }
302 }
303
304 void
305 AudioRegionView::fade_in_changed ()
306 {
307         reset_fade_in_shape ();
308 }
309
310 void
311 AudioRegionView::fade_out_changed ()
312 {
313         reset_fade_out_shape ();
314 }
315 void
316 AudioRegionView::fade_in_active_changed ()
317 {
318 //      uint32_t r,g,b,a;
319 //      uint32_t col;
320 //      UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
321
322         if (audio_region()->fade_in_active()) {
323                 fade_in_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,90);                          // FIXME make a themeable colour
324                 fade_in_shape->property_width_pixels() = 1;
325                 fade_in_shape->property_outline_color_rgba() = RGBA_TO_UINT(180,180,180,190);                   // FIXME make a themeable colour
326         } else {
327                 fade_in_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,20);                          // FIXME make a themeable colour
328                 fade_in_shape->property_width_pixels() = 1;
329                 fade_in_shape->property_outline_color_rgba() = RGBA_TO_UINT(45,45,45,150);                      // FIXME make a themeable colour
330         }
331 }
332
333 void
334 AudioRegionView::fade_out_active_changed ()
335 {
336 //      uint32_t r,g,b,a;
337 //      uint32_t col;
338 //      UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
339
340         if (audio_region()->fade_out_active()) {
341                 fade_out_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,90);                         // FIXME make a themeable colour
342                 fade_out_shape->property_width_pixels() = 1;
343                 fade_out_shape->property_outline_color_rgba() = RGBA_TO_UINT(180,180,180,200);                  // FIXME make a themeable colour
344         } else {
345                 fade_out_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,20);                         // FIXME make a themeable colour
346                 fade_out_shape->property_width_pixels() = 1;
347                 fade_out_shape->property_outline_color_rgba() = RGBA_TO_UINT(45,45,45,200);                     // FIXME make a themeable colour
348         }
349 }
350
351
352 void
353 AudioRegionView::region_scale_amplitude_changed ()
354 {
355         ENSURE_GUI_THREAD (*this, &AudioRegionView::region_scale_amplitude_changed)
356
357         for (uint32_t n = 0; n < waves.size(); ++n) {
358                 // force a reload of the cache
359                 waves[n]->property_data_src() = _region.get();
360         }
361 }
362
363 void
364 AudioRegionView::region_renamed ()
365 {
366         Glib::ustring str = RegionView::make_name ();
367
368         if (audio_region()->speed_mismatch (trackview.session()->frame_rate())) {
369                 str = string ("*") + str;
370         }
371
372         if (_region->muted()) {
373                 str = string ("!") + str;
374         }
375
376         set_item_name (str, this);
377         set_name_text (str);
378 }
379
380 void
381 AudioRegionView::region_resized (const PropertyChange& what_changed)
382 {
383         AudioGhostRegion* agr;
384
385         RegionView::region_resized(what_changed);
386         PropertyChange interesting_stuff;
387
388         interesting_stuff.add (ARDOUR::Properties::start);
389         interesting_stuff.add (ARDOUR::Properties::length);
390
391         if (what_changed.contains (interesting_stuff)) {
392
393                 for (uint32_t n = 0; n < waves.size(); ++n) {
394                         waves[n]->property_region_start() = _region->start();
395                 }
396
397                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
398                         if ((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
399
400                                 for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
401                                         (*w)->property_region_start() = _region->start();
402                                 }
403                         }
404                 }
405                 
406                 /* hide transient lines that extend beyond the region end */
407                 
408                 list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator l;
409                 
410                 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
411                         if ((*l).first > _region->length()- 1){
412                           (*l).second->hide();
413                         }
414                         else {
415                           (*l).second->show();
416                         }
417                 }
418         }
419 }
420
421 void
422 AudioRegionView::reset_width_dependent_items (double pixel_width)
423 {
424         RegionView::reset_width_dependent_items(pixel_width);
425         assert(_pixel_width == pixel_width);
426
427         if (zero_line) {
428                 zero_line->property_x2() = pixel_width - 1.0;
429         }
430
431         if (fade_in_handle) {
432                 if (pixel_width <= 6.0) {
433                         fade_in_handle->hide();
434                         fade_out_handle->hide();
435                 } else {
436                         if (_height < 5.0) {
437                                 fade_in_handle->hide();
438                                 fade_out_handle->hide();
439                         } else {
440                                 if (trackview.session()->config.get_show_region_fades()) {
441                                         fade_in_handle->show();
442                                         fade_out_handle->show();
443                                 }
444                         }
445                 }
446         }
447
448         AnalysisFeatureList analysis_features = _region->transients();
449         AnalysisFeatureList::const_iterator i;
450         list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator l;
451
452         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
453                 (*l).second->property_x1() = trackview.editor().frame_to_pixel (*i);
454                 (*l).second->property_x2() = trackview.editor().frame_to_pixel (*i);
455         }
456         
457         reset_fade_shapes ();
458 }
459
460 void
461 AudioRegionView::region_muted ()
462 {
463         RegionView::region_muted();
464
465         for (uint32_t n=0; n < waves.size(); ++n) {
466                 if (_region->muted()) {
467                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
468                 } else {
469                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
470                 }
471         }
472 }
473
474 void
475 AudioRegionView::setup_fade_handle_positions()
476 {
477         /* position of fade handle offset from the top of the region view */
478         double const handle_pos = 2;
479         /* height of fade handles */
480         double const handle_height = 5;
481
482         if (fade_in_handle) {
483                 fade_in_handle->property_y1() = handle_pos;
484                 fade_in_handle->property_y2() = handle_pos + handle_height;
485         }
486
487         if (fade_out_handle) {
488                 fade_out_handle->property_y1() = handle_pos;
489                 fade_out_handle->property_y2() = handle_pos + handle_height;
490         }
491 }
492
493 void
494 AudioRegionView::set_height (gdouble height)
495 {
496         RegionView::set_height (height);
497
498         uint32_t wcnt = waves.size();
499
500         for (uint32_t n = 0; n < wcnt; ++n) {
501                 gdouble ht;
502
503                 if (height < NAME_HIGHLIGHT_THRESH) {
504                         ht = ((height - 2 * wcnt) / (double) wcnt);
505                 } else {
506                         ht = (((height - 2 * wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
507                 }
508
509                 gdouble yoff = n * (ht + 1);
510
511                 waves[n]->property_height() = ht;
512                 waves[n]->property_y() = yoff + 2;
513         }
514
515         if (gain_line) {
516
517                 if ((height/wcnt) < NAME_HIGHLIGHT_THRESH) {
518                         gain_line->hide ();
519                 } else {
520                         if (_flags & EnvelopeVisible) {
521                                 gain_line->show ();
522                         }
523                 }
524
525                 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE) - 2);
526         }
527
528         manage_zero_line ();
529         reset_fade_shapes ();
530         
531         /* Update hights for any active feature lines */
532         list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator l;
533
534         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
535                 (*l).second->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
536         }       
537
538         if (name_pixbuf) {
539                 name_pixbuf->raise_to_top();
540         }
541 }
542
543 void
544 AudioRegionView::manage_zero_line ()
545 {
546         if (!zero_line) {
547                 return;
548         }
549
550         if (_height >= 100) {
551                 double const wave_midpoint = (_height - NAME_HIGHLIGHT_SIZE) / 2.0;
552                 zero_line->property_y1() = wave_midpoint;
553                 zero_line->property_y2() = wave_midpoint;
554                 zero_line->show();
555         } else {
556                 zero_line->hide();
557         }
558 }
559
560 void
561 AudioRegionView::reset_fade_shapes ()
562 {
563         reset_fade_in_shape ();
564         reset_fade_out_shape ();
565 }
566
567 void
568 AudioRegionView::reset_fade_in_shape ()
569 {
570         reset_fade_in_shape_width ((nframes_t) audio_region()->fade_in()->back()->when);
571 }
572
573 void
574 AudioRegionView::reset_fade_in_shape_width (nframes_t width)
575 {
576         if (fade_in_handle == 0) {
577                 return;
578         }
579
580         /* smallest size for a fade is 64 frames */
581
582         width = std::max ((nframes_t) 64, width);
583
584         Points* points;
585         double pwidth = width / samples_per_unit;
586         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
587         double h;
588
589         if (_height < 5) {
590                 fade_in_shape->hide();
591                 fade_in_handle->hide();
592                 return;
593         }
594
595         double handle_center;
596         handle_center = pwidth;
597
598         if (handle_center > 7.0) {
599                 handle_center -= 3.0;
600         } else {
601                 handle_center = 3.0;
602         }
603
604         fade_in_handle->property_x1() =  handle_center - 3.0;
605         fade_in_handle->property_x2() =  handle_center + 3.0;
606
607         if (pwidth < 5) {
608                 fade_in_shape->hide();
609                 return;
610         }
611
612         if (trackview.session()->config.get_show_region_fades()) {
613                 fade_in_shape->show();
614         }
615
616         float curve[npoints];
617         audio_region()->fade_in()->curve().get_vector (0, audio_region()->fade_in()->back()->when, curve, npoints);
618
619         points = get_canvas_points ("fade in shape", npoints + 3);
620
621         if (_height >= NAME_HIGHLIGHT_THRESH) {
622                 h = _height - NAME_HIGHLIGHT_SIZE;
623         } else {
624                 h = _height;
625         }
626
627         /* points *MUST* be in anti-clockwise order */
628
629         uint32_t pi, pc;
630         double xdelta = pwidth/npoints;
631
632         for (pi = 0, pc = 0; pc < npoints; ++pc) {
633                 (*points)[pi].set_x(1 + (pc * xdelta));
634                 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
635         }
636
637         /* fold back */
638
639         (*points)[pi].set_x(pwidth);
640         (*points)[pi++].set_y(2);
641
642         (*points)[pi].set_x(1);
643         (*points)[pi++].set_y(2);
644
645         /* connect the dots ... */
646
647         (*points)[pi] = (*points)[0];
648
649         fade_in_shape->property_points() = *points;
650         delete points;
651
652         /* ensure trim handle stays on top */
653         frame_handle_start->raise_to_top();
654 }
655
656 void
657 AudioRegionView::reset_fade_out_shape ()
658 {
659         reset_fade_out_shape_width ((nframes_t) audio_region()->fade_out()->back()->when);
660 }
661
662 void
663 AudioRegionView::reset_fade_out_shape_width (nframes_t width)
664 {
665         if (fade_out_handle == 0) {
666                 return;
667         }
668
669         /* smallest size for a fade is 64 frames */
670
671         width = std::max ((nframes_t) 64, width);
672
673         Points* points;
674         double pwidth = width / samples_per_unit;
675         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
676         double h;
677
678         if (_height < 5) {
679                 fade_out_shape->hide();
680                 fade_out_handle->hide();
681                 return;
682         }
683
684         double handle_center;
685         handle_center = (_region->length() - width) / samples_per_unit;
686
687         if (handle_center > 7.0) {
688                 handle_center -= 3.0;
689         } else {
690                 handle_center = 3.0;
691         }
692
693         fade_out_handle->property_x1() =  handle_center - 3.0;
694         fade_out_handle->property_x2() =  handle_center + 3.0;
695
696         /* don't show shape if its too small */
697
698         if (pwidth < 5) {
699                 fade_out_shape->hide();
700                 return;
701         }
702
703         if (trackview.session()->config.get_show_region_fades()) {
704                 fade_out_shape->show();
705         }
706
707         float curve[npoints];
708         audio_region()->fade_out()->curve().get_vector (0, audio_region()->fade_out()->back()->when, curve, npoints);
709
710         if (_height >= NAME_HIGHLIGHT_THRESH) {
711                 h = _height - NAME_HIGHLIGHT_SIZE;
712         } else {
713                 h = _height;
714         }
715
716         /* points *MUST* be in anti-clockwise order */
717
718         points = get_canvas_points ("fade out shape", npoints + 3);
719
720         uint32_t pi, pc;
721         double xdelta = pwidth/npoints;
722
723         for (pi = 0, pc = 0; pc < npoints; ++pc) {
724                 (*points)[pi].set_x(_pixel_width - 1 - pwidth + (pc*xdelta));
725                 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
726         }
727
728         /* fold back */
729
730         (*points)[pi].set_x(_pixel_width);
731         (*points)[pi++].set_y(h);
732
733         (*points)[pi].set_x(_pixel_width);
734         (*points)[pi++].set_y(2);
735
736         /* connect the dots ... */
737
738         (*points)[pi] = (*points)[0];
739
740         fade_out_shape->property_points() = *points;
741         delete points;
742
743         /* ensure trim handle stays on top */
744         frame_handle_end->raise_to_top();
745 }
746
747 void
748 AudioRegionView::set_samples_per_unit (gdouble spu)
749 {
750         RegionView::set_samples_per_unit (spu);
751
752         if (_flags & WaveformVisible) {
753                 for (uint32_t n=0; n < waves.size(); ++n) {
754                         waves[n]->property_samples_per_unit() = spu;
755                 }
756         }
757
758         if (gain_line) {
759                 gain_line->reset ();
760         }
761
762         reset_fade_shapes ();
763 }
764
765 void
766 AudioRegionView::set_amplitude_above_axis (gdouble spp)
767 {
768         for (uint32_t n=0; n < waves.size(); ++n) {
769                 waves[n]->property_amplitude_above_axis() = spp;
770         }
771 }
772
773 void
774 AudioRegionView::compute_colors (Gdk::Color const & basic_color)
775 {
776         RegionView::compute_colors (basic_color);
777
778         uint32_t r, g, b, a;
779
780         /* gain color computed in envelope_active_changed() */
781
782         UINT_TO_RGBA (fill_color, &r, &g, &b, &a);
783         fade_color = RGBA_TO_UINT(r,g,b,120);
784 }
785
786 void
787 AudioRegionView::set_colors ()
788 {
789         RegionView::set_colors();
790
791         if (gain_line) {
792                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
793         }
794
795         for (uint32_t n=0; n < waves.size(); ++n) {
796                 if (_region->muted()) {
797                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
798                 } else {
799                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
800                 }
801
802                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
803                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
804         }
805 }
806
807 void
808 AudioRegionView::set_waveform_visible (bool yn)
809 {
810         if (((_flags & WaveformVisible) != yn)) {
811                 if (yn) {
812                         for (uint32_t n=0; n < waves.size(); ++n) {
813                                 /* make sure the zoom level is correct, since we don't update
814                                    this when waveforms are hidden.
815                                 */
816                                 waves[n]->property_samples_per_unit() = samples_per_unit;
817                                 waves[n]->show();
818                         }
819                         _flags |= WaveformVisible;
820                 } else {
821                         for (uint32_t n=0; n < waves.size(); ++n) {
822                                 waves[n]->hide();
823                         }
824                         _flags &= ~WaveformVisible;
825                 }
826                 store_flags ();
827         }
828 }
829
830 void
831 AudioRegionView::temporarily_hide_envelope ()
832 {
833         if (gain_line) {
834                 gain_line->hide ();
835         }
836 }
837
838 void
839 AudioRegionView::unhide_envelope ()
840 {
841         if (gain_line && (_flags & EnvelopeVisible)) {
842                 gain_line->show ();
843         }
844 }
845
846 void
847 AudioRegionView::set_envelope_visible (bool yn)
848 {
849         if (gain_line && ((_flags & EnvelopeVisible) != yn)) {
850                 if (yn) {
851                         gain_line->show ();
852                         _flags |= EnvelopeVisible;
853                 } else {
854                         gain_line->hide ();
855                         _flags &= ~EnvelopeVisible;
856                 }
857                 store_flags ();
858         }
859 }
860
861 void
862 AudioRegionView::create_waves ()
863 {
864         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
865         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
866
867         if (!atv.track()) {
868                 return;
869         }
870
871         ChanCount nchans = atv.track()->n_channels();
872
873         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
874         //              << " and channels = " << nchans.n_audio() << endl;
875
876         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
877         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
878                 tmp_waves.push_back (0);
879         }
880
881         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
882                 delete *i;
883         }
884
885         _data_ready_connections.clear ();
886
887         for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
888                 _data_ready_connections.push_back (0);
889         }
890
891         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
892
893                 if (n >= audio_region()->n_channels()) {
894                         break;
895                 }
896
897                 wave_caches.push_back (WaveView::create_cache ());
898
899                 // cerr << "\tchannel " << n << endl;
900
901                 if (wait_for_data) {
902                         if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
903                                 // cerr << "\tData is ready\n";
904                                 create_one_wave (n, true);
905                         } else {
906                                 // cerr << "\tdata is not ready\n";
907                                 // we'll get a PeaksReady signal from the source in the future
908                                 // and will call create_one_wave(n) then.
909                         }
910
911                 } else {
912                         // cerr << "\tdon't delay, display today!\n";
913                         create_one_wave (n, true);
914                 }
915
916         }
917 }
918
919 void
920 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
921 {
922         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
923         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
924         uint32_t nchans = atv.track()->n_channels().n_audio();
925         uint32_t n;
926         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
927         gdouble ht;
928
929         if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
930                 ht = ((trackview.current_height()) / (double) nchans);
931         } else {
932                 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
933         }
934
935         gdouble yoff = which * ht;
936
937         WaveView *wave = new WaveView(*group);
938
939         wave->property_data_src() = (gpointer) _region.get();
940         wave->property_cache() =  wave_caches[which];
941         wave->property_cache_updater() = true;
942         wave->property_channel() =  which;
943         wave->property_length_function() = (gpointer) region_length_from_c;
944         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
945         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
946         wave->property_x() =  0.0;
947         wave->property_y() =  yoff;
948         wave->property_height() =  (double) ht;
949         wave->property_samples_per_unit() =  samples_per_unit;
950         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
951
952         if (_recregion) {
953                 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
954                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
955         } else {
956                 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
957                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
958         }
959
960         wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
961         wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
962         wave->property_region_start() = _region->start();
963         wave->property_rectified() = (bool) (_flags & WaveformRectified);
964         wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
965
966         if (!(_flags & WaveformVisible)) {
967                 wave->hide();
968         }
969
970         /* note: calling this function is serialized by the lock
971            held in the peak building thread that signals that
972            peaks are ready for use *or* by the fact that it is
973            called one by one from the GUI thread.
974         */
975
976         if (which < nchans) {
977                 tmp_waves[which] = wave;
978         } else {
979                 /* n-channel track, >n-channel source */
980         }
981
982         /* see if we're all ready */
983
984         for (n = 0; n < nchans; ++n) {
985                 if (tmp_waves[n] == 0) {
986                         break;
987                 }
988         }
989
990         if (n == nwaves && waves.empty()) {
991                 /* all waves are ready */
992                 tmp_waves.resize(nwaves);
993
994                 waves = tmp_waves;
995                 tmp_waves.clear ();
996
997                 /* all waves created, don't hook into peaks ready anymore */
998                 delete _data_ready_connections[which];
999                 _data_ready_connections[which] = 0;
1000
1001 #if 0
1002                 if (!zero_line) {
1003                         zero_line = new ArdourCanvas::SimpleLine (*group);
1004                         zero_line->property_x1() = (gdouble) 1.0;
1005                         zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
1006                         zero_line->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1007                         manage_zero_line ();
1008                 }
1009 #endif
1010         }
1011 }
1012
1013 void
1014 AudioRegionView::peaks_ready_handler (uint32_t which)
1015 {
1016         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AudioRegionView::create_one_wave, this, which, false));
1017         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
1018 }
1019
1020 void
1021 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
1022 {
1023         if (gain_line == 0) {
1024                 return;
1025         }
1026
1027         double x, y;
1028
1029         /* don't create points that can't be seen */
1030
1031         set_envelope_visible (true);
1032
1033         x = ev->button.x;
1034         y = ev->button.y;
1035
1036         item->w2i (x, y);
1037
1038         nframes_t fx = trackview.editor().pixel_to_frame (x);
1039
1040         if (fx > _region->length()) {
1041                 return;
1042         }
1043
1044         /* compute vertical fractional position */
1045
1046         y = 1.0 - (y / (_height - NAME_HIGHLIGHT_SIZE));
1047
1048         /* map using gain line */
1049
1050         gain_line->view_to_model_coord (x, y);
1051
1052         /* XXX STATEFUL: can't convert to stateful diff until we 
1053            can represent automation data with it.
1054         */
1055
1056         trackview.session()->begin_reversible_command (_("add gain control point"));
1057         XMLNode &before = audio_region()->envelope()->get_state();
1058
1059         if (!audio_region()->envelope_active()) {
1060                 XMLNode &region_before = audio_region()->get_state();
1061                 audio_region()->set_envelope_active(true);
1062                 XMLNode &region_after = audio_region()->get_state();
1063                 trackview.session()->add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
1064         }
1065
1066         audio_region()->envelope()->add (fx, y);
1067
1068         XMLNode &after = audio_region()->envelope()->get_state();
1069         trackview.session()->add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1070         trackview.session()->commit_reversible_command ();
1071 }
1072
1073 void
1074 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent */*ev*/)
1075 {
1076         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1077         audio_region()->envelope()->erase (cp->model());
1078 }
1079
1080 void
1081 AudioRegionView::store_flags()
1082 {
1083         XMLNode *node = new XMLNode ("GUI");
1084
1085         node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
1086         node->add_property ("envelope-visible", (_flags & EnvelopeVisible) ? "yes" : "no");
1087         node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1088         node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1089
1090         _region->add_extra_xml (*node);
1091 }
1092
1093 void
1094 AudioRegionView::set_flags (XMLNode* node)
1095 {
1096         XMLProperty *prop;
1097
1098         if ((prop = node->property ("waveform-visible")) != 0) {
1099                 if (string_is_affirmative (prop->value())) {
1100                         _flags |= WaveformVisible;
1101                 }
1102         }
1103
1104         if ((prop = node->property ("envelope-visible")) != 0) {
1105                 if (string_is_affirmative (prop->value())) {
1106                         _flags |= EnvelopeVisible;
1107                 }
1108         }
1109
1110         if ((prop = node->property ("waveform-rectified")) != 0) {
1111                 if (string_is_affirmative (prop->value())) {
1112                         _flags |= WaveformRectified;
1113                 }
1114         }
1115
1116         if ((prop = node->property ("waveform-logscaled")) != 0) {
1117                 if (string_is_affirmative (prop->value())) {
1118                         _flags |= WaveformLogScaled;
1119                 }
1120         }
1121 }
1122
1123 void
1124 AudioRegionView::set_waveform_shape (WaveformShape shape)
1125 {
1126         bool yn;
1127
1128         /* this slightly odd approach is to leave the door open to
1129            other "shapes" such as spectral displays, etc.
1130         */
1131
1132         switch (shape) {
1133         case Rectified:
1134                 yn = true;
1135                 break;
1136
1137         default:
1138                 yn = false;
1139                 break;
1140         }
1141
1142         if (yn != (bool) (_flags & WaveformRectified)) {
1143                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1144                         (*wave)->property_rectified() = yn;
1145                 }
1146
1147                 if (zero_line) {
1148                         if (yn) {
1149                                 zero_line->hide();
1150                         } else {
1151                                 zero_line->show();
1152                         }
1153                 }
1154
1155                 if (yn) {
1156                         _flags |= WaveformRectified;
1157                 } else {
1158                         _flags &= ~WaveformRectified;
1159                 }
1160                 store_flags ();
1161         }
1162 }
1163
1164 void
1165 AudioRegionView::set_waveform_scale (WaveformScale scale)
1166 {
1167         bool yn = (scale == Logarithmic);
1168
1169         if (yn != (bool) (_flags & WaveformLogScaled)) {
1170                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1171                         (*wave)->property_logscaled() = yn;
1172                 }
1173
1174                 if (yn) {
1175                         _flags |= WaveformLogScaled;
1176                 } else {
1177                         _flags &= ~WaveformLogScaled;
1178                 }
1179                 store_flags ();
1180         }
1181 }
1182
1183
1184 GhostRegion*
1185 AudioRegionView::add_ghost (TimeAxisView& tv)
1186 {
1187         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1188         assert(rtv);
1189
1190         double unit_position = _region->position () / samples_per_unit;
1191         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1192         uint32_t nchans;
1193
1194         nchans = rtv->track()->n_channels().n_audio();
1195
1196         for (uint32_t n = 0; n < nchans; ++n) {
1197
1198                 if (n >= audio_region()->n_channels()) {
1199                         break;
1200                 }
1201
1202                 WaveView *wave = new WaveView(*ghost->group);
1203
1204                 wave->property_data_src() = _region.get();
1205                 wave->property_cache() =  wave_caches[n];
1206                 wave->property_cache_updater() = false;
1207                 wave->property_channel() = n;
1208                 wave->property_length_function() = (gpointer)region_length_from_c;
1209                 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1210                 wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
1211                 wave->property_x() =  0.0;
1212                 wave->property_samples_per_unit() =  samples_per_unit;
1213                 wave->property_amplitude_above_axis() =  _amplitude_above_axis;
1214
1215                 wave->property_region_start() = _region->start();
1216
1217                 ghost->waves.push_back(wave);
1218         }
1219
1220         ghost->set_height ();
1221         ghost->set_duration (_region->length() / samples_per_unit);
1222         ghost->set_colors();
1223         ghosts.push_back (ghost);
1224
1225         return ghost;
1226 }
1227
1228 void
1229 AudioRegionView::entered (bool internal_editing)
1230 {
1231         if (gain_line && _flags & EnvelopeVisible) {
1232                 gain_line->show_all_control_points ();
1233         }
1234
1235         uint32_t r,g,b,a;
1236         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1237         a=255;
1238
1239         if (fade_in_handle && !internal_editing) {
1240                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1241                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1242         }
1243 }
1244
1245 void
1246 AudioRegionView::exited ()
1247 {
1248         if (gain_line) {
1249                 gain_line->hide_all_but_selected_control_points ();
1250         }
1251
1252         uint32_t r,g,b,a;
1253         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1254         a=0;
1255
1256         if (fade_in_handle) {
1257                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1258                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1259         }
1260 }
1261
1262 void
1263 AudioRegionView::envelope_active_changed ()
1264 {
1265         if (gain_line) {
1266                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1267         }
1268 }
1269
1270 void
1271 AudioRegionView::set_waveview_data_src()
1272 {
1273         AudioGhostRegion* agr;
1274         double unit_length= _region->length() / samples_per_unit;
1275
1276         for (uint32_t n = 0; n < waves.size(); ++n) {
1277                 // TODO: something else to let it know the channel
1278                 waves[n]->property_data_src() = _region.get();
1279         }
1280
1281         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1282
1283                 (*i)->set_duration (unit_length);
1284
1285                 if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
1286                         for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
1287                                 (*w)->property_data_src() = _region.get();
1288                         }
1289                 }
1290         }
1291
1292 }
1293
1294 void
1295 AudioRegionView::color_handler ()
1296 {
1297         //case cMutedWaveForm:
1298         //case cWaveForm:
1299         //case cWaveFormClip:
1300         //case cZeroLine:
1301         set_colors ();
1302
1303         //case cGainLineInactive:
1304         //case cGainLine:
1305         envelope_active_changed();
1306
1307 }
1308
1309 void
1310 AudioRegionView::set_frame_color ()
1311 {
1312         if (!frame) {
1313                 return;
1314         }
1315
1316         if (_region->opaque()) {
1317                 fill_opacity = 130;
1318         } else {
1319                 fill_opacity = 0;
1320         }
1321
1322         uint32_t r,g,b,a;
1323
1324         if (_selected && should_show_selection) {
1325                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get(), &r, &g, &b, &a);
1326                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1327
1328                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1329                         if (_region->muted()) {
1330                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1331                         } else {
1332                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1333                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1334                         }
1335                 }
1336         } else {
1337                 if (_recregion) {
1338                         UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_RecordingRect.get(), &r, &g, &b, &a);
1339                         frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, a);
1340
1341                         for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1342                                 if (_region->muted()) {
1343                                         (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA);
1344                                 } else {
1345                                         (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
1346                                         (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
1347                                 }
1348                         }
1349                 } else {
1350                         UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_FrameBase.get(), &r, &g, &b, &a);
1351                         frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1352
1353                         for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1354                                 if (_region->muted()) {
1355                                         (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1356                                 } else {
1357                                         (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1358                                         (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1359                                 }
1360                         }
1361                 }
1362         }
1363 }
1364
1365 void
1366 AudioRegionView::set_fade_visibility (bool yn)
1367 {
1368         if (yn) {
1369                 if (fade_in_shape) {
1370                         fade_in_shape->show();
1371                 }
1372                 if (fade_out_shape) {
1373                         fade_out_shape->show ();
1374                 }
1375                 if (fade_in_handle) {
1376                         fade_in_handle->show ();
1377                 }
1378                 if (fade_out_handle) {
1379                         fade_out_handle->show ();
1380                 }
1381         } else {
1382                 if (fade_in_shape) {
1383                         fade_in_shape->hide();
1384                 }
1385                 if (fade_out_shape) {
1386                         fade_out_shape->hide ();
1387                 }
1388                 if (fade_in_handle) {
1389                         fade_in_handle->hide ();
1390                 }
1391                 if (fade_out_handle) {
1392                         fade_out_handle->hide ();
1393                 }
1394         }
1395 }
1396
1397 void
1398 AudioRegionView::update_coverage_frames (LayerDisplay d)
1399 {
1400         RegionView::update_coverage_frames (d);
1401
1402         fade_in_handle->raise_to_top ();
1403         fade_out_handle->raise_to_top ();
1404 }
1405
1406 void
1407 AudioRegionView::show_region_editor ()
1408 {
1409         if (editor == 0) {
1410                 editor = new AudioRegionEditor (trackview.session(), audio_region());
1411         }
1412
1413         editor->present ();
1414         editor->show_all();
1415 }
1416
1417 void
1418 AudioRegionView::transients_changed ()
1419 {
1420         AnalysisFeatureList analysis_features = _region->transients();
1421
1422         while (feature_lines.size() < analysis_features.size()) {
1423                 ArdourCanvas::SimpleLine* l = new ArdourCanvas::SimpleLine (*group);
1424                 l->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1425                 feature_lines.push_back (make_pair(0, l));
1426         }
1427
1428         while (feature_lines.size() > analysis_features.size()) {
1429                 ArdourCanvas::SimpleLine *line = feature_lines.back().second;
1430                 feature_lines.pop_back ();
1431                 delete line;
1432         }
1433
1434         AnalysisFeatureList::const_iterator i;
1435         list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator l;
1436
1437         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1438                 (*l).first = *i;
1439                 (*l).second->property_x1() = trackview.editor().frame_to_pixel (*i);
1440                 (*l).second->property_x2() = trackview.editor().frame_to_pixel (*i);            
1441                 (*l).second->property_y1() = 2;
1442                 (*l).second->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
1443                 (*l).second->set_data("regionview", this);
1444                 (*l).second->show ();
1445                 (*l).second->raise_to_top ();
1446                 
1447                 (*l).second->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), (*l).second, this));
1448         }
1449 }
1450
1451 void
1452 AudioRegionView::update_transient(float old_pos, float new_pos)
1453 {
1454         /* Find frame at old pos, calulate new frame then update region transients*/
1455         list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator l;
1456
1457         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1458                 /* Simple line x1 has been updated in drag so we compare to new_pos */
1459                 if (rint(new_pos) == rint((*l).second->property_x1())) {
1460                     
1461                     nframes64_t old_frame = (*l).first;
1462                     nframes64_t new_frame = trackview.editor().pixel_to_frame (new_pos);
1463
1464                     _region->update_transient (old_frame, new_frame);
1465                     
1466                     break;
1467                 }
1468         }
1469 }
1470
1471 void
1472 AudioRegionView::remove_transient(float pos)
1473 {
1474         /* Find frame at old pos, calulate new frame then update region transients*/
1475         list<std::pair<nframes64_t, ArdourCanvas::SimpleLine*> >::iterator l;
1476
1477         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1478                 /* Simple line x1 has been updated in drag so we compare to new_pos */
1479                 if (rint(pos) == rint((*l).second->property_x1())) {
1480                     _region->remove_transient ((*l).first);                 
1481                     break;
1482                 }
1483         }
1484 }