ec0a230f49eee5c3879db6209feb36c60bc7aff6
[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         gain_line->set_visibility (automation_line_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                         gain_line->set_visibility (automation_line_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         if (gain_line) {
854                 gain_line->set_visibility (automation_line_visibility());
855         }
856 }
857
858 void
859 AudioRegionView::update_envelope_visible ()
860 {
861         if (gain_line) {
862                 gain_line->set_visibility (automation_line_visibility());
863         }
864 }
865
866 void
867 AudioRegionView::create_waves ()
868 {
869         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
870         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
871
872         if (!atv.track()) {
873                 return;
874         }
875
876         ChanCount nchans = atv.track()->n_channels();
877
878         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
879         //              << " and channels = " << nchans.n_audio() << endl;
880
881         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
882         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
883                 tmp_waves.push_back (0);
884         }
885
886         for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
887                 delete *i;
888         }
889
890         _data_ready_connections.clear ();
891
892         for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
893                 _data_ready_connections.push_back (0);
894         }
895
896         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
897
898                 if (n >= audio_region()->n_channels()) {
899                         break;
900                 }
901
902                 wave_caches.push_back (WaveView::create_cache ());
903
904                 // cerr << "\tchannel " << n << endl;
905
906                 if (wait_for_data) {
907                         if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
908                                 // cerr << "\tData is ready\n";
909                                 create_one_wave (n, true);
910                         } else {
911                                 // cerr << "\tdata is not ready\n";
912                                 // we'll get a PeaksReady signal from the source in the future
913                                 // and will call create_one_wave(n) then.
914                         }
915
916                 } else {
917                         // cerr << "\tdon't delay, display today!\n";
918                         create_one_wave (n, true);
919                 }
920
921         }
922 }
923
924 void
925 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
926 {
927         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
928         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
929         uint32_t nchans = atv.track()->n_channels().n_audio();
930         uint32_t n;
931         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
932         gdouble ht;
933
934         if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
935                 ht = ((trackview.current_height()) / (double) nchans);
936         } else {
937                 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
938         }
939
940         gdouble yoff = which * ht;
941
942         WaveView *wave = new WaveView(*group);
943
944         wave->property_data_src() = (gpointer) _region.get();
945         wave->property_cache() =  wave_caches[which];
946         wave->property_cache_updater() = true;
947         wave->property_channel() =  which;
948         wave->property_length_function() = (gpointer) region_length_from_c;
949         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
950         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
951         wave->property_x() =  0.0;
952         wave->property_y() =  yoff;
953         wave->property_height() =  (double) ht;
954         wave->property_samples_per_unit() =  samples_per_unit;
955         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
956
957         if (_recregion) {
958                 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
959                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
960         } else {
961                 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
962                 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
963         }
964
965         wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
966         wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
967         wave->property_zero_line() = true;
968         wave->property_region_start() = _region->start();
969         wave->property_rectified() = (bool) (_flags & WaveformRectified);
970         wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
971
972         if (!(_flags & WaveformVisible)) {
973                 wave->hide();
974         }
975
976         /* note: calling this function is serialized by the lock
977            held in the peak building thread that signals that
978            peaks are ready for use *or* by the fact that it is
979            called one by one from the GUI thread.
980         */
981
982         if (which < nchans) {
983                 tmp_waves[which] = wave;
984         } else {
985                 /* n-channel track, >n-channel source */
986         }
987
988         /* see if we're all ready */
989
990         for (n = 0; n < nchans; ++n) {
991                 if (tmp_waves[n] == 0) {
992                         break;
993                 }
994         }
995
996         if (n == nwaves && waves.empty()) {
997                 /* all waves are ready */
998                 tmp_waves.resize(nwaves);
999
1000                 waves = tmp_waves;
1001                 tmp_waves.clear ();
1002
1003                 /* all waves created, don't hook into peaks ready anymore */
1004                 delete _data_ready_connections[which];
1005                 _data_ready_connections[which] = 0;
1006         }
1007 }
1008
1009 void
1010 AudioRegionView::peaks_ready_handler (uint32_t which)
1011 {
1012         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AudioRegionView::create_one_wave, this, which, false));
1013         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
1014 }
1015
1016 void
1017 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
1018 {
1019         if (!gain_line) {
1020                 return;
1021         }
1022
1023         double x, y;
1024
1025         /* don't create points that can't be seen */
1026
1027         gain_line->set_visibility (automation_line_visibility());
1028
1029         x = ev->button.x;
1030         y = ev->button.y;
1031
1032         item->w2i (x, y);
1033
1034         framepos_t fx = trackview.editor().pixel_to_frame (x);
1035
1036         if (fx > _region->length()) {
1037                 return;
1038         }
1039
1040         /* compute vertical fractional position */
1041
1042         y = 1.0 - (y / (_height - NAME_HIGHLIGHT_SIZE));
1043
1044         /* map using gain line */
1045
1046         gain_line->view_to_model_coord (x, y);
1047
1048         /* XXX STATEFUL: can't convert to stateful diff until we
1049            can represent automation data with it.
1050         */
1051
1052         trackview.session()->begin_reversible_command (_("add gain control point"));
1053         XMLNode &before = audio_region()->envelope()->get_state();
1054
1055         if (!audio_region()->envelope_active()) {
1056                 XMLNode &region_before = audio_region()->get_state();
1057                 audio_region()->set_envelope_active(true);
1058                 XMLNode &region_after = audio_region()->get_state();
1059                 trackview.session()->add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
1060         }
1061
1062         audio_region()->envelope()->add (fx, y);
1063
1064         XMLNode &after = audio_region()->envelope()->get_state();
1065         trackview.session()->add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1066         trackview.session()->commit_reversible_command ();
1067 }
1068
1069 void
1070 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent */*ev*/)
1071 {
1072         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1073         audio_region()->envelope()->erase (cp->model());
1074 }
1075
1076 void
1077 AudioRegionView::store_flags()
1078 {
1079         XMLNode *node = new XMLNode ("GUI");
1080
1081         node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
1082         node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1083         node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1084
1085         _region->add_extra_xml (*node);
1086 }
1087
1088 void
1089 AudioRegionView::set_flags (XMLNode* node)
1090 {
1091         XMLProperty *prop;
1092
1093         if ((prop = node->property ("waveform-visible")) != 0) {
1094                 if (string_is_affirmative (prop->value())) {
1095                         _flags |= WaveformVisible;
1096                 }
1097         }
1098
1099         if ((prop = node->property ("waveform-rectified")) != 0) {
1100                 if (string_is_affirmative (prop->value())) {
1101                         _flags |= WaveformRectified;
1102                 }
1103         }
1104
1105         if ((prop = node->property ("waveform-logscaled")) != 0) {
1106                 if (string_is_affirmative (prop->value())) {
1107                         _flags |= WaveformLogScaled;
1108                 }
1109         }
1110 }
1111
1112 void
1113 AudioRegionView::set_waveform_shape (WaveformShape shape)
1114 {
1115         bool yn;
1116
1117         /* this slightly odd approach is to leave the door open to
1118            other "shapes" such as spectral displays, etc.
1119         */
1120
1121         switch (shape) {
1122         case Rectified:
1123                 yn = true;
1124                 break;
1125
1126         default:
1127                 yn = false;
1128                 break;
1129         }
1130
1131         if (yn != (bool) (_flags & WaveformRectified)) {
1132                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1133                         (*wave)->property_rectified() = yn;
1134                 }
1135
1136                 if (yn) {
1137                         _flags |= WaveformRectified;
1138                 } else {
1139                         _flags &= ~WaveformRectified;
1140                 }
1141                 store_flags ();
1142         }
1143 }
1144
1145 void
1146 AudioRegionView::set_waveform_scale (WaveformScale scale)
1147 {
1148         bool yn = (scale == Logarithmic);
1149
1150         if (yn != (bool) (_flags & WaveformLogScaled)) {
1151                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1152                         (*wave)->property_logscaled() = yn;
1153                 }
1154
1155                 if (yn) {
1156                         _flags |= WaveformLogScaled;
1157                 } else {
1158                         _flags &= ~WaveformLogScaled;
1159                 }
1160                 store_flags ();
1161         }
1162 }
1163
1164
1165 GhostRegion*
1166 AudioRegionView::add_ghost (TimeAxisView& tv)
1167 {
1168         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1169         assert(rtv);
1170
1171         double unit_position = _region->position () / samples_per_unit;
1172         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1173         uint32_t nchans;
1174
1175         nchans = rtv->track()->n_channels().n_audio();
1176
1177         for (uint32_t n = 0; n < nchans; ++n) {
1178
1179                 if (n >= audio_region()->n_channels()) {
1180                         break;
1181                 }
1182
1183                 WaveView *wave = new WaveView(*ghost->group);
1184
1185                 wave->property_data_src() = _region.get();
1186                 wave->property_cache() =  wave_caches[n];
1187                 wave->property_cache_updater() = false;
1188                 wave->property_channel() = n;
1189                 wave->property_length_function() = (gpointer)region_length_from_c;
1190                 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1191                 wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
1192                 wave->property_x() =  0.0;
1193                 wave->property_samples_per_unit() =  samples_per_unit;
1194                 wave->property_amplitude_above_axis() =  _amplitude_above_axis;
1195
1196                 wave->property_region_start() = _region->start();
1197
1198                 ghost->waves.push_back(wave);
1199         }
1200
1201         ghost->set_height ();
1202         ghost->set_duration (_region->length() / samples_per_unit);
1203         ghost->set_colors();
1204         ghosts.push_back (ghost);
1205
1206         return ghost;
1207 }
1208
1209 void
1210 AudioRegionView::entered (bool internal_editing)
1211 {
1212         trackview.editor().set_current_trimmable (_region);
1213         trackview.editor().set_current_movable (_region);
1214         
1215         if (gain_line && trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1216                 gain_line->add_visibility (AutomationLine::ControlPoints);
1217         }
1218
1219         if (fade_in_handle && !internal_editing) {
1220                 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 255);
1221                 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 255);
1222         }
1223 }
1224
1225 void
1226 AudioRegionView::exited ()
1227 {
1228         trackview.editor().set_current_trimmable (boost::shared_ptr<Trimmable>());
1229         trackview.editor().set_current_movable (boost::shared_ptr<Movable>());
1230
1231         if (gain_line && trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1232                 gain_line->remove_visibility (AutomationLine::ControlPoints);
1233         }
1234
1235         if (fade_in_handle) {
1236                 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 0);
1237                 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 0);
1238         }
1239 }
1240
1241 void
1242 AudioRegionView::envelope_active_changed ()
1243 {
1244         if (gain_line) {
1245                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1246         }
1247 }
1248
1249 void
1250 AudioRegionView::set_waveview_data_src()
1251 {
1252         AudioGhostRegion* agr;
1253         double unit_length= _region->length() / samples_per_unit;
1254
1255         for (uint32_t n = 0; n < waves.size(); ++n) {
1256                 // TODO: something else to let it know the channel
1257                 waves[n]->property_data_src() = _region.get();
1258         }
1259
1260         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1261
1262                 (*i)->set_duration (unit_length);
1263
1264                 if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
1265                         for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
1266                                 (*w)->property_data_src() = _region.get();
1267                         }
1268                 }
1269         }
1270
1271 }
1272
1273 void
1274 AudioRegionView::color_handler ()
1275 {
1276         //case cMutedWaveForm:
1277         //case cWaveForm:
1278         //case cWaveFormClip:
1279         //case cZeroLine:
1280         set_colors ();
1281
1282         //case cGainLineInactive:
1283         //case cGainLine:
1284         envelope_active_changed();
1285
1286 }
1287
1288 void
1289 AudioRegionView::set_frame_color ()
1290 {
1291         if (!frame) {
1292                 return;
1293         }
1294
1295         if (_region->opaque()) {
1296                 fill_opacity = 130;
1297         } else {
1298                 fill_opacity = 0;
1299         }
1300
1301         TimeAxisViewItem::set_frame_color ();
1302
1303         uint32_t wc;
1304         uint32_t fc;
1305
1306         if (_selected) {
1307                 if (_region->muted()) {
1308                         wc = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1309                 } else {
1310                         wc = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1311                 }
1312                 fc = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1313         } else {
1314                 if (_recregion) {
1315                         if (_region->muted()) {
1316                                 wc = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA);
1317                         } else {
1318                                 wc = ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
1319                         }
1320                         fc = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
1321                 } else {
1322                         if (_region->muted()) {
1323                                 wc = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1324                         } else {
1325                                 wc = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1326                         }
1327                         fc = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1328                 }
1329         }
1330
1331         for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1332                 if (_region->muted()) {
1333                         (*w)->property_wave_color() = wc;
1334                 } else {
1335                         (*w)->property_wave_color() = wc;
1336                         (*w)->property_fill_color() = fc;
1337                 }
1338         }
1339 }
1340
1341 void
1342 AudioRegionView::set_fade_visibility (bool yn)
1343 {
1344         if (yn) {
1345                 if (fade_in_shape) {
1346                         fade_in_shape->show();
1347                 }
1348                 if (fade_out_shape) {
1349                         fade_out_shape->show ();
1350                 }
1351                 if (fade_in_handle) {
1352                         fade_in_handle->show ();
1353                 }
1354                 if (fade_out_handle) {
1355                         fade_out_handle->show ();
1356                 }
1357         } else {
1358                 if (fade_in_shape) {
1359                         fade_in_shape->hide();
1360                 }
1361                 if (fade_out_shape) {
1362                         fade_out_shape->hide ();
1363                 }
1364                 if (fade_in_handle) {
1365                         fade_in_handle->hide ();
1366                 }
1367                 if (fade_out_handle) {
1368                         fade_out_handle->hide ();
1369                 }
1370         }
1371 }
1372
1373 void
1374 AudioRegionView::update_coverage_frames (LayerDisplay d)
1375 {
1376         RegionView::update_coverage_frames (d);
1377
1378         if (fade_in_handle) {
1379                 fade_in_handle->raise_to_top ();
1380                 fade_out_handle->raise_to_top ();
1381         }
1382 }
1383
1384 void
1385 AudioRegionView::show_region_editor ()
1386 {
1387         if (editor == 0) {
1388                 editor = new AudioRegionEditor (trackview.session(), audio_region());
1389         }
1390
1391         editor->present ();
1392         editor->set_position (Gtk::WIN_POS_MOUSE);
1393         editor->show_all();
1394 }
1395
1396
1397 void
1398 AudioRegionView::show_fade_line (framepos_t pos)
1399 {
1400         fade_position_line->property_x1() = trackview.editor().frame_to_pixel (pos);
1401         fade_position_line->property_x2() = trackview.editor().frame_to_pixel (pos);
1402         fade_position_line->show ();
1403         fade_position_line->raise_to_top ();
1404 }
1405
1406 void
1407 AudioRegionView::hide_fade_line ()
1408 {
1409         fade_position_line->hide ();
1410 }
1411
1412
1413 void
1414 AudioRegionView::transients_changed ()
1415 {
1416         AnalysisFeatureList analysis_features = _region->transients();
1417
1418         while (feature_lines.size() < analysis_features.size()) {
1419
1420                 ArdourCanvas::Line* canvas_item = new ArdourCanvas::Line(*group);
1421
1422                 ArdourCanvas::Points points;
1423
1424                 points.push_back(Gnome::Art::Point(-1.0, 2.0)); // first x-coord needs to be a non-normal value
1425                 points.push_back(Gnome::Art::Point(1.0, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
1426
1427                 canvas_item->property_points() = points;
1428                 canvas_item->property_width_pixels() = 1;
1429                 canvas_item->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1430                 canvas_item->property_first_arrowhead() = TRUE;
1431                 canvas_item->property_last_arrowhead() = TRUE;
1432                 canvas_item->property_arrow_shape_a() = 11.0;
1433                 canvas_item->property_arrow_shape_b() = 0.0;
1434                 canvas_item->property_arrow_shape_c() = 4.0;
1435
1436                 canvas_item->raise_to_top ();
1437                 canvas_item->show ();
1438
1439                 canvas_item->set_data ("regionview", this);
1440                 canvas_item->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), canvas_item, this));
1441
1442                 feature_lines.push_back (make_pair(0, canvas_item));
1443         }
1444
1445         while (feature_lines.size() > analysis_features.size()) {
1446                 ArdourCanvas::Line* line = feature_lines.back().second;
1447                 feature_lines.pop_back ();
1448                 delete line;
1449         }
1450
1451         AnalysisFeatureList::const_iterator i;
1452         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1453
1454         for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1455
1456                 ArdourCanvas::Points points;
1457
1458                 float *pos = new float;
1459                 *pos = trackview.editor().frame_to_pixel (*i);
1460
1461                 points.push_back(Gnome::Art::Point(*pos, 2.0)); // first x-coord needs to be a non-normal value
1462                 points.push_back(Gnome::Art::Point(*pos, _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1));
1463
1464                 (*l).second->property_points() = points;
1465                 (*l).second->set_data ("position", pos);
1466
1467                 (*l).first = *i;
1468         }
1469 }
1470
1471 void
1472 AudioRegionView::update_transient(float /*old_pos*/, float new_pos)
1473 {
1474         /* Find frame at old pos, calulate new frame then update region transients*/
1475         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1476
1477         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1478
1479                 /* Line has been updated in drag so we compare to new_pos */
1480
1481                 float* pos = (float*) (*l).second->get_data ("position");
1482
1483                 if (rint(new_pos) == rint(*pos)) {
1484
1485                     framepos_t old_frame = (*l).first;
1486                     framepos_t new_frame = trackview.editor().pixel_to_frame (new_pos);
1487
1488                     _region->update_transient (old_frame, new_frame);
1489
1490                     break;
1491                 }
1492         }
1493 }
1494
1495 void
1496 AudioRegionView::remove_transient(float pos)
1497 {
1498         /* Find frame at old pos, calulate new frame then update region transients*/
1499         list<std::pair<framepos_t, ArdourCanvas::Line*> >::iterator l;
1500
1501         for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1502
1503                 /* Line has been updated in drag so we compare to new_pos */
1504                 float *line_pos = (float*) (*l).second->get_data ("position");
1505
1506                 if (rint(pos) == rint(*line_pos)) {
1507                     _region->remove_transient ((*l).first);
1508                     break;
1509                 }
1510         }
1511 }
1512
1513 void
1514 AudioRegionView::thaw_after_trim ()
1515 {
1516         RegionView::thaw_after_trim ();
1517         unhide_envelope ();
1518         drag_end ();
1519 }
1520
1521 void
1522 AudioRegionView::redraw_start_xfade ()
1523 {
1524         boost::shared_ptr<AudioRegion> ar (audio_region());
1525
1526         if (!ar->fade_in() || ar->fade_in()->empty()) {
1527                 return;
1528         }
1529
1530         if (!ar->fade_in_is_xfade()) {
1531                 if (start_xfade_in) {
1532                         start_xfade_in->hide ();
1533                         start_xfade_out->hide ();
1534                         start_xfade_rect->hide ();
1535                 }
1536                 return;
1537         }
1538
1539         redraw_start_xfade_to (ar, ar->fade_in()->back()->when);
1540 }
1541
1542 void
1543 AudioRegionView::redraw_start_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t len)
1544 {
1545         int32_t const npoints = trackview.editor().frame_to_pixel (len);
1546
1547         if (npoints < 3) {
1548                 return;
1549         }
1550
1551         if (!start_xfade_in) {
1552                 start_xfade_in = new ArdourCanvas::Line (*group);
1553                 start_xfade_in->property_width_pixels() = 1;
1554                 start_xfade_in->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_GainLine.get();
1555         }
1556
1557         if (!start_xfade_out) {
1558                 start_xfade_out = new ArdourCanvas::Line (*group);
1559                 start_xfade_out->property_width_pixels() = 1;
1560                 uint32_t col = UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->canvasvar_GainLine.get(), 125);
1561                 start_xfade_out->property_fill_color_rgba() = col;
1562         }
1563
1564         if (!start_xfade_rect) {
1565                 start_xfade_rect = new ArdourCanvas::SimpleRect (*group);
1566                 start_xfade_rect->property_draw() = true;
1567                 start_xfade_rect->property_fill() = true;;
1568                 start_xfade_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
1569                 start_xfade_rect->property_outline_pixels() = 0;
1570                 start_xfade_rect->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_start_xfade_event), start_xfade_rect, this));
1571                 start_xfade_rect->set_data ("regionview", this);
1572         }
1573
1574         Points* points = get_canvas_points ("xfade edit redraw", npoints);
1575         boost::scoped_ptr<float> vec (new float[npoints]);
1576         double effective_height = _height - NAME_HIGHLIGHT_SIZE - 1.0;
1577
1578         ar->fade_in()->curve().get_vector (0, ar->fade_in()->back()->when, vec.get(), npoints);
1579
1580         for (int i = 0, pci = 0; i < npoints; ++i) {
1581                 Gnome::Art::Point &p ((*points)[pci++]);
1582                 p.set_x (i);
1583                 p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1584         }
1585
1586         start_xfade_rect->property_x1() = ((*points)[0]).get_x();
1587         start_xfade_rect->property_y1() = 1.0;
1588         start_xfade_rect->property_x2() = ((*points)[npoints-1]).get_x();
1589         start_xfade_rect->property_y2() = effective_height;
1590         start_xfade_rect->show ();
1591         start_xfade_rect->raise_to_top ();
1592
1593         start_xfade_in->property_points() = *points;
1594         start_xfade_in->show ();
1595         start_xfade_in->raise_to_top ();
1596
1597         /* fade out line */
1598
1599         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_in();
1600
1601         if (!inverse) {
1602
1603                 for (int i = 0, pci = 0; i < npoints; ++i) {
1604                         Gnome::Art::Point &p ((*points)[pci++]);
1605                         p.set_x (i);
1606                         p.set_y (1.0 + effective_height - (effective_height * (1.0 - vec.get()[i])));
1607                 }
1608
1609         } else {
1610
1611                 inverse->curve().get_vector (0, inverse->back()->when, vec.get(), npoints);
1612
1613                 for (int i = 0, pci = 0; i < npoints; ++i) {
1614                         Gnome::Art::Point &p ((*points)[pci++]);
1615                         p.set_x (i);
1616                         p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1617                 }
1618         }
1619
1620         start_xfade_out->property_points() = *points;
1621         start_xfade_out->show ();
1622         start_xfade_out->raise_to_top ();
1623
1624         delete points;
1625 }
1626
1627 void
1628 AudioRegionView::redraw_end_xfade ()
1629 {
1630         boost::shared_ptr<AudioRegion> ar (audio_region());
1631
1632         if (!ar->fade_out() || ar->fade_out()->empty()) {
1633                 return;
1634         }
1635
1636         if (!ar->fade_out_is_xfade()) {
1637                 if (end_xfade_in) {
1638                         end_xfade_in->hide ();
1639                         end_xfade_out->hide ();
1640                         end_xfade_rect->hide ();
1641                 }
1642                 return;
1643         }
1644
1645         redraw_end_xfade_to (ar, ar->fade_out()->back()->when);
1646 }
1647
1648 void
1649 AudioRegionView::redraw_end_xfade_to (boost::shared_ptr<AudioRegion> ar, framecnt_t len)
1650 {
1651         int32_t const npoints = trackview.editor().frame_to_pixel (len);
1652
1653         if (npoints < 3) {
1654                 return;
1655         }
1656
1657         if (!end_xfade_in) {
1658                 end_xfade_in = new ArdourCanvas::Line (*group);
1659                 end_xfade_in->property_width_pixels() = 1;
1660                 end_xfade_in->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_GainLine.get();
1661         }
1662
1663         if (!end_xfade_out) {
1664                 end_xfade_out = new ArdourCanvas::Line (*group);
1665                 end_xfade_out->property_width_pixels() = 1;
1666                 uint32_t col UINT_RGBA_CHANGE_A (ARDOUR_UI::config()->canvasvar_GainLine.get(), 125);
1667                 end_xfade_out->property_fill_color_rgba() = col;
1668         }
1669
1670         if (!end_xfade_rect) {
1671                 end_xfade_rect = new ArdourCanvas::SimpleRect (*group);
1672                 end_xfade_rect->property_draw() = true;
1673                 end_xfade_rect->property_fill() = true;;
1674                 end_xfade_rect->property_fill_color_rgba() =  ARDOUR_UI::config()->canvasvar_ActiveCrossfade.get();
1675                 end_xfade_rect->property_outline_pixels() = 0;
1676                 end_xfade_rect->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_end_xfade_event), end_xfade_rect, this));
1677                 end_xfade_rect->set_data ("regionview", this);
1678         }
1679
1680         Points* points = get_canvas_points ("xfade edit redraw", npoints);
1681         boost::scoped_ptr<float> vec (new float[npoints]);
1682
1683         ar->fade_out()->curve().get_vector (0, ar->fade_out()->back()->when, vec.get(), npoints);
1684
1685         double rend = trackview.editor().frame_to_pixel (_region->length() - len);
1686         double effective_height = _height - NAME_HIGHLIGHT_SIZE - 1;
1687
1688         for (int i = 0, pci = 0; i < npoints; ++i) {
1689                 Gnome::Art::Point &p ((*points)[pci++]);
1690                 p.set_x (rend + i);
1691                 p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1692         }
1693
1694         end_xfade_rect->property_x1() = ((*points)[0]).get_x();
1695         end_xfade_rect->property_y1() = 1;
1696         end_xfade_rect->property_x2() = ((*points)[npoints-1]).get_x();
1697         end_xfade_rect->property_y2() = effective_height;
1698         end_xfade_rect->show ();
1699         end_xfade_rect->raise_to_top ();
1700
1701         end_xfade_in->property_points() = *points;
1702         end_xfade_in->show ();
1703         end_xfade_in->raise_to_top ();
1704
1705         /* fade in line */
1706
1707         boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_out ();
1708
1709         if (!inverse) {
1710
1711                 for (int i = 0, pci = 0; i < npoints; ++i) {
1712                         Gnome::Art::Point &p ((*points)[pci++]);
1713                         p.set_x (rend + i);
1714                         p.set_y (1.0 + effective_height - (effective_height * (1.0 - vec.get()[i])));
1715                 }
1716
1717         } else {
1718
1719                 inverse->curve().get_vector (inverse->front()->when, inverse->back()->when, vec.get(), npoints);
1720
1721                 for (int i = 0, pci = 0; i < npoints; ++i) {
1722                         Gnome::Art::Point &p ((*points)[pci++]);
1723                         p.set_x (rend + i);
1724                         p.set_y (1.0 + effective_height - (effective_height * vec.get()[i]));
1725                 }
1726         }
1727
1728         end_xfade_out->property_points() = *points;
1729         end_xfade_out->show ();
1730         end_xfade_out->raise_to_top ();
1731
1732
1733         delete points;
1734 }
1735
1736 void
1737 AudioRegionView::hide_xfades ()
1738 {
1739         if (start_xfade_in) {
1740                 start_xfade_in->hide();
1741         }
1742         if (start_xfade_out) {
1743                 start_xfade_out->hide();
1744         }
1745         if (start_xfade_rect) {
1746                 start_xfade_rect->hide ();
1747         }
1748         if (end_xfade_in) {
1749                 end_xfade_in->hide();
1750         }
1751         if (end_xfade_out) {
1752                 end_xfade_out->hide();
1753         }
1754         if (end_xfade_rect) {
1755                 end_xfade_rect->hide ();
1756         }
1757 }
1758
1759 void
1760 AudioRegionView::show_xfades ()
1761 {
1762         if (start_xfade_in) {
1763                 start_xfade_in->show();
1764         }
1765         if (start_xfade_out) {
1766                 start_xfade_out->show();
1767         }
1768         if (start_xfade_rect) {
1769                 start_xfade_rect->show ();
1770         }
1771         if (end_xfade_in) {
1772                 end_xfade_in->show();
1773         }
1774         if (end_xfade_out) {
1775                 end_xfade_out->show();
1776         }
1777         if (end_xfade_rect) {
1778                 end_xfade_rect->show ();
1779         }
1780 }
1781
1782 void
1783 AudioRegionView::drag_start ()
1784 {
1785         TimeAxisViewItem::drag_start ();
1786         AudioTimeAxisView* atav = dynamic_cast<AudioTimeAxisView*> (&trackview);
1787
1788         if (atav) {
1789                 AudioStreamView* av = atav->audio_view();
1790                 if (av) {
1791                         /* this will hide our xfades too */
1792                         av->hide_xfades_with (audio_region());
1793                 }
1794         }
1795 }
1796
1797 void
1798 AudioRegionView::drag_end ()
1799 {
1800         TimeAxisViewItem::drag_end ();
1801         /* fades will be redrawn if they changed */
1802 }
1803
1804 AutomationLine::VisibleAspects
1805 AudioRegionView::automation_line_visibility () const
1806 {
1807         if (Config->get_show_region_gain() || trackview.editor().current_mouse_mode() == Editing::MouseGain) {
1808                 return AutomationLine::Line;
1809         } else {
1810                 return AutomationLine::VisibleAspects (0);
1811         }
1812 }