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