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.
24 #include <gtkmm2ext/gtk_ui.h>
26 #include <ardour/audioplaylist.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/audiosource.h>
29 #include <ardour/audio_diskstream.h>
30 #include <ardour/audio_track.h>
31 #include <ardour/playlist_templates.h>
32 #include <ardour/source.h>
34 #include "audio_streamview.h"
35 #include "audio_regionview.h"
36 #include "taperegionview.h"
37 #include "audio_time_axis.h"
38 #include "canvas-waveview.h"
39 #include "canvas-simplerect.h"
40 #include "region_selection.h"
41 #include "selection.h"
42 #include "public_editor.h"
43 #include "ardour_ui.h"
44 #include "crossfade_view.h"
45 #include "rgb_macros.h"
46 #include "gui_thread.h"
50 using namespace ARDOUR;
52 using namespace Editing;
54 AudioStreamView::AudioStreamView (AudioTimeAxisView& tv)
57 region_color = _trackview.color();
58 crossfades_visible = true;
60 if (tv.is_audio_track())
61 stream_base_color = color_map[cAudioTrackBase];
63 stream_base_color = color_map[cAudioBusBase];
65 /* set_position() will position the group */
67 canvas_group = new ArdourCanvas::Group(*_trackview.canvas_display);
69 canvas_rect = new ArdourCanvas::SimpleRect (*canvas_group);
70 canvas_rect->property_x1() = 0.0;
71 canvas_rect->property_y1() = 0.0;
72 canvas_rect->property_x2() = 1000000.0;
73 canvas_rect->property_y2() = (double) tv.height;
74 canvas_rect->property_outline_color_rgba() = color_map[cAudioTrackOutline];
75 canvas_rect->property_outline_what() = (guint32) (0x1|0x2|0x8); // outline ends and bottom
76 canvas_rect->property_fill_color_rgba() = stream_base_color;
78 canvas_rect->signal_event().connect (bind (mem_fun (_trackview.editor, &PublicEditor::canvas_stream_view_event), canvas_rect, &_trackview));
80 _samples_per_unit = _trackview.editor.get_current_zoom();
81 _amplitude_above_axis = 1.0;
83 if (_trackview.is_audio_track()) {
84 _trackview.audio_track()->DiskstreamChanged.connect (mem_fun (*this, &AudioStreamView::diskstream_changed));
85 _trackview.session().TransportStateChange.connect (mem_fun (*this, &AudioStreamView::transport_changed));
86 _trackview.get_diskstream()->RecordEnableChanged.connect (mem_fun (*this, &AudioStreamView::rec_enable_changed));
87 _trackview.session().RecordStateChanged.connect (mem_fun (*this, &AudioStreamView::sess_rec_enable_changed));
92 use_rec_regions = tv.editor.show_waveforms_recording ();
93 last_rec_peak_frame = 0;
95 ColorChanged.connect (mem_fun (*this, &AudioStreamView::color_handler));
98 AudioStreamView::~AudioStreamView ()
100 undisplay_diskstream ();
105 AudioStreamView::set_height (gdouble h)
107 /* limit the values to something sane-ish */
108 if (h < 10.0 || h > 1000.0) {
112 StreamView::set_height(h);
114 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
115 (*i)->set_height (h);
122 AudioStreamView::set_samples_per_unit (gdouble spp)
124 StreamView::set_samples_per_unit(spp);
126 for (CrossfadeViewList::iterator xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
127 (*xi)->set_samples_per_unit (spp);
134 AudioStreamView::set_amplitude_above_axis (gdouble app)
137 RegionViewList::iterator i;
143 _amplitude_above_axis = app;
145 for (i = region_views.begin(); i != region_views.end(); ++i) {
146 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
148 arv->set_amplitude_above_axis (app);
155 AudioStreamView::add_region_view_internal (Region *r, bool wait_for_waves)
157 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_region_view), r));
159 AudioRegion* region = dynamic_cast<AudioRegion*> (r);
165 AudioRegionView *region_view;
166 list<RegionView *>::iterator i;
168 for (i = region_views.begin(); i != region_views.end(); ++i) {
169 if (&(*i)->region() == r) {
171 /* great. we already have a AudioRegionView for this Region. use it again. */
173 (*i)->set_valid (true);
178 switch (_trackview.audio_track()->mode()) {
180 region_view = new AudioRegionView (canvas_group, _trackview, *region,
181 _samples_per_unit, region_color);
184 region_view = new TapeAudioRegionView (canvas_group, _trackview, *region,
185 _samples_per_unit, region_color);
189 region_view->init (region_color, wait_for_waves);
190 region_view->set_amplitude_above_axis(_amplitude_above_axis);
191 region_views.push_front (region_view);
193 /* follow global waveform setting */
195 region_view->set_waveform_visible(_trackview.editor.show_waveforms());
197 /* catch regionview going away */
199 region->GoingAway.connect (mem_fun (*this, &AudioStreamView::remove_region_view));
201 RegionViewAdded (region_view);
205 AudioStreamView::remove_region_view (Region *r)
207 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_region_view), r));
209 StreamView::remove_region_view(r);
211 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
212 list<CrossfadeView*>::iterator tmp;
217 AudioRegion* ar = dynamic_cast<AudioRegion*>(r);
218 if (ar && (*i)->crossfade.involves (*ar)) {
220 crossfade_views.erase (i);
228 AudioStreamView::undisplay_diskstream ()
230 StreamView::undisplay_diskstream();
232 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
236 crossfade_views.clear ();
240 AudioStreamView::playlist_modified ()
242 ENSURE_GUI_THREAD (mem_fun (*this, &AudioStreamView::playlist_modified));
244 StreamView::playlist_modified();
246 /* if the playlist is modified, make sure xfades are on top and all the regionviews are stacked
250 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
251 (*i)->get_canvas_group()->raise_to_top();
256 AudioStreamView::playlist_changed (Diskstream *ds)
258 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::playlist_changed), ds));
260 StreamView::playlist_changed(ds);
262 AudioPlaylist* apl = dynamic_cast<AudioPlaylist*>(ds->playlist());
264 playlist_connections.push_back (apl->NewCrossfade.connect (mem_fun (*this, &AudioStreamView::add_crossfade)));
268 AudioStreamView::add_crossfade (Crossfade *crossfade)
270 AudioRegionView* lview = 0;
271 AudioRegionView* rview = 0;
273 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::add_crossfade), crossfade));
275 /* first see if we already have a CrossfadeView for this Crossfade */
277 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
278 if (&(*i)->crossfade == crossfade) {
279 if (!crossfades_visible) {
284 (*i)->set_valid (true);
289 /* create a new one */
291 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
292 AudioRegionView* arv = dynamic_cast<AudioRegionView*>(*i);
294 if (!lview && arv && &(arv->region()) == &crossfade->out()) {
297 if (!rview && arv && &(arv->region()) == &crossfade->in()) {
302 CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display,
309 crossfade->Invalidated.connect (mem_fun (*this, &AudioStreamView::remove_crossfade));
310 crossfade_views.push_back (cv);
312 if (!crossfades_visible) {
318 AudioStreamView::remove_crossfade (Crossfade *xfade)
320 ENSURE_GUI_THREAD (bind (mem_fun (*this, &AudioStreamView::remove_crossfade), xfade));
322 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
323 if (&(*i)->crossfade == xfade) {
325 crossfade_views.erase (i);
332 AudioStreamView::redisplay_diskstream ()
334 list<RegionView *>::iterator i, tmp;
335 list<CrossfadeView*>::iterator xi, tmpx;
338 for (i = region_views.begin(); i != region_views.end(); ++i) {
339 (*i)->set_valid (false);
342 for (xi = crossfade_views.begin(); xi != crossfade_views.end(); ++xi) {
343 (*xi)->set_valid (false);
344 if ((*xi)->visible()) {
349 if (_trackview.is_audio_track()) {
350 _trackview.get_diskstream()->playlist()->foreach_region (static_cast<StreamView*>(this), &StreamView::add_region_view);
351 AudioPlaylist* apl = dynamic_cast<AudioPlaylist*>(_trackview.get_diskstream()->playlist());
353 apl->foreach_crossfade (this, &AudioStreamView::add_crossfade);
356 for (i = region_views.begin(); i != region_views.end(); ) {
360 if (!(*i)->is_valid()) {
362 region_views.erase (i);
368 for (xi = crossfade_views.begin(); xi != crossfade_views.end();) {
372 if (!(*xi)->valid()) {
374 crossfade_views.erase (xi);
380 /* now fix layering */
382 playlist_modified ();
386 AudioStreamView::set_show_waveforms (bool yn)
388 for (list<RegionView *>::iterator i = region_views.begin(); i != region_views.end(); ++i) {
389 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
391 arv->set_waveform_visible (yn);
396 AudioStreamView::set_waveform_shape (WaveformShape shape)
398 for (RegionViewList::iterator i = region_views.begin(); i != region_views.end(); ++i) {
399 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
401 arv->set_waveform_shape (shape);
406 AudioStreamView::setup_rec_box ()
408 // cerr << _trackview.name() << " streamview SRB\n";
410 if (_trackview.session().transport_rolling()) {
412 // cerr << "\trolling\n";
415 _trackview.session().record_status() == Session::Recording &&
416 _trackview.get_diskstream()->record_enabled()) {
418 if (_trackview.audio_track()->mode() == Normal && use_rec_regions && rec_regions.size() == rec_rects.size()) {
420 /* add a new region, but don't bother if they set use_rec_regions mid-record */
422 AudioRegion::SourceList sources;
424 for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
427 peak_ready_connections.clear();
430 AudioDiskstream* ads = dynamic_cast<AudioDiskstream*>(_trackview.get_diskstream());
433 for (uint32_t n=0; n < ads->n_channels(); ++n) {
434 AudioSource *src = (AudioSource *) ads->write_source (n);
436 sources.push_back (src);
437 peak_ready_connections.push_back (src->PeakRangeReady.connect (bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), src)));
443 jack_nframes_t start = 0;
444 if (rec_regions.size() > 0) {
445 start = rec_regions.back()->start() + _trackview.get_diskstream()->get_captured_frames(rec_regions.size()-1);
448 AudioRegion * region = new AudioRegion(sources, start, 1 , "", 0, (Region::Flag)(Region::DefaultFlags | Region::DoNotSaveState), false);
449 region->set_position (_trackview.session().transport_frame(), this);
450 rec_regions.push_back (region);
451 /* catch it if it goes away */
452 region->GoingAway.connect (mem_fun (*this, &AudioStreamView::remove_rec_region));
454 /* we add the region later */
457 /* start a new rec box */
461 at = _trackview.audio_track(); /* we know what it is already */
462 AudioDiskstream& ds = at->audio_diskstream();
463 jack_nframes_t frame_pos = ds.current_capture_start ();
464 gdouble xstart = _trackview.editor.frame_to_pixel (frame_pos);
468 switch (_trackview.audio_track()->mode()) {
471 fill_color = color_map[cRecordingRectFill];
476 fill_color = color_map[cRecordingRectFill];
477 /* make the recording rect translucent to allow
478 the user to see the peak data coming in, etc.
480 fill_color = UINT_RGBA_CHANGE_A (fill_color, 120);
484 ArdourCanvas::SimpleRect * rec_rect = new Gnome::Canvas::SimpleRect (*canvas_group);
485 rec_rect->property_x1() = xstart;
486 rec_rect->property_y1() = 1.0;
487 rec_rect->property_x2() = xend;
488 rec_rect->property_y2() = (double) _trackview.height - 1;
489 rec_rect->property_outline_color_rgba() = color_map[cRecordingRectOutline];
490 rec_rect->property_fill_color_rgba() = fill_color;
493 recbox.rectangle = rec_rect;
494 recbox.start = _trackview.session().transport_frame();
497 rec_rects.push_back (recbox);
499 screen_update_connection.disconnect();
500 screen_update_connection = ARDOUR_UI::instance()->SuperRapidScreenUpdate.connect (mem_fun (*this, &AudioStreamView::update_rec_box));
504 } else if (rec_active &&
505 (_trackview.session().record_status() != Session::Recording ||
506 !_trackview.get_diskstream()->record_enabled())) {
508 screen_update_connection.disconnect();
510 rec_updating = false;
516 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
518 if (!rec_rects.empty() || !rec_regions.empty()) {
520 /* disconnect rapid update */
521 screen_update_connection.disconnect();
523 for (list<sigc::connection>::iterator prc = peak_ready_connections.begin(); prc != peak_ready_connections.end(); ++prc) {
526 peak_ready_connections.clear();
528 rec_updating = false;
530 last_rec_peak_frame = 0;
532 /* remove temp regions */
533 for (list<Region*>::iterator iter=rec_regions.begin(); iter != rec_regions.end(); )
535 list<Region*>::iterator tmp;
540 /* this will trigger the remove_region_view */
548 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
551 /* transport stopped, clear boxes */
552 for (vector<RecBoxInfo>::iterator iter=rec_rects.begin(); iter != rec_rects.end(); ++iter) {
553 RecBoxInfo &rect = (*iter);
554 delete rect.rectangle;
564 AudioStreamView::foreach_crossfadeview (void (CrossfadeView::*pmf)(void))
566 for (list<CrossfadeView*>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
572 AudioStreamView::rec_peak_range_ready (jack_nframes_t start, jack_nframes_t cnt, Source * src)
574 // this is called from the peak building thread
576 ENSURE_GUI_THREAD(bind (mem_fun (*this, &AudioStreamView::rec_peak_range_ready), start, cnt, src));
578 if (rec_peak_ready_map.size() == 0 || start+cnt > last_rec_peak_frame) {
579 last_rec_peak_frame = start + cnt;
582 rec_peak_ready_map[src] = true;
584 if (rec_peak_ready_map.size() == _trackview.get_diskstream()->n_channels()) {
585 this->update_rec_regions ();
586 rec_peak_ready_map.clear();
591 AudioStreamView::update_rec_regions ()
593 if (use_rec_regions) {
597 for (list<Region*>::iterator iter = rec_regions.begin(); iter != rec_regions.end(); n++) {
599 list<Region*>::iterator tmp;
604 if (!canvas_item_visible (rec_rects[n].rectangle)) {
605 /* rect already hidden, this region is done */
611 AudioRegion * region = dynamic_cast<AudioRegion*>(*iter);
614 jack_nframes_t origlen = region->length();
616 if (region == rec_regions.back() && rec_active) {
618 if (last_rec_peak_frame > region->start()) {
620 jack_nframes_t nlen = last_rec_peak_frame - region->start();
622 if (nlen != region->length()) {
625 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
626 region->set_length (nlen, this);
627 region->thaw ("updated");
630 /* our special initial length */
631 add_region_view_internal (region, false);
634 /* also update rect */
635 ArdourCanvas::SimpleRect * rect = rec_rects[n].rectangle;
636 gdouble xend = _trackview.editor.frame_to_pixel (region->position() + region->length());
637 rect->property_x2() = xend;
643 jack_nframes_t nlen = _trackview.get_diskstream()->get_captured_frames(n);
645 if (nlen != region->length()) {
647 if (region->source(0).length() >= region->start() + nlen) {
650 region->set_position (_trackview.get_diskstream()->get_capture_start_frame(n), this);
651 region->set_length (nlen, this);
652 region->thaw ("updated");
655 /* our special initial length */
656 add_region_view_internal (region, false);
660 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
673 AudioStreamView::show_all_xfades ()
675 foreach_crossfadeview (&CrossfadeView::show);
676 crossfades_visible = true;
680 AudioStreamView::hide_all_xfades ()
682 foreach_crossfadeview (&CrossfadeView::hide);
683 crossfades_visible = false;
687 AudioStreamView::hide_xfades_involving (AudioRegionView& rv)
689 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
690 if ((*i)->crossfade.involves (rv.audio_region())) {
697 AudioStreamView::reveal_xfades_involving (AudioRegionView& rv)
699 for (list<CrossfadeView *>::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
700 if ((*i)->crossfade.involves (rv.audio_region()) && (*i)->visible()) {
707 AudioStreamView::color_handler (ColorID id, uint32_t val)
710 case cAudioTrackBase:
711 if (_trackview.is_audio_track()) {
712 canvas_rect->property_fill_color_rgba() = val;
716 if (!_trackview.is_audio_track()) {
717 canvas_rect->property_fill_color_rgba() = val;
720 case cAudioTrackOutline:
721 canvas_rect->property_outline_color_rgba() = val;