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