patch from rodrigo severo to make mackie timecode display work
[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 <boost/scoped_ptr.hpp>
24
25 #include <gtkmm.h>
26
27 #include <gtkmm2ext/gtk_ui.h>
28
29 #include "ardour/playlist.h"
30 #include "ardour/audioregion.h"
31 #include "ardour/audiosource.h"
32 #include "ardour/profile.h"
33 #include "ardour/session.h"
34
35 #include "pbd/memento_command.h"
36 #include "pbd/stacktrace.h"
37
38 #include "evoral/Curve.hpp"
39
40 #include "streamview.h"
41 #include "audio_region_view.h"
42 #include "audio_time_axis.h"
43 #include "simplerect.h"
44 #include "simpleline.h"
45 #include "waveview.h"
46 #include "public_editor.h"
47 #include "audio_region_editor.h"
48 #include "audio_streamview.h"
49 #include "region_gain_line.h"
50 #include "control_point.h"
51 #include "ghostregion.h"
52 #include "audio_time_axis.h"
53 #include "utils.h"
54 #include "rgb_macros.h"
55 #include "gui_thread.h"
56 #include "ardour_ui.h"
57
58 #include "i18n.h"
59
60 #define MUTED_ALPHA 10
61
62 using namespace std;
63 using namespace ARDOUR;
64 using namespace PBD;
65 using namespace Editing;
66 using namespace ArdourCanvas;
67
68 static const int32_t sync_mark_width = 9;
69
70 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
71                                   Gdk::Color const & basic_color)
72         : RegionView (parent, tv, r, spu, basic_color)
73         , sync_mark(0)
74         , fade_in_shape(0)
75         , fade_out_shape(0)
76         , fade_in_handle(0)
77         , fade_out_handle(0)
78         , fade_position_line(0)
79         , start_xfade_in (0)
80         , start_xfade_out (0)
81         , start_xfade_rect (0)
82         , end_xfade_in (0)
83         , end_xfade_out (0)
84         , end_xfade_rect (0)
85         , _amplitude_above_axis(1.0)
86         , _flags(0)
87         , fade_color(0)
88 {
89 }
90
91 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
92                                   Gdk::Color const & basic_color, bool recording, TimeAxisViewItem::Visibility visibility)
93         : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
94         , sync_mark(0)
95         , fade_in_shape(0)
96         , fade_out_shape(0)
97         , fade_in_handle(0)
98         , fade_out_handle(0)
99         , fade_position_line(0)
100         , start_xfade_in (0)
101         , start_xfade_out (0)
102         , start_xfade_rect (0)
103         , end_xfade_in (0)
104         , end_xfade_out (0)
105         , end_xfade_rect (0)
106         , _amplitude_above_axis(1.0)
107         , _flags(0)
108         , fade_color(0)
109 {
110 }
111
112 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
113         : RegionView (other, boost::shared_ptr<Region> (other_region))
114         , fade_in_shape(0)
115         , fade_out_shape(0)
116         , fade_in_handle(0)
117         , fade_out_handle(0)
118         , fade_position_line(0)
119         , start_xfade_in (0)
120         , start_xfade_out (0)
121         , start_xfade_rect (0)
122         , end_xfade_in (0)
123         , end_xfade_out (0)
124         , end_xfade_rect (0)
125         , _amplitude_above_axis (other._amplitude_above_axis)
126         , _flags (other._flags)
127         , fade_color(0)
128 {
129         Gdk::Color c;
130         int r,g,b,a;
131
132         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
133         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
134
135         init (c, true);
136 }
137
138 void
139 AudioRegionView::init (Gdk::Color const & basic_color, bool wfd)
140 {
141         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
142         // where order is important and where it isn't...
143
144         RegionView::init (basic_color, wfd);
145
146         XMLNode *node;
147
148         _amplitude_above_axis = 1.0;
149         _flags                = 0;
150
151         if ((node = _region->extra_xml ("GUI")) != 0) {
152                 set_flags (node);
153         } else {
154                 _flags = WaveformVisible;
155                 store_flags ();
156         }
157
158         compute_colors (basic_color);
159
160         create_waves ();
161
162         fade_in_shape = new ArdourCanvas::Polygon (*group);
163         fade_in_shape->property_fill_color_rgba() = fade_color;
164         fade_in_shape->set_data ("regionview", this);
165
166         fade_out_shape = new ArdourCanvas::Polygon (*group);
167         fade_out_shape->property_fill_color_rgba() = fade_color;
168         fade_out_shape->set_data ("regionview", this);
169
170         if (!_recregion) {
171                 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
172                 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fill_color, 0);
173                 fade_in_handle->property_outline_pixels() = 0;
174
175                 fade_in_handle->set_data ("regionview", this);
176
177                 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
178                 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fill_color, 0);
179                 fade_out_handle->property_outline_pixels() = 0;
180
181                 fade_out_handle->set_data ("regionview", this);
182
183                 fade_position_line = new ArdourCanvas::SimpleLine (*group);
184                 fade_position_line->property_color_rgba() = 0xBBBBBBAA;
185                 fade_position_line->property_y1() = 7;
186                 fade_position_line->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
187
188                 fade_position_line->hide();
189         }
190
191         setup_fade_handle_positions ();
192
193         if (!trackview.session()->config.get_show_region_fades()) {
194                 set_fade_visibility (false);
195         }
196
197         const string line_name = _region->name() + ":gain";
198
199         if (!Profile->get_sae()) {
200                 gain_line.reset (new AudioRegionGainLine (line_name, *this, *group, audio_region()->envelope()));
201         }
202         
203         update_envelope_visibility ();
204         gain_line->reset ();
205
206         set_height (trackview.current_height());
207
208         region_muted ();
209         region_sync_changed ();
210
211         region_resized (ARDOUR::bounds_change);
212         set_waveview_data_src();
213         region_locked ();
214         envelope_active_changed ();
215         fade_in_active_changed ();
216         fade_out_active_changed ();
217
218         reset_width_dependent_items (_pixel_width);
219
220         fade_in_shape->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_event), fade_in_shape, this));
221         if (fade_in_handle) {
222                 fade_in_handle->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
223         }
224
225         fade_out_shape->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_event), fade_out_shape, this));
226
227         if (fade_out_handle) {
228                 fade_out_handle->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
229         }
230
231         set_colors ();
232
233         /* XXX sync mark drag? */
234 }
235
236 AudioRegionView::~AudioRegionView ()
237 {
238         in_destructor = true;
239
240         RegionViewGoingAway (this); /* EMIT_SIGNAL */
241
242         for (vector<GnomeCanvasWaveViewCache *>::iterator cache = wave_caches.begin(); cache != wave_caches.end() ; ++cache) {
243                 gnome_canvas_waveview_cache_destroy (*cache);
244         }
245
246         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
247                 delete *i;
248         }
249
250         for (list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator i = feature_lines.begin(); i != feature_lines.end(); ++i) {
251                 delete ((*i).second);
252         }
253
254         /* all waveviews etc will be destroyed when the group is destroyed */
255 }
256
257 boost::shared_ptr<ARDOUR::AudioRegion>
258 AudioRegionView::audio_region() const
259 {
260         // "Guaranteed" to succeed...
261         return boost::dynamic_pointer_cast<AudioRegion>(_region);
262 }
263
264 void
265 AudioRegionView::region_changed (const PropertyChange& what_changed)
266 {
267         ENSURE_GUI_THREAD (*this, &AudioRegionView::region_changed, what_changed);
268         // cerr << "AudioRegionView::region_changed() called" << endl;
269
270         RegionView::region_changed (what_changed);
271
272         if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
273                 region_scale_amplitude_changed ();
274         }
275         if (what_changed.contains (ARDOUR::Properties::fade_in)) {
276                 fade_in_changed ();
277         }
278         if (what_changed.contains (ARDOUR::Properties::fade_out)) {
279                 fade_out_changed ();
280         }
281         if (what_changed.contains (ARDOUR::Properties::fade_in_active)) {
282                 fade_in_active_changed ();
283         }
284         if (what_changed.contains (ARDOUR::Properties::fade_out_active)) {
285                 fade_out_active_changed ();
286         }
287         if (what_changed.contains (ARDOUR::Properties::envelope_active)) {
288                 envelope_active_changed ();
289         }
290         if (what_changed.contains (ARDOUR::Properties::valid_transients)) {
291                 transients_changed ();
292         }
293 }
294
295 void
296 AudioRegionView::fade_in_changed ()
297 {
298         reset_fade_in_shape ();
299 }
300
301 void
302 AudioRegionView::fade_out_changed ()
303 {
304         reset_fade_out_shape ();
305 }
306
307 void
308 AudioRegionView::fade_in_active_changed ()
309 {
310         if (audio_region()->fade_in_active()) {
311                 fade_in_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,90);                          // FIXME make a themeable colour
312                 fade_in_shape->property_width_pixels() = 1;
313         } else {
314                 fade_in_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,20);                          // FIXME make a themeable colour
315                 fade_in_shape->property_width_pixels() = 1;
316         }
317 }
318
319 void
320 AudioRegionView::fade_out_active_changed ()
321 {
322         if (audio_region()->fade_out_active()) {
323                 fade_out_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,90);                         // FIXME make a themeable colour
324                 fade_out_shape->property_width_pixels() = 1;
325         } else {
326                 fade_out_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,20);                         // FIXME make a themeable colour
327                 fade_out_shape->property_width_pixels() = 1;
328         }
329 }
330
331
332 void
333 AudioRegionView::region_scale_amplitude_changed ()
334 {
335         ENSURE_GUI_THREAD (*this, &AudioRegionView::region_scale_amplitude_changed)
336
337         for (uint32_t n = 0; n < waves.size(); ++n) {
338                 // force a reload of the cache
339                 waves[n]->property_data_src() = _region.get();
340         }
341 }
342
343 void
344 AudioRegionView::region_renamed ()
345 {
346         std::string str = RegionView::make_name ();
347
348         if (audio_region()->speed_mismatch (trackview.session()->frame_rate())) {
349                 str = string ("*") + str;
350         }
351
352         if (_region->muted()) {
353                 str = string ("!") + str;
354         }
355
356         set_item_name (str, this);
357         set_name_text (str);
358 }
359
360 void
361 AudioRegionView::region_resized (const PropertyChange& what_changed)
362 {
363         AudioGhostRegion* agr;
364
365         RegionView::region_resized(what_changed);
366         PropertyChange interesting_stuff;
367
368         interesting_stuff.add (ARDOUR::Properties::start);
369         interesting_stuff.add (ARDOUR::Properties::length);
370
371         if (what_changed.contains (interesting_stuff)) {
372
373                 for (uint32_t n = 0; n < waves.size(); ++n) {
374                         waves[n]->property_region_start() = _region->start();
375                 }
376
377                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
378                         if ((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
379
380                                 for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
381                                         (*w)->property_region_start() = _region->start();
382                                 }
383                         }
384                 }
385
386                 /* hide transient lines that extend beyond the region end */
387
388                 list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
389
390                 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
391                         if (l->first > _region->length() - 1) {
392                                 l->second->hide();
393                         } else {
394                                 l->second->show();
395                         }
396                 }
397         }
398 }
399
400 void
401 AudioRegionView::reset_width_dependent_items (double pixel_width)
402 {
403         RegionView::reset_width_dependent_items(pixel_width);
404         assert(_pixel_width == pixel_width);
405
406         if (fade_in_handle) {
407                 if (pixel_width <= 6.0 || _height < 5.0 || !trackview.session()->config.get_show_region_fades()) {
408                         fade_in_handle->hide();
409                         fade_out_handle->hide();
410                 }
411                 else {
412                         fade_in_handle->show();
413                         fade_out_handle->show();
414                 }
415         }
416
417         AnalysisFeatureList analysis_features = _region->transients();
418         AnalysisFeatureList::const_iterator i;
419
420         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
421
422         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
423
424                 float x_pos = trackview.editor().frame_to_pixel (*i);
425
426                 ArdourCanvas::Points points;
427                 points.push_back(Gnome::Art::Point(x_pos, 2.0)); // first x-coord needs to be a non-normal value
428                 points.push_back(Gnome::Art::Point(x_pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
429
430                 (*l).first = *i;
431                 (*l).second->property_points() = points;
432         }
433
434         reset_fade_shapes ();
435 }
436
437 void
438 AudioRegionView::region_muted ()
439 {
440         RegionView::region_muted();
441
442         for (uint32_t n=0; n < waves.size(); ++n) {
443                 if (_region->muted()) {
444                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
445                 } else {
446                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
447                 }
448         }
449 }
450
451 void
452 AudioRegionView::setup_fade_handle_positions()
453 {
454         /* position of fade handle offset from the top of the region view */
455         double const handle_pos = 2;
456         /* height of fade handles */
457         double const handle_height = 5;
458
459         if (fade_in_handle) {
460                 fade_in_handle->property_y1() = handle_pos;
461                 fade_in_handle->property_y2() = handle_pos + handle_height;
462         }
463
464         if (fade_out_handle) {
465                 fade_out_handle->property_y1() = handle_pos;
466                 fade_out_handle->property_y2() = handle_pos + handle_height;
467         }
468 }
469
470 void
471 AudioRegionView::set_height (gdouble height)
472 {
473         RegionView::set_height (height);
474
475         uint32_t wcnt = waves.size();
476
477         for (uint32_t n = 0; n < wcnt; ++n) {
478                 gdouble ht;
479
480                 if (height < NAME_HIGHLIGHT_THRESH) {
481                         ht = ((height - 2 * wcnt) / (double) wcnt);
482                 } else {
483                         ht = (((height - 2 * wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
484                 }
485
486                 gdouble yoff = n * (ht + 1);
487
488                 waves[n]->property_height() = ht;
489                 waves[n]->property_y() = yoff + 2;
490         }
491
492         if (gain_line) {
493
494                 if ((height/wcnt) < NAME_HIGHLIGHT_THRESH) {
495                         gain_line->hide ();
496                 } else {
497                         update_envelope_visibility ();
498                 }
499
500                 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE) - 2);
501         }
502
503         reset_fade_shapes ();
504
505         /* Update hights for any active feature lines */
506         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
507
508         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
509
510                 float pos_x = trackview.editor().frame_to_pixel((*l).first);
511
512                 ArdourCanvas::Points points;
513
514                 points.push_back(Gnome::Art::Point(pos_x, 2.0)); // first x-coord needs to be a non-normal value
515                 points.push_back(Gnome::Art::Point(pos_x, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
516
517                 (*l).second->property_points() = points;
518         }
519
520         if (fade_position_line) {
521
522                 if (height < NAME_HIGHLIGHT_THRESH) {
523                         fade_position_line->property_y2() = _height - 1;
524                 }
525                 else {
526                         fade_position_line->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
527                 }
528         }
529
530         if (name_pixbuf) {
531                 name_pixbuf->raise_to_top();
532         }
533 }
534
535 void
536 AudioRegionView::reset_fade_shapes ()
537 {
538         reset_fade_in_shape ();
539         reset_fade_out_shape ();
540 }
541
542 void
543 AudioRegionView::reset_fade_in_shape ()
544 {
545         reset_fade_in_shape_width ((framecnt_t) audio_region()->fade_in()->back()->when);
546 }
547
548 void
549 AudioRegionView::reset_fade_in_shape_width (framecnt_t width)
550 {
551         if (dragging()) {
552                 return;
553         }
554
555         if (audio_region()->fade_in_is_xfade()) {
556                 if (fade_in_handle) {
557                         fade_in_handle->hide ();
558                         fade_in_shape->hide ();
559                 }
560                 redraw_start_xfade ();
561                 return;
562         } else {
563                 if (start_xfade_in) {
564                         start_xfade_in->hide ();
565                         start_xfade_out->hide ();
566                         start_xfade_rect->hide ();
567                 }
568         }
569
570         if (fade_in_handle == 0) {
571                 return;
572         }
573
574         fade_in_handle->show ();
575
576         /* smallest size for a fade is 64 frames */
577
578         width = std::max ((framecnt_t) 64, width);
579
580         Points* points;
581
582         /* round here to prevent little visual glitches with sub-pixel placement */
583         double const pwidth = rint (width / samples_per_unit);
584         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
585         double h;
586
587         if (_height < 5) {
588                 fade_in_shape->hide();
589                 fade_in_handle->hide();
590                 return;
591         }
592
593         double const handle_center = pwidth;
594
595         /* Put the fade in handle so that its left side is at the end-of-fade line */
596         fade_in_handle->property_x1() = handle_center;
597         fade_in_handle->property_x2() = handle_center + 6;
598
599         if (pwidth < 5) {
600                 fade_in_shape->hide();
601                 return;
602         }
603
604         if (trackview.session()->config.get_show_region_fades()) {
605                 fade_in_shape->show();
606         }
607
608         float curve[npoints];
609         audio_region()->fade_in()->curve().get_vector (0, audio_region()->fade_in()->back()->when, curve, npoints);
610
611         points = get_canvas_points ("fade in shape", npoints + 3);
612
613         if (_height >= NAME_HIGHLIGHT_THRESH) {
614                 h = _height - NAME_HIGHLIGHT_SIZE - 2;
615         } else {
616                 h = _height;
617         }
618
619         /* points *MUST* be in anti-clockwise order */
620
621         uint32_t pi, pc;
622         double xdelta = pwidth/npoints;
623
624         for (pi = 0, pc = 0; pc < npoints; ++pc) {
625                 (*points)[pi].set_x(1 + (pc * xdelta));
626                 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
627         }
628
629         /* fold back */
630
631         (*points)[pi].set_x(pwidth);
632         (*points)[pi++].set_y(2);
633
634         (*points)[pi].set_x(1);
635         (*points)[pi++].set_y(2);
636
637         /* connect the dots ... */
638
639         (*points)[pi] = (*points)[0];
640
641         fade_in_shape->property_points() = *points;
642         delete points;
643
644         /* ensure trim handle stays on top */
645         if (frame_handle_start) {
646                 frame_handle_start->raise_to_top();
647         }
648 }
649
650 void
651 AudioRegionView::reset_fade_out_shape ()
652 {
653         reset_fade_out_shape_width ((framecnt_t) audio_region()->fade_out()->back()->when);
654 }
655
656 void
657 AudioRegionView::reset_fade_out_shape_width (framecnt_t width)
658 {
659         if (dragging()) {
660                 return;
661         }
662
663         if (audio_region()->fade_out_is_xfade()) {
664                 if (fade_out_handle) {
665                         fade_out_handle->hide ();
666                         fade_out_shape->hide ();
667                 }
668                 redraw_end_xfade ();
669                 return;
670         } else {
671                 if (end_xfade_in) {
672                         end_xfade_in->hide ();
673                         end_xfade_out->hide ();
674                         end_xfade_rect->hide ();
675                 }
676         }
677
678         if (fade_out_handle == 0) {
679                 return;
680         }
681
682         fade_out_handle->show ();
683
684         /* smallest size for a fade is 64 frames */
685
686         width = std::max ((framecnt_t) 64, width);
687
688         Points* points;
689
690         /* round here to prevent little visual glitches with sub-pixel placement */
691         double const pwidth = rint (width / samples_per_unit);
692         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
693         double h;
694
695         if (_height < 5) {
696                 fade_out_shape->hide();
697                 fade_out_handle->hide();
698                 return;
699         }
700
701         double const handle_center = (_region->length() - width) / samples_per_unit;
702
703         /* Put the fade out handle so that its right side is at the end-of-fade line;
704          * it's `one out' for precise pixel accuracy.
705          */
706         fade_out_handle->property_x1() = handle_center - 5;
707         fade_out_handle->property_x2() = handle_center + 1;
708
709         /* don't show shape if its too small */
710
711         if (pwidth < 5) {
712                 fade_out_shape->hide();
713                 return;
714         }
715
716         if (trackview.session()->config.get_show_region_fades()) {
717                 fade_out_shape->show();
718         }
719
720         float curve[npoints];
721         audio_region()->fade_out()->curve().get_vector (0, audio_region()->fade_out()->back()->when, curve, npoints);
722
723         if (_height >= NAME_HIGHLIGHT_THRESH) {
724                 h = _height - NAME_HIGHLIGHT_SIZE - 2;
725         } else {
726                 h = _height;
727         }
728
729         /* points *MUST* be in anti-clockwise order */
730
731         points = get_canvas_points ("fade out shape", npoints + 3);
732
733         uint32_t pi, pc;
734         double xdelta = pwidth/npoints;
735
736         for (pi = 0, pc = 0; pc < npoints; ++pc) {
737                 (*points)[pi].set_x(_pixel_width - pwidth + (pc * xdelta));
738                 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
739         }
740
741         /* fold back */
742
743         (*points)[pi].set_x(_pixel_width);
744         (*points)[pi++].set_y(h);
745
746         (*points)[pi].set_x(_pixel_width);
747         (*points)[pi++].set_y(2);
748
749         /* connect the dots ... */
750
751         (*points)[pi] = (*points)[0];
752
753         fade_out_shape->property_points() = *points;
754         delete points;
755
756         /* ensure trim handle stays on top */
757         if (frame_handle_end) {
758                 frame_handle_end->raise_to_top();
759         }
760 }
761
762 void
763 AudioRegionView::set_samples_per_unit (gdouble spu)
764 {
765         RegionView::set_samples_per_unit (spu);
766
767         if (_flags & WaveformVisible) {
768                 for (uint32_t n=0; n < waves.size(); ++n) {
769                         waves[n]->property_samples_per_unit() = spu;
770                 }
771         }
772
773         if (gain_line) {
774                 gain_line->reset ();
775         }
776
777         reset_fade_shapes ();
778 }
779
780 void
781 AudioRegionView::set_amplitude_above_axis (gdouble spp)
782 {
783         for (uint32_t n=0; n < waves.size(); ++n) {
784                 waves[n]->property_amplitude_above_axis() = spp;
785         }
786 }
787
788 void
789 AudioRegionView::compute_colors (Gdk::Color const & basic_color)
790 {
791         RegionView::compute_colors (basic_color);
792
793         /* gain color computed in envelope_active_changed() */
794
795         fade_color = UINT_RGBA_CHANGE_A (fill_color, 120);
796 }
797
798 void
799 AudioRegionView::set_colors ()
800 {
801         RegionView::set_colors();
802
803         if (gain_line) {
804                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
805         }
806
807         for (uint32_t n=0; n < waves.size(); ++n) {
808                 if (_region->muted()) {
809                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
810                 } else {
811                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
812                 }
813
814                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
815                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
816         }
817 }
818
819 void
820 AudioRegionView::set_waveform_visible (bool yn)
821 {
822         if (((_flags & WaveformVisible) != yn)) {
823                 if (yn) {
824                         for (uint32_t n=0; n < waves.size(); ++n) {
825                                 /* make sure the zoom level is correct, since we don't update
826                                    this when waveforms are hidden.
827                                 */
828                                 waves[n]->property_samples_per_unit() = samples_per_unit;
829                                 waves[n]->show();
830                         }
831                         _flags |= WaveformVisible;
832                 } else {
833                         for (uint32_t n=0; n < waves.size(); ++n) {
834                                 waves[n]->hide();
835                         }
836                         _flags &= ~WaveformVisible;
837                 }
838                 store_flags ();
839         }
840 }
841
842 void
843 AudioRegionView::temporarily_hide_envelope ()
844 {
845         if (gain_line) {
846                 gain_line->hide ();
847         }
848 }
849
850 void
851 AudioRegionView::unhide_envelope ()
852 {
853         update_envelope_visibility ();
854 }
855
856 void
857 AudioRegionView::update_envelope_visibility ()
858 {
859         if (!gain_line) {
860                 return;
861         }
862
863         if (Config->get_show_region_gain() || trackview.editor().current_mouse_mode() == Editing::MouseGain) {
864                 gain_line->add_visibility (AutomationLine::Line);
865         } else {
866                 gain_line->hide ();
867         }
868 }
869
870 void
871 AudioRegionView::create_waves ()
872 {
873         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
874         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
875
876         if (!atv.track()) {
877                 return;
878         }
879
880         ChanCount nchans = atv.track()->n_channels();
881
882         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
883         //              << " and channels = " << nchans.n_audio() << endl;
884
885         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
886         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
887                 tmp_waves.push_back (0);
888         }
889
890         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
891                 delete *i;
892         }
893
894         _data_ready_connections.clear ();
895
896         for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
897                 _data_ready_connections.push_back (0);
898         }
899
900         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
901
902                 if (n >= audio_region()->n_channels()) {
903                         break;
904                 }
905
906                 wave_caches.push_back (WaveView::create_cache ());
907
908                 // cerr << "\tchannel " << n << endl;
909
910                 if (wait_for_data) {
911                         if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
912                                 // cerr << "\tData is ready\n";
913                                 create_one_wave (n, true);
914                         } else {
915                                 // cerr << "\tdata is not ready\n";
916                                 // we'll get a PeaksReady signal from the source in the future
917                                 // and will call create_one_wave(n) then.
918                         }
919
920                 } else {
921                         // cerr << "\tdon't delay, display today!\n";
922                         create_one_wave (n, true);
923                 }
924
925         }
926 }
927
928 void
929 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
930 {
931         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
932         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
933         uint32_t nchans = atv.track()->n_channels().n_audio();
934         uint32_t n;
935         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
936         gdouble ht;
937
938         if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
939                 ht = ((trackview.current_height()) / (double) nchans);
940         } else {
941                 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
942         }
943
944         gdouble yoff = which * ht;
945
946         WaveView *wave = new WaveView(*group);
947
948         wave->property_data_src() = (gpointer) _region.get();
949         wave->property_cache() =  wave_caches[which];
950         wave->property_cache_updater() = true;
951         wave->property_channel() =  which;
952         wave->property_length_function() = (gpointer) region_length_from_c;
953         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
954         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
955         wave->property_x() =  0.0;
956         wave->property_y() =  yoff;
957         wave->property_height() =  (double) ht;
958         wave->property_samples_per_unit() =  samples_per_unit;
959         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
960
961         if (_recregion) {
962                 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
963                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
964         } else {
965                 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
966                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
967         }
968
969         wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
970         wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
971         wave->property_zero_line() = true;
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 }
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) {
1024                 return;
1025         }
1026
1027         double x, y;
1028
1029         /* don't create points that can't be seen */
1030
1031         update_envelope_visibility ();
1032
1033         x = ev->button.x;
1034         y = ev->button.y;
1035
1036         item->w2i (x, y);
1037
1038         framepos_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 ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1087         node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1088
1089         _region->add_extra_xml (*node);
1090 }
1091
1092 void
1093 AudioRegionView::set_flags (XMLNode* node)
1094 {
1095         XMLProperty *prop;
1096
1097         if ((prop = node->property ("waveform-visible")) != 0) {
1098                 if (string_is_affirmative (prop->value())) {
1099                         _flags |= WaveformVisible;
1100                 }
1101         }
1102
1103         if ((prop = node->property ("waveform-rectified")) != 0) {
1104                 if (string_is_affirmative (prop->value())) {
1105                         _flags |= WaveformRectified;
1106                 }
1107         }
1108
1109         if ((prop = node->property ("waveform-logscaled")) != 0) {
1110                 if (string_is_affirmative (prop->value())) {
1111                         _flags |= WaveformLogScaled;
1112                 }
1113         }
1114 }
1115
1116 void
1117 AudioRegionView::set_waveform_shape (WaveformShape shape)
1118 {
1119         bool yn;
1120
1121         /* this slightly odd approach is to leave the door open to
1122            other "shapes" such as spectral displays, etc.
1123         */
1124
1125         switch (shape) {
1126         case Rectified:
1127                 yn = true;
1128                 break;
1129
1130         default:
1131                 yn = false;
1132                 break;
1133         }
1134
1135         if (yn != (bool) (_flags & WaveformRectified)) {
1136                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1137                         (*wave)->property_rectified() = yn;
1138                 }
1139
1140                 if (yn) {
1141                         _flags |= WaveformRectified;
1142                 } else {
1143                         _flags &= ~WaveformRectified;
1144                 }
1145                 store_flags ();
1146         }
1147 }
1148
1149 void
1150 AudioRegionView::set_waveform_scale (WaveformScale scale)
1151 {
1152         bool yn = (scale == Logarithmic);
1153
1154         if (yn != (bool) (_flags & WaveformLogScaled)) {
1155                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1156                         (*wave)->property_logscaled() = yn;
1157                 }
1158
1159                 if (yn) {
1160                         _flags |= WaveformLogScaled;
1161                 } else {
1162                         _flags &= ~WaveformLogScaled;
1163                 }
1164                 store_flags ();
1165         }
1166 }
1167
1168
1169 GhostRegion*
1170 AudioRegionView::add_ghost (TimeAxisView& tv)
1171 {
1172         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1173         assert(rtv);
1174
1175         double unit_position = _region->position () / samples_per_unit;
1176         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1177         uint32_t nchans;
1178
1179         nchans = rtv->track()->n_channels().n_audio();
1180
1181         for (uint32_t n = 0; n < nchans; ++n) {
1182
1183                 if (n >= audio_region()->n_channels()) {
1184                         break;
1185                 }
1186
1187                 WaveView *wave = new WaveView(*ghost->group);
1188
1189                 wave->property_data_src() = _region.get();
1190                 wave->property_cache() =  wave_caches[n];
1191                 wave->property_cache_updater() = false;
1192                 wave->property_channel() = n;
1193                 wave->property_length_function() = (gpointer)region_length_from_c;
1194                 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1195                 wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
1196                 wave->property_x() =  0.0;
1197                 wave->property_samples_per_unit() =  samples_per_unit;
1198                 wave->property_amplitude_above_axis() =  _amplitude_above_axis;
1199
1200                 wave->property_region_start() = _region->start();
1201
1202                 ghost->waves.push_back(wave);
1203         }
1204
1205         ghost->set_height ();
1206         ghost->set_duration (_region->length() / samples_per_unit);
1207         ghost->set_colors();
1208         ghosts.push_back (ghost);
1209
1210         return ghost;
1211 }
1212
1213 void
1214 AudioRegionView::entered (bool internal_editing)
1215 {
1216         trackview.editor().set_current_trimmable (_region);
1217         trackview.editor().set_current_movable (_region);
1218         
1219         if (gain_line && trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1220                 gain_line->add_visibility (AutomationLine::ControlPoints);
1221         }
1222
1223         if (fade_in_handle && !internal_editing) {
1224                 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 255);
1225                 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 255);
1226         }
1227 }
1228
1229 void
1230 AudioRegionView::exited ()
1231 {
1232         trackview.editor().set_current_trimmable (boost::shared_ptr<Trimmable>());
1233         trackview.editor().set_current_movable (boost::shared_ptr<Movable>());
1234
1235         if (gain_line && trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1236                 gain_line->remove_visibility (AutomationLine::ControlPoints);
1237         }
1238
1239         if (fade_in_handle) {
1240                 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 0);
1241                 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 0);
1242         }
1243 }
1244
1245 void
1246 AudioRegionView::envelope_active_changed ()
1247 {
1248         if (gain_line) {
1249                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1250         }
1251 }
1252
1253 void
1254 AudioRegionView::set_waveview_data_src()
1255 {
1256         AudioGhostRegion* agr;
1257         double unit_length= _region->length() / samples_per_unit;
1258
1259         for (uint32_t n = 0; n < waves.size(); ++n) {
1260                 // TODO: something else to let it know the channel
1261                 waves[n]->property_data_src() = _region.get();
1262         }
1263
1264         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1265
1266                 (*i)->set_duration (unit_length);
1267
1268                 if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
1269                         for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
1270                                 (*w)->property_data_src() = _region.get();
1271                         }
1272                 }
1273         }
1274
1275 }
1276
1277 void
1278 AudioRegionView::color_handler ()
1279 {
1280         //case cMutedWaveForm:
1281         //case cWaveForm:
1282         //case cWaveFormClip:
1283         //case cZeroLine:
1284         set_colors ();
1285
1286         //case cGainLineInactive:
1287         //case cGainLine:
1288         envelope_active_changed();
1289
1290 }
1291
1292 void
1293 AudioRegionView::set_frame_color ()
1294 {
1295         if (!frame) {
1296                 return;
1297         }
1298
1299         if (_region->opaque()) {
1300                 fill_opacity = 130;
1301         } else {
1302                 fill_opacity = 0;
1303         }
1304
1305         TimeAxisViewItem::set_frame_color ();
1306
1307         uint32_t wc;
1308         uint32_t fc;
1309
1310         if (_selected) {
1311                 if (_region->muted()) {
1312                         wc = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1313                 } else {
1314                         wc = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1315                 }
1316                 fc = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1317         } else {
1318                 if (_recregion) {
1319                         if (_region->muted()) {
1320                                 wc = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA);
1321                         } else {
1322                                 wc = ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
1323                         }
1324                         fc = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
1325                 } else {
1326                         if (_region->muted()) {
1327                                 wc = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1328                         } else {
1329                                 wc = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1330                         }
1331                         fc = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1332                 }
1333         }
1334
1335         for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1336                 if (_region->muted()) {
1337                         (*w)->property_wave_color() = wc;
1338                 } else {
1339                         (*w)->property_wave_color() = wc;
1340                         (*w)->property_fill_color() = fc;
1341                 }
1342         }
1343 }
1344
1345 void
1346 AudioRegionView::set_fade_visibility (bool yn)
1347 {
1348         if (yn) {
1349                 if (fade_in_shape) {
1350                         fade_in_shape->show();
1351                 }
1352                 if (fade_out_shape) {
1353                         fade_out_shape->show ();
1354                 }
1355                 if (fade_in_handle) {
1356                         fade_in_handle->show ();
1357                 }
1358                 if (fade_out_handle) {
1359                         fade_out_handle->show ();
1360                 }
1361         } else {
1362                 if (fade_in_shape) {
1363                         fade_in_shape->hide();
1364                 }
1365                 if (fade_out_shape) {
1366                         fade_out_shape->hide ();
1367                 }
1368                 if (fade_in_handle) {
1369                         fade_in_handle->hide ();
1370                 }
1371                 if (fade_out_handle) {
1372                         fade_out_handle->hide ();
1373                 }
1374         }
1375 }
1376
1377 void
1378 AudioRegionView::update_coverage_frames (LayerDisplay d)
1379 {
1380         RegionView::update_coverage_frames (d);
1381
1382         if (fade_in_handle) {
1383                 fade_in_handle->raise_to_top ();
1384                 fade_out_handle->raise_to_top ();
1385         }
1386 }
1387
1388 void
1389 AudioRegionView::show_region_editor ()
1390 {
1391         if (editor == 0) {
1392                 editor = new AudioRegionEditor (trackview.session(), audio_region());
1393         }
1394
1395         editor->present ();
1396         editor->set_position (Gtk::WIN_POS_MOUSE);
1397         editor->show_all();
1398 }
1399
1400
1401 void
1402 AudioRegionView::show_fade_line (framepos_t pos)
1403 {
1404         fade_position_line->property_x1() = trackview.editor().frame_to_pixel (pos);
1405         fade_position_line->property_x2() = trackview.editor().frame_to_pixel (pos);
1406         fade_position_line->show ();
1407         fade_position_line->raise_to_top ();
1408 }
1409
1410 void
1411 AudioRegionView::hide_fade_line ()
1412 {
1413         fade_position_line->hide ();
1414 }
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
1424                 ArdourCanvas::Line* canvas_item = new ArdourCanvas::Line(*group);
1425
1426                 ArdourCanvas::Points points;
1427
1428                 points.push_back(Gnome::Art::Point(-1.0, 2.0)); // first x-coord needs to be a non-normal value
1429                 points.push_back(Gnome::Art::Point(1.0, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
1430
1431                 canvas_item->property_points() = points;
1432                 canvas_item->property_width_pixels() = 1;
1433                 canvas_item->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1434                 canvas_item->property_first_arrowhead() = TRUE;
1435                 canvas_item->property_last_arrowhead() = TRUE;
1436                 canvas_item->property_arrow_shape_a() = 11.0;
1437                 canvas_item->property_arrow_shape_b() = 0.0;
1438                 canvas_item->property_arrow_shape_c() = 4.0;
1439
1440                 canvas_item->raise_to_top ();
1441                 canvas_item->show ();
1442
1443                 canvas_item->set_data ("regionview", this);
1444                 canvas_item->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), canvas_item, this));
1445
1446                 feature_lines.push_back (make_pair(0, canvas_item));
1447         }
1448
1449         while (feature_lines.size() > analysis_features.size()) {
1450                 ArdourCanvas::Line* line = feature_lines.back().second;
1451                 feature_lines.pop_back ();
1452                 delete line;
1453         }
1454
1455         AnalysisFeatureList::const_iterator i;
1456         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1457
1458         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1459
1460                 ArdourCanvas::Points points;
1461
1462                 float *pos = new float;
1463                 *pos = trackview.editor().frame_to_pixel (*i);
1464
1465                 points.push_back(Gnome::Art::Point(*pos, 2.0)); // first x-coord needs to be a non-normal value
1466                 points.push_back(Gnome::Art::Point(*pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
1467
1468                 (*l).second->property_points() = points;
1469                 (*l).second->set_data ("position", pos);
1470
1471                 (*l).first = *i;
1472         }
1473 }
1474
1475 void
1476 AudioRegionView::update_transient(float /*old_pos*/, float new_pos)
1477 {
1478         /* Find frame at old pos, calulate new frame then update region transients*/
1479         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1480
1481         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1482
1483                 /* Line has been updated in drag so we compare to new_pos */
1484
1485                 float* pos = (float*) (*l).second->get_data ("position");
1486
1487                 if (rint(new_pos) == rint(*pos)) {
1488
1489                     framepos_t old_frame = (*l).first;
1490                     framepos_t new_frame = trackview.editor().pixel_to_frame (new_pos);
1491
1492                     _region->update_transient (old_frame, new_frame);
1493
1494                     break;
1495                 }
1496         }
1497 }
1498
1499 void
1500 AudioRegionView::remove_transient(float pos)
1501 {
1502         /* Find frame at old pos, calulate new frame then update region transients*/
1503         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1504
1505         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1506
1507                 /* Line has been updated in drag so we compare to new_pos */
1508                 float *line_pos = (float*) (*l).second->get_data ("position");
1509
1510                 if (rint(pos) == rint(*line_pos)) {
1511                     _region->remove_transient ((*l).first);
1512                     break;
1513                 }
1514         }
1515 }
1516
1517 void
1518 AudioRegionView::thaw_after_trim ()
1519 {
1520         RegionView::thaw_after_trim ();
1521         unhide_envelope ();
1522         drag_end ();
1523 }
1524
1525 void
1526 AudioRegionView::redraw_start_xfade ()
1527 {
1528         boost::shared_ptr<AudioRegion> ar (audio_region());
1529
1530         if (!ar->fade_in() || ar->fade_in()->empty()) {
1531                 return;
1532         }
1533
1534         if (!ar->fade_in_is_xfade()) {
1535                 if (start_xfade_in) {
1536                         start_xfade_in->hide ();
1537                         start_xfade_out->hide ();
1538                         start_xfade_rect->hide ();
1539                 }
1540                 return;
1541         }
1542
1543         redraw_start_xfade_to (ar, ar->fade_in()->back()->when);
1544 }
1545
1546 void
1547 AudioRegionView::redraw_start_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t len)
1548 {
1549         int32_t const npoints = trackview.editor().frame_to_pixel (len);
1550
1551         if (npoints < 3) {
1552                 return;
1553         }
1554
1555         if (!start_xfade_in) {
1556                 start_xfade_in = new ArdourCanvas::Line (*group);
1557                 start_xfade_in->property_width_pixels() = 1;
1558                 start_xfade_in->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_GainLine.get();
1559         }
1560
1561         if (!start_xfade_out) {
1562                 start_xfade_out = new ArdourCanvas::Line (*group);
1563                 start_xfade_out->property_width_pixels() = 1;
1564                 uint32_t col = UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->canvasvar_GainLine.get(), 125);
1565                 start_xfade_out->property_fill_color_rgba() = col;
1566         }
1567
1568         if (!start_xfade_rect) {
1569                 start_xfade_rect = new ArdourCanvas::SimpleRect (*group);
1570                 start_xfade_rect->property_draw() = true;
1571                 start_xfade_rect->property_fill() = true;;
1572                 start_xfade_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
1573                 start_xfade_rect->property_outline_pixels() = 0;
1574                 start_xfade_rect->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_start_xfade_event), start_xfade_rect, this));
1575                 start_xfade_rect->set_data ("regionview", this);
1576         }
1577
1578         Points* points = get_canvas_points ("xfade edit redraw", npoints);
1579         boost::scoped_ptr<float> vec (new float[npoints]);
1580         double effective_height = _height - NAME_HIGHLIGHT_SIZE - 1.0;
1581
1582         ar->fade_in()->curve().get_vector (0, ar->fade_in()->back()->when, vec.get(), npoints);
1583
1584         for (int i = 0, pci = 0; i < npoints; ++i) {
1585                 Gnome::Art::Point &p ((*points)[pci++]);
1586                 p.set_x (i);
1587                 p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1588         }
1589
1590         start_xfade_rect->property_x1() = ((*points)[0]).get_x();
1591         start_xfade_rect->property_y1() = 1.0;
1592         start_xfade_rect->property_x2() = ((*points)[npoints-1]).get_x();
1593         start_xfade_rect->property_y2() = effective_height;
1594         start_xfade_rect->show ();
1595         start_xfade_rect->raise_to_top ();
1596
1597         start_xfade_in->property_points() = *points;
1598         start_xfade_in->show ();
1599         start_xfade_in->raise_to_top ();
1600
1601         /* fade out line */
1602
1603         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_in();
1604
1605         if (!inverse) {
1606
1607                 for (int i = 0, pci = 0; i < npoints; ++i) {
1608                         Gnome::Art::Point &p ((*points)[pci++]);
1609                         p.set_x (i);
1610                         p.set_y (1.0 + effective_height - (effective_height * (1.0 - vec.get()[i])));
1611                 }
1612
1613         } else {
1614
1615                 inverse->curve().get_vector (0, inverse->back()->when, vec.get(), npoints);
1616
1617                 for (int i = 0, pci = 0; i < npoints; ++i) {
1618                         Gnome::Art::Point &p ((*points)[pci++]);
1619                         p.set_x (i);
1620                         p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1621                 }
1622         }
1623
1624         start_xfade_out->property_points() = *points;
1625         start_xfade_out->show ();
1626         start_xfade_out->raise_to_top ();
1627
1628         delete points;
1629 }
1630
1631 void
1632 AudioRegionView::redraw_end_xfade ()
1633 {
1634         boost::shared_ptr<AudioRegion> ar (audio_region());
1635
1636         if (!ar->fade_out() || ar->fade_out()->empty()) {
1637                 return;
1638         }
1639
1640         if (!ar->fade_out_is_xfade()) {
1641                 if (end_xfade_in) {
1642                         end_xfade_in->hide ();
1643                         end_xfade_out->hide ();
1644                         end_xfade_rect->hide ();
1645                 }
1646                 return;
1647         }
1648
1649         redraw_end_xfade_to (ar, ar->fade_out()->back()->when);
1650 }
1651
1652 void
1653 AudioRegionView::redraw_end_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t len)
1654 {
1655         int32_t const npoints = trackview.editor().frame_to_pixel (len);
1656
1657         if (npoints < 3) {
1658                 return;
1659         }
1660
1661         if (!end_xfade_in) {
1662                 end_xfade_in = new ArdourCanvas::Line (*group);
1663                 end_xfade_in->property_width_pixels() = 1;
1664                 end_xfade_in->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_GainLine.get();
1665         }
1666
1667         if (!end_xfade_out) {
1668                 end_xfade_out = new ArdourCanvas::Line (*group);
1669                 end_xfade_out->property_width_pixels() = 1;
1670                 uint32_t col UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->canvasvar_GainLine.get(), 125);
1671                 end_xfade_out->property_fill_color_rgba() = col;
1672         }
1673
1674         if (!end_xfade_rect) {
1675                 end_xfade_rect = new ArdourCanvas::SimpleRect (*group);
1676                 end_xfade_rect->property_draw() = true;
1677                 end_xfade_rect->property_fill() = true;;
1678                 end_xfade_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
1679                 end_xfade_rect->property_outline_pixels() = 0;
1680                 end_xfade_rect->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_end_xfade_event), end_xfade_rect, this));
1681                 end_xfade_rect->set_data ("regionview", this);
1682         }
1683
1684         Points* points = get_canvas_points ("xfade edit redraw", npoints);
1685         boost::scoped_ptr<float> vec (new float[npoints]);
1686
1687         ar->fade_out()->curve().get_vector (0, ar->fade_out()->back()->when, vec.get(), npoints);
1688
1689         double rend = trackview.editor().frame_to_pixel (_region->length() - len);
1690         double effective_height = _height - NAME_HIGHLIGHT_SIZE - 1;
1691
1692         for (int i = 0, pci = 0; i < npoints; ++i) {
1693                 Gnome::Art::Point &p ((*points)[pci++]);
1694                 p.set_x (rend + i);
1695                 p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1696         }
1697
1698         end_xfade_rect->property_x1() = ((*points)[0]).get_x();
1699         end_xfade_rect->property_y1() = 1;
1700         end_xfade_rect->property_x2() = ((*points)[npoints-1]).get_x();
1701         end_xfade_rect->property_y2() = effective_height;
1702         end_xfade_rect->show ();
1703         end_xfade_rect->raise_to_top ();
1704
1705         end_xfade_in->property_points() = *points;
1706         end_xfade_in->show ();
1707         end_xfade_in->raise_to_top ();
1708
1709         /* fade in line */
1710
1711         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_out ();
1712
1713         if (!inverse) {
1714
1715                 for (int i = 0, pci = 0; i < npoints; ++i) {
1716                         Gnome::Art::Point &p ((*points)[pci++]);
1717                         p.set_x (rend + i);
1718                         p.set_y (1.0 + effective_height - (effective_height * (1.0 - vec.get()[i])));
1719                 }
1720
1721         } else {
1722
1723                 inverse->curve().get_vector (inverse->front()->when, inverse->back()->when, vec.get(), npoints);
1724
1725                 for (int i = 0, pci = 0; i < npoints; ++i) {
1726                         Gnome::Art::Point &p ((*points)[pci++]);
1727                         p.set_x (rend + i);
1728                         p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1729                 }
1730         }
1731
1732         end_xfade_out->property_points() = *points;
1733         end_xfade_out->show ();
1734         end_xfade_out->raise_to_top ();
1735
1736
1737         delete points;
1738 }
1739
1740 void
1741 AudioRegionView::hide_xfades ()
1742 {
1743         if (start_xfade_in) {
1744                 start_xfade_in->hide();
1745         }
1746         if (start_xfade_out) {
1747                 start_xfade_out->hide();
1748         }
1749         if (start_xfade_rect) {
1750                 start_xfade_rect->hide ();
1751         }
1752         if (end_xfade_in) {
1753                 end_xfade_in->hide();
1754         }
1755         if (end_xfade_out) {
1756                 end_xfade_out->hide();
1757         }
1758         if (end_xfade_rect) {
1759                 end_xfade_rect->hide ();
1760         }
1761 }
1762
1763 void
1764 AudioRegionView::show_xfades ()
1765 {
1766         if (start_xfade_in) {
1767                 start_xfade_in->show();
1768         }
1769         if (start_xfade_out) {
1770                 start_xfade_out->show();
1771         }
1772         if (start_xfade_rect) {
1773                 start_xfade_rect->show ();
1774         }
1775         if (end_xfade_in) {
1776                 end_xfade_in->show();
1777         }
1778         if (end_xfade_out) {
1779                 end_xfade_out->show();
1780         }
1781         if (end_xfade_rect) {
1782                 end_xfade_rect->show ();
1783         }
1784 }
1785
1786 void
1787 AudioRegionView::drag_start ()
1788 {
1789         TimeAxisViewItem::drag_start ();
1790         AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&trackview);
1791
1792         if (atav) {
1793                 AudioStreamView* av = atav->audio_view();
1794                 if (av) {
1795                         /* this will hide our xfades too */
1796                         av->hide_xfades_with (audio_region());
1797                 }
1798         }
1799 }
1800
1801 void
1802 AudioRegionView::drag_end ()
1803 {
1804         TimeAxisViewItem::drag_end ();
1805         /* fades will be redrawn if they changed */
1806 }
1807