Fix MIDI rec regions.
[ardour.git] / gtk2_ardour / audio_streamview.cc
1 /*
2     Copyright (C) 2001, 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <cmath>
20 #include <cassert>
21 #include <utility>
22
23 #include <gtkmm.h>
24
25 #include <gtkmm2ext/gtk_ui.h>
26
27 #include "ardour/audioplaylist.h"
28 #include "ardour/audioregion.h"
29 #include "ardour/audiofilesource.h"
30 #include "ardour/audio_diskstream.h"
31 #include "ardour/audio_track.h"
32 #include "ardour/source.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/profile.h"
35
36 #include "audio_streamview.h"
37 #include "audio_region_view.h"
38 #include "tape_region_view.h"
39 #include "audio_time_axis.h"
40 #include "canvas-waveview.h"
41 #include "canvas-simplerect.h"
42 #include "region_selection.h"
43 #include "selection.h"
44 #include "public_editor.h"
45 #include "ardour_ui.h"
46 #include "crossfade_view.h"
47 #include "rgb_macros.h"
48 #include "gui_thread.h"
49 #include "utils.h"
50
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56 using namespace Editing;
57
58 AudioStreamView::AudioStreamView (AudioTimeAxisView& tv)
59         : StreamView (tv)
60 {
61         crossfades_visible = true;
62         color_handler ();
63         _amplitude_above_axis = 1.0;
64
65         use_rec_regions = tv.editor().show_waveforms_recording ();
66
67         Config->ParameterChanged.connect (sigc::mem_fun (*this, &AudioStreamView::parameter_changed));
68 }
69
70 AudioStreamView::~AudioStreamView ()
71 {
72 }
73
74 int
75 AudioStreamView::set_samples_per_unit (gdouble spp)
76 {
77         StreamView::set_samples_per_unit(spp);
78
79         for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
80                 xi->second->set_samples_per_unit (spp);
81         }
82
83         return 0;
84 }
85
86 int
87 AudioStreamView::set_amplitude_above_axis (gdouble app)
88 {
89         RegionViewList::iterator i;
90
91         if (app < 1.0) {
92                 return -1;
93         }
94
95         _amplitude_above_axis = app;
96
97         for (i = region_views.begin(); i != region_views.end(); ++i) {
98                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
99                 if (arv)
100                         arv->set_amplitude_above_axis (app);
101         }
102
103         return 0;
104 }
105
106 RegionView*
107 AudioStreamView::create_region_view (boost::shared_ptr<Region> r, bool wait_for_waves, bool recording)
108 {
109         AudioRegionView *region_view = 0;
110         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
111
112         if (region == 0) {
113                 return 0;
114         }
115
116         switch (_trackview.audio_track()->mode()) {
117
118         case NonLayered:
119         case Normal:
120                 if (recording) {
121                         region_view = new AudioRegionView (canvas_group, _trackview, region,
122                                         _samples_per_unit, region_color, recording, TimeAxisViewItem::Visibility(
123                                                         TimeAxisViewItem::ShowFrame | TimeAxisViewItem::HideFrameRight));
124                 } else {
125                         region_view = new AudioRegionView (canvas_group, _trackview, region,
126                                         _samples_per_unit, region_color);
127                 }
128                 break;
129         case Destructive:
130                 region_view = new TapeAudioRegionView (canvas_group, _trackview, region,
131                                 _samples_per_unit, region_color);
132                 break;
133         default:
134                 fatal << string_compose (_("programming error: %1"), "illegal track mode in ::add_region_view_internal") << endmsg;
135                 /*NOTREACHED*/
136
137         }
138
139         region_view->init (region_color, wait_for_waves);
140         region_view->set_amplitude_above_axis(_amplitude_above_axis);
141         region_view->set_height (child_height ());
142
143         /* if its the special single-sample length that we use for rec-regions, make it
144            insensitive to events
145         */
146
147         if (region->length() == 1) {
148                 region_view->set_sensitive (false);
149         }
150
151         region_view->set_waveform_scale (Config->get_waveform_scale ());
152         region_view->set_waveform_shape (Config->get_waveform_shape ());
153         region_view->set_waveform_visible (Config->get_show_waveforms ());
154
155         return region_view;
156 }
157
158 RegionView*
159 AudioStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wait_for_waves, bool recording)
160 {
161         RegionView *region_view = create_region_view (r, wait_for_waves, recording);
162         if (region_view == 0) {
163                 return 0;
164         }
165
166 //      if(!recording){
167 //              for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
168 //                      if ((*i)->region() == r) {
169 //                              cerr << "audio_streamview in add_region_view_internal region found" << endl;
170                                 /* great. we already have a AudioRegionView for this Region. use it again. */
171
172 //                              (*i)->set_valid (true);
173
174                                 // this might not be necessary
175 //                              AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
176
177 //                              if (arv) {
178 //                                      arv->set_waveform_scale (_waveform_scale);
179 //                                      arv->set_waveform_shape (_waveform_shape);
180 //                              }
181
182 //                              return NULL;
183 //                      }
184 //              }
185 //      }
186
187         region_views.push_front (region_view);
188
189         /* catch regionview going away */
190         r->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_region_view), boost::weak_ptr<Region> (r)));
191
192         RegionViewAdded (region_view);
193
194         return region_view;
195 }
196
197 void
198 AudioStreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
199 {
200         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), weak_r));
201
202         boost::shared_ptr<Region> r (weak_r.lock());
203
204         if (!r) {
205                 return;
206         }
207
208         if (!_trackview.session().deletion_in_progress()) {
209
210                 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
211                         CrossfadeViewList::iterator tmp;
212
213                         tmp = i;
214                         ++tmp;
215
216                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
217                         if (ar && i->second->crossfade->involves (ar)) {
218                                 delete i->second;
219                                 crossfade_views.erase (i);
220                         }
221
222                         i = tmp;
223                 }
224         }
225
226         StreamView::remove_region_view(r);
227 }
228
229 void
230 AudioStreamView::undisplay_diskstream ()
231 {
232         StreamView::undisplay_diskstream();
233
234         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
235                 delete i->second;
236         }
237
238         crossfade_views.clear ();
239 }
240
241 void
242 AudioStreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
243 {
244         boost::shared_ptr<Diskstream> sp (ds.lock());
245         if (sp) {
246                 playlist_modified (sp);
247         }
248 }
249
250 void
251 AudioStreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
252 {
253         /* we do not allow shared_ptr<T> to be bound to slots */
254         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_modified_weak), ds));
255
256         StreamView::playlist_modified (ds);
257
258         /* make sure xfades are on top and all the regionviews are stacked correctly. */
259
260         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
261                 i->second->get_canvas_group()->raise_to_top();
262         }
263 }
264
265 void
266 AudioStreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
267 {
268         boost::shared_ptr<Diskstream> sp (ds.lock());
269         if (sp) {
270                 playlist_changed (sp);
271         }
272 }
273
274 void
275 AudioStreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
276 {
277         ENSURE_GUI_THREAD (bind (
278                         mem_fun (*this, &AudioStreamView::playlist_changed_weak),
279                         boost::weak_ptr<Diskstream> (ds)));
280
281         StreamView::playlist_changed(ds);
282
283         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(ds->playlist());
284         if (apl) {
285                 playlist_connections.push_back (apl->NewCrossfade.connect (
286                                 mem_fun (*this, &AudioStreamView::add_crossfade)));
287         }
288 }
289
290 void
291 AudioStreamView::add_crossfade_weak (boost::weak_ptr<Crossfade> crossfade)
292 {
293         boost::shared_ptr<Crossfade> sp (crossfade.lock());
294
295         if (!sp) {
296                 return;
297         }
298
299         add_crossfade (sp);
300 }
301
302 void
303 AudioStreamView::add_crossfade (boost::shared_ptr<Crossfade> crossfade)
304 {
305         AudioRegionView* lview = 0;
306         AudioRegionView* rview = 0;
307
308         /* we do not allow shared_ptr<T> to be bound to slots */
309
310         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade_weak), boost::weak_ptr<Crossfade> (crossfade)));
311
312         /* first see if we already have a CrossfadeView for this Crossfade */
313
314         CrossfadeViewList::iterator i = crossfade_views.find (crossfade);
315         if (i != crossfade_views.end()) {
316                 if (!crossfades_visible) {
317                         i->second->hide();
318                 } else {
319                         i->second->show ();
320                 }
321                 i->second->set_valid (true);
322                 return;
323         }
324
325         /* create a new one */
326
327         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
328                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
329
330                 if (!lview && arv && (arv->region() == crossfade->out())) {
331                         lview = arv;
332                 }
333                 if (!rview && arv && (arv->region() == crossfade->in())) {
334                         rview = arv;
335                 }
336         }
337
338         CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display (),
339                                                _trackview,
340                                                 crossfade,
341                                                _samples_per_unit,
342                                                region_color,
343                                                *lview, *rview);
344         cv->set_valid (true);
345         crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
346         crossfade_views[cv->crossfade] = cv;
347         if (!_trackview.session().config.get_xfades_visible() || !crossfades_visible) {
348                 cv->hide ();
349         }
350
351         update_content_height (cv);
352 }
353
354 void
355 AudioStreamView::remove_crossfade (boost::shared_ptr<Region> r)
356 {
357         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), r));
358
359         boost::shared_ptr<Crossfade> xfade = boost::dynamic_pointer_cast<Crossfade> (r);
360
361         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
362                 if (i->second->crossfade == xfade) {
363                         delete i->second;
364                         crossfade_views.erase (i);
365                         break;
366                 }
367         }
368 }
369
370 void
371 AudioStreamView::redisplay_diskstream ()
372 {
373         list<RegionView *>::iterator i;
374         CrossfadeViewList::iterator xi, tmpx;
375
376         // Flag region views as invalid and disable drawing
377         for (i = region_views.begin(); i != region_views.end(); ++i) {
378                 (*i)->set_valid (false);
379                 (*i)->enable_display (false);
380         }
381
382         // Flag crossfade views as invalid
383         for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
384                 xi->second->set_valid (false);
385                 if (xi->second->visible()) {
386                         xi->second->show ();
387                 }
388         }
389
390         // Add and display region and crossfade views, and flag them as valid
391
392         if (_trackview.is_audio_track()) {
393                 _trackview.get_diskstream()->playlist()->foreach_region(
394                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
395                         );
396
397                 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(
398                                 _trackview.get_diskstream()->playlist()
399                         );
400
401                 if (apl) {
402                         apl->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade));
403                 }
404         }
405
406         // Remove invalid crossfade views
407         for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
408                 tmpx = xi;
409                 tmpx++;
410
411                 if (!xi->second->valid()) {
412                         delete xi->second;
413                         crossfade_views.erase (xi);
414                 }
415
416                 xi = tmpx;
417         }
418
419         // Stack regions by layer, and remove invalid regions
420         layer_regions();
421 }
422
423 void
424 AudioStreamView::set_show_waveforms (bool yn)
425 {
426         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
427                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
428                 if (arv) {
429                         arv->set_waveform_visible (yn);
430                 }
431         }
432 }
433
434 void
435 AudioStreamView::set_waveform_shape (WaveformShape shape)
436 {
437         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
438                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
439                 if (arv)
440                         arv->set_waveform_shape (shape);
441         }
442 }
443
444 void
445 AudioStreamView::set_waveform_scale (WaveformScale scale)
446 {
447         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
448                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
449                 if (arv) {
450                         arv->set_waveform_scale (scale);
451                 }
452         }
453 }
454
455 void
456 AudioStreamView::setup_rec_box ()
457 {
458         //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
459
460         if (_trackview.session().transport_rolling()) {
461
462                 // cerr << "\trolling\n";
463
464                 if (!rec_active &&
465                     _trackview.session().record_status() == Session::Recording &&
466                     _trackview.get_diskstream()->record_enabled()) {
467                         if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
468
469                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
470
471                                 SourceList sources;
472
473                                 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin();
474                                                 prc != rec_data_ready_connections.end(); ++prc) {
475                                         (*prc).disconnect();
476                                 }
477                                 rec_data_ready_connections.clear();
478
479                                 boost::shared_ptr<AudioDiskstream> ads = _trackview.audio_track()->audio_diskstream();
480
481                                 for (uint32_t n=0; n < ads->n_channels().n_audio(); ++n) {
482                                         boost::shared_ptr<AudioFileSource> src = ads->write_source (n);
483                                         if (src) {
484                                                 sources.push_back (src);
485
486                                                 rec_data_ready_connections.push_back (src->PeakRangeReady.connect (bind
487                                                         (mem_fun (*this, &AudioStreamView::rec_peak_range_ready),
488                                                          boost::weak_ptr<Source>(src))));
489                                         }
490                                 }
491
492                                 // handle multi
493
494                                 nframes_t start = 0;
495                                 if (rec_regions.size() > 0) {
496                                         start = rec_regions.back().first->start()
497                                                         + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
498                                 }
499
500                                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>(
501                                                 RegionFactory::create (sources, start, 1, "", 0, Region::DefaultFlags, false)));
502                                 assert(region);
503                                 region->block_property_changes ();
504                                 region->set_position (_trackview.session().transport_frame(), this);
505                                 rec_regions.push_back (make_pair(region, (RegionView*)0));
506                         }
507
508                         /* start a new rec box */
509
510                         boost::shared_ptr<AudioTrack> at;
511
512                         at = _trackview.audio_track(); /* we know what it is already */
513                         boost::shared_ptr<AudioDiskstream> ds = at->audio_diskstream();
514                         nframes_t frame_pos = ds->current_capture_start ();
515                         gdouble xstart = _trackview.editor().frame_to_pixel (frame_pos);
516                         gdouble xend;
517                         uint32_t fill_color;
518
519                         switch (_trackview.audio_track()->mode()) {
520                         case Normal:
521                         case NonLayered:
522                                 xend = xstart;
523                                 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
524                                 break;
525
526                         case Destructive:
527                                 xend = xstart + 2;
528                                 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
529                                 /* make the recording rect translucent to allow
530                                    the user to see the peak data coming in, etc.
531                                 */
532                                 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
533                                 break;
534                         }
535
536                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
537                         rec_rect->property_x1() = xstart;
538                         rec_rect->property_y1() = 1.0;
539                         rec_rect->property_x2() = xend;
540                         rec_rect->property_y2() = child_height ();
541                         rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
542                         rec_rect->property_outline_what() = 0x1 | 0x2 | 0x4 | 0x8;
543                         rec_rect->property_fill_color_rgba() = fill_color;
544                         rec_rect->lower_to_bottom();
545
546                         RecBoxInfo recbox;
547                         recbox.rectangle = rec_rect;
548                         recbox.start = _trackview.session().transport_frame();
549                         recbox.length = 0;
550
551                         rec_rects.push_back (recbox);
552
553                         screen_update_connection.disconnect();
554                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (
555                                         mem_fun (*this, &AudioStreamView::update_rec_box));
556                         rec_updating = true;
557                         rec_active = true;
558
559                 } else if (rec_active &&
560                            (_trackview.session().record_status() != Session::Recording ||
561                             !_trackview.get_diskstream()->record_enabled())) {
562                         screen_update_connection.disconnect();
563                         rec_active = false;
564                         rec_updating = false;
565                 }
566
567         } else {
568
569                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
570
571                 if (!rec_rects.empty() || !rec_regions.empty()) {
572
573                         /* disconnect rapid update */
574                         screen_update_connection.disconnect();
575
576                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin();
577                                         prc != rec_data_ready_connections.end(); ++prc) {
578                                 (*prc).disconnect();
579                         }
580                         rec_data_ready_connections.clear();
581
582                         rec_updating = false;
583                         rec_active = false;
584
585                         /* remove temp regions */
586
587                         for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); ) {
588                                 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
589
590                                 tmp = iter;
591                                 ++tmp;
592
593                                 (*iter).first->drop_references ();
594
595                                 iter = tmp;
596                         }
597
598                         rec_regions.clear();
599
600                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
601
602                         /* transport stopped, clear boxes */
603                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
604                                 RecBoxInfo &rect = (*iter);
605                                 delete rect.rectangle;
606                         }
607
608                         rec_rects.clear();
609
610                 }
611         }
612 }
613
614 void
615 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
616 {
617         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
618                 (i->second->*pmf) ();
619         }
620 }
621
622 void
623 AudioStreamView::rec_peak_range_ready (nframes_t start, nframes_t cnt, boost::weak_ptr<Source> weak_src)
624 {
625         ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, weak_src));
626
627         boost::shared_ptr<Source> src (weak_src.lock());
628
629         if (!src) {
630                 return;
631         }
632
633         // this is called from the peak building thread
634
635         if (rec_data_ready_map.size() == 0 || start + cnt > last_rec_data_frame) {
636                 last_rec_data_frame = start + cnt;
637         }
638
639         rec_data_ready_map[src] = true;
640
641         if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().n_audio()) {
642                 this->update_rec_regions ();
643                 rec_data_ready_map.clear();
644         }
645 }
646
647 void
648 AudioStreamView::update_rec_regions ()
649 {
650         if (use_rec_regions) {
651                 uint32_t n = 0;
652
653                 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin();
654                                 iter != rec_regions.end(); n++) {
655
656                         list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp = iter;
657                         ++tmp;
658
659                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
660                                 /* rect already hidden, this region is done */
661                                 iter = tmp;
662                                 continue;
663                         }
664
665                         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(iter->first);
666                         if (!region) {
667                                 iter = tmp;
668                                 continue;
669                         }
670
671                         nframes_t origlen = region->length();
672
673                         if (region == rec_regions.back().first && rec_active) {
674
675                                 if (last_rec_data_frame > region->start()) {
676
677                                         nframes_t nlen = last_rec_data_frame - region->start();
678
679                                         if (nlen != region->length()) {
680
681                                                 region->freeze ();
682                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
683                                                 region->set_length (nlen, this);
684                                                 region->thaw ("updated");
685
686                                                 if (origlen == 1) {
687                                                         /* our special initial length */
688                                                         add_region_view_internal (region, false, true);
689                                                 }
690
691                                                 /* also update rect */
692                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
693                                                 gdouble xend = _trackview.editor().frame_to_pixel (region->position() + region->length());
694                                                 rect->property_x2() = xend;
695                                         }
696                                 }
697
698                         } else {
699
700                                 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
701
702                                 if (nlen != region->length()) {
703
704                                         if (region->source_length(0) >= region->start() + nlen) {
705
706                                                 region->freeze ();
707                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
708                                                 region->set_length (nlen, this);
709                                                 region->thaw ("updated");
710
711                                                 if (origlen == 1) {
712                                                         /* our special initial length */
713                                                         add_region_view_internal (region, false, true);
714                                                 }
715
716                                                 /* also hide rect */
717                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
718                                                 rect->hide();
719
720                                         }
721                                 }
722                         }
723
724                         iter = tmp;
725                 }
726         }
727 }
728
729 void
730 AudioStreamView::show_all_fades ()
731 {
732         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
733                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
734                 if (arv) {
735                         arv->set_fade_visibility (true);
736                 }
737         }
738 }
739
740 void
741 AudioStreamView::hide_all_fades ()
742 {
743         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
744                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
745                 if (arv) {
746                         arv->set_fade_visibility (false);
747                 }
748         }
749 }
750
751 void
752 AudioStreamView::show_all_xfades ()
753 {
754         foreach_crossfadeview (&CrossfadeView::show);
755         crossfades_visible = true;
756 }
757
758 void
759 AudioStreamView::hide_all_xfades ()
760 {
761         foreach_crossfadeview (&CrossfadeView::hide);
762         crossfades_visible = false;
763 }
764
765 void
766 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
767 {
768         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
769                 if (i->second->crossfade->involves (rv.audio_region())) {
770                         i->second->fake_hide ();
771                 }
772         }
773 }
774
775 void
776 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
777 {
778         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
779                 if (i->second->crossfade->involves (rv.audio_region()) && i->second->visible()) {
780                         i->second->show ();
781                 }
782         }
783 }
784
785 void
786 AudioStreamView::color_handler ()
787 {
788         //case cAudioTrackBase:
789         if (_trackview.is_track()) {
790                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase.get();
791         }
792
793         //case cAudioBusBase:
794         if (!_trackview.is_track()) {
795                 if (Profile->get_sae() && _trackview.route()->is_master()) {
796                         canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase.get();
797                 } else {
798                         canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase.get();
799                 }
800         }
801 }
802
803 void
804 AudioStreamView::update_contents_height ()
805 {
806         StreamView::update_contents_height ();
807
808         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
809                 update_content_height (i->second);
810         }
811 }
812
813 void
814 AudioStreamView::update_content_height (CrossfadeView* cv)
815 {
816         cv->show ();
817
818         if (_layer_display == Overlaid) {
819
820                 cv->set_y (0);
821                 cv->set_height (height);
822
823         } else {
824
825                 layer_t const inl = cv->crossfade->in()->layer ();
826                 layer_t const outl = cv->crossfade->out()->layer ();
827
828                 layer_t const high = max (inl, outl);
829                 layer_t const low = min (inl, outl);
830
831                 const double h = child_height ();
832
833                 cv->set_y ((_layers - high - 1) * h);
834                 cv->set_height ((high - low + 1) * h);
835
836         }
837 }
838
839 void
840 AudioStreamView::parameter_changed (string const & p)
841 {
842         if (p == "show-waveforms") {
843                 set_show_waveforms (Config->get_show_waveforms ());
844         } else if (p == "waveform-scale") {
845                 set_waveform_scale (Config->get_waveform_scale ());
846         } else if (p == "waveform-shape") {
847                 set_waveform_shape (Config->get_waveform_shape ());
848         }
849 }