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