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/source.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/profile.h"
36 #include "audio_streamview.h"
37 #include "audio_region_view.h"
38 #include "tape_region_view.h"
39 #include "audio_time_axis.h"
40 #include "canvas-waveview.h"
41 #include "canvas-simplerect.h"
42 #include "region_selection.h"
43 #include "selection.h"
44 #include "public_editor.h"
45 #include "ardour_ui.h"
46 #include "crossfade_view.h"
47 #include "rgb_macros.h"
48 #include "gui_thread.h"
54 using namespace ARDOUR;
56 using namespace Editing;
58 AudioStreamView::AudioStreamView (AudioTimeAxisView& tv)
61 crossfades_visible = true;
62 _waveform_scale = LinearWaveform;
63 _waveform_shape = Traditional;
65 _amplitude_above_axis = 1.0;
67 use_rec_regions = tv.editor().show_waveforms_recording ();
70 AudioStreamView::~AudioStreamView ()
75 AudioStreamView::set_samples_per_unit (gdouble spp)
77 StreamView::set_samples_per_unit(spp);
79 for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
80 (*xi)->set_samples_per_unit (spp);
87 AudioStreamView::set_amplitude_above_axis (gdouble app)
89 RegionViewList::iterator i;
95 _amplitude_above_axis = app;
97 for (i = region_views.begin(); i != region_views.end(); ++i) {
98 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
100 arv->set_amplitude_above_axis (app);
107 AudioStreamView::create_region_view (boost::shared_ptr<Region> r, bool wait_for_waves, bool recording)
109 AudioRegionView *region_view = 0;
110 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
116 switch (_trackview.audio_track()->mode()) {
121 region_view = new AudioRegionView (canvas_group, _trackview, region,
122 _samples_per_unit, region_color, recording, TimeAxisViewItem::Visibility(
123 TimeAxisViewItem::ShowFrame | TimeAxisViewItem::HideFrameRight));
125 region_view = new AudioRegionView (canvas_group, _trackview, region,
126 _samples_per_unit, region_color);
130 region_view = new TapeAudioRegionView (canvas_group, _trackview, region,
131 _samples_per_unit, region_color);
134 fatal << string_compose (_("programming error: %1"), "illegal track mode in ::add_region_view_internal") << endmsg;
139 region_view->init (region_color, wait_for_waves);
140 region_view->set_amplitude_above_axis(_amplitude_above_axis);
141 region_view->set_height (child_height ());
143 /* if its the special single-sample length that we use for rec-regions, make it
144 insensitive to events
147 if (region->length() == 1) {
148 region_view->set_sensitive (false);
151 /* if this was the first one, then lets query the waveform scale and shape.
152 otherwise, we set it to the current value */
154 if (region_views.size() == 1) {
156 if (region_view->waveform_logscaled()) {
157 _waveform_scale = LogWaveform;
159 _waveform_scale = LinearWaveform;
162 if (region_view->waveform_rectified()) {
163 _waveform_shape = Rectified;
165 _waveform_shape = Traditional;
169 region_view->set_waveform_scale(_waveform_scale);
170 region_view->set_waveform_shape(_waveform_shape);
173 /* follow global waveform setting */
174 region_view->set_waveform_visible(_trackview.editor().show_waveforms());
180 AudioStreamView::add_region_view_internal (boost::shared_ptr<Region> r, bool wait_for_waves, bool recording)
182 RegionView *region_view = create_region_view (r, wait_for_waves, recording);
183 if (region_view == 0) {
188 // for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
189 // if ((*i)->region() == r) {
190 // cerr << "audio_streamview in add_region_view_internal region found" << endl;
191 /* great. we already have a AudioRegionView for this Region. use it again. */
193 // (*i)->set_valid (true);
195 // this might not be necessary
196 // AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
199 // arv->set_waveform_scale (_waveform_scale);
200 // arv->set_waveform_shape (_waveform_shape);
208 region_views.push_front (region_view);
210 /* catch regionview going away */
211 r->GoingAway.connect (bind (mem_fun (*this, &AudioStreamView::remove_region_view), boost::weak_ptr<Region> (r)));
213 RegionViewAdded (region_view);
219 AudioStreamView::remove_region_view (boost::weak_ptr<Region> weak_r)
221 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), weak_r));
223 boost::shared_ptr<Region> r (weak_r.lock());
229 if (!_trackview.session().deletion_in_progress()) {
231 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
232 list<CrossfadeView*>::iterator tmp;
237 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
238 if (ar && (*i)->crossfade->involves (ar)) {
240 crossfade_views.erase (i);
247 StreamView::remove_region_view(r);
251 AudioStreamView::undisplay_diskstream ()
253 StreamView::undisplay_diskstream();
255 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
259 crossfade_views.clear ();
263 AudioStreamView::playlist_modified_weak (boost::weak_ptr<Diskstream> ds)
265 boost::shared_ptr<Diskstream> sp (ds.lock());
267 playlist_modified (sp);
272 AudioStreamView::playlist_modified (boost::shared_ptr<Diskstream> ds)
274 /* we do not allow shared_ptr<T> to be bound to slots */
275 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_modified_weak), ds));
277 StreamView::playlist_modified (ds);
279 /* make sure xfades are on top and all the regionviews are stacked correctly. */
281 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
282 (*i)->get_canvas_group()->raise_to_top();
287 AudioStreamView::playlist_changed_weak (boost::weak_ptr<Diskstream> ds)
289 boost::shared_ptr<Diskstream> sp (ds.lock());
291 playlist_changed (sp);
296 AudioStreamView::playlist_changed (boost::shared_ptr<Diskstream> ds)
298 ENSURE_GUI_THREAD (bind (
299 mem_fun (*this, &AudioStreamView::playlist_changed_weak),
300 boost::weak_ptr<Diskstream> (ds)));
302 StreamView::playlist_changed(ds);
304 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(ds->playlist());
306 playlist_connections.push_back (apl->NewCrossfade.connect (
307 mem_fun (*this, &AudioStreamView::add_crossfade)));
312 AudioStreamView::add_crossfade_weak (boost::weak_ptr<Crossfade> crossfade)
314 boost::shared_ptr<Crossfade> sp (crossfade.lock());
324 AudioStreamView::add_crossfade (boost::shared_ptr<Crossfade> crossfade)
326 AudioRegionView* lview = 0;
327 AudioRegionView* rview = 0;
329 /* we do not allow shared_ptr<T> to be bound to slots */
331 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade_weak), boost::weak_ptr<Crossfade> (crossfade)));
333 /* first see if we already have a CrossfadeView for this Crossfade */
335 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
336 if ((*i)->crossfade == crossfade) {
338 if (!crossfades_visible || _layer_display == Stacked) {
343 (*i)->set_valid (true);
348 /* create a new one */
350 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
351 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
353 if (!lview && arv && (arv->region() == crossfade->out())) {
356 if (!rview && arv && (arv->region() == crossfade->in())) {
361 CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display (),
367 cv->set_valid (true);
368 crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
369 crossfade_views.push_back (cv);
370 if (!_trackview.session().config.get_xfades_visible() || !crossfades_visible || _layer_display == Stacked) {
376 AudioStreamView::remove_crossfade (boost::shared_ptr<Region> r)
378 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), r));
380 boost::shared_ptr<Crossfade> xfade = boost::dynamic_pointer_cast<Crossfade> (r);
382 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
383 if ((*i)->crossfade == xfade) {
385 crossfade_views.erase (i);
392 AudioStreamView::redisplay_diskstream ()
394 list<RegionView *>::iterator i;
395 list<CrossfadeView*>::iterator xi, tmpx;
397 // Flag region views as invalid and disable drawing
398 for (i = region_views.begin(); i != region_views.end(); ++i) {
399 (*i)->set_valid (false);
400 (*i)->enable_display (false);
403 // Flag crossfade views as invalid
404 for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
405 (*xi)->set_valid (false);
406 if ((*xi)->visible() && _layer_display != Stacked) {
411 // Add and display region and crossfade views, and flag them as valid
413 if (_trackview.is_audio_track()) {
414 _trackview.get_diskstream()->playlist()->foreach_region(
415 sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view))
418 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(
419 _trackview.get_diskstream()->playlist()
423 apl->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade));
427 // Remove invalid crossfade views
428 for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
432 if (!(*xi)->valid()) {
434 crossfade_views.erase (xi);
440 // Stack regions by layer, and remove invalid regions
445 AudioStreamView::set_show_waveforms (bool yn)
447 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
448 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
450 arv->set_waveform_visible (yn);
456 AudioStreamView::set_waveform_shape (WaveformShape shape)
458 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
459 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
461 arv->set_waveform_shape (shape);
463 _waveform_shape = shape;
467 AudioStreamView::set_waveform_scale (WaveformScale scale)
469 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
470 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
472 arv->set_waveform_scale (scale);
474 _waveform_scale = scale;
478 AudioStreamView::setup_rec_box ()
480 //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
482 if (_trackview.session().transport_rolling()) {
484 // cerr << "\trolling\n";
487 _trackview.session().record_status() == Session::Recording &&
488 _trackview.get_diskstream()->record_enabled()) {
489 if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
491 /* add a new region, but don't bother if they set use_rec_regions mid-record */
495 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
498 rec_data_ready_connections.clear();
501 boost::shared_ptr<AudioDiskstream> ads = boost::dynamic_pointer_cast<AudioDiskstream>(_trackview.get_diskstream());
504 for (uint32_t n=0; n < ads->n_channels().n_audio(); ++n) {
505 boost::shared_ptr<AudioFileSource> src = boost::static_pointer_cast<AudioFileSource> (ads->write_source (n));
507 sources.push_back (src);
509 rec_data_ready_connections.push_back (src->PeakRangeReady.connect (bind
510 (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), boost::weak_ptr<Source>(src))));
517 if (rec_regions.size() > 0) {
518 start = rec_regions.back().first->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
521 boost::shared_ptr<AudioRegion> region (boost::dynamic_pointer_cast<AudioRegion>
522 (RegionFactory::create (sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false)));
524 region->set_position (_trackview.session().transport_frame(), this);
525 rec_regions.push_back (make_pair(region, (RegionView*)0));
528 /* start a new rec box */
530 boost::shared_ptr<AudioTrack> at;
532 at = _trackview.audio_track(); /* we know what it is already */
533 boost::shared_ptr<AudioDiskstream> ds = at->audio_diskstream();
534 nframes_t frame_pos = ds->current_capture_start ();
535 gdouble xstart = _trackview.editor().frame_to_pixel (frame_pos);
539 switch (_trackview.audio_track()->mode()) {
543 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
548 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
549 /* make the recording rect translucent to allow
550 the user to see the peak data coming in, etc.
552 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
556 ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
557 rec_rect->property_x1() = xstart;
558 rec_rect->property_y1() = 1.0;
559 rec_rect->property_x2() = xend;
560 rec_rect->property_y2() = child_height ();
561 rec_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame.get();
562 rec_rect->property_outline_what() = 0x1 | 0x2 | 0x4 | 0x8;
563 rec_rect->property_fill_color_rgba() = fill_color;
564 rec_rect->lower_to_bottom();
567 recbox.rectangle = rec_rect;
568 recbox.start = _trackview.session().transport_frame();
571 rec_rects.push_back (recbox);
573 screen_update_connection.disconnect();
574 screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &AudioStreamView::update_rec_box));
578 } else if (rec_active &&
579 (_trackview.session().record_status() != Session::Recording ||
580 !_trackview.get_diskstream()->record_enabled())) {
581 screen_update_connection.disconnect();
583 rec_updating = false;
588 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
590 if (!rec_rects.empty() || !rec_regions.empty()) {
592 /* disconnect rapid update */
593 screen_update_connection.disconnect();
595 for (list<sigc::connection>::iterator prc = rec_data_ready_connections.begin(); prc != rec_data_ready_connections.end(); ++prc) {
598 rec_data_ready_connections.clear();
600 rec_updating = false;
603 /* remove temp regions */
605 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); ) {
606 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
611 (*iter).first->drop_references ();
618 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
620 /* transport stopped, clear boxes */
621 for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
622 RecBoxInfo &rect = (*iter);
623 delete rect.rectangle;
633 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
635 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
641 AudioStreamView::rec_peak_range_ready (nframes_t start, nframes_t cnt, boost::weak_ptr<Source> weak_src)
643 ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, weak_src));
645 boost::shared_ptr<Source> src (weak_src.lock());
651 // this is called from the peak building thread
653 if (rec_data_ready_map.size() == 0 || start+cnt > last_rec_data_frame) {
654 last_rec_data_frame = start + cnt;
657 rec_data_ready_map[src] = true;
659 if (rec_data_ready_map.size() == _trackview.get_diskstream()->n_channels().n_audio()) {
660 this->update_rec_regions ();
661 rec_data_ready_map.clear();
666 AudioStreamView::update_rec_regions ()
668 if (use_rec_regions) {
671 for (list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
673 list<pair<boost::shared_ptr<Region>,RegionView*> >::iterator tmp;
678 if (!canvas_item_visible (rec_rects[n].rectangle)) {
679 /* rect already hidden, this region is done */
684 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(iter->first);
689 nframes_t origlen = region->length();
691 if (region == rec_regions.back().first && rec_active) {
693 if (last_rec_data_frame > region->start()) {
695 nframes_t nlen = last_rec_data_frame - region->start();
697 if (nlen != region->length()) {
700 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
701 region->set_length (nlen, this);
702 region->thaw ("updated");
705 /* our special initial length */
706 add_region_view_internal (region, false, true);
709 /* also update rect */
710 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
711 gdouble xend = _trackview.editor().frame_to_pixel (region->position() + region->length());
712 rect->property_x2() = xend;
718 nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
720 if (nlen != region->length()) {
722 if (region->source_length(0) >= region->start() + nlen) {
725 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
726 region->set_length (nlen, this);
727 region->thaw ("updated");
730 /* our special initial length */
731 add_region_view_internal (region, false, true);
735 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
748 AudioStreamView::show_all_fades ()
750 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
751 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
753 arv->set_fade_visibility (true);
759 AudioStreamView::hide_all_fades ()
761 for (list<RegionView*>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
762 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
764 arv->set_fade_visibility (false);
770 AudioStreamView::show_all_xfades ()
772 foreach_crossfadeview (&CrossfadeView::show);
773 crossfades_visible = true;
777 AudioStreamView::hide_all_xfades ()
779 foreach_crossfadeview (&CrossfadeView::hide);
780 crossfades_visible = false;
784 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
786 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
787 if ((*i)->crossfade->involves (rv.audio_region())) {
794 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
796 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
797 if ((*i)->crossfade->involves (rv.audio_region()) && (*i)->visible() && _layer_display != Stacked) {
804 AudioStreamView::color_handler ()
806 //case cAudioTrackBase:
807 if (_trackview.is_track()) {
808 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase.get();
811 //case cAudioBusBase:
812 if (!_trackview.is_track()) {
813 if (Profile->get_sae() && _trackview.route()->is_master()) {
814 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase.get();
816 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase.get();
822 AudioStreamView::update_contents_height ()
824 StreamView::update_contents_height ();
826 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
827 if (_layer_display == Overlaid) {
829 (*i)->set_height (height);