Some improvements to performance with crossfades: don't recompute a whole track's...
[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 || _layer_display == Stacked) {
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 || _layer_display == Stacked) {
348                 cv->hide ();
349         }
350 }
351
352 void
353 AudioStreamView::remove_crossfade (boost::shared_ptr<Region> r)
354 {
355         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), r));
356
357         boost::shared_ptr<Crossfade> xfade = boost::dynamic_pointer_cast<Crossfade> (r);
358
359         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
360                 if (i->second->crossfade == xfade) {
361                         delete i->second;
362                         crossfade_views.erase (i);
363                         break;
364                 }
365         }
366 }
367
368 void
369 AudioStreamView::redisplay_diskstream ()
370 {
371         list<RegionView *>::iterator i;
372         CrossfadeViewList::iterator xi, tmpx;
373
374         // Flag region views as invalid and disable drawing
375         for (i = region_views.begin(); i != region_views.end(); ++i) {
376                 (*i)->set_valid (false);
377                 (*i)->enable_display (false);
378         }
379
380         // Flag crossfade views as invalid
381         for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
382                 xi->second->set_valid (false);
383                 if (xi->second->visible() && _layer_display != Stacked) {
384                         xi->second->show ();
385                 }
386         }
387
388         // Add and display region and crossfade views, and flag them as valid
389
390         if (_trackview.is_audio_track()) {
391                 _trackview.get_diskstream()->playlist()->foreach_region(
392                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
393                         );
394
395                 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(
396                                 _trackview.get_diskstream()->playlist()
397                         );
398                 
399                 if (apl) {
400                         apl->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade));
401                 }
402         }
403         
404         // Remove invalid crossfade views
405         for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
406                 tmpx = xi;
407                 tmpx++;
408
409                 if (!xi->second->valid()) {
410                         delete xi->second;
411                         crossfade_views.erase (xi);
412                 }
413
414                 xi = tmpx;
415         }
416
417         // Stack regions by layer, and remove invalid regions
418         layer_regions();
419 }
420
421 void
422 AudioStreamView::set_show_waveforms (bool yn)
423 {
424         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
425                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
426                 if (arv) {
427                         arv->set_waveform_visible (yn);
428                 }
429         }
430 }
431
432 void
433 AudioStreamView::set_waveform_shape (WaveformShape shape)
434 {
435         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
436                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
437                 if (arv)
438                         arv->set_waveform_shape (shape);
439         }
440 }               
441
442 void
443 AudioStreamView::set_waveform_scale (WaveformScale scale)
444 {
445         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
446                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
447                 if (arv) {
448                         arv->set_waveform_scale (scale);
449                 }
450         }
451 }               
452
453 void
454 AudioStreamView::setup_rec_box ()
455 {
456         //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
457
458         if (_trackview.session().transport_rolling()) {
459
460                 // cerr << "\trolling\n";
461
462                 if (!rec_active && 
463                     _trackview.session().record_status() == Session::Recording && 
464                     _trackview.get_diskstream()->record_enabled()) {
465                         if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
466
467                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
468
469                                 SourceList sources;
470
471                                 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
472                                         (*prc).disconnect();
473                                 }
474                                 rec_data_ready_connections.clear();
475                                         
476                                 // FIXME
477                                 boost::shared_ptr<AudioDiskstream> ads = boost::dynamic_pointer_cast<AudioDiskstream>(_trackview.get_diskstream());
478                                 assert(ads);
479
480                                 for (uint32_t n=0; n < ads->n_channels().n_audio(); ++n) {
481                                         boost::shared_ptr<AudioFileSource> src = boost::static_pointer_cast<AudioFileSource> (ads->write_source (n));
482                                         if (src) {
483                                                 sources.push_back (src);
484                                                 
485                                                 rec_data_ready_connections.push_back (src->PeakRangeReady.connect (bind
486                                                         (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), boost::weak_ptr<Source>(src)))); 
487                                         }
488                                 }
489
490                                 // handle multi
491                                 
492                                 nframes_t start = 0;
493                                 if (rec_regions.size() > 0) {
494                                         start = rec_regions.back().first->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
495                                 }
496                                 
497                                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>
498                                                                        (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags), false)));
499                                 assert(region);
500                                 region->block_property_changes ();
501                                 region->set_position (_trackview.session().transport_frame(), this);
502                                 rec_regions.push_back (make_pair(region, (RegionView*)0));
503                         }
504                         
505                         /* start a new rec box */
506
507                         boost::shared_ptr<AudioTrack> at;
508
509                         at = _trackview.audio_track(); /* we know what it is already */
510                         boost::shared_ptr<AudioDiskstream> ds = at->audio_diskstream();
511                         nframes_t frame_pos = ds->current_capture_start ();
512                         gdouble xstart = _trackview.editor().frame_to_pixel (frame_pos);
513                         gdouble xend;
514                         uint32_t fill_color;
515
516                         switch (_trackview.audio_track()->mode()) {
517                         case Normal:
518                         case NonLayered:
519                                 xend = xstart;
520                                 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
521                                 break;
522
523                         case Destructive:
524                                 xend = xstart + 2;
525                                 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
526                                 /* make the recording rect translucent to allow
527                                    the user to see the peak data coming in, etc.
528                                 */
529                                 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
530                                 break;
531                         }
532                         
533                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
534                         rec_rect->property_x1() = xstart;
535                         rec_rect->property_y1() = 1.0;
536                         rec_rect->property_x2() = xend;
537                         rec_rect->property_y2() = child_height ();
538                         rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
539                         rec_rect->property_outline_what() = 0x1 | 0x2 | 0x4 | 0x8;
540                         rec_rect->property_fill_color_rgba() = fill_color;
541                         rec_rect->lower_to_bottom();
542                         
543                         RecBoxInfo recbox;
544                         recbox.rectangle = rec_rect;
545                         recbox.start = _trackview.session().transport_frame();
546                         recbox.length = 0;
547                         
548                         rec_rects.push_back (recbox);
549                         
550                         screen_update_connection.disconnect();
551                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &AudioStreamView::update_rec_box));   
552                         rec_updating = true;
553                         rec_active = true;
554
555                 } else if (rec_active &&
556                            (_trackview.session().record_status() != Session::Recording ||
557                             !_trackview.get_diskstream()->record_enabled())) {
558                         screen_update_connection.disconnect();
559                         rec_active = false;
560                         rec_updating = false;
561                 }
562                 
563         } else {
564
565                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
566
567                 if (!rec_rects.empty() || !rec_regions.empty()) {
568
569                         /* disconnect rapid update */
570                         screen_update_connection.disconnect();
571
572                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
573                                 (*prc).disconnect();
574                         }
575                         rec_data_ready_connections.clear();
576
577                         rec_updating = false;
578                         rec_active = false;
579                         
580                         /* remove temp regions */
581
582                         for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); ) {
583                                 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
584
585                                 tmp = iter;
586                                 ++tmp;
587
588                                 (*iter).first->drop_references ();
589
590                                 iter = tmp;
591                         }
592                                 
593                         rec_regions.clear();
594
595                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
596
597                         /* transport stopped, clear boxes */
598                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
599                                 RecBoxInfo &rect = (*iter);
600                                 delete rect.rectangle;
601                         }
602                         
603                         rec_rects.clear();
604                         
605                 }
606         }
607 }
608
609 void
610 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
611 {
612         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
613                 (i->second->*pmf) ();
614         }
615 }
616
617 void
618 AudioStreamView::rec_peak_range_ready (nframes_t start, nframes_t cnt, boost::weak_ptr<Source> weak_src)
619 {
620         ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, weak_src));
621         
622         boost::shared_ptr<Source> src (weak_src.lock());
623
624         if (!src) {
625                 return; 
626         }
627
628         // this is called from the peak building thread
629         
630         if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
631                 last_rec_data_frame = start + cnt;
632         }
633         
634         rec_data_ready_map[src] = true;
635         
636         if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().n_audio()) {
637                 this->update_rec_regions ();
638                 rec_data_ready_map.clear();
639         }
640 }
641
642 void
643 AudioStreamView::update_rec_regions ()
644 {
645         if (use_rec_regions) {
646                 uint32_t n = 0;
647
648                 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
649
650                         list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
651
652                         tmp = iter;
653                         ++tmp;
654
655                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
656                                 /* rect already hidden, this region is done */
657                                 iter = tmp;
658                                 continue;
659                         }
660                         
661                         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(iter->first);
662                         if (!region) {
663                                 continue;
664                         }
665
666                         nframes_t origlen = region->length();
667
668                         if (region == rec_regions.back().first && rec_active) {
669
670                                 if (last_rec_data_frame > region->start()) {
671
672                                         nframes_t nlen = last_rec_data_frame - region->start();
673
674                                         if (nlen != region->length()) {
675
676                                                 region->freeze ();
677                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
678                                                 region->set_length (nlen, this);
679                                                 region->thaw ("updated");
680
681                                                 if (origlen == 1) {
682                                                         /* our special initial length */
683                                                         add_region_view_internal (region, false, true);
684                                                 }
685
686                                                 /* also update rect */
687                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
688                                                 gdouble xend = _trackview.editor().frame_to_pixel (region->position() + region->length());
689                                                 rect->property_x2() = xend;
690                                         }
691                                 }
692
693                         } else {
694
695                                 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
696
697                                 if (nlen != region->length()) {
698
699                                         if (region->source_length(0) >= region->start() + nlen) {
700
701                                                 region->freeze ();
702                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
703                                                 region->set_length (nlen, this);
704                                                 region->thaw ("updated");
705                                                 
706                                                 if (origlen == 1) {
707                                                         /* our special initial length */
708                                                         add_region_view_internal (region, false, true);
709                                                 }
710                                                 
711                                                 /* also hide rect */
712                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
713                                                 rect->hide();
714
715                                         }
716                                 }
717                         }
718
719                         iter = tmp;
720                 }
721         }
722 }
723
724 void
725 AudioStreamView::show_all_fades ()
726 {
727         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
728                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
729                 if (arv) {
730                         arv->set_fade_visibility (true);
731                 }
732         }
733 }
734
735 void
736 AudioStreamView::hide_all_fades ()
737 {
738         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
739                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
740                 if (arv) {
741                         arv->set_fade_visibility (false);
742                 }
743         }
744 }
745
746 void
747 AudioStreamView::show_all_xfades ()
748 {
749         foreach_crossfadeview (&CrossfadeView::show);
750         crossfades_visible = true;
751 }
752
753 void
754 AudioStreamView::hide_all_xfades ()
755 {
756         foreach_crossfadeview (&CrossfadeView::hide);
757         crossfades_visible = false;
758 }
759
760 void
761 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
762 {
763         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
764                 if (i->second->crossfade->involves (rv.audio_region())) {
765                         i->second->fake_hide ();
766                 }
767         }
768 }
769
770 void
771 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
772 {
773         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
774                 if (i->second->crossfade->involves (rv.audio_region()) && i->second->visible() && _layer_display != Stacked) {
775                         i->second->show ();
776                 }
777         }
778 }
779
780 void
781 AudioStreamView::color_handler ()
782 {
783         //case cAudioTrackBase:
784         if (_trackview.is_track()) {
785                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase.get();
786         } 
787
788         //case cAudioBusBase:
789         if (!_trackview.is_track()) {
790                 if (Profile->get_sae() && _trackview.route()->is_master()) {
791                         canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase.get();
792                 } else {
793                         canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase.get();
794                 }
795         }
796 }
797
798 void
799 AudioStreamView::update_contents_height ()
800 {
801         StreamView::update_contents_height ();
802         
803         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
804                 if (_layer_display == Overlaid) {
805                         i->second->show ();
806                         i->second->set_height (height);
807                 } else {
808                         i->second->hide ();
809                 }
810         }
811 }
812
813 void
814 AudioStreamView::parameter_changed (string const & p)
815 {
816         if (p == "show-waveforms") {
817                 set_show_waveforms (Config->get_show_waveforms ());
818         } else if (p == "waveform-scale") {
819                 set_waveform_scale (Config->get_waveform_scale ());
820         } else if (p == "waveform-shape") {
821                 set_waveform_shape (Config->get_waveform_shape ());
822         }
823 }