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