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