the return of VST support
[ardour.git] / gtk2_ardour / streamview.cc
1 #include <cmath>
2
3 #include <gtkmm.h>
4
5 #include <gtkmm2ext/gtk_ui.h>
6
7 #include <ardour/audioplaylist.h>
8 #include <ardour/audioregion.h>
9 #include <ardour/audiosource.h>
10 #include <ardour/audio_diskstream.h>
11 #include <ardour/audio_track.h>
12 #include <ardour/playlist_templates.h>
13 #include <ardour/source.h>
14
15 #include "streamview.h"
16 #include "regionview.h"
17 #include "taperegionview.h"
18 #include "audio_time_axis.h"
19 #include "canvas-waveview.h"
20 #include "canvas-simplerect.h"
21 #include "region_selection.h"
22 #include "selection.h"
23 #include "public_editor.h"
24 #include "ardour_ui.h"
25 #include "crossfade_view.h"
26 #include "rgb_macros.h"
27 #include "gui_thread.h"
28 #include "utils.h"
29 #include "color.h"
30
31 using namespace ARDOUR;
32 using namespace PBD;
33 using namespace Editing;
34
35 StreamView::StreamView (AudioTimeAxisView& tv)
36         : _trackview (tv)
37 {
38         region_color = _trackview.color();
39         crossfades_visible = true;
40
41         if (tv.is_audio_track()) {
42                 /* TRACK */
43                 //stream_base_color = RGBA_TO_UINT (222,223,218,255);
44                 stream_base_color = color_map[cAudioTrackBase];
45         } else {
46                 /* BUS */
47                 //stream_base_color = RGBA_TO_UINT (230,226,238,255);
48                 stream_base_color = color_map[cAudioBusBase];
49         }
50
51         /* set_position() will position the group */
52
53         canvas_group = new ArdourCanvas::Group(*_trackview.canvas_display);
54
55         canvas_rect = new ArdourCanvas::SimpleRect (*canvas_group);
56         canvas_rect->property_x1() = 0.0;
57         canvas_rect->property_y1() = 0.0;
58         canvas_rect->property_x2() = 1000000.0;
59         canvas_rect->property_y2() = (double) tv.height;
60         canvas_rect->property_outline_color_rgba() = color_map[cAudioTrackOutline];
61         canvas_rect->property_outline_what() = (guint32) (0x1|0x2|0x8);  // outline ends and bottom 
62         canvas_rect->property_fill_color_rgba() = stream_base_color;
63
64         canvas_rect->signal_event().connect (bind (mem_fun (_trackview.editor, &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
65
66         _samples_per_unit = _trackview.editor.get_current_zoom();
67         _amplitude_above_axis = 1.0;
68
69         if (_trackview.is_audio_track()) {
70                 _trackview.audio_track()->diskstream_changed.connect (mem_fun (*this, &StreamView::diskstream_changed));
71                 _trackview.session().TransportStateChange.connect (mem_fun (*this, &StreamView::transport_changed));
72                 _trackview.get_diskstream()->record_enable_changed.connect (mem_fun (*this, &StreamView::rec_enable_changed));
73                 _trackview.session().RecordStateChanged.connect (mem_fun (*this, &StreamView::sess_rec_enable_changed));
74         } 
75
76         rec_updating = false;
77         rec_active = false;
78         use_rec_regions = tv.editor.show_waveforms_recording ();
79         last_rec_peak_frame = 0;
80
81         ColorChanged.connect (mem_fun (*this, &StreamView::color_handler));
82 }
83
84 StreamView::~StreamView ()
85 {
86         undisplay_diskstream ();
87         delete canvas_group;
88 }
89
90 void
91 StreamView::attach ()
92 {
93         if (_trackview.is_audio_track()) {
94                 display_diskstream (_trackview.get_diskstream());
95         }
96 }
97
98 int
99 StreamView::set_position (gdouble x, gdouble y)
100
101 {
102         canvas_group->property_x() = x;
103         canvas_group->property_y() = y;
104         return 0;
105 }
106
107 int
108 StreamView::set_height (gdouble h)
109 {
110         /* limit the values to something sane-ish */
111
112         if (h < 10.0 || h > 1000.0) {
113                 return -1;
114         }
115
116         canvas_rect->property_y2() = h;
117
118         for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
119                 (*i)->set_height (h);
120         }
121
122         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
123                 (*i)->set_height (h);
124         }
125
126         for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
127                 RecBoxInfo &recbox = (*i);
128                 recbox.rectangle->property_y2() = h - 1.0;
129         }
130
131         return 0;
132 }
133
134 int 
135 StreamView::set_samples_per_unit (gdouble spp)
136 {
137         AudioRegionViewList::iterator i;
138
139         if (spp < 1.0) {
140                 return -1;
141         }
142
143         _samples_per_unit = spp;
144
145         for (i = region_views.begin(); i != region_views.end(); ++i) {
146                 (*i)->set_samples_per_unit (spp);
147         }
148
149         for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
150                 (*xi)->set_samples_per_unit (spp);
151         }
152
153         for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
154                 RecBoxInfo &recbox = (*xi);
155                 
156                 gdouble xstart = _trackview.editor.frame_to_pixel ( recbox.start );
157                 gdouble xend = _trackview.editor.frame_to_pixel ( recbox.start + recbox.length );
158
159                 recbox.rectangle->property_x1() = xstart;
160                 recbox.rectangle->property_x2() = xend;
161         }
162
163         return 0;
164 }
165
166 int 
167 StreamView::set_amplitude_above_axis (gdouble app)
168
169 {
170         AudioRegionViewList::iterator i;
171
172         if (app < 1.0) {
173                 return -1;
174         }
175
176         _amplitude_above_axis = app;
177
178         for (i = region_views.begin(); i != region_views.end(); ++i) {
179                 (*i)->set_amplitude_above_axis (app);
180         }
181
182         return 0;
183 }
184
185 void
186 StreamView::add_region_view (Region *r)
187 {
188         add_region_view_internal (r, true);
189 }
190
191 void
192 StreamView::add_region_view_internal (Region *r, bool wait_for_waves)
193 {
194         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::add_region_view), r));
195
196         AudioRegion* region = dynamic_cast<AudioRegion*> (r);
197
198         if (region == 0) {
199                 return;
200         }
201
202         AudioRegionView *region_view;
203         list<AudioRegionView *>::iterator i;
204
205         for (i = region_views.begin(); i != region_views.end(); ++i) {
206                 if (&(*i)->region == region) {
207                         
208                         /* great. we already have a AudioRegionView for this Region. use it again.
209                          */
210
211                         (*i)->set_valid (true);
212                         return;
213                 }
214         }
215         
216         switch (_trackview.audio_track()->mode()) {
217         case Normal:
218                 region_view = new AudioRegionView (canvas_group, _trackview, *region, 
219                                                    _samples_per_unit, region_color);
220                 break;
221         case Destructive:
222                 region_view = new TapeAudioRegionView (canvas_group, _trackview, *region, 
223                                                        _samples_per_unit, region_color);
224                 break;
225         }
226
227         region_view->init (_amplitude_above_axis, region_color, wait_for_waves);
228         region_views.push_front (region_view);
229         
230         /* follow global waveform setting */
231
232         region_view->set_waveform_visible(_trackview.editor.show_waveforms());
233
234         /* catch regionview going away */
235
236         region->GoingAway.connect (mem_fun (*this, &StreamView::remove_region_view));
237         
238         AudioRegionViewAdded (region_view);
239 }
240
241 void
242 StreamView::remove_region_view (Region *r)
243 {
244         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_region_view), r));
245
246         AudioRegion* ar = dynamic_cast<AudioRegion*> (r);
247
248         if (ar == 0) {
249                 return;
250         }
251
252         for (list<AudioRegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
253                 if (&((*i)->region) == ar) {
254                         delete *i;
255                         region_views.erase (i);
256                         break;
257                 }
258         }
259
260         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
261                 list<CrossfadeView*>::iterator tmp;
262                 
263                 tmp = i;
264                 ++tmp;
265                 
266                 if ((*i)->crossfade.involves (*ar)) {
267                         delete *i;
268                         crossfade_views.erase (i);
269                 }
270                 
271                 i = tmp;
272         }
273 }
274
275 void
276 StreamView::remove_rec_region (Region *r)
277 {
278         ENSURE_GUI_THREAD(bind (mem_fun (*this, &StreamView::remove_rec_region), r));
279         
280         if (!Gtkmm2ext::UI::instance()->caller_is_ui_thread()) {
281                 fatal << "region deleted from non-GUI thread!" << endmsg;
282                 /*NOTREACHED*/
283         } 
284
285         AudioRegion* ar = dynamic_cast<AudioRegion*> (r);
286
287         if (ar == 0) {
288                 return;
289         }
290
291         for (list<AudioRegion *>::iterator i = rec_regions.begin(); i != rec_regions.end(); ++i) {
292                 if (*i == ar) {
293                         rec_regions.erase (i);
294                         break;
295                 }
296         }
297 }
298
299 void
300 StreamView::undisplay_diskstream ()
301 {
302         
303         for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
304                 delete *i;
305         }
306
307         for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
308                 delete *i;
309         }
310
311         region_views.clear();
312         crossfade_views.clear ();
313 }
314
315 void
316 StreamView::display_diskstream (AudioDiskstream *ds)
317 {
318         playlist_change_connection.disconnect();
319         playlist_changed (ds);
320         playlist_change_connection = ds->PlaylistChanged.connect (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
321 }
322
323 void
324 StreamView::playlist_modified ()
325 {
326         ENSURE_GUI_THREAD (mem_fun (*this, &StreamView::playlist_modified));
327
328         /* if the playlist is modified, make sure xfades are on top and all the regionviews are stacked 
329            correctly.
330         */
331
332         for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
333                 region_layered (*i);
334         }
335
336         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
337                 (*i)->get_canvas_group()->raise_to_top();
338         }
339 }
340
341 void
342 StreamView::playlist_changed (AudioDiskstream *ds)
343 {
344         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
345
346         /* disconnect from old playlist */
347
348         for (vector<sigc::connection>::iterator i = playlist_connections.begin(); i != playlist_connections.end(); ++i) {
349                 (*i).disconnect();
350         }
351         
352         playlist_connections.clear();
353         undisplay_diskstream ();
354
355         /* draw it */
356
357         redisplay_diskstream ();
358
359         /* catch changes */
360
361         playlist_connections.push_back (ds->playlist()->RegionAdded.connect (mem_fun (*this, &StreamView::add_region_view)));
362         playlist_connections.push_back (ds->playlist()->RegionRemoved.connect (mem_fun (*this, &StreamView::remove_region_view)));
363         playlist_connections.push_back (ds->playlist()->StateChanged.connect (mem_fun (*this, &StreamView::playlist_state_changed)));
364         playlist_connections.push_back (ds->playlist()->Modified.connect (mem_fun (*this, &StreamView::playlist_modified)));
365         playlist_connections.push_back (ds->playlist()->NewCrossfade.connect (mem_fun (*this, &StreamView::add_crossfade)));
366 }
367
368 void
369 StreamView::add_crossfade (Crossfade *crossfade)
370 {
371         AudioRegionView* lview = 0;
372         AudioRegionView* rview = 0;
373
374         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::add_crossfade), crossfade));
375
376         /* first see if we already have a CrossfadeView for this Crossfade */
377
378         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
379                 if (&(*i)->crossfade == crossfade) {
380                         if (!crossfades_visible) {
381                                 (*i)->hide();
382                         } else {
383                                 (*i)->show ();
384                         }
385                         (*i)->set_valid (true);
386                         return;
387                 }
388         }
389
390         /* create a new one */
391
392         for (list<AudioRegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
393                 if (!lview && &((*i)->region) == &crossfade->out()) {
394                         lview = *i;
395                 }
396                 if (!rview && &((*i)->region) == &crossfade->in()) {
397                         rview = *i;
398                 }
399         }
400
401         CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display,
402                                                _trackview,
403                                                *crossfade,
404                                                _samples_per_unit,
405                                                region_color,
406                                                *lview, *rview);
407
408         crossfade->Invalidated.connect (mem_fun (*this, &StreamView::remove_crossfade));
409         crossfade_views.push_back (cv);
410
411         if (!crossfades_visible) {
412                 cv->hide ();
413         }
414 }
415
416 void
417 StreamView::remove_crossfade (Crossfade *xfade)
418 {
419         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_crossfade), xfade));
420
421         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
422                 if (&(*i)->crossfade == xfade) {
423                         delete *i;
424                         crossfade_views.erase (i);
425                         break;
426                 }
427         }
428 }
429
430 void
431 StreamView::playlist_state_changed (Change ignored)
432 {
433         ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_state_changed), ignored));
434
435         redisplay_diskstream ();
436 }
437
438 void
439 StreamView::redisplay_diskstream ()
440 {
441         list<AudioRegionView *>::iterator i, tmp;
442         list<CrossfadeView*>::iterator xi, tmpx;
443
444         
445         for (i = region_views.begin(); i != region_views.end(); ++i) {
446                 (*i)->set_valid (false);
447         }
448
449         for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
450                 (*xi)->set_valid (false);
451                 if ((*xi)->visible()) {
452                         (*xi)->show ();
453                 }
454         }
455
456         if (_trackview.is_audio_track()) {
457                 _trackview.get_diskstream()->playlist()->foreach_region (this, &StreamView::add_region_view);
458                 _trackview.get_diskstream()->playlist()->foreach_crossfade (this, &StreamView::add_crossfade);
459         }
460
461         for (i = region_views.begin(); i != region_views.end(); ) {
462                 tmp = i;
463                 tmp++;
464
465                 if (!(*i)->is_valid()) {
466                         delete *i;
467                         region_views.erase (i);
468                 } 
469
470                 i = tmp;
471         }
472
473         for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
474                 tmpx = xi;
475                 tmpx++;
476
477                 if (!(*xi)->valid()) {
478                         delete *xi;
479                         crossfade_views.erase (xi);
480                 }
481
482                 xi = tmpx;
483         }
484
485         /* now fix layering */
486
487         playlist_modified ();
488 }
489
490 void
491 StreamView::diskstream_changed (void *src_ignored)
492 {
493         AudioTrack *at;
494
495         if ((at = _trackview.audio_track()) != 0) {
496                 AudioDiskstream& ds = at->disk_stream();
497                 /* XXX grrr: when will SigC++ allow me to bind references? */
498                 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun (*this, &StreamView::display_diskstream), &ds));
499         } else {
500                 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::undisplay_diskstream));
501         }
502 }
503
504 void
505 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
506
507 {
508         list<AudioRegionView *>::iterator i;
509
510         switch (target) {
511         case RegionColor:
512                 region_color = color;
513                 for (i = region_views.begin(); i != region_views.end(); ++i) {
514                         (*i)->set_color (region_color);
515                 }
516                 // stream_base_color = RGBA_TO_UINT (color.red/256, color.green/256, color.blue/256, 255);
517                 // gnome_canvas_item_set (canvas_rect, "fill_color_rgba", stream_base_color, NULL);
518                 break;
519                 
520         case StreamBaseColor:
521                 // stream_base_color = RGBA_TO_UINT (color.red/256, color.green/256, color.blue/256, 255);
522                 // gnome_canvas_item_set (canvas_rect, "fill_color_rgba", stream_base_color, NULL);
523                 break;
524         }
525 }
526
527 void
528 StreamView::set_show_waveforms (bool yn)
529 {
530         for (list<AudioRegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
531                         (*i)->set_waveform_visible (yn);
532         }
533 }
534
535 void
536 StreamView::set_selected_regionviews (AudioRegionSelection& regions)
537 {
538         bool selected;
539
540         // cerr << _trackview.name() << " (selected = " << regions.size() << ")" << endl;
541         for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
542                 
543                 selected = false;
544                 
545                 for (AudioRegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
546                         if (*i == *ii) {
547                                 selected = true;
548                         }
549                 }
550                 
551                 // cerr << "\tregion " << (*i)->region.name() << " selected = " << selected << endl;
552                 (*i)->set_selected (selected);
553         }
554 }
555
556 void
557 StreamView::get_selectables (jack_nframes_t start, jack_nframes_t end, list<Selectable*>& results)
558 {
559         for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
560                 if ((*i)->region.coverage(start, end) != OverlapNone) {
561                         results.push_back (*i);
562                 }
563         }
564 }
565
566 void
567 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
568 {
569         for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
570                 if (!sel.audio_regions.contains (*i)) {
571                         results.push_back (*i);
572                 }
573         }
574 }
575
576 void
577 StreamView::set_waveform_shape (WaveformShape shape)
578 {
579         for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
580                 (*i)->set_waveform_shape (shape);
581         }
582 }               
583                 
584 void
585 StreamView::region_layered (AudioRegionView* rv)
586 {
587         rv->get_canvas_group()->lower_to_bottom();
588
589         /* don't ever leave it at the bottom, since then it doesn't
590            get events - the  parent group does instead ...
591         */
592         
593         rv->get_canvas_group()->raise (rv->region.layer() + 1);
594 }
595
596 void
597 StreamView::rec_enable_changed (void *src)
598 {
599         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
600 }
601
602 void
603 StreamView::sess_rec_enable_changed ()
604 {
605         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
606 }
607
608 void
609 StreamView::transport_changed()
610 {
611         Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
612 }
613
614 void
615 StreamView::setup_rec_box ()
616 {
617         // cerr << _trackview.name() << " streamview SRB\n";
618
619         if (_trackview.session().transport_rolling()) {
620
621                 // cerr << "\trolling\n";
622
623                 if (!rec_active && 
624                     _trackview.session().record_status() == Session::Recording && 
625                     _trackview.get_diskstream()->record_enabled()) {
626
627                         if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
628
629                                 /* add a new region, but don't bother if they set use_rec_regions mid-record */
630
631                                 AudioRegion::SourceList sources;
632
633                                 for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
634                                         (*prc).disconnect();
635                                 }
636                                 peak_ready_connections.clear();
637                                         
638                                 for (uint32_t n=0; n < _trackview.get_diskstream()->n_channels(); ++n) {
639                                         AudioSource *src = (AudioSource *) _trackview.get_diskstream()->write_source (n);
640                                         if (src) {
641                                                 sources.push_back (src);
642                                                 peak_ready_connections.push_back (src->PeakRangeReady.connect (bind (mem_fun (*this, &StreamView::rec_peak_range_ready), src))); 
643                                         }
644                                 }
645
646                                 // handle multi
647                                 
648                                 jack_nframes_t start = 0;
649                                 if (rec_regions.size() > 0) {
650                                         start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
651                                 }
652                                 
653                                 AudioRegion * region = new AudioRegion(sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false);
654                                 region->set_position (_trackview.session().transport_frame(), this);
655                                 rec_regions.push_back (region);
656                                 /* catch it if it goes away */
657                                 region->GoingAway.connect (mem_fun (*this, &StreamView::remove_rec_region));
658
659                                 /* we add the region later */
660                         }
661                         
662                         /* start a new rec box */
663
664                         AudioTrack* at;
665
666                         at = _trackview.audio_track(); /* we know what it is already */
667                         AudioDiskstream& ds = at->disk_stream();
668                         jack_nframes_t frame_pos = ds.current_capture_start ();
669                         gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
670                         gdouble xend;
671                         uint32_t fill_color;
672
673                         switch (_trackview.audio_track()->mode()) {
674                         case Normal:
675                                 xend = xstart;
676                                 fill_color = color_map[cRecordingRectFill];
677                                 break;
678
679                         case Destructive:
680                                 xend = xstart + 2;
681                                 fill_color = color_map[cRecordingRectFill];
682                                 /* make the recording rect translucent to allow
683                                    the user to see the peak data coming in, etc.
684                                 */
685                                 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
686                                 break;
687                         }
688                         
689                         ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
690                         rec_rect->property_x1() = xstart;
691                         rec_rect->property_y1() = 1.0;
692                         rec_rect->property_x2() = xend;
693                         rec_rect->property_y2() = (double) _trackview.height - 1;
694                         rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
695                         rec_rect->property_fill_color_rgba() = fill_color;
696                         
697                         RecBoxInfo recbox;
698                         recbox.rectangle = rec_rect;
699                         recbox.start = _trackview.session().transport_frame();
700                         recbox.length = 0;
701                         
702                         rec_rects.push_back (recbox);
703                         
704                         screen_update_connection.disconnect();
705                         screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &StreamView::update_rec_box));        
706                         rec_updating = true;
707                         rec_active = true;
708
709                 } else if (rec_active &&
710                            (_trackview.session().record_status() != Session::Recording ||
711                             !_trackview.get_diskstream()->record_enabled())) {
712
713                         screen_update_connection.disconnect();
714                         rec_active = false;
715                         rec_updating = false;
716
717                 }
718                 
719         } else {
720
721                 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
722
723                 if (!rec_rects.empty() || !rec_regions.empty()) {
724
725                         /* disconnect rapid update */
726                         screen_update_connection.disconnect();
727
728                         for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
729                                 (*prc).disconnect();
730                         }
731                         peak_ready_connections.clear();
732
733                         rec_updating = false;
734                         rec_active = false;
735                         last_rec_peak_frame = 0;
736                         
737                         /* remove temp regions */
738                         for (list<AudioRegion*>::iterator iter=rec_regions.begin(); iter != rec_regions.end(); )
739                         {
740                                 list<AudioRegion*>::iterator tmp;
741
742                                 tmp = iter;
743                                 ++tmp;
744
745                                 /* this will trigger the remove_region_view */
746                                 delete *iter;
747
748                                 iter = tmp;
749                         }
750                         
751                         rec_regions.clear();
752
753                         // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
754                 
755
756                         /* transport stopped, clear boxes */
757                         for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
758                                 RecBoxInfo &rect = (*iter);
759                                 delete rect.rectangle;
760                         }
761                         
762                         rec_rects.clear();
763                         
764                 }
765         }
766 }
767
768
769 void
770 StreamView::update_rec_box ()
771 {
772         if (rec_active && rec_rects.size() > 0) {
773                 /* only update the last box */
774                 RecBoxInfo & rect = rec_rects.back();
775                 jack_nframes_t at = _trackview.get_diskstream()->current_capture_end();
776                 double xstart;
777                 double xend;
778                 
779                 switch (_trackview.audio_track()->mode()) {
780                 case Normal:
781                         rect.length = at - rect.start;
782                         xstart = _trackview.editor.frame_to_pixel (rect.start);
783                         xend = _trackview.editor.frame_to_pixel (at);
784                         break;
785                         
786                 case Destructive:
787                         rect.length = 2;
788                         xstart = _trackview.editor.frame_to_pixel (_trackview.get_diskstream()->current_capture_start());
789                         xend = _trackview.editor.frame_to_pixel (at);
790                         break;
791                 }
792                 
793                 rect.rectangle->property_x1() = xstart;
794                 rect.rectangle->property_x2() = xend;
795         }
796 }
797         
798 AudioRegionView*
799 StreamView::find_view (const AudioRegion& region)
800 {
801         for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
802
803                 if (&(*i)->region == &region) {
804                         return *i;
805                 }
806         }
807         return 0;
808 }
809         
810 void
811 StreamView::foreach_regionview (sigc::slot<void,AudioRegionView*> slot)
812 {
813         for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
814                 slot (*i);
815         }
816 }
817
818 void
819 StreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
820 {
821         for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
822                 ((*i)->*pmf) ();
823         }
824 }
825
826 void
827 StreamView::rec_peak_range_ready (jack_nframes_t start, jack_nframes_t cnt, Source * src)
828 {
829         // this is called from the peak building thread
830
831         ENSURE_GUI_THREAD(bind (mem_fun (*this, &StreamView::rec_peak_range_ready), start, cnt, src));
832         
833         if (rec_peak_ready_map.size() == 0 || start+cnt > last_rec_peak_frame) {
834                 last_rec_peak_frame = start + cnt;
835         }
836
837         rec_peak_ready_map[src] = true;
838
839         if (rec_peak_ready_map.size() == _trackview.get_diskstream()->n_channels()) {
840                 this->update_rec_regions ();
841                 rec_peak_ready_map.clear();
842         }
843 }
844
845 void
846 StreamView::update_rec_regions ()
847 {
848         if (use_rec_regions) {
849
850                 uint32_t n = 0;
851
852                 for (list<AudioRegion*>::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
853
854                         list<AudioRegion*>::iterator tmp;
855
856                         tmp = iter;
857                         ++tmp;
858
859                         if (!canvas_item_visible (rec_rects[n].rectangle)) {
860                                 /* rect already hidden, this region is done */
861                                 iter = tmp;
862                                 continue;
863                         }
864                         
865                         AudioRegion * region = (*iter);
866                         jack_nframes_t origlen = region->length();
867
868                         if (region == rec_regions.back() && rec_active) {
869
870                                 if (last_rec_peak_frame > region->start()) {
871
872                                         jack_nframes_t nlen = last_rec_peak_frame - region->start();
873
874                                         if (nlen != region->length()) {
875
876                                                 region->freeze ();
877                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
878                                                 region->set_length (nlen, this);
879                                                 region->thaw ("updated");
880
881                                                 if (origlen == 1) {
882                                                         /* our special initial length */
883                                                         add_region_view_internal (region, false);
884                                                 }
885
886                                                 /* also update rect */
887                                                 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
888                                                 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
889                                                 rect->property_x2() = xend;
890                                         }
891                                 }
892
893                         } else {
894
895                                 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
896
897                                 if (nlen != region->length()) {
898
899                                         if (region->source(0).length() >= region->start() + nlen) {
900
901                                                 region->freeze ();
902                                                 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
903                                                 region->set_length (nlen, this);
904                                                 region->thaw ("updated");
905                                                 
906                                                 if (origlen == 1) {
907                                                         /* our special initial length */
908                                                         add_region_view_internal (region, false);
909                                                 }
910                                                 
911                                                 /* also hide rect */
912                                                 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
913                                                 rect->hide();
914
915                                         }
916                                 }
917                         }
918
919                         iter = tmp;
920                 }
921         }
922 }
923
924 void
925 StreamView::show_all_xfades ()
926 {
927         foreach_crossfadeview (&CrossfadeView::show);
928         crossfades_visible = true;
929 }
930
931 void
932 StreamView::hide_all_xfades ()
933 {
934         foreach_crossfadeview (&CrossfadeView::hide);
935         crossfades_visible = false;
936 }
937
938 void
939 StreamView::hide_xfades_involving (AudioRegionView& rv)
940 {
941         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
942                 if ((*i)->crossfade.involves (rv.region)) {
943                         (*i)->fake_hide ();
944                 }
945         }
946 }
947
948 void
949 StreamView::reveal_xfades_involving (AudioRegionView& rv)
950 {
951         for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
952                 if ((*i)->crossfade.involves (rv.region) && (*i)->visible()) {
953                         (*i)->show ();
954                 }
955         }
956 }
957
958 void
959 StreamView::color_handler (ColorID id, uint32_t val)
960 {
961         switch (id) {
962         case cAudioTrackBase:
963                 if (_trackview.is_audio_track()) {
964                         canvas_rect->property_fill_color_rgba() = val;
965                 } 
966                 break;
967         case cAudioBusBase:
968                 if (!_trackview.is_audio_track()) {
969                         canvas_rect->property_fill_color_rgba() = val;
970                 }
971                 break;
972         case cAudioTrackOutline:
973                 canvas_rect->property_outline_color_rgba() = val;
974                 break;
975
976         default:
977                 break;
978         }
979 }