1 /*
2 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
4 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2007 Doug McLain <doug@nostar.net>
6 * Copyright (C) 2014-2015 Ben Loftis <ben@harrisonconsoles.com>
7 * Copyright (C) 2014-2017 Nick Mainsbridge <mainsbridge@gmail.com>
8 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
9 * Copyright (C) 2015-2016 Tim Mayberry <mojofunk@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26 #include <cmath>
27 #include <cassert>
28 #include <algorithm>
29 #include <vector>
30
31 #include <boost/scoped_array.hpp>
32
33 #include <gtkmm.h>
34
35 #include "ardour/playlist.h"
36 #include "ardour/audioregion.h"
37 #include "ardour/audiosource.h"
38 #include "ardour/profile.h"
39 #include "ardour/session.h"
40
41 #include "pbd/memento_command.h"
42
43 #include "evoral/Curve.h"
44
45 #include "gtkmm2ext/gtk_ui.h"
46 #include "gtkmm2ext/utils.h"
47 #include "gtkmm2ext/colors.h"
48
49 #include "canvas/rectangle.h"
50 #include "canvas/polygon.h"
51 #include "canvas/poly_line.h"
52 #include "canvas/line.h"
53 #include "canvas/text.h"
54 #include "canvas/xfade_curve.h"
55 #include "canvas/debug.h"
56
57 #include "waveview/debug.h"
58
59 #include "streamview.h"
60 #include "audio_region_view.h"
61 #include "audio_time_axis.h"
62 #include "enums_convert.h"
63 #include "public_editor.h"
64 #include "audio_region_editor.h"
65 #include "audio_streamview.h"
66 #include "region_gain_line.h"
67 #include "control_point.h"
68 #include "ghostregion.h"
69 #include "audio_time_axis.h"
70 #include "rgb_macros.h"
71 #include "gui_thread.h"
72 #include "ui_config.h"
73
74 #include "pbd/i18n.h"
75
76 using namespace std;
77 using namespace ARDOUR;
78 using namespace PBD;
79 using namespace Editing;
80 using namespace ArdourCanvas;
81
82 static double const handle_size = 10; /* height of fade handles */
83
84 Cairo::RefPtr<Cairo::Pattern> AudioRegionView::pending_peak_pattern;
85
create_pending_peak_pattern()86 static Cairo::RefPtr<Cairo::Pattern> create_pending_peak_pattern() {
87 cairo_surface_t * is = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 8, 8);
88
89 // create checker pattern
90 unsigned char *img = cairo_image_surface_get_data (is);
91 cairo_surface_flush (is);
92 const int stride = cairo_image_surface_get_stride (is);
93
94 for (int y = 0; y < 8; ++y) {
95 for (int x = 0; x < 8; ++x) {
96 const int off = (y * stride + x * 4);
97 uint32_t *pixel = (uint32_t*) &img[off];
98 if ((x < 4) ^ (y < 4)) {
99 *pixel = 0xa0000000;
100 } else {
101 *pixel = 0x40000000;
102 }
103 }
104 }
105 cairo_surface_mark_dirty (is);
106
107 cairo_pattern_t* pat = cairo_pattern_create_for_surface (is);
108 cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
109 Cairo::RefPtr<Cairo::Pattern> p (new Cairo::Pattern (pat, false));
110 cairo_surface_destroy (is);
111 return p;
112 }
113
AudioRegionView(ArdourCanvas::Container * parent,RouteTimeAxisView & tv,boost::shared_ptr<AudioRegion> r,double spu,uint32_t basic_color)114 AudioRegionView::AudioRegionView (ArdourCanvas::Container *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
115 uint32_t basic_color)
116 : RegionView (parent, tv, r, spu, basic_color)
117 , sync_mark(0)
118 , fade_in_handle(0)
119 , fade_out_handle(0)
120 , fade_in_trim_handle(0)
121 , fade_out_trim_handle(0)
122 , pending_peak_data(0)
123 , start_xfade_curve (0)
124 , start_xfade_rect (0)
125 , _start_xfade_visible (false)
126 , end_xfade_curve (0)
127 , end_xfade_rect (0)
128 , _end_xfade_visible (false)
129 , _amplitude_above_axis(1.0)
130 , trim_fade_in_drag_active(false)
131 , trim_fade_out_drag_active(false)
132 {
133 }
134
AudioRegionView(ArdourCanvas::Container * parent,RouteTimeAxisView & tv,boost::shared_ptr<AudioRegion> r,double spu,uint32_t basic_color,bool recording,TimeAxisViewItem::Visibility visibility)135 AudioRegionView::AudioRegionView (ArdourCanvas::Container *parent, RouteTimeAxisView &tv, boost::shared_ptr<AudioRegion> r, double spu,
136 uint32_t basic_color, bool recording, TimeAxisViewItem::Visibility visibility)
137 : RegionView (parent, tv, r, spu, basic_color, recording, visibility)
138 , sync_mark(0)
139 , fade_in_handle(0)
140 , fade_out_handle(0)
141 , fade_in_trim_handle(0)
142 , fade_out_trim_handle(0)
143 , pending_peak_data(0)
144 , start_xfade_curve (0)
145 , start_xfade_rect (0)
146 , _start_xfade_visible (false)
147 , end_xfade_curve (0)
148 , end_xfade_rect (0)
149 , _end_xfade_visible (false)
150 , _amplitude_above_axis(1.0)
151 , trim_fade_in_drag_active(false)
152 , trim_fade_out_drag_active(false)
153 {
154 }
155
AudioRegionView(const AudioRegionView & other,boost::shared_ptr<AudioRegion> other_region)156 AudioRegionView::AudioRegionView (const AudioRegionView& other, boost::shared_ptr<AudioRegion> other_region)
157 : RegionView (other, boost::shared_ptr<Region> (other_region))
158 , fade_in_handle(0)
159 , fade_out_handle(0)
160 , fade_in_trim_handle(0)
161 , fade_out_trim_handle(0)
162 , pending_peak_data(0)
163 , start_xfade_curve (0)
164 , start_xfade_rect (0)
165 , _start_xfade_visible (false)
166 , end_xfade_curve (0)
167 , end_xfade_rect (0)
168 , _end_xfade_visible (false)
169 , _amplitude_above_axis (other._amplitude_above_axis)
170 , trim_fade_in_drag_active(false)
171 , trim_fade_out_drag_active(false)
172 {
173 init (true);
174 }
175
176 void
init(bool wfd)177 AudioRegionView::init (bool wfd)
178 {
179 // FIXME: Some redundancy here with RegionView::init. Need to figure out
180 // where order is important and where it isn't...
181
182 if (!pending_peak_pattern) {
183 pending_peak_pattern = create_pending_peak_pattern();
184 }
185
186 // needs to be created first, RegionView::init() calls set_height()
187 pending_peak_data = new ArdourCanvas::Rectangle (group);
188 CANVAS_DEBUG_NAME (pending_peak_data, string_compose ("pending peak rectangle for %1", region()->name()));
189 pending_peak_data->set_outline_color (Gtkmm2ext::rgba_to_color (0, 0, 0, 0.0));
190 pending_peak_data->set_pattern (pending_peak_pattern);
191 pending_peak_data->set_data ("regionview", this);
192 pending_peak_data->hide ();
193
194 RegionView::init (wfd);
195
196 _amplitude_above_axis = 1.0;
197
198 create_waves ();
199
200 if (!_recregion) {
201 fade_in_handle = new ArdourCanvas::Rectangle (group);
202 CANVAS_DEBUG_NAME (fade_in_handle, string_compose ("fade in handle for %1", region()->name()));
203 fade_in_handle->set_outline_color (Gtkmm2ext::rgba_to_color (0, 0, 0, 1.0));
204 fade_in_handle->set_fill_color (UIConfiguration::instance().color ("inactive fade handle"));
205 fade_in_handle->set_data ("regionview", this);
206 fade_in_handle->hide ();
207
208 fade_out_handle = new ArdourCanvas::Rectangle (group);
209 CANVAS_DEBUG_NAME (fade_out_handle, string_compose ("fade out handle for %1", region()->name()));
210 fade_out_handle->set_outline_color (Gtkmm2ext::rgba_to_color (0, 0, 0, 1.0));
211 fade_out_handle->set_fill_color (UIConfiguration::instance().color ("inactive fade handle"));
212 fade_out_handle->set_data ("regionview", this);
213 fade_out_handle->hide ();
214
215 fade_in_trim_handle = new ArdourCanvas::Rectangle (group);
216 CANVAS_DEBUG_NAME (fade_in_handle, string_compose ("fade in trim handle for %1", region()->name()));
217 fade_in_trim_handle->set_outline_color (Gtkmm2ext::rgba_to_color (0, 0, 0, 1.0));
218 fade_in_trim_handle->set_fill_color (UIConfiguration::instance().color ("inactive fade handle"));
219 fade_in_trim_handle->set_data ("regionview", this);
220 fade_in_trim_handle->hide ();
221
222 fade_out_trim_handle = new ArdourCanvas::Rectangle (group);
223 CANVAS_DEBUG_NAME (fade_out_handle, string_compose ("fade out trim handle for %1", region()->name()));
224 fade_out_trim_handle->set_outline_color (Gtkmm2ext::rgba_to_color (0, 0, 0, 1.0));
225 fade_out_trim_handle->set_fill_color (UIConfiguration::instance().color ("inactive fade handle"));
226 fade_out_trim_handle->set_data ("regionview", this);
227 fade_out_trim_handle->hide ();
228 }
229
230 setup_fade_handle_positions ();
231
232 if (!trackview.session()->config.get_show_region_fades()) {
233 set_fade_visibility (false);
234 }
235
236 const string line_name = _region->name() + ":gain";
237
238 gain_line.reset (new AudioRegionGainLine (line_name, *this, *group, audio_region()->envelope()));
239
240 update_envelope_visibility ();
241 gain_line->reset ();
242
243 /* streamview will call set_height() */
244 //set_height (trackview.current_height()); // XXX not correct for Layered mode, but set_height() will fix later.
245
246 region_muted ();
247 region_sync_changed ();
248
249 region_resized (ARDOUR::bounds_change);
250 /* region_resized sets ghost region duration */
251
252 /* region_locked is a synonym for region_renamed () which is called in region_muted() above */
253 //region_locked ();
254
255 envelope_active_changed ();
256 fade_in_active_changed ();
257 fade_out_active_changed ();
258
259 reset_width_dependent_items (_pixel_width);
260
261 if (fade_in_handle) {
262 fade_in_handle->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_handle, this, false));
263 }
264
265 if (fade_out_handle) {
266 fade_out_handle->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_handle, this, false));
267 }
268
269 if (fade_in_trim_handle) {
270 fade_in_trim_handle->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_in_handle_event), fade_in_trim_handle, this, true));
271 }
272
273 if (fade_out_trim_handle) {
274 fade_out_trim_handle->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_fade_out_handle_event), fade_out_trim_handle, this, true));
275 }
276
277 set_colors ();
278
279 setup_waveform_visibility ();
280
281 /* XXX sync mark drag? */
282 }
283
~AudioRegionView()284 AudioRegionView::~AudioRegionView ()
285 {
286 in_destructor = true;
287
288 RegionViewGoingAway (this); /* EMIT_SIGNAL */
289
290 for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
291 delete *i;
292 }
293 _data_ready_connections.clear ();
294
295 for (list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator i = feature_lines.begin(); i != feature_lines.end(); ++i) {
296 delete ((*i).second);
297 }
298
299 /* all waveviews etc will be destroyed when the group is destroyed */
300 }
301
302 boost::shared_ptr<ARDOUR::AudioRegion>
audio_region() const303 AudioRegionView::audio_region() const
304 {
305 // "Guaranteed" to succeed...
306 return boost::dynamic_pointer_cast<AudioRegion>(_region);
307 }
308
309 void
region_changed(const PropertyChange & what_changed)310 AudioRegionView::region_changed (const PropertyChange& what_changed)
311 {
312 ENSURE_GUI_THREAD (*this, &AudioRegionView::region_changed, what_changed);
313
314 RegionView::region_changed (what_changed);
315
316 if (what_changed.contains (ARDOUR::Properties::scale_amplitude)) {
317 region_scale_amplitude_changed ();
318 }
319 if (what_changed.contains (ARDOUR::Properties::fade_in)) {
320 fade_in_changed ();
321 }
322 if (what_changed.contains (ARDOUR::Properties::fade_out)) {
323 fade_out_changed ();
324 }
325 if (what_changed.contains (ARDOUR::Properties::fade_in_active)) {
326 fade_in_active_changed ();
327 }
328 if (what_changed.contains (ARDOUR::Properties::fade_out_active)) {
329 fade_out_active_changed ();
330 }
331 if (what_changed.contains (ARDOUR::Properties::envelope_active)) {
332 envelope_active_changed ();
333 }
334 if (what_changed.contains (ARDOUR::Properties::valid_transients)) {
335 transients_changed ();
336 }
337 }
338
339 void
fade_in_changed()340 AudioRegionView::fade_in_changed ()
341 {
342 reset_fade_in_shape ();
343 }
344
345 void
fade_out_changed()346 AudioRegionView::fade_out_changed ()
347 {
348 reset_fade_out_shape ();
349 }
350
351 void
fade_in_active_changed()352 AudioRegionView::fade_in_active_changed ()
353 {
354 if (start_xfade_rect) {
355 if (audio_region()->fade_in_active()) {
356 start_xfade_rect->set_fill (false);
357 } else {
358 start_xfade_rect->set_fill_color (UIConfiguration::instance().color_mod ("inactive crossfade", "inactive crossfade"));
359 start_xfade_rect->set_fill (true);
360 }
361 }
362 }
363
364 void
fade_out_active_changed()365 AudioRegionView::fade_out_active_changed ()
366 {
367 if (end_xfade_rect) {
368 if (audio_region()->fade_out_active()) {
369 end_xfade_rect->set_fill (false);
370 } else {
371 end_xfade_rect->set_fill_color (UIConfiguration::instance().color_mod ("inactive crossfade", "inactive crossfade"));
372 end_xfade_rect->set_fill (true);
373 }
374 }
375 }
376
377
378 void
region_scale_amplitude_changed()379 AudioRegionView::region_scale_amplitude_changed ()
380 {
381 for (uint32_t n = 0; n < waves.size(); ++n) {
382 waves[n]->gain_changed ();
383 }
384 region_renamed ();
385 }
386
387 void
region_renamed()388 AudioRegionView::region_renamed ()
389 {
390 std::string str = RegionView::make_name ();
391
392 if (audio_region()->speed_mismatch (trackview.session()->sample_rate())) {
393 str = string ("*") + str;
394 }
395
396 if (_region->muted()) {
397 str = string ("!") + str;
398 }
399
400
401 boost::shared_ptr<AudioRegion> ar (audio_region());
402 if (ar->scale_amplitude() != 1.0) {
403 char tmp[32];
404 snprintf (tmp, 32, " (%.1fdB)", accurate_coefficient_to_dB (ar->scale_amplitude()));
405 str += tmp;
406 }
407
408 set_item_name (str, this);
409 set_name_text (str);
410 }
411
412 void
region_resized(const PropertyChange & what_changed)413 AudioRegionView::region_resized (const PropertyChange& what_changed)
414 {
415 AudioGhostRegion* agr;
416
417 RegionView::region_resized(what_changed);
418 PropertyChange interesting_stuff;
419
420 interesting_stuff.add (ARDOUR::Properties::start);
421 interesting_stuff.add (ARDOUR::Properties::length);
422
423 if (what_changed.contains (interesting_stuff)) {
424
425 for (uint32_t n = 0; n < waves.size(); ++n) {
426 waves[n]->region_resized ();
427 }
428
429 for (vector<GhostRegion*>::iterator i = ghosts.begin(); i != ghosts.end(); ++i) {
430 if ((agr = dynamic_cast<AudioGhostRegion*>(*i)) != 0) {
431
432 for (vector<ArdourWaveView::WaveView*>::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) {
433 (*w)->region_resized ();
434 }
435 }
436 }
437
438 /* hide transient lines that extend beyond the region */
439 list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator l;
440 samplepos_t first = _region->first_sample();
441 samplepos_t last = _region->last_sample();
442
443 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
444 if (l->first < first || l->first >= last) {
445 l->second->hide();
446 } else {
447 l->second->show();
448 }
449 }
450 }
451 }
452
453 void
reset_width_dependent_items(double pixel_width)454 AudioRegionView::reset_width_dependent_items (double pixel_width)
455 {
456 if (pixel_width == _width) {
457 return;
458 }
459
460 RegionView::reset_width_dependent_items(pixel_width);
461 assert(_pixel_width == pixel_width);
462
463 pending_peak_data->set_x1(pixel_width);
464
465 if (pixel_width <= 20.0 || _height < 5.0 || !trackview.session()->config.get_show_region_fades()) {
466 if (fade_in_handle) { fade_in_handle->hide(); }
467 if (fade_out_handle) { fade_out_handle->hide(); }
468 if (fade_in_trim_handle) { fade_in_trim_handle->hide(); }
469 if (fade_out_trim_handle) { fade_out_trim_handle->hide(); }
470 if (start_xfade_rect) { start_xfade_rect->set_outline (false); }
471 if (end_xfade_rect) { end_xfade_rect->set_outline (false); }
472 }
473
474 reset_fade_shapes ();
475
476 /* Update feature lines */
477 AnalysisFeatureList analysis_features;
478 _region->transients (analysis_features);
479
480 if (feature_lines.size () != analysis_features.size ()) {
481 cerr << "postponed freature line update.\n"; // XXX
482 // AudioRegionView::transients_changed () will pick up on this
483 return;
484 }
485
486 samplepos_t position = _region->position();
487
488 AnalysisFeatureList::const_iterator i;
489 list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator l;
490 double y1;
491 if (_height >= NAME_HIGHLIGHT_THRESH) {
492 y1 = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
493 } else {
494 y1 = _height - 1;
495 }
496 for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
497 float x_pos = trackview.editor().sample_to_pixel ((*i) - position);
498 (*l).first = *i;
499 (*l).second->set (ArdourCanvas::Duple (x_pos, 2.0),
500 ArdourCanvas::Duple (x_pos, y1));
501 }
502 }
503
504 void
region_muted()505 AudioRegionView::region_muted ()
506 {
507 RegionView::region_muted();
508 set_waveform_colors ();
509 }
510
511 void
setup_fade_handle_positions()512 AudioRegionView::setup_fade_handle_positions()
513 {
514 /* position of fade handle offset from the top of the region view */
515 double const handle_pos = 0.0;
516
517 if (fade_in_handle) {
518 fade_in_handle->set_y0 (handle_pos);
519 fade_in_handle->set_y1 (handle_pos + handle_size);
520 }
521
522 if (fade_out_handle) {
523 fade_out_handle->set_y0 (handle_pos);
524 fade_out_handle->set_y1 (handle_pos + handle_size);
525 }
526
527 if (fade_in_trim_handle) {
528 fade_in_trim_handle->set_y0 (_height - handle_size);
529 fade_in_trim_handle->set_y1 (_height);
530 }
531
532 if (fade_out_trim_handle) {
533 fade_out_trim_handle->set_y0 (_height - handle_size );
534 fade_out_trim_handle->set_y1 (_height);
535 }
536 }
537
538 void
set_height(gdouble height)539 AudioRegionView::set_height (gdouble height)
540 {
541 uint32_t gap = UIConfiguration::instance().get_vertical_region_gap ();
542 float ui_scale = UIConfiguration::instance().get_ui_scale ();
543 if (gap > 0 && ui_scale > 0) {
544 gap = ceil (gap * ui_scale);
545 }
546
547 height = std::max (3.0, height - gap);
548
549 if (height == _height) {
550 return;
551 }
552
553 RegionView::set_height (height);
554 pending_peak_data->set_y1 (height);
555
556 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
557 uint32_t nchans = atv.track()->n_channels().n_audio();
558
559 if (!tmp_waves.empty () || !waves.empty ()) {
560
561 gdouble ht;
562
563 if (!UIConfiguration::instance().get_show_name_highlight() || (height < NAME_HIGHLIGHT_THRESH)) {
564 ht = height / (double) nchans;
565 } else {
566 ht = (height - NAME_HIGHLIGHT_SIZE) / (double) nchans;
567 }
568
569 uint32_t wcnt = waves.size();
570 for (uint32_t n = 0; n < wcnt; ++n) {
571 gdouble yoff = floor (ht * n);
572 waves[n]->set_height (ht);
573 waves[n]->set_y_position (yoff);
574 }
575
576 wcnt = tmp_waves.size();
577 for (uint32_t n = 0; n < wcnt; ++n) {
578 if (!tmp_waves[n]) {
579 continue;
580 }
581 gdouble yoff = floor (ht * n);
582 tmp_waves[n]->set_height (ht);
583 tmp_waves[n]->set_y_position (yoff);
584 }
585 }
586
587 if (gain_line) {
588
589 if ((height / nchans) < NAME_HIGHLIGHT_THRESH) {
590 gain_line->hide ();
591 } else {
592 update_envelope_visibility ();
593 }
594
595 gain_line->set_height ((uint32_t) rint (height - NAME_HIGHLIGHT_SIZE) - 2);
596 }
597
598 reset_fade_shapes ();
599
600 /* Update heights for any feature lines */
601 samplepos_t position = _region->position();
602 list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator l;
603 double y1;
604 if (_height >= NAME_HIGHLIGHT_THRESH) {
605 y1 = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
606 } else {
607 y1 = _height - 1;
608 }
609 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
610 float pos_x = trackview.editor().sample_to_pixel((*l).first - position);
611 (*l).second->set (ArdourCanvas::Duple (pos_x, 2.0),
612 ArdourCanvas::Duple (pos_x, y1));
613 }
614
615 if (name_text) {
616 name_text->raise_to_top();
617 }
618
619 setup_fade_handle_positions();
620 }
621
622 void
reset_fade_shapes()623 AudioRegionView::reset_fade_shapes ()
624 {
625 if (!trim_fade_in_drag_active) { reset_fade_in_shape (); }
626 if (!trim_fade_out_drag_active) { reset_fade_out_shape (); }
627 }
628
629 void
reset_fade_in_shape()630 AudioRegionView::reset_fade_in_shape ()
631 {
632 reset_fade_in_shape_width (audio_region(), (samplecnt_t) audio_region()->fade_in()->back()->when);
633 }
634
635 void
reset_fade_in_shape_width(boost::shared_ptr<AudioRegion> ar,samplecnt_t width,bool drag_active)636 AudioRegionView::reset_fade_in_shape_width (boost::shared_ptr<AudioRegion> ar, samplecnt_t width, bool drag_active)
637 {
638 trim_fade_in_drag_active = drag_active;
639 if (fade_in_handle == 0) {
640 return;
641 }
642
643 /* smallest size for a fade is 64 samples */
644
645 width = std::max ((samplecnt_t) 64, width);
646
647 /* round here to prevent little visual glitches with sub-pixel placement */
648 double const pwidth = floor (width / samples_per_pixel);
649 double const handle_left = pwidth;
650
651 /* Put the fade in handle so that its left side is at the end-of-fade line */
652 fade_in_handle->set_x0 (handle_left);
653 fade_in_handle->set_x1 (handle_left + handle_size);
654
655 if (fade_in_trim_handle) {
656 fade_in_trim_handle->set_x0 (0);
657 fade_in_trim_handle->set_x1 (handle_size);
658 }
659
660 if (fade_in_handle->visible()) {
661 //see comment for drag_start
662 entered();
663 }
664
665 if (pwidth < 5) {
666 hide_start_xfade();
667 return;
668 }
669
670 if (!trackview.session()->config.get_show_region_fades()) {
671 hide_start_xfade ();
672 return;
673 }
674
675 double effective_height;
676
677 if (_height >= NAME_HIGHLIGHT_THRESH) {
678 effective_height = _height - NAME_HIGHLIGHT_SIZE;
679 } else {
680 effective_height = _height;
681 }
682
683 /* points *MUST* be in anti-clockwise order */
684
685 Points points;
686 Points::size_type pi;
687 boost::shared_ptr<const Evoral::ControlList> list (audio_region()->fade_in());
688 Evoral::ControlList::const_iterator x;
689 double length = list->length();
690
691 points.assign (list->size(), Duple());
692
693 for (x = list->begin(), pi = 0; x != list->end(); ++x, ++pi) {
694 points[pi].x = (pwidth * ((*x)->when/length));
695 points[pi].y = effective_height - ((*x)->value * (effective_height - 1.));
696 }
697
698 /* draw the line */
699
700 redraw_start_xfade_to (ar, width, points, effective_height, handle_left);
701
702 /* ensure trim handle stays on top */
703 if (frame_handle_start) {
704 frame_handle_start->raise_to_top();
705 }
706 }
707
708 void
reset_fade_out_shape()709 AudioRegionView::reset_fade_out_shape ()
710 {
711 reset_fade_out_shape_width (audio_region(), (samplecnt_t) audio_region()->fade_out()->back()->when);
712 }
713
714 void
reset_fade_out_shape_width(boost::shared_ptr<AudioRegion> ar,samplecnt_t width,bool drag_active)715 AudioRegionView::reset_fade_out_shape_width (boost::shared_ptr<AudioRegion> ar, samplecnt_t width, bool drag_active)
716 {
717 trim_fade_out_drag_active = drag_active;
718 if (fade_out_handle == 0) {
719 return;
720 }
721
722 /* smallest size for a fade is 64 samples */
723
724 width = std::max ((samplecnt_t) 64, width);
725
726
727 double const pwidth = floor(trackview.editor().sample_to_pixel (width));
728
729 /* the right edge should be right on the region frame is the pixel
730 * width is zero. Hence the additional + 1.0 at the end.
731 */
732
733 double const handle_right = rint(trackview.editor().sample_to_pixel (_region->length()) - pwidth);
734 double const trim_handle_right = rint(trackview.editor().sample_to_pixel (_region->length()));
735
736 /* Put the fade out handle so that its right side is at the end-of-fade line;
737 */
738 fade_out_handle->set_x0 (handle_right - handle_size);
739 fade_out_handle->set_x1 (handle_right);
740 if (fade_out_trim_handle) {
741 fade_out_trim_handle->set_x0 (1 + trim_handle_right - handle_size);
742 fade_out_trim_handle->set_x1 (1 + trim_handle_right);
743 }
744
745 if (fade_out_handle->visible()) {
746 //see comment for drag_start
747 entered();
748 }
749 /* don't show shape if its too small */
750
751 if (pwidth < 5) {
752 hide_end_xfade();
753 return;
754 }
755
756 if (!trackview.session()->config.get_show_region_fades()) {
757 hide_end_xfade();
758 return;
759 }
760
761 double effective_height;
762
763 effective_height = _height;
764
765 if (UIConfiguration::instance().get_show_name_highlight() && effective_height >= NAME_HIGHLIGHT_THRESH) {
766 effective_height -= NAME_HIGHLIGHT_SIZE;
767 }
768
769 /* points *MUST* be in anti-clockwise order */
770
771 Points points;
772 Points::size_type pi;
773 boost::shared_ptr<const Evoral::ControlList> list (audio_region()->fade_out());
774 Evoral::ControlList::const_iterator x;
775 double length = list->length();
776
777 points.assign (list->size(), Duple());
778
779 for (x = list->begin(), pi = 0; x != list->end(); ++x, ++pi) {
780 points[pi].x = _pixel_width - pwidth + (pwidth * ((*x)->when/length));
781 points[pi].y = effective_height - ((*x)->value * (effective_height - 1.));
782 }
783
784 /* draw the line */
785
786 redraw_end_xfade_to (ar, width, points, effective_height, handle_right, pwidth);
787
788 /* ensure trim handle stays on top */
789 if (frame_handle_end) {
790 frame_handle_end->raise_to_top();
791 }
792 }
793
794 samplepos_t
get_fade_in_shape_width()795 AudioRegionView::get_fade_in_shape_width ()
796 {
797 return audio_region()->fade_in()->back()->when;
798 }
799
800 samplepos_t
get_fade_out_shape_width()801 AudioRegionView::get_fade_out_shape_width ()
802 {
803 return audio_region()->fade_out()->back()->when;
804 }
805
806
807 void
redraw_start_xfade()808 AudioRegionView::redraw_start_xfade ()
809 {
810 boost::shared_ptr<AudioRegion> ar (audio_region());
811
812 if (!ar->fade_in() || ar->fade_in()->empty()) {
813 return;
814 }
815
816 show_start_xfade();
817 reset_fade_in_shape_width (ar, ar->fade_in()->back()->when);
818 }
819
820 void
redraw_start_xfade_to(boost::shared_ptr<AudioRegion> ar,samplecnt_t,Points & points,double effective_height,double rect_width)821 AudioRegionView::redraw_start_xfade_to (boost::shared_ptr<AudioRegion> ar, samplecnt_t /*width*/, Points& points, double effective_height,
822 double rect_width)
823 {
824 if (points.size() < 2) {
825 return;
826 }
827
828 if (!start_xfade_curve) {
829 start_xfade_curve = new ArdourCanvas::XFadeCurve (group, ArdourCanvas::XFadeCurve::Start);
830 CANVAS_DEBUG_NAME (start_xfade_curve, string_compose ("xfade start out line for %1", region()->name()));
831 start_xfade_curve->set_fill_color (UIConfiguration::instance().color_mod ("active crossfade", "crossfade alpha"));
832 start_xfade_curve->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
833 start_xfade_curve->set_ignore_events (true);
834 }
835 if (!start_xfade_rect) {
836 start_xfade_rect = new ArdourCanvas::Rectangle (group);
837 CANVAS_DEBUG_NAME (start_xfade_rect, string_compose ("xfade start rect for %1", region()->name()));
838 start_xfade_rect->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
839 start_xfade_rect->set_fill (false);
840 start_xfade_rect->set_outline (false);
841 start_xfade_rect->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_start_xfade_event), start_xfade_rect, this));
842 start_xfade_rect->set_data ("regionview", this);
843 }
844
845 start_xfade_rect->set (ArdourCanvas::Rect (0.0, 0.0, rect_width, effective_height));
846
847 /* fade out line */
848
849 boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_in ();
850 Points ipoints;
851 Points::size_type npoints;
852
853 if (!inverse) {
854
855 /* there is no explicit inverse fade in curve, so take the
856 * regular fade in curve given to use as "points" (already a
857 * set of coordinates), and convert to the inverse shape.
858 */
859
860 npoints = points.size();
861 ipoints.assign (npoints, Duple());
862
863 for (Points::size_type i = 0, pci = 0; i < npoints; ++i, ++pci) {
864 ArdourCanvas::Duple &p (ipoints[pci]);
865 /* leave x-axis alone but invert with respect to y-axis */
866 p.y = effective_height - points[pci].y;
867 }
868
869 } else {
870
871 /* there is an explicit inverse fade in curve. Grab the points
872 and convert them into coordinates for the inverse fade in
873 line.
874 */
875
876 npoints = inverse->size();
877 ipoints.assign (npoints, Duple());
878
879 Evoral::ControlList::const_iterator x;
880 Points::size_type pi;
881 double length = inverse->length();
882
883 for (x = inverse->begin(), pi = 0; x != inverse->end(); ++x, ++pi) {
884 ArdourCanvas::Duple& p (ipoints[pi]);
885 p.x = (rect_width * ((*x)->when/length));
886 p.y = effective_height - ((*x)->value * (effective_height));
887 }
888 }
889
890 start_xfade_curve->set_inout (points, ipoints);
891
892 show_start_xfade();
893 }
894
895 void
redraw_end_xfade()896 AudioRegionView::redraw_end_xfade ()
897 {
898 boost::shared_ptr<AudioRegion> ar (audio_region());
899
900 if (!ar->fade_out() || ar->fade_out()->empty()) {
901 return;
902 }
903
904 show_end_xfade();
905
906 reset_fade_out_shape_width (ar, ar->fade_out()->back()->when);
907 }
908
909 void
redraw_end_xfade_to(boost::shared_ptr<AudioRegion> ar,samplecnt_t width,Points & points,double effective_height,double rect_edge,double rect_width)910 AudioRegionView::redraw_end_xfade_to (boost::shared_ptr<AudioRegion> ar, samplecnt_t width, Points& points, double effective_height,
911 double rect_edge, double rect_width)
912 {
913 if (points.size() < 2) {
914 return;
915 }
916
917 if (!end_xfade_curve) {
918 end_xfade_curve = new ArdourCanvas::XFadeCurve (group, ArdourCanvas::XFadeCurve::End);
919 CANVAS_DEBUG_NAME (end_xfade_curve, string_compose ("xfade end out line for %1", region()->name()));
920 end_xfade_curve->set_fill_color (UIConfiguration::instance().color_mod ("active crossfade", "crossfade alpha"));
921 end_xfade_curve->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
922 end_xfade_curve->set_ignore_events (true);
923 }
924
925 if (!end_xfade_rect) {
926 end_xfade_rect = new ArdourCanvas::Rectangle (group);
927 CANVAS_DEBUG_NAME (end_xfade_rect, string_compose ("xfade end rect for %1", region()->name()));
928 end_xfade_rect->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
929 end_xfade_rect->set_fill (false);
930 end_xfade_rect->set_outline (false);
931 end_xfade_rect->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_end_xfade_event), end_xfade_rect, this));
932 end_xfade_rect->set_data ("regionview", this);
933 }
934
935 end_xfade_rect->set (ArdourCanvas::Rect (rect_edge, 0.0, rect_edge + rect_width, effective_height));
936
937 /* fade in line */
938
939 boost::shared_ptr<AutomationList> inverse = ar->inverse_fade_out ();
940 Points ipoints;
941 Points::size_type npoints;
942
943 if (!inverse) {
944
945 /* there is no explicit inverse fade out curve, so take the
946 * regular fade out curve given to use as "points" (already a
947 * set of coordinates), and convert to the inverse shape.
948 */
949
950 npoints = points.size();
951 ipoints.assign (npoints, Duple());
952
953 Points::size_type pci;
954
955 for (pci = 0; pci < npoints; ++pci) {
956 ArdourCanvas::Duple &p (ipoints[pci]);
957 p.y = effective_height - points[pci].y;
958 }
959
960 } else {
961
962 /* there is an explicit inverse fade out curve. Grab the points
963 and convert them into coordinates for the inverse fade out
964 line.
965 */
966
967 npoints = inverse->size();
968 ipoints.assign (npoints, Duple());
969
970 const double rend = trackview.editor().sample_to_pixel (_region->length() - width);
971
972 Evoral::ControlList::const_iterator x;
973 Points::size_type pi;
974 double length = inverse->length();
975
976 for (x = inverse->begin(), pi = 0; x != inverse->end(); ++x, ++pi) {
977 ArdourCanvas::Duple& p (ipoints[pi]);
978 p.x = (rect_width * ((*x)->when/length)) + rend;
979 p.y = effective_height - ((*x)->value * (effective_height));
980 }
981 }
982
983 end_xfade_curve->set_inout (ipoints, points);
984
985 show_end_xfade();
986 }
987
988 void
hide_xfades()989 AudioRegionView::hide_xfades ()
990 {
991 hide_start_xfade ();
992 hide_end_xfade ();
993 }
994
995 void
hide_start_xfade()996 AudioRegionView::hide_start_xfade ()
997 {
998 if (start_xfade_curve) {
999 start_xfade_curve->hide();
1000 }
1001 if (start_xfade_rect) {
1002 start_xfade_rect->hide ();
1003 }
1004
1005 _start_xfade_visible = false;
1006 }
1007
1008 void
hide_end_xfade()1009 AudioRegionView::hide_end_xfade ()
1010 {
1011 if (end_xfade_curve) {
1012 end_xfade_curve->hide();
1013 }
1014 if (end_xfade_rect) {
1015 end_xfade_rect->hide ();
1016 }
1017
1018 _end_xfade_visible = false;
1019 }
1020
1021 void
show_start_xfade()1022 AudioRegionView::show_start_xfade ()
1023 {
1024 if (start_xfade_curve) {
1025 start_xfade_curve->show();
1026 }
1027 if (start_xfade_rect) {
1028 start_xfade_rect->show ();
1029 }
1030
1031 _start_xfade_visible = true;
1032 }
1033
1034 void
show_end_xfade()1035 AudioRegionView::show_end_xfade ()
1036 {
1037 if (end_xfade_curve) {
1038 end_xfade_curve->show();
1039 }
1040 if (end_xfade_rect) {
1041 end_xfade_rect->show ();
1042 }
1043
1044 _end_xfade_visible = true;
1045 }
1046
1047 void
set_samples_per_pixel(gdouble fpp)1048 AudioRegionView::set_samples_per_pixel (gdouble fpp)
1049 {
1050 RegionView::set_samples_per_pixel (fpp);
1051
1052 if (UIConfiguration::instance().get_show_waveforms ()) {
1053 for (uint32_t n = 0; n < waves.size(); ++n) {
1054 waves[n]->set_samples_per_pixel (fpp);
1055 }
1056 }
1057
1058 if (gain_line) {
1059 gain_line->reset ();
1060 }
1061
1062 reset_fade_shapes ();
1063 }
1064
1065 void
set_amplitude_above_axis(gdouble a)1066 AudioRegionView::set_amplitude_above_axis (gdouble a)
1067 {
1068 for (uint32_t n=0; n < waves.size(); ++n) {
1069 waves[n]->set_amplitude_above_axis (a);
1070 }
1071 }
1072
1073 void
set_colors()1074 AudioRegionView::set_colors ()
1075 {
1076 RegionView::set_colors();
1077
1078 if (gain_line) {
1079 gain_line->set_line_color (audio_region()->envelope_active() ?
1080 UIConfiguration::instance().color ("gain line") :
1081 UIConfiguration::instance().color_mod ("gain line inactive", "gain line inactive"));
1082 }
1083
1084 set_waveform_colors ();
1085
1086 if (start_xfade_curve) {
1087 start_xfade_curve->set_fill_color (UIConfiguration::instance().color_mod ("active crossfade", "crossfade alpha"));
1088 start_xfade_curve->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
1089 }
1090 if (end_xfade_curve) {
1091 end_xfade_curve->set_fill_color (UIConfiguration::instance().color_mod ("active crossfade", "crossfade alpha"));
1092 end_xfade_curve->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
1093 }
1094
1095 if (start_xfade_rect) {
1096 start_xfade_rect->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
1097 }
1098 if (end_xfade_rect) {
1099 end_xfade_rect->set_outline_color (UIConfiguration::instance().color ("crossfade line"));
1100 }
1101 }
1102
1103 void
setup_waveform_visibility()1104 AudioRegionView::setup_waveform_visibility ()
1105 {
1106 if (UIConfiguration::instance().get_show_waveforms ()) {
1107 for (uint32_t n = 0; n < waves.size(); ++n) {
1108 /* make sure the zoom level is correct, since we don't update
1109 this when waveforms are hidden.
1110 */
1111 // CAIROCANVAS
1112 // waves[n]->set_samples_per_pixel (_samples_per_pixel);
1113 waves[n]->show();
1114 }
1115 } else {
1116 for (uint32_t n = 0; n < waves.size(); ++n) {
1117 waves[n]->hide();
1118 }
1119 }
1120 }
1121
1122 void
temporarily_hide_envelope()1123 AudioRegionView::temporarily_hide_envelope ()
1124 {
1125 if (gain_line) {
1126 gain_line->hide ();
1127 }
1128 }
1129
1130 void
unhide_envelope()1131 AudioRegionView::unhide_envelope ()
1132 {
1133 update_envelope_visibility ();
1134 }
1135
1136 void
update_envelope_visibility()1137 AudioRegionView::update_envelope_visibility ()
1138 {
1139 if (!gain_line) {
1140 return;
1141 }
1142
1143 if (trackview.editor().current_mouse_mode() == Editing::MouseDraw || trackview.editor().current_mouse_mode() == Editing::MouseContent ) {
1144 gain_line->set_visibility (AutomationLine::VisibleAspects(AutomationLine::ControlPoints|AutomationLine::Line));
1145 gain_line->canvas_group().raise_to_top ();
1146 } else if (UIConfiguration::instance().get_show_region_gain() || trackview.editor().current_mouse_mode() == Editing::MouseRange ) {
1147 gain_line->set_visibility (AutomationLine::VisibleAspects(AutomationLine::Line));
1148 gain_line->canvas_group().raise_to_top ();
1149 } else {
1150 gain_line->set_visibility (AutomationLine::VisibleAspects(0));
1151 }
1152 }
1153
1154 void
delete_waves()1155 AudioRegionView::delete_waves ()
1156 {
1157 for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
1158 delete *i;
1159 }
1160 _data_ready_connections.clear ();
1161
1162 for (vector<ArdourWaveView::WaveView*>::iterator w = waves.begin(); w != waves.end(); ++w) {
1163 group->remove(*w);
1164 delete *w;
1165 }
1166 waves.clear();
1167
1168 while (!tmp_waves.empty ()) {
1169 delete tmp_waves.back ();
1170 tmp_waves.pop_back ();
1171 }
1172 pending_peak_data->show ();
1173 }
1174
1175 void
create_waves()1176 AudioRegionView::create_waves ()
1177 {
1178 // cerr << "AudioRegionView::create_waves() called on " << this << endl;//DEBUG
1179 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
1180
1181 if (!atv.track()) {
1182 return;
1183 }
1184
1185 ChanCount nchans = atv.track()->n_channels();
1186
1187 // cerr << "creating waves for " << _region->name() << " with wfd = " << wait_for_data
1188 // << " and channels = " << nchans.n_audio() << endl;
1189
1190 /* in tmp_waves, set up null pointers for each channel so the vector is allocated */
1191 for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
1192 tmp_waves.push_back (0);
1193 }
1194
1195 for (vector<ScopedConnection*>::iterator i = _data_ready_connections.begin(); i != _data_ready_connections.end(); ++i) {
1196 delete *i;
1197 }
1198
1199 _data_ready_connections.clear ();
1200
1201 for (uint32_t i = 0; i < nchans.n_audio(); ++i) {
1202 _data_ready_connections.push_back (0);
1203 }
1204
1205 for (uint32_t n = 0; n < nchans.n_audio(); ++n) {
1206
1207 if (n >= audio_region()->n_channels()) {
1208 break;
1209 }
1210
1211 // cerr << "\tchannel " << n << endl;
1212
1213 if (wait_for_data) {
1214 if (audio_region()->audio_source(n)->peaks_ready (boost::bind (&AudioRegionView::peaks_ready_handler, this, n), &_data_ready_connections[n], gui_context())) {
1215 // cerr << "\tData is ready for channel " << n << "\n";
1216 create_one_wave (n, true);
1217 } else {
1218 // cerr << "\tdata is not ready for channel " << n << "\n";
1219 // we'll get a PeaksReady signal from the source in the future
1220 // and will call create_one_wave(n) then.
1221 pending_peak_data->show ();
1222 }
1223
1224 } else {
1225 // cerr << "\tdon't delay, display channel " << n << " today!\n";
1226 create_one_wave (n, true);
1227 }
1228
1229 }
1230 }
1231
1232 void
create_one_wave(uint32_t which,bool)1233 AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/)
1234 {
1235 //cerr << "AudioRegionView::create_one_wave() called which: " << which << " this: " << this << endl;//DEBUG
1236 RouteTimeAxisView& atv (*(dynamic_cast<RouteTimeAxisView*>(&trackview))); // ick
1237 if (!trackview.session() || trackview.session()->deletion_in_progress () || !atv.track()) {
1238 /* peaks_ready_handler() may be called from peak_thread_work() while
1239 * session deletion is in progress.
1240 * Since session-unload happens in the GUI thread, we need to test
1241 * in this context.
1242 */
1243 return;
1244 }
1245 uint32_t nchans = atv.track()->n_channels().n_audio();
1246 uint32_t n;
1247 uint32_t nwaves = std::min (nchans, audio_region()->n_channels());
1248 gdouble ht;
1249
1250 /* compare to set_height(), use _height as set by streamview (child_height),
1251 * not trackview.current_height() to take stacked layering into acconnt
1252 */
1253 if (!UIConfiguration::instance().get_show_name_highlight() || (_height < NAME_HIGHLIGHT_THRESH)) {
1254 ht = _height / (double) nchans;
1255 } else {
1256 ht = (_height - NAME_HIGHLIGHT_SIZE) / (double) nchans;
1257 }
1258
1259 /* first waveview starts at 1.0, not 0.0 since that will overlap the frame */
1260 gdouble yoff = which * ht;
1261
1262 ArdourWaveView::WaveView *wave = new ArdourWaveView::WaveView (group, audio_region ());
1263 CANVAS_DEBUG_NAME (wave, string_compose ("wave view for chn %1 of %2", which, get_item_name()));
1264
1265 wave->set_channel (which);
1266 wave->set_y_position (yoff);
1267 wave->set_height (ht);
1268 wave->set_samples_per_pixel (samples_per_pixel);
1269 wave->set_show_zero_line (true);
1270 wave->set_clip_level (UIConfiguration::instance().get_waveform_clip_level ());
1271 wave->set_start_shift (1.0);
1272
1273 wave->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_wave_view_event), wave, this));
1274
1275 switch (UIConfiguration::instance().get_waveform_shape()) {
1276 case Rectified:
1277 wave->set_shape (ArdourWaveView::WaveView::Rectified);
1278 break;
1279 default:
1280 wave->set_shape (ArdourWaveView::WaveView::Normal);
1281 }
1282
1283 wave->set_logscaled (UIConfiguration::instance().get_waveform_scale() == Logarithmic);
1284
1285 vector<ArdourWaveView::WaveView*> v;
1286 v.push_back (wave);
1287 set_some_waveform_colors (v);
1288
1289 if (!UIConfiguration::instance().get_show_waveforms ()) {
1290 wave->hide();
1291 }
1292
1293 /* note: calling this function is serialized by the lock
1294 held in the peak building thread that signals that
1295 peaks are ready for use *or* by the fact that it is
1296 called one by one from the GUI thread.
1297 */
1298
1299 if (which < nchans) {
1300 tmp_waves[which] = wave;
1301 } else {
1302 /* n-channel track, >n-channel source */
1303 }
1304
1305 /* see if we're all ready */
1306
1307 for (n = 0; n < nchans; ++n) {
1308 if (tmp_waves[n] == 0) {
1309 break;
1310 }
1311 }
1312
1313 if (n == nwaves) {
1314 /* all waves are ready */
1315 tmp_waves.resize(nwaves);
1316
1317 waves.swap(tmp_waves);
1318
1319 while (!tmp_waves.empty ()) {
1320 delete tmp_waves.back ();
1321 tmp_waves.pop_back ();
1322 }
1323
1324 /* indicate peak-completed */
1325 pending_peak_data->hide ();
1326
1327 /* Restore stacked coverage */
1328 LayerDisplay layer_display;
1329 if (trackview.get_gui_property ("layer-display", layer_display)) {
1330 update_coverage_frame (layer_display);
1331 }
1332 }
1333
1334 /* channel wave created, don't hook into peaks ready anymore */
1335 delete _data_ready_connections[which];
1336 _data_ready_connections[which] = 0;
1337
1338 maybe_raise_cue_markers ();
1339 }
1340
1341 void
peaks_ready_handler(uint32_t which)1342 AudioRegionView::peaks_ready_handler (uint32_t which)
1343 {
1344 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AudioRegionView::create_one_wave, this, which, false));
1345 // cerr << "AudioRegionView::peaks_ready_handler() called on " << which << " this: " << this << endl;
1346 }
1347
1348 void
add_gain_point_event(ArdourCanvas::Item * item,GdkEvent * ev,bool with_guard_points)1349 AudioRegionView::add_gain_point_event (ArdourCanvas::Item *item, GdkEvent *ev, bool with_guard_points)
1350 {
1351 if (!gain_line) {
1352 return;
1353 }
1354
1355 uint32_t before_p, after_p;
1356 double mx = ev->button.x;
1357 double my = ev->button.y;
1358
1359 item->canvas_to_item (mx, my);
1360
1361 samplecnt_t const sample_within_region = (samplecnt_t) floor (mx * samples_per_pixel);
1362
1363 if (!gain_line->control_points_adjacent (sample_within_region, before_p, after_p)) {
1364 /* no adjacent points */
1365 return;
1366 }
1367
1368 /* y is in item frame */
1369 double const bx = gain_line->nth (before_p)->get_x();
1370 double const ax = gain_line->nth (after_p)->get_x();
1371 double const click_ratio = (ax - mx) / (ax - bx);
1372
1373 double y = ((gain_line->nth (before_p)->get_y() * click_ratio) + (gain_line->nth (after_p)->get_y() * (1 - click_ratio)));
1374
1375 /* don't create points that can't be seen */
1376
1377 update_envelope_visibility ();
1378 samplepos_t rpos = region ()->position ();
1379 MusicSample snap_pos (trackview.editor().pixel_to_sample (mx) + rpos, 0);
1380 trackview.editor ().snap_to_with_modifier (snap_pos, ev);
1381 samplepos_t fx = snap_pos.sample - rpos;
1382
1383 if (fx > _region->length()) {
1384 return;
1385 }
1386
1387 /* compute vertical fractional position */
1388
1389 y = 1.0 - (y / (gain_line->height()));
1390
1391 /* map using gain line */
1392
1393 gain_line->view_to_model_coord (mx, y);
1394
1395 /* XXX STATEFUL: can't convert to stateful diff until we
1396 can represent automation data with it.
1397 */
1398
1399 XMLNode &before = audio_region()->envelope()->get_state();
1400 MementoCommand<AudioRegion>* region_memento = 0;
1401
1402 if (!audio_region()->envelope_active()) {
1403 XMLNode ®ion_before = audio_region()->get_state();
1404 audio_region()->set_envelope_active(true);
1405 XMLNode ®ion_after = audio_region()->get_state();
1406 region_memento = new MementoCommand<AudioRegion>(*(audio_region().get()), ®ion_before, ®ion_after);
1407 }
1408
1409 if (audio_region()->envelope()->editor_add (fx, y, with_guard_points)) {
1410 XMLNode &after = audio_region()->envelope()->get_state();
1411 std::list<Selectable*> results;
1412
1413 trackview.editor().begin_reversible_command (_("add gain control point"));
1414
1415 if (region_memento) {
1416 trackview.session()->add_command (region_memento);
1417 }
1418
1419 trackview.session()->add_command (new MementoCommand<AutomationList>(*audio_region()->envelope().get(), &before, &after));
1420
1421 gain_line->get_selectables (fx + region ()->position (), fx + region ()->position (), 0.0, 1.0, results);
1422 trackview.editor ().get_selection ().set (results);
1423
1424 trackview.editor ().commit_reversible_command ();
1425 trackview.session ()->set_dirty ();
1426 } else {
1427 delete region_memento;
1428 }
1429 }
1430
1431 void
remove_gain_point_event(ArdourCanvas::Item * item,GdkEvent *)1432 AudioRegionView::remove_gain_point_event (ArdourCanvas::Item *item, GdkEvent* /*ev*/)
1433 {
1434 ControlPoint *cp = reinterpret_cast<ControlPoint *> (item->get_data ("control_point"));
1435 audio_region()->envelope()->erase (cp->model());
1436 }
1437
1438 GhostRegion*
add_ghost(TimeAxisView & tv)1439 AudioRegionView::add_ghost (TimeAxisView& tv)
1440 {
1441 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&trackview);
1442
1443 if (!rtv) {
1444 return 0;
1445 }
1446
1447 double unit_position = _region->position () / samples_per_pixel;
1448 AudioGhostRegion* ghost = new AudioGhostRegion (*this, tv, trackview, unit_position);
1449 uint32_t nchans;
1450
1451 nchans = rtv->track()->n_channels().n_audio();
1452
1453 for (uint32_t n = 0; n < nchans; ++n) {
1454
1455 if (n >= audio_region()->n_channels()) {
1456 break;
1457 }
1458
1459 ArdourWaveView::WaveView *wave = new ArdourWaveView::WaveView (ghost->group, audio_region());
1460 CANVAS_DEBUG_NAME (wave, string_compose ("ghost wave for %1", get_item_name()));
1461
1462 wave->set_channel (n);
1463 wave->set_samples_per_pixel (samples_per_pixel);
1464 wave->set_amplitude_above_axis (_amplitude_above_axis);
1465
1466 ghost->waves.push_back(wave);
1467 }
1468
1469 ghost->set_height ();
1470 ghost->set_duration (_region->length() / samples_per_pixel);
1471 ghost->set_colors();
1472 ghosts.push_back (ghost);
1473
1474 return ghost;
1475 }
1476
1477 void
entered()1478 AudioRegionView::entered ()
1479 {
1480 trackview.editor().set_current_trimmable (_region);
1481 trackview.editor().set_current_movable (_region);
1482
1483 update_envelope_visibility();
1484
1485 if ((trackview.editor().current_mouse_mode() == Editing::MouseObject)) {
1486 if (start_xfade_rect) {
1487 start_xfade_rect->set_outline (true);
1488 }
1489 if (end_xfade_rect) {
1490 end_xfade_rect->set_outline (true);
1491 }
1492 if (fade_in_handle) {
1493 fade_in_handle->show ();
1494 fade_in_handle->raise_to_top ();
1495 }
1496 if (fade_out_handle) {
1497 fade_out_handle->show ();
1498 fade_out_handle->raise_to_top ();
1499 }
1500 if (fade_in_trim_handle) {
1501 boost::shared_ptr<AudioRegion> ar (audio_region());
1502 if (!ar->locked() && (ar->fade_in()->back()->when > 64 || (ar->can_trim() & Trimmable::FrontTrimEarlier))) {
1503 fade_in_trim_handle->show ();
1504 fade_in_trim_handle->raise_to_top ();
1505 } else {
1506 fade_in_trim_handle->hide ();
1507 }
1508 }
1509 if (fade_out_trim_handle) {
1510 boost::shared_ptr<AudioRegion> ar (audio_region());
1511 if (!ar->locked() && (ar->fade_out()->back()->when > 64 || (ar->can_trim() & Trimmable::EndTrimLater))) {
1512 fade_out_trim_handle->show ();
1513 fade_out_trim_handle->raise_to_top ();
1514 } else {
1515 fade_out_trim_handle->hide ();
1516 }
1517 }
1518 } else { //this happens when we switch tools; if we switch away from Grab mode, hide all the fade handles
1519 if (fade_in_handle) { fade_in_handle->hide(); }
1520 if (fade_out_handle) { fade_out_handle->hide(); }
1521 if (fade_in_trim_handle) { fade_in_trim_handle->hide(); }
1522 if (fade_out_trim_handle) { fade_out_trim_handle->hide(); }
1523 if (start_xfade_rect) { start_xfade_rect->set_outline (false); }
1524 if (end_xfade_rect) { end_xfade_rect->set_outline (false); }
1525 }
1526 }
1527
1528 void
exited()1529 AudioRegionView::exited ()
1530 {
1531 trackview.editor().set_current_trimmable (boost::shared_ptr<Trimmable>());
1532 trackview.editor().set_current_movable (boost::shared_ptr<Movable>());
1533
1534 // if (gain_line) {
1535 // gain_line->remove_visibility (AutomationLine::ControlPoints);
1536 // }
1537
1538 if (fade_in_handle) { fade_in_handle->hide(); }
1539 if (fade_out_handle) { fade_out_handle->hide(); }
1540 if (fade_in_trim_handle) { fade_in_trim_handle->hide(); }
1541 if (fade_out_trim_handle) { fade_out_trim_handle->hide(); }
1542 if (start_xfade_rect) { start_xfade_rect->set_outline (false); }
1543 if (end_xfade_rect) { end_xfade_rect->set_outline (false); }
1544 }
1545
1546 void
envelope_active_changed()1547 AudioRegionView::envelope_active_changed ()
1548 {
1549 if (gain_line) {
1550 gain_line->set_line_color (audio_region()->envelope_active() ?
1551 UIConfiguration::instance().color ("gain line") :
1552 UIConfiguration::instance().color_mod ("gain line inactive", "gain line inactive"));
1553 update_envelope_visibility ();
1554 }
1555 }
1556
1557 void
color_handler()1558 AudioRegionView::color_handler ()
1559 {
1560 //case cMutedWaveForm:
1561 //case cWaveForm:
1562 //case cWaveFormClip:
1563 //case cZeroLine:
1564 set_colors ();
1565
1566 //case cGainLineInactive:
1567 //case cGainLine:
1568 envelope_active_changed();
1569
1570 }
1571
1572 void
set_waveform_colors()1573 AudioRegionView::set_waveform_colors ()
1574 {
1575 set_some_waveform_colors (waves);
1576 }
1577
1578 void
set_some_waveform_colors(vector<ArdourWaveView::WaveView * > & waves_to_color)1579 AudioRegionView::set_some_waveform_colors (vector<ArdourWaveView::WaveView*>& waves_to_color)
1580 {
1581 Gtkmm2ext::Color fill = fill_color;
1582 Gtkmm2ext::Color outline = fill;
1583
1584 Gtkmm2ext::Color clip = UIConfiguration::instance().color ("clipped waveform");
1585 Gtkmm2ext::Color zero = UIConfiguration::instance().color ("zero line");
1586
1587 /* use track/region color to fill wform */
1588 fill = fill_color;
1589 fill = UINT_INTERPOLATE (fill, UIConfiguration::instance().color ("waveform fill"), 0.5);
1590
1591 /* set outline */
1592 outline = UIConfiguration::instance().color ("waveform outline");
1593
1594 if (_selected) {
1595 outline = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("selected waveform outline"), 0xC0);
1596 fill = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("selected waveform fill"), 0xC0);
1597 } else if (_dragging) {
1598 outline = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("waveform outline"), 0xC0);
1599 fill = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("waveform fill"), 0xC0);
1600 } else if (_region->muted()) {
1601 outline = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("waveform outline"), 80);
1602 fill = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("waveform fill"), 0);
1603 } else if (!_region->opaque()) {
1604 outline = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("waveform outline"), 70);
1605 fill = UINT_RGBA_CHANGE_A(UIConfiguration::instance().color ("waveform fill"), 70);
1606 }
1607
1608 /* recorded region, override to red */
1609 if (_recregion) {
1610 outline = UIConfiguration::instance().color ("recording waveform outline");
1611 fill = UIConfiguration::instance().color ("recording waveform fill");
1612 }
1613
1614 for (vector<ArdourWaveView::WaveView*>::iterator w = waves_to_color.begin(); w != waves_to_color.end(); ++w) {
1615 (*w)->set_fill_color (fill);
1616 (*w)->set_outline_color (outline);
1617 (*w)->set_clip_color (clip);
1618 (*w)->set_zero_color (zero);
1619 }
1620 }
1621
1622 void
set_frame_color()1623 AudioRegionView::set_frame_color ()
1624 {
1625 if (!frame) {
1626 return;
1627 }
1628
1629 RegionView::set_frame_color ();
1630
1631 set_waveform_colors ();
1632 }
1633
1634 void
set_fade_visibility(bool yn)1635 AudioRegionView::set_fade_visibility (bool yn)
1636 {
1637 if (yn) {
1638 if (start_xfade_curve) { start_xfade_curve->show (); }
1639 if (end_xfade_curve) { end_xfade_curve->show (); }
1640 if (start_xfade_rect) { start_xfade_rect->show (); }
1641 if (end_xfade_rect) { end_xfade_rect->show (); }
1642 } else {
1643 if (start_xfade_curve) { start_xfade_curve->hide(); }
1644 if (end_xfade_curve) { end_xfade_curve->hide(); }
1645 if (fade_in_handle) { fade_in_handle->hide(); }
1646 if (fade_out_handle) { fade_out_handle->hide(); }
1647 if (fade_in_trim_handle) { fade_in_trim_handle->hide(); }
1648 if (fade_out_trim_handle) { fade_out_trim_handle->hide(); }
1649 if (start_xfade_rect) { start_xfade_rect->hide (); }
1650 if (end_xfade_rect) { end_xfade_rect->hide (); }
1651 if (start_xfade_rect) { start_xfade_rect->set_outline (false); }
1652 if (end_xfade_rect) { end_xfade_rect->set_outline (false); }
1653 }
1654 }
1655
1656 void
update_coverage_frame(LayerDisplay d)1657 AudioRegionView::update_coverage_frame (LayerDisplay d)
1658 {
1659 RegionView::update_coverage_frame (d);
1660
1661 if (d == Stacked) {
1662 if (fade_in_handle) { fade_in_handle->raise_to_top (); }
1663 if (fade_out_handle) { fade_out_handle->raise_to_top (); }
1664 if (fade_in_trim_handle) { fade_in_trim_handle->raise_to_top (); }
1665 if (fade_out_trim_handle) { fade_out_trim_handle->raise_to_top (); }
1666 }
1667 }
1668
1669 void
show_region_editor()1670 AudioRegionView::show_region_editor ()
1671 {
1672 if (editor == 0) {
1673 editor = new AudioRegionEditor (trackview.session(), audio_region());
1674 }
1675
1676 editor->present ();
1677 editor->show_all();
1678 }
1679
1680 void
transients_changed()1681 AudioRegionView::transients_changed ()
1682 {
1683 AnalysisFeatureList analysis_features;
1684 _region->transients (analysis_features);
1685 samplepos_t position = _region->position();
1686 samplepos_t first = _region->first_sample();
1687 samplepos_t last = _region->last_sample();
1688
1689 double y1;
1690 if (_height >= NAME_HIGHLIGHT_THRESH) {
1691 y1 = _height - TimeAxisViewItem::NAME_HIGHLIGHT_SIZE - 1;
1692 } else {
1693 y1 = _height - 1;
1694 }
1695
1696 while (feature_lines.size() < analysis_features.size()) {
1697 ArdourCanvas::Line* canvas_item = new ArdourCanvas::Line(group);
1698 CANVAS_DEBUG_NAME (canvas_item, string_compose ("transient group for %1", region()->name()));
1699 canvas_item->set_outline_color (UIConfiguration::instance().color ("zero line")); // also in Editor::leave_handler()
1700
1701 canvas_item->set (ArdourCanvas::Duple (-1.0, 2.0),
1702 ArdourCanvas::Duple (1.0, y1));
1703
1704 canvas_item->raise_to_top ();
1705 canvas_item->show ();
1706
1707 canvas_item->set_data ("regionview", this);
1708 canvas_item->Event.connect (sigc::bind (sigc::mem_fun (PublicEditor::instance(), &PublicEditor::canvas_feature_line_event), canvas_item, this));
1709
1710 feature_lines.push_back (make_pair(0, canvas_item));
1711 }
1712
1713 while (feature_lines.size() > analysis_features.size()) {
1714 ArdourCanvas::Line* line = feature_lines.back().second;
1715 feature_lines.pop_back ();
1716 delete line;
1717 }
1718
1719 AnalysisFeatureList::const_iterator i;
1720 list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator l;
1721
1722 for (i = analysis_features.begin(), l = feature_lines.begin(); i != analysis_features.end() && l != feature_lines.end(); ++i, ++l) {
1723
1724 float *pos = new float;
1725 *pos = trackview.editor().sample_to_pixel (*i - position);
1726
1727 (*l).second->set (
1728 ArdourCanvas::Duple (*pos, 2.0),
1729 ArdourCanvas::Duple (*pos, y1)
1730 );
1731
1732 (*l).second->set_data ("position", pos); // is this *modified* (drag?), if not use *i
1733 (*l).first = *i;
1734
1735 if (l->first < first || l->first >= last) {
1736 l->second->hide();
1737 } else {
1738 l->second->show();
1739 }
1740 }
1741 }
1742
1743 void
update_transient(float,float new_pos)1744 AudioRegionView::update_transient(float /*old_pos*/, float new_pos)
1745 {
1746 /* Find sample at old pos, calulate new sample then update region transients*/
1747 list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator l;
1748
1749 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1750
1751 /* Line has been updated in drag so we compare to new_pos */
1752
1753 float* pos = (float*) (*l).second->get_data ("position");
1754
1755 if (rint(new_pos) == rint(*pos)) {
1756 samplepos_t position = _region->position();
1757 samplepos_t old_sample = (*l).first;
1758 samplepos_t new_sample = trackview.editor().pixel_to_sample (new_pos) + position;
1759 _region->update_transient (old_sample, new_sample);
1760 break;
1761 }
1762 }
1763 }
1764
1765 void
remove_transient(float pos)1766 AudioRegionView::remove_transient (float pos)
1767 {
1768 /* this is called from Editor::remove_transient () with pos == get_data ("position")
1769 * which is the item's x-coordinate inside the ARV.
1770 *
1771 * Find sample at old pos, calulate new sample then update region transients
1772 */
1773 list<std::pair<samplepos_t, ArdourCanvas::Line*> >::iterator l;
1774
1775 for (l = feature_lines.begin(); l != feature_lines.end(); ++l) {
1776 float *line_pos = (float*) (*l).second->get_data ("position");
1777 if (rint(pos) == rint(*line_pos)) {
1778 _region->remove_transient ((*l).first);
1779 break;
1780 }
1781 }
1782 }
1783
1784 void
thaw_after_trim()1785 AudioRegionView::thaw_after_trim ()
1786 {
1787 RegionView::thaw_after_trim ();
1788 unhide_envelope ();
1789 drag_end ();
1790 }
1791
1792
1793 void
show_xfades()1794 AudioRegionView::show_xfades ()
1795 {
1796 show_start_xfade ();
1797 show_end_xfade ();
1798 }
1799
1800 void
drag_start()1801 AudioRegionView::drag_start ()
1802 {
1803 TimeAxisViewItem::drag_start ();
1804
1805 //we used to hide xfades here. I don't see the point with the new model, but we can re-implement if needed
1806 }
1807
1808 void
drag_end()1809 AudioRegionView::drag_end ()
1810 {
1811 TimeAxisViewItem::drag_end ();
1812 //see comment for drag_start
1813
1814 if (fade_in_handle && fade_in_handle->visible()) {
1815 // lenght of region or fade changed, re-check
1816 // if fade_in_trim_handle or fade_out_trim_handle should
1817 // be visible. -- If the fade_in_handle is visible
1818 // we have focus and are not in internal edit mode.
1819 entered();
1820 }
1821 }
1822
1823 void
parameter_changed(string const & p)1824 AudioRegionView::parameter_changed (string const & p)
1825 {
1826 RegionView::parameter_changed (p);
1827 if (p == "show-waveforms") {
1828 setup_waveform_visibility ();
1829 }
1830 }
1831