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