* first working version of editing MIDI channels of individual notes, see: http:...
[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 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu, 
82                                   Gdk::Color& basic_color, TimeAxisViewItem::Visibility visibility)
83         : RegionView (parent, tv, r, spu, basic_color, visibility)
84         , sync_mark(0)
85         , zero_line(0)
86         , fade_in_shape(0)
87         , fade_out_shape(0)
88         , fade_in_handle(0)
89         , fade_out_handle(0)
90         , gain_line(0)
91         , _amplitude_above_axis(1.0)
92         , _flags(0)
93         , fade_color(0)
94 {
95 }
96
97
98 AudioRegionView::AudioRegionView (const AudioRegionView& other)
99         : RegionView (other)
100         , zero_line(0)
101         , fade_in_shape(0)
102         , fade_out_shape(0)
103         , fade_in_handle(0)
104         , fade_out_handle(0)
105         , gain_line(0)
106         , _amplitude_above_axis(1.0)
107         , _flags(0)
108         , fade_color(0)
109
110 {
111         Gdk::Color c;
112         int r,g,b,a;
113
114         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
115         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
116         
117         init (c, false);
118 }
119
120 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
121         : RegionView (other, boost::shared_ptr<Region> (other_region))
122         , zero_line(0)
123         , fade_in_shape(0)
124         , fade_out_shape(0)
125         , fade_in_handle(0)
126         , fade_out_handle(0)
127         , gain_line(0)
128         , _amplitude_above_axis(1.0)
129         , _flags(0)
130         , fade_color(0)
131
132 {
133         Gdk::Color c;
134         int r,g,b,a;
135
136         UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
137         c.set_rgb_p (r/255.0, g/255.0, b/255.0);
138
139         init (c, true);
140 }
141
142 void
143 AudioRegionView::init (Gdk::Color& basic_color, bool wfd)
144 {
145         // FIXME: Some redundancy here with RegionView::init.  Need to figure out
146         // where order is important and where it isn't...
147         
148         RegionView::init (basic_color, wfd);
149         
150         XMLNode *node;
151
152         _amplitude_above_axis = 1.0;
153         zero_line             = 0;
154         _flags                = 0;
155
156         if ((node = _region->extra_xml ("GUI")) != 0) {
157                 set_flags (node);
158         } else {
159                 _flags = WaveformVisible;
160                 store_flags ();
161         }
162
163         if (trackview.editor.new_regionviews_display_gain()) {
164                 _flags |= EnvelopeVisible;
165         }
166
167         compute_colors (basic_color);
168         
169         create_waves ();
170
171         fade_in_shape = new ArdourCanvas::Polygon (*group);
172         fade_in_shape->property_fill_color_rgba() = fade_color;
173         fade_in_shape->set_data ("regionview", this);
174         
175         fade_out_shape = new ArdourCanvas::Polygon (*group);
176         fade_out_shape->property_fill_color_rgba() = fade_color;
177         fade_out_shape->set_data ("regionview", this);
178
179
180         {
181                 uint32_t r,g,b,a;
182                 UINT_TO_RGBA(fill_color,&r,&g,&b,&a);
183         
184
185                 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
186                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
187                 fade_in_handle->property_outline_pixels() = 0;
188                 
189                 fade_in_handle->set_data ("regionview", this);
190                 
191                 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
192                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
193                 fade_out_handle->property_outline_pixels() = 0;
194                 
195                 fade_out_handle->set_data ("regionview", this);
196         }
197
198         setup_fade_handle_positions ();
199
200         string line_name = _region->name();
201         line_name += ':';
202         line_name += "gain";
203
204         if (!Profile->get_sae()) {
205                 gain_line = new AudioRegionGainLine (line_name, trackview.session(), *this, *group, audio_region()->envelope());
206         }
207
208         if (!(_flags & EnvelopeVisible)) {
209                 gain_line->hide ();
210         } else {
211                 gain_line->show ();
212         }
213
214         gain_line->reset ();
215
216         set_y_position_and_height (0, trackview.height);
217
218         region_muted ();
219         region_sync_changed ();
220         region_resized (BoundsChanged);
221         set_waveview_data_src();
222         region_locked ();
223         envelope_active_changed ();
224         fade_in_active_changed ();
225         fade_out_active_changed ();
226
227         reset_width_dependent_items (_pixel_width);
228
229         fade_in_shape->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_event), fade_in_shape, this));
230         fade_in_handle->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
231         fade_out_shape->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_event), fade_out_shape, this));
232         fade_out_handle->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
233
234         set_colors ();
235
236         /* XXX sync mark drag? */
237 }
238
239 AudioRegionView::~AudioRegionView ()
240 {
241         in_destructor = true;
242
243         RegionViewGoingAway (this); /* EMIT_SIGNAL */
244
245         for (vector<GnomeCanvasWaveViewCache *>::iterator cache = wave_caches.begin(); cache != wave_caches.end() ; ++cache) {
246                 gnome_canvas_waveview_cache_destroy (*cache);
247         }
248
249         /* all waveviews etc will be destroyed when the group is destroyed */
250
251         if (gain_line) {
252                 delete gain_line;
253         }
254 }
255
256 boost::shared_ptr<ARDOUR::AudioRegion>
257 AudioRegionView::audio_region() const
258 {
259         // "Guaranteed" to succeed...
260         return boost::dynamic_pointer_cast<AudioRegion>(_region);
261 }
262
263 void
264 AudioRegionView::region_changed (Change what_changed)
265 {
266         ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionView::region_changed), what_changed));
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::set_y_position_and_height (double y, double h)
438 {
439         RegionView::set_y_position_and_height(y, h - 1);
440         
441         const uint32_t wcnt = waves.size();
442
443         _y_position = y;
444         _height = h;
445
446         for (uint32_t n = 0; n < wcnt; ++n) {
447                 double ht;
448
449                 if (h <= NAME_HIGHLIGHT_THRESH) {
450                         ht = ((_height - 2 * wcnt) / (double) wcnt);
451                 } else {
452                         ht = (((_height - 2 * wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
453                 }
454                 
455                 double const yoff = n * (ht + 1);
456                 
457                 waves[n]->property_height() = ht;
458                 waves[n]->property_y() = _y_position + yoff + 2;
459         }
460
461         if (gain_line) {
462                 if ((_height / wcnt) < NAME_HIGHLIGHT_SIZE) {
463                         gain_line->hide ();
464                 } else {
465                         if (_flags & EnvelopeVisible) {
466                                 gain_line->show ();
467                         }
468                 }
469                 gain_line->set_y_position_and_height ((uint32_t) _y_position, (uint32_t) rint (_height - NAME_HIGHLIGHT_SIZE));
470         }
471
472         setup_fade_handle_positions ();
473         manage_zero_line ();
474         reset_fade_shapes ();
475         
476         if (name_text) {
477                 name_text->raise_to_top();
478         }
479 }
480
481 void
482 AudioRegionView::setup_fade_handle_positions()
483 {
484         /* position of fade handle offset from the top of the region view */
485         double const handle_pos = 2;
486         /* height of fade handles */
487         double const handle_height = 5;
488
489         if (fade_in_handle) {
490                 fade_in_handle->property_y1() = _y_position + handle_pos;
491                 fade_in_handle->property_y2() = _y_position + handle_pos + handle_height;
492         }
493         
494         if (fade_out_handle) {
495                 fade_out_handle->property_y1() = _y_position + handle_pos;
496                 fade_out_handle->property_y2() = _y_position + handle_pos + handle_height;
497         }
498 }
499
500 void
501 AudioRegionView::manage_zero_line ()
502 {
503         if (!zero_line) {
504                 return;
505         }
506
507         if (_height >= 100) {
508                 double const wave_midpoint = _y_position + (_height - NAME_HIGHLIGHT_SIZE) / 2.0;
509                 zero_line->property_y1() = wave_midpoint;
510                 zero_line->property_y2() = wave_midpoint;
511                 zero_line->show();
512         } else {
513                 zero_line->hide();
514         }
515 }
516
517 void
518 AudioRegionView::reset_fade_shapes ()
519 {
520         reset_fade_in_shape ();
521         reset_fade_out_shape ();
522 }
523
524 void
525 AudioRegionView::reset_fade_in_shape ()
526 {
527         reset_fade_in_shape_width ((nframes_t) audio_region()->fade_in()->back()->when);
528 }
529         
530 void
531 AudioRegionView::reset_fade_in_shape_width (nframes_t width)
532 {
533         if (fade_in_handle == 0) {
534                 return;
535         }
536
537         /* smallest size for a fade is 64 frames */
538
539         width = std::max ((nframes_t) 64, width);
540
541         Points* points;
542         double pwidth = width / samples_per_unit;
543         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
544         double h; 
545         
546         if (_height < 5) {
547                 fade_in_shape->hide();
548                 fade_in_handle->hide();
549                 return;
550         }
551
552         double handle_center;
553         handle_center = pwidth;
554         
555         if (handle_center > 7.0) {
556                 handle_center -= 3.0;
557         } else {
558                 handle_center = 3.0;
559         }
560
561         fade_in_handle->property_x1() =  handle_center - 3.0;
562         fade_in_handle->property_x2() =  handle_center + 3.0;
563         
564         if (pwidth < 5) {
565                 fade_in_shape->hide();
566                 return;
567         }
568
569         fade_in_shape->show();
570
571         float curve[npoints];
572         audio_region()->fade_in()->curve().get_vector (0, audio_region()->fade_in()->back()->when, curve, npoints);
573
574         points = get_canvas_points ("fade in shape", npoints+3);
575
576         if (_height > NAME_HIGHLIGHT_THRESH) {
577                 h = _height - NAME_HIGHLIGHT_SIZE;
578         } else {
579                 h = _height;
580         }
581
582         /* points *MUST* be in anti-clockwise order */
583
584         uint32_t pi, pc;
585         double xdelta = pwidth/npoints;
586
587         for (pi = 0, pc = 0; pc < npoints; ++pc) {
588                 (*points)[pi].set_x(1 + (pc * xdelta));
589                 (*points)[pi++].set_y(_y_position + 2 + (h - (curve[pc] * h)));
590         }
591         
592         /* fold back */
593
594         (*points)[pi].set_x(pwidth);
595         (*points)[pi++].set_y(_y_position + 2);
596
597         (*points)[pi].set_x(1);
598         (*points)[pi++].set_y(_y_position + 2);
599
600         /* connect the dots ... */
601
602         (*points)[pi] = (*points)[0];
603         
604         fade_in_shape->property_points() = *points;
605         delete points;
606 }
607
608 void
609 AudioRegionView::reset_fade_out_shape ()
610 {
611         reset_fade_out_shape_width ((nframes_t) audio_region()->fade_out()->back()->when);
612 }
613
614 void
615 AudioRegionView::reset_fade_out_shape_width (nframes_t width)
616 {       
617         if (fade_out_handle == 0) {
618                 return;
619         }
620
621         /* smallest size for a fade is 64 frames */
622
623         width = std::max ((nframes_t) 64, width);
624
625         Points* points;
626         double pwidth = width / samples_per_unit;
627         uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
628         double h;
629
630         if (_height < 5) {
631                 fade_out_shape->hide();
632                 fade_out_handle->hide();
633                 return;
634         }
635
636         double handle_center;
637         handle_center = (_region->length() - width) / samples_per_unit;
638         
639         if (handle_center > 7.0) {
640                 handle_center -= 3.0;
641         } else {
642                 handle_center = 3.0;
643         }
644         
645         fade_out_handle->property_x1() =  handle_center - 3.0;
646         fade_out_handle->property_x2() =  handle_center + 3.0;
647
648         /* don't show shape if its too small */
649         
650         if (pwidth < 5) {
651                 fade_out_shape->hide();
652                 return;
653         } 
654         
655         fade_out_shape->show();
656
657         float curve[npoints];
658         audio_region()->fade_out()->curve().get_vector (0, audio_region()->fade_out()->back()->when, curve, npoints);
659
660         if (_height > NAME_HIGHLIGHT_THRESH) {
661                 h = _height - NAME_HIGHLIGHT_SIZE;
662         } else {
663                 h = _height;
664         }
665
666         /* points *MUST* be in anti-clockwise order */
667
668         points = get_canvas_points ("fade out shape", npoints+3);
669
670         uint32_t pi, pc;
671         double xdelta = pwidth/npoints;
672
673         for (pi = 0, pc = 0; pc < npoints; ++pc) {
674                 (*points)[pi].set_x(_pixel_width - 1 - pwidth + (pc*xdelta));
675                 (*points)[pi++].set_y(_y_position + 2 + (h - (curve[pc] * h)));
676         }
677         
678         /* fold back */
679
680         (*points)[pi].set_x(_pixel_width);
681         (*points)[pi++].set_y(_y_position + h);
682
683         (*points)[pi].set_x(_pixel_width);
684         (*points)[pi++].set_y(_y_position + 2);
685
686         /* connect the dots ... */
687
688         (*points)[pi] = (*points)[0];
689
690         fade_out_shape->property_points() = *points;
691         delete points;
692 }
693
694 void
695 AudioRegionView::set_samples_per_unit (gdouble spu)
696 {
697         RegionView::set_samples_per_unit (spu);
698
699         if (_flags & WaveformVisible) {
700                 for (uint32_t n=0; n < waves.size(); ++n) {
701                         waves[n]->property_samples_per_unit() = spu;
702                 }
703         }
704
705         if (gain_line) {
706                 gain_line->reset ();
707         }
708
709         reset_fade_shapes ();
710 }
711
712 void
713 AudioRegionView::set_amplitude_above_axis (gdouble spp)
714 {
715         for (uint32_t n=0; n < waves.size(); ++n) {
716                 waves[n]->property_amplitude_above_axis() = spp;
717         }
718 }
719
720 void
721 AudioRegionView::compute_colors (Gdk::Color& basic_color)
722 {
723         RegionView::compute_colors(basic_color);
724         
725         uint32_t r, g, b, a;
726
727         /* gain color computed in envelope_active_changed() */
728
729         UINT_TO_RGBA (fill_color, &r, &g, &b, &a);
730         fade_color = RGBA_TO_UINT(r,g,b,120);
731 }
732
733 void
734 AudioRegionView::set_colors ()
735 {
736         RegionView::set_colors();
737         
738         if (gain_line) {
739                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
740         }
741
742         for (uint32_t n=0; n < waves.size(); ++n) {
743                 if (_region->muted()) {
744                         waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
745                 } else {
746                         waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
747                 }
748
749                 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
750                 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
751         }
752 }
753
754 void
755 AudioRegionView::show_region_editor ()
756 {
757         if (editor == 0) {
758                 editor = new AudioRegionEditor (trackview.session(), audio_region(), *this);
759                 // GTK2FIX : how to ensure float without realizing
760                 // editor->realize ();
761                 // trackview.editor.ensure_float (*editor);
762         } 
763
764         editor->present ();
765         editor->show_all();
766 }
767
768 void
769 AudioRegionView::set_waveform_visible (bool yn)
770 {
771         if (((_flags & WaveformVisible) != yn)) {
772                 if (yn) {
773                         for (uint32_t n=0; n < waves.size(); ++n) {
774                                 /* make sure the zoom level is correct, since we don't update
775                                    this when waveforms are hidden.
776                                 */
777                                 waves[n]->property_samples_per_unit() = samples_per_unit;
778                                 waves[n]->show();
779                         }
780                         _flags |= WaveformVisible;
781                 } else {
782                         for (uint32_t n=0; n < waves.size(); ++n) {
783                                 waves[n]->hide();
784                         }
785                         _flags &= ~WaveformVisible;
786                 }
787                 store_flags ();
788         }
789 }
790
791 void
792 AudioRegionView::temporarily_hide_envelope ()
793 {
794         if (gain_line) {
795                 gain_line->hide ();
796         }
797 }
798
799 void
800 AudioRegionView::unhide_envelope ()
801 {
802         if (gain_line && (_flags & EnvelopeVisible)) {
803                 gain_line->show ();
804         }
805 }
806
807 void
808 AudioRegionView::set_envelope_visible (bool yn)
809 {
810         if (gain_line && ((_flags & EnvelopeVisible) != yn)) {
811                 if (yn) {
812                         gain_line->show ();
813                         _flags |= EnvelopeVisible;
814                 } else {
815                         gain_line->hide ();
816                         _flags &= ~EnvelopeVisible;
817                 }
818                 store_flags ();
819         }
820 }
821
822 void
823 AudioRegionView::create_waves ()
824 {
825         // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
826         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
827
828         if (!atv.get_diskstream()) {
829                 return;
830         }
831
832         ChanCount nchans = atv.get_diskstream()->n_channels();
833
834         // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
835         //              << " and channels = " << nchans.n_audio() << endl;
836         
837         /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
838         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
839                 tmp_waves.push_back (0);
840         }
841
842         for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
843                 
844                 if (n >= audio_region()->n_channels()) {
845                         break;
846                 }
847                 
848                 wave_caches.push_back (WaveView::create_cache ());
849
850                 // cerr << "\tchannel " << n << endl;
851
852                 if (wait_for_data) {
853                         if (audio_region()->audio_source(n)->peaks_ready (bind (mem_fun(*this, &AudioRegionView::peaks_ready_handler), n), data_ready_connection)) {
854                                 // cerr << "\tData is ready\n";
855                                 cerr << "\tData is ready\n";
856                                 create_one_wave (n, true);
857                         } else {
858                                 // cerr << "\tdata is not ready\n";
859                                 // we'll get a PeaksReady signal from the source in the future
860                                 // and will call create_one_wave(n) then.
861                         }
862                         
863                 } else {
864                         // cerr << "\tdon't delay, display today!\n";
865                         create_one_wave (n, true);
866                 }
867
868         }
869 }
870
871 void
872 AudioRegionView::create_one_wave (uint32_t which, bool direct)
873 {
874         //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
875         RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
876         uint32_t nchans = atv.get_diskstream()->n_channels().n_audio();
877         uint32_t n;
878         uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
879         gdouble ht;
880
881         if (trackview.height < NAME_HIGHLIGHT_SIZE) {
882                 ht = ((trackview.height) / (double) nchans);
883         } else {
884                 ht = ((trackview.height - NAME_HIGHLIGHT_SIZE) / (double) nchans);
885         }
886
887         gdouble yoff = which * ht;
888
889         WaveView *wave = new WaveView(*group);
890
891         wave->property_data_src() = (gpointer) _region.get();
892         wave->property_cache() =  wave_caches[which];
893         wave->property_cache_updater() = true;
894         wave->property_channel() =  which;
895         wave->property_length_function() = (gpointer) region_length_from_c;
896         wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
897         wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
898         wave->property_x() =  0.0;
899         wave->property_y() =  yoff;
900         wave->property_height() =  (double) ht;
901         wave->property_samples_per_unit() =  samples_per_unit;
902         wave->property_amplitude_above_axis() =  _amplitude_above_axis;
903         wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
904         wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
905         wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
906         wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
907         wave->property_region_start() = _region->start();
908         wave->property_rectified() = (bool) (_flags & WaveformRectified);
909         wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
910
911         if (!(_flags & WaveformVisible)) {
912                 wave->hide();
913         }
914
915         /* note: calling this function is serialized by the lock
916            held in the peak building thread that signals that
917            peaks are ready for use *or* by the fact that it is
918            called one by one from the GUI thread.
919         */
920
921         if (which < nchans) {
922                 tmp_waves[which] = wave;
923         } else {
924                 /* n-channel track, >n-channel source */
925         }
926         
927         /* see if we're all ready */
928         
929         for (n = 0; n < nchans; ++n) {
930                 if (tmp_waves[n] == 0) {
931                         break;
932                 }
933         }
934
935         if (n == nwaves && waves.empty()) {
936                 /* all waves are ready */
937                 tmp_waves.resize(nwaves);
938
939                 waves = tmp_waves;
940                 tmp_waves.clear ();
941
942                 /* all waves created, don't hook into peaks ready anymore */
943                 data_ready_connection.disconnect ();            
944
945                 if(0)
946                 if (!zero_line) {
947                         zero_line = new ArdourCanvas::SimpleLine (*group);
948                         zero_line->property_x1() = (gdouble) 1.0;
949                         zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
950                         zero_line->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
951                         manage_zero_line ();
952                 }
953         }
954 }
955
956 void
957 AudioRegionView::peaks_ready_handler (uint32_t which)
958 {
959         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &AudioRegionView::create_one_wave), which, false));
960         // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
961 }
962
963 void
964 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
965 {
966         if (gain_line == 0) {
967                 return;
968         }
969
970         double x, y;
971
972         /* don't create points that can't be seen */
973
974         set_envelope_visible (true);
975         
976         x = ev->button.x;
977         y = ev->button.y;
978
979         item->w2i (x, y);
980
981         nframes_t fx = trackview.editor.pixel_to_frame (x);
982
983         if (fx > _region->length()) {
984                 return;
985         }
986
987         /* compute vertical fractional position */
988
989         y = 1.0 - ((y - _y_position) / (_height - NAME_HIGHLIGHT_SIZE));
990         
991         /* map using gain line */
992
993         gain_line->view_to_model_y (y);
994
995         trackview.session().begin_reversible_command (_("add gain control point"));
996         XMLNode &before = audio_region()->envelope()->get_state();
997
998         if (!audio_region()->envelope_active()) {
999                 XMLNode &region_before = audio_region()->get_state();
1000                 audio_region()->set_envelope_active(true);
1001                 XMLNode &region_after = audio_region()->get_state();
1002                 trackview.session().add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
1003         }
1004
1005         audio_region()->envelope()->add (fx, y);
1006         
1007         XMLNode &after = audio_region()->envelope()->get_state();
1008         trackview.session().add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1009         trackview.session().commit_reversible_command ();
1010 }
1011
1012 void
1013 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
1014 {
1015         ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1016         audio_region()->envelope()->erase (cp->model());
1017 }
1018
1019 void
1020 AudioRegionView::store_flags()
1021 {
1022         XMLNode *node = new XMLNode ("GUI");
1023
1024         node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
1025         node->add_property ("envelope-visible", (_flags & EnvelopeVisible) ? "yes" : "no");
1026         node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1027         node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1028
1029         _region->add_extra_xml (*node);
1030 }
1031
1032 void
1033 AudioRegionView::set_flags (XMLNode* node)
1034 {
1035         XMLProperty *prop;
1036
1037         if ((prop = node->property ("waveform-visible")) != 0) {
1038                 if (prop->value() == "yes") {
1039                         _flags |= WaveformVisible;
1040                 }
1041         }
1042
1043         if ((prop = node->property ("envelope-visible")) != 0) {
1044                 if (prop->value() == "yes") {
1045                         _flags |= EnvelopeVisible;
1046                 }
1047         }
1048
1049         if ((prop = node->property ("waveform-rectified")) != 0) {
1050                 if (prop->value() == "yes") {
1051                         _flags |= WaveformRectified;
1052                 }
1053         }
1054
1055         if ((prop = node->property ("waveform-logscaled")) != 0) {
1056                 if (prop->value() == "yes") {
1057                         _flags |= WaveformLogScaled;
1058                 }
1059         }
1060 }
1061         
1062 void
1063 AudioRegionView::set_waveform_shape (WaveformShape shape)
1064 {
1065         bool yn;
1066
1067         /* this slightly odd approach is to leave the door open to 
1068            other "shapes" such as spectral displays, etc.
1069         */
1070
1071         switch (shape) {
1072         case Rectified:
1073                 yn = true;
1074                 break;
1075
1076         default:
1077                 yn = false;
1078                 break;
1079         }
1080
1081         if (yn != (bool) (_flags & WaveformRectified)) {
1082                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1083                         (*wave)->property_rectified() = yn;
1084                 }
1085
1086                 if (zero_line) {
1087                         if (yn) {
1088                                 zero_line->hide();
1089                         } else {
1090                                 zero_line->show();
1091                         }
1092                 }
1093
1094                 if (yn) {
1095                         _flags |= WaveformRectified;
1096                 } else {
1097                         _flags &= ~WaveformRectified;
1098                 }
1099                 store_flags ();
1100         }
1101 }
1102
1103 void
1104 AudioRegionView::set_waveform_scale (WaveformScale scale)
1105 {
1106         bool yn = (scale == LogWaveform);
1107
1108         if (yn != (bool) (_flags & WaveformLogScaled)) {
1109                 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1110                         (*wave)->property_logscaled() = yn;
1111                 }
1112
1113                 if (yn) {
1114                         _flags |= WaveformLogScaled;
1115                 } else {
1116                         _flags &= ~WaveformLogScaled;
1117                 }
1118                 store_flags ();
1119         }
1120 }
1121
1122
1123 GhostRegion*
1124 AudioRegionView::add_ghost (TimeAxisView& tv)
1125 {
1126         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1127         assert(rtv);
1128
1129         double unit_position = _region->position () / samples_per_unit;
1130         AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1131         uint32_t nchans;
1132         
1133         nchans = rtv->get_diskstream()->n_channels().n_audio();
1134
1135         for (uint32_t n = 0; n < nchans; ++n) {
1136                 
1137                 if (n >= audio_region()->n_channels()) {
1138                         break;
1139                 }
1140                 
1141                 WaveView *wave = new WaveView(*ghost->group);
1142
1143                 wave->property_data_src() = _region.get();
1144                 wave->property_cache() =  wave_caches[n];
1145                 wave->property_cache_updater() = false;
1146                 wave->property_channel() = n;
1147                 wave->property_length_function() = (gpointer)region_length_from_c;
1148                 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1149                 wave->property_peak_function() =  (gpointer) region_read_peaks_from_c;
1150                 wave->property_x() =  0.0;
1151                 wave->property_samples_per_unit() =  samples_per_unit;
1152                 wave->property_amplitude_above_axis() =  _amplitude_above_axis;
1153
1154                 wave->property_region_start() = _region->start();
1155
1156                 ghost->waves.push_back(wave);
1157         }
1158
1159         ghost->set_height ();
1160         ghost->set_duration (_region->length() / samples_per_unit);
1161         ghost->set_colors();
1162         ghosts.push_back (ghost);
1163
1164         ghost->GoingAway.connect (mem_fun(*this, &AudioRegionView::remove_ghost));
1165
1166         return ghost;
1167 }
1168
1169 void
1170 AudioRegionView::entered ()
1171 {
1172         if (gain_line && _flags & EnvelopeVisible) {
1173                 gain_line->show_all_control_points ();
1174         }
1175
1176         uint32_t r,g,b,a;
1177         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1178         a=255;
1179         
1180         if (fade_in_handle) {
1181                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1182                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1183         }
1184 }
1185
1186 void
1187 AudioRegionView::exited ()
1188 {
1189         if (gain_line) {
1190                 gain_line->hide_all_but_selected_control_points ();
1191         }
1192         
1193         uint32_t r,g,b,a;
1194         UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1195         a=0;
1196         
1197         if (fade_in_handle) {
1198                 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1199                 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1200         }
1201 }
1202
1203 void
1204 AudioRegionView::envelope_active_changed ()
1205 {
1206         if (gain_line) {
1207                 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1208         }
1209 }
1210
1211 void
1212 AudioRegionView::set_waveview_data_src()
1213 {
1214         AudioGhostRegion* agr;
1215         double unit_length= _region->length() / samples_per_unit;
1216
1217         for (uint32_t n = 0; n < waves.size(); ++n) {
1218                 // TODO: something else to let it know the channel
1219                 waves[n]->property_data_src() = _region.get();
1220         }
1221         
1222         for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1223                 
1224                 (*i)->set_duration (unit_length);
1225                 
1226                 if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
1227                         for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
1228                                 (*w)->property_data_src() = _region.get();
1229                         }
1230                 }
1231         }
1232
1233 }
1234
1235 void
1236 AudioRegionView::color_handler ()
1237 {
1238         //case cMutedWaveForm:
1239         //case cWaveForm:
1240         //case cWaveFormClip:
1241         //case cZeroLine:
1242         set_colors ();
1243
1244         //case cGainLineInactive:
1245         //case cGainLine:
1246         envelope_active_changed();
1247
1248 }
1249
1250 void
1251 AudioRegionView::set_frame_color ()
1252 {
1253         if (!frame) {
1254                 return;
1255         }
1256
1257         if (_region->opaque()) {
1258                 fill_opacity = 130;
1259         } else {
1260                 fill_opacity = 0;
1261         }
1262
1263         uint32_t r,g,b,a;
1264         
1265         if (_selected && should_show_selection) {
1266                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get(), &r, &g, &b, &a);
1267                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1268
1269                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1270                         if (_region->muted()) {
1271                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1272                         } else {
1273                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1274                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1275                         }
1276                 }
1277         } else {
1278                 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_FrameBase.get(), &r, &g, &b, &a);
1279                 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1280
1281                 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1282                         if (_region->muted()) {
1283                                 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1284                         } else {
1285                                 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1286                                 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1287                         }
1288                 }
1289         }
1290 }