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