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