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