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