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