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