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