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