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