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