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