sanitycheck should be looking for SCHED_FIFO
[ardour2.git] / gtk2_ardour / audio_region_view.cc
blobb72d95ca158e0fd47b9ca613326c70326ab8d2f3
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.
19 #include <cmath>
20 #include <cassert>
21 #include <algorithm>
23 #include <gtkmm.h>
25 #include <gtkmm2ext/gtk_ui.h>
27 #include "ardour/playlist.h"
28 #include "ardour/audioregion.h"
29 #include "ardour/audiosource.h"
30 #include "ardour/profile.h"
31 #include "ardour/session.h"
33 #include "pbd/memento_command.h"
34 #include "pbd/stacktrace.h"
36 #include "evoral/Curve.hpp"
38 #include "streamview.h"
39 #include "audio_region_view.h"
40 #include "audio_time_axis.h"
41 #include "simplerect.h"
42 #include "simpleline.h"
43 #include "waveview.h"
44 #include "public_editor.h"
45 #include "audio_region_editor.h"
46 #include "region_gain_line.h"
47 #include "control_point.h"
48 #include "ghostregion.h"
49 #include "audio_time_axis.h"
50 #include "utils.h"
51 #include "rgb_macros.h"
52 #include "gui_thread.h"
53 #include "ardour_ui.h"
55 #include "i18n.h"
57 #define MUTED_ALPHA 10
59 using namespace std;
60 using namespace ARDOUR;
61 using namespace PBD;
62 using namespace Editing;
63 using namespace ArdourCanvas;
65 static const int32_t sync_mark_width = 9;
67 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
68 Gdk::Color const & basic_color)
69 : RegionView (parent, tv, r, spu, basic_color)
70 , sync_mark(0)
71 , zero_line(0)
72 , fade_in_shape(0)
73 , fade_out_shape(0)
74 , fade_in_handle(0)
75 , fade_out_handle(0)
76 , fade_position_line(0)
77 , gain_line(0)
78 , _amplitude_above_axis(1.0)
79 , _flags(0)
80 , fade_color(0)
85 AudioRegionView::AudioRegionView (ArdourCanvas::Group *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
86 Gdk::Color const & basic_color, bool recording, TimeAxisViewItem::Visibility visibility)
87 : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
88 , sync_mark(0)
89 , zero_line(0)
90 , fade_in_shape(0)
91 , fade_out_shape(0)
92 , fade_in_handle(0)
93 , fade_out_handle(0)
94 , fade_position_line(0)
95 , gain_line(0)
96 , _amplitude_above_axis(1.0)
97 , _flags(0)
98 , fade_color(0)
103 AudioRegionView::AudioRegionView (const AudioRegionView& other)
104 : sigc::trackable(other)
105 , RegionView (other)
106 , zero_line(0)
107 , fade_in_shape(0)
108 , fade_out_shape(0)
109 , fade_in_handle(0)
110 , fade_out_handle(0)
111 , fade_position_line(0)
112 , gain_line(0)
113 , _amplitude_above_axis(1.0)
114 , _flags(0)
115 , fade_color(0)
117 Gdk::Color c;
118 int r,g,b,a;
120 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
121 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
123 init (c, false);
126 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
127 : RegionView (other, boost::shared_ptr<Region> (other_region))
128 , zero_line(0)
129 , fade_in_shape(0)
130 , fade_out_shape(0)
131 , fade_in_handle(0)
132 , fade_out_handle(0)
133 , fade_position_line(0)
134 , gain_line(0)
135 , _amplitude_above_axis(1.0)
136 , _flags(0)
137 , fade_color(0)
139 Gdk::Color c;
140 int r,g,b,a;
142 UINT_TO_RGBA (other.fill_color, &r, &g, &b, &a);
143 c.set_rgb_p (r/255.0, g/255.0, b/255.0);
145 init (c, true);
148 void
149 AudioRegionView::init (Gdk::Color const & basic_color, bool wfd)
151 // FIXME: Some redundancy here with RegionView::init. Need to figure out
152 // where order is important and where it isn't...
154 RegionView::init (basic_color, wfd);
156 XMLNode *node;
158 _amplitude_above_axis = 1.0;
159 zero_line = 0;
160 _flags = 0;
162 if ((node = _region->extra_xml ("GUI")) != 0) {
163 set_flags (node);
164 } else {
165 _flags = WaveformVisible;
166 store_flags ();
169 /* make envelope visible if it has anything interesting in it */
170 boost::shared_ptr<AutomationList> env = audio_region()->envelope ();
171 if (env->size() > 2 || (env->size() == 2 && env->front()->value != env->back()->value)) {
172 _flags |= EnvelopeVisible;
175 compute_colors (basic_color);
177 create_waves ();
179 fade_in_shape = new ArdourCanvas::Polygon (*group);
180 fade_in_shape->property_fill_color_rgba() = fade_color;
181 fade_in_shape->set_data ("regionview", this);
183 fade_out_shape = new ArdourCanvas::Polygon (*group);
184 fade_out_shape->property_fill_color_rgba() = fade_color;
185 fade_out_shape->set_data ("regionview", this);
187 if (!_recregion) {
188 fade_in_handle = new ArdourCanvas::SimpleRect (*group);
189 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fill_color, 0);
190 fade_in_handle->property_outline_pixels() = 0;
192 fade_in_handle->set_data ("regionview", this);
194 fade_out_handle = new ArdourCanvas::SimpleRect (*group);
195 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fill_color, 0);
196 fade_out_handle->property_outline_pixels() = 0;
198 fade_out_handle->set_data ("regionview", this);
200 fade_position_line = new ArdourCanvas::SimpleLine (*group);
201 fade_position_line->property_color_rgba() = 0xBBBBBBAA;
202 fade_position_line->property_y1() = 7;
203 fade_position_line->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
205 fade_position_line->hide();
208 setup_fade_handle_positions ();
210 if (!trackview.session()->config.get_show_region_fades()) {
211 set_fade_visibility (false);
214 const string line_name = _region->name() + ":gain";
216 if (!Profile->get_sae()) {
217 gain_line = new AudioRegionGainLine (line_name, *this, *group, audio_region()->envelope());
220 if (!(_flags & EnvelopeVisible)) {
221 gain_line->hide ();
222 } else {
223 gain_line->show ();
226 gain_line->reset ();
228 set_height (trackview.current_height());
230 region_muted ();
231 region_sync_changed ();
233 region_resized (ARDOUR::bounds_change);
234 set_waveview_data_src();
235 region_locked ();
236 envelope_active_changed ();
237 fade_in_active_changed ();
238 fade_out_active_changed ();
240 reset_width_dependent_items (_pixel_width);
242 fade_in_shape->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_event), fade_in_shape, this));
243 if (fade_in_handle) {
244 fade_in_handle->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this));
247 fade_out_shape->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_event), fade_out_shape, this));
249 if (fade_out_handle) {
250 fade_out_handle->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this));
253 set_colors ();
255 /* XXX sync mark drag? */
258 AudioRegionView::~AudioRegionView ()
260 in_destructor = true;
262 RegionViewGoingAway (this); /* EMIT_SIGNAL */
264 for (vector<GnomeCanvasWaveViewCache *>::iterator cache = wave_caches.begin(); cache != wave_caches.end() ; ++cache) {
265 gnome_canvas_waveview_cache_destroy (*cache);
268 for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
269 delete *i;
272 for (list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator i = feature_lines.begin(); i != feature_lines.end(); ++i) {
273 delete ((*i).second);
276 /* all waveviews etc will be destroyed when the group is destroyed */
278 delete gain_line;
281 boost::shared_ptr<ARDOUR::AudioRegion>
282 AudioRegionView::audio_region() const
284 // "Guaranteed" to succeed...
285 return boost::dynamic_pointer_cast<AudioRegion>(_region);
288 void
289 AudioRegionView::region_changed (const PropertyChange& what_changed)
291 ENSURE_GUI_THREAD (*this, &AudioRegionView::region_changed, what_changed);
292 // cerr << "AudioRegionView::region_changed() called" << endl;
294 RegionView::region_changed (what_changed);
296 if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
297 region_scale_amplitude_changed ();
299 if (what_changed.contains (ARDOUR::Properties::fade_in)) {
300 fade_in_changed ();
302 if (what_changed.contains (ARDOUR::Properties::fade_out)) {
303 fade_out_changed ();
305 if (what_changed.contains (ARDOUR::Properties::fade_in_active)) {
306 fade_in_active_changed ();
308 if (what_changed.contains (ARDOUR::Properties::fade_out_active)) {
309 fade_out_active_changed ();
311 if (what_changed.contains (ARDOUR::Properties::envelope_active)) {
312 envelope_active_changed ();
314 if (what_changed.contains (ARDOUR::Properties::valid_transients)) {
315 transients_changed ();
319 void
320 AudioRegionView::fade_in_changed ()
322 reset_fade_in_shape ();
325 void
326 AudioRegionView::fade_out_changed ()
328 reset_fade_out_shape ();
330 void
331 AudioRegionView::fade_in_active_changed ()
333 if (audio_region()->fade_in_active()) {
334 fade_in_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,90); // FIXME make a themeable colour
335 fade_in_shape->property_width_pixels() = 1;
336 } else {
337 fade_in_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,20); // FIXME make a themeable colour
338 fade_in_shape->property_width_pixels() = 1;
342 void
343 AudioRegionView::fade_out_active_changed ()
345 if (audio_region()->fade_out_active()) {
346 fade_out_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,90); // FIXME make a themeable colour
347 fade_out_shape->property_width_pixels() = 1;
348 } else {
349 fade_out_shape->property_fill_color_rgba() = RGBA_TO_UINT(45,45,45,20); // FIXME make a themeable colour
350 fade_out_shape->property_width_pixels() = 1;
355 void
356 AudioRegionView::region_scale_amplitude_changed ()
358 ENSURE_GUI_THREAD (*this, &AudioRegionView::region_scale_amplitude_changed)
360 for (uint32_t n = 0; n < waves.size(); ++n) {
361 // force a reload of the cache
362 waves[n]->property_data_src() = _region.get();
366 void
367 AudioRegionView::region_renamed ()
369 std::string str = RegionView::make_name ();
371 if (audio_region()->speed_mismatch (trackview.session()->frame_rate())) {
372 str = string ("*") + str;
375 if (_region->muted()) {
376 str = string ("!") + str;
379 set_item_name (str, this);
380 set_name_text (str);
383 void
384 AudioRegionView::region_resized (const PropertyChange& what_changed)
386 AudioGhostRegion* agr;
388 RegionView::region_resized(what_changed);
389 PropertyChange interesting_stuff;
391 interesting_stuff.add (ARDOUR::Properties::start);
392 interesting_stuff.add (ARDOUR::Properties::length);
394 if (what_changed.contains (interesting_stuff)) {
396 for (uint32_t n = 0; n < waves.size(); ++n) {
397 waves[n]->property_region_start() = _region->start();
400 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
401 if ((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
403 for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
404 (*w)->property_region_start() = _region->start();
409 /* hide transient lines that extend beyond the region end */
411 list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator l;
413 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
414 if ((*l).first > _region->length()- 1){
415 (*l).second->hide();
417 else {
418 (*l).second->show();
424 void
425 AudioRegionView::reset_width_dependent_items (double pixel_width)
427 RegionView::reset_width_dependent_items(pixel_width);
428 assert(_pixel_width == pixel_width);
430 if (zero_line) {
431 zero_line->property_x2() = pixel_width - 1.0;
434 if (fade_in_handle) {
435 if (pixel_width <= 6.0 || _height < 5.0 || !trackview.session()->config.get_show_region_fades()) {
436 fade_in_handle->hide();
437 fade_out_handle->hide();
439 else {
440 fade_in_handle->show();
441 fade_out_handle->show();
445 AnalysisFeatureList analysis_features = _region->transients();
446 AnalysisFeatureList::const_iterator i;
447 list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator l;
449 for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
450 (*l).second->property_x1() = trackview.editor().frame_to_pixel (*i);
451 (*l).second->property_x2() = trackview.editor().frame_to_pixel (*i);
454 reset_fade_shapes ();
457 void
458 AudioRegionView::region_muted ()
460 RegionView::region_muted();
462 for (uint32_t n=0; n < waves.size(); ++n) {
463 if (_region->muted()) {
464 waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
465 } else {
466 waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
471 void
472 AudioRegionView::setup_fade_handle_positions()
474 /* position of fade handle offset from the top of the region view */
475 double const handle_pos = 2;
476 /* height of fade handles */
477 double const handle_height = 5;
479 if (fade_in_handle) {
480 fade_in_handle->property_y1() = handle_pos;
481 fade_in_handle->property_y2() = handle_pos + handle_height;
484 if (fade_out_handle) {
485 fade_out_handle->property_y1() = handle_pos;
486 fade_out_handle->property_y2() = handle_pos + handle_height;
490 void
491 AudioRegionView::set_height (gdouble height)
493 RegionView::set_height (height);
495 uint32_t wcnt = waves.size();
497 for (uint32_t n = 0; n < wcnt; ++n) {
498 gdouble ht;
500 if (height < NAME_HIGHLIGHT_THRESH) {
501 ht = ((height - 2 * wcnt) / (double) wcnt);
502 } else {
503 ht = (((height - 2 * wcnt) - NAME_HIGHLIGHT_SIZE) / (double) wcnt);
506 gdouble yoff = n * (ht + 1);
508 waves[n]->property_height() = ht;
509 waves[n]->property_y() = yoff + 2;
512 if (gain_line) {
514 if ((height/wcnt) < NAME_HIGHLIGHT_THRESH) {
515 gain_line->hide ();
516 } else {
517 if (_flags & EnvelopeVisible) {
518 gain_line->show ();
522 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE) - 2);
525 manage_zero_line ();
526 reset_fade_shapes ();
528 /* Update hights for any active feature lines */
529 list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator l;
531 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
532 (*l).second->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
535 if (fade_position_line) {
536 if (height < NAME_HIGHLIGHT_THRESH) {
537 fade_position_line->property_y2() = _height - 1;
539 else {
540 fade_position_line->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
544 if (name_pixbuf) {
545 name_pixbuf->raise_to_top();
549 void
550 AudioRegionView::manage_zero_line ()
552 if (!zero_line) {
553 return;
556 if (_height >= 100) {
557 double const wave_midpoint = (_height - NAME_HIGHLIGHT_SIZE) / 2.0;
558 zero_line->property_y1() = wave_midpoint;
559 zero_line->property_y2() = wave_midpoint;
560 zero_line->show();
561 } else {
562 zero_line->hide();
566 void
567 AudioRegionView::reset_fade_shapes ()
569 reset_fade_in_shape ();
570 reset_fade_out_shape ();
573 void
574 AudioRegionView::reset_fade_in_shape ()
576 reset_fade_in_shape_width ((framecnt_t) audio_region()->fade_in()->back()->when);
579 void
580 AudioRegionView::reset_fade_in_shape_width (framecnt_t width)
582 if (fade_in_handle == 0) {
583 return;
586 /* smallest size for a fade is 64 frames */
588 width = std::max ((framecnt_t) 64, width);
590 Points* points;
592 /* round here to prevent little visual glitches with sub-pixel placement */
593 double const pwidth = rint (width / samples_per_unit);
594 uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
595 double h;
597 if (_height < 5) {
598 fade_in_shape->hide();
599 fade_in_handle->hide();
600 return;
603 double const handle_center = pwidth;
605 /* Put the fade in handle so that its left side is at the end-of-fade line */
606 fade_in_handle->property_x1() = handle_center;
607 fade_in_handle->property_x2() = handle_center + 6;
609 if (pwidth < 5) {
610 fade_in_shape->hide();
611 return;
614 if (trackview.session()->config.get_show_region_fades()) {
615 fade_in_shape->show();
618 float curve[npoints];
619 audio_region()->fade_in()->curve().get_vector (0, audio_region()->fade_in()->back()->when, curve, npoints);
621 points = get_canvas_points ("fade in shape", npoints + 3);
623 if (_height >= NAME_HIGHLIGHT_THRESH) {
624 h = _height - NAME_HIGHLIGHT_SIZE - 2;
625 } else {
626 h = _height;
629 /* points *MUST* be in anti-clockwise order */
631 uint32_t pi, pc;
632 double xdelta = pwidth/npoints;
634 for (pi = 0, pc = 0; pc < npoints; ++pc) {
635 (*points)[pi].set_x(1 + (pc * xdelta));
636 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
639 /* fold back */
641 (*points)[pi].set_x(pwidth);
642 (*points)[pi++].set_y(2);
644 (*points)[pi].set_x(1);
645 (*points)[pi++].set_y(2);
647 /* connect the dots ... */
649 (*points)[pi] = (*points)[0];
651 fade_in_shape->property_points() = *points;
652 delete points;
654 /* ensure trim handle stays on top */
655 if (frame_handle_start) {
656 frame_handle_start->raise_to_top();
660 void
661 AudioRegionView::reset_fade_out_shape ()
663 reset_fade_out_shape_width ((framecnt_t) audio_region()->fade_out()->back()->when);
666 void
667 AudioRegionView::reset_fade_out_shape_width (framecnt_t width)
669 if (fade_out_handle == 0) {
670 return;
673 /* smallest size for a fade is 64 frames */
675 width = std::max ((framecnt_t) 64, width);
677 Points* points;
679 /* round here to prevent little visual glitches with sub-pixel placement */
680 double const pwidth = rint (width / samples_per_unit);
681 uint32_t npoints = std::min (gdk_screen_width(), (int) pwidth);
682 double h;
684 if (_height < 5) {
685 fade_out_shape->hide();
686 fade_out_handle->hide();
687 return;
690 double const handle_center = (_region->length() - width) / samples_per_unit;
692 /* Put the fade out handle so that its right side is at the end-of-fade line;
693 * it's `one out' for precise pixel accuracy.
695 fade_out_handle->property_x1() = handle_center - 5;
696 fade_out_handle->property_x2() = handle_center + 1;
698 /* don't show shape if its too small */
700 if (pwidth < 5) {
701 fade_out_shape->hide();
702 return;
705 if (trackview.session()->config.get_show_region_fades()) {
706 fade_out_shape->show();
709 float curve[npoints];
710 audio_region()->fade_out()->curve().get_vector (0, audio_region()->fade_out()->back()->when, curve, npoints);
712 if (_height >= NAME_HIGHLIGHT_THRESH) {
713 h = _height - NAME_HIGHLIGHT_SIZE - 2;
714 } else {
715 h = _height;
718 /* points *MUST* be in anti-clockwise order */
720 points = get_canvas_points ("fade out shape", npoints + 3);
722 uint32_t pi, pc;
723 double xdelta = pwidth/npoints;
725 for (pi = 0, pc = 0; pc < npoints; ++pc) {
726 (*points)[pi].set_x(_pixel_width - pwidth + (pc * xdelta));
727 (*points)[pi++].set_y(2 + (h - (curve[pc] * h)));
730 /* fold back */
732 (*points)[pi].set_x(_pixel_width);
733 (*points)[pi++].set_y(h);
735 (*points)[pi].set_x(_pixel_width);
736 (*points)[pi++].set_y(2);
738 /* connect the dots ... */
740 (*points)[pi] = (*points)[0];
742 fade_out_shape->property_points() = *points;
743 delete points;
745 /* ensure trim handle stays on top */
746 if (frame_handle_end) {
747 frame_handle_end->raise_to_top();
751 void
752 AudioRegionView::set_samples_per_unit (gdouble spu)
754 RegionView::set_samples_per_unit (spu);
756 if (_flags & WaveformVisible) {
757 for (uint32_t n=0; n < waves.size(); ++n) {
758 waves[n]->property_samples_per_unit() = spu;
762 if (gain_line) {
763 gain_line->reset ();
766 reset_fade_shapes ();
769 void
770 AudioRegionView::set_amplitude_above_axis (gdouble spp)
772 for (uint32_t n=0; n < waves.size(); ++n) {
773 waves[n]->property_amplitude_above_axis() = spp;
777 void
778 AudioRegionView::compute_colors (Gdk::Color const & basic_color)
780 RegionView::compute_colors (basic_color);
782 /* gain color computed in envelope_active_changed() */
784 fade_color = UINT_RGBA_CHANGE_A (fill_color, 120);
787 void
788 AudioRegionView::set_colors ()
790 RegionView::set_colors();
792 if (gain_line) {
793 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
796 for (uint32_t n=0; n < waves.size(); ++n) {
797 if (_region->muted()) {
798 waves[n]->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
799 } else {
800 waves[n]->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
803 waves[n]->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
804 waves[n]->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
808 void
809 AudioRegionView::set_waveform_visible (bool yn)
811 if (((_flags & WaveformVisible) != yn)) {
812 if (yn) {
813 for (uint32_t n=0; n < waves.size(); ++n) {
814 /* make sure the zoom level is correct, since we don't update
815 this when waveforms are hidden.
817 waves[n]->property_samples_per_unit() = samples_per_unit;
818 waves[n]->show();
820 _flags |= WaveformVisible;
821 } else {
822 for (uint32_t n=0; n < waves.size(); ++n) {
823 waves[n]->hide();
825 _flags &= ~WaveformVisible;
827 store_flags ();
831 void
832 AudioRegionView::temporarily_hide_envelope ()
834 if (gain_line) {
835 gain_line->hide ();
839 void
840 AudioRegionView::unhide_envelope ()
842 if (gain_line && (_flags & EnvelopeVisible)) {
843 gain_line->show ();
847 void
848 AudioRegionView::set_envelope_visible (bool yn)
850 if (gain_line && ((_flags & EnvelopeVisible) != yn)) {
851 if (yn) {
852 gain_line->show ();
853 _flags |= EnvelopeVisible;
854 } else {
855 gain_line->hide ();
856 _flags &= ~EnvelopeVisible;
858 store_flags ();
862 void
863 AudioRegionView::create_waves ()
865 // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
866 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
868 if (!atv.track()) {
869 return;
872 ChanCount nchans = atv.track()->n_channels();
874 // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
875 // << " and channels = " << nchans.n_audio() << endl;
877 /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
878 for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
879 tmp_waves.push_back (0);
882 for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
883 delete *i;
886 _data_ready_connections.clear ();
888 for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
889 _data_ready_connections.push_back (0);
892 for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
894 if (n >= audio_region()->n_channels()) {
895 break;
898 wave_caches.push_back (WaveView::create_cache ());
900 // cerr << "\tchannel " << n << endl;
902 if (wait_for_data) {
903 if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
904 // cerr << "\tData is ready\n";
905 create_one_wave (n, true);
906 } else {
907 // cerr << "\tdata is not ready\n";
908 // we'll get a PeaksReady signal from the source in the future
909 // and will call create_one_wave(n) then.
912 } else {
913 // cerr << "\tdon't delay, display today!\n";
914 create_one_wave (n, true);
920 void
921 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
923 //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
924 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
925 uint32_t nchans = atv.track()->n_channels().n_audio();
926 uint32_t n;
927 uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
928 gdouble ht;
930 if (trackview.current_height() < NAME_HIGHLIGHT_THRESH) {
931 ht = ((trackview.current_height()) / (double) nchans);
932 } else {
933 ht = ((trackview.current_height() - NAME_HIGHLIGHT_SIZE) / (double) nchans);
936 gdouble yoff = which * ht;
938 WaveView *wave = new WaveView(*group);
940 wave->property_data_src() = (gpointer) _region.get();
941 wave->property_cache() = wave_caches[which];
942 wave->property_cache_updater() = true;
943 wave->property_channel() = which;
944 wave->property_length_function() = (gpointer) region_length_from_c;
945 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
946 wave->property_peak_function() = (gpointer) region_read_peaks_from_c;
947 wave->property_x() = 0.0;
948 wave->property_y() = yoff;
949 wave->property_height() = (double) ht;
950 wave->property_samples_per_unit() = samples_per_unit;
951 wave->property_amplitude_above_axis() = _amplitude_above_axis;
953 if (_recregion) {
954 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
955 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
956 } else {
957 wave->property_wave_color() = _region->muted() ? UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA) : ARDOUR_UI::config()->canvasvar_WaveForm.get();
958 wave->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
961 wave->property_clip_color() = ARDOUR_UI::config()->canvasvar_WaveFormClip.get();
962 wave->property_zero_color() = ARDOUR_UI::config()->canvasvar_ZeroLine.get();
963 wave->property_region_start() = _region->start();
964 wave->property_rectified() = (bool) (_flags & WaveformRectified);
965 wave->property_logscaled() = (bool) (_flags & WaveformLogScaled);
967 if (!(_flags & WaveformVisible)) {
968 wave->hide();
971 /* note: calling this function is serialized by the lock
972 held in the peak building thread that signals that
973 peaks are ready for use *or* by the fact that it is
974 called one by one from the GUI thread.
977 if (which < nchans) {
978 tmp_waves[which] = wave;
979 } else {
980 /* n-channel track, >n-channel source */
983 /* see if we're all ready */
985 for (n = 0; n < nchans; ++n) {
986 if (tmp_waves[n] == 0) {
987 break;
991 if (n == nwaves && waves.empty()) {
992 /* all waves are ready */
993 tmp_waves.resize(nwaves);
995 waves = tmp_waves;
996 tmp_waves.clear ();
998 /* all waves created, don't hook into peaks ready anymore */
999 delete _data_ready_connections[which];
1000 _data_ready_connections[which] = 0;
1002 #if 0
1003 if (!zero_line) {
1004 zero_line = new ArdourCanvas::SimpleLine (*group);
1005 zero_line->property_x1() = (gdouble) 1.0;
1006 zero_line->property_x2() = (gdouble) (_region->length() / samples_per_unit) - 1.0;
1007 zero_line->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1008 manage_zero_line ();
1010 #endif
1014 void
1015 AudioRegionView::peaks_ready_handler (uint32_t which)
1017 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AudioRegionView::create_one_wave, this, which, false));
1018 // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
1021 void
1022 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev)
1024 if (gain_line == 0) {
1025 return;
1028 double x, y;
1030 /* don't create points that can't be seen */
1032 set_envelope_visible (true);
1034 x = ev->button.x;
1035 y = ev->button.y;
1037 item->w2i (x, y);
1039 framepos_t fx = trackview.editor().pixel_to_frame (x);
1041 if (fx > _region->length()) {
1042 return;
1045 /* compute vertical fractional position */
1047 y = 1.0 - (y / (_height - NAME_HIGHLIGHT_SIZE));
1049 /* map using gain line */
1051 gain_line->view_to_model_coord (x, y);
1053 /* XXX STATEFUL: can't convert to stateful diff until we
1054 can represent automation data with it.
1057 trackview.session()->begin_reversible_command (_("add gain control point"));
1058 XMLNode &before = audio_region()->envelope()->get_state();
1060 if (!audio_region()->envelope_active()) {
1061 XMLNode &region_before = audio_region()->get_state();
1062 audio_region()->set_envelope_active(true);
1063 XMLNode &region_after = audio_region()->get_state();
1064 trackview.session()->add_command (new MementoCommand<AudioRegion>(*(audio_region().get()), &region_before, &region_after));
1067 audio_region()->envelope()->add (fx, y);
1069 XMLNode &after = audio_region()->envelope()->get_state();
1070 trackview.session()->add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1071 trackview.session()->commit_reversible_command ();
1074 void
1075 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent */*ev*/)
1077 ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1078 audio_region()->envelope()->erase (cp->model());
1081 void
1082 AudioRegionView::store_flags()
1084 XMLNode *node = new XMLNode ("GUI");
1086 node->add_property ("waveform-visible", (_flags & WaveformVisible) ? "yes" : "no");
1087 node->add_property ("envelope-visible", (_flags & EnvelopeVisible) ? "yes" : "no");
1088 node->add_property ("waveform-rectified", (_flags & WaveformRectified) ? "yes" : "no");
1089 node->add_property ("waveform-logscaled", (_flags & WaveformLogScaled) ? "yes" : "no");
1091 _region->add_extra_xml (*node);
1094 void
1095 AudioRegionView::set_flags (XMLNode* node)
1097 XMLProperty *prop;
1099 if ((prop = node->property ("waveform-visible")) != 0) {
1100 if (string_is_affirmative (prop->value())) {
1101 _flags |= WaveformVisible;
1105 if ((prop = node->property ("envelope-visible")) != 0) {
1106 if (string_is_affirmative (prop->value())) {
1107 _flags |= EnvelopeVisible;
1111 if ((prop = node->property ("waveform-rectified")) != 0) {
1112 if (string_is_affirmative (prop->value())) {
1113 _flags |= WaveformRectified;
1117 if ((prop = node->property ("waveform-logscaled")) != 0) {
1118 if (string_is_affirmative (prop->value())) {
1119 _flags |= WaveformLogScaled;
1124 void
1125 AudioRegionView::set_waveform_shape (WaveformShape shape)
1127 bool yn;
1129 /* this slightly odd approach is to leave the door open to
1130 other "shapes" such as spectral displays, etc.
1133 switch (shape) {
1134 case Rectified:
1135 yn = true;
1136 break;
1138 default:
1139 yn = false;
1140 break;
1143 if (yn != (bool) (_flags & WaveformRectified)) {
1144 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1145 (*wave)->property_rectified() = yn;
1148 if (zero_line) {
1149 if (yn) {
1150 zero_line->hide();
1151 } else {
1152 zero_line->show();
1156 if (yn) {
1157 _flags |= WaveformRectified;
1158 } else {
1159 _flags &= ~WaveformRectified;
1161 store_flags ();
1165 void
1166 AudioRegionView::set_waveform_scale (WaveformScale scale)
1168 bool yn = (scale == Logarithmic);
1170 if (yn != (bool) (_flags & WaveformLogScaled)) {
1171 for (vector<WaveView *>::iterator wave = waves.begin(); wave != waves.end() ; ++wave) {
1172 (*wave)->property_logscaled() = yn;
1175 if (yn) {
1176 _flags |= WaveformLogScaled;
1177 } else {
1178 _flags &= ~WaveformLogScaled;
1180 store_flags ();
1185 GhostRegion*
1186 AudioRegionView::add_ghost (TimeAxisView& tv)
1188 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1189 assert(rtv);
1191 double unit_position = _region->position () / samples_per_unit;
1192 AudioGhostRegion* ghost = new AudioGhostRegion (tv, trackview, unit_position);
1193 uint32_t nchans;
1195 nchans = rtv->track()->n_channels().n_audio();
1197 for (uint32_t n = 0; n < nchans; ++n) {
1199 if (n >= audio_region()->n_channels()) {
1200 break;
1203 WaveView *wave = new WaveView(*ghost->group);
1205 wave->property_data_src() = _region.get();
1206 wave->property_cache() = wave_caches[n];
1207 wave->property_cache_updater() = false;
1208 wave->property_channel() = n;
1209 wave->property_length_function() = (gpointer)region_length_from_c;
1210 wave->property_sourcefile_length_function() = (gpointer) sourcefile_length_from_c;
1211 wave->property_peak_function() = (gpointer) region_read_peaks_from_c;
1212 wave->property_x() = 0.0;
1213 wave->property_samples_per_unit() = samples_per_unit;
1214 wave->property_amplitude_above_axis() = _amplitude_above_axis;
1216 wave->property_region_start() = _region->start();
1218 ghost->waves.push_back(wave);
1221 ghost->set_height ();
1222 ghost->set_duration (_region->length() / samples_per_unit);
1223 ghost->set_colors();
1224 ghosts.push_back (ghost);
1226 return ghost;
1229 void
1230 AudioRegionView::entered (bool internal_editing)
1232 trackview.editor().set_current_trimmable (_region);
1233 trackview.editor().set_current_movable (_region);
1235 if (gain_line && _flags & EnvelopeVisible) {
1236 gain_line->show_all_control_points ();
1239 if (fade_in_handle && !internal_editing) {
1240 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 255);
1241 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 255);
1245 void
1246 AudioRegionView::exited ()
1248 trackview.editor().set_current_trimmable (boost::shared_ptr<Trimmable>());
1249 trackview.editor().set_current_movable (boost::shared_ptr<Movable>());
1251 if (gain_line) {
1252 gain_line->hide_all_but_selected_control_points ();
1255 if (fade_in_handle) {
1256 fade_in_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 0);
1257 fade_out_handle->property_fill_color_rgba() = UINT_RGBA_CHANGE_A (fade_color, 0);
1261 void
1262 AudioRegionView::envelope_active_changed ()
1264 if (gain_line) {
1265 gain_line->set_line_color (audio_region()->envelope_active() ? ARDOUR_UI::config()->canvasvar_GainLine.get() : ARDOUR_UI::config()->canvasvar_GainLineInactive.get());
1269 void
1270 AudioRegionView::set_waveview_data_src()
1272 AudioGhostRegion* agr;
1273 double unit_length= _region->length() / samples_per_unit;
1275 for (uint32_t n = 0; n < waves.size(); ++n) {
1276 // TODO: something else to let it know the channel
1277 waves[n]->property_data_src() = _region.get();
1280 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
1282 (*i)->set_duration (unit_length);
1284 if((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
1285 for (vector<WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
1286 (*w)->property_data_src() = _region.get();
1293 void
1294 AudioRegionView::color_handler ()
1296 //case cMutedWaveForm:
1297 //case cWaveForm:
1298 //case cWaveFormClip:
1299 //case cZeroLine:
1300 set_colors ();
1302 //case cGainLineInactive:
1303 //case cGainLine:
1304 envelope_active_changed();
1308 void
1309 AudioRegionView::set_frame_color ()
1311 if (!frame) {
1312 return;
1315 if (_region->opaque()) {
1316 fill_opacity = 130;
1317 } else {
1318 fill_opacity = 0;
1321 TimeAxisViewItem::set_frame_color ();
1323 if (_selected) {
1324 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1325 if (_region->muted()) {
1326 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get(), MUTED_ALPHA);
1327 } else {
1328 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveForm.get();
1329 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_SelectedWaveFormFill.get();
1332 } else {
1333 if (_recregion) {
1334 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1335 if (_region->muted()) {
1336 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_RecWaveForm.get(), MUTED_ALPHA);
1337 } else {
1338 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_RecWaveForm.get();
1339 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_RecWaveFormFill.get();
1342 } else {
1343 for (vector<ArdourCanvas::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1344 if (_region->muted()) {
1345 (*w)->property_wave_color() = UINT_RGBA_CHANGE_A(ARDOUR_UI::config()->canvasvar_WaveForm.get(), MUTED_ALPHA);
1346 } else {
1347 (*w)->property_wave_color() = ARDOUR_UI::config()->canvasvar_WaveForm.get();
1348 (*w)->property_fill_color() = ARDOUR_UI::config()->canvasvar_WaveFormFill.get();
1355 void
1356 AudioRegionView::set_fade_visibility (bool yn)
1358 if (yn) {
1359 if (fade_in_shape) {
1360 fade_in_shape->show();
1362 if (fade_out_shape) {
1363 fade_out_shape->show ();
1365 if (fade_in_handle) {
1366 fade_in_handle->show ();
1368 if (fade_out_handle) {
1369 fade_out_handle->show ();
1371 } else {
1372 if (fade_in_shape) {
1373 fade_in_shape->hide();
1375 if (fade_out_shape) {
1376 fade_out_shape->hide ();
1378 if (fade_in_handle) {
1379 fade_in_handle->hide ();
1381 if (fade_out_handle) {
1382 fade_out_handle->hide ();
1387 void
1388 AudioRegionView::update_coverage_frames (LayerDisplay d)
1390 RegionView::update_coverage_frames (d);
1392 if (fade_in_handle) {
1393 fade_in_handle->raise_to_top ();
1394 fade_out_handle->raise_to_top ();
1398 void
1399 AudioRegionView::show_region_editor ()
1401 if (editor == 0) {
1402 editor = new AudioRegionEditor (trackview.session(), audio_region());
1405 editor->present ();
1406 editor->set_position (Gtk::WIN_POS_MOUSE);
1407 editor->show_all();
1411 void
1412 AudioRegionView::show_fade_line (framepos_t pos)
1414 fade_position_line->property_x1() = trackview.editor().frame_to_pixel (pos);
1415 fade_position_line->property_x2() = trackview.editor().frame_to_pixel (pos);
1416 fade_position_line->show ();
1417 fade_position_line->raise_to_top ();
1420 void
1421 AudioRegionView::hide_fade_line ()
1423 fade_position_line->hide ();
1427 void
1428 AudioRegionView::transients_changed ()
1430 AnalysisFeatureList analysis_features = _region->transients();
1432 while (feature_lines.size() < analysis_features.size()) {
1433 ArdourCanvas::SimpleLine* l = new ArdourCanvas::SimpleLine (*group);
1434 l->property_color_rgba() = (guint) ARDOUR_UI::config()->canvasvar_ZeroLine.get();
1435 feature_lines.push_back (make_pair(0, l));
1438 while (feature_lines.size() > analysis_features.size()) {
1439 ArdourCanvas::SimpleLine *line = feature_lines.back().second;
1440 feature_lines.pop_back ();
1441 delete line;
1444 AnalysisFeatureList::const_iterator i;
1445 list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator l;
1447 for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1448 (*l).first = *i;
1449 (*l).second->property_x1() = trackview.editor().frame_to_pixel (*i);
1450 (*l).second->property_x2() = trackview.editor().frame_to_pixel (*i);
1451 (*l).second->property_y1() = 2;
1452 (*l).second->property_y2() = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
1453 (*l).second->set_data("regionview", this);
1454 (*l).second->show ();
1455 (*l).second->raise_to_top ();
1457 (*l).second->signal_event().connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), (*l).second, this));
1461 void
1462 AudioRegionView::update_transient(float /*old_pos*/, float new_pos)
1464 /* Find frame at old pos, calulate new frame then update region transients*/
1465 list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator l;
1467 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1468 /* Simple line x1 has been updated in drag so we compare to new_pos */
1469 if (rint(new_pos) == rint((*l).second->property_x1())) {
1471 framepos_t old_frame = (*l).first;
1472 framepos_t new_frame = trackview.editor().pixel_to_frame (new_pos);
1474 _region->update_transient (old_frame, new_frame);
1476 break;
1481 void
1482 AudioRegionView::remove_transient(float pos)
1484 /* Find frame at old pos, calulate new frame then update region transients*/
1485 list<std::pair<framepos_t, ArdourCanvas::SimpleLine*> >::iterator l;
1487 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1488 /* Simple line x1 has been updated in drag so we compare to new_pos */
1489 if (rint(pos) == rint((*l).second->property_x1())) {
1490 _region->remove_transient ((*l).first);
1491 break;
1496 void
1497 AudioRegionView::thaw_after_trim ()
1499 RegionView::thaw_after_trim ();
1501 unhide_envelope ();