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