e2aa080b645a3a90d19b7dd2cd7ca5a2b598b97c
[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         _waveform_scale = LinearWaveform;
63         _waveform_shape = Traditional;
64         color_handler ();
65         _amplitude_above_axis = 1.0;
66
67         use_rec_regions = tv.editor().show_waveforms_recording ();
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)->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         /* if this was the first one, then lets query the waveform scale and shape.
152            otherwise, we set it to the current value */
153            
154         if (region_views.size() == 1) {
155
156                 if (region_view->waveform_logscaled()) {
157                         _waveform_scale = LogWaveform;
158                 } else {
159                         _waveform_scale = LinearWaveform;
160                 }
161
162                 if (region_view->waveform_rectified()) {
163                         _waveform_shape = Rectified;
164                 } else {
165                         _waveform_shape = Traditional;
166                 }
167         }
168         else {
169                 region_view->set_waveform_scale(_waveform_scale);
170                 region_view->set_waveform_shape(_waveform_shape);
171         }
172         
173         /* follow global waveform setting */
174         region_view->set_waveform_visible(_trackview.editor().show_waveforms());
175
176         return region_view;     
177 }
178
179 RegionView*
180 AudioStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wait_for_waves, bool recording)
181 {
182         RegionView *region_view = create_region_view (r, wait_for_waves, recording);
183         if (region_view == 0) {
184                 return 0;
185         }
186
187 //      if(!recording){
188 //              for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
189 //                      if ((*i)->region() == r) {
190 //                              cerr << "audio_streamview in add_region_view_internal region found" << endl;
191                                 /* great. we already have a AudioRegionView for this Region. use it again. */
192                                 
193 //                              (*i)->set_valid (true);
194
195                                 // this might not be necessary
196 //                              AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
197
198 //                              if (arv) {
199 //                                      arv->set_waveform_scale (_waveform_scale);
200 //                                      arv->set_waveform_shape (_waveform_shape);
201 //                              }
202                                         
203 //                              return NULL;
204 //                      }
205 //              }
206 //      }
207
208         region_views.push_front (region_view);
209
210         /* catch regionview going away */
211         r->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_region_view), boost::weak_ptr<Region> (r)));
212
213         RegionViewAdded (region_view);
214
215         return region_view;
216 }
217
218 void
219 AudioStreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
220 {
221         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), weak_r));
222
223         boost::shared_ptr<Region> r (weak_r.lock());
224
225         if (!r) {
226                 return;
227         }
228
229         if (!_trackview.session().deletion_in_progress()) {
230
231                 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
232                         list<CrossfadeView*>::iterator tmp;
233                         
234                         tmp = i;
235                         ++tmp;
236                         
237                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
238                         if (ar && (*i)->crossfade->involves (ar)) {
239                                 delete *i;
240                                 crossfade_views.erase (i);
241                         }
242                         
243                         i = tmp;
244                 }
245         }
246
247         StreamView::remove_region_view(r);
248 }
249
250 void
251 AudioStreamView::undisplay_diskstream ()
252 {
253         StreamView::undisplay_diskstream();
254
255         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
256                 delete *i;
257         }
258
259         crossfade_views.clear ();
260 }
261
262 void
263 AudioStreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
264 {
265         boost::shared_ptr<Diskstream> sp (ds.lock());
266         if (sp) {
267                 playlist_modified (sp);
268         }
269 }
270
271 void
272 AudioStreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
273 {
274         /* we do not allow shared_ptr<T> to be bound to slots */
275         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_modified_weak), ds));
276
277         StreamView::playlist_modified (ds);
278         
279         /* make sure xfades are on top and all the regionviews are stacked correctly. */
280
281         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
282                 (*i)->get_canvas_group()->raise_to_top();
283         }
284 }
285
286 void
287 AudioStreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
288 {
289         boost::shared_ptr<Diskstream> sp (ds.lock());
290         if (sp) {
291                 playlist_changed (sp);
292         }
293 }
294
295 void
296 AudioStreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
297 {
298         ENSURE_GUI_THREAD (bind (
299                         mem_fun (*this, &AudioStreamView::playlist_changed_weak),
300                         boost::weak_ptr<Diskstream> (ds)));
301
302         StreamView::playlist_changed(ds);
303
304         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(ds->playlist());
305         if (apl) {
306                 playlist_connections.push_back (apl->NewCrossfade.connect (
307                                 mem_fun (*this, &AudioStreamView::add_crossfade)));
308         }
309 }
310
311 void
312 AudioStreamView::add_crossfade_weak (boost::weak_ptr<Crossfade> crossfade)
313 {
314         boost::shared_ptr<Crossfade> sp (crossfade.lock());
315
316         if (!sp) {
317                 return;
318         }
319
320         add_crossfade (sp);
321 }
322
323 void
324 AudioStreamView::add_crossfade (boost::shared_ptr<Crossfade> crossfade)
325 {
326         AudioRegionView* lview = 0;
327         AudioRegionView* rview = 0;
328
329         /* we do not allow shared_ptr<T> to be bound to slots */
330         
331         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade_weak), boost::weak_ptr<Crossfade> (crossfade)));
332
333         /* first see if we already have a CrossfadeView for this Crossfade */
334
335         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
336                 if ((*i)->crossfade == crossfade) {
337
338                         if (!crossfades_visible || _layer_display == Stacked) {
339                                 (*i)->hide();
340                         } else {
341                                 (*i)->show ();
342                         }
343                         (*i)->set_valid (true);
344                         return;
345                 }
346         }
347
348         /* create a new one */
349
350         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
351                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
352
353                 if (!lview && arv && (arv->region() == crossfade->out())) {
354                         lview = arv;
355                 }
356                 if (!rview && arv && (arv->region() == crossfade->in())) {
357                         rview = arv;
358                 }
359         }
360
361         CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display (),
362                                                _trackview,
363                                                 crossfade,
364                                                _samples_per_unit,
365                                                region_color,
366                                                *lview, *rview);
367         cv->set_valid (true);
368         crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
369         crossfade_views.push_back (cv);
370         if (!_trackview.session().config.get_xfades_visible() || !crossfades_visible || _layer_display == Stacked) {
371                 cv->hide ();
372         }
373 }
374
375 void
376 AudioStreamView::remove_crossfade (boost::shared_ptr<Region> r)
377 {
378         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), r));
379
380         boost::shared_ptr<Crossfade> xfade = boost::dynamic_pointer_cast<Crossfade> (r);
381
382         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
383                 if ((*i)->crossfade == xfade) {
384                         delete *i;
385                         crossfade_views.erase (i);
386                         break;
387                 }
388         }
389 }
390
391 void
392 AudioStreamView::redisplay_diskstream ()
393 {
394         list<RegionView *>::iterator i;
395         list<CrossfadeView*>::iterator xi, tmpx;
396
397         // Flag region views as invalid and disable drawing
398         for (i = region_views.begin(); i != region_views.end(); ++i) {
399                 (*i)->set_valid (false);
400                 (*i)->enable_display (false);
401         }
402
403         // Flag crossfade views as invalid
404         for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
405                 (*xi)->set_valid (false);
406                 if ((*xi)->visible() && _layer_display != Stacked) {
407                         (*xi)->show ();
408                 }
409         }
410
411         // Add and display region and crossfade views, and flag them as valid
412
413         if (_trackview.is_audio_track()) {
414                 _trackview.get_diskstream()->playlist()->foreach_region(
415                         sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
416                         );
417
418                 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(
419                                 _trackview.get_diskstream()->playlist()
420                         );
421                 
422                 if (apl) {
423                         apl->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade));
424                 }
425         }
426         
427         // Remove invalid crossfade views
428         for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
429                 tmpx = xi;
430                 tmpx++;
431
432                 if (!(*xi)->valid()) {
433                         delete *xi;
434                         crossfade_views.erase (xi);
435                 }
436
437                 xi = tmpx;
438         }
439
440         // Stack regions by layer, and remove invalid regions
441         layer_regions();
442 }
443
444 void
445 AudioStreamView::set_show_waveforms (bool yn)
446 {
447         for (list<RegionView *>::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_visible (yn);
451                 }
452         }
453 }
454
455 void
456 AudioStreamView::set_waveform_shape (WaveformShape shape)
457 {
458         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
459                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
460                 if (arv)
461                         arv->set_waveform_shape (shape);
462         }
463         _waveform_shape = shape;
464 }               
465
466 void
467 AudioStreamView::set_waveform_scale (WaveformScale scale)
468 {
469         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
470                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
471                 if (arv) 
472                         arv->set_waveform_scale (scale);
473         }
474         _waveform_scale = scale;
475 }               
476
477 void
478 AudioStreamView::setup_rec_box ()
479 {
480         //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
481
482         if (_trackview.session().transport_rolling()) {
483
484                 // cerr << "\trolling\n";
485
486                 if (!rec_active && 
487                     _trackview.session().record_status() == Session::Recording && 
488                     _trackview.get_diskstream()->record_enabled()) {
489                         if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
490
491                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
492
493                                 SourceList sources;
494
495                                 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
496                                         (*prc).disconnect();
497                                 }
498                                 rec_data_ready_connections.clear();
499                                         
500                                 // FIXME
501                                 boost::shared_ptr<AudioDiskstream> ads = boost::dynamic_pointer_cast<AudioDiskstream>(_trackview.get_diskstream());
502                                 assert(ads);
503
504                                 for (uint32_t n=0; n < ads->n_channels().n_audio(); ++n) {
505                                         boost::shared_ptr<AudioFileSource> src = boost::static_pointer_cast<AudioFileSource> (ads->write_source (n));
506                                         if (src) {
507                                                 sources.push_back (src);
508                                                 
509                                                 rec_data_ready_connections.push_back (src->PeakRangeReady.connect (bind
510                                                         (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), boost::weak_ptr<Source>(src)))); 
511                                         }
512                                 }
513
514                                 // handle multi
515                                 
516                                 nframes_t start = 0;
517                                 if (rec_regions.size() > 0) {
518                                         start = rec_regions.back().first->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
519                                 }
520                                 
521                                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>
522                                                                        (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
523                                 assert(region);
524                                 region->set_position (_trackview.session().transport_frame(), this);
525                                 rec_regions.push_back (make_pair(region, (RegionView*)0));
526                         }
527                         
528                         /* start a new rec box */
529
530                         boost::shared_ptr<AudioTrack> at;
531
532                         at = _trackview.audio_track(); /* we know what it is already */
533                         boost::shared_ptr<AudioDiskstream> ds = at->audio_diskstream();
534                         nframes_t frame_pos = ds->current_capture_start ();
535                         gdouble xstart = _trackview.editor().frame_to_pixel (frame_pos);
536                         gdouble xend;
537                         uint32_t fill_color;
538
539                         switch (_trackview.audio_track()->mode()) {
540                         case Normal:
541                         case NonLayered:
542                                 xend = xstart;
543                                 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
544                                 break;
545
546                         case Destructive:
547                                 xend = xstart + 2;
548                                 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
549                                 /* make the recording rect translucent to allow
550                                    the user to see the peak data coming in, etc.
551                                 */
552                                 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
553                                 break;
554                         }
555                         
556                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
557                         rec_rect->property_x1() = xstart;
558                         rec_rect->property_y1() = 1.0;
559                         rec_rect->property_x2() = xend;
560                         rec_rect->property_y2() = child_height ();
561                         rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
562                         rec_rect->property_outline_what() = 0x1 | 0x2 | 0x4 | 0x8;
563                         rec_rect->property_fill_color_rgba() = fill_color;
564                         rec_rect->lower_to_bottom();
565                         
566                         RecBoxInfo recbox;
567                         recbox.rectangle = rec_rect;
568                         recbox.start = _trackview.session().transport_frame();
569                         recbox.length = 0;
570                         
571                         rec_rects.push_back (recbox);
572                         
573                         screen_update_connection.disconnect();
574                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &AudioStreamView::update_rec_box));   
575                         rec_updating = true;
576                         rec_active = true;
577
578                 } else if (rec_active &&
579                            (_trackview.session().record_status() != Session::Recording ||
580                             !_trackview.get_diskstream()->record_enabled())) {
581                         screen_update_connection.disconnect();
582                         rec_active = false;
583                         rec_updating = false;
584                 }
585                 
586         } else {
587
588                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
589
590                 if (!rec_rects.empty() || !rec_regions.empty()) {
591
592                         /* disconnect rapid update */
593                         screen_update_connection.disconnect();
594
595                         for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
596                                 (*prc).disconnect();
597                         }
598                         rec_data_ready_connections.clear();
599
600                         rec_updating = false;
601                         rec_active = false;
602                         
603                         /* remove temp regions */
604
605                         for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); ) {
606                                 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
607
608                                 tmp = iter;
609                                 ++tmp;
610
611                                 (*iter).first->drop_references ();
612
613                                 iter = tmp;
614                         }
615                                 
616                         rec_regions.clear();
617
618                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
619
620                         /* transport stopped, clear boxes */
621                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
622                                 RecBoxInfo &rect = (*iter);
623                                 delete rect.rectangle;
624                         }
625                         
626                         rec_rects.clear();
627                         
628                 }
629         }
630 }
631
632 void
633 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
634 {
635         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
636                 ((*i)->*pmf) ();
637         }
638 }
639
640 void
641 AudioStreamView::rec_peak_range_ready (nframes_t start, nframes_t cnt, boost::weak_ptr<Source> weak_src)
642 {
643         ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, weak_src));
644         
645         boost::shared_ptr<Source> src (weak_src.lock());
646
647         if (!src) {
648                 return; 
649         }
650
651         // this is called from the peak building thread
652         
653         if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
654                 last_rec_data_frame = start + cnt;
655         }
656         
657         rec_data_ready_map[src] = true;
658         
659         if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().n_audio()) {
660                 this->update_rec_regions ();
661                 rec_data_ready_map.clear();
662         }
663 }
664
665 void
666 AudioStreamView::update_rec_regions ()
667 {
668         if (use_rec_regions) {
669                 uint32_t n = 0;
670
671                 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
672
673                         list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
674
675                         tmp = iter;
676                         ++tmp;
677
678                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
679                                 /* rect already hidden, this region is done */
680                                 iter = tmp;
681                                 continue;
682                         }
683                         
684                         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(iter->first);
685                         if (!region) {
686                                 continue;
687                         }
688
689                         nframes_t origlen = region->length();
690
691                         if (region == rec_regions.back().first && rec_active) {
692
693                                 if (last_rec_data_frame > region->start()) {
694
695                                         nframes_t nlen = last_rec_data_frame - region->start();
696
697                                         if (nlen != region->length()) {
698
699                                                 region->freeze ();
700                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
701                                                 region->set_length (nlen, this);
702                                                 region->thaw ("updated");
703
704                                                 if (origlen == 1) {
705                                                         /* our special initial length */
706                                                         add_region_view_internal (region, false, true);
707                                                 }
708
709                                                 /* also update rect */
710                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
711                                                 gdouble xend = _trackview.editor().frame_to_pixel (region->position() + region->length());
712                                                 rect->property_x2() = xend;
713                                         }
714                                 }
715
716                         } else {
717
718                                 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
719
720                                 if (nlen != region->length()) {
721
722                                         if (region->source_length(0) >= region->start() + nlen) {
723
724                                                 region->freeze ();
725                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
726                                                 region->set_length (nlen, this);
727                                                 region->thaw ("updated");
728                                                 
729                                                 if (origlen == 1) {
730                                                         /* our special initial length */
731                                                         add_region_view_internal (region, false, true);
732                                                 }
733                                                 
734                                                 /* also hide rect */
735                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
736                                                 rect->hide();
737
738                                         }
739                                 }
740                         }
741
742                         iter = tmp;
743                 }
744         }
745 }
746
747 void
748 AudioStreamView::show_all_fades ()
749 {
750         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
751                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
752                 if (arv) {
753                         arv->set_fade_visibility (true);
754                 }
755         }
756 }
757
758 void
759 AudioStreamView::hide_all_fades ()
760 {
761         for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
762                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
763                 if (arv) {
764                         arv->set_fade_visibility (false);
765                 }
766         }
767 }
768
769 void
770 AudioStreamView::show_all_xfades ()
771 {
772         foreach_crossfadeview (&CrossfadeView::show);
773         crossfades_visible = true;
774 }
775
776 void
777 AudioStreamView::hide_all_xfades ()
778 {
779         foreach_crossfadeview (&CrossfadeView::hide);
780         crossfades_visible = false;
781 }
782
783 void
784 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
785 {
786         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
787                 if ((*i)->crossfade->involves (rv.audio_region())) {
788                         (*i)->fake_hide ();
789                 }
790         }
791 }
792
793 void
794 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
795 {
796         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
797                 if ((*i)->crossfade->involves (rv.audio_region()) && (*i)->visible() && _layer_display != Stacked) {
798                         (*i)->show ();
799                 }
800         }
801 }
802
803 void
804 AudioStreamView::color_handler ()
805 {
806         //case cAudioTrackBase:
807         if (_trackview.is_track()) {
808                 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase.get();
809         } 
810
811         //case cAudioBusBase:
812         if (!_trackview.is_track()) {
813                 if (Profile->get_sae() && _trackview.route()->is_master()) {
814                         canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase.get();
815                 } else {
816                         canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase.get();
817                 }
818         }
819 }
820
821 void
822 AudioStreamView::update_contents_height ()
823 {
824         StreamView::update_contents_height ();
825         
826         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
827                 if (_layer_display == Overlaid) {
828                         (*i)->show ();
829                         (*i)->set_height (height);
830                 } else {
831                         (*i)->hide ();
832                 }
833         }
834 }