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