c3e3b9a0368b8af1fa7ac3dbfbc61f48f236a269
[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, wfd);
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         AudioGhostRegion* agr;
352
353         RegionView::region_resized(what_changed);
354
355         if (what_changed & Change (StartChanged|LengthChanged)) {
356
357                 for (uint32_t n = 0; n < waves.size(); ++n) {
358                         waves[n]->property_region_start() = _region->start();
359                 }
360                 
361                 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
362                         if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
363
364                                 for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
365                                         (*w)->property_region_start() = _region->start();
366                                 }
367                         }
368                 }
369         }
370 }
371
372 void
373 AudioRegionView::reset_width_dependent_items (double pixel_width)
374 {
375         RegionView::reset_width_dependent_items(pixel_width);
376         assert(_pixel_width == pixel_width);
377
378         if (zero_line) {
379                 zero_line->property_x2() = pixel_width - 1.0;
380         }
381
382         if (fade_in_handle) {
383                 if (pixel_width <= 6.0) {
384                         fade_in_handle->hide();
385                         fade_out_handle->hide();
386                 } else {
387                         if (_height < 5.0) {
388                                 fade_in_handle->hide();
389                                 fade_out_handle->hide();
390                         } else {
391                                 fade_in_handle->show();
392                                 fade_out_handle->show();
393                         }
394                 }
395         }
396
397         reset_fade_shapes ();
398 }
399
400 void
401 AudioRegionView::region_muted ()
402 {
403         RegionView::region_muted();
404
405         for (uint32_t n=0; n < waves.size(); ++n) {
406                 if (_region->muted()) {
407                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
408                 } else {
409                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
410                 }
411         }
412 }
413
414 void
415 AudioRegionView::set_y_position_and_height (double y, double h)
416 {
417         RegionView::set_y_position_and_height(y, h - 1);
418         
419         const uint32_t wcnt = waves.size();
420
421         _y_position = y;
422         _height = h;
423
424         for (uint32_t n = 0; n < wcnt; ++n) {
425                 double ht;
426
427                 if (h <= NAME_HIGHLIGHT_THRESH) {
428                         ht = ((_height - 2 * wcnt) / (double) wcnt);
429                 } else {
430                         ht = (((_height - 2 * wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
431                 }
432                 
433                 double const yoff = n * (ht + 1);
434                 
435                 waves[n]->property_height() = ht;
436                 waves[n]->property_y() = _y_position + yoff + 2;
437         }
438
439         if (gain_line) {
440                 if ((_height / wcnt) < NAME_HIGHLIGHT_SIZE) {
441                         gain_line->hide ();
442                 } else {
443                         if (_flags & EnvelopeVisible) {
444                                 gain_line->show ();
445                         }
446                 }
447                 gain_line->set_y_position_and_height ((uint32_t) _y_position, (uint32_t) rint (_height - NAME_HIGHLIGHT_SIZE));
448         }
449
450         setup_fade_handle_positions ();
451         manage_zero_line ();
452         reset_fade_shapes ();
453         
454         if (name_text) {
455                 name_text->raise_to_top();
456         }
457 }
458
459 void
460 AudioRegionView::setup_fade_handle_positions()
461 {
462         /* position of fade handle offset from the top of the region view */
463         double const handle_pos = 2;
464         /* height of fade handles */
465         double const handle_height = 5;
466
467         if (fade_in_handle) {
468                 fade_in_handle->property_y1() = _y_position + handle_pos;
469                 fade_in_handle->property_y2() = _y_position + handle_pos + handle_height;
470         }
471         
472         if (fade_out_handle) {
473                 fade_out_handle->property_y1() = _y_position + handle_pos;
474                 fade_out_handle->property_y2() = _y_position + handle_pos + handle_height;
475         }
476 }
477
478 void
479 AudioRegionView::manage_zero_line ()
480 {
481         if (!zero_line) {
482                 return;
483         }
484
485         if (_height >= 100) {
486                 double const wave_midpoint = _y_position + (_height - NAME_HIGHLIGHT_SIZE) / 2.0;
487                 zero_line->property_y1() = wave_midpoint;
488                 zero_line->property_y2() = wave_midpoint;
489                 zero_line->show();
490         } else {
491                 zero_line->hide();
492         }
493 }
494
495 void
496 AudioRegionView::reset_fade_shapes ()
497 {
498         reset_fade_in_shape ();
499         reset_fade_out_shape ();
500 }
501
502 void
503 AudioRegionView::reset_fade_in_shape ()
504 {
505         reset_fade_in_shape_width ((nframes_t) audio_region()->fade_in()->back()->when);
506 }
507         
508 void
509 AudioRegionView::reset_fade_in_shape_width (nframes_t width)
510 {
511         if (fade_in_handle == 0) {
512                 return;
513         }
514
515         /* smallest size for a fade is 64 frames */
516
517         width = std::max ((nframes_t) 64, width);
518
519         Points* points;
520         double pwidth = width / samples_per_unit;
521         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
522         double h; 
523         
524         if (_height < 5) {
525                 fade_in_shape->hide();
526                 fade_in_handle->hide();
527                 return;
528         }
529
530         double handle_center;
531         handle_center = pwidth;
532         
533         if (handle_center > 7.0) {
534                 handle_center -= 3.0;
535         } else {
536                 handle_center = 3.0;
537         }
538
539         fade_in_handle->property_x1() =  handle_center - 3.0;
540         fade_in_handle->property_x2() =  handle_center + 3.0;
541         
542         if (pwidth < 5) {
543                 fade_in_shape->hide();
544                 return;
545         }
546
547         fade_in_shape->show();
548
549         float curve[npoints];
550         audio_region()->fade_in()->curve().get_vector (0, audio_region()->fade_in()->back()->when, curve, npoints);
551
552         points = get_canvas_points ("fade in shape", npoints+3);
553
554         if (_height > NAME_HIGHLIGHT_THRESH) {
555                 h = _height - NAME_HIGHLIGHT_SIZE;
556         } else {
557                 h = _height;
558         }
559
560         /* points *MUST* be in anti-clockwise order */
561
562         uint32_t pi, pc;
563         double xdelta = pwidth/npoints;
564
565         for (pi = 0, pc = 0; pc < npoints; ++pc) {
566                 (*points)[pi].set_x(1 + (pc * xdelta));
567                 (*points)[pi++].set_y(_y_position + 2 + (h - (curve[pc] * h)));
568         }
569         
570         /* fold back */
571
572         (*points)[pi].set_x(pwidth);
573         (*points)[pi++].set_y(_y_position + 2);
574
575         (*points)[pi].set_x(1);
576         (*points)[pi++].set_y(_y_position + 2);
577
578         /* connect the dots ... */
579
580         (*points)[pi] = (*points)[0];
581         
582         fade_in_shape->property_points() = *points;
583         delete points;
584 }
585
586 void
587 AudioRegionView::reset_fade_out_shape ()
588 {
589         reset_fade_out_shape_width ((nframes_t) audio_region()->fade_out()->back()->when);
590 }
591
592 void
593 AudioRegionView::reset_fade_out_shape_width (nframes_t width)
594 {       
595         if (fade_out_handle == 0) {
596                 return;
597         }
598
599         /* smallest size for a fade is 64 frames */
600
601         width = std::max ((nframes_t) 64, width);
602
603         Points* points;
604         double pwidth = width / samples_per_unit;
605         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
606         double h;
607
608         if (_height < 5) {
609                 fade_out_shape->hide();
610                 fade_out_handle->hide();
611                 return;
612         }
613
614         double handle_center;
615         handle_center = (_region->length() - width) / samples_per_unit;
616         
617         if (handle_center > 7.0) {
618                 handle_center -= 3.0;
619         } else {
620                 handle_center = 3.0;
621         }
622         
623         fade_out_handle->property_x1() =  handle_center - 3.0;
624         fade_out_handle->property_x2() =  handle_center + 3.0;
625
626         /* don't show shape if its too small */
627         
628         if (pwidth < 5) {
629                 fade_out_shape->hide();
630                 return;
631         } 
632         
633         fade_out_shape->show();
634
635         float curve[npoints];
636         audio_region()->fade_out()->curve().get_vector (0, audio_region()->fade_out()->back()->when, curve, npoints);
637
638         if (_height > NAME_HIGHLIGHT_THRESH) {
639                 h = _height - NAME_HIGHLIGHT_SIZE;
640         } else {
641                 h = _height;
642         }
643
644         /* points *MUST* be in anti-clockwise order */
645
646         points = get_canvas_points ("fade out shape", npoints+3);
647
648         uint32_t pi, pc;
649         double xdelta = pwidth/npoints;
650
651         for (pi = 0, pc = 0; pc < npoints; ++pc) {
652                 (*points)[pi].set_x(_pixel_width - 1 - pwidth + (pc*xdelta));
653                 (*points)[pi++].set_y(_y_position + 2 + (h - (curve[pc] * h)));
654         }
655         
656         /* fold back */
657
658         (*points)[pi].set_x(_pixel_width);
659         (*points)[pi++].set_y(_y_position + h);
660
661         (*points)[pi].set_x(_pixel_width);
662         (*points)[pi++].set_y(_y_position + 2);
663
664         /* connect the dots ... */
665
666         (*points)[pi] = (*points)[0];
667
668         fade_out_shape->property_points() = *points;
669         delete points;
670 }
671
672 void
673 AudioRegionView::set_samples_per_unit (gdouble spu)
674 {
675         RegionView::set_samples_per_unit (spu);
676
677         if (_flags & WaveformVisible) {
678                 for (uint32_t n=0; n < waves.size(); ++n) {
679                         waves[n]->property_samples_per_unit() = spu;
680                 }
681         }
682
683         if (gain_line) {
684                 gain_line->reset ();
685         }
686
687         reset_fade_shapes ();
688 }
689
690 void
691 AudioRegionView::set_amplitude_above_axis (gdouble spp)
692 {
693         for (uint32_t n=0; n < waves.size(); ++n) {
694                 waves[n]->property_amplitude_above_axis() = spp;
695         }
696 }
697
698 void
699 AudioRegionView::compute_colors (Gdk::Color& basic_color)
700 {
701         RegionView::compute_colors(basic_color);
702         
703         uint32_t r, g, b, a;
704
705         /* gain color computed in envelope_active_changed() */
706
707         UINT_TO_RGBA (fill_color, &r, &g, &b, &a);
708         fade_color = RGBA_TO_UINT(r,g,b,120);
709 }
710
711 void
712 AudioRegionView::set_colors ()
713 {
714         RegionView::set_colors();
715         
716         if (gain_line) {
717                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
718         }
719
720         for (uint32_t n=0; n < waves.size(); ++n) {
721                 if (_region->muted()) {
722                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
723                 } else {
724                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
725                 }
726
727                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
728                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
729         }
730 }
731
732 void
733 AudioRegionView::show_region_editor ()
734 {
735         if (editor == 0) {
736                 editor = new AudioRegionEditor (trackview.session(), audio_region(), *this);
737                 // GTK2FIX : how to ensure float without realizing
738                 // editor->realize ();
739                 // trackview.editor.ensure_float (*editor);
740         } 
741
742         editor->present ();
743         editor->show_all();
744 }
745
746 void
747 AudioRegionView::set_waveform_visible (bool yn)
748 {
749         if (((_flags & WaveformVisible) != yn)) {
750                 if (yn) {
751                         for (uint32_t n=0; n < waves.size(); ++n) {
752                                 /* make sure the zoom level is correct, since we don't update
753                                    this when waveforms are hidden.
754                                 */
755                                 waves[n]->property_samples_per_unit() = samples_per_unit;
756                                 waves[n]->show();
757                         }
758                         _flags |= WaveformVisible;
759                 } else {
760                         for (uint32_t n=0; n < waves.size(); ++n) {
761                                 waves[n]->hide();
762                         }
763                         _flags &= ~WaveformVisible;
764                 }
765                 store_flags ();
766         }
767 }
768
769 void
770 AudioRegionView::temporarily_hide_envelope ()
771 {
772         if (gain_line) {
773                 gain_line->hide ();
774         }
775 }
776
777 void
778 AudioRegionView::unhide_envelope ()
779 {
780         if (gain_line && (_flags & EnvelopeVisible)) {
781                 gain_line->show ();
782         }
783 }
784
785 void
786 AudioRegionView::set_envelope_visible (bool yn)
787 {
788         if (gain_line && ((_flags & EnvelopeVisible) != yn)) {
789                 if (yn) {
790                         gain_line->show ();
791                         _flags |= EnvelopeVisible;
792                 } else {
793                         gain_line->hide ();
794                         _flags &= ~EnvelopeVisible;
795                 }
796                 store_flags ();
797         }
798 }
799
800 void
801 AudioRegionView::create_waves ()
802 {
803         //cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
804         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
805
806         if (!atv.get_diskstream()) {
807                 return;
808         }
809
810         ChanCount nchans = atv.get_diskstream()->n_channels();
811
812         cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
813                         << " and channels = " << nchans.n_audio() << endl;
814         
815         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
816         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
817                 tmp_waves.push_back (0);
818         }
819
820         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
821                 
822                 if (n >= audio_region()->n_channels()) {
823                         break;
824                 }
825                 
826                 wave_caches.push_back (WaveView::create_cache ());
827
828                 cerr << "\tchannel " << n << endl;
829
830                 if (wait_for_data) {
831                         if (audio_region()->audio_source(n)->peaks_ready (bind (mem_fun(*this, &AudioRegionView::peaks_ready_handler), n), data_ready_connection)) {
832                                 cerr << "\tData is ready\n";
833                                 cerr << "\tData is ready\n";
834                                 create_one_wave (n, true);
835                         } else {
836                                 cerr << "\tdata is not ready\n";
837                                 // we'll get a PeaksReady signal from the source in the future
838                                 // and will call create_one_wave(n) then.
839                         }
840                         
841                 } else {
842                         cerr << "\tdon't delay, display today!\n";
843                         create_one_wave (n, true);
844                 }
845
846         }
847 }
848
849 void
850 AudioRegionView::create_one_wave (uint32_t which, bool direct)
851 {
852         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
853         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
854         uint32_t nchans = atv.get_diskstream()->n_channels().n_audio();
855         uint32_t n;
856         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
857         gdouble ht;
858
859         if (trackview.height < NAME_HIGHLIGHT_SIZE) {
860                 ht = ((trackview.height) / (double) nchans);
861         } else {
862                 ht = ((trackview.height - NAME_HIGHLIGHT_SIZE) / (double) nchans);
863         }
864
865         gdouble yoff = which * ht;
866
867         WaveView *wave = new WaveView(*group);
868
869         wave->property_data_src() = (gpointer) _region.get();
870         wave->property_cache() =  wave_caches[which];
871         wave->property_cache_updater() = true;
872         wave->property_channel() =  which;
873         wave->property_length_function() = (gpointer) region_length_from_c;
874         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
875         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
876         wave->property_x() =  0.0;
877         wave->property_y() =  yoff;
878         wave->property_height() =  (double) ht;
879         wave->property_samples_per_unit() =  samples_per_unit;
880         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
881         wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
882         wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
883         wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
884         wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
885         wave->property_region_start() = _region->start();
886         wave->property_rectified() = (bool) (_flags & WaveformRectified);
887         wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
888
889         if (!(_flags & WaveformVisible)) {
890                 wave->hide();
891         }
892
893         /* note: calling this function is serialized by the lock
894            held in the peak building thread that signals that
895            peaks are ready for use *or* by the fact that it is
896            called one by one from the GUI thread.
897         */
898
899         if (which < nchans) {
900                 tmp_waves[which] = wave;
901         } else {
902                 /* n-channel track, >n-channel source */
903         }
904         
905         /* see if we're all ready */
906         
907         for (n = 0; n < nchans; ++n) {
908                 if (tmp_waves[n] == 0) {
909                         break;
910                 }
911         }
912
913         if (n == nwaves && waves.empty()) {
914                 /* all waves are ready */
915                 tmp_waves.resize(nwaves);
916
917                 waves = tmp_waves;
918                 tmp_waves.clear ();
919
920                 /* all waves created, don't hook into peaks ready anymore */
921                 data_ready_connection.disconnect ();            
922
923                 if(0)
924                 if (!zero_line) {
925                         zero_line = new ArdourCanvas::SimpleLine (*group);
926                         zero_line->property_x1() = (gdouble) 1.0;
927                         zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
928                         zero_line->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
929                         manage_zero_line ();
930                 }
931         }
932 }
933
934 void
935 AudioRegionView::peaks_ready_handler (uint32_t which)
936 {
937         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &AudioRegionView::create_one_wave), which, false));
938         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
939 }
940
941 void
942 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
943 {
944         if (gain_line == 0) {
945                 return;
946         }
947
948         double x, y;
949
950         /* don't create points that can't be seen */
951
952         set_envelope_visible (true);
953         
954         x = ev->button.x;
955         y = ev->button.y;
956
957         item->w2i (x, y);
958
959         nframes_t fx = trackview.editor.pixel_to_frame (x);
960
961         if (fx > _region->length()) {
962                 return;
963         }
964
965         /* compute vertical fractional position */
966
967         y = 1.0 - ((y - _y_position) / (_height - NAME_HIGHLIGHT_SIZE));
968         
969         /* map using gain line */
970
971         gain_line->view_to_model_y (y);
972
973         trackview.session().begin_reversible_command (_("add gain control point"));
974         XMLNode &before = audio_region()->envelope()->get_state();
975
976         if (!audio_region()->envelope_active()) {
977                 XMLNode &region_before = audio_region()->get_state();
978                 audio_region()->set_envelope_active(true);
979                 XMLNode &region_after = audio_region()->get_state();
980                 trackview.session().add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
981         }
982
983         audio_region()->envelope()->add (fx, y);
984         
985         XMLNode &after = audio_region()->envelope()->get_state();
986         trackview.session().add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
987         trackview.session().commit_reversible_command ();
988 }
989
990 void
991 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
992 {
993         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
994         audio_region()->envelope()->erase (cp->model());
995 }
996
997 void
998 AudioRegionView::store_flags()
999 {
1000         XMLNode *node = new XMLNode ("GUI");
1001
1002         node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
1003         node->add_property ("envelope-visible", (_flags & EnvelopeVisible) ? "yes" : "no");
1004         node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1005         node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1006
1007         _region->add_extra_xml (*node);
1008 }
1009
1010 void
1011 AudioRegionView::set_flags (XMLNode* node)
1012 {
1013         XMLProperty *prop;
1014
1015         if ((prop = node->property ("waveform-visible")) != 0) {
1016                 if (prop->value() == "yes") {
1017                         _flags |= WaveformVisible;
1018                 }
1019         }
1020
1021         if ((prop = node->property ("envelope-visible")) != 0) {
1022                 if (prop->value() == "yes") {
1023                         _flags |= EnvelopeVisible;
1024                 }
1025         }
1026
1027         if ((prop = node->property ("waveform-rectified")) != 0) {
1028                 if (prop->value() == "yes") {
1029                         _flags |= WaveformRectified;
1030                 }
1031         }
1032
1033         if ((prop = node->property ("waveform-logscaled")) != 0) {
1034                 if (prop->value() == "yes") {
1035                         _flags |= WaveformLogScaled;
1036                 }
1037         }
1038 }
1039         
1040 void
1041 AudioRegionView::set_waveform_shape (WaveformShape shape)
1042 {
1043         bool yn;
1044
1045         /* this slightly odd approach is to leave the door open to 
1046            other "shapes" such as spectral displays, etc.
1047         */
1048
1049         switch (shape) {
1050         case Rectified:
1051                 yn = true;
1052                 break;
1053
1054         default:
1055                 yn = false;
1056                 break;
1057         }
1058
1059         if (yn != (bool) (_flags & WaveformRectified)) {
1060                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1061                         (*wave)->property_rectified() = yn;
1062                 }
1063
1064                 if (zero_line) {
1065                         if (yn) {
1066                                 zero_line->hide();
1067                         } else {
1068                                 zero_line->show();
1069                         }
1070                 }
1071
1072                 if (yn) {
1073                         _flags |= WaveformRectified;
1074                 } else {
1075                         _flags &= ~WaveformRectified;
1076                 }
1077                 store_flags ();
1078         }
1079 }
1080
1081 void
1082 AudioRegionView::set_waveform_scale (WaveformScale scale)
1083 {
1084         bool yn = (scale == LogWaveform);
1085
1086         if (yn != (bool) (_flags & WaveformLogScaled)) {
1087                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1088                         (*wave)->property_logscaled() = yn;
1089                 }
1090
1091                 if (yn) {
1092                         _flags |= WaveformLogScaled;
1093                 } else {
1094                         _flags &= ~WaveformLogScaled;
1095                 }
1096                 store_flags ();
1097         }
1098 }
1099
1100
1101 GhostRegion*
1102 AudioRegionView::add_ghost (TimeAxisView& tv)
1103 {
1104         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1105         assert(rtv);
1106
1107         double unit_position = _region->position () / samples_per_unit;
1108         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1109         uint32_t nchans;
1110         
1111         nchans = rtv->get_diskstream()->n_channels().n_audio();
1112
1113         for (uint32_t n = 0; n < nchans; ++n) {
1114                 
1115                 if (n >= audio_region()->n_channels()) {
1116                         break;
1117                 }
1118                 
1119                 WaveView *wave = new WaveView(*ghost->group);
1120
1121                 wave->property_data_src() = _region.get();
1122                 wave->property_cache() =  wave_caches[n];
1123                 wave->property_cache_updater() = false;
1124                 wave->property_channel() = n;
1125                 wave->property_length_function() = (gpointer)region_length_from_c;
1126                 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1127                 wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
1128                 wave->property_x() =  0.0;
1129                 wave->property_samples_per_unit() =  samples_per_unit;
1130                 wave->property_amplitude_above_axis() =  _amplitude_above_axis;
1131
1132                 wave->property_region_start() = _region->start();
1133
1134                 ghost->waves.push_back(wave);
1135         }
1136
1137         ghost->set_height ();
1138         ghost->set_duration (_region->length() / samples_per_unit);
1139         ghost->set_colors();
1140         ghosts.push_back (ghost);
1141
1142         ghost->GoingAway.connect (mem_fun(*this, &AudioRegionView::remove_ghost));
1143
1144         return ghost;
1145 }
1146
1147 void
1148 AudioRegionView::entered ()
1149 {
1150         if (gain_line && _flags & EnvelopeVisible) {
1151                 gain_line->show_all_control_points ();
1152         }
1153
1154         uint32_t r,g,b,a;
1155         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1156         a=255;
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::exited ()
1166 {
1167         if (gain_line) {
1168                 gain_line->hide_all_but_selected_control_points ();
1169         }
1170         
1171         uint32_t r,g,b,a;
1172         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1173         a=0;
1174         
1175         if (fade_in_handle) {
1176                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1177                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1178         }
1179 }
1180
1181 void
1182 AudioRegionView::envelope_active_changed ()
1183 {
1184         if (gain_line) {
1185                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1186         }
1187 }
1188
1189 void
1190 AudioRegionView::set_waveview_data_src()
1191 {
1192         AudioGhostRegion* agr;
1193         double unit_length= _region->length() / samples_per_unit;
1194
1195         for (uint32_t n = 0; n < waves.size(); ++n) {
1196                 // TODO: something else to let it know the channel
1197                 waves[n]->property_data_src() = _region.get();
1198         }
1199         
1200         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1201                 
1202                 (*i)->set_duration (unit_length);
1203                 
1204                 if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
1205                         for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
1206                                 (*w)->property_data_src() = _region.get();
1207                         }
1208                 }
1209         }
1210
1211 }
1212
1213 void
1214 AudioRegionView::color_handler ()
1215 {
1216         //case cMutedWaveForm:
1217         //case cWaveForm:
1218         //case cWaveFormClip:
1219         //case cZeroLine:
1220         set_colors ();
1221
1222         //case cGainLineInactive:
1223         //case cGainLine:
1224         envelope_active_changed();
1225
1226 }
1227
1228 void
1229 AudioRegionView::set_frame_color ()
1230 {
1231         if (!frame) {
1232                 return;
1233         }
1234
1235         if (_region->opaque()) {
1236                 fill_opacity = 130;
1237         } else {
1238                 fill_opacity = 0;
1239         }
1240
1241         uint32_t r,g,b,a;
1242         
1243         if (_selected && should_show_selection) {
1244                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get(), &r, &g, &b, &a);
1245                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1246
1247                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1248                         if (_region->muted()) {
1249                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1250                         } else {
1251                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1252                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1253                         }
1254                 }
1255         } else {
1256                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_FrameBase.get(), &r, &g, &b, &a);
1257                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1258
1259                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1260                         if (_region->muted()) {
1261                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1262                         } else {
1263                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1264                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1265                         }
1266                 }
1267         }
1268 }