5 #include <gtkmm2ext/gtk_ui.h>
7 #include <ardour/audioplaylist.h>
8 #include <ardour/audioregion.h>
9 #include <ardour/diskstream.h>
10 #include <ardour/audio_track.h>
11 #include <ardour/playlist_templates.h>
12 #include <ardour/source.h>
14 #include "streamview.h"
15 #include "regionview.h"
16 #include "taperegionview.h"
17 #include "audio_time_axis.h"
18 #include "canvas-waveview.h"
19 #include "canvas-simplerect.h"
20 #include "region_selection.h"
21 #include "selection.h"
22 #include "public_editor.h"
23 #include "ardour_ui.h"
24 #include "crossfade_view.h"
25 #include "rgb_macros.h"
26 #include "gui_thread.h"
30 using namespace ARDOUR;
31 using namespace Editing;
33 StreamView::StreamView (AudioTimeAxisView& tv)
36 region_color = _trackview.color();
37 crossfades_visible = true;
39 if (tv.is_audio_track()) {
41 //stream_base_color = RGBA_TO_UINT (222,223,218,255);
42 stream_base_color = color_map[cAudioTrackBase];
45 //stream_base_color = RGBA_TO_UINT (230,226,238,255);
46 stream_base_color = color_map[cAudioBusBase];
49 /* set_position() will position the group */
51 canvas_group = new ArdourCanvas::Group(*_trackview.canvas_display);
53 canvas_rect = new ArdourCanvas::SimpleRect (*canvas_group);
54 canvas_rect->property_x1() = 0.0;
55 canvas_rect->property_y1() = 0.0;
56 canvas_rect->property_x2() = 1000000.0;
57 canvas_rect->property_y2() = (double) tv.height;
58 canvas_rect->property_outline_color_rgba() = color_map[cAudioTrackOutline];
59 canvas_rect->property_outline_what() = (guint32) (0x1|0x2|0x8); // outline ends and bottom
60 canvas_rect->property_fill_color_rgba() = stream_base_color;
62 canvas_rect->signal_event().connect (bind (mem_fun (_trackview.editor, &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
64 _samples_per_unit = _trackview.editor.get_current_zoom();
65 _amplitude_above_axis = 1.0;
67 if (_trackview.is_audio_track()) {
68 _trackview.audio_track()->diskstream_changed.connect (mem_fun (*this, &StreamView::diskstream_changed));
69 _trackview.session().TransportStateChange.connect (mem_fun (*this, &StreamView::transport_changed));
70 _trackview.get_diskstream()->record_enable_changed.connect (mem_fun (*this, &StreamView::rec_enable_changed));
71 _trackview.session().RecordStateChanged.connect (mem_fun (*this, &StreamView::sess_rec_enable_changed));
76 use_rec_regions = tv.editor.show_waveforms_recording ();
77 last_rec_peak_frame = 0;
79 ColorChanged.connect (mem_fun (*this, &StreamView::color_handler));
82 StreamView::~StreamView ()
84 undisplay_diskstream ();
91 if (_trackview.is_audio_track()) {
92 display_diskstream (_trackview.get_diskstream());
97 StreamView::set_position (gdouble x, gdouble y)
100 canvas_group->property_x() = x;
101 canvas_group->property_y() = y;
106 StreamView::set_height (gdouble h)
108 /* limit the values to something sane-ish */
110 if (h < 10.0 || h > 1000.0) {
114 canvas_rect->property_y2() = h;
116 for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
117 (*i)->set_height (h);
120 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
121 (*i)->set_height (h);
124 for (vector<RecBoxInfo>::iterator i = rec_rects.begin(); i != rec_rects.end(); ++i) {
125 RecBoxInfo &recbox = (*i);
126 recbox.rectangle->property_y2() = h - 1.0;
133 StreamView::set_samples_per_unit (gdouble spp)
135 AudioRegionViewList::iterator i;
141 _samples_per_unit = spp;
143 for (i = region_views.begin(); i != region_views.end(); ++i) {
144 (*i)->set_samples_per_unit (spp);
147 for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
148 (*xi)->set_samples_per_unit (spp);
151 for (vector<RecBoxInfo>::iterator xi = rec_rects.begin(); xi != rec_rects.end(); ++xi) {
152 RecBoxInfo &recbox = (*xi);
154 gdouble xstart = _trackview.editor.frame_to_pixel ( recbox.start );
155 gdouble xend = _trackview.editor.frame_to_pixel ( recbox.start + recbox.length );
157 recbox.rectangle->property_x1() = xstart;
158 recbox.rectangle->property_x2() = xend;
165 StreamView::set_amplitude_above_axis (gdouble app)
168 AudioRegionViewList::iterator i;
174 _amplitude_above_axis = app;
176 for (i = region_views.begin(); i != region_views.end(); ++i) {
177 (*i)->set_amplitude_above_axis (app);
184 StreamView::add_region_view (Region *r)
186 add_region_view_internal (r, true);
190 StreamView::add_region_view_internal (Region *r, bool wait_for_waves)
192 ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::add_region_view), r));
194 AudioRegion* region = dynamic_cast<AudioRegion*> (r);
200 AudioRegionView *region_view;
201 list<AudioRegionView *>::iterator i;
203 for (i = region_views.begin(); i != region_views.end(); ++i) {
204 if (&(*i)->region == region) {
206 /* great. we already have a AudioRegionView for this Region. use it again.
209 (*i)->set_valid (true);
214 switch (_trackview.audio_track()->mode()) {
216 region_view = new AudioRegionView (canvas_group, _trackview, *region,
217 _samples_per_unit, region_color);
220 region_view = new TapeAudioRegionView (canvas_group, _trackview, *region,
221 _samples_per_unit, region_color);
225 region_view->init (_amplitude_above_axis, region_color, wait_for_waves);
226 region_views.push_front (region_view);
228 /* follow global waveform setting */
230 region_view->set_waveform_visible(_trackview.editor.show_waveforms());
232 /* catch regionview going away */
234 region->GoingAway.connect (mem_fun (*this, &StreamView::remove_region_view));
236 AudioRegionViewAdded (region_view);
240 StreamView::remove_region_view (Region *r)
242 ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_region_view), r));
244 AudioRegion* ar = dynamic_cast<AudioRegion*> (r);
250 for (list<AudioRegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
251 if (&((*i)->region) == ar) {
253 region_views.erase (i);
258 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
259 list<CrossfadeView*>::iterator tmp;
264 if ((*i)->crossfade.involves (*ar)) {
266 crossfade_views.erase (i);
274 StreamView::remove_rec_region (Region *r)
276 ENSURE_GUI_THREAD(bind (mem_fun (*this, &StreamView::remove_rec_region), r));
278 if (!Gtkmm2ext::UI::instance()->caller_is_gui_thread()) {
279 fatal << "region deleted from non-GUI thread!" << endmsg;
283 AudioRegion* ar = dynamic_cast<AudioRegion*> (r);
289 for (list<AudioRegion *>::iterator i = rec_regions.begin(); i != rec_regions.end(); ++i) {
291 rec_regions.erase (i);
298 StreamView::undisplay_diskstream ()
301 for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
305 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
309 region_views.clear();
310 crossfade_views.clear ();
314 StreamView::display_diskstream (DiskStream *ds)
316 playlist_change_connection.disconnect();
317 playlist_changed (ds);
318 playlist_change_connection = ds->PlaylistChanged.connect (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
322 StreamView::playlist_modified ()
324 ENSURE_GUI_THREAD (mem_fun (*this, &StreamView::playlist_modified));
326 /* if the playlist is modified, make sure xfades are on top and all the regionviews are stacked
330 for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
334 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
335 (*i)->get_canvas_group()->raise_to_top();
340 StreamView::playlist_changed (DiskStream *ds)
342 ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_changed), ds));
344 /* disconnect from old playlist */
346 for (vector<sigc::connection>::iterator i = playlist_connections.begin(); i != playlist_connections.end(); ++i) {
350 playlist_connections.clear();
351 undisplay_diskstream ();
355 redisplay_diskstream ();
359 playlist_connections.push_back (ds->playlist()->RegionAdded.connect (mem_fun (*this, &StreamView::add_region_view)));
360 playlist_connections.push_back (ds->playlist()->RegionRemoved.connect (mem_fun (*this, &StreamView::remove_region_view)));
361 playlist_connections.push_back (ds->playlist()->StateChanged.connect (mem_fun (*this, &StreamView::playlist_state_changed)));
362 playlist_connections.push_back (ds->playlist()->Modified.connect (mem_fun (*this, &StreamView::playlist_modified)));
363 playlist_connections.push_back (ds->playlist()->NewCrossfade.connect (mem_fun (*this, &StreamView::add_crossfade)));
367 StreamView::add_crossfade (Crossfade *crossfade)
369 AudioRegionView* lview = 0;
370 AudioRegionView* rview = 0;
372 ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::add_crossfade), crossfade));
374 /* first see if we already have a CrossfadeView for this Crossfade */
376 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
377 if (&(*i)->crossfade == crossfade) {
378 if (!crossfades_visible) {
383 (*i)->set_valid (true);
388 /* create a new one */
390 for (list<AudioRegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
391 if (!lview && &((*i)->region) == &crossfade->out()) {
394 if (!rview && &((*i)->region) == &crossfade->in()) {
399 CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display,
406 crossfade->Invalidated.connect (mem_fun (*this, &StreamView::remove_crossfade));
407 crossfade_views.push_back (cv);
409 if (!crossfades_visible) {
415 StreamView::remove_crossfade (Crossfade *xfade)
417 ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::remove_crossfade), xfade));
419 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
420 if (&(*i)->crossfade == xfade) {
422 crossfade_views.erase (i);
429 StreamView::playlist_state_changed (Change ignored)
431 ENSURE_GUI_THREAD (bind (mem_fun (*this, &StreamView::playlist_state_changed), ignored));
433 redisplay_diskstream ();
437 StreamView::redisplay_diskstream ()
439 list<AudioRegionView *>::iterator i, tmp;
440 list<CrossfadeView*>::iterator xi, tmpx;
443 for (i = region_views.begin(); i != region_views.end(); ++i) {
444 (*i)->set_valid (false);
447 for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
448 (*xi)->set_valid (false);
449 if ((*xi)->visible()) {
454 if (_trackview.is_audio_track()) {
455 _trackview.get_diskstream()->playlist()->foreach_region (this, &StreamView::add_region_view);
456 _trackview.get_diskstream()->playlist()->foreach_crossfade (this, &StreamView::add_crossfade);
459 for (i = region_views.begin(); i != region_views.end(); ) {
463 if (!(*i)->is_valid()) {
465 region_views.erase (i);
471 for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
475 if (!(*xi)->valid()) {
477 crossfade_views.erase (xi);
483 /* now fix layering */
485 playlist_modified ();
489 StreamView::diskstream_changed (void *src_ignored)
493 if ((at = _trackview.audio_track()) != 0) {
494 DiskStream& ds = at->disk_stream();
495 /* XXX grrr: when will SigC++ allow me to bind references? */
496 Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun (*this, &StreamView::display_diskstream), &ds));
498 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::undisplay_diskstream));
503 StreamView::apply_color (Gdk::Color& color, ColorTarget target)
506 list<AudioRegionView *>::iterator i;
510 region_color = color;
511 for (i = region_views.begin(); i != region_views.end(); ++i) {
512 (*i)->set_color (region_color);
514 // stream_base_color = RGBA_TO_UINT (color.red/256, color.green/256, color.blue/256, 255);
515 // gnome_canvas_item_set (canvas_rect, "fill_color_rgba", stream_base_color, NULL);
518 case StreamBaseColor:
519 // stream_base_color = RGBA_TO_UINT (color.red/256, color.green/256, color.blue/256, 255);
520 // gnome_canvas_item_set (canvas_rect, "fill_color_rgba", stream_base_color, NULL);
526 StreamView::set_show_waveforms (bool yn)
528 for (list<AudioRegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
529 (*i)->set_waveform_visible (yn);
534 StreamView::set_selected_regionviews (AudioRegionSelection& regions)
538 for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
542 for (AudioRegionSelection::iterator ii = regions.begin(); ii != regions.end(); ++ii) {
548 (*i)->set_selected (selected, this);
553 StreamView::get_selectables (jack_nframes_t start, jack_nframes_t end, list<Selectable*>& results)
555 for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
556 if ((*i)->region.coverage(start, end) != OverlapNone) {
557 results.push_back (*i);
563 StreamView::get_inverted_selectables (Selection& sel, list<Selectable*>& results)
565 for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
566 if (!sel.audio_regions.contains (*i)) {
567 results.push_back (*i);
573 StreamView::set_waveform_shape (WaveformShape shape)
575 for (AudioRegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
576 (*i)->set_waveform_shape (shape);
581 StreamView::region_layered (AudioRegionView* rv)
583 rv->get_canvas_group()->lower_to_bottom();
585 /* don't ever leave it at the bottom, since then it doesn't
586 get events - the parent group does instead ...
589 rv->get_canvas_group()->raise (rv->region.layer() + 1);
593 StreamView::rec_enable_changed (void *src)
595 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
599 StreamView::sess_rec_enable_changed ()
601 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
605 StreamView::transport_changed()
607 Gtkmm2ext::UI::instance()->call_slot (mem_fun (*this, &StreamView::setup_rec_box));
611 StreamView::setup_rec_box ()
613 // cerr << _trackview.name() << " streamview SRB\n";
615 if (_trackview.session().transport_rolling()) {
617 // cerr << "\trolling\n";
620 && _trackview.session().record_status() == Session::Recording
621 && _trackview.get_diskstream()->record_enabled()) {
623 if (use_rec_regions && rec_regions.size() == rec_rects.size()) {
624 /* add a new region, but don't bother if they set use_rec_regions mid-record */
626 AudioRegion::SourceList sources;
628 for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
631 peak_ready_connections.clear();
633 for (uint32_t n=0; n < _trackview.get_diskstream()->n_channels(); ++n) {
634 Source *src = (Source *) _trackview.get_diskstream()->write_source (n);
636 sources.push_back (src);
637 peak_ready_connections.push_back (src->PeakRangeReady.connect (bind (mem_fun (*this, &StreamView::rec_peak_range_ready), src)));
643 jack_nframes_t start = 0;
644 if (rec_regions.size() > 0) {
645 start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
648 AudioRegion * region = new AudioRegion(sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false);
649 region->set_position (_trackview.session().transport_frame(), this);
650 rec_regions.push_back (region);
651 /* catch it if it goes away */
652 region->GoingAway.connect (mem_fun (*this, &StreamView::remove_rec_region));
654 /* we add the region later */
657 /* start a new rec box */
661 at = _trackview.audio_track(); /* we know what it is already */
662 DiskStream& ds = at->disk_stream();
663 jack_nframes_t frame_pos = ds.current_capture_start ();
664 gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
665 gdouble xend = xstart;
667 ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
668 rec_rect->property_x1() = xstart;
669 rec_rect->property_y1() = 1.0;
670 rec_rect->property_x2() = xend;
671 rec_rect->property_y2() = (double) _trackview.height - 1;
672 rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
673 rec_rect->property_fill_color_rgba() = color_map[cRecordingRectFill];
676 recbox.rectangle = rec_rect;
677 recbox.start = _trackview.session().transport_frame();
680 rec_rects.push_back (recbox);
682 screen_update_connection.disconnect();
683 screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &StreamView::update_rec_box));
687 } else if (rec_active &&
688 (_trackview.session().record_status() != Session::Recording ||
689 !_trackview.get_diskstream()->record_enabled())) {
691 screen_update_connection.disconnect();
693 rec_updating = false;
699 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
701 if (!rec_rects.empty() || !rec_regions.empty()) {
703 /* disconnect rapid update */
704 screen_update_connection.disconnect();
706 for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
709 peak_ready_connections.clear();
711 rec_updating = false;
713 last_rec_peak_frame = 0;
715 /* remove temp regions */
716 for (list<AudioRegion*>::iterator iter=rec_regions.begin(); iter != rec_regions.end(); )
718 list<AudioRegion*>::iterator tmp;
723 /* this will trigger the remove_region_view */
731 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
734 /* transport stopped, clear boxes */
735 for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
736 RecBoxInfo &rect = (*iter);
737 delete rect.rectangle;
748 StreamView::update_rec_box ()
750 /* only update the last box */
751 if (rec_active && rec_rects.size() > 0) {
752 RecBoxInfo & rect = rec_rects.back();
753 jack_nframes_t at = _trackview.get_diskstream()->current_capture_end();
755 rect.length = at - rect.start;
757 gdouble xstart = _trackview.editor.frame_to_pixel ( rect.start );
758 gdouble xend = _trackview.editor.frame_to_pixel ( at );
760 rect.rectangle->property_x1() = xstart;
761 rect.rectangle->property_x2() = xend;
766 StreamView::find_view (const AudioRegion& region)
768 for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
770 if (&(*i)->region == ®ion) {
778 StreamView::foreach_regionview (sigc::slot<void,AudioRegionView*> slot)
780 for (list<AudioRegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
786 StreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
788 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
794 StreamView::rec_peak_range_ready (jack_nframes_t start, jack_nframes_t cnt, Source * src)
796 // this is called from the peak building thread
798 ENSURE_GUI_THREAD(bind (mem_fun (*this, &StreamView::rec_peak_range_ready), start, cnt, src));
800 if (rec_peak_ready_map.size() == 0 || start+cnt > last_rec_peak_frame) {
801 last_rec_peak_frame = start + cnt;
804 rec_peak_ready_map[src] = true;
806 if (rec_peak_ready_map.size() == _trackview.get_diskstream()->n_channels()) {
807 this->update_rec_regions ();
808 rec_peak_ready_map.clear();
813 StreamView::update_rec_regions ()
815 if (use_rec_regions) {
819 for (list<AudioRegion*>::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
821 list<AudioRegion*>::iterator tmp;
826 if (!canvas_item_visible (rec_rects[n].rectangle)) {
827 /* rect already hidden, this region is done */
832 AudioRegion * region = (*iter);
833 jack_nframes_t origlen = region->length();
835 if (region == rec_regions.back() && rec_active) {
837 if (last_rec_peak_frame > region->start()) {
839 jack_nframes_t nlen = last_rec_peak_frame - region->start();
841 if (nlen != region->length()) {
844 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
845 region->set_length (nlen, this);
846 region->thaw ("updated");
849 /* our special initial length */
850 add_region_view_internal (region, false);
853 /* also update rect */
854 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
855 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
856 rect->property_x2() = xend;
862 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
864 if (nlen != region->length()) {
866 if (region->source(0).length() >= region->start() + nlen) {
869 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
870 region->set_length (nlen, this);
871 region->thaw ("updated");
874 /* our special initial length */
875 add_region_view_internal (region, false);
879 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
892 StreamView::show_all_xfades ()
894 foreach_crossfadeview (&CrossfadeView::show);
895 crossfades_visible = true;
899 StreamView::hide_all_xfades ()
901 foreach_crossfadeview (&CrossfadeView::hide);
902 crossfades_visible = false;
906 StreamView::hide_xfades_involving (AudioRegionView& rv)
908 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
909 if ((*i)->crossfade.involves (rv.region)) {
916 StreamView::reveal_xfades_involving (AudioRegionView& rv)
918 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
919 if ((*i)->crossfade.involves (rv.region) && (*i)->visible()) {
926 StreamView::color_handler (ColorID id, uint32_t val)
929 case cAudioTrackBase:
930 if (_trackview.is_audio_track()) {
931 canvas_rect->property_fill_color_rgba() = val;
935 if (!_trackview.is_audio_track()) {
936 canvas_rect->property_fill_color_rgba() = val;
939 case cAudioTrackOutline:
940 canvas_rect->property_outline_color_rgba() = val;