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