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