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