c7bb048945d107711e60a62a351060a357ce65e9
[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 #include <pbd/stacktrace.h>
35
36 #include "streamview.h"
37 #include "audio_region_view.h"
38 #include "audio_time_axis.h"
39 #include "simplerect.h"
40 #include "simpleline.h"
41 #include "waveview.h"
42 #include "public_editor.h"
43 #include "audio_region_editor.h"
44 #include "region_gain_line.h"
45 #include "ghostregion.h"
46 #include "audio_time_axis.h"
47 #include "utils.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50
51 #include "i18n.h"
52
53 using namespace sigc;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Editing;
57 using namespace ArdourCanvas;
58
59 static const int32_t sync_mark_width = 9;
60
61 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
62                                   Gdk::Color& basic_color)
63         : RegionView (parent, tv, r, spu, basic_color)
64         , sync_mark(0)
65         , zero_line(0)
66         , fade_in_shape(0)
67         , fade_out_shape(0)
68         , fade_in_handle(0)
69         , fade_out_handle(0)
70         , gain_line(0)
71         , _amplitude_above_axis(1.0)
72         , _flags(0)
73         , fade_color(0)
74 {
75 }
76
77 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu, 
78                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
79         : RegionView (parent, tv, r, spu, basic_color, visibility)
80         , sync_mark(0)
81         , zero_line(0)
82         , fade_in_shape(0)
83         , fade_out_shape(0)
84         , fade_in_handle(0)
85         , fade_out_handle(0)
86         , gain_line(0)
87         , _amplitude_above_axis(1.0)
88         , _flags(0)
89         , fade_color(0)
90 {
91 }
92
93 void
94 AudioRegionView::init (Gdk::Color& basic_color, bool wfd)
95 {
96         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
97         // where order is important and where it isn't...
98         
99         RegionView::init(basic_color, wfd);
100
101         XMLNode *node;
102
103         _amplitude_above_axis = 1.0;
104         zero_line             = 0;
105         _flags                = 0;
106
107         if ((node = _region->extra_xml ("GUI")) != 0) {
108                 set_flags (node);
109         } else {
110                 _flags = WaveformVisible;
111                 store_flags ();
112         }
113
114         if (trackview.editor.new_regionviews_display_gain()) {
115                 _flags |= EnvelopeVisible;
116         }
117
118         compute_colors (basic_color);
119
120         create_waves ();
121
122         fade_in_shape = new ArdourCanvas::Polygon (*group);
123         fade_in_shape->property_fill_color_rgba() = fade_color;
124         fade_in_shape->set_data ("regionview", this);
125         
126         fade_out_shape = new ArdourCanvas::Polygon (*group);
127         fade_out_shape->property_fill_color_rgba() = fade_color;
128         fade_out_shape->set_data ("regionview", this);
129
130
131         {
132                 uint32_t r,g,b,a;
133                 UINT_TO_RGBA(fill_color,&r,&g,&b,&a);
134         
135
136                 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
137                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
138                 fade_in_handle->property_outline_pixels() = 0;
139                 fade_in_handle->property_y1() = 2.0;
140                 fade_in_handle->property_y2() = 7.0;
141                 
142                 fade_in_handle->set_data ("regionview", this);
143                 
144                 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
145                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
146                 fade_out_handle->property_outline_pixels() = 0;
147                 fade_out_handle->property_y1() = 2.0;
148                 fade_out_handle->property_y2() = 7.0;
149                 
150                 fade_out_handle->set_data ("regionview", this);
151         }
152
153         string foo = _region->name();
154         foo += ':';
155         foo += "gain";
156
157         gain_line = new AudioRegionGainLine (foo, trackview.session(), *this, *group, audio_region()->envelope());
158
159         if (!(_flags & EnvelopeVisible)) {
160                 gain_line->hide ();
161         } else {
162                 gain_line->show ();
163         }
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 boost::shared_ptr<ARDOUR::AudioRegion>
208 AudioRegionView::audio_region() const
209 {
210         // "Guaranteed" to succeed...
211         return boost::dynamic_pointer_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.get();
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 ((nframes_t) audio_region()->fade_in().back()->when);
452 }
453         
454 void
455 AudioRegionView::reset_fade_in_shape_width (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 ((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 ((nframes_t) audio_region()->fade_out().back()->when);
536 }
537
538 void
539 AudioRegionView::reset_fade_out_shape_width (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 ((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                 if (zero_line) {
775                         delete zero_line;
776                 }
777                 zero_line = new ArdourCanvas::SimpleLine (*group);
778                 zero_line->property_x1() = (gdouble) 1.0;
779                 zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
780                 zero_line->property_color_rgba() = (guint) color_map[cZeroLine];
781                 manage_zero_line ();
782         }
783 }
784
785 void
786 AudioRegionView::create_one_wave (uint32_t which, bool direct)
787 {
788         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
789         uint32_t nchans = atv.get_diskstream()->n_channels();
790         uint32_t n;
791         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
792         gdouble ht;
793
794         if (trackview.height < NAME_HIGHLIGHT_SIZE) {
795                 ht = ((trackview.height) / (double) nchans);
796         } else {
797                 ht = ((trackview.height - NAME_HIGHLIGHT_SIZE) / (double) nchans);
798         }
799
800         gdouble yoff = which * ht;
801
802         WaveView *wave = new WaveView(*group);
803
804         wave->property_data_src() = (gpointer) _region.get();
805         wave->property_cache() =  wave_caches[which];
806         wave->property_cache_updater() = true;
807         wave->property_channel() =  which;
808         wave->property_length_function() = (gpointer) region_length_from_c;
809         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
810         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
811         wave->property_x() =  0.0;
812         wave->property_y() =  yoff;
813         wave->property_height() =  (double) ht;
814         wave->property_samples_per_unit() =  samples_per_unit;
815         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
816         wave->property_wave_color() = _region->muted() ? color_map[cMutedWaveForm] : color_map[cWaveForm];
817         wave->property_region_start() = _region->start();
818
819         if (!(_flags & WaveformVisible)) {
820                 wave->hide();
821         }
822
823         /* note: calling this function is serialized by the lock
824            held in the peak building thread that signals that
825            peaks are ready for use *or* by the fact that it is
826            called one by one from the GUI thread.
827         */
828
829         if (which < nchans) {
830                 tmp_waves[which] = wave;
831         } else {
832                 /* n-channel track, >n-channel source */
833         }
834         
835         /* see if we're all ready */
836         
837         for (n = 0; n < nchans; ++n) {
838                 if (tmp_waves[n] == 0) {
839                         break;
840                 }
841         }
842         
843         if (n == nwaves && waves.empty()) {
844                 /* all waves are ready */
845                 tmp_waves.resize(nwaves);
846
847                 waves = tmp_waves;
848                 tmp_waves.clear ();
849
850                 if (!zero_line) {
851                         zero_line = new ArdourCanvas::SimpleLine (*group);
852                         zero_line->property_x1() = (gdouble) 1.0;
853                         zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
854                         zero_line->property_color_rgba() = (guint) color_map[cZeroLine];
855                         manage_zero_line ();
856                 }
857         }
858 }
859
860 void
861 AudioRegionView::peaks_ready_handler (uint32_t which)
862 {
863         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &AudioRegionView::create_one_wave), which, false));
864
865         if (!waves.empty()) {
866                 /* all waves created, don't hook into peaks ready anymore */
867                 data_ready_connection.disconnect ();            
868         }
869 }
870
871 void
872 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
873 {
874         if (gain_line == 0) {
875                 return;
876         }
877
878         double x, y;
879
880         /* don't create points that can't be seen */
881
882         set_envelope_visible (true);
883         
884         x = ev->button.x;
885         y = ev->button.y;
886
887         item->w2i (x, y);
888
889         nframes_t fx = trackview.editor.pixel_to_frame (x);
890
891         if (fx > _region->length()) {
892                 return;
893         }
894
895         /* compute vertical fractional position */
896
897         y = 1.0 - (y / (trackview.height - NAME_HIGHLIGHT_SIZE));
898         
899         /* map using gain line */
900
901         gain_line->view_to_model_y (y);
902
903         trackview.session().begin_reversible_command (_("add gain control point"));
904         XMLNode &before = audio_region()->envelope().get_state();
905
906         if (!audio_region()->envelope_active()) {
907                 XMLNode &region_before = audio_region()->get_state();
908                 audio_region()->set_envelope_active(true);
909                 XMLNode &region_after = audio_region()->get_state();
910                 trackview.session().add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_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 }