2 Copyright (C) 2001, 2006 Paul Davis
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.
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.
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.
25 #include <gtkmm2ext/gtk_ui.h>
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/playlist_templates.h"
33 #include "ardour/source.h"
34 #include "ardour/region_factory.h"
35 #include "ardour/profile.h"
37 #include "audio_streamview.h"
38 #include "audio_region_view.h"
39 #include "tape_region_view.h"
40 #include "audio_time_axis.h"
41 #include "canvas-waveview.h"
42 #include "canvas-simplerect.h"
43 #include "region_selection.h"
44 #include "selection.h"
45 #include "public_editor.h"
46 #include "ardour_ui.h"
47 #include "crossfade_view.h"
48 #include "rgb_macros.h"
49 #include "gui_thread.h"
55 using namespace ARDOUR;
57 using namespace Editing;
59 AudioStreamView::AudioStreamView (AudioTimeAxisView& tv)
62 crossfades_visible = true;
63 _waveform_scale = LinearWaveform;
64 _waveform_shape = Traditional;
66 _amplitude_above_axis = 1.0;
68 use_rec_regions = tv.editor().show_waveforms_recording ();
71 AudioStreamView::~AudioStreamView ()
76 AudioStreamView::set_samples_per_unit (gdouble spp)
78 StreamView::set_samples_per_unit(spp);
80 for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
81 (*xi)->set_samples_per_unit (spp);
88 AudioStreamView::set_amplitude_above_axis (gdouble app)
90 RegionViewList::iterator i;
96 _amplitude_above_axis = app;
98 for (i = region_views.begin(); i != region_views.end(); ++i) {
99 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
101 arv->set_amplitude_above_axis (app);
108 AudioStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wait_for_waves, bool recording)
110 AudioRegionView *region_view = 0;
111 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
118 // for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
119 // if ((*i)->region() == r) {
120 // cerr << "audio_streamview in add_region_view_internal region found" << endl;
121 /* great. we already have a AudioRegionView for this Region. use it again. */
123 // (*i)->set_valid (true);
125 // this might not be necessary
126 // AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
129 // arv->set_waveform_scale (_waveform_scale);
130 // arv->set_waveform_shape (_waveform_shape);
138 switch (_trackview.audio_track()->mode()) {
143 region_view = new AudioRegionView (canvas_group, _trackview, region,
144 _samples_per_unit, region_color, recording, TimeAxisViewItem::Visibility(
145 TimeAxisViewItem::ShowFrame | TimeAxisViewItem::HideFrameRight));
147 region_view = new AudioRegionView (canvas_group, _trackview, region,
148 _samples_per_unit, region_color);
152 region_view = new TapeAudioRegionView (canvas_group, _trackview, region,
153 _samples_per_unit, region_color);
156 fatal << string_compose (_("programming error: %1"), "illegal track mode in ::add_region_view_internal") << endmsg;
161 region_view->init (region_color, wait_for_waves);
162 region_view->set_amplitude_above_axis(_amplitude_above_axis);
163 region_view->set_height (child_height ());
164 region_views.push_front (region_view);
166 /* if its the special single-sample length that we use for rec-regions, make it
167 insensitive to events
170 if (region->length() == 1) {
171 region_view->set_sensitive (false);
174 /* if this was the first one, then lets query the waveform scale and shape.
175 otherwise, we set it to the current value */
177 if (region_views.size() == 1) {
179 if (region_view->waveform_logscaled()) {
180 _waveform_scale = LogWaveform;
182 _waveform_scale = LinearWaveform;
185 if (region_view->waveform_rectified()) {
186 _waveform_shape = Rectified;
188 _waveform_shape = Traditional;
192 region_view->set_waveform_scale(_waveform_scale);
193 region_view->set_waveform_shape(_waveform_shape);
196 /* follow global waveform setting */
197 region_view->set_waveform_visible(_trackview.editor().show_waveforms());
199 /* catch regionview going away */
200 region->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_region_view), boost::weak_ptr<Region> (r)));
202 RegionViewAdded (region_view);
208 AudioStreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
210 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), weak_r));
212 boost::shared_ptr<Region> r (weak_r.lock());
218 if (!_trackview.session().deletion_in_progress()) {
220 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
221 list<CrossfadeView*>::iterator tmp;
226 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
227 if (ar && (*i)->crossfade->involves (ar)) {
229 crossfade_views.erase (i);
236 StreamView::remove_region_view(r);
240 AudioStreamView::undisplay_diskstream ()
242 StreamView::undisplay_diskstream();
244 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
248 crossfade_views.clear ();
252 AudioStreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
254 boost::shared_ptr<Diskstream> sp (ds.lock());
256 playlist_modified (sp);
261 AudioStreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
263 /* we do not allow shared_ptr<T> to be bound to slots */
264 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_modified_weak), ds));
266 StreamView::playlist_modified (ds);
268 /* make sure xfades are on top and all the regionviews are stacked correctly. */
270 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
271 (*i)->get_canvas_group()->raise_to_top();
276 AudioStreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
278 boost::shared_ptr<Diskstream> sp (ds.lock());
280 playlist_changed (sp);
285 AudioStreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
287 ENSURE_GUI_THREAD (bind (
288 mem_fun (*this, &AudioStreamView::playlist_changed_weak),
289 boost::weak_ptr<Diskstream> (ds)));
291 StreamView::playlist_changed(ds);
293 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(ds->playlist());
295 playlist_connections.push_back (apl->NewCrossfade.connect (
296 mem_fun (*this, &AudioStreamView::add_crossfade)));
301 AudioStreamView::add_crossfade_weak (boost::weak_ptr<Crossfade> crossfade)
303 boost::shared_ptr<Crossfade> sp (crossfade.lock());
313 AudioStreamView::add_crossfade (boost::shared_ptr<Crossfade> crossfade)
315 AudioRegionView* lview = 0;
316 AudioRegionView* rview = 0;
318 /* we do not allow shared_ptr<T> to be bound to slots */
320 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade_weak), boost::weak_ptr<Crossfade> (crossfade)));
322 /* first see if we already have a CrossfadeView for this Crossfade */
324 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
325 if ((*i)->crossfade == crossfade) {
327 if (!crossfades_visible || _layer_display == Stacked) {
332 (*i)->set_valid (true);
337 /* create a new one */
339 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
340 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
342 if (!lview && arv && (arv->region() == crossfade->out())) {
345 if (!rview && arv && (arv->region() == crossfade->in())) {
350 CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display (),
356 cv->set_valid (true);
357 crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
358 crossfade_views.push_back (cv);
359 if (!Config->get_xfades_visible() || !crossfades_visible || _layer_display == Stacked) {
365 AudioStreamView::remove_crossfade (boost::shared_ptr<Region> r)
367 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), r));
369 boost::shared_ptr<Crossfade> xfade = boost::dynamic_pointer_cast<Crossfade> (r);
371 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
372 if ((*i)->crossfade == xfade) {
374 crossfade_views.erase (i);
381 AudioStreamView::redisplay_diskstream ()
383 list<RegionView *>::iterator i;
384 list<CrossfadeView*>::iterator xi, tmpx;
386 // Flag region views as invalid and disable drawing
387 for (i = region_views.begin(); i != region_views.end(); ++i) {
388 (*i)->set_valid (false);
389 (*i)->enable_display (false);
392 // Flag crossfade views as invalid
393 for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
394 (*xi)->set_valid (false);
395 if ((*xi)->visible() && _layer_display != Stacked) {
400 // Add and display region and crossfade views, and flag them as valid
402 if (_trackview.is_audio_track()) {
403 _trackview.get_diskstream()->playlist()->foreach_region(
404 static_cast<StreamView*>(this),
405 &StreamView::add_region_view);
407 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(
408 _trackview.get_diskstream()->playlist());
410 apl->foreach_crossfade (this, &AudioStreamView::add_crossfade);
413 // Remove invalid crossfade views
414 for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
418 if (!(*xi)->valid()) {
420 crossfade_views.erase (xi);
426 // Stack regions by layer, and remove invalid regions
431 AudioStreamView::set_show_waveforms (bool yn)
433 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
434 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
436 arv->set_waveform_visible (yn);
442 AudioStreamView::set_waveform_shape (WaveformShape shape)
444 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
445 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
447 arv->set_waveform_shape (shape);
449 _waveform_shape = shape;
453 AudioStreamView::set_waveform_scale (WaveformScale scale)
455 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
456 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
458 arv->set_waveform_scale (scale);
460 _waveform_scale = scale;
464 AudioStreamView::setup_rec_box ()
466 //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
468 if (_trackview.session().transport_rolling()) {
470 // cerr << "\trolling\n";
473 _trackview.session().record_status() == Session::Recording &&
474 _trackview.get_diskstream()->record_enabled()) {
475 if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
477 /* add a new region, but don't bother if they set use_rec_regions mid-record */
481 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
484 rec_data_ready_connections.clear();
487 boost::shared_ptr<AudioDiskstream> ads = boost::dynamic_pointer_cast<AudioDiskstream>(_trackview.get_diskstream());
490 for (uint32_t n=0; n < ads->n_channels().n_audio(); ++n) {
491 boost::shared_ptr<AudioFileSource> src = boost::static_pointer_cast<AudioFileSource> (ads->write_source (n));
493 sources.push_back (src);
495 rec_data_ready_connections.push_back (src->PeakRangeReady.connect (bind
496 (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), boost::weak_ptr<Source>(src))));
503 if (rec_regions.size() > 0) {
504 start = rec_regions.back().first->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
507 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>
508 (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
510 region->set_position (_trackview.session().transport_frame(), this);
511 rec_regions.push_back (make_pair(region, (RegionView*)0));
514 /* start a new rec box */
516 boost::shared_ptr<AudioTrack> at;
518 at = _trackview.audio_track(); /* we know what it is already */
519 boost::shared_ptr<AudioDiskstream> ds = at->audio_diskstream();
520 nframes_t frame_pos = ds->current_capture_start ();
521 gdouble xstart = _trackview.editor().frame_to_pixel (frame_pos);
525 switch (_trackview.audio_track()->mode()) {
529 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
534 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
535 /* make the recording rect translucent to allow
536 the user to see the peak data coming in, etc.
538 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
542 ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
543 rec_rect->property_x1() = xstart;
544 rec_rect->property_y1() = 1.0;
545 rec_rect->property_x2() = xend;
546 rec_rect->property_y2() = child_height ();
547 rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
548 rec_rect->property_outline_what() = 0x1 | 0x2 | 0x4 | 0x8;
549 rec_rect->property_fill_color_rgba() = fill_color;
550 rec_rect->lower_to_bottom();
553 recbox.rectangle = rec_rect;
554 recbox.start = _trackview.session().transport_frame();
557 rec_rects.push_back (recbox);
559 screen_update_connection.disconnect();
560 screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &AudioStreamView::update_rec_box));
564 } else if (rec_active &&
565 (_trackview.session().record_status() != Session::Recording ||
566 !_trackview.get_diskstream()->record_enabled())) {
567 screen_update_connection.disconnect();
569 rec_updating = false;
574 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
576 if (!rec_rects.empty() || !rec_regions.empty()) {
578 /* disconnect rapid update */
579 screen_update_connection.disconnect();
581 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
584 rec_data_ready_connections.clear();
586 rec_updating = false;
589 /* remove temp regions */
591 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); ) {
592 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
597 (*iter).first->drop_references ();
604 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
606 /* transport stopped, clear boxes */
607 for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
608 RecBoxInfo &rect = (*iter);
609 delete rect.rectangle;
619 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
621 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
627 AudioStreamView::rec_peak_range_ready (nframes_t start, nframes_t cnt, boost::weak_ptr<Source> weak_src)
629 ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, weak_src));
631 boost::shared_ptr<Source> src (weak_src.lock());
637 // this is called from the peak building thread
639 if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
640 last_rec_data_frame = start + cnt;
643 rec_data_ready_map[src] = true;
645 if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().n_audio()) {
646 this->update_rec_regions ();
647 rec_data_ready_map.clear();
652 AudioStreamView::update_rec_regions ()
654 if (use_rec_regions) {
657 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
659 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
664 if (!canvas_item_visible (rec_rects[n].rectangle)) {
665 /* rect already hidden, this region is done */
670 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(iter->first);
675 nframes_t origlen = region->length();
677 if (region == rec_regions.back().first && rec_active) {
679 if (last_rec_data_frame > region->start()) {
681 nframes_t nlen = last_rec_data_frame - region->start();
683 if (nlen != region->length()) {
686 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
687 region->set_length (nlen, this);
688 region->thaw ("updated");
691 /* our special initial length */
692 add_region_view_internal (region, false, true);
695 /* also update rect */
696 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
697 gdouble xend = _trackview.editor().frame_to_pixel (region->position() + region->length());
698 rect->property_x2() = xend;
704 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
706 if (nlen != region->length()) {
708 if (region->source_length(0) >= region->start() + nlen) {
711 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
712 region->set_length (nlen, this);
713 region->thaw ("updated");
716 /* our special initial length */
717 add_region_view_internal (region, false, true);
721 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
734 AudioStreamView::show_all_fades ()
736 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
737 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
739 arv->set_fade_visibility (true);
745 AudioStreamView::hide_all_fades ()
747 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
748 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
750 arv->set_fade_visibility (false);
756 AudioStreamView::show_all_xfades ()
758 foreach_crossfadeview (&CrossfadeView::show);
759 crossfades_visible = true;
763 AudioStreamView::hide_all_xfades ()
765 foreach_crossfadeview (&CrossfadeView::hide);
766 crossfades_visible = false;
770 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
772 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
773 if ((*i)->crossfade->involves (rv.audio_region())) {
780 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
782 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
783 if ((*i)->crossfade->involves (rv.audio_region()) && (*i)->visible() && _layer_display != Stacked) {
790 AudioStreamView::color_handler ()
792 //case cAudioTrackBase:
793 if (_trackview.is_track()) {
794 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase.get();
797 //case cAudioBusBase:
798 if (!_trackview.is_track()) {
799 if (Profile->get_sae() && _trackview.route()->is_master()) {
800 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase.get();
802 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase.get();
808 AudioStreamView::update_contents_height ()
810 StreamView::update_contents_height ();
812 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
813 if (_layer_display == Overlaid) {
815 (*i)->set_height (height);