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