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