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