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