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