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 /* catch region going away */
196 r
->DropReferences
.connect (*this, invalidator (*this), boost::bind (&AudioStreamView::remove_region_view
, this, boost::weak_ptr
<Region
> (r
)), gui_context());
198 RegionViewAdded (region_view
);
204 AudioStreamView::remove_region_view (boost::weak_ptr
<Region
> weak_r
)
206 ENSURE_GUI_THREAD (*this, &AudioStreamView::remove_region_view
, weak_r
);
208 boost::shared_ptr
<Region
> r (weak_r
.lock());
214 if (!_trackview
.session()->deletion_in_progress()) {
216 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end();) {
217 CrossfadeViewList::iterator tmp
;
222 boost::shared_ptr
<AudioRegion
> ar
= boost::dynamic_pointer_cast
<AudioRegion
>(r
);
223 if (ar
&& i
->second
->crossfade
->involves (ar
)) {
225 crossfade_views
.erase (i
);
232 StreamView::remove_region_view(r
);
236 AudioStreamView::undisplay_track ()
238 StreamView::undisplay_track ();
240 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
244 crossfade_views
.clear ();
248 AudioStreamView::playlist_layered (boost::weak_ptr
<Track
> wtr
)
250 boost::shared_ptr
<Track
> tr (wtr
.lock());
256 StreamView::playlist_layered (wtr
);
258 /* make sure xfades are on top and all the regionviews are stacked correctly. */
260 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
261 i
->second
->get_canvas_group()->raise_to_top();
266 AudioStreamView::playlist_switched (boost::weak_ptr
<Track
> wtr
)
268 boost::shared_ptr
<Track
> tr (wtr
.lock());
274 playlist_connections
.drop_connections ();
276 StreamView::playlist_switched (tr
);
278 boost::shared_ptr
<AudioPlaylist
> apl
= boost::dynamic_pointer_cast
<AudioPlaylist
> (tr
->playlist());
281 apl
->NewCrossfade
.connect (playlist_connections
, invalidator (*this), ui_bind (&AudioStreamView::add_crossfade
, this, _1
), gui_context());
286 AudioStreamView::add_crossfade (boost::weak_ptr
<Crossfade
> wc
)
288 boost::shared_ptr
<Crossfade
> crossfade (wc
.lock());
294 AudioRegionView
* lview
= 0;
295 AudioRegionView
* rview
= 0;
297 /* first see if we already have a CrossfadeView for this Crossfade */
299 CrossfadeViewList::iterator i
= crossfade_views
.find (crossfade
);
300 if (i
!= crossfade_views
.end()) {
301 if (!crossfades_visible
) {
306 i
->second
->set_valid (true);
310 /* create a new one */
312 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
313 AudioRegionView
* arv
= dynamic_cast<AudioRegionView
*>(*i
);
315 if (!lview
&& arv
&& (arv
->region() == crossfade
->out())) {
318 if (!rview
&& arv
&& (arv
->region() == crossfade
->in())) {
323 CrossfadeView
*cv
= new CrossfadeView (_trackview
.canvas_display (),
329 cv
->set_valid (true);
330 crossfade
->Invalidated
.connect (*this, invalidator (*this), ui_bind (&AudioStreamView::remove_crossfade
, this, _1
), gui_context());
331 crossfade_views
[cv
->crossfade
] = cv
;
332 if (!crossfades_visible
) {
336 update_content_height (cv
);
340 AudioStreamView::remove_crossfade (boost::shared_ptr
<Region
> r
)
342 ENSURE_GUI_THREAD (*this, &AudioStreamView::remove_crossfade
, r
)
344 boost::shared_ptr
<Crossfade
> xfade
= boost::dynamic_pointer_cast
<Crossfade
> (r
);
346 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
347 if (i
->second
->crossfade
== xfade
) {
349 crossfade_views
.erase (i
);
356 AudioStreamView::redisplay_track ()
358 list
<RegionView
*>::iterator i
;
359 CrossfadeViewList::iterator xi
, tmpx
;
361 // Flag region views as invalid and disable drawing
362 for (i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
363 (*i
)->set_valid (false);
364 (*i
)->enable_display (false);
367 // Flag crossfade views as invalid
368 for (xi
= crossfade_views
.begin(); xi
!= crossfade_views
.end(); ++xi
) {
369 xi
->second
->set_valid (false);
370 if (xi
->second
->visible()) {
375 // Add and display region and crossfade views, and flag them as valid
377 if (_trackview
.is_audio_track()) {
378 _trackview
.track()->playlist()->foreach_region(
379 sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view
))
382 boost::shared_ptr
<AudioPlaylist
> apl
= boost::dynamic_pointer_cast
<AudioPlaylist
>(
383 _trackview
.track()->playlist()
387 apl
->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade
));
391 // Remove invalid crossfade views
392 for (xi
= crossfade_views
.begin(); xi
!= crossfade_views
.end();) {
396 if (!xi
->second
->valid()) {
398 crossfade_views
.erase (xi
);
404 // Stack regions by layer, and remove invalid regions
409 AudioStreamView::set_show_waveforms (bool yn
)
411 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
412 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
414 arv
->set_waveform_visible (yn
);
420 AudioStreamView::set_waveform_shape (WaveformShape shape
)
422 for (RegionViewList::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
423 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
425 arv
->set_waveform_shape (shape
);
430 AudioStreamView::set_waveform_scale (WaveformScale scale
)
432 for (RegionViewList::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
433 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
435 arv
->set_waveform_scale (scale
);
441 AudioStreamView::setup_rec_box ()
443 //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
445 if (_trackview
.session()->transport_rolling()) {
447 // cerr << "\trolling\n";
450 _trackview
.session()->record_status() == Session::Recording
&&
451 _trackview
.track()->record_enabled()) {
452 if (_trackview
.audio_track()->mode() == Normal
&& Config
->get_show_waveforms_while_recording() && rec_regions
.size() == rec_rects
.size()) {
454 /* add a new region, but don't bother if they set show-waveforms-while-recording mid-record */
458 rec_data_ready_connections
.drop_connections ();
459 boost::shared_ptr
<AudioTrack
> tr
= _trackview
.audio_track();
461 for (uint32_t n
= 0; n
< tr
->n_channels().n_audio(); ++n
) {
462 boost::shared_ptr
<AudioFileSource
> src
= tr
->write_source (n
);
464 sources
.push_back (src
);
465 src
->PeakRangeReady
.connect (rec_data_ready_connections
,
467 ui_bind (&AudioStreamView::rec_peak_range_ready
, this, _1
, _2
, boost::weak_ptr
<Source
>(src
)),
474 framepos_t start
= 0;
475 if (rec_regions
.size() > 0) {
476 start
= rec_regions
.back().first
->start()
477 + _trackview
.track()->get_captured_frames(rec_regions
.size()-1);
482 plist
.add (Properties::start
, start
);
483 plist
.add (Properties::length
, 1);
484 plist
.add (Properties::name
, string());
485 plist
.add (Properties::layer
, 0);
487 boost::shared_ptr
<AudioRegion
> region (
488 boost::dynamic_pointer_cast
<AudioRegion
>(RegionFactory::create (sources
, plist
, false)));
491 region
->set_position (_trackview
.session()->transport_frame(), this);
492 rec_regions
.push_back (make_pair(region
, (RegionView
*) 0));
495 /* start a new rec box */
497 boost::shared_ptr
<AudioTrack
> at
;
499 at
= _trackview
.audio_track(); /* we know what it is already */
500 framepos_t
const frame_pos
= at
->current_capture_start ();
501 gdouble xstart
= _trackview
.editor().frame_to_pixel (frame_pos
);
505 switch (_trackview
.audio_track()->mode()) {
509 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
514 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
515 /* make the recording rect translucent to allow
516 the user to see the peak data coming in, etc.
518 fill_color
= UINT_RGBA_CHANGE_A (fill_color
, 120);
522 ArdourCanvas::SimpleRect
* rec_rect
= new Gnome::Canvas::SimpleRect (*_canvas_group
);
523 rec_rect
->property_x1() = xstart
;
524 rec_rect
->property_y1() = 1.0;
525 rec_rect
->property_x2() = xend
;
526 rec_rect
->property_y2() = child_height ();
527 rec_rect
->property_outline_what() = 0x0;
528 rec_rect
->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame
.get();
529 rec_rect
->property_fill_color_rgba() = fill_color
;
530 rec_rect
->lower_to_bottom();
533 recbox
.rectangle
= rec_rect
;
534 recbox
.start
= _trackview
.session()->transport_frame();
537 rec_rects
.push_back (recbox
);
539 screen_update_connection
.disconnect();
540 screen_update_connection
= ARDOUR_UI::instance()->SuperRapidScreenUpdate
.connect (
541 sigc::mem_fun (*this, &AudioStreamView::update_rec_box
));
545 } else if (rec_active
&&
546 (_trackview
.session()->record_status() != Session::Recording
||
547 !_trackview
.track()->record_enabled())) {
548 screen_update_connection
.disconnect();
550 rec_updating
= false;
555 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
557 if (!rec_rects
.empty() || !rec_regions
.empty()) {
559 /* disconnect rapid update */
560 screen_update_connection
.disconnect();
561 rec_data_ready_connections
.drop_connections ();
562 rec_updating
= false;
565 /* remove temp regions */
567 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin(); iter
!= rec_regions
.end(); ) {
568 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
;
573 (*iter
).first
->drop_references ();
580 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
582 /* transport stopped, clear boxes */
583 for (vector
<RecBoxInfo
>::iterator iter
=rec_rects
.begin(); iter
!= rec_rects
.end(); ++iter
) {
584 RecBoxInfo
&rect
= (*iter
);
585 delete rect
.rectangle
;
595 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf
)(void))
597 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
598 (i
->second
->*pmf
) ();
603 AudioStreamView::rec_peak_range_ready (framepos_t start
, framecnt_t cnt
, boost::weak_ptr
<Source
> weak_src
)
605 ENSURE_GUI_THREAD (*this, &AudioStreamView::rec_peak_range_ready
, start
, cnt
, weak_src
)
607 boost::shared_ptr
<Source
> src (weak_src
.lock());
613 // this is called from the peak building thread
615 if (rec_data_ready_map
.size() == 0 || start
+ cnt
> last_rec_data_frame
) {
616 last_rec_data_frame
= start
+ cnt
;
619 rec_data_ready_map
[src
] = true;
621 if (rec_data_ready_map
.size() == _trackview
.track()->n_channels().n_audio()) {
622 update_rec_regions (start
, cnt
);
623 rec_data_ready_map
.clear();
628 AudioStreamView::update_rec_regions (framepos_t start
, framecnt_t cnt
)
630 if (!Config
->get_show_waveforms_while_recording ()) {
636 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin(); iter
!= rec_regions
.end(); n
++) {
638 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
= iter
;
641 assert (n
< rec_rects
.size());
643 if (!canvas_item_visible (rec_rects
[n
].rectangle
)) {
644 /* rect already hidden, this region is done */
649 boost::shared_ptr
<AudioRegion
> region
= boost::dynamic_pointer_cast
<AudioRegion
>(iter
->first
);
656 framecnt_t origlen
= region
->length();
658 if (region
== rec_regions
.back().first
&& rec_active
) {
660 if (last_rec_data_frame
> region
->start()) {
662 framecnt_t nlen
= last_rec_data_frame
- region
->start();
664 if (nlen
!= region
->length()) {
666 region
->suspend_property_changes ();
667 region
->set_position (_trackview
.track()->get_capture_start_frame(n
), this);
668 region
->set_length (nlen
, this);
669 region
->resume_property_changes ();
672 /* our special initial length */
673 add_region_view_internal (region
, false, true);
674 setup_new_rec_layer_time (region
);
677 check_record_layers (region
, (region
->position() - region
->start() + start
+ cnt
));
679 /* also update rect */
680 ArdourCanvas::SimpleRect
* rect
= rec_rects
[n
].rectangle
;
681 gdouble xend
= _trackview
.editor().frame_to_pixel (region
->position() + region
->length());
682 rect
->property_x2() = xend
;
687 framecnt_t nlen
= _trackview
.track()->get_captured_frames(n
);
689 if (nlen
!= region
->length()) {
691 if (region
->source_length(0) >= region
->start() + nlen
) {
693 region
->suspend_property_changes ();
694 region
->set_position (_trackview
.track()->get_capture_start_frame(n
), this);
695 region
->set_length (nlen
, this);
696 region
->resume_property_changes ();
699 /* our special initial length */
700 add_region_view_internal (region
, false, true);
704 ArdourCanvas::Item
* rect
= rec_rects
[n
].rectangle
;
717 AudioStreamView::show_all_fades ()
719 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
720 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
722 arv
->set_fade_visibility (true);
728 AudioStreamView::hide_all_fades ()
730 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
731 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
733 arv
->set_fade_visibility (false);
739 AudioStreamView::show_all_xfades ()
741 foreach_crossfadeview (&CrossfadeView::show
);
742 crossfades_visible
= true;
746 AudioStreamView::hide_all_xfades ()
748 foreach_crossfadeview (&CrossfadeView::hide
);
749 crossfades_visible
= false;
753 AudioStreamView::hide_xfades_involving (AudioRegionView
& rv
)
755 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
756 if (i
->second
->crossfade
->involves (rv
.audio_region())) {
757 i
->second
->fake_hide ();
763 AudioStreamView::reveal_xfades_involving (AudioRegionView
& rv
)
765 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
766 if (i
->second
->crossfade
->involves (rv
.audio_region()) && i
->second
->visible()) {
773 AudioStreamView::color_handler ()
775 //case cAudioTrackBase:
776 if (_trackview
.is_track()) {
777 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase
.get();
780 //case cAudioBusBase:
781 if (!_trackview
.is_track()) {
782 if (Profile
->get_sae() && _trackview
.route()->is_master()) {
783 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase
.get();
785 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase
.get();
791 AudioStreamView::update_contents_height ()
793 StreamView::update_contents_height ();
795 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
796 update_content_height (i
->second
);
801 AudioStreamView::update_content_height (CrossfadeView
* cv
)
803 if (_layer_display
== Overlaid
) {
806 cv
->set_height (height
);
810 layer_t
const inl
= cv
->crossfade
->in()->layer ();
811 layer_t
const outl
= cv
->crossfade
->out()->layer ();
813 layer_t
const high
= max (inl
, outl
);
814 layer_t
const low
= min (inl
, outl
);
816 const double h
= child_height ();
818 cv
->set_y ((_layers
- high
- 1) * h
);
819 cv
->set_height ((high
- low
+ 1) * h
);
825 AudioStreamView::parameter_changed (string
const & p
)
827 if (p
== "show-waveforms") {
828 set_show_waveforms (Config
->get_show_waveforms ());
829 } else if (p
== "waveform-scale") {
830 set_waveform_scale (Config
->get_waveform_scale ());
831 } else if (p
== "waveform-shape") {
832 set_waveform_shape (Config
->get_waveform_shape ());
837 AudioStreamView::horizontal_position_changed ()
839 /* we only `draw' the bit of the curve that is visible, so we need to update here */
841 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
842 i
->second
->horizontal_position_changed ();