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