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
= true;
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
| TimeAxisViewItem::HideFrameRight
));
126 region_view
= new AudioRegionView (_canvas_group
, _trackview
, region
,
127 _samples_per_unit
, region_color
);
131 region_view
= new TapeAudioRegionView (_canvas_group
, _trackview
, region
,
132 _samples_per_unit
, region_color
);
135 fatal
<< string_compose (_("programming error: %1"), "illegal track mode in ::add_region_view_internal") << endmsg
;
140 region_view
->init (region_color
, wait_for_waves
);
141 region_view
->set_amplitude_above_axis(_amplitude_above_axis
);
142 region_view
->set_height (child_height ());
144 /* if its the special single-sample length that we use for rec-regions, make it
145 insensitive to events
148 if (region
->length() == 1) {
149 region_view
->set_sensitive (false);
152 region_view
->set_waveform_scale (Config
->get_waveform_scale ());
153 region_view
->set_waveform_shape (Config
->get_waveform_shape ());
154 region_view
->set_waveform_visible (Config
->get_show_waveforms ());
160 AudioStreamView::add_region_view_internal (boost::shared_ptr
<Region
> r
, bool wait_for_waves
, bool recording
)
162 RegionView
*region_view
= create_region_view (r
, wait_for_waves
, recording
);
163 if (region_view
== 0) {
168 // for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
169 // if ((*i)->region() == r) {
170 // cerr << "audio_streamview in add_region_view_internal region found" << endl;
171 /* great. we already have a AudioRegionView for this Region. use it again. */
173 // (*i)->set_valid (true);
175 // this might not be necessary
176 // AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
179 // arv->set_waveform_scale (_waveform_scale);
180 // arv->set_waveform_shape (_waveform_shape);
188 region_views
.push_front (region_view
);
190 /* catch region going away */
192 r
->DropReferences
.connect (*this, invalidator (*this), boost::bind (&AudioStreamView::remove_region_view
, this, boost::weak_ptr
<Region
> (r
)), gui_context());
194 RegionViewAdded (region_view
);
200 AudioStreamView::remove_region_view (boost::weak_ptr
<Region
> weak_r
)
202 ENSURE_GUI_THREAD (*this, &AudioStreamView::remove_region_view
, weak_r
);
204 boost::shared_ptr
<Region
> r (weak_r
.lock());
210 if (!_trackview
.session()->deletion_in_progress()) {
212 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end();) {
213 CrossfadeViewList::iterator tmp
;
218 boost::shared_ptr
<AudioRegion
> ar
= boost::dynamic_pointer_cast
<AudioRegion
>(r
);
219 if (ar
&& i
->second
->crossfade
->involves (ar
)) {
221 crossfade_views
.erase (i
);
228 StreamView::remove_region_view(r
);
232 AudioStreamView::undisplay_track ()
234 StreamView::undisplay_track ();
236 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
240 crossfade_views
.clear ();
244 AudioStreamView::playlist_layered (boost::weak_ptr
<Track
> wtr
)
246 boost::shared_ptr
<Track
> tr (wtr
.lock());
252 StreamView::playlist_layered (wtr
);
254 /* make sure xfades are on top and all the regionviews are stacked correctly. */
256 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
257 i
->second
->get_canvas_group()->raise_to_top();
262 AudioStreamView::playlist_switched (boost::weak_ptr
<Track
> wtr
)
264 boost::shared_ptr
<Track
> tr (wtr
.lock());
270 playlist_connections
.drop_connections ();
272 StreamView::playlist_switched (tr
);
274 boost::shared_ptr
<AudioPlaylist
> apl
= boost::dynamic_pointer_cast
<AudioPlaylist
> (tr
->playlist());
277 apl
->NewCrossfade
.connect (playlist_connections
, invalidator (*this), ui_bind (&AudioStreamView::add_crossfade
, this, _1
), gui_context());
282 AudioStreamView::add_crossfade (boost::weak_ptr
<Crossfade
> wc
)
284 boost::shared_ptr
<Crossfade
> crossfade (wc
.lock());
290 AudioRegionView
* lview
= 0;
291 AudioRegionView
* rview
= 0;
293 /* first see if we already have a CrossfadeView for this Crossfade */
295 CrossfadeViewList::iterator i
= crossfade_views
.find (crossfade
);
296 if (i
!= crossfade_views
.end()) {
297 if (!crossfades_visible
) {
302 i
->second
->set_valid (true);
306 /* create a new one */
308 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
309 AudioRegionView
* arv
= dynamic_cast<AudioRegionView
*>(*i
);
311 if (!lview
&& arv
&& (arv
->region() == crossfade
->out())) {
314 if (!rview
&& arv
&& (arv
->region() == crossfade
->in())) {
319 CrossfadeView
*cv
= new CrossfadeView (_trackview
.canvas_display (),
325 cv
->set_valid (true);
326 crossfade
->Invalidated
.connect (*this, invalidator (*this), ui_bind (&AudioStreamView::remove_crossfade
, this, _1
), gui_context());
327 crossfade_views
[cv
->crossfade
] = cv
;
328 if (!_trackview
.session()->config
.get_xfades_visible() || !crossfades_visible
) {
332 update_content_height (cv
);
336 AudioStreamView::remove_crossfade (boost::shared_ptr
<Region
> r
)
338 ENSURE_GUI_THREAD (*this, &AudioStreamView::remove_crossfade
, r
)
340 boost::shared_ptr
<Crossfade
> xfade
= boost::dynamic_pointer_cast
<Crossfade
> (r
);
342 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
343 if (i
->second
->crossfade
== xfade
) {
345 crossfade_views
.erase (i
);
352 AudioStreamView::redisplay_track ()
354 list
<RegionView
*>::iterator i
;
355 CrossfadeViewList::iterator xi
, tmpx
;
357 // Flag region views as invalid and disable drawing
358 for (i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
359 (*i
)->set_valid (false);
360 (*i
)->enable_display (false);
363 // Flag crossfade views as invalid
364 for (xi
= crossfade_views
.begin(); xi
!= crossfade_views
.end(); ++xi
) {
365 xi
->second
->set_valid (false);
366 if (xi
->second
->visible()) {
371 // Add and display region and crossfade views, and flag them as valid
373 if (_trackview
.is_audio_track()) {
374 _trackview
.track()->playlist()->foreach_region(
375 sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view
))
378 boost::shared_ptr
<AudioPlaylist
> apl
= boost::dynamic_pointer_cast
<AudioPlaylist
>(
379 _trackview
.track()->playlist()
383 apl
->foreach_crossfade (sigc::mem_fun (*this, &AudioStreamView::add_crossfade
));
387 // Remove invalid crossfade views
388 for (xi
= crossfade_views
.begin(); xi
!= crossfade_views
.end();) {
392 if (!xi
->second
->valid()) {
394 crossfade_views
.erase (xi
);
400 // Stack regions by layer, and remove invalid regions
405 AudioStreamView::set_show_waveforms (bool yn
)
407 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
408 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
410 arv
->set_waveform_visible (yn
);
416 AudioStreamView::set_waveform_shape (WaveformShape shape
)
418 for (RegionViewList::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
419 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
421 arv
->set_waveform_shape (shape
);
426 AudioStreamView::set_waveform_scale (WaveformScale scale
)
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_scale (scale
);
437 AudioStreamView::setup_rec_box ()
439 //cerr << _trackview.name() << " streamview SRB region_views.size() = " << region_views.size() << endl;
441 if (_trackview
.session()->transport_rolling()) {
443 // cerr << "\trolling\n";
446 _trackview
.session()->record_status() == Session::Recording
&&
447 _trackview
.track()->record_enabled()) {
448 if (_trackview
.audio_track()->mode() == Normal
&& Config
->get_show_waveforms_while_recording() && rec_regions
.size() == rec_rects
.size()) {
450 /* add a new region, but don't bother if they set show-waveforms-while-recording mid-record */
454 rec_data_ready_connections
.drop_connections ();
455 boost::shared_ptr
<AudioTrack
> tr
= _trackview
.audio_track();
457 for (uint32_t n
= 0; n
< tr
->n_channels().n_audio(); ++n
) {
458 boost::shared_ptr
<AudioFileSource
> src
= tr
->write_source (n
);
460 sources
.push_back (src
);
461 src
->PeakRangeReady
.connect (rec_data_ready_connections
,
463 ui_bind (&AudioStreamView::rec_peak_range_ready
, this, _1
, _2
, boost::weak_ptr
<Source
>(src
)),
471 if (rec_regions
.size() > 0) {
472 start
= rec_regions
.back().first
->start()
473 + _trackview
.track()->get_captured_frames(rec_regions
.size()-1);
478 plist
.add (Properties::start
, start
);
479 plist
.add (Properties::length
, 1);
480 plist
.add (Properties::name
, string());
481 plist
.add (Properties::layer
, 0);
483 boost::shared_ptr
<AudioRegion
> region (
484 boost::dynamic_pointer_cast
<AudioRegion
>(RegionFactory::create (sources
, plist
, false)));
487 region
->block_property_changes ();
488 region
->set_position (_trackview
.session()->transport_frame(), this);
489 rec_regions
.push_back (make_pair(region
, (RegionView
*)0));
492 /* start a new rec box */
494 boost::shared_ptr
<AudioTrack
> at
;
496 at
= _trackview
.audio_track(); /* we know what it is already */
497 nframes_t frame_pos
= at
->current_capture_start ();
498 gdouble xstart
= _trackview
.editor().frame_to_pixel (frame_pos
);
502 switch (_trackview
.audio_track()->mode()) {
506 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
511 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
512 /* make the recording rect translucent to allow
513 the user to see the peak data coming in, etc.
515 fill_color
= UINT_RGBA_CHANGE_A (fill_color
, 120);
519 ArdourCanvas::SimpleRect
* rec_rect
= new Gnome::Canvas::SimpleRect (*_canvas_group
);
520 rec_rect
->property_x1() = xstart
;
521 rec_rect
->property_y1() = 1.0;
522 rec_rect
->property_x2() = xend
;
523 rec_rect
->property_y2() = child_height ();
524 rec_rect
->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_TimeAxisFrame
.get();
525 rec_rect
->property_outline_what() = 0x1 | 0x2 | 0x4 | 0x8;
526 rec_rect
->property_fill_color_rgba() = fill_color
;
527 rec_rect
->lower_to_bottom();
530 recbox
.rectangle
= rec_rect
;
531 recbox
.start
= _trackview
.session()->transport_frame();
534 rec_rects
.push_back (recbox
);
536 screen_update_connection
.disconnect();
537 screen_update_connection
= ARDOUR_UI::instance()->SuperRapidScreenUpdate
.connect (
538 sigc::mem_fun (*this, &AudioStreamView::update_rec_box
));
542 } else if (rec_active
&&
543 (_trackview
.session()->record_status() != Session::Recording
||
544 !_trackview
.track()->record_enabled())) {
545 screen_update_connection
.disconnect();
547 rec_updating
= false;
552 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
554 if (!rec_rects
.empty() || !rec_regions
.empty()) {
556 /* disconnect rapid update */
557 screen_update_connection
.disconnect();
558 rec_data_ready_connections
.drop_connections ();
559 rec_updating
= false;
562 /* remove temp regions */
564 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin(); iter
!= rec_regions
.end(); ) {
565 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
;
570 (*iter
).first
->drop_references ();
577 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
579 /* transport stopped, clear boxes */
580 for (vector
<RecBoxInfo
>::iterator iter
=rec_rects
.begin(); iter
!= rec_rects
.end(); ++iter
) {
581 RecBoxInfo
&rect
= (*iter
);
582 delete rect
.rectangle
;
592 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf
)(void))
594 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
595 (i
->second
->*pmf
) ();
600 AudioStreamView::rec_peak_range_ready (nframes_t start
, nframes_t cnt
, boost::weak_ptr
<Source
> weak_src
)
602 ENSURE_GUI_THREAD (*this, &AudioStreamView::rec_peak_range_ready
, start
, cnt
, weak_src
)
604 boost::shared_ptr
<Source
> src (weak_src
.lock());
610 // this is called from the peak building thread
612 if (rec_data_ready_map
.size() == 0 || start
+ cnt
> last_rec_data_frame
) {
613 last_rec_data_frame
= start
+ cnt
;
616 rec_data_ready_map
[src
] = true;
618 if (rec_data_ready_map
.size() == _trackview
.track()->n_channels().n_audio()) {
619 this->update_rec_regions ();
620 rec_data_ready_map
.clear();
625 AudioStreamView::update_rec_regions ()
627 if (Config
->get_show_waveforms_while_recording ()) {
630 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin();
631 iter
!= rec_regions
.end(); n
++) {
633 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
= iter
;
636 if (!canvas_item_visible (rec_rects
[n
].rectangle
)) {
637 /* rect already hidden, this region is done */
642 boost::shared_ptr
<AudioRegion
> region
= boost::dynamic_pointer_cast
<AudioRegion
>(iter
->first
);
648 nframes_t origlen
= region
->length();
650 if (region
== rec_regions
.back().first
&& rec_active
) {
652 if (last_rec_data_frame
> region
->start()) {
654 nframes_t nlen
= last_rec_data_frame
- region
->start();
656 if (nlen
!= region
->length()) {
658 region
->suspend_property_changes ();
659 region
->set_position (_trackview
.track()->get_capture_start_frame(n
), this);
660 region
->set_length (nlen
, this);
661 region
->resume_property_changes ();
664 /* our special initial length */
665 add_region_view_internal (region
, false, true);
668 /* also update rect */
669 ArdourCanvas::SimpleRect
* rect
= rec_rects
[n
].rectangle
;
670 gdouble xend
= _trackview
.editor().frame_to_pixel (region
->position() + region
->length());
671 rect
->property_x2() = xend
;
677 nframes_t nlen
= _trackview
.track()->get_captured_frames(n
);
679 if (nlen
!= region
->length()) {
681 if (region
->source_length(0) >= region
->start() + nlen
) {
683 region
->suspend_property_changes ();
684 region
->set_position (_trackview
.track()->get_capture_start_frame(n
), this);
685 region
->set_length (nlen
, this);
686 region
->resume_property_changes ();
689 /* our special initial length */
690 add_region_view_internal (region
, false, true);
694 ArdourCanvas::Item
* rect
= rec_rects
[n
].rectangle
;
707 AudioStreamView::show_all_fades ()
709 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
710 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
712 arv
->set_fade_visibility (true);
718 AudioStreamView::hide_all_fades ()
720 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
721 AudioRegionView
* const arv
= dynamic_cast<AudioRegionView
*>(*i
);
723 arv
->set_fade_visibility (false);
729 AudioStreamView::show_all_xfades ()
731 foreach_crossfadeview (&CrossfadeView::show
);
732 crossfades_visible
= true;
736 AudioStreamView::hide_all_xfades ()
738 foreach_crossfadeview (&CrossfadeView::hide
);
739 crossfades_visible
= false;
743 AudioStreamView::hide_xfades_involving (AudioRegionView
& rv
)
745 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
746 if (i
->second
->crossfade
->involves (rv
.audio_region())) {
747 i
->second
->fake_hide ();
753 AudioStreamView::reveal_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()) && i
->second
->visible()) {
763 AudioStreamView::color_handler ()
765 //case cAudioTrackBase:
766 if (_trackview
.is_track()) {
767 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioTrackBase
.get();
770 //case cAudioBusBase:
771 if (!_trackview
.is_track()) {
772 if (Profile
->get_sae() && _trackview
.route()->is_master()) {
773 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioMasterBusBase
.get();
775 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase
.get();
781 AudioStreamView::update_contents_height ()
783 StreamView::update_contents_height ();
785 for (CrossfadeViewList::iterator i
= crossfade_views
.begin(); i
!= crossfade_views
.end(); ++i
) {
786 update_content_height (i
->second
);
791 AudioStreamView::update_content_height (CrossfadeView
* cv
)
795 if (_layer_display
== Overlaid
) {
798 cv
->set_height (height
);
802 layer_t
const inl
= cv
->crossfade
->in()->layer ();
803 layer_t
const outl
= cv
->crossfade
->out()->layer ();
805 layer_t
const high
= max (inl
, outl
);
806 layer_t
const low
= min (inl
, outl
);
808 const double h
= child_height ();
810 cv
->set_y ((_layers
- high
- 1) * h
);
811 cv
->set_height ((high
- low
+ 1) * h
);
817 AudioStreamView::parameter_changed (string
const & p
)
819 if (p
== "show-waveforms") {
820 set_show_waveforms (Config
->get_show_waveforms ());
821 } else if (p
== "waveform-scale") {
822 set_waveform_scale (Config
->get_waveform_scale ());
823 } else if (p
== "waveform-shape") {
824 set_waveform_shape (Config
->get_waveform_shape ());