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