* fixed missing track color mode persistence
[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_views.push_front (region_view);
159
160         /* if its the special single-sample length that we use for rec-regions, make it 
161            insensitive to events 
162         */
163
164         if (region->length() == 1) {
165                 region_view->set_sensitive (false);
166         }
167
168         /* if this was the first one, then lets query the waveform scale and shape.
169            otherwise, we set it to the current value */
170            
171         if (region_views.size() == 1) {
172                 if (region_view->waveform_logscaled()) {
173                         _waveform_scale = LogWaveform;
174                 } else {
175                         _waveform_scale = LinearWaveform;
176                 }
177
178                 if (region_view->waveform_rectified()) {
179                         _waveform_shape = Rectified;
180                 } else {
181                         _waveform_shape = Traditional;
182                 }
183         }
184         else {
185                 region_view->set_waveform_scale(_waveform_scale);
186                 region_view->set_waveform_shape(_waveform_shape);
187         }
188         
189         /* follow global waveform setting */
190
191         region_view->set_waveform_visible(_trackview.editor.show_waveforms());
192
193         /* catch regionview going away */
194         region->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_region_view), boost::weak_ptr<Region> (r)));
195
196         RegionViewAdded (region_view);
197
198         return region_view;
199 }
200
201 void
202 AudioStreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
203 {
204         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), weak_r));
205
206         boost::shared_ptr<Region> r (weak_r.lock());
207
208         if (!r) {
209                 return;
210         }
211
212         if (!_trackview.session().deletion_in_progress()) {
213
214                 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
215                         list<CrossfadeView*>::iterator tmp;
216                         
217                         tmp = i;
218                         ++tmp;
219                         
220                         boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
221                         if (ar && (*i)->crossfade->involves (ar)) {
222                                 delete *i;
223                                 crossfade_views.erase (i);
224                         }
225                         
226                         i = tmp;
227                 }
228         }
229
230         StreamView::remove_region_view(r);
231 }
232
233 void
234 AudioStreamView::undisplay_diskstream ()
235 {
236         StreamView::undisplay_diskstream();
237
238         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
239                 delete *i;
240         }
241
242         crossfade_views.clear ();
243 }
244
245 void
246 AudioStreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
247 {
248         boost::shared_ptr<Diskstream> sp (ds.lock());
249         if (!sp) {
250                 return;
251         }
252
253         playlist_modified (sp);
254 }
255
256 void
257 AudioStreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
258 {
259         /* we do not allow shared_ptr<T> to be bound to slots */
260         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_modified_weak), ds));
261
262         StreamView::playlist_modified (ds);
263         
264         /* make sure xfades are on top and all the regionviews are stacked correctly. */
265
266         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
267                 (*i)->get_canvas_group()->raise_to_top();
268         }
269 }
270
271 void
272 AudioStreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
273 {
274         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_changed), ds));
275
276         StreamView::playlist_changed(ds);
277
278         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(ds->playlist());
279         if (apl)
280                 playlist_connections.push_back (apl->NewCrossfade.connect (mem_fun (*this, &AudioStreamView::add_crossfade)));
281 }
282
283 void
284 AudioStreamView::add_crossfade_weak (boost::weak_ptr<Crossfade> crossfade)
285 {
286         boost::shared_ptr<Crossfade> sp (crossfade.lock());
287
288         if (!sp) {
289                 return;
290         }
291
292         add_crossfade (sp);
293 }
294
295 void
296 AudioStreamView::add_crossfade (boost::shared_ptr<Crossfade> crossfade)
297 {
298         AudioRegionView* lview = 0;
299         AudioRegionView* rview = 0;
300
301         /* we do not allow shared_ptr<T> to be bound to slots */
302         
303         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade_weak), boost::weak_ptr<Crossfade> (crossfade)));
304
305         /* first see if we already have a CrossfadeView for this Crossfade */
306
307         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
308                 if ((*i)->crossfade == crossfade) {
309
310                         if (!crossfades_visible || layer_display == Stacked) {
311                                 (*i)->hide();
312                         } else {
313                                 (*i)->show ();
314                         }
315                         (*i)->set_valid (true);
316                         return;
317                 }
318         }
319
320         /* create a new one */
321
322         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
323                 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
324
325                 if (!lview && arv && (arv->region() == crossfade->out())) {
326                         lview = arv;
327                 }
328                 if (!rview && arv && (arv->region() == crossfade->in())) {
329                         rview = arv;
330                 }
331         }
332
333         CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display,
334                                                _trackview,
335                                                 crossfade,
336                                                _samples_per_unit,
337                                                region_color,
338                                                *lview, *rview);
339         cv->set_valid (true);
340         crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
341         crossfade_views.push_back (cv);
342         if (!Config->get_xfades_visible() || !crossfades_visible || layer_display == Stacked) {
343                 cv->hide ();
344         }
345 }
346
347 void
348 AudioStreamView::remove_crossfade (boost::shared_ptr<Region> r)
349 {
350         ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), r));
351
352         boost::shared_ptr<Crossfade> xfade = boost::dynamic_pointer_cast<Crossfade> (r);
353
354         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
355                 if ((*i)->crossfade == xfade) {
356                         delete *i;
357                         crossfade_views.erase (i);
358                         break;
359                 }
360         }
361 }
362
363 void
364 AudioStreamView::redisplay_diskstream ()
365 {
366         list<RegionView *>::iterator i, tmp;
367         list<CrossfadeView*>::iterator xi, tmpx;
368
369         for (i = region_views.begin(); i != region_views.end(); ++i) {
370                 (*i)->set_valid (false);
371         }
372
373         for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
374                 (*xi)->set_valid (false);
375                 if ((*xi)->visible() && layer_display != Stacked) {
376                         (*xi)->show ();
377                 }
378         }
379
380         if (_trackview.is_audio_track()) {
381                 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
382
383                 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_trackview.get_diskstream()->playlist());
384                 if (apl)
385                         apl->foreach_crossfade (this, &AudioStreamView::add_crossfade);
386         }
387
388         RegionViewList copy;
389
390         /* Place regions */
391         for (i = region_views.begin(); i != region_views.end(); ) {
392                 tmp = i;
393                 tmp++;
394
395                 if (!(*i)->is_valid()) {
396                         delete *i;
397                         region_views.erase (i);
398                         i = tmp;
399                         continue;
400                 } else {
401                         (*i)->enable_display(true);
402                 }
403
404                 /* Sort regionviews by layer so that when we call region_layered ()
405                    the canvas layering works out (in non-stacked mode). */
406
407                 if (copy.size() == 0) {
408                         copy.push_front((*i));
409                         i = tmp;
410                         continue;
411                 }
412
413                 RegionViewList::iterator k = copy.begin();
414                 RegionViewList::iterator l = copy.end();
415                 l--;
416
417                 if ((*i)->region()->layer() <= (*k)->region()->layer()) {
418                         copy.push_front((*i));
419                         i = tmp;
420                         continue;
421                 } else if ((*i)->region()->layer() >= (*l)->region()->layer()) {
422                         copy.push_back((*i));
423                         i = tmp;
424                         continue;
425                 }
426
427                 for (RegionViewList::iterator j = copy.begin(); j != copy.end(); ++j) {
428                         if ((*j)->region()->layer() >= (*i)->region()->layer()) {
429                                 copy.insert(j, (*i));
430                                 break;
431                         }
432                 }
433
434                 i = tmp;
435         }
436
437         for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
438                 tmpx = xi;
439                 tmpx++;
440
441                 if (!(*xi)->valid()) {
442                         delete *xi;
443                         crossfade_views.erase (xi);
444                 }
445
446                 xi = tmpx;
447         }
448         
449         /* now fix layering */
450
451         for (RegionViewList::iterator i = copy.begin(); i != copy.end(); ++i) {
452                 region_layered (*i);
453         }
454 }
455
456 void
457 AudioStreamView::set_show_waveforms (bool yn)
458 {
459         for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
460                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
461                 if (arv) {
462                         arv->set_waveform_visible (yn);
463                 }
464         }
465 }
466
467 void
468 AudioStreamView::set_waveform_shape (WaveformShape shape)
469 {
470         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
471                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
472                 if (arv)
473                         arv->set_waveform_shape (shape);
474         }
475         _waveform_shape = shape;
476 }               
477
478 void
479 AudioStreamView::set_waveform_scale (WaveformScale scale)
480 {
481         for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
482                 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
483                 if (arv) 
484                         arv->set_waveform_scale (scale);
485         }
486         _waveform_scale = scale;
487 }               
488
489 void
490 AudioStreamView::setup_rec_box ()
491 {
492         //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
493
494         if (_trackview.session().transport_rolling()) {
495
496                 // cerr << "\trolling\n";
497
498                 if (!rec_active && 
499                     _trackview.session().record_status() == Session::Recording && 
500                     _trackview.get_diskstream()->record_enabled()) {
501                         if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
502
503                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
504
505                                 SourceList sources;
506
507                                 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
508                                         (*prc).disconnect();
509                                 }
510                                 rec_data_ready_connections.clear();
511                                         
512                                 // FIXME
513                                 boost::shared_ptr<AudioDiskstream> ads = boost::dynamic_pointer_cast<AudioDiskstream>(_trackview.get_diskstream());
514                                 assert(ads);
515
516                                 for (uint32_t n=0; n < ads->n_channels().n_audio(); ++n) {
517                                         boost::shared_ptr<AudioFileSource> src = boost::static_pointer_cast<AudioFileSource> (ads->write_source (n));
518                                         if (src) {
519                                                 sources.push_back (src);
520                                                 
521                                                 rec_data_ready_connections.push_back (src->PeakRangeReady.connect (bind
522                                                         (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), boost::weak_ptr<Source>(src)))); 
523                                         }
524                                 }
525
526                                 // handle multi
527                                 
528                                 nframes_t start = 0;
529                                 if (rec_regions.size() > 0) {
530                                         start = rec_regions.back().first->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
531                                 }
532                                 
533                                 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>
534                                                                        (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
535                                 assert(region);
536                                 region->set_position (_trackview.session().transport_frame(), this);
537
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() = (double) _trackview.current_height() - 1;
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 }