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