fix math bug with numthreads computation
[ardour2.git] / gtk2_ardour / audio_streamview.cc
blob65efd9654c39cce1199bcda758c37e973f7429dc
1 /*
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.
19 #include <cmath>
20 #include <cassert>
21 #include <utility>
23 #include <gtkmm.h>
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"
52 #include "utils.h"
54 #include "i18n.h"
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59 using namespace Editing;
61 AudioStreamView::AudioStreamView (AudioTimeAxisView& tv)
62 : StreamView (tv)
64 crossfades_visible = true;
65 color_handler ();
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 ()
75 int
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);
84 return 0;
87 int
88 AudioStreamView::set_amplitude_above_axis (gdouble app)
90 RegionViewList::iterator i;
92 if (app < 1.0) {
93 return -1;
96 _amplitude_above_axis = app;
98 for (i = region_views.begin(); i != region_views.end(); ++i) {
99 AudioRegionView* const arv = dynamic_cast<AudioRegionView*>(*i);
100 if (arv)
101 arv->set_amplitude_above_axis (app);
104 return 0;
107 RegionView*
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);
113 if (region == 0) {
114 return 0;
117 switch (_trackview.audio_track()->mode()) {
119 case NonLayered:
120 case Normal:
121 if (recording) {
122 region_view = new AudioRegionView (_canvas_group, _trackview, region,
123 _samples_per_unit, region_color, recording, TimeAxisViewItem::Visibility(
124 TimeAxisViewItem::ShowFrame | TimeAxisViewItem::HideFrameRight));
125 } else {
126 region_view = new AudioRegionView (_canvas_group, _trackview, region,
127 _samples_per_unit, region_color);
129 break;
130 case Destructive:
131 region_view = new TapeAudioRegionView (_canvas_group, _trackview, region,
132 _samples_per_unit, region_color);
133 break;
134 default:
135 fatal << string_compose (_("programming error: %1"), "illegal track mode in ::add_region_view_internal") << endmsg;
136 /*NOTREACHED*/
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 ());
156 return region_view;
159 RegionView*
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) {
164 return 0;
167 // if(!recording){
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);
178 // if (arv) {
179 // arv->set_waveform_scale (_waveform_scale);
180 // arv->set_waveform_shape (_waveform_shape);
181 // }
183 // return NULL;
184 // }
185 // }
186 // }
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);
196 return region_view;
199 void
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());
206 if (!r) {
207 return;
210 if (!_trackview.session()->deletion_in_progress()) {
212 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end();) {
213 CrossfadeViewList::iterator tmp;
215 tmp = i;
216 ++tmp;
218 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion>(r);
219 if (ar && i->second->crossfade->involves (ar)) {
220 delete i->second;
221 crossfade_views.erase (i);
224 i = tmp;
228 StreamView::remove_region_view(r);
231 void
232 AudioStreamView::undisplay_track ()
234 StreamView::undisplay_track ();
236 for (CrossfadeViewList::iterator i = crossfade_views.begin(); i != crossfade_views.end(); ++i) {
237 delete i->second;
240 crossfade_views.clear ();
243 void
244 AudioStreamView::playlist_layered (boost::weak_ptr<Track> wtr)
246 boost::shared_ptr<Track> tr (wtr.lock());
248 if (!tr) {
249 return;
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();
261 void
262 AudioStreamView::playlist_switched (boost::weak_ptr<Track> wtr)
264 boost::shared_ptr<Track> tr (wtr.lock());
266 if (!tr) {
267 return;
270 playlist_connections.drop_connections ();
272 StreamView::playlist_switched (tr);
274 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist> (tr->playlist());
276 if (apl) {
277 apl->NewCrossfade.connect (playlist_connections, invalidator (*this), ui_bind (&AudioStreamView::add_crossfade, this, _1), gui_context());
281 void
282 AudioStreamView::add_crossfade (boost::weak_ptr<Crossfade> wc)
284 boost::shared_ptr<Crossfade> crossfade (wc.lock());
286 if (!crossfade) {
287 return;
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) {
298 i->second->hide();
299 } else {
300 i->second->show ();
302 i->second->set_valid (true);
303 return;
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())) {
312 lview = arv;
314 if (!rview && arv && (arv->region() == crossfade->in())) {
315 rview = arv;
319 CrossfadeView *cv = new CrossfadeView (_trackview.canvas_display (),
320 _trackview,
321 crossfade,
322 _samples_per_unit,
323 region_color,
324 *lview, *rview);
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) {
329 cv->hide ();
332 update_content_height (cv);
335 void
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) {
344 delete i->second;
345 crossfade_views.erase (i);
346 break;
351 void
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()) {
367 xi->second->show ();
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()
382 if (apl) {
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();) {
389 tmpx = xi;
390 tmpx++;
392 if (!xi->second->valid()) {
393 delete xi->second;
394 crossfade_views.erase (xi);
397 xi = tmpx;
400 // Stack regions by layer, and remove invalid regions
401 layer_regions();
404 void
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);
409 if (arv) {
410 arv->set_waveform_visible (yn);
415 void
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);
420 if (arv)
421 arv->set_waveform_shape (shape);
425 void
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);
430 if (arv) {
431 arv->set_waveform_scale (scale);
436 void
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";
445 if (!rec_active &&
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 */
452 SourceList sources;
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);
459 if (src) {
460 sources.push_back (src);
461 src->PeakRangeReady.connect (rec_data_ready_connections,
462 invalidator (*this),
463 ui_bind (&AudioStreamView::rec_peak_range_ready, this, _1, _2, boost::weak_ptr<Source>(src)),
464 gui_context());
468 // handle multi
470 nframes_t start = 0;
471 if (rec_regions.size() > 0) {
472 start = rec_regions.back().first->start()
473 + _trackview.track()->get_captured_frames(rec_regions.size()-1);
476 PropertyList plist;
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)));
486 assert(region);
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);
499 gdouble xend;
500 uint32_t fill_color;
502 switch (_trackview.audio_track()->mode()) {
503 case Normal:
504 case NonLayered:
505 xend = xstart;
506 fill_color = ARDOUR_UI::config()->canvasvar_RecordingRect.get();
507 break;
509 case Destructive:
510 xend = xstart + 2;
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);
516 break;
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();
529 RecBoxInfo recbox;
530 recbox.rectangle = rec_rect;
531 recbox.start = _trackview.session()->transport_frame();
532 recbox.length = 0;
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));
539 rec_updating = true;
540 rec_active = true;
542 } else if (rec_active &&
543 (_trackview.session()->record_status() != Session::Recording ||
544 !_trackview.track()->record_enabled())) {
545 screen_update_connection.disconnect();
546 rec_active = false;
547 rec_updating = false;
550 } else {
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;
560 rec_active = 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;
567 tmp = iter;
568 ++tmp;
570 (*iter).first->drop_references ();
572 iter = tmp;
575 rec_regions.clear();
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;
585 rec_rects.clear();
591 void
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) ();
599 void
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());
606 if (!src) {
607 return;
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();
624 void
625 AudioStreamView::update_rec_regions ()
627 if (Config->get_show_waveforms_while_recording ()) {
628 uint32_t n = 0;
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;
634 ++tmp;
636 if (!canvas_item_visible (rec_rects[n].rectangle)) {
637 /* rect already hidden, this region is done */
638 iter = tmp;
639 continue;
642 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion>(iter->first);
643 if (!region) {
644 iter = tmp;
645 continue;
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 ();
663 if (origlen == 1) {
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;
675 } else {
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 ();
688 if (origlen == 1) {
689 /* our special initial length */
690 add_region_view_internal (region, false, true);
693 /* also hide rect */
694 ArdourCanvas::Item * rect = rec_rects[n].rectangle;
695 rect->hide();
701 iter = tmp;
706 void
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);
711 if (arv) {
712 arv->set_fade_visibility (true);
717 void
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);
722 if (arv) {
723 arv->set_fade_visibility (false);
728 void
729 AudioStreamView::show_all_xfades ()
731 foreach_crossfadeview (&CrossfadeView::show);
732 crossfades_visible = true;
735 void
736 AudioStreamView::hide_all_xfades ()
738 foreach_crossfadeview (&CrossfadeView::hide);
739 crossfades_visible = false;
742 void
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 ();
752 void
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()) {
757 i->second->show ();
762 void
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();
774 } else {
775 canvas_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AudioBusBase.get();
780 void
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);
790 void
791 AudioStreamView::update_content_height (CrossfadeView* cv)
793 cv->show ();
795 if (_layer_display == Overlaid) {
797 cv->set_y (0);
798 cv->set_height (height);
800 } else {
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);
816 void
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 ());