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 "pbd/stacktrace.h"
29 #include "ardour/audioplaylist.h"
30 #include "ardour/audioregion.h"
31 #include "ardour/audiofilesource.h"
32 #include "ardour/audio_track.h"
33 #include "ardour/source.h"
34 #include "ardour/region_factory.h"
35 #include "ardour/profile.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/session.h"
39 #include "audio_streamview.h"
40 #include "audio_region_view.h"
41 #include "tape_region_view.h"
42 #include "audio_time_axis.h"
43 #include "canvas-waveview.h"
44 #include "canvas-simplerect.h"
45 #include "region_selection.h"
46 #include "selection.h"
47 #include "public_editor.h"
48 #include "ardour_ui.h"
49 #include "crossfade_view.h"
50 #include "rgb_macros.h"
51 #include "gui_thread.h"
57 using namespace ARDOUR
;
59 using namespace Editing
;
61 AudioStreamView::AudioStreamView (AudioTimeAxisView
& tv
)
64 crossfades_visible
= tv
.session()->config
.get_xfades_visible ();
66 _amplitude_above_axis
= 1.0;
68 Config
->ParameterChanged
.connect (*this, invalidator (*this), ui_bind (&AudioStreamView::parameter_changed
, this, _1
), gui_context());
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
->second
->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::create_region_view (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
);
117 switch (_trackview
.audio_track()->mode()) {
122 region_view
= new AudioRegionView (_canvas_group
, _trackview
, region
,
123 _samples_per_unit
, region_color
, recording
, TimeAxisViewItem::Visibility(
124 TimeAxisViewItem::ShowFrame
|
125 TimeAxisViewItem::HideFrameRight
|
126 TimeAxisViewItem::HideFrameLeft
|
127 TimeAxisViewItem::HideFrameTB
));
129 region_view
= new AudioRegionView (_canvas_group
, _trackview
, region
,
130 _samples_per_unit
, region_color
);
134 region_view
= new TapeAudioRegionView (_canvas_group
, _trackview
, region
,
135 _samples_per_unit
, region_color
);
138 fatal
<< string_compose (_("programming error: %1"), "illegal track mode in ::add_region_view_internal") << endmsg
;
143 region_view
->init (region_color
, wait_for_waves
);
144 region_view
->set_amplitude_above_axis(_amplitude_above_axis
);
145 region_view
->set_height (child_height ());
147 /* if its the special single-sample length that we use for rec-regions, make it
148 insensitive to events
151 if (region
->length() == 1) {
152 region_view
->set_sensitive (false);
155 region_view
->set_waveform_scale (Config
->get_waveform_scale ());
156 region_view
->set_waveform_shape (Config
->get_waveform_shape ());
157 region_view
->set_waveform_visible (Config
->get_show_waveforms ());
163 AudioStreamView::add_region_view_internal (boost::shared_ptr
<Region
> r
, bool wait_for_waves
, bool recording
)
165 RegionView
*region_view
= create_region_view (r
, wait_for_waves
, recording
);
167 if (region_view
== 0) {
172 // for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
173 // if ((*i)->region() == r) {
174 // cerr << "audio_streamview in add_region_view_internal region found" << endl;
175 /* great. we already have a AudioRegionView for this Region. use it again. */
177 // (*i)->set_valid (true);
179 // this might not be necessary
180 // AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
183 // arv->set_waveform_scale (_waveform_scale);
184 // arv->set_waveform_shape (_waveform_shape);
192 region_views
.push_front (region_view
);
194 if (_trackview
.editor().internal_editing()) {
195 region_view
->hide_rect ();
197 region_view
->show_rect ();
200 /* catch region going away */
202 r
->DropReferences
.connect (*this, invalidator (*this), boost::bind (&AudioStreamView::remove_region_view
, this, boost::weak_ptr
<Region
> (r
)), gui_context());
204 RegionViewAdded (region_view
);
210 AudioStreamView::remove_region_view (boost::weak_ptr
<Region
> weak_r
)
212 ENSURE_GUI_THREAD (*this, &AudioStreamView::remove_region_view
, weak_r
);
214 boost::shared_ptr
<Region
> r (weak_r
.lock());
220 if (!_trackview
.session()->deletion_in_progress()) {
222 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end();) {
223 CrossfadeViewList::iterator tmp
;
228 boost::shared_ptr
<AudioRegion
> ar
= boost::dynamic_pointer_cast
<AudioRegion
>(r
);
229 if (ar
&& i
->second
->crossfade
->involves (ar
)) {
231 crossfade_views
.erase (i
);
238 StreamView::remove_region_view(r
);
242 AudioStreamView::undisplay_track ()
244 StreamView::undisplay_track ();
246 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
250 crossfade_views
.clear ();
254 AudioStreamView::playlist_layered (boost::weak_ptr
<Track
> wtr
)
256 boost::shared_ptr
<Track
> tr (wtr
.lock());
262 StreamView::playlist_layered (wtr
);
264 /* make sure xfades are on top and all the regionviews are stacked correctly. */
266 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
267 i
->second
->get_canvas_group()->raise_to_top();
272 AudioStreamView::playlist_switched (boost::weak_ptr
<Track
> wtr
)
274 boost::shared_ptr
<Track
> tr (wtr
.lock());
280 playlist_connections
.drop_connections ();
282 StreamView::playlist_switched (tr
);
284 boost::shared_ptr
<AudioPlaylist
> apl
= boost::dynamic_pointer_cast
<AudioPlaylist
> (tr
->playlist());
287 apl
->NewCrossfade
.connect (playlist_connections
, invalidator (*this), ui_bind (&AudioStreamView::add_crossfade
, this, _1
), gui_context());
292 AudioStreamView::add_crossfade (boost::weak_ptr
<Crossfade
> wc
)
294 boost::shared_ptr
<Crossfade
> crossfade (wc
.lock());
300 AudioRegionView
* lview
= 0;
301 AudioRegionView
* rview
= 0;
303 /* first see if we already have a CrossfadeView for this Crossfade */
305 CrossfadeViewList::iterator i
= crossfade_views
.find (crossfade
);
306 if (i
!= crossfade_views
.end()) {
307 if (!crossfades_visible
) {
312 i
->second
->set_valid (true);
316 /* create a new one */
318 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
319 AudioRegionView
* arv
= dynamic_cast<AudioRegionView
*>(*i
);
321 if (!lview
&& arv
&& (arv
->region() == crossfade
->out())) {
324 if (!rview
&& arv
&& (arv
->region() == crossfade
->in())) {
329 CrossfadeView
*cv
= new CrossfadeView (_trackview
.canvas_display (),
335 cv
->set_valid (true);
336 crossfade
->Invalidated
.connect (*this, invalidator (*this), ui_bind (&AudioStreamView::remove_crossfade
, this, _1
), gui_context());
337 crossfade_views
[cv
->crossfade
] = cv
;
338 if (!crossfades_visible
) {
342 update_content_height (cv
);
346 AudioStreamView::remove_crossfade (boost::shared_ptr
<Region
> r
)
348 ENSURE_GUI_THREAD (*this, &AudioStreamView::remove_crossfade
, r
)
350 boost::shared_ptr
<Crossfade
> xfade
= boost::dynamic_pointer_cast
<Crossfade
> (r
);
352 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
353 if (i
->second
->crossfade
== xfade
) {
355 crossfade_views
.erase (i
);
362 AudioStreamView::redisplay_track ()
364 list
<RegionView
*>::iterator i
;
365 CrossfadeViewList::iterator xi
, tmpx
;
367 // Flag region views as invalid and disable drawing
368 for (i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
369 (*i
)->set_valid (false);
370 (*i
)->enable_display (false);
373 // Flag crossfade views as invalid
374 for (xi
= crossfade_views
.begin(); xi
!= crossfade_views
.end(); ++xi
) {
375 xi
->second
->set_valid (false);
376 if (xi
->second
->visible()) {
381 // Add and display region and crossfade views, and flag them as valid
383 if (_trackview
.is_audio_track()) {
384 _trackview
.track()->playlist()->foreach_region(
385 sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view
))
388 boost::shared_ptr
<AudioPlaylist
> apl
= boost::dynamic_pointer_cast
<AudioPlaylist
>(
389 _trackview
.track()->playlist()
393 apl
->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade
));
397 // Remove invalid crossfade views
398 for (xi
= crossfade_views
.begin(); xi
!= crossfade_views
.end();) {
402 if (!xi
->second
->valid()) {
404 crossfade_views
.erase (xi
);
410 // Stack regions by layer, and remove invalid regions
415 AudioStreamView::set_show_waveforms (bool yn
)
417 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
418 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
420 arv
->set_waveform_visible (yn
);
426 AudioStreamView::set_waveform_shape (WaveformShape shape
)
428 for (RegionViewList::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
429 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
431 arv
->set_waveform_shape (shape
);
436 AudioStreamView::set_waveform_scale (WaveformScale scale
)
438 for (RegionViewList::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
439 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
441 arv
->set_waveform_scale (scale
);
447 AudioStreamView::setup_rec_box ()
449 //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
451 if (_trackview
.session()->transport_rolling()) {
453 // cerr << "\trolling\n";
456 _trackview
.session()->record_status() == Session::Recording
&&
457 _trackview
.track()->record_enabled()) {
458 if (_trackview
.audio_track()->mode() == Normal
&& Config
->get_show_waveforms_while_recording() && rec_regions
.size() == rec_rects
.size()) {
460 /* add a new region, but don't bother if they set show-waveforms-while-recording mid-record */
464 rec_data_ready_connections
.drop_connections ();
465 boost::shared_ptr
<AudioTrack
> tr
= _trackview
.audio_track();
467 for (uint32_t n
= 0; n
< tr
->n_channels().n_audio(); ++n
) {
468 boost::shared_ptr
<AudioFileSource
> src
= tr
->write_source (n
);
470 sources
.push_back (src
);
471 src
->PeakRangeReady
.connect (rec_data_ready_connections
,
473 ui_bind (&AudioStreamView::rec_peak_range_ready
, this, _1
, _2
, boost::weak_ptr
<Source
>(src
)),
480 framepos_t start
= 0;
481 if (rec_regions
.size() > 0) {
482 start
= rec_regions
.back().first
->start()
483 + _trackview
.track()->get_captured_frames(rec_regions
.size()-1);
488 plist
.add (Properties::start
, start
);
489 plist
.add (Properties::length
, 1);
490 plist
.add (Properties::name
, string());
491 plist
.add (Properties::layer
, 0);
493 boost::shared_ptr
<AudioRegion
> region (
494 boost::dynamic_pointer_cast
<AudioRegion
>(RegionFactory::create (sources
, plist
, false)));
497 region
->set_position (_trackview
.session()->transport_frame());
498 rec_regions
.push_back (make_pair(region
, (RegionView
*) 0));
501 /* start a new rec box */
503 boost::shared_ptr
<AudioTrack
> at
;
505 at
= _trackview
.audio_track(); /* we know what it is already */
506 framepos_t
const frame_pos
= at
->current_capture_start ();
507 gdouble xstart
= _trackview
.editor().frame_to_pixel (frame_pos
);
511 switch (_trackview
.audio_track()->mode()) {
515 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
520 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
521 /* make the recording rect translucent to allow
522 the user to see the peak data coming in, etc.
524 fill_color
= UINT_RGBA_CHANGE_A (fill_color
, 120);
528 ArdourCanvas::SimpleRect
* rec_rect
= new Gnome::Canvas::SimpleRect (*_canvas_group
);
529 rec_rect
->property_x1() = xstart
;
530 rec_rect
->property_y1() = 1.0;
531 rec_rect
->property_x2() = xend
;
532 rec_rect
->property_y2() = child_height ();
533 rec_rect
->property_outline_what() = 0x0;
534 rec_rect
->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame
.get();
535 rec_rect
->property_fill_color_rgba() = fill_color
;
536 rec_rect
->lower_to_bottom();
539 recbox
.rectangle
= rec_rect
;
540 recbox
.start
= _trackview
.session()->transport_frame();
543 rec_rects
.push_back (recbox
);
545 screen_update_connection
.disconnect();
546 screen_update_connection
= ARDOUR_UI::instance()->SuperRapidScreenUpdate
.connect (
547 sigc::mem_fun (*this, &AudioStreamView::update_rec_box
));
551 } else if (rec_active
&&
552 (_trackview
.session()->record_status() != Session::Recording
||
553 !_trackview
.track()->record_enabled())) {
554 screen_update_connection
.disconnect();
556 rec_updating
= false;
561 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
563 if (!rec_rects
.empty() || !rec_regions
.empty()) {
565 /* disconnect rapid update */
566 screen_update_connection
.disconnect();
567 rec_data_ready_connections
.drop_connections ();
568 rec_updating
= false;
571 /* remove temp regions */
573 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin(); iter
!= rec_regions
.end(); ) {
574 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
;
579 (*iter
).first
->drop_references ();
586 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
588 /* transport stopped, clear boxes */
589 for (vector
<RecBoxInfo
>::iterator iter
=rec_rects
.begin(); iter
!= rec_rects
.end(); ++iter
) {
590 RecBoxInfo
&rect
= (*iter
);
591 delete rect
.rectangle
;
601 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf
)(void))
603 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
604 (i
->second
->*pmf
) ();
609 AudioStreamView::rec_peak_range_ready (framepos_t start
, framecnt_t cnt
, boost::weak_ptr
<Source
> weak_src
)
611 ENSURE_GUI_THREAD (*this, &AudioStreamView::rec_peak_range_ready
, start
, cnt
, weak_src
)
613 boost::shared_ptr
<Source
> src (weak_src
.lock());
619 // this is called from the peak building thread
621 if (rec_data_ready_map
.size() == 0 || start
+ cnt
> last_rec_data_frame
) {
622 last_rec_data_frame
= start
+ cnt
;
625 rec_data_ready_map
[src
] = true;
627 if (rec_data_ready_map
.size() == _trackview
.track()->n_channels().n_audio()) {
628 update_rec_regions (start
, cnt
);
629 rec_data_ready_map
.clear();
634 AudioStreamView::update_rec_regions (framepos_t start
, framecnt_t cnt
)
636 if (!Config
->get_show_waveforms_while_recording ()) {
642 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin(); iter
!= rec_regions
.end(); n
++) {
644 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
= iter
;
647 assert (n
< rec_rects
.size());
649 if (!canvas_item_visible (rec_rects
[n
].rectangle
)) {
650 /* rect already hidden, this region is done */
655 boost::shared_ptr
<AudioRegion
> region
= boost::dynamic_pointer_cast
<AudioRegion
>(iter
->first
);
662 framecnt_t origlen
= region
->length();
664 if (region
== rec_regions
.back().first
&& rec_active
) {
666 if (last_rec_data_frame
> region
->start()) {
668 framecnt_t nlen
= last_rec_data_frame
- region
->start();
670 if (nlen
!= region
->length()) {
672 region
->suspend_property_changes ();
673 region
->set_position (_trackview
.track()->get_capture_start_frame(n
));
674 region
->set_length (nlen
);
675 region
->resume_property_changes ();
678 /* our special initial length */
679 add_region_view_internal (region
, false, true);
680 setup_new_rec_layer_time (region
);
683 check_record_layers (region
, (region
->position() - region
->start() + start
+ cnt
));
685 /* also update rect */
686 ArdourCanvas::SimpleRect
* rect
= rec_rects
[n
].rectangle
;
687 gdouble xend
= _trackview
.editor().frame_to_pixel (region
->position() + region
->length());
688 rect
->property_x2() = xend
;
693 framecnt_t nlen
= _trackview
.track()->get_captured_frames(n
);
695 if (nlen
!= region
->length()) {
697 if (region
->source_length(0) >= region
->start() + nlen
) {
699 region
->suspend_property_changes ();
700 region
->set_position (_trackview
.track()->get_capture_start_frame(n
));
701 region
->set_length (nlen
);
702 region
->resume_property_changes ();
705 /* our special initial length */
706 add_region_view_internal (region
, false, true);
710 ArdourCanvas::Item
* rect
= rec_rects
[n
].rectangle
;
723 AudioStreamView::show_all_fades ()
725 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
726 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
728 arv
->set_fade_visibility (true);
734 AudioStreamView::hide_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 (false);
745 AudioStreamView::show_all_xfades ()
747 foreach_crossfadeview (&CrossfadeView::show
);
748 crossfades_visible
= true;
752 AudioStreamView::hide_all_xfades ()
754 foreach_crossfadeview (&CrossfadeView::hide
);
755 crossfades_visible
= false;
759 AudioStreamView::hide_xfades_involving (AudioRegionView
& rv
)
761 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
762 if (i
->second
->crossfade
->involves (rv
.audio_region())) {
763 i
->second
->fake_hide ();
769 AudioStreamView::reveal_xfades_involving (AudioRegionView
& rv
)
771 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
772 if (i
->second
->crossfade
->involves (rv
.audio_region()) && i
->second
->visible()) {
779 AudioStreamView::color_handler ()
781 //case cAudioTrackBase:
782 if (_trackview
.is_track()) {
783 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase
.get();
786 //case cAudioBusBase:
787 if (!_trackview
.is_track()) {
788 if (Profile
->get_sae() && _trackview
.route()->is_master()) {
789 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase
.get();
791 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase
.get();
797 AudioStreamView::update_contents_height ()
799 StreamView::update_contents_height ();
801 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
802 update_content_height (i
->second
);
807 AudioStreamView::update_content_height (CrossfadeView
* cv
)
809 if (_layer_display
== Overlaid
) {
812 cv
->set_height (height
);
816 layer_t
const inl
= cv
->crossfade
->in()->layer ();
817 layer_t
const outl
= cv
->crossfade
->out()->layer ();
819 layer_t
const high
= max (inl
, outl
);
820 layer_t
const low
= min (inl
, outl
);
822 const double h
= child_height ();
824 cv
->set_y ((_layers
- high
- 1) * h
);
825 cv
->set_height ((high
- low
+ 1) * h
);
831 AudioStreamView::parameter_changed (string
const & p
)
833 if (p
== "show-waveforms") {
834 set_show_waveforms (Config
->get_show_waveforms ());
835 } else if (p
== "waveform-scale") {
836 set_waveform_scale (Config
->get_waveform_scale ());
837 } else if (p
== "waveform-shape") {
838 set_waveform_shape (Config
->get_waveform_shape ());
843 AudioStreamView::horizontal_position_changed ()
845 /* we only `draw' the bit of the curve that is visible, so we need to update here */
847 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
848 i
->second
->horizontal_position_changed ();