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