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