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