fix import/embed with "sequence files" option
[ardour2.git] / gtk2_ardour / audio_region_view.cc
blob20310c4f4f21a45185641e38aea6454e313fe8ac
1 /*
2 Copyright (C) 2001-2006 Paul Davis
4 This program is free software; you can r>edistribute 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.
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.
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.
20 #include <cmath>
21 #include <cassert>
22 #include <algorithm>
24 #include <gtkmm.h>
26 #include <gtkmm2ext/gtk_ui.h>
28 #include <ardour/playlist.h>
29 #include <ardour/audioregion.h>
30 #include <ardour/audiosource.h>
31 #include <ardour/audio_diskstream.h>
32 #include <ardour/profile.h>
33 #include <pbd/memento_command.h>
34 #include <pbd/stacktrace.h>
36 #include "streamview.h"
37 #include "audio_region_view.h"
38 #include "audio_time_axis.h"
39 #include "simplerect.h"
40 #include "simpleline.h"
41 #include "waveview.h"
42 #include "public_editor.h"
43 #include "audio_region_editor.h"
44 #include "region_gain_line.h"
45 #include "ghostregion.h"
46 #include "audio_time_axis.h"
47 #include "utils.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
50 #include "ardour_ui.h"
52 #include "i18n.h"
54 #define MUTED_ALPHA 10
56 using namespace sigc;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Editing;
60 using namespace ArdourCanvas;
62 static const int32_t sync_mark_width = 9;
64 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
65 Gdk::Color& basic_color)
66 : RegionView (parent, tv, r, spu, basic_color)
67 , sync_mark(0)
68 , zero_line(0)
69 , fade_in_shape(0)
70 , fade_out_shape(0)
71 , fade_in_handle(0)
72 , fade_out_handle(0)
73 , gain_line(0)
74 , _amplitude_above_axis(1.0)
75 , _flags(0)
76 , fade_color(0)
81 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
82 Gdk::Color& basic_color, bool recording, TimeAxisViewItem::Visibility visibility)
83 : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
84 , sync_mark(0)
85 , zero_line(0)
86 , fade_in_shape(0)
87 , fade_out_shape(0)
88 , fade_in_handle(0)
89 , fade_out_handle(0)
90 , gain_line(0)
91 , _amplitude_above_axis(1.0)
92 , _flags(0)
93 , fade_color(0)
98 AudioRegionView::AudioRegionView (const AudioRegionView& other)
99 : RegionView (other)
100 , zero_line(0)
101 , fade_in_shape(0)
102 , fade_out_shape(0)
103 , fade_in_handle(0)
104 , fade_out_handle(0)
105 , gain_line(0)
106 , _amplitude_above_axis(1.0)
107 , _flags(0)
108 , fade_color(0)
111 Gdk::Color c;
112 int r,g,b,a;
114 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
115 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
117 init (c, true);
120 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
121 : RegionView (other, boost::shared_ptr<Region> (other_region))
122 , zero_line(0)
123 , fade_in_shape(0)
124 , fade_out_shape(0)
125 , fade_in_handle(0)
126 , fade_out_handle(0)
127 , gain_line(0)
128 , _amplitude_above_axis(1.0)
129 , _flags(0)
130 , fade_color(0)
133 Gdk::Color c;
134 int r,g,b,a;
136 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
137 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
139 init (c, true);
142 void
143 AudioRegionView::init (Gdk::Color& basic_color, bool wfd)
145 // FIXME: Some redundancy here with RegionView::init. Need to figure out
146 // where order is important and where it isn't...
148 RegionView::init (basic_color, wfd);
150 XMLNode *node;
152 _amplitude_above_axis = 1.0;
153 zero_line = 0;
154 _flags = 0;
156 if ((node = _region->extra_xml ("GUI")) != 0) {
157 set_flags (node);
158 } else {
159 _flags = WaveformVisible;
160 store_flags ();
163 if (trackview.editor.new_regionviews_display_gain()) {
164 _flags |= EnvelopeVisible;
167 compute_colors (basic_color);
169 create_waves ();
171 fade_in_shape = new ArdourCanvas::Polygon (*group);
172 fade_in_shape->property_fill_color_rgba() = fade_color;
173 fade_in_shape->set_data ("regionview", this);
175 fade_out_shape = new ArdourCanvas::Polygon (*group);
176 fade_out_shape->property_fill_color_rgba() = fade_color;
177 fade_out_shape->set_data ("regionview", this);
180 uint32_t r,g,b,a;
181 UINT_TO_RGBA(fill_color,&r,&g,&b,&a);
184 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
185 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
186 fade_in_handle->property_outline_pixels() = 0;
187 fade_in_handle->property_y1() = 2.0;
188 fade_in_handle->property_y2() = 7.0;
190 fade_in_handle->set_data ("regionview", this);
192 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
193 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,0);
194 fade_out_handle->property_outline_pixels() = 0;
195 fade_out_handle->property_y1() = 2.0;
196 fade_out_handle->property_y2() = 7.0;
198 fade_out_handle->set_data ("regionview", this);
201 if (!Config->get_show_region_fades()) {
202 set_fade_visibility (false);
205 string foo = _region->name();
206 foo += ':';
207 foo += "gain";
209 if (!Profile->get_sae()) {
210 gain_line = new AudioRegionGainLine (foo, trackview.session(), *this, *group, audio_region()->envelope());
212 if (!(_flags & EnvelopeVisible)) {
213 gain_line->hide ();
214 } else {
215 gain_line->show ();
218 gain_line->reset ();
221 set_height (trackview.current_height());
223 region_muted ();
224 region_sync_changed ();
225 region_resized (BoundsChanged);
226 set_waveview_data_src();
227 region_locked ();
228 envelope_active_changed ();
229 fade_in_active_changed ();
230 fade_out_active_changed ();
232 _region->StateChanged.connect (mem_fun(*this, &AudioRegionView::region_changed));
234 fade_in_shape->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_event), fade_in_shape, this));
235 fade_in_handle->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
236 fade_out_shape->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_event), fade_out_shape, this));
237 fade_out_handle->signal_event().connect (bind (mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
239 set_colors ();
241 /* XXX sync mark drag? */
244 AudioRegionView::~AudioRegionView ()
246 in_destructor = true;
248 RegionViewGoingAway (this); /* EMIT_SIGNAL */
250 for (vector<GnomeCanvasWaveViewCache *>::iterator cache = wave_caches.begin(); cache != wave_caches.end() ; ++cache) {
251 gnome_canvas_waveview_cache_destroy (*cache);
254 /* all waveviews etc will be destroyed when the group is destroyed */
256 if (gain_line) {
257 delete gain_line;
261 boost::shared_ptr<ARDOUR::AudioRegion>
262 AudioRegionView::audio_region() const
264 // "Guaranteed" to succeed...
265 return boost::dynamic_pointer_cast<AudioRegion>(_region);
268 void
269 AudioRegionView::region_changed (Change what_changed)
271 ENSURE_GUI_THREAD (bind (mem_fun(*this, &AudioRegionView::region_changed), what_changed));
272 //cerr << "AudioRegionView::region_changed() called" << endl;
274 RegionView::region_changed(what_changed);
276 if (what_changed & AudioRegion::ScaleAmplitudeChanged) {
277 region_scale_amplitude_changed ();
279 if (what_changed & AudioRegion::FadeInChanged) {
280 fade_in_changed ();
282 if (what_changed & AudioRegion::FadeOutChanged) {
283 fade_out_changed ();
285 if (what_changed & AudioRegion::FadeInActiveChanged) {
286 fade_in_active_changed ();
288 if (what_changed & AudioRegion::FadeOutActiveChanged) {
289 fade_out_active_changed ();
291 if (what_changed & AudioRegion::EnvelopeActiveChanged) {
292 envelope_active_changed ();
296 void
297 AudioRegionView::fade_in_changed ()
299 reset_fade_in_shape ();
302 void
303 AudioRegionView::fade_out_changed ()
305 reset_fade_out_shape ();
307 void
308 AudioRegionView::fade_in_active_changed ()
310 uint32_t r,g,b,a;
311 uint32_t col;
312 UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
314 if (audio_region()->fade_in_active()) {
315 col = RGBA_TO_UINT(r,g,b,120);
316 fade_in_shape->property_fill_color_rgba() = col;
317 fade_in_shape->property_width_pixels() = 0;
318 fade_in_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,0);
319 } else {
320 col = RGBA_TO_UINT(r,g,b,0);
321 fade_in_shape->property_fill_color_rgba() = col;
322 fade_in_shape->property_width_pixels() = 1;
323 fade_in_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,255);
327 void
328 AudioRegionView::fade_out_active_changed ()
330 uint32_t r,g,b,a;
331 uint32_t col;
332 UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
334 if (audio_region()->fade_out_active()) {
335 col = RGBA_TO_UINT(r,g,b,120);
336 fade_out_shape->property_fill_color_rgba() = col;
337 fade_out_shape->property_width_pixels() = 0;
338 fade_out_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,0);
339 } else {
340 col = RGBA_TO_UINT(r,g,b,0);
341 fade_out_shape->property_fill_color_rgba() = col;
342 fade_out_shape->property_width_pixels() = 1;
343 fade_out_shape->property_outline_color_rgba() = RGBA_TO_UINT(r,g,b,255);
348 void
349 AudioRegionView::region_scale_amplitude_changed ()
351 ENSURE_GUI_THREAD (mem_fun(*this, &AudioRegionView::region_scale_amplitude_changed));
353 for (uint32_t n = 0; n < waves.size(); ++n) {
354 // force a reload of the cache
355 waves[n]->property_data_src() = _region.get();
359 void
360 AudioRegionView::region_resized (Change what_changed)
362 RegionView::region_resized(what_changed);
364 if (what_changed & Change (StartChanged|LengthChanged)) {
366 for (uint32_t n = 0; n < waves.size(); ++n) {
367 waves[n]->property_region_start() = _region->start();
370 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
372 for (vector<WaveView*>::iterator w = (*i)->waves.begin(); w != (*i)->waves.end(); ++w) {
373 (*w)->property_region_start() = _region->start();
379 void
380 AudioRegionView::reset_width_dependent_items (double pixel_width)
382 RegionView::reset_width_dependent_items(pixel_width);
383 assert(_pixel_width == pixel_width);
385 if (zero_line) {
386 zero_line->property_x2() = pixel_width - 1.0;
389 if (fade_in_handle) {
390 if (pixel_width <= 6.0) {
391 fade_in_handle->hide();
392 fade_out_handle->hide();
393 } else {
394 if (_height < 5.0) {
395 fade_in_handle->hide();
396 fade_out_handle->hide();
397 } else {
398 if (Config->get_show_region_fades()) {
399 fade_in_handle->show();
400 fade_out_handle->show();
406 reset_fade_shapes ();
409 void
410 AudioRegionView::region_muted ()
412 RegionView::region_muted();
414 for (uint32_t n=0; n < waves.size(); ++n) {
415 if (_region->muted()) {
416 waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
417 } else {
418 waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
424 void
425 AudioRegionView::set_height (gdouble height)
427 RegionView::set_height (height);
429 uint32_t wcnt = waves.size();
431 // FIXME: ick
432 height -= 2;
433 TimeAxisViewItem::set_height (height);
435 for (uint32_t n=0; n < wcnt; ++n) {
436 gdouble ht;
438 if ((height) < NAME_HIGHLIGHT_THRESH) {
439 ht = ((height-2*wcnt) / (double) wcnt);
440 } else {
441 ht = (((height-2*wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
444 gdouble yoff = n * (ht+1);
446 waves[n]->property_height() = ht;
447 waves[n]->property_y() = yoff + 2;
450 if (gain_line) {
451 if ((height/wcnt) < NAME_HIGHLIGHT_THRESH) {
452 gain_line->hide ();
453 } else {
454 if (_flags & EnvelopeVisible) {
455 gain_line->show ();
458 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE - 2));
461 manage_zero_line ();
462 reset_fade_shapes ();
466 void
467 AudioRegionView::manage_zero_line ()
469 if (!zero_line) {
470 return;
473 if (_height >= 100) {
474 gdouble wave_midpoint = (_height - NAME_HIGHLIGHT_SIZE) / 2.0;
475 zero_line->property_y1() = wave_midpoint;
476 zero_line->property_y2() = wave_midpoint;
477 zero_line->show();
478 } else {
479 zero_line->hide();
483 void
484 AudioRegionView::reset_fade_shapes ()
486 reset_fade_in_shape ();
487 reset_fade_out_shape ();
490 void
491 AudioRegionView::reset_fade_in_shape ()
493 reset_fade_in_shape_width ((nframes_t) audio_region()->fade_in().back()->when);
496 void
497 AudioRegionView::reset_fade_in_shape_width (nframes_t width)
499 if (fade_in_handle == 0) {
500 return;
503 /* smallest size for a fade is 64 frames */
505 width = std::max ((nframes_t) 64, width);
507 Points* points;
508 double pwidth = width / samples_per_unit;
509 uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
510 double h;
512 if (_height < 5) {
513 fade_in_shape->hide();
514 fade_in_handle->hide();
515 return;
518 double handle_center;
519 handle_center = pwidth;
521 if (handle_center > 7.0) {
522 handle_center -= 3.0;
523 } else {
524 handle_center = 3.0;
527 fade_in_handle->property_x1() = handle_center - 3.0;
528 fade_in_handle->property_x2() = handle_center + 3.0;
530 if (pwidth < 5) {
531 fade_in_shape->hide();
532 return;
535 if (Config->get_show_region_fades()) {
536 fade_in_shape->show();
539 float curve[npoints];
540 audio_region()->fade_in().get_vector (0, audio_region()->fade_in().back()->when, curve, npoints);
542 points = get_canvas_points ("fade in shape", npoints+3);
544 if (_height >= NAME_HIGHLIGHT_THRESH) {
545 h = _height - NAME_HIGHLIGHT_SIZE;
546 } else {
547 h = _height;
550 /* points *MUST* be in anti-clockwise order */
552 uint32_t pi, pc;
553 double xdelta = pwidth/npoints;
555 for (pi = 0, pc = 0; pc < npoints; ++pc) {
556 (*points)[pi].set_x(1 + (pc * xdelta));
557 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
560 /* fold back */
562 (*points)[pi].set_x(pwidth);
563 (*points)[pi++].set_y(2);
565 (*points)[pi].set_x(1);
566 (*points)[pi++].set_y(2);
568 /* connect the dots ... */
570 (*points)[pi] = (*points)[0];
572 fade_in_shape->property_points() = *points;
573 delete points;
576 void
577 AudioRegionView::reset_fade_out_shape ()
579 reset_fade_out_shape_width ((nframes_t) audio_region()->fade_out().back()->when);
582 void
583 AudioRegionView::reset_fade_out_shape_width (nframes_t width)
585 if (fade_out_handle == 0) {
586 return;
589 /* smallest size for a fade is 64 frames */
591 width = std::max ((nframes_t) 64, width);
593 Points* points;
594 double pwidth = width / samples_per_unit;
595 uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
596 double h;
598 if (_height < 5) {
599 fade_out_shape->hide();
600 fade_out_handle->hide();
601 return;
604 double handle_center;
605 handle_center = (_region->length() - width) / samples_per_unit;
607 if (handle_center > 7.0) {
608 handle_center -= 3.0;
609 } else {
610 handle_center = 3.0;
613 fade_out_handle->property_x1() = handle_center - 3.0;
614 fade_out_handle->property_x2() = handle_center + 3.0;
616 /* don't show shape if its too small */
618 if (pwidth < 5) {
619 fade_out_shape->hide();
620 return;
623 if (Config->get_show_region_fades()) {
624 fade_out_shape->show();
627 float curve[npoints];
628 audio_region()->fade_out().get_vector (0, audio_region()->fade_out().back()->when, curve, npoints);
630 if (_height >= NAME_HIGHLIGHT_THRESH) {
631 h = _height - NAME_HIGHLIGHT_SIZE;
632 } else {
633 h = _height;
636 /* points *MUST* be in anti-clockwise order */
638 points = get_canvas_points ("fade out shape", npoints+3);
640 uint32_t pi, pc;
641 double xdelta = pwidth/npoints;
643 for (pi = 0, pc = 0; pc < npoints; ++pc) {
644 (*points)[pi].set_x(_pixel_width - 1 - pwidth + (pc*xdelta));
645 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
648 /* fold back */
650 (*points)[pi].set_x(_pixel_width);
651 (*points)[pi++].set_y(h);
653 (*points)[pi].set_x(_pixel_width);
654 (*points)[pi++].set_y(2);
656 /* connect the dots ... */
658 (*points)[pi] = (*points)[0];
660 fade_out_shape->property_points() = *points;
661 delete points;
664 void
665 AudioRegionView::set_samples_per_unit (gdouble spu)
667 RegionView::set_samples_per_unit (spu);
669 if (_flags & WaveformVisible) {
670 for (uint32_t n=0; n < waves.size(); ++n) {
671 waves[n]->property_samples_per_unit() = spu;
675 if (gain_line) {
676 gain_line->reset ();
679 reset_fade_shapes ();
682 void
683 AudioRegionView::set_amplitude_above_axis (gdouble spp)
685 for (uint32_t n=0; n < waves.size(); ++n) {
686 waves[n]->property_amplitude_above_axis() = spp;
690 void
691 AudioRegionView::compute_colors (Gdk::Color& basic_color)
693 RegionView::compute_colors(basic_color);
695 uint32_t r, g, b, a;
697 /* gain color computed in envelope_active_changed() */
699 UINT_TO_RGBA (fill_color, &r, &g, &b, &a);
700 fade_color = RGBA_TO_UINT(r,g,b,120);
703 void
704 AudioRegionView::set_colors ()
706 RegionView::set_colors();
708 if (gain_line) {
709 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
712 for (uint32_t n=0; n < waves.size(); ++n) {
713 if (_region->muted()) {
714 waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
715 } else {
716 waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
719 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
720 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
724 void
725 AudioRegionView::show_region_editor ()
727 if (editor == 0) {
728 editor = new AudioRegionEditor (trackview.session(), audio_region(), *this);
729 // GTK2FIX : how to ensure float without realizing
730 // editor->realize ();
731 // trackview.editor.ensure_float (*editor);
734 editor->present ();
735 editor->show_all();
738 void
739 AudioRegionView::set_waveform_visible (bool yn)
741 if (((_flags & WaveformVisible) != yn)) {
742 if (yn) {
743 for (uint32_t n=0; n < waves.size(); ++n) {
744 /* make sure the zoom level is correct, since we don't update
745 this when waveforms are hidden.
747 waves[n]->property_samples_per_unit() = samples_per_unit;
748 waves[n]->show();
750 _flags |= WaveformVisible;
751 } else {
752 for (uint32_t n=0; n < waves.size(); ++n) {
753 waves[n]->hide();
755 _flags &= ~WaveformVisible;
757 store_flags ();
761 void
762 AudioRegionView::temporarily_hide_envelope ()
764 if (gain_line) {
765 gain_line->hide ();
769 void
770 AudioRegionView::unhide_envelope ()
772 if (gain_line && (_flags & EnvelopeVisible)) {
773 gain_line->show ();
777 void
778 AudioRegionView::set_envelope_visible (bool yn)
780 if (gain_line && ((_flags & EnvelopeVisible) != yn)) {
781 if (yn) {
782 gain_line->show ();
783 _flags |= EnvelopeVisible;
784 } else {
785 gain_line->hide ();
786 _flags &= ~EnvelopeVisible;
788 store_flags ();
792 void
793 AudioRegionView::create_waves ()
795 // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
796 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
798 if (!atv.get_diskstream()) {
799 return;
802 uint32_t nchans = atv.get_diskstream()->n_channels();
804 // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data << " and channels = " << nchans << endl;
806 /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
807 for (uint32_t n = 0; n < nchans; ++n) {
808 tmp_waves.push_back (0);
811 for (uint32_t n = 0; n < nchans; ++n) {
813 if (n >= audio_region()->n_channels()) {
814 break;
817 wave_caches.push_back (WaveView::create_cache ());
819 // cerr << "\tchannel " << n << endl;
821 if (wait_for_data) {
822 if (audio_region()->source(n)->peaks_ready (bind (mem_fun(*this, &AudioRegionView::peaks_ready_handler), n), data_ready_connection)) {
823 // cerr << "\tData is ready\n";
824 create_one_wave (n, true);
825 } else {
826 // cerr << "\tdata is not ready\n";
827 // we'll get a PeaksReady signal from the source in the future
828 // and will call create_one_wave(n) then.
831 } else {
832 // cerr << "\tdon't delay, display today!\n";
833 create_one_wave (n, true);
839 void
840 AudioRegionView::create_one_wave (uint32_t which, bool direct)
842 //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
843 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
844 uint32_t nchans = atv.get_diskstream()->n_channels();
845 uint32_t n;
846 uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
847 gdouble ht;
849 if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
850 ht = ((trackview.current_height()) / (double) nchans);
851 } else {
852 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
855 gdouble yoff = which * ht;
857 WaveView *wave = new WaveView(*group);
859 wave->property_data_src() = (gpointer) _region.get();
860 wave->property_cache() = wave_caches[which];
861 wave->property_cache_updater() = true;
862 wave->property_channel() = which;
863 wave->property_length_function() = (gpointer) region_length_from_c;
864 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
865 wave->property_peak_function() = (gpointer) region_read_peaks_from_c;
866 wave->property_x() = 0.0;
867 wave->property_y() = yoff;
868 wave->property_height() = (double) ht;
869 wave->property_samples_per_unit() = samples_per_unit;
870 wave->property_amplitude_above_axis() = _amplitude_above_axis;
872 if (_recregion) {
873 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
874 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
875 } else {
876 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
877 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
880 wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
881 wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
882 wave->property_region_start() = _region->start();
883 wave->property_rectified() = (bool) (_flags & WaveformRectified);
884 wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
886 if (!(_flags & WaveformVisible)) {
887 wave->hide();
890 /* note: calling this function is serialized by the lock
891 held in the peak building thread that signals that
892 peaks are ready for use *or* by the fact that it is
893 called one by one from the GUI thread.
896 if (which < nchans) {
897 tmp_waves[which] = wave;
898 } else {
899 /* n-channel track, >n-channel source */
902 /* see if we're all ready */
904 for (n = 0; n < nchans; ++n) {
905 if (tmp_waves[n] == 0) {
906 break;
910 if (n == nwaves && waves.empty()) {
911 /* all waves are ready */
912 tmp_waves.resize(nwaves);
914 waves = tmp_waves;
915 tmp_waves.clear ();
917 /* all waves created, don't hook into peaks ready anymore */
918 data_ready_connection.disconnect ();
920 if (!zero_line) {
921 zero_line = new ArdourCanvas::SimpleLine (*group);
922 zero_line->property_x1() = (gdouble) 1.0;
923 zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
924 zero_line->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
925 manage_zero_line ();
930 void
931 AudioRegionView::peaks_ready_handler (uint32_t which)
933 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &AudioRegionView::create_one_wave), which, false));
934 // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
937 void
938 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
940 if (gain_line == 0) {
941 return;
944 double x, y;
946 /* don't create points that can't be seen */
948 set_envelope_visible (true);
950 x = ev->button.x;
951 y = ev->button.y;
953 item->w2i (x, y);
955 nframes_t fx = trackview.editor.pixel_to_frame (x);
957 if (fx > _region->length()) {
958 return;
961 /* compute vertical fractional position */
963 y = 1.0 - (y / (trackview.current_height() - NAME_HIGHLIGHT_SIZE));
965 /* map using gain line */
967 gain_line->view_to_model_y (y);
969 trackview.session().begin_reversible_command (_("add gain control point"));
970 XMLNode &before = audio_region()->envelope().get_state();
972 if (!audio_region()->envelope_active()) {
973 XMLNode &region_before = audio_region()->get_state();
974 audio_region()->set_envelope_active(true);
975 XMLNode &region_after = audio_region()->get_state();
976 trackview.session().add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
979 audio_region()->envelope().add (fx, y);
981 XMLNode &after = audio_region()->envelope().get_state();
982 trackview.session().add_command (new MementoCommand<Curve>(audio_region()->envelope(), &before, &after));
983 trackview.session().commit_reversible_command ();
986 void
987 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
989 ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
990 audio_region()->envelope().erase (cp->model);
993 void
994 AudioRegionView::store_flags()
996 XMLNode *node = new XMLNode ("GUI");
998 node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
999 node->add_property ("envelope-visible", (_flags & EnvelopeVisible) ? "yes" : "no");
1000 node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1001 node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1003 _region->add_extra_xml (*node);
1006 void
1007 AudioRegionView::set_flags (XMLNode* node)
1009 XMLProperty *prop;
1011 if ((prop = node->property ("waveform-visible")) != 0) {
1012 if (string_is_affirmative (prop->value())) {
1013 _flags |= WaveformVisible;
1017 if ((prop = node->property ("envelope-visible")) != 0) {
1018 if (string_is_affirmative (prop->value())) {
1019 _flags |= EnvelopeVisible;
1023 if ((prop = node->property ("waveform-rectified")) != 0) {
1024 if (string_is_affirmative (prop->value())) {
1025 _flags |= WaveformRectified;
1029 if ((prop = node->property ("waveform-logscaled")) != 0) {
1030 if (string_is_affirmative (prop->value())) {
1031 _flags |= WaveformLogScaled;
1036 void
1037 AudioRegionView::set_waveform_shape (WaveformShape shape)
1039 bool yn;
1041 /* this slightly odd approach is to leave the door open to
1042 other "shapes" such as spectral displays, etc.
1045 switch (shape) {
1046 case Rectified:
1047 yn = true;
1048 break;
1050 default:
1051 yn = false;
1052 break;
1055 if (yn != (bool) (_flags & WaveformRectified)) {
1056 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1057 (*wave)->property_rectified() = yn;
1060 if (zero_line) {
1061 if (yn) {
1062 zero_line->hide();
1063 } else {
1064 zero_line->show();
1068 if (yn) {
1069 _flags |= WaveformRectified;
1070 } else {
1071 _flags &= ~WaveformRectified;
1073 store_flags ();
1077 void
1078 AudioRegionView::set_waveform_scale (WaveformScale scale)
1080 bool yn = (scale == LogWaveform);
1082 if (yn != (bool) (_flags & WaveformLogScaled)) {
1083 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1084 (*wave)->property_logscaled() = yn;
1087 if (yn) {
1088 _flags |= WaveformLogScaled;
1089 } else {
1090 _flags &= ~WaveformLogScaled;
1092 store_flags ();
1097 GhostRegion*
1098 AudioRegionView::add_ghost (AutomationTimeAxisView& atv)
1100 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1101 assert(rtv);
1103 double unit_position = _region->position () / samples_per_unit;
1104 GhostRegion* ghost = new GhostRegion (atv, unit_position);
1105 uint32_t nchans;
1107 nchans = rtv->get_diskstream()->n_channels();
1109 for (uint32_t n = 0; n < nchans; ++n) {
1111 if (n >= audio_region()->n_channels()) {
1112 break;
1115 WaveView *wave = new WaveView(*ghost->group);
1117 wave->property_data_src() = _region.get();
1118 wave->property_cache() = wave_caches[n];
1119 wave->property_cache_updater() = false;
1120 wave->property_channel() = n;
1121 wave->property_length_function() = (gpointer)region_length_from_c;
1122 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1123 wave->property_peak_function() = (gpointer) region_read_peaks_from_c;
1124 wave->property_x() = 0.0;
1125 wave->property_samples_per_unit() = samples_per_unit;
1126 wave->property_amplitude_above_axis() = _amplitude_above_axis;
1127 wave->property_wave_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWave.get();
1128 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWave.get();
1129 wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_GhostTrackWaveClip.get();
1130 wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_GhostTrackZeroLine.get();
1131 wave->property_region_start() = _region->start();
1133 ghost->waves.push_back(wave);
1136 ghost->set_height ();
1137 ghost->set_duration (_region->length() / samples_per_unit);
1138 ghosts.push_back (ghost);
1140 ghost->GoingAway.connect (mem_fun(*this, &AudioRegionView::remove_ghost));
1142 return ghost;
1145 void
1146 AudioRegionView::entered ()
1148 if (gain_line && _flags & EnvelopeVisible) {
1149 gain_line->show_all_control_points ();
1152 uint32_t r,g,b,a;
1153 UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1154 a=255;
1156 if (fade_in_handle) {
1157 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1158 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1162 void
1163 AudioRegionView::exited ()
1165 if (gain_line) {
1166 gain_line->hide_all_but_selected_control_points ();
1169 uint32_t r,g,b,a;
1170 UINT_TO_RGBA(fade_color,&r,&g,&b,&a);
1171 a=0;
1173 if (fade_in_handle) {
1174 fade_in_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1175 fade_out_handle->property_fill_color_rgba() = RGBA_TO_UINT(r,g,b,a);
1179 void
1180 AudioRegionView::envelope_active_changed ()
1182 if (gain_line) {
1183 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1187 void
1188 AudioRegionView::set_waveview_data_src()
1191 double unit_length= _region->length() / samples_per_unit;
1193 for (uint32_t n = 0; n < waves.size(); ++n) {
1194 // TODO: something else to let it know the channel
1195 waves[n]->property_data_src() = _region.get();
1198 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1200 (*i)->set_duration (unit_length);
1202 for (vector<WaveView*>::iterator w = (*i)->waves.begin(); w != (*i)->waves.end(); ++w) {
1203 (*w)->property_data_src() = _region.get();
1209 void
1210 AudioRegionView::color_handler ()
1212 set_colors ();
1213 envelope_active_changed();
1216 void
1217 AudioRegionView::set_frame_color ()
1219 if (!frame) {
1220 return;
1223 if (_region->opaque()) {
1224 fill_opacity = 130;
1225 } else {
1226 fill_opacity = 0;
1229 uint32_t r,g,b,a;
1231 if (_selected && should_show_selection) {
1232 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_SelectedFrameBase.get(), &r, &g, &b, &a);
1233 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1235 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1236 if (_region->muted()) {
1237 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1238 } else {
1239 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1240 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1243 } else {
1244 if (_recregion) {
1245 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_RecordingRect.get(), &r, &g, &b, &a);
1246 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, a);
1248 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1249 if (_region->muted()) {
1250 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA);
1251 } else {
1252 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
1253 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
1256 } else {
1257 UINT_TO_RGBA(ARDOUR_UI::config()->canvasvar_FrameBase.get(), &r, &g, &b, &a);
1258 frame->property_fill_color_rgba() = RGBA_TO_UINT(r, g, b, fill_opacity ? fill_opacity : a);
1260 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1261 if (_region->muted()) {
1262 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1263 } else {
1264 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1265 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1272 void
1273 AudioRegionView::set_fade_visibility (bool yn)
1275 if (yn) {
1276 if (fade_in_shape) {
1277 fade_in_shape->show();
1279 if (fade_out_shape) {
1280 fade_out_shape->show ();
1282 if (fade_in_handle) {
1283 fade_in_handle->show ();
1285 if (fade_out_handle) {
1286 fade_out_handle->show ();
1288 } else {
1289 if (fade_in_shape) {
1290 fade_in_shape->hide();
1292 if (fade_out_shape) {
1293 fade_out_shape->hide ();
1295 if (fade_in_handle) {
1296 fade_in_handle->hide ();
1298 if (fade_out_handle) {
1299 fade_out_handle->hide ();