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