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