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