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