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