From beb73edf553dadbbef7a02727c04f51294b0f4e6 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Mon, 17 Jul 2017 20:12:33 +0200 Subject: Purify libcanvas, remove libardour dependency A canvas is just a canvas. Move WaveView into its own library. --- gtk2_ardour/ardev_common.sh.in | 2 +- gtk2_ardour/ardour_ui_options.cc | 21 +- gtk2_ardour/audio_region_view.cc | 20 +- gtk2_ardour/audio_region_view.h | 9 +- gtk2_ardour/color_theme_manager.cc | 9 +- gtk2_ardour/crossfade_edit.h | 5 +- gtk2_ardour/ghostregion.cc | 10 +- gtk2_ardour/ghostregion.h | 4 +- gtk2_ardour/rc_option_editor.cc | 4 +- gtk2_ardour/tape_region_view.cc | 14 +- gtk2_ardour/wscript | 3 + libs/canvas/canvas/debug.h | 1 - libs/canvas/canvas/fwd.h | 7 +- libs/canvas/canvas/wave_view.h | 271 ------ libs/canvas/canvas/wave_view_private.h | 365 ------- libs/canvas/debug.cc | 1 - libs/canvas/fill.cc | 2 - libs/canvas/item.cc | 2 - libs/canvas/outline.cc | 2 - libs/canvas/test/wave_view.cc | 164 ---- libs/canvas/test/wave_view.h | 26 - libs/canvas/wave_view.cc | 1426 ---------------------------- libs/canvas/wave_view_private.cc | 458 --------- libs/canvas/wscript | 10 +- libs/waveview/debug.cc | 23 + libs/waveview/wave_view.cc | 1425 +++++++++++++++++++++++++++ libs/waveview/wave_view_private.cc | 458 +++++++++ libs/waveview/waveview/debug.h | 35 + libs/waveview/waveview/visibility.h | 45 + libs/waveview/waveview/wave_view.h | 268 ++++++ libs/waveview/waveview/wave_view_private.h | 365 +++++++ libs/waveview/wscript | 64 ++ wscript | 1 + 33 files changed, 2747 insertions(+), 2773 deletions(-) delete mode 100644 libs/canvas/canvas/wave_view.h delete mode 100644 libs/canvas/canvas/wave_view_private.h delete mode 100644 libs/canvas/test/wave_view.cc delete mode 100644 libs/canvas/test/wave_view.h delete mode 100644 libs/canvas/wave_view.cc delete mode 100644 libs/canvas/wave_view_private.cc create mode 100644 libs/waveview/debug.cc create mode 100644 libs/waveview/wave_view.cc create mode 100644 libs/waveview/wave_view_private.cc create mode 100644 libs/waveview/waveview/debug.h create mode 100644 libs/waveview/waveview/visibility.h create mode 100644 libs/waveview/waveview/wave_view.h create mode 100644 libs/waveview/waveview/wave_view_private.h create mode 100644 libs/waveview/wscript diff --git a/gtk2_ardour/ardev_common.sh.in b/gtk2_ardour/ardev_common.sh.in index 0904e143c0..4a96798e70 100644 --- a/gtk2_ardour/ardev_common.sh.in +++ b/gtk2_ardour/ardev_common.sh.in @@ -36,7 +36,7 @@ export ARDOUR_DLL_PATH=$libs export GTK_PATH=~/.ardour3:$libs/clearlooks-newer export VAMP_PATH=$libs/vamp-plugins${VAMP_PATH:+:$VAMP_PATH} -export LD_LIBRARY_PATH=$libs/ptformat:$libs/qm-dsp:$libs/vamp-sdk:$libs/surfaces:$libs/surfaces/control_protocol:$libs/ardour:$libs/midi++2:$libs/pbd:$libs/rubberband:$libs/soundtouch:$libs/gtkmm2ext:$libs/widgets:$libs/appleutility:$libs/taglib:$libs/evoral:$libs/evoral/src/libsmf:$libs/audiographer:$libs/timecode:$libs/libltc:$libs/canvas:$libs/ardouralsautil${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} +export LD_LIBRARY_PATH=$libs/ptformat:$libs/qm-dsp:$libs/vamp-sdk:$libs/surfaces:$libs/surfaces/control_protocol:$libs/ardour:$libs/midi++2:$libs/pbd:$libs/rubberband:$libs/soundtouch:$libs/gtkmm2ext:$libs/widgets:$libs/appleutility:$libs/taglib:$libs/evoral:$libs/evoral/src/libsmf:$libs/audiographer:$libs/timecode:$libs/libltc:$libs/canvas:$libs/waveview:$libs/ardouralsautil${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} # DYLD_LIBRARY_PATH is for darwin. export DYLD_FALLBACK_LIBRARY_PATH=$LD_LIBRARY_PATH diff --git a/gtk2_ardour/ardour_ui_options.cc b/gtk2_ardour/ardour_ui_options.cc index 638743897b..14a8695c8b 100644 --- a/gtk2_ardour/ardour_ui_options.cc +++ b/gtk2_ardour/ardour_ui_options.cc @@ -25,12 +25,11 @@ #include "pbd/stacktrace.h" #include "pbd/unwind.h" -#include - #include "ardour/rc_configuration.h" #include "ardour/session.h" -#include "canvas/wave_view.h" +#include "gtkmm2ext/utils.h" +#include "waveview/wave_view.h" #include "audio_clock.h" #include "ardour_ui.h" @@ -476,7 +475,7 @@ ARDOUR_UI::parameter_changed (std::string p) Gtkmm2ext::disable_tooltips (); } } else if (p == "waveform-gradient-depth") { - ArdourCanvas::WaveView::set_global_gradient_depth (UIConfiguration::instance().get_waveform_gradient_depth()); + ArdourWaveView::WaveView::set_global_gradient_depth (UIConfiguration::instance().get_waveform_gradient_depth()); } else if (p == "show-mini-timeline") { repack_transport_hbox (); } else if (p == "show-toolbar-recpunch") { @@ -490,17 +489,17 @@ ARDOUR_UI::parameter_changed (std::string p) } else if (p == "show-secondary-clock") { update_clock_visibility (); } else if (p == "waveform-scale") { - ArdourCanvas::WaveView::set_global_logscaled (UIConfiguration::instance().get_waveform_scale() == Logarithmic); + ArdourWaveView::WaveView::set_global_logscaled (UIConfiguration::instance().get_waveform_scale() == Logarithmic); } else if (p == "widget-prelight") { CairoWidget::set_widget_prelight (UIConfiguration::instance().get_widget_prelight()); } else if (p == "waveform-shape") { - ArdourCanvas::WaveView::set_global_shape (UIConfiguration::instance().get_waveform_shape() == Rectified - ? ArdourCanvas::WaveView::Rectified : ArdourCanvas::WaveView::Normal); + ArdourWaveView::WaveView::set_global_shape (UIConfiguration::instance().get_waveform_shape() == Rectified + ? ArdourWaveView::WaveView::Rectified : ArdourWaveView::WaveView::Normal); } else if (p == "show-waveform-clipping") { - ArdourCanvas::WaveView::set_global_show_waveform_clipping (UIConfiguration::instance().get_show_waveform_clipping()); + ArdourWaveView::WaveView::set_global_show_waveform_clipping (UIConfiguration::instance().get_show_waveform_clipping()); } else if (p == "waveform-cache-size") { /* GUI option has units of megabytes; image cache uses units of bytes */ - ArdourCanvas::WaveView::set_image_cache_size (UIConfiguration::instance().get_waveform_cache_size() * 1048576); + ArdourWaveView::WaveView::set_image_cache_size (UIConfiguration::instance().get_waveform_cache_size() * 1048576); } else if (p == "use-wm-visibility") { VisibilityTracker::set_use_window_manager_visibility (UIConfiguration::instance().get_use_wm_visibility()); } else if (p == "action-table-columns") { @@ -516,9 +515,9 @@ ARDOUR_UI::parameter_changed (std::string p) } else if (p == "layered-record-mode") { layered_button.set_active (_session->config.get_layered_record_mode ()); } else if (p == "show-waveform-clipping") { - ArdourCanvas::WaveView::set_global_show_waveform_clipping (UIConfiguration::instance().get_show_waveform_clipping()); + ArdourWaveView::WaveView::set_global_show_waveform_clipping (UIConfiguration::instance().get_show_waveform_clipping()); } else if (p == "waveform-gradient-depth") { - ArdourCanvas::WaveView::set_global_gradient_depth (UIConfiguration::instance().get_waveform_gradient_depth()); + ArdourWaveView::WaveView::set_global_gradient_depth (UIConfiguration::instance().get_waveform_gradient_depth()); } else if (p == "flat-buttons") { bool flat = UIConfiguration::instance().get_flat_buttons(); if (ArdourButton::flat_buttons () != flat) { diff --git a/gtk2_ardour/audio_region_view.cc b/gtk2_ardour/audio_region_view.cc index e81043588c..fd35f23645 100644 --- a/gtk2_ardour/audio_region_view.cc +++ b/gtk2_ardour/audio_region_view.cc @@ -48,6 +48,8 @@ #include "canvas/xfade_curve.h" #include "canvas/debug.h" +#include "waveview/debug.h" + #include "streamview.h" #include "audio_region_view.h" #include "audio_time_axis.h" @@ -427,7 +429,7 @@ AudioRegionView::region_resized (const PropertyChange& what_changed) for (vector::iterator i = ghosts.begin(); i != ghosts.end(); ++i) { if ((agr = dynamic_cast(*i)) != 0) { - for (vector::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) { + for (vector::iterator w = agr->waves.begin(); w != agr->waves.end(); ++w) { (*w)->region_resized (); } } @@ -1139,7 +1141,7 @@ AudioRegionView::delete_waves () } _data_ready_connections.clear (); - for (vector::iterator w = waves.begin(); w != waves.end(); ++w) { + for (vector::iterator w = waves.begin(); w != waves.end(); ++w) { group->remove(*w); } waves.clear(); @@ -1237,7 +1239,7 @@ AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/) gdouble yoff = which * ht; - WaveView *wave = new WaveView (group, audio_region ()); + ArdourWaveView::WaveView *wave = new ArdourWaveView::WaveView (group, audio_region ()); CANVAS_DEBUG_NAME (wave, string_compose ("wave view for chn %1 of %2", which, get_item_name())); wave->set_channel (which); @@ -1252,15 +1254,15 @@ AudioRegionView::create_one_wave (uint32_t which, bool /*direct*/) switch (UIConfiguration::instance().get_waveform_shape()) { case Rectified: - wave->set_shape (WaveView::Rectified); + wave->set_shape (ArdourWaveView::WaveView::Rectified); break; default: - wave->set_shape (WaveView::Normal); + wave->set_shape (ArdourWaveView::WaveView::Normal); } wave->set_logscaled (UIConfiguration::instance().get_waveform_scale() == Logarithmic); - vector v; + vector v; v.push_back (wave); set_some_waveform_colors (v); @@ -1426,7 +1428,7 @@ AudioRegionView::add_ghost (TimeAxisView& tv) break; } - WaveView *wave = new WaveView (ghost->group, audio_region()); + ArdourWaveView::WaveView *wave = new ArdourWaveView::WaveView (ghost->group, audio_region()); CANVAS_DEBUG_NAME (wave, string_compose ("ghost wave for %1", get_item_name())); wave->set_channel (n); @@ -1546,7 +1548,7 @@ AudioRegionView::set_waveform_colors () } void -AudioRegionView::set_some_waveform_colors (vector& waves_to_color) +AudioRegionView::set_some_waveform_colors (vector& waves_to_color) { Gtkmm2ext::Color fill; Gtkmm2ext::Color outline; @@ -1578,7 +1580,7 @@ AudioRegionView::set_some_waveform_colors (vector& wave } } - for (vector::iterator w = waves_to_color.begin(); w != waves_to_color.end(); ++w) { + for (vector::iterator w = waves_to_color.begin(); w != waves_to_color.end(); ++w) { (*w)->set_fill_color (fill); (*w)->set_outline_color (outline); (*w)->set_clip_color (clip); diff --git a/gtk2_ardour/audio_region_view.h b/gtk2_ardour/audio_region_view.h index cc0ab0500e..8841ee2e43 100644 --- a/gtk2_ardour/audio_region_view.h +++ b/gtk2_ardour/audio_region_view.h @@ -29,9 +29,10 @@ #include "ardour/audioregion.h" #include "canvas/fwd.h" -#include "canvas/wave_view.h" #include "canvas/xfade_curve.h" +#include "waveview/wave_view.h" + #include "region_view.h" #include "time_axis_view_item.h" #include "automation_line.h" @@ -151,8 +152,8 @@ protected: WaveformLogScaled = 0x10, }; - std::vector waves; - std::vector tmp_waves; ///< see ::create_waves() + std::vector waves; + std::vector tmp_waves; ///< see ::create_waves() std::list > feature_lines; @@ -209,7 +210,7 @@ private: void parameter_changed (std::string const &); void setup_waveform_visibility (); - void set_some_waveform_colors (std::vector& waves_to_color); + void set_some_waveform_colors (std::vector& waves_to_color); /** A ScopedConnection for each PeaksReady callback (one per channel). Each member * may be 0 if no connection exists. diff --git a/gtk2_ardour/color_theme_manager.cc b/gtk2_ardour/color_theme_manager.cc index f43d8ff2de..c9ee7ac308 100644 --- a/gtk2_ardour/color_theme_manager.cc +++ b/gtk2_ardour/color_theme_manager.cc @@ -26,9 +26,6 @@ #include "pbd/gstdio_compat.h" -#include "gtkmm2ext/cell_renderer_color_selector.h" -#include "gtkmm2ext/utils.h" - #include "pbd/compose.h" #include "pbd/file_utils.h" #include "pbd/replace_all.h" @@ -36,10 +33,14 @@ #include "ardour/filesystem_paths.h" #include "ardour/profile.h" +#include "gtkmm2ext/cell_renderer_color_selector.h" +#include "gtkmm2ext/utils.h" + #include "canvas/container.h" #include "canvas/rectangle.h" #include "canvas/scroll_group.h" -#include "canvas/wave_view.h" + +#include "waveview/wave_view.h" #include "ardour_dialog.h" #include "color_theme_manager.h" diff --git a/gtk2_ardour/crossfade_edit.h b/gtk2_ardour/crossfade_edit.h index 911b9a7df6..f18b26ae8a 100644 --- a/gtk2_ardour/crossfade_edit.h +++ b/gtk2_ardour/crossfade_edit.h @@ -44,6 +44,9 @@ namespace ArdourCanvas { class Rectangle; class Line; class Polygon; +} + +namespace ArdourWaveview { class WaveView; } @@ -119,7 +122,7 @@ private: std::list points; ARDOUR::AutomationList normative_curve; /* 0 - 1.0, linear */ ARDOUR::AutomationList gain_curve; /* 0 - 2.0, gain mapping */ - std::vector waves; + std::vector waves; Half(); }; diff --git a/gtk2_ardour/ghostregion.cc b/gtk2_ardour/ghostregion.cc index 799e53d3ca..b531bd9aa8 100644 --- a/gtk2_ardour/ghostregion.cc +++ b/gtk2_ardour/ghostregion.cc @@ -17,15 +17,17 @@ */ +#include "evoral/Note.hpp" + #include "ardour/parameter_descriptor.h" -#include "evoral/Note.hpp" #include "canvas/container.h" #include "canvas/polygon.h" #include "canvas/rectangle.h" -#include "canvas/wave_view.h" #include "canvas/debug.h" +#include "waveview/wave_view.h" + #include "automation_time_axis.h" #include "ghostregion.h" #include "midi_streamview.h" @@ -126,7 +128,7 @@ AudioGhostRegion::AudioGhostRegion(RegionView& rv, void AudioGhostRegion::set_samples_per_pixel (double fpp) { - for (vector::iterator i = waves.begin(); i != waves.end(); ++i) { + for (vector::iterator i = waves.begin(); i != waves.end(); ++i) { (*i)->set_samples_per_pixel (fpp); } } @@ -134,7 +136,7 @@ AudioGhostRegion::set_samples_per_pixel (double fpp) void AudioGhostRegion::set_height () { - vector::iterator i; + vector::iterator i; uint32_t n; GhostRegion::set_height(); diff --git a/gtk2_ardour/ghostregion.h b/gtk2_ardour/ghostregion.h index 7192538394..d8cff83143 100644 --- a/gtk2_ardour/ghostregion.h +++ b/gtk2_ardour/ghostregion.h @@ -24,7 +24,7 @@ #include #include "pbd/signals.h" -namespace ArdourCanvas { +namespace ArdourWaveView { class WaveView; } @@ -76,7 +76,7 @@ public: void set_height(); void set_colors(); - std::vector waves; + std::vector waves; }; class MidiGhostRegion : public GhostRegion { diff --git a/gtk2_ardour/rc_option_editor.cc b/gtk2_ardour/rc_option_editor.cc index b69cddf89e..91ceba039e 100644 --- a/gtk2_ardour/rc_option_editor.cc +++ b/gtk2_ardour/rc_option_editor.cc @@ -51,7 +51,7 @@ #include "ardour/plugin_manager.h" #include "control_protocol/control_protocol.h" -#include "canvas/wave_view.h" +#include "waveview/wave_view.h" #include "widgets/paths_dialog.h" #include "widgets/tooltips.h" @@ -1198,7 +1198,7 @@ private: { UIConfiguration::instance().set_waveform_clip_level (_clip_level_adjustment.get_value()); /* XXX: should be triggered from the parameter changed signal */ - ArdourCanvas::WaveView::set_clip_level (_clip_level_adjustment.get_value()); + ArdourWaveView::WaveView::set_clip_level (_clip_level_adjustment.get_value()); } Adjustment _clip_level_adjustment; diff --git a/gtk2_ardour/tape_region_view.cc b/gtk2_ardour/tape_region_view.cc index 284ba62eab..1e3c31f565 100644 --- a/gtk2_ardour/tape_region_view.cc +++ b/gtk2_ardour/tape_region_view.cc @@ -91,10 +91,10 @@ TapeAudioRegionView::update (uint32_t /*n*/) #if 0 /* deadlock: -#1 0x00007f9570ebd77c in g_mutex_lock_slowpath (mutex=0x7f9575157760 ) at ././glib/gthread-posix.c:1313 +#1 0x00007f9570ebd77c in g_mutex_lock_slowpath (mutex=0x7f9575157760 ) at ././glib/gthread-posix.c:1313 #2 0x000055f6f8d1aac0 in Glib::Threads::Mutex::Lock::Lock(Glib::Threads::Mutex&) (this=0x7ffc4d905aa0, mutex=...) at /usr/include/glibmm-2.4/glibmm/threads.h:688 -#3 0x00007f9574f05054 in ArdourCanvas::WaveView::invalidate_image_cache() (this=0x55f6fdf341a0) at ../libs/canvas/wave_view.cc:275 -#4 0x00007f9574f0b1b0 in ArdourCanvas::WaveView::gain_changed() (this=0x55f6fdf341a0) at ../libs/canvas/wave_view.cc:1329 +#3 0x00007f9574f05054 in ArdourWaveView::WaveView::invalidate_image_cache() (this=0x55f6fdf341a0) at ../libs/canvas/wave_view.cc:275 +#4 0x00007f9574f0b1b0 in ArdourWaveView::WaveView::gain_changed() (this=0x55f6fdf341a0) at ../libs/canvas/wave_view.cc:1329 #5 0x000055f6f96eb1b8 in TapeAudioRegionView::update(unsigned int) (this=0x55f6fdf32640) at ../gtk2_ardour/tape_region_view.cc:102 #6 0x000055f6f96eba9f in boost::_mfi::mf1::operator()(TapeAudioRegionView*, unsigned int) const (this=0x55f6fa74ce10, p=0x55f6fdf32640, a1=0) at /usr/include/boost/bind/mem_fn_template.hpp:165 @@ -125,12 +125,12 @@ TapeAudioRegionView::update (uint32_t /*n*/) #25 0x00007f957394eb15 in ARDOUR::AudioSource::read_peaks(ARDOUR::PeakData*, long, long, long, double) const (this=0x55f6fd359140, peaks=0x55f700da6b80, npeaks=1356, start=113190, cnt=447480, samples_per_visual_peak=330) at ../libs/ardour/audiosource.cc:337 #26 0x00007f957393a905 in ARDOUR::AudioRegion::read_peaks(ARDOUR::PeakData*, long, long, long, unsigned int, double) const (this= 0x55f6fd84c3b0, buf=0x55f700da6b80, npeaks=1356, offset=113190, cnt=447480, chan_n=0, frames_per_pixel=330) at ../libs/ardour/audioregion.cc:431 -#27 0x00007f9574f09caa in ArdourCanvas::WaveView::generate_image(boost::shared_ptr, bool) const (this=0x55f6fdf341a0, req=..., in_render_thread=false) +#27 0x00007f9574f09caa in ArdourWaveView::WaveView::generate_image(boost::shared_ptr, bool) const (this=0x55f6fdf341a0, req=..., in_render_thread=false) at ../libs/canvas/wave_view.cc:1005 -#28 0x00007f9574f08e99 in ArdourCanvas::WaveView::get_image(long, long, bool&) const (this=0x55f6fdf341a0, start=225060, end=448800, full_image=@0x7ffc4d9070c7: false) +#28 0x00007f9574f08e99 in ArdourWaveView::WaveView::get_image(long, long, bool&) const (this=0x55f6fdf341a0, start=225060, end=448800, full_image=@0x7ffc4d9070c7: false) ---Type to continue, or q to quit--- at ../libs/canvas/wave_view.cc:870 -#29 0x00007f9574f0a6e8 in ArdourCanvas::WaveView::render(ArdourCanvas::Rect const&, Cairo::RefPtr) const (this=0x55f6fdf341a0, area=..., context=...) +#29 0x00007f9574f0a6e8 in ArdourWaveView::WaveView::render(ArdourCanvas::Rect const&, Cairo::RefPtr) const (this=0x55f6fdf341a0, area=..., context=...) at ../libs/canvas/wave_view.cc:1180 */ @@ -145,7 +145,7 @@ TapeAudioRegionView::update (uint32_t /*n*/) /* this is a quick hack to draw something (abuse gain_changed to force * an image-cache invalidation. * - * TODO: ArdourCanvas::WaveView needs an API to look up the specific channel "n" + * TODO: ArdourWaveView::WaveView needs an API to look up the specific channel "n" * and a special case to not only invalidate the cache but re-expose the * waveform. e.g. * diff --git a/gtk2_ardour/wscript b/gtk2_ardour/wscript index 6b0b4037c4..7df4ba2721 100644 --- a/gtk2_ardour/wscript +++ b/gtk2_ardour/wscript @@ -481,6 +481,7 @@ def build(bld): 'libmidipp', 'libgtkmm2ext', 'libcanvas', + 'libwaveview', 'libwidgets', 'libptformat', ] @@ -553,6 +554,7 @@ def build(bld): 'libgtk2_ardour', 'libgtkmm2ext', 'libcanvas', + 'libwaveview', 'libwidgets', 'libptformat', ] @@ -599,6 +601,7 @@ def build(bld): 'libmidipp', 'libgtkmm2ext', 'libcanvas', + 'libwaveview', 'libwidgets', 'libptformat', ] diff --git a/libs/canvas/canvas/debug.h b/libs/canvas/canvas/debug.h index ecf7eee46f..b0797531b0 100644 --- a/libs/canvas/canvas/debug.h +++ b/libs/canvas/canvas/debug.h @@ -33,7 +33,6 @@ namespace PBD { LIBCANVAS_API extern DebugBits CanvasEvents; LIBCANVAS_API extern DebugBits CanvasRender; LIBCANVAS_API extern DebugBits CanvasEnterLeave; - LIBCANVAS_API extern DebugBits WaveView; } } diff --git a/libs/canvas/canvas/fwd.h b/libs/canvas/canvas/fwd.h index 61cab3c9d2..bae8ef20fb 100644 --- a/libs/canvas/canvas/fwd.h +++ b/libs/canvas/canvas/fwd.h @@ -22,18 +22,17 @@ #define __canvas_canvas_fwd_h__ namespace ArdourCanvas { - class WaveView; class Line; - class LineSet; + class LineSet; class Rectangle; - class Ruler; + class Ruler; class Polygon; class PolyLine; class GtkCanvas; class GtkCanvasViewport; class Text; class Curve; - class ScrollGroup; + class ScrollGroup; } #endif /* __canvas_canvas_fwd_h__ */ diff --git a/libs/canvas/canvas/wave_view.h b/libs/canvas/canvas/wave_view.h deleted file mode 100644 index 1767262151..0000000000 --- a/libs/canvas/canvas/wave_view.h +++ /dev/null @@ -1,271 +0,0 @@ -/* - Copyright (C) 2011-2013 Paul Davis - Copyright (C) 2017 Tim Mayberry - Author: Carl Hetherington - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#ifndef __CANVAS_WAVE_VIEW_H__ -#define __CANVAS_WAVE_VIEW_H__ - -#include -#include - -#include "ardour/types.h" - -#include - -#include "canvas/visibility.h" -#include "canvas/item.h" - -namespace ARDOUR { - class AudioRegion; -} - -namespace Gdk { - class Pixbuf; -} - -class WaveViewTest; - -namespace ArdourCanvas { - -class WaveViewCacheGroup; -class WaveViewDrawRequest; -class WaveViewDrawRequestQueue; -class WaveViewImage; -class WaveViewProperties; -class WaveViewDrawingThread; - -class LIBCANVAS_API WaveView : public Item, public sigc::trackable -{ -public: - enum Shape { Normal, Rectified }; - - std::string debug_name () const; - - /* Displays a single channel of waveform data for the given Region. - - x = 0 in the waveview corresponds to the first waveform datum taken - from region->start() samples into the source data. - - x = N in the waveview corresponds to the (N * spp)'th sample - measured from region->start() into the source data. - - when drawing, we will map the zeroth-pixel of the waveview - into a window. - - The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces - that cache sections of the display. This is filled on-demand and - never cleared until something explicitly marks the cache invalid - (such as a change in samples_per_pixel, the log scaling, rectified or - other view parameters). - */ - - WaveView (Canvas*, boost::shared_ptr); - WaveView (Item*, boost::shared_ptr); - ~WaveView (); - - virtual void prepare_for_render (Rect const& window_area) const; - - virtual void render (Rect const & area, Cairo::RefPtr) const; - - void compute_bounding_box () const; - - void set_samples_per_pixel (double); - void set_height (Distance); - void set_channel (int); - void set_region_start (ARDOUR::frameoffset_t); - - /** Change the first position drawn by @param pixels. - * @param pixels must be positive. This is used by - * AudioRegionViews in Ardour to avoid drawing the - * first pixel of a waveform, and exists in case - * there are uses for WaveView where we do not - * want this behaviour. - */ - void set_start_shift (double pixels); - - void set_fill_color (Gtkmm2ext::Color); - void set_outline_color (Gtkmm2ext::Color); - - void region_resized (); - void gain_changed (); - - void set_show_zero_line (bool); - bool show_zero_line () const; - - void set_zero_color (Gtkmm2ext::Color); - void set_clip_color (Gtkmm2ext::Color); - void set_logscaled (bool); - - void set_gradient_depth (double); - double gradient_depth () const; - - void set_shape (Shape); - - void set_always_get_image_in_thread (bool yn); - - /* currently missing because we don't need them (yet): - * set_shape_independent(); - * set_logscaled_independent(); - */ - - static void set_global_gradient_depth (double); - static void set_global_logscaled (bool); - static void set_global_shape (Shape); - static void set_global_show_waveform_clipping (bool); - - static double global_gradient_depth () { return _global_gradient_depth; } - - static bool global_logscaled () { return _global_logscaled; } - - static Shape global_shape () { return _global_shape; } - - void set_amplitude_above_axis (double v); - - double amplitude_above_axis () const; - - static void set_clip_level (double dB); - static PBD::Signal0 ClipLevelChanged; - - static void start_drawing_thread (); - static void stop_drawing_thread (); - - static void set_image_cache_size (uint64_t); - -#ifdef CANVAS_COMPATIBILITY - void*& property_gain_src () { - return _foo_void; - } - void*& property_gain_function () { - return _foo_void; - } - -private: - void* _foo_void; -#endif - -private: - friend class ::WaveViewTest; - friend class WaveViewThreadClient; - friend class WaveViewDrawingThread; - - boost::shared_ptr _region; - - boost::scoped_ptr _props; - - mutable boost::shared_ptr _image; - - mutable boost::shared_ptr _cache_group; - - bool _shape_independent; - bool _logscaled_independent; - bool _gradient_depth_independent; - - /** Under almost conditions, this is going to return _region->length(), - * but if region_start has been reset, then we need to use this modified - * computation. - */ - ARDOUR::framecnt_t region_length () const; - - /** Under almost conditions, this is going to return _region->start() + - * _region->length(), but if region_start has been reset, then we need to use - * this modified computation. - */ - ARDOUR::framepos_t region_end () const; - - /** - * _image stays non-null after the first time it is set - */ - bool rendered () const { return _image.get(); } - - bool draw_image_in_gui_thread () const; - - /** If true, calls to render() will render a missing wave image in the GUI - * thread. Generally set to false, but true after a call to set_height(). - */ - mutable bool _draw_image_in_gui_thread; - - /** If true, calls to render() will render a missing wave image in the GUI - * thread. Set true for waveviews we expect to keep updating (e.g. while - * recording) - */ - bool _always_draw_image_in_gui_thread; - - void init(); - - mutable boost::shared_ptr current_request; - - PBD::ScopedConnectionList invalidation_connection; - - static double _global_gradient_depth; - static bool _global_logscaled; - static Shape _global_shape; - static bool _global_show_waveform_clipping; - static double _global_clip_level; - - static PBD::Signal0 VisualPropertiesChanged; - - void handle_visual_property_change (); - void handle_clip_level_change (); - - struct LineTips { - double top; - double bot; - double spread; - bool clip_max; - bool clip_min; - - LineTips () : top (0.0), bot (0.0), clip_max (false), clip_min (false) {} - }; - - static ArdourCanvas::Coord y_extent (double, Shape const, double const height); - - static void compute_tips (ARDOUR::PeakData const& peak, LineTips& tips, double const effective_height); - - static void draw_image (Cairo::RefPtr&, ARDOUR::PeakData*, int n_peaks, - boost::shared_ptr); - static void draw_absent_image (Cairo::RefPtr&, ARDOUR::PeakData*, int); - - ARDOUR::framecnt_t optimal_image_width_samples () const; - - void set_image (boost::shared_ptr img) const; - - // @return true if item area intersects with draw area - bool get_item_and_draw_rect_in_window_coords (Rect const& canvas_rect, Rect& item_area, - Rect& draw_rect) const; - - boost::shared_ptr create_draw_request (WaveViewProperties const&) const; - - void queue_draw_request (boost::shared_ptr const&) const; - - static void process_draw_request (boost::shared_ptr); - - boost::shared_ptr get_cache_group () const; - - /** - * Notify the Cache that we are dropping our reference to the - * CacheGroup so it can also do so if it is the only reference holder - * of the cache group. - */ - void reset_cache_group (); -}; - -} // namespace ArdourCanvas - -#endif // __CANVAS_WAVE_VIEW_H__ diff --git a/libs/canvas/canvas/wave_view_private.h b/libs/canvas/canvas/wave_view_private.h deleted file mode 100644 index d870300998..0000000000 --- a/libs/canvas/canvas/wave_view_private.h +++ /dev/null @@ -1,365 +0,0 @@ -/* - Copyright (C) 2017 Tim Mayberry - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#ifndef __CANVAS_WAVE_VIEW_PRIVATE_H__ -#define __CANVAS_WAVE_VIEW_PRIVATE_H__ - -#include - -#include "canvas/wave_view.h" - -namespace ARDOUR { - class AudioRegion; -} - -namespace ArdourCanvas { - -struct WaveViewProperties -{ -public: // ctors - WaveViewProperties (boost::shared_ptr region); - - // WaveViewProperties (WaveViewProperties const& other) = default; - - // WaveViewProperties& operator=(WaveViewProperties const& other) = default; - -public: // member variables - - framepos_t region_start; - framepos_t region_end; - uint16_t channel; - double height; - double samples_per_pixel; - double amplitude; - double amplitude_above_axis; - Gtkmm2ext::Color fill_color; - Gtkmm2ext::Color outline_color; - Gtkmm2ext::Color zero_color; - Gtkmm2ext::Color clip_color; - bool show_zero; - bool logscaled; - WaveView::Shape shape; - double gradient_depth; - double start_shift; - -private: // member variables - - framepos_t sample_start; - framepos_t sample_end; - -public: // methods - - bool is_valid () const - { - return (sample_end != 0 && samples_per_pixel != 0); - } - - void set_width_samples (ARDOUR::framecnt_t const width_samples) - { - assert (is_valid()); - assert (width_samples != 0); - ARDOUR::framecnt_t half_width = width_samples / 2; - framepos_t new_sample_start = std::max (region_start, get_center_sample () - half_width); - framepos_t new_sample_end = std::min (get_center_sample () + half_width, region_end); - assert (new_sample_start <= new_sample_end); - sample_start = new_sample_start; - sample_end = new_sample_end; - } - - uint64_t get_width_pixels () const - { - return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel))); - } - - - void set_sample_offsets (framepos_t const start, framepos_t const end) - { - assert (start <= end); - - // sample_start and sample_end are bounded by the region limits. - if (start < region_start) { - sample_start = region_start; - } else if (start > region_end) { - sample_start = region_end; - } else { - sample_start = start; - } - - if (end > region_end) { - sample_end = region_end; - } else if (end < region_start) { - sample_end = region_start; - } else { - sample_end = end; - } - - assert (sample_start <= sample_end); - } - - framepos_t get_sample_start () const - { - return sample_start; - } - - framepos_t get_sample_end () const - { - return sample_end; - } - - void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel) - { - assert (start_pixel <= end_pixel); - /** - * It is possible for the new sample positions to be past the region_end, - * so we have to do bounds checking/adjustment for this in set_sample_offsets. - */ - framepos_t new_sample_start = region_start + (start_pixel * samples_per_pixel); - framepos_t new_sample_end = region_start + (end_pixel * samples_per_pixel); - set_sample_offsets (new_sample_start, new_sample_end); - } - - ARDOUR::framecnt_t get_length_samples () const - { - assert (sample_start <= sample_end); - return sample_end - sample_start; - } - - framepos_t get_center_sample () - { - return sample_start + (get_length_samples() / 2); - } - - bool is_equivalent (WaveViewProperties const& other) - { - return (samples_per_pixel == other.samples_per_pixel && - contains (other.sample_start, other.sample_end) && channel == other.channel && - height == other.height && amplitude == other.amplitude && - amplitude_above_axis == other.amplitude_above_axis && fill_color == other.fill_color && - outline_color == other.outline_color && zero_color == other.zero_color && - clip_color == other.clip_color && show_zero == other.show_zero && - logscaled == other.logscaled && shape == other.shape && - gradient_depth == other.gradient_depth); - // region_start && start_shift?? - } - - bool contains (framepos_t start, framepos_t end) - { - return (sample_start <= start && end <= sample_end); - } -}; - -struct WaveViewImage { -public: // ctors - WaveViewImage (boost::shared_ptr const& region_ptr, - WaveViewProperties const& properties); - - ~WaveViewImage (); - -public: // member variables - boost::weak_ptr region; - WaveViewProperties props; - Cairo::RefPtr cairo_image; - uint64_t timestamp; - -public: // methods - bool finished() { return static_cast(cairo_image); } - - bool - contains_image_with_properties (WaveViewProperties const& other_props) - { - return cairo_image && props.is_equivalent (other_props); - } - - bool is_valid () { - return props.is_valid (); - } - - size_t size_in_bytes () - { - // 4 = bytes per FORMAT_ARGB32 pixel - return props.height * props.get_width_pixels() * 4; - } -}; - -struct WaveViewDrawRequest -{ -public: - WaveViewDrawRequest (); - ~WaveViewDrawRequest (); - - bool stopped() const { return (bool) g_atomic_int_get (const_cast(&stop)); } - void cancel() { g_atomic_int_set (&stop, 1); } - bool finished() { return image->finished(); } - - boost::shared_ptr image; - - bool is_valid () { - return (image && image->is_valid()); - } - -private: - gint stop; /* intended for atomic access */ -}; - -class WaveViewCache; - -class WaveViewCacheGroup -{ -public: - WaveViewCacheGroup (WaveViewCache& parent_cache); - - ~WaveViewCacheGroup (); - -public: - - // @return image with matching properties or null - boost::shared_ptr lookup_image (WaveViewProperties const&); - - void add_image (boost::shared_ptr); - - bool full () const { return _cached_images.size() > max_size(); } - - static uint32_t max_size () { return 16; } - - void clear_cache (); - -private: - - /** - * At time of writing we don't strictly need a reference to the parent cache - * as there is only a single global cache but if the image cache ever becomes - * a per canvas cache then a using a reference is handy. - */ - WaveViewCache& _parent_cache; - - typedef std::list > ImageCache; - ImageCache _cached_images; -}; - -class WaveViewCache -{ -public: - static WaveViewCache* get_instance (); - - uint64_t image_cache_threshold () const { return _image_cache_threshold; } - void set_image_cache_threshold (uint64_t); - - void clear_cache (); - - boost::shared_ptr get_cache_group (boost::shared_ptr); - - void reset_cache_group (boost::shared_ptr&); - -private: - WaveViewCache(); - ~WaveViewCache(); - -private: - typedef std::map, boost::shared_ptr > - CacheGroups; - - CacheGroups cache_group_map; - - uint64_t image_cache_size; - uint64_t _image_cache_threshold; - -private: - friend class WaveViewCacheGroup; - - void increase_size (uint64_t bytes); - void decrease_size (uint64_t bytes); - - bool full () { return image_cache_size > _image_cache_threshold; } -}; - -class WaveViewDrawRequestQueue -{ -public: - - void enqueue (boost::shared_ptr&); - - // @return valid request or null if non-blocking or no request is available - boost::shared_ptr dequeue (bool block); - - void wake_up (); - -private: - - mutable Glib::Threads::Mutex _queue_mutex; - Glib::Threads::Cond _cond; - - typedef std::deque > DrawRequestQueueType; - DrawRequestQueueType _queue; -}; - -class WaveViewDrawingThread -{ -public: - WaveViewDrawingThread (); - ~WaveViewDrawingThread (); - -private: - void start (); - void quit (); - void run (); - -private: - Glib::Threads::Thread* _thread; - gint _quit; -}; - -class WaveViewThreads { -private: - WaveViewThreads (); - ~WaveViewThreads (); - -public: - static void initialize (); - static void deinitialize (); - - static bool enabled () { return (instance); } - - static void enqueue_draw_request (boost::shared_ptr&); - -private: - friend class WaveViewDrawingThread; - - static void wake_up (); - - // will block until a request is available - static boost::shared_ptr dequeue_draw_request (); - - void start_threads (); - void stop_threads (); - -private: - static uint32_t init_count; - static WaveViewThreads* instance; - - // TODO use std::unique_ptr when possible - typedef std::vector > WaveViewThreadList; - - WaveViewThreadList _threads; - WaveViewDrawRequestQueue _request_queue; -}; - - -} // namespace ArdourCanvas - -#endif // __CANVAS_WAVE_VIEW_PRIVATE_H__ diff --git a/libs/canvas/debug.cc b/libs/canvas/debug.cc index 086c69b7a7..e958678d7d 100644 --- a/libs/canvas/debug.cc +++ b/libs/canvas/debug.cc @@ -29,7 +29,6 @@ PBD::DebugBits PBD::DEBUG::CanvasItemsDirtied = PBD::new_debug_bit ("canvasitems PBD::DebugBits PBD::DEBUG::CanvasEvents = PBD::new_debug_bit ("canvasevents"); PBD::DebugBits PBD::DEBUG::CanvasRender = PBD::new_debug_bit ("canvasrender"); PBD::DebugBits PBD::DEBUG::CanvasEnterLeave = PBD::new_debug_bit ("canvasenterleave"); -PBD::DebugBits PBD::DEBUG::WaveView = PBD::new_debug_bit ("waveview"); struct timeval ArdourCanvas::epoch; map ArdourCanvas::last_time; diff --git a/libs/canvas/fill.cc b/libs/canvas/fill.cc index 9fe1d5b4e7..32af6bd74b 100644 --- a/libs/canvas/fill.cc +++ b/libs/canvas/fill.cc @@ -19,8 +19,6 @@ #include -#include "ardour/utils.h" - #include "pbd/compose.h" #include "pbd/convert.h" diff --git a/libs/canvas/item.cc b/libs/canvas/item.cc index 98fe6362db..c941e8e980 100644 --- a/libs/canvas/item.cc +++ b/libs/canvas/item.cc @@ -21,8 +21,6 @@ #include "pbd/demangle.h" #include "pbd/convert.h" -#include "ardour/utils.h" - #include "canvas/canvas.h" #include "canvas/debug.h" #include "canvas/item.h" diff --git a/libs/canvas/outline.cc b/libs/canvas/outline.cc index 4f26de693b..fcb7b75d40 100644 --- a/libs/canvas/outline.cc +++ b/libs/canvas/outline.cc @@ -22,8 +22,6 @@ #include "pbd/compose.h" #include "pbd/convert.h" -#include "ardour/utils.h" - #include "canvas/item.h" #include "canvas/outline.h" #include "canvas/debug.h" diff --git a/libs/canvas/test/wave_view.cc b/libs/canvas/test/wave_view.cc deleted file mode 100644 index de7118b90c..0000000000 --- a/libs/canvas/test/wave_view.cc +++ /dev/null @@ -1,164 +0,0 @@ -#include -#include "pbd/textreceiver.h" -#include "gtkmm2ext/utils.h" -#include "midi++/manager.h" -#include "ardour/session.h" -#include "ardour/audioengine.h" -#include "ardour/source_factory.h" -#include "ardour/audiosource.h" -#include "ardour/audiofilesource.h" -#include "ardour/region_factory.h" -#include "ardour/audioregion.h" -#include "canvas/wave_view.h" -#include "canvas/canvas.h" -#include "wave_view.h" - -using namespace std; -using namespace PBD; -using namespace ARDOUR; -using namespace ArdourCanvas; - -CPPUNIT_TEST_SUITE_REGISTRATION (WaveViewTest); - -TextReceiver text_receiver ("test"); - -void -WaveViewTest::setUp () -{ - init (false, true); - Gtkmm2ext::init (); - SessionEvent::create_per_thread_pool ("test", 512); - - Gtk::Main kit (); - Gtk::Main::init_gtkmm_internals (); - - text_receiver.listen_to (error); - text_receiver.listen_to (info); - text_receiver.listen_to (fatal); - text_receiver.listen_to (warning); - - AudioFileSource::set_build_peakfiles (true); - AudioFileSource::set_build_missing_peakfiles (true); - - AudioEngine engine ("test", ""); - MIDI::Manager::create (engine.jack ()); - CPPUNIT_ASSERT (engine.start () == 0); - - Session session (engine, "tmp_session", "tmp_session"); - engine.set_session (&session); - - char buf[256]; - getcwd (buf, sizeof (buf)); - string const path = string_compose ("%1/../../libs/canvas/test/sine.wav", buf); - - boost::shared_ptr source = SourceFactory::createReadable ( - DataType::AUDIO, session, path, 0, (Source::Flag) 0, false, true - ); - - boost::shared_ptr audio_file_source = boost::dynamic_pointer_cast (source); - - audio_file_source->setup_peakfile (); - - PBD::PropertyList properties; - properties.add (Properties::position, 128); - properties.add (Properties::length, audio_file_source->readable_length ()); - _region = RegionFactory::create (source, properties, false); - _audio_region = boost::dynamic_pointer_cast (_region); -} - -void -WaveViewTest::make_canvas () -{ - /* this leaks various things, but hey ho */ - - _canvas = new ImageCanvas (Duple (256, 256)); - _wave_view = new WaveView (_canvas->root(), _audio_region); - _wave_view->set_frames_per_pixel ((double) (44100 / 1000) / 64); - _wave_view->set_height (64); -} - -void -WaveViewTest::all () -{ - /* XXX: we run these all from the same method so that the setUp code only - gets called once; there are various singletons etc. in Ardour which don't - like being recreated. - */ - - render_all_at_once (); - render_in_pieces (); - cache (); -} - -void -WaveViewTest::render_all_at_once () -{ - make_canvas (); - - _canvas->render_to_image (Rect (0, 0, 256, 256)); - _canvas->write_to_png ("waveview_1.png"); - - /* XXX: doesn't check the result! */ -} - -void -WaveViewTest::render_in_pieces () -{ - make_canvas (); - - cout << "\n\n--------------> PIECES\n"; - _canvas->render_to_image (Rect (0, 0, 128, 256)); - _canvas->render_to_image (Rect (128, 0, 256, 256)); - _canvas->write_to_png ("waveview_2.png"); - cout << "\n\n<-------------- PIECES\n"; - - /* XXX: doesn't check the result! */ -} - -void -WaveViewTest::cache () -{ - make_canvas (); - - /* Whole of the render area needs caching from scratch */ - - _wave_view->invalidate_whole_cache (); - - Rect whole (0, 0, 256, 256); - _canvas->render_to_image (whole); - - CPPUNIT_ASSERT (_wave_view->_cache.size() == 1); - CPPUNIT_ASSERT (_wave_view->_cache.front()->start() == 0); - CPPUNIT_ASSERT (_wave_view->_cache.front()->end() == 256); - - _wave_view->invalidate_whole_cache (); - - /* Render a bit in the middle */ - - Rect part1 (128, 0, 196, 256); - _canvas->render_to_image (part1); - - CPPUNIT_ASSERT (_wave_view->_cache.size() == 1); - CPPUNIT_ASSERT (_wave_view->_cache.front()->start() == 128); - CPPUNIT_ASSERT (_wave_view->_cache.front()->end() == 196); - - /* Now render the whole thing and check that the cache sorts itself out */ - - _canvas->render_to_image (whole); - - CPPUNIT_ASSERT (_wave_view->_cache.size() == 3); - - list::iterator i = _wave_view->_cache.begin (); - - CPPUNIT_ASSERT ((*i)->start() == 0); - CPPUNIT_ASSERT ((*i)->end() == 128); - ++i; - - CPPUNIT_ASSERT ((*i)->start() == 128); - CPPUNIT_ASSERT ((*i)->end() == 196); - ++i; - - CPPUNIT_ASSERT ((*i)->start() == 196); - CPPUNIT_ASSERT ((*i)->end() == 256); - ++i; -} diff --git a/libs/canvas/test/wave_view.h b/libs/canvas/test/wave_view.h deleted file mode 100644 index 69f7ce772f..0000000000 --- a/libs/canvas/test/wave_view.h +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include - -class WaveViewTest : public CppUnit::TestFixture -{ - CPPUNIT_TEST_SUITE (WaveViewTest); - CPPUNIT_TEST (all); - CPPUNIT_TEST_SUITE_END (); - -public: - void setUp (); - void all (); - -private: - void make_canvas (); - void render_all_at_once (); - void render_in_pieces (); - void cache (); - - ArdourCanvas::ImageCanvas* _canvas; - ArdourCanvas::WaveView* _wave_view; - boost::shared_ptr _region; - boost::shared_ptr _audio_region; -}; - - diff --git a/libs/canvas/wave_view.cc b/libs/canvas/wave_view.cc deleted file mode 100644 index 8024b2655d..0000000000 --- a/libs/canvas/wave_view.cc +++ /dev/null @@ -1,1426 +0,0 @@ -/* - Copyright (C) 2011-2013 Paul Davis - Copyright (C) 2017 Tim Mayberry - Author: Carl Hetherington - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#include - -#include - -#include - -#include - -#include "gtkmm2ext/utils.h" -#include "gtkmm2ext/gui_thread.h" - -#include "pbd/base_ui.h" -#include "pbd/compose.h" -#include "pbd/convert.h" -#include "pbd/signals.h" -#include "pbd/stacktrace.h" - -#include "ardour/types.h" -#include "ardour/dB.h" -#include "ardour/lmath.h" -#include "ardour/audioregion.h" -#include "ardour/audiosource.h" -#include "ardour/session.h" - -#include "canvas/canvas.h" -#include "gtkmm2ext/colors.h" -#include "canvas/debug.h" -#include "canvas/wave_view.h" -#include "canvas/wave_view_private.h" - -#include - -#include "gtkmm2ext/gui_thread.h" - -using namespace std; -using namespace ARDOUR; -using namespace PBD; -using namespace ArdourCanvas; -using namespace Gtkmm2ext; - -double WaveView::_global_gradient_depth = 0.6; -bool WaveView::_global_logscaled = false; -WaveView::Shape WaveView::_global_shape = WaveView::Normal; -bool WaveView::_global_show_waveform_clipping = true; -double WaveView::_global_clip_level = 0.98853; - -PBD::Signal0 WaveView::VisualPropertiesChanged; -PBD::Signal0 WaveView::ClipLevelChanged; - -/* NO_THREAD_WAVEVIEWS is defined by the top level wscript - * if --no-threaded-waveviws is provided at the configure step. - */ - -#ifndef NO_THREADED_WAVEVIEWS -#define ENABLE_THREADED_WAVEFORM_RENDERING -#endif - -WaveView::WaveView (Canvas* c, boost::shared_ptr region) - : Item (c) - , _region (region) - , _props (new WaveViewProperties (region)) - , _shape_independent (false) - , _logscaled_independent (false) - , _gradient_depth_independent (false) - , _draw_image_in_gui_thread (false) - , _always_draw_image_in_gui_thread (false) -{ - init (); -} - -WaveView::WaveView (Item* parent, boost::shared_ptr region) - : Item (parent) - , _region (region) - , _props (new WaveViewProperties (region)) - , _shape_independent (false) - , _logscaled_independent (false) - , _gradient_depth_independent (false) - , _draw_image_in_gui_thread (false) - , _always_draw_image_in_gui_thread (false) -{ - init (); -} - -void -WaveView::init () -{ -#ifdef ENABLE_THREADED_WAVEFORM_RENDERING - WaveViewThreads::initialize (); -#endif - - _props->fill_color = _fill_color; - _props->outline_color = _outline_color; - - VisualPropertiesChanged.connect_same_thread ( - invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this)); - ClipLevelChanged.connect_same_thread (invalidation_connection, - boost::bind (&WaveView::handle_clip_level_change, this)); -} - -WaveView::~WaveView () -{ -#ifdef ENABLE_THREADED_WAVEFORM_RENDERING - WaveViewThreads::deinitialize (); -#endif - - reset_cache_group (); -} - -string -WaveView::debug_name() const -{ - return _region->name () + string (":") + PBD::to_string (_props->channel + 1); -} - -void -WaveView::set_always_get_image_in_thread (bool yn) -{ - _always_draw_image_in_gui_thread = yn; -} - -void -WaveView::handle_visual_property_change () -{ - bool changed = false; - - if (!_shape_independent && (_props->shape != global_shape())) { - _props->shape = global_shape(); - changed = true; - } - - if (!_logscaled_independent && (_props->logscaled != global_logscaled())) { - _props->logscaled = global_logscaled(); - changed = true; - } - - if (!_gradient_depth_independent && (_props->gradient_depth != global_gradient_depth())) { - _props->gradient_depth = global_gradient_depth(); - changed = true; - } - - if (changed) { - begin_visual_change (); - end_visual_change (); - } -} - -void -WaveView::handle_clip_level_change () -{ - begin_visual_change (); - end_visual_change (); -} - -void -WaveView::set_fill_color (Color c) -{ - if (c != _fill_color) { - begin_visual_change (); - Fill::set_fill_color (c); - _props->fill_color = _fill_color; // ugh - end_visual_change (); - } -} - -void -WaveView::set_outline_color (Color c) -{ - if (c != _outline_color) { - begin_visual_change (); - Outline::set_outline_color (c); - _props->outline_color = c; - end_visual_change (); - } -} - -void -WaveView::set_samples_per_pixel (double samples_per_pixel) -{ - if (_props->samples_per_pixel != samples_per_pixel) { - begin_change (); - - _props->samples_per_pixel = samples_per_pixel; - _bounding_box_dirty = true; - - end_change (); - } -} - -static inline float -_log_meter (float power, double lower_db, double upper_db, double non_linearity) -{ - return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity)); -} - -static inline float -alt_log_meter (float power) -{ - return _log_meter (power, -192.0, 0.0, 8.0); -} - -void -WaveView::set_clip_level (double dB) -{ - const double clip_level = dB_to_coefficient (dB); - if (_global_clip_level != clip_level) { - _global_clip_level = clip_level; - ClipLevelChanged (); - } -} - -boost::shared_ptr -WaveView::create_draw_request (WaveViewProperties const& props) const -{ - assert (props.is_valid()); - - boost::shared_ptr request (new WaveViewDrawRequest); - - request->image = boost::shared_ptr (new WaveViewImage (_region, props)); - return request; -} - -void -WaveView::prepare_for_render (Rect const& area) const -{ - if (draw_image_in_gui_thread()) { - // Drawing image in GUI thread in WaveView::render - return; - } - - Rect draw_rect; - Rect self_rect; - - // all in window coordinate space - if (!get_item_and_draw_rect_in_window_coords (area, self_rect, draw_rect)) { - return; - } - - double const image_start_pixel_offset = draw_rect.x0 - self_rect.x0; - double const image_end_pixel_offset = draw_rect.x1 - self_rect.x0; - - WaveViewProperties required_props = *_props; - - required_props.set_sample_positions_from_pixel_offsets (image_start_pixel_offset, - image_end_pixel_offset); - - if (!required_props.is_valid ()) { - return; - } - - if (_image) { - if (_image->props.is_equivalent (required_props)) { - return; - } else { - // Image does not contain sample area required - } - } - - boost::shared_ptr request = create_draw_request (required_props); - - queue_draw_request (request); -} - -bool -WaveView::get_item_and_draw_rect_in_window_coords (Rect const& canvas_rect, Rect& item_rect, - Rect& draw_rect) const -{ - /* a WaveView is intimately connected to an AudioRegion. It will - * display the waveform within the region, anywhere from the start of - * the region to its end. - * - * the area we've been asked to render may overlap with area covered - * by the region in any of the normal ways: - * - * - it may begin and end within the area covered by the region - * - it may start before and end after the area covered by region - * - it may start before and end within the area covered by the region - * - it may start within and end after the area covered by the region - * - it may be precisely coincident with the area covered by region. - * - * So let's start by determining the area covered by the region, in - * window coordinates. It begins at zero (in item coordinates for this - * waveview, and extends to region_length() / _samples_per_pixel. - */ - - double const width = region_length() / _props->samples_per_pixel; - item_rect = item_to_window (Rect (0.0, 0.0, width, _props->height)); - - /* Now lets get the intersection with the area we've been asked to draw */ - - draw_rect = item_rect.intersection (canvas_rect); - - if (!draw_rect) { - // No intersection with drawing area - return false; - } - - /* draw_rect now defines the rectangle we need to update/render the waveview - * into, in window coordinate space. - * - * We round down in case we were asked to draw "between" pixels at the start - * and/or end. - */ - draw_rect.x0 = floor (draw_rect.x0); - draw_rect.x1 = floor (draw_rect.x1); - - return true; -} - -void -WaveView::queue_draw_request (boost::shared_ptr const& request) const -{ - // Don't enqueue any requests without a thread to dequeue them. - assert (WaveViewThreads::enabled()); - - if (!request || !request->is_valid()) { - return; - } - - if (current_request) { - current_request->cancel (); - } - - boost::shared_ptr cached_image = - get_cache_group ()->lookup_image (request->image->props); - - if (cached_image) { - // The image may not be finished at this point but that is fine, great in - // fact as it means it should only need to be drawn once. - request->image = cached_image; - current_request = request; - } else { - // now we can finally set an optimal image now that we are not using the - // properties for comparisons. - request->image->props.set_width_samples (optimal_image_width_samples ()); - - current_request = request; - - // Add it to the cache so that other WaveViews can refer to the same image - get_cache_group()->add_image (current_request->image); - - WaveViewThreads::enqueue_draw_request (current_request); - } -} - -void -WaveView::compute_tips (ARDOUR::PeakData const& peak, WaveView::LineTips& tips, - double const effective_height) -{ - /* remember: canvas (and cairo) coordinate space puts the origin at the upper left. - - So, a sample value of 1.0 (0dbFS) will be computed as: - - (1.0 - 1.0) * 0.5 * effective_height - - which evaluates to 0, or the top of the image. - - A sample value of -1.0 will be computed as - - (1.0 + 1.0) * 0.5 * effective height - - which evaluates to effective height, or the bottom of the image. - */ - - const double pmax = (1.0 - peak.max) * 0.5 * effective_height; - const double pmin = (1.0 - peak.min) * 0.5 * effective_height; - - /* remember that the bottom of the image (pmin) has larger y-coordinates - than the top (pmax). - */ - - double spread = (pmin - pmax) * 0.5; - - /* find the nearest pixel to the nominal center. */ - const double center = round (pmin - spread); - - if (spread < 1.0) { - /* minimum distance between line ends is 1 pixel, and we want it "centered" on a pixel, - as per cairo single-pixel line issues. - - NOTE: the caller will not draw a line between these two points if the spread is - less than 2 pixels. So only the tips.top value matters, which is where we will - draw a single pixel as part of the outline. - */ - tips.top = center; - tips.bot = center + 1.0; - } else { - /* round spread above and below center to an integer number of pixels */ - spread = round (spread); - /* top and bottom are located equally either side of the center */ - tips.top = center - spread; - tips.bot = center + spread; - } - - tips.top = min (effective_height, max (0.0, tips.top)); - tips.bot = min (effective_height, max (0.0, tips.bot)); -} - - -Coord -WaveView::y_extent (double s, Shape const shape, double const height) -{ - assert (shape == Rectified); - return floor ((1.0 - s) * height); -} - -void -WaveView::draw_absent_image (Cairo::RefPtr& image, PeakData* peaks, int n_peaks) -{ - const double height = image->get_height(); - - Cairo::RefPtr stripe = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); - - Cairo::RefPtr stripe_context = Cairo::Context::create (stripe); - stripe_context->set_antialias (Cairo::ANTIALIAS_NONE); - - uint32_t stripe_separation = 150; - double start = - floor (height / stripe_separation) * stripe_separation; - int stripe_x = 0; - - while (start < n_peaks) { - - stripe_context->move_to (start, 0); - stripe_x = start + height; - stripe_context->line_to (stripe_x, height); - start += stripe_separation; - } - - stripe_context->set_source_rgba (1.0, 1.0, 1.0, 1.0); - stripe_context->set_line_cap (Cairo::LINE_CAP_SQUARE); - stripe_context->set_line_width(50); - stripe_context->stroke(); - - Cairo::RefPtr context = Cairo::Context::create (image); - - context->set_source_rgba (1.0, 1.0, 0.0, 0.3); - context->mask (stripe, 0, 0); - context->fill (); -} - -struct ImageSet { - Cairo::RefPtr wave; - Cairo::RefPtr outline; - Cairo::RefPtr clip; - Cairo::RefPtr zero; - - ImageSet() : - wave (0), outline (0), clip (0), zero (0) {} -}; - -void -WaveView::draw_image (Cairo::RefPtr& image, PeakData* peaks, int n_peaks, - boost::shared_ptr req) -{ - const double height = image->get_height(); - - ImageSet images; - - images.wave = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); - images.outline = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); - images.clip = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); - images.zero = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); - - Cairo::RefPtr wave_context = Cairo::Context::create (images.wave); - Cairo::RefPtr outline_context = Cairo::Context::create (images.outline); - Cairo::RefPtr clip_context = Cairo::Context::create (images.clip); - Cairo::RefPtr zero_context = Cairo::Context::create (images.zero); - wave_context->set_antialias (Cairo::ANTIALIAS_NONE); - outline_context->set_antialias (Cairo::ANTIALIAS_NONE); - clip_context->set_antialias (Cairo::ANTIALIAS_NONE); - zero_context->set_antialias (Cairo::ANTIALIAS_NONE); - - boost::scoped_array tips (new LineTips[n_peaks]); - - /* Clip level nominally set to -0.9dBFS to account for inter-sample - interpolation possibly clipping (value may be too low). - - We adjust by the region's own gain (but note: not by any gain - automation or its gain envelope) so that clip indicators are closer - to providing data about on-disk data. This multiplication is - needed because the data we get from AudioRegion::read_peaks() - has been scaled by scale_amplitude() already. - */ - - const double clip_level = _global_clip_level * req->image->props.amplitude; - - const Shape shape = req->image->props.shape; - const bool logscaled = req->image->props.logscaled; - - if (req->image->props.shape == WaveView::Rectified) { - - /* each peak is a line from the bottom of the waveview - * to a point determined by max (peaks[i].max, - * peaks[i].min) - */ - - if (logscaled) { - for (int i = 0; i < n_peaks; ++i) { - - tips[i].bot = height - 1.0; - const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (peaks[i].max), fabs (peaks[i].min)))); - tips[i].top = y_extent (p, shape, height); - tips[i].spread = p * height; - - if (peaks[i].max >= clip_level) { - tips[i].clip_max = true; - } - - if (-(peaks[i].min) >= clip_level) { - tips[i].clip_min = true; - } - } - - } else { - for (int i = 0; i < n_peaks; ++i) { - - tips[i].bot = height - 1.0; - const double p = max(fabs (peaks[i].max), fabs (peaks[i].min)); - tips[i].top = y_extent (p, shape, height); - tips[i].spread = p * height; - if (p >= clip_level) { - tips[i].clip_max = true; - } - } - - } - - } else { - - if (logscaled) { - for (int i = 0; i < n_peaks; ++i) { - PeakData p; - p.max = peaks[i].max; - p.min = peaks[i].min; - - if (peaks[i].max >= clip_level) { - tips[i].clip_max = true; - } - if (-(peaks[i].min) >= clip_level) { - tips[i].clip_min = true; - } - - if (p.max > 0.0) { - p.max = alt_log_meter (fast_coefficient_to_dB (p.max)); - } else if (p.max < 0.0) { - p.max =-alt_log_meter (fast_coefficient_to_dB (-p.max)); - } else { - p.max = 0.0; - } - - if (p.min > 0.0) { - p.min = alt_log_meter (fast_coefficient_to_dB (p.min)); - } else if (p.min < 0.0) { - p.min = -alt_log_meter (fast_coefficient_to_dB (-p.min)); - } else { - p.min = 0.0; - } - - compute_tips (p, tips[i], height); - tips[i].spread = tips[i].bot - tips[i].top; - } - - } else { - for (int i = 0; i < n_peaks; ++i) { - if (peaks[i].max >= clip_level) { - tips[i].clip_max = true; - } - if (-(peaks[i].min) >= clip_level) { - tips[i].clip_min = true; - } - - compute_tips (peaks[i], tips[i], height); - tips[i].spread = tips[i].bot - tips[i].top; - } - - } - } - - if (req->stopped()) { - return; - } - - Color alpha_one = rgba_to_color (0, 0, 0, 1.0); - - set_source_rgba (wave_context, alpha_one); - set_source_rgba (outline_context, alpha_one); - set_source_rgba (clip_context, alpha_one); - set_source_rgba (zero_context, alpha_one); - - /* ensure single-pixel lines */ - - wave_context->set_line_width (1.0); - wave_context->translate (0.5, 0.5); - - outline_context->set_line_width (1.0); - outline_context->translate (0.5, 0.5); - - clip_context->set_line_width (1.0); - clip_context->translate (0.5, 0.5); - - zero_context->set_line_width (1.0); - zero_context->translate (0.5, 0.5); - - /* the height of the clip-indicator should be at most 7 pixels, - * or 5% of the height of the waveview item. - */ - - const double clip_height = min (7.0, ceil (height * 0.05)); - - /* There are 3 possible components to draw at each x-axis position: the - waveform "line", the zero line and an outline/clip indicator. We - have to decide which of the 3 to draw at each position, pixel by - pixel. This makes the rendering less efficient but it is the only - way I can see to do this correctly. - - To avoid constant source swapping and stroking, we draw the components separately - onto four alpha only image surfaces for use as a mask. - - With only 1 pixel of spread between the top and bottom of the line, - we just draw the upper outline/clip indicator. - - With 2 pixels of spread, we draw the upper and lower outline clip - indicators. - - With 3 pixels of spread we draw the upper and lower outline/clip - indicators and at least 1 pixel of the waveform line. - - With 5 pixels of spread, we draw all components. - - We can do rectified as two separate passes because we have a much - easier decision regarding whether to draw the waveform line. We - always draw the clip/outline indicators. - */ - - if (shape == WaveView::Rectified) { - - for (int i = 0; i < n_peaks; ++i) { - - /* waveform line */ - - if (tips[i].spread >= 1.0) { - wave_context->move_to (i, tips[i].top); - wave_context->line_to (i, tips[i].bot); - } - - /* clip indicator */ - - if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) { - clip_context->move_to (i, tips[i].top); - /* clip-indicating upper terminal line */ - clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + .5))); - } else { - outline_context->move_to (i, tips[i].top); - /* normal upper terminal dot */ - outline_context->rel_line_to (0, -1.0); - } - } - - wave_context->stroke (); - clip_context->stroke (); - outline_context->stroke (); - - } else { - const int height_zero = floor(height * .5); - - for (int i = 0; i < n_peaks; ++i) { - - /* waveform line */ - - if (tips[i].spread >= 2.0) { - wave_context->move_to (i, tips[i].top); - wave_context->line_to (i, tips[i].bot); - } - - /* draw square waves and other discontiguous points clearly */ - if (i > 0) { - if (tips[i-1].top + 2 < tips[i].top) { - wave_context->move_to (i-1, tips[i-1].top); - wave_context->line_to (i-1, (tips[i].bot + tips[i-1].top)/2); - wave_context->move_to (i, (tips[i].bot + tips[i-1].top)/2); - wave_context->line_to (i, tips[i].top); - } else if (tips[i-1].bot > tips[i].bot + 2) { - wave_context->move_to (i-1, tips[i-1].bot); - wave_context->line_to (i-1, (tips[i].top + tips[i-1].bot)/2); - wave_context->move_to (i, (tips[i].top + tips[i-1].bot)/2); - wave_context->line_to (i, tips[i].bot); - } - } - - /* zero line, show only if there is enough spread - or the waveform line does not cross zero line */ - bool const show_zero_line = req->image->props.show_zero; - - if (show_zero_line && ((tips[i].spread >= 5.0) || (tips[i].top > height_zero ) || (tips[i].bot < height_zero)) ) { - zero_context->move_to (i, height_zero); - zero_context->rel_line_to (1.0, 0); - } - - if (tips[i].spread > 1.0) { - bool clipped = false; - /* outline/clip indicators */ - if (_global_show_waveform_clipping && tips[i].clip_max) { - clip_context->move_to (i, tips[i].top); - /* clip-indicating upper terminal line */ - clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + 0.5))); - clipped = true; - } - - if (_global_show_waveform_clipping && tips[i].clip_min) { - clip_context->move_to (i, tips[i].bot); - /* clip-indicating lower terminal line */ - clip_context->rel_line_to (0, - min (clip_height, ceil(tips[i].spread + 0.5))); - clipped = true; - } - - if (!clipped && tips[i].spread > 2.0) { - /* only draw the outline if the spread - implies 3 or more pixels (so that we see 1 - white pixel in the middle). - */ - outline_context->move_to (i, tips[i].bot); - /* normal lower terminal dot; line moves up */ - outline_context->rel_line_to (0, -1.0); - - outline_context->move_to (i, tips[i].top); - /* normal upper terminal dot, line moves down */ - outline_context->rel_line_to (0, 1.0); - } - } else { - bool clipped = false; - /* outline/clip indicator */ - if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) { - clip_context->move_to (i, tips[i].top); - /* clip-indicating upper / lower terminal line */ - clip_context->rel_line_to (0, 1.0); - clipped = true; - } - - if (!clipped) { - /* special case where only 1 pixel of - * the waveform line is drawn (and - * nothing else). - * - * we draw a 1px "line", pretending - * that the span is 1.0 (whether it is - * zero or 1.0) - */ - wave_context->move_to (i, tips[i].top); - wave_context->rel_line_to (0, 1.0); - } - } - } - - wave_context->stroke (); - outline_context->stroke (); - clip_context->stroke (); - zero_context->stroke (); - } - - if (req->stopped()) { - return; - } - - Cairo::RefPtr context = Cairo::Context::create (image); - - /* Here we set a source colour and use the various components as a mask. */ - - const Color fill_color = req->image->props.fill_color; - const double gradient_depth = req->image->props.gradient_depth; - - if (gradient_depth != 0.0) { - - Cairo::RefPtr gradient (Cairo::LinearGradient::create (0, 0, 0, height)); - - double stops[3]; - - double r, g, b, a; - - - if (shape == Rectified) { - stops[0] = 0.1; - stops[1] = 0.3; - stops[2] = 0.9; - } else { - stops[0] = 0.1; - stops[1] = 0.5; - stops[2] = 0.9; - } - - color_to_rgba (fill_color, r, g, b, a); - gradient->add_color_stop_rgba (stops[1], r, g, b, a); - /* generate a new color for the middle of the gradient */ - double h, s, v; - color_to_hsv (fill_color, h, s, v); - /* change v towards white */ - v *= 1.0 - gradient_depth; - Color center = hsva_to_color (h, s, v, a); - color_to_rgba (center, r, g, b, a); - - gradient->add_color_stop_rgba (stops[0], r, g, b, a); - gradient->add_color_stop_rgba (stops[2], r, g, b, a); - - context->set_source (gradient); - } else { - set_source_rgba (context, fill_color); - } - - if (req->stopped()) { - return; - } - - context->mask (images.wave, 0, 0); - context->fill (); - - set_source_rgba (context, req->image->props.outline_color); - context->mask (images.outline, 0, 0); - context->fill (); - - set_source_rgba (context, req->image->props.clip_color); - context->mask (images.clip, 0, 0); - context->fill (); - - set_source_rgba (context, req->image->props.zero_color); - context->mask (images.zero, 0, 0); - context->fill (); -} - -framecnt_t -WaveView::optimal_image_width_samples () const -{ - /* Compute how wide the image should be in samples. - * - * The resulting image should be wider than the canvas width so that the - * image does not have to be redrawn each time the canvas offset changes, but - * drawing too much unnecessarily, for instance when zooming into the canvas - * the part of the image that is outside of the visible canvas area may never - * be displayed and will just increase apparent render time and reduce - * responsiveness in non-threaded rendering and cause "flashing" waveforms in - * threaded rendering mode. - * - * Another thing to consider is that if there are a number of waveforms on - * the canvas that are the width of the canvas then we don't want to have to - * draw the images for them all at once as it will cause a spike in render - * time, or in threaded rendering mode it will mean all the draw requests will - * the queued during the same frame/expose event. This issue can be - * alleviated by using an element of randomness in selecting the image width. - * - * If the value of samples per pixel is less than 1/10th of a second, use - * 1/10th of a second instead. - */ - - framecnt_t canvas_width_samples = _canvas->visible_area().width() * _props->samples_per_pixel; - const framecnt_t one_tenth_of_second = _region->session().frame_rate() / 10; - - /* If zoomed in where a canvas item interects with the canvas area but - * stretches for many pages either side, to avoid having draw all images when - * the canvas scrolls by a page width the multiplier would have to be a - * randomized amount centered around 3 times the visible canvas width, but - * for other operations like zooming or even with a stationary playhead it is - * a lot of extra drawing that can affect performance. - * - * So without making things too complicated with different widths for - * different operations, try to use a width that is a balance and will work - * well for scrolling(non-page width) so all the images aren't redrawn at the - * same time but also faster for sequential zooming operations. - * - * Canvas items that don't intersect with the edges of the visible canvas - * will of course only draw images that are the pixel width of the item. - * - * It is a perhaps a coincidence that these values are centered roughly - * around the golden ratio but they did work well in my testing. - */ - const double min_multiplier = 1.4; - const double max_multiplier = 1.8; - - /** - * A combination of high resolution screens, high samplerates and high - * zoom levels(1 sample per pixel) can cause 1/10 of a second(in - * pixels) to exceed the cairo image size limit. - */ - const double cairo_image_limit = 32767.0; - const double max_image_width = cairo_image_limit / max_multiplier; - - framecnt_t max_width_samples = floor (max_image_width / _props->samples_per_pixel); - - const framecnt_t one_tenth_of_second_limited = std::min (one_tenth_of_second, max_width_samples); - - framecnt_t new_sample_count = std::max (canvas_width_samples, one_tenth_of_second_limited); - - const double multiplier = g_random_double_range (min_multiplier, max_multiplier); - - return new_sample_count * multiplier; -} - -void -WaveView::set_image (boost::shared_ptr img) const -{ - get_cache_group ()->add_image (img); - _image = img; -} - -void -WaveView::process_draw_request (boost::shared_ptr req) -{ - boost::shared_ptr region = req->image->region.lock(); - - if (!region) { - return; - } - - if (req->stopped()) { - return; - } - - WaveViewProperties const& props = req->image->props; - - const int n_peaks = props.get_width_pixels (); - - assert (n_peaks > 0 && n_peaks < 32767); - - boost::scoped_array peaks (new PeakData[n_peaks]); - - /* Note that Region::read_peaks() takes a start position based on an - offset into the Region's **SOURCE**, rather than an offset into - the Region itself. - */ - - framecnt_t peaks_read = - region->read_peaks (peaks.get (), n_peaks, props.get_sample_start (), - props.get_length_samples (), props.channel, props.samples_per_pixel); - - if (req->stopped()) { - return; - } - - Cairo::RefPtr cairo_image = - Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, req->image->props.height); - - // http://cairographics.org/manual/cairo-Image-Surfaces.html#cairo-image-surface-create - // This function always returns a valid pointer, but it will return a pointer to a "nil" surface.. - // but there's some evidence that req->image can be NULL. - // http://tracker.ardour.org/view.php?id=6478 - assert (cairo_image); - - if (peaks_read > 0) { - - /* region amplitude will have been used to generate the - * peak values already, but not the visual-only - * amplitude_above_axis. So apply that here before - * rendering. - */ - - const double amplitude_above_axis = props.amplitude_above_axis; - - if (amplitude_above_axis != 1.0) { - for (framecnt_t i = 0; i < n_peaks; ++i) { - peaks[i].max *= amplitude_above_axis; - peaks[i].min *= amplitude_above_axis; - } - } - - draw_image (cairo_image, peaks.get(), n_peaks, req); - - } else { - draw_absent_image (cairo_image, peaks.get(), n_peaks); - } - - if (req->stopped ()) { - return; - } - - // Assign now that we are sure all drawing is complete as that is what - // determines whether a request was finished. - req->image->cairo_image = cairo_image; -} - -bool -WaveView::draw_image_in_gui_thread () const -{ - return _draw_image_in_gui_thread || _always_draw_image_in_gui_thread || !rendered () || - !WaveViewThreads::enabled (); -} - -void -WaveView::render (Rect const & area, Cairo::RefPtr context) const -{ - assert (_props->samples_per_pixel != 0); - - if (!_region) { // assert? - return; - } - - Rect draw; - Rect self; - - if (!get_item_and_draw_rect_in_window_coords (area, self, draw)) { - assert(true); - return; - } - - double const image_start_pixel_offset = draw.x0 - self.x0; - double const image_end_pixel_offset = draw.x1 - self.x0; - - if (image_start_pixel_offset == image_end_pixel_offset) { - // this may happen if zoomed very far out with a small region - return; - } - - WaveViewProperties required_props = *_props; - - required_props.set_sample_positions_from_pixel_offsets (image_start_pixel_offset, - image_end_pixel_offset); - - assert (required_props.is_valid()); - - boost::shared_ptr image_to_draw; - - if (current_request) { - if (!current_request->image->props.is_equivalent (required_props)) { - // The WaveView properties may have been updated during recording between - // prepare_for_render and render calls and the new required props have - // different end sample value. - current_request->cancel (); - current_request.reset (); - } else if (current_request->finished ()) { - image_to_draw = current_request->image; - current_request.reset (); - } - } else { - // No current Request - } - - if (!image_to_draw && _image) { - if (_image->props.is_equivalent (required_props)) { - // Image contains required properties - image_to_draw = _image; - } else { - // Image does not contain properties required - } - } - - if (!image_to_draw) { - image_to_draw = get_cache_group ()->lookup_image (required_props); - if (image_to_draw && !image_to_draw->finished ()) { - // Found equivalent but unfinished Image in cache - image_to_draw.reset (); - } - } - - if (!image_to_draw) { - // No existing image to draw - - boost::shared_ptr const request = create_draw_request (required_props); - - if (draw_image_in_gui_thread ()) { - // now that we have to draw something, draw more than required. - request->image->props.set_width_samples (optimal_image_width_samples ()); - - process_draw_request (request); - - image_to_draw = request->image; - - } else if (current_request) { - if (current_request->finished ()) { - // There is a chance the request is now finished since checking above - image_to_draw = current_request->image; - current_request.reset (); - } else if (_canvas->get_microseconds_since_render_start () < 15000) { - current_request->cancel (); - current_request.reset (); - - // Drawing image in GUI thread as we have time - - // now that we have to draw something, draw more than required. - request->image->props.set_width_samples (optimal_image_width_samples ()); - - process_draw_request (request); - - image_to_draw = request->image; - } else { - // Waiting for current request to finish - redraw (); - return; - } - } else { - // Defer the rendering to another thread or perhaps render pass if - // a thread cannot generate it in time. - queue_draw_request (request); - redraw (); - return; - } - } - - /* reset this so that future missing images can be generated in a worker thread. */ - _draw_image_in_gui_thread = false; - - assert (image_to_draw); - - /* compute the first pixel of the image that should be used when we - * render the specified range. - */ - - double image_origin_in_self_coordinates = - (image_to_draw->props.get_sample_start () - _props->region_start) / _props->samples_per_pixel; - - /* the image may only be a best-effort ... it may not span the entire - * range requested, though it is guaranteed to cover the start. So - * determine how many pixels we can actually draw. - */ - - const double draw_start_pixel = draw.x0; - const double draw_end_pixel = draw.x1; - - double draw_width_pixels = draw_end_pixel - draw_start_pixel; - - if (image_to_draw != _image) { - - /* the image is guaranteed to start at or before - * draw_start. But if it starts before draw_start, that reduces - * the maximum available width we can render with. - * - * so .. clamp the draw width to the smaller of what we need to - * draw or the available width of the image. - */ - draw_width_pixels = min ((double)image_to_draw->cairo_image->get_width (), draw_width_pixels); - - set_image (image_to_draw); - } - - context->rectangle (draw_start_pixel, draw.y0, draw_width_pixels, draw.height()); - - /* round image origin position to an exact pixel in device space to - * avoid blurring - */ - - double x = self.x0 + image_origin_in_self_coordinates; - double y = self.y0; - context->user_to_device (x, y); - x = round (x); - y = round (y); - context->device_to_user (x, y); - - /* the coordinates specify where in "user coordinates" (i.e. what we - * generally call "canvas coordinates" in this code) the image origin - * will appear. So specifying (10,10) will put the upper left corner of - * the image at (10,10) in user space. - */ - - context->set_source (image_to_draw->cairo_image, x, y); - context->fill (); -} - -void -WaveView::compute_bounding_box () const -{ - if (_region) { - _bounding_box = Rect (0.0, 0.0, region_length() / _props->samples_per_pixel, _props->height); - } else { - _bounding_box = Rect (); - } - - _bounding_box_dirty = false; -} - -void -WaveView::set_height (Distance height) -{ - if (_props->height != height) { - begin_change (); - - _props->height = height; - _draw_image_in_gui_thread = true; - - _bounding_box_dirty = true; - end_change (); - } -} - -void -WaveView::set_channel (int channel) -{ - if (_props->channel != channel) { - begin_change (); - _props->channel = channel; - reset_cache_group (); - _bounding_box_dirty = true; - end_change (); - } -} - -void -WaveView::set_logscaled (bool yn) -{ - if (_props->logscaled != yn) { - begin_visual_change (); - _props->logscaled = yn; - end_visual_change (); - } -} - -void -WaveView::set_gradient_depth (double) -{ - // TODO ?? -} - -double -WaveView::gradient_depth () const -{ - return _props->gradient_depth; -} - -void -WaveView::gain_changed () -{ - begin_visual_change (); - _props->amplitude = _region->scale_amplitude (); - _draw_image_in_gui_thread = true; - end_visual_change (); -} - -void -WaveView::set_zero_color (Color c) -{ - if (_props->zero_color != c) { - begin_visual_change (); - _props->zero_color = c; - end_visual_change (); - } -} - -void -WaveView::set_clip_color (Color c) -{ - if (_props->clip_color != c) { - begin_visual_change (); - _props->clip_color = c; - end_visual_change (); - } -} - -void -WaveView::set_show_zero_line (bool yn) -{ - if (_props->show_zero != yn) { - begin_visual_change (); - _props->show_zero = yn; - end_visual_change (); - } -} - -bool -WaveView::show_zero_line () const -{ - return _props->show_zero; -} - -void -WaveView::set_shape (Shape s) -{ - if (_props->shape != s) { - begin_visual_change (); - _props->shape = s; - end_visual_change (); - } -} - -void -WaveView::set_amplitude_above_axis (double a) -{ - if (fabs (_props->amplitude_above_axis - a) > 0.01) { - begin_visual_change (); - _props->amplitude_above_axis = a; - _draw_image_in_gui_thread = true; - end_visual_change (); - } -} - -double -WaveView::amplitude_above_axis () const -{ - return _props->amplitude_above_axis; -} - -void -WaveView::set_global_shape (Shape s) -{ - if (_global_shape != s) { - _global_shape = s; - WaveViewCache::get_instance()->clear_cache (); - VisualPropertiesChanged (); /* EMIT SIGNAL */ - } -} - -void -WaveView::set_global_logscaled (bool yn) -{ - if (_global_logscaled != yn) { - _global_logscaled = yn; - WaveViewCache::get_instance()->clear_cache (); - VisualPropertiesChanged (); /* EMIT SIGNAL */ - } -} - -framecnt_t -WaveView::region_length() const -{ - return _region->length() - (_props->region_start - _region->start()); -} - -framepos_t -WaveView::region_end() const -{ - return _props->region_start + region_length(); -} - -void -WaveView::set_region_start (frameoffset_t start) -{ - if (!_region) { - return; - } - - if (_props->region_start == start) { - return; - } - - begin_change (); - _props->region_start = start; - _bounding_box_dirty = true; - end_change (); -} - -void -WaveView::region_resized () -{ - /* Called when the region start or end (thus length) has changed. - */ - - if (!_region) { - return; - } - - begin_change (); - _props->region_start = _region->start(); - _props->region_end = _region->start() + _region->length(); - _bounding_box_dirty = true; - end_change (); -} - -void -WaveView::set_global_gradient_depth (double depth) -{ - if (_global_gradient_depth != depth) { - _global_gradient_depth = depth; - VisualPropertiesChanged (); /* EMIT SIGNAL */ - } -} - -void -WaveView::set_global_show_waveform_clipping (bool yn) -{ - if (_global_show_waveform_clipping != yn) { - _global_show_waveform_clipping = yn; - ClipLevelChanged (); - } -} - -void -WaveView::set_start_shift (double pixels) -{ - if (pixels < 0) { - return; - } - - begin_visual_change (); - //_start_shift = pixels; - end_visual_change (); -} - -void -WaveView::set_image_cache_size (uint64_t sz) -{ - WaveViewCache::get_instance()->set_image_cache_threshold (sz); -} - -boost::shared_ptr -WaveView::get_cache_group () const -{ - if (_cache_group) { - return _cache_group; - } - - boost::shared_ptr source = _region->audio_source (_props->channel); - assert (source); - - _cache_group = WaveViewCache::get_instance ()->get_cache_group (source); - - return _cache_group; -} - -void -WaveView::reset_cache_group () -{ - WaveViewCache::get_instance()->reset_cache_group (_cache_group); -} diff --git a/libs/canvas/wave_view_private.cc b/libs/canvas/wave_view_private.cc deleted file mode 100644 index a788eb9c6f..0000000000 --- a/libs/canvas/wave_view_private.cc +++ /dev/null @@ -1,458 +0,0 @@ -/* - Copyright (C) 2017 Tim Mayberry - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ - -#include -#include "ardour/lmath.h" - -#include "canvas/wave_view_private.h" - -#include "pbd/cpus.h" - -#include "ardour/audioregion.h" -#include "ardour/audiosource.h" - -namespace ArdourCanvas { - -WaveViewProperties::WaveViewProperties (boost::shared_ptr region) - : region_start (region->start ()) - , region_end (region->start () + region->length ()) - , channel (0) - , height (64) - , samples_per_pixel (0) - , amplitude (region->scale_amplitude ()) - , amplitude_above_axis (1.0) - , fill_color (0x000000ff) - , outline_color (0xff0000ff) - , zero_color (0xff0000ff) - , clip_color (0xff0000ff) - , show_zero (false) - , logscaled (WaveView::global_logscaled()) - , shape (WaveView::global_shape()) - , gradient_depth (WaveView::global_gradient_depth ()) - , start_shift (0.0) // currently unused - , sample_start (0) - , sample_end (0) -{ - -} - -/*-------------------------------------------------*/ - -WaveViewImage::WaveViewImage (boost::shared_ptr const& region_ptr, - WaveViewProperties const& properties) - : region (region_ptr) - , props (properties) - , timestamp (0) -{ - -} - -WaveViewImage::~WaveViewImage () -{ - -} - -/*-------------------------------------------------*/ - -WaveViewCacheGroup::WaveViewCacheGroup (WaveViewCache& parent_cache) - : _parent_cache (parent_cache) -{ - -} - -WaveViewCacheGroup::~WaveViewCacheGroup () -{ - clear_cache (); -} - -void -WaveViewCacheGroup::add_image (boost::shared_ptr image) -{ - if (!image) { - // Not adding invalid image to cache - return; - } - - ImageCache::iterator oldest_image_it = _cached_images.begin(); - ImageCache::iterator second_oldest_image_it = _cached_images.end(); - - for (ImageCache::iterator it = _cached_images.begin (); it != _cached_images.end (); ++it) { - if ((*it) == image) { - // Must never be more than one instance of the image in the cache - (*it)->timestamp = g_get_monotonic_time (); - return; - } else if ((*it)->props.is_equivalent (image->props)) { - // Equivalent Image already in cache, updating timestamp - (*it)->timestamp = g_get_monotonic_time (); - return; - } - - if ((*it)->timestamp < (*oldest_image_it)->timestamp) { - second_oldest_image_it = oldest_image_it; - oldest_image_it = it; - } - } - - // no duplicate or equivalent image so we are definitely adding it to cache - image->timestamp = g_get_monotonic_time (); - - if (_parent_cache.full () || full ()) { - if (oldest_image_it != _cached_images.end()) { - // Replacing oldest Image in cache - _parent_cache.decrease_size ((*oldest_image_it)->size_in_bytes ()); - *oldest_image_it = image; - _parent_cache.increase_size (image->size_in_bytes ()); - - if (second_oldest_image_it != _cached_images.end ()) { - // Removing second oldest Image in cache - _parent_cache.decrease_size ((*second_oldest_image_it)->size_in_bytes ()); - _cached_images.erase (second_oldest_image_it); - } - return; - } else { - /** - * Add the image to the cache even if the threshold is exceeded so that - * new WaveViews can still cache images with a full cache, the size of - * the cache will quickly equalize back to the threshold as new images - * are added and the size of the cache is reduced. - */ - } - } - - _cached_images.push_back (image); - _parent_cache.increase_size (image->size_in_bytes ()); -} - -boost::shared_ptr -WaveViewCacheGroup::lookup_image (WaveViewProperties const& props) -{ - for (ImageCache::iterator i = _cached_images.begin (); i != _cached_images.end (); ++i) { - if ((*i)->props.is_equivalent (props)) { - return (*i); - } - } - return boost::shared_ptr(); -} - -void -WaveViewCacheGroup::clear_cache () -{ - // Tell the parent cache about the images we are about to drop references to - for (ImageCache::iterator it = _cached_images.begin (); it != _cached_images.end (); ++it) { - _parent_cache.decrease_size ((*it)->size_in_bytes ()); - } - _cached_images.clear (); -} - -/*-------------------------------------------------*/ - -WaveViewCache::WaveViewCache () - : image_cache_size (0) - , _image_cache_threshold (100 * 1048576) /* bytes */ -{ - -} - -WaveViewCache::~WaveViewCache () -{ -} - -WaveViewCache* -WaveViewCache::get_instance () -{ - static WaveViewCache* instance = new WaveViewCache; - return instance; -} - -void -WaveViewCache::increase_size (uint64_t bytes) -{ - image_cache_size += bytes; -} - -void -WaveViewCache::decrease_size (uint64_t bytes) -{ - assert (image_cache_size - bytes < image_cache_size); - image_cache_size -= bytes; -} - -boost::shared_ptr -WaveViewCache::get_cache_group (boost::shared_ptr source) -{ - CacheGroups::iterator it = cache_group_map.find (source); - - if (it != cache_group_map.end()) { - // Found existing CacheGroup for AudioSource - return it->second; - } - - boost::shared_ptr new_group (new WaveViewCacheGroup (*this)); - - bool inserted = cache_group_map.insert (std::make_pair (source, new_group)).second; - - assert (inserted); - - return new_group; -} - -void -WaveViewCache::reset_cache_group (boost::shared_ptr& group) -{ - if (!group) { - return; - } - - CacheGroups::iterator it = cache_group_map.begin(); - - while (it != cache_group_map.end()) { - if (it->second == group) { - break; - } - ++it; - } - - assert (it != cache_group_map.end ()); - - group.reset(); - - if (it->second.unique()) { - cache_group_map.erase (it); - } -} - -void -WaveViewCache::clear_cache () -{ - for (CacheGroups::iterator it = cache_group_map.begin (); it != cache_group_map.end (); ++it) { - (*it).second->clear_cache (); - } -} - -void -WaveViewCache::set_image_cache_threshold (uint64_t sz) -{ - _image_cache_threshold = sz; -} - -/*-------------------------------------------------*/ - -WaveViewDrawRequest::WaveViewDrawRequest () : stop (0) -{ - -} - -WaveViewDrawRequest::~WaveViewDrawRequest () -{ - -} - -void -WaveViewDrawRequestQueue::enqueue (boost::shared_ptr& request) -{ - Glib::Threads::Mutex::Lock lm (_queue_mutex); - - _queue.push_back (request); - _cond.broadcast (); -} - -void -WaveViewDrawRequestQueue::wake_up () -{ - boost::shared_ptr null_ptr; - // hack!?...wake up the drawing thread - enqueue (null_ptr); -} - -boost::shared_ptr -WaveViewDrawRequestQueue::dequeue (bool block) -{ - if (block) { - _queue_mutex.lock(); - } else { - if (!_queue_mutex.trylock()) { - return boost::shared_ptr(); - } - } - - // _queue_mutex is always held at this point - - if (_queue.empty()) { - if (block) { - _cond.wait (_queue_mutex); - } else { - _queue_mutex.unlock(); - return boost::shared_ptr(); - } - } - - boost::shared_ptr req; - - if (!_queue.empty()) { - req = _queue.front (); - _queue.pop_front (); - } else { - // Queue empty, returning empty DrawRequest - } - - _queue_mutex.unlock(); - - return req; -} - -/*-------------------------------------------------*/ - -WaveViewThreads::WaveViewThreads () -{ - -} - -WaveViewThreads::~WaveViewThreads () -{ - -} - -uint32_t WaveViewThreads::init_count = 0; - -WaveViewThreads* WaveViewThreads::instance = 0; - -void -WaveViewThreads::initialize () -{ - // no need for atomics as only called from GUI thread - if (++init_count == 1) { - assert(!instance); - instance = new WaveViewThreads; - instance->start_threads(); - } -} - -void -WaveViewThreads::deinitialize () -{ - if (--init_count == 0) { - instance->stop_threads(); - delete instance; - instance = 0; - } -} - -void -WaveViewThreads::enqueue_draw_request (boost::shared_ptr& request) -{ - assert (instance); - instance->_request_queue.enqueue (request); -} - -boost::shared_ptr -WaveViewThreads::dequeue_draw_request () -{ - assert (instance); - return instance->_request_queue.dequeue (true); -} - -void -WaveViewThreads::wake_up () -{ - assert (instance); - return instance->_request_queue.wake_up (); -} - -void -WaveViewThreads::start_threads () -{ - assert (!_threads.size()); - - int num_cpus = hardware_concurrency (); - - uint32_t num_threads = std::max (1, num_cpus - 1); - - for (uint32_t i = 0; i != num_threads; ++i) { - boost::shared_ptr new_thread (new WaveViewDrawingThread ()); - _threads.push_back(new_thread); - } -} - -void -WaveViewThreads::stop_threads () -{ - assert (_threads.size()); - - _threads.clear (); -} - -/*-------------------------------------------------*/ - -WaveViewDrawingThread::WaveViewDrawingThread () - : _thread(0) - , _quit(0) -{ - start (); -} - -WaveViewDrawingThread::~WaveViewDrawingThread () -{ - quit (); -} - -void -WaveViewDrawingThread::start () -{ - assert (!_thread); - - _thread = Glib::Threads::Thread::create (sigc::mem_fun (*this, &WaveViewDrawingThread::run)); -} - -void -WaveViewDrawingThread::quit () -{ - assert (_thread); - - g_atomic_int_set (&_quit, 1); - WaveViewThreads::wake_up (); - _thread->join(); - _thread = 0; -} - -void -WaveViewDrawingThread::run () -{ - while (true) { - - if (g_atomic_int_get (&_quit)) { - break; - } - - // block until a request is available. - boost::shared_ptr req = WaveViewThreads::dequeue_draw_request (); - - if (req && !req->stopped()) { - try { - WaveView::process_draw_request (req); - } catch (...) { - /* just in case it was set before the exception, whatever it was */ - req->image->cairo_image.clear (); - } - } else { - // null or stopped Request, processing skipped - } - } -} - -} // namespace ArdourCanvas diff --git a/libs/canvas/wscript b/libs/canvas/wscript index 0d99e6856d..d92695d6fd 100644 --- a/libs/canvas/wscript +++ b/libs/canvas/wscript @@ -60,8 +60,6 @@ canvas_sources = [ 'tracking_text.cc', 'types.cc', 'utils.cc', - 'wave_view.cc', - 'wave_view_private.cc', 'widget.cc', 'xfade_curve.cc', ] @@ -88,7 +86,7 @@ def build(bld): obj.export_includes = ['.'] obj.includes = ['.'] obj.uselib = 'SIGCPP CAIROMM GTKMM BOOST XML' - obj.use = [ 'libpbd', 'libevoral', 'libardour', 'libgtkmm2ext', 'libevoral' ] + obj.use = [ 'libpbd', 'libgtkmm2ext' ] obj.name = 'libcanvas' obj.target = 'canvas' obj.vnum = CANVAS_LIB_VERSION @@ -113,7 +111,7 @@ def build(bld): unit_testobj.includes = obj.includes + ['test', '../pbd'] unit_testobj.uselib = 'CPPUNIT SIGCPP CAIROMM GTKMM' - unit_testobj.uselib_local = 'libcanvas libevoral libardour libgtkmm2ext' + unit_testobj.uselib_local = 'libcanvas libgtkmm2ext' unit_testobj.name = 'libcanvas-unit-tests' unit_testobj.target = 'run-tests' unit_testobj.install_path = '' @@ -138,7 +136,7 @@ def build(bld): manual_testobj.source = t manual_testobj.includes = obj.includes + ['test', '../pbd'] manual_testobj.uselib = 'CPPUNIT SIGCPP CAIROMM GTKMM' - manual_testobj.uselib_local = 'libcanvas libevoral libardour libgtkmm2ext' + manual_testobj.uselib_local = 'libcanvas libgtkmm2ext' manual_testobj.name = 'libcanvas-manual-test-%s' % name manual_testobj.target = target manual_testobj.install_path = '' @@ -157,7 +155,7 @@ def build(bld): manual_testobj.source = [ t, 'benchmark/benchmark.cc' ] manual_testobj.includes = obj.includes + ['test', '../pbd'] manual_testobj.uselib = 'CPPUNIT SIGCPP CAIROMM GTKMM' - manual_testobj.uselib_local = 'libcanvas libevoral libardour libgtkmm2ext' + manual_testobj.uselib_local = 'libcanvas libgtkmm2ext' manual_testobj.name = 'libcanvas-benchmark-%s' % name manual_testobj.target = target manual_testobj.install_path = '' diff --git a/libs/waveview/debug.cc b/libs/waveview/debug.cc new file mode 100644 index 0000000000..c0a897e8f2 --- /dev/null +++ b/libs/waveview/debug.cc @@ -0,0 +1,23 @@ +/* + Copyright (C) 2011 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include "pbd/debug.h" +#include "waveview/debug.h" + +PBD::DebugBits PBD::DEBUG::WaveView = PBD::new_debug_bit ("waveview"); diff --git a/libs/waveview/wave_view.cc b/libs/waveview/wave_view.cc new file mode 100644 index 0000000000..80292ce53d --- /dev/null +++ b/libs/waveview/wave_view.cc @@ -0,0 +1,1425 @@ +/* + Copyright (C) 2011-2013 Paul Davis + Copyright (C) 2017 Tim Mayberry + Author: Carl Hetherington + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include + +#include + +#include + +#include +#include + +#include "pbd/base_ui.h" +#include "pbd/compose.h" +#include "pbd/convert.h" +#include "pbd/signals.h" +#include "pbd/stacktrace.h" + +#include "ardour/types.h" +#include "ardour/dB.h" +#include "ardour/lmath.h" +#include "ardour/audioregion.h" +#include "ardour/audiosource.h" +#include "ardour/session.h" + +#include "gtkmm2ext/colors.h" +#include "gtkmm2ext/gui_thread.h" +#include "gtkmm2ext/utils.h" + +#include "canvas/canvas.h" +#include "canvas/debug.h" + +#include "waveview/wave_view.h" +#include "waveview/wave_view_private.h" + +using namespace std; +using namespace PBD; +using namespace ARDOUR; +using namespace Gtkmm2ext; +using namespace ArdourCanvas; +using namespace ArdourWaveView; + +double WaveView::_global_gradient_depth = 0.6; +bool WaveView::_global_logscaled = false; +WaveView::Shape WaveView::_global_shape = WaveView::Normal; +bool WaveView::_global_show_waveform_clipping = true; +double WaveView::_global_clip_level = 0.98853; + +PBD::Signal0 WaveView::VisualPropertiesChanged; +PBD::Signal0 WaveView::ClipLevelChanged; + +/* NO_THREAD_WAVEVIEWS is defined by the top level wscript + * if --no-threaded-waveviws is provided at the configure step. + */ + +#ifndef NO_THREADED_WAVEVIEWS +#define ENABLE_THREADED_WAVEFORM_RENDERING +#endif + +WaveView::WaveView (Canvas* c, boost::shared_ptr region) + : Item (c) + , _region (region) + , _props (new WaveViewProperties (region)) + , _shape_independent (false) + , _logscaled_independent (false) + , _gradient_depth_independent (false) + , _draw_image_in_gui_thread (false) + , _always_draw_image_in_gui_thread (false) +{ + init (); +} + +WaveView::WaveView (Item* parent, boost::shared_ptr region) + : Item (parent) + , _region (region) + , _props (new WaveViewProperties (region)) + , _shape_independent (false) + , _logscaled_independent (false) + , _gradient_depth_independent (false) + , _draw_image_in_gui_thread (false) + , _always_draw_image_in_gui_thread (false) +{ + init (); +} + +void +WaveView::init () +{ +#ifdef ENABLE_THREADED_WAVEFORM_RENDERING + WaveViewThreads::initialize (); +#endif + + _props->fill_color = _fill_color; + _props->outline_color = _outline_color; + + VisualPropertiesChanged.connect_same_thread ( + invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this)); + ClipLevelChanged.connect_same_thread (invalidation_connection, + boost::bind (&WaveView::handle_clip_level_change, this)); +} + +WaveView::~WaveView () +{ +#ifdef ENABLE_THREADED_WAVEFORM_RENDERING + WaveViewThreads::deinitialize (); +#endif + + reset_cache_group (); +} + +string +WaveView::debug_name() const +{ + return _region->name () + string (":") + PBD::to_string (_props->channel + 1); +} + +void +WaveView::set_always_get_image_in_thread (bool yn) +{ + _always_draw_image_in_gui_thread = yn; +} + +void +WaveView::handle_visual_property_change () +{ + bool changed = false; + + if (!_shape_independent && (_props->shape != global_shape())) { + _props->shape = global_shape(); + changed = true; + } + + if (!_logscaled_independent && (_props->logscaled != global_logscaled())) { + _props->logscaled = global_logscaled(); + changed = true; + } + + if (!_gradient_depth_independent && (_props->gradient_depth != global_gradient_depth())) { + _props->gradient_depth = global_gradient_depth(); + changed = true; + } + + if (changed) { + begin_visual_change (); + end_visual_change (); + } +} + +void +WaveView::handle_clip_level_change () +{ + begin_visual_change (); + end_visual_change (); +} + +void +WaveView::set_fill_color (Color c) +{ + if (c != _fill_color) { + begin_visual_change (); + Fill::set_fill_color (c); + _props->fill_color = _fill_color; // ugh + end_visual_change (); + } +} + +void +WaveView::set_outline_color (Color c) +{ + if (c != _outline_color) { + begin_visual_change (); + Outline::set_outline_color (c); + _props->outline_color = c; + end_visual_change (); + } +} + +void +WaveView::set_samples_per_pixel (double samples_per_pixel) +{ + if (_props->samples_per_pixel != samples_per_pixel) { + begin_change (); + + _props->samples_per_pixel = samples_per_pixel; + _bounding_box_dirty = true; + + end_change (); + } +} + +static inline float +_log_meter (float power, double lower_db, double upper_db, double non_linearity) +{ + return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity)); +} + +static inline float +alt_log_meter (float power) +{ + return _log_meter (power, -192.0, 0.0, 8.0); +} + +void +WaveView::set_clip_level (double dB) +{ + const double clip_level = dB_to_coefficient (dB); + if (_global_clip_level != clip_level) { + _global_clip_level = clip_level; + ClipLevelChanged (); + } +} + +boost::shared_ptr +WaveView::create_draw_request (WaveViewProperties const& props) const +{ + assert (props.is_valid()); + + boost::shared_ptr request (new WaveViewDrawRequest); + + request->image = boost::shared_ptr (new WaveViewImage (_region, props)); + return request; +} + +void +WaveView::prepare_for_render (Rect const& area) const +{ + if (draw_image_in_gui_thread()) { + // Drawing image in GUI thread in WaveView::render + return; + } + + Rect draw_rect; + Rect self_rect; + + // all in window coordinate space + if (!get_item_and_draw_rect_in_window_coords (area, self_rect, draw_rect)) { + return; + } + + double const image_start_pixel_offset = draw_rect.x0 - self_rect.x0; + double const image_end_pixel_offset = draw_rect.x1 - self_rect.x0; + + WaveViewProperties required_props = *_props; + + required_props.set_sample_positions_from_pixel_offsets (image_start_pixel_offset, + image_end_pixel_offset); + + if (!required_props.is_valid ()) { + return; + } + + if (_image) { + if (_image->props.is_equivalent (required_props)) { + return; + } else { + // Image does not contain sample area required + } + } + + boost::shared_ptr request = create_draw_request (required_props); + + queue_draw_request (request); +} + +bool +WaveView::get_item_and_draw_rect_in_window_coords (Rect const& canvas_rect, Rect& item_rect, + Rect& draw_rect) const +{ + /* a WaveView is intimately connected to an AudioRegion. It will + * display the waveform within the region, anywhere from the start of + * the region to its end. + * + * the area we've been asked to render may overlap with area covered + * by the region in any of the normal ways: + * + * - it may begin and end within the area covered by the region + * - it may start before and end after the area covered by region + * - it may start before and end within the area covered by the region + * - it may start within and end after the area covered by the region + * - it may be precisely coincident with the area covered by region. + * + * So let's start by determining the area covered by the region, in + * window coordinates. It begins at zero (in item coordinates for this + * waveview, and extends to region_length() / _samples_per_pixel. + */ + + double const width = region_length() / _props->samples_per_pixel; + item_rect = item_to_window (Rect (0.0, 0.0, width, _props->height)); + + /* Now lets get the intersection with the area we've been asked to draw */ + + draw_rect = item_rect.intersection (canvas_rect); + + if (!draw_rect) { + // No intersection with drawing area + return false; + } + + /* draw_rect now defines the rectangle we need to update/render the waveview + * into, in window coordinate space. + * + * We round down in case we were asked to draw "between" pixels at the start + * and/or end. + */ + draw_rect.x0 = floor (draw_rect.x0); + draw_rect.x1 = floor (draw_rect.x1); + + return true; +} + +void +WaveView::queue_draw_request (boost::shared_ptr const& request) const +{ + // Don't enqueue any requests without a thread to dequeue them. + assert (WaveViewThreads::enabled()); + + if (!request || !request->is_valid()) { + return; + } + + if (current_request) { + current_request->cancel (); + } + + boost::shared_ptr cached_image = + get_cache_group ()->lookup_image (request->image->props); + + if (cached_image) { + // The image may not be finished at this point but that is fine, great in + // fact as it means it should only need to be drawn once. + request->image = cached_image; + current_request = request; + } else { + // now we can finally set an optimal image now that we are not using the + // properties for comparisons. + request->image->props.set_width_samples (optimal_image_width_samples ()); + + current_request = request; + + // Add it to the cache so that other WaveViews can refer to the same image + get_cache_group()->add_image (current_request->image); + + WaveViewThreads::enqueue_draw_request (current_request); + } +} + +void +WaveView::compute_tips (ARDOUR::PeakData const& peak, WaveView::LineTips& tips, + double const effective_height) +{ + /* remember: canvas (and cairo) coordinate space puts the origin at the upper left. + + So, a sample value of 1.0 (0dbFS) will be computed as: + + (1.0 - 1.0) * 0.5 * effective_height + + which evaluates to 0, or the top of the image. + + A sample value of -1.0 will be computed as + + (1.0 + 1.0) * 0.5 * effective height + + which evaluates to effective height, or the bottom of the image. + */ + + const double pmax = (1.0 - peak.max) * 0.5 * effective_height; + const double pmin = (1.0 - peak.min) * 0.5 * effective_height; + + /* remember that the bottom of the image (pmin) has larger y-coordinates + than the top (pmax). + */ + + double spread = (pmin - pmax) * 0.5; + + /* find the nearest pixel to the nominal center. */ + const double center = round (pmin - spread); + + if (spread < 1.0) { + /* minimum distance between line ends is 1 pixel, and we want it "centered" on a pixel, + as per cairo single-pixel line issues. + + NOTE: the caller will not draw a line between these two points if the spread is + less than 2 pixels. So only the tips.top value matters, which is where we will + draw a single pixel as part of the outline. + */ + tips.top = center; + tips.bot = center + 1.0; + } else { + /* round spread above and below center to an integer number of pixels */ + spread = round (spread); + /* top and bottom are located equally either side of the center */ + tips.top = center - spread; + tips.bot = center + spread; + } + + tips.top = min (effective_height, max (0.0, tips.top)); + tips.bot = min (effective_height, max (0.0, tips.bot)); +} + + +Coord +WaveView::y_extent (double s, Shape const shape, double const height) +{ + assert (shape == Rectified); + return floor ((1.0 - s) * height); +} + +void +WaveView::draw_absent_image (Cairo::RefPtr& image, PeakData* peaks, int n_peaks) +{ + const double height = image->get_height(); + + Cairo::RefPtr stripe = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); + + Cairo::RefPtr stripe_context = Cairo::Context::create (stripe); + stripe_context->set_antialias (Cairo::ANTIALIAS_NONE); + + uint32_t stripe_separation = 150; + double start = - floor (height / stripe_separation) * stripe_separation; + int stripe_x = 0; + + while (start < n_peaks) { + + stripe_context->move_to (start, 0); + stripe_x = start + height; + stripe_context->line_to (stripe_x, height); + start += stripe_separation; + } + + stripe_context->set_source_rgba (1.0, 1.0, 1.0, 1.0); + stripe_context->set_line_cap (Cairo::LINE_CAP_SQUARE); + stripe_context->set_line_width(50); + stripe_context->stroke(); + + Cairo::RefPtr context = Cairo::Context::create (image); + + context->set_source_rgba (1.0, 1.0, 0.0, 0.3); + context->mask (stripe, 0, 0); + context->fill (); +} + +struct ImageSet { + Cairo::RefPtr wave; + Cairo::RefPtr outline; + Cairo::RefPtr clip; + Cairo::RefPtr zero; + + ImageSet() : + wave (0), outline (0), clip (0), zero (0) {} +}; + +void +WaveView::draw_image (Cairo::RefPtr& image, PeakData* peaks, int n_peaks, + boost::shared_ptr req) +{ + const double height = image->get_height(); + + ImageSet images; + + images.wave = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); + images.outline = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); + images.clip = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); + images.zero = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, height); + + Cairo::RefPtr wave_context = Cairo::Context::create (images.wave); + Cairo::RefPtr outline_context = Cairo::Context::create (images.outline); + Cairo::RefPtr clip_context = Cairo::Context::create (images.clip); + Cairo::RefPtr zero_context = Cairo::Context::create (images.zero); + wave_context->set_antialias (Cairo::ANTIALIAS_NONE); + outline_context->set_antialias (Cairo::ANTIALIAS_NONE); + clip_context->set_antialias (Cairo::ANTIALIAS_NONE); + zero_context->set_antialias (Cairo::ANTIALIAS_NONE); + + boost::scoped_array tips (new LineTips[n_peaks]); + + /* Clip level nominally set to -0.9dBFS to account for inter-sample + interpolation possibly clipping (value may be too low). + + We adjust by the region's own gain (but note: not by any gain + automation or its gain envelope) so that clip indicators are closer + to providing data about on-disk data. This multiplication is + needed because the data we get from AudioRegion::read_peaks() + has been scaled by scale_amplitude() already. + */ + + const double clip_level = _global_clip_level * req->image->props.amplitude; + + const Shape shape = req->image->props.shape; + const bool logscaled = req->image->props.logscaled; + + if (req->image->props.shape == WaveView::Rectified) { + + /* each peak is a line from the bottom of the waveview + * to a point determined by max (peaks[i].max, + * peaks[i].min) + */ + + if (logscaled) { + for (int i = 0; i < n_peaks; ++i) { + + tips[i].bot = height - 1.0; + const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (peaks[i].max), fabs (peaks[i].min)))); + tips[i].top = y_extent (p, shape, height); + tips[i].spread = p * height; + + if (peaks[i].max >= clip_level) { + tips[i].clip_max = true; + } + + if (-(peaks[i].min) >= clip_level) { + tips[i].clip_min = true; + } + } + + } else { + for (int i = 0; i < n_peaks; ++i) { + + tips[i].bot = height - 1.0; + const double p = max(fabs (peaks[i].max), fabs (peaks[i].min)); + tips[i].top = y_extent (p, shape, height); + tips[i].spread = p * height; + if (p >= clip_level) { + tips[i].clip_max = true; + } + } + + } + + } else { + + if (logscaled) { + for (int i = 0; i < n_peaks; ++i) { + PeakData p; + p.max = peaks[i].max; + p.min = peaks[i].min; + + if (peaks[i].max >= clip_level) { + tips[i].clip_max = true; + } + if (-(peaks[i].min) >= clip_level) { + tips[i].clip_min = true; + } + + if (p.max > 0.0) { + p.max = alt_log_meter (fast_coefficient_to_dB (p.max)); + } else if (p.max < 0.0) { + p.max =-alt_log_meter (fast_coefficient_to_dB (-p.max)); + } else { + p.max = 0.0; + } + + if (p.min > 0.0) { + p.min = alt_log_meter (fast_coefficient_to_dB (p.min)); + } else if (p.min < 0.0) { + p.min = -alt_log_meter (fast_coefficient_to_dB (-p.min)); + } else { + p.min = 0.0; + } + + compute_tips (p, tips[i], height); + tips[i].spread = tips[i].bot - tips[i].top; + } + + } else { + for (int i = 0; i < n_peaks; ++i) { + if (peaks[i].max >= clip_level) { + tips[i].clip_max = true; + } + if (-(peaks[i].min) >= clip_level) { + tips[i].clip_min = true; + } + + compute_tips (peaks[i], tips[i], height); + tips[i].spread = tips[i].bot - tips[i].top; + } + + } + } + + if (req->stopped()) { + return; + } + + Color alpha_one = rgba_to_color (0, 0, 0, 1.0); + + set_source_rgba (wave_context, alpha_one); + set_source_rgba (outline_context, alpha_one); + set_source_rgba (clip_context, alpha_one); + set_source_rgba (zero_context, alpha_one); + + /* ensure single-pixel lines */ + + wave_context->set_line_width (1.0); + wave_context->translate (0.5, 0.5); + + outline_context->set_line_width (1.0); + outline_context->translate (0.5, 0.5); + + clip_context->set_line_width (1.0); + clip_context->translate (0.5, 0.5); + + zero_context->set_line_width (1.0); + zero_context->translate (0.5, 0.5); + + /* the height of the clip-indicator should be at most 7 pixels, + * or 5% of the height of the waveview item. + */ + + const double clip_height = min (7.0, ceil (height * 0.05)); + + /* There are 3 possible components to draw at each x-axis position: the + waveform "line", the zero line and an outline/clip indicator. We + have to decide which of the 3 to draw at each position, pixel by + pixel. This makes the rendering less efficient but it is the only + way I can see to do this correctly. + + To avoid constant source swapping and stroking, we draw the components separately + onto four alpha only image surfaces for use as a mask. + + With only 1 pixel of spread between the top and bottom of the line, + we just draw the upper outline/clip indicator. + + With 2 pixels of spread, we draw the upper and lower outline clip + indicators. + + With 3 pixels of spread we draw the upper and lower outline/clip + indicators and at least 1 pixel of the waveform line. + + With 5 pixels of spread, we draw all components. + + We can do rectified as two separate passes because we have a much + easier decision regarding whether to draw the waveform line. We + always draw the clip/outline indicators. + */ + + if (shape == WaveView::Rectified) { + + for (int i = 0; i < n_peaks; ++i) { + + /* waveform line */ + + if (tips[i].spread >= 1.0) { + wave_context->move_to (i, tips[i].top); + wave_context->line_to (i, tips[i].bot); + } + + /* clip indicator */ + + if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) { + clip_context->move_to (i, tips[i].top); + /* clip-indicating upper terminal line */ + clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + .5))); + } else { + outline_context->move_to (i, tips[i].top); + /* normal upper terminal dot */ + outline_context->rel_line_to (0, -1.0); + } + } + + wave_context->stroke (); + clip_context->stroke (); + outline_context->stroke (); + + } else { + const int height_zero = floor(height * .5); + + for (int i = 0; i < n_peaks; ++i) { + + /* waveform line */ + + if (tips[i].spread >= 2.0) { + wave_context->move_to (i, tips[i].top); + wave_context->line_to (i, tips[i].bot); + } + + /* draw square waves and other discontiguous points clearly */ + if (i > 0) { + if (tips[i-1].top + 2 < tips[i].top) { + wave_context->move_to (i-1, tips[i-1].top); + wave_context->line_to (i-1, (tips[i].bot + tips[i-1].top)/2); + wave_context->move_to (i, (tips[i].bot + tips[i-1].top)/2); + wave_context->line_to (i, tips[i].top); + } else if (tips[i-1].bot > tips[i].bot + 2) { + wave_context->move_to (i-1, tips[i-1].bot); + wave_context->line_to (i-1, (tips[i].top + tips[i-1].bot)/2); + wave_context->move_to (i, (tips[i].top + tips[i-1].bot)/2); + wave_context->line_to (i, tips[i].bot); + } + } + + /* zero line, show only if there is enough spread + or the waveform line does not cross zero line */ + bool const show_zero_line = req->image->props.show_zero; + + if (show_zero_line && ((tips[i].spread >= 5.0) || (tips[i].top > height_zero ) || (tips[i].bot < height_zero)) ) { + zero_context->move_to (i, height_zero); + zero_context->rel_line_to (1.0, 0); + } + + if (tips[i].spread > 1.0) { + bool clipped = false; + /* outline/clip indicators */ + if (_global_show_waveform_clipping && tips[i].clip_max) { + clip_context->move_to (i, tips[i].top); + /* clip-indicating upper terminal line */ + clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + 0.5))); + clipped = true; + } + + if (_global_show_waveform_clipping && tips[i].clip_min) { + clip_context->move_to (i, tips[i].bot); + /* clip-indicating lower terminal line */ + clip_context->rel_line_to (0, - min (clip_height, ceil(tips[i].spread + 0.5))); + clipped = true; + } + + if (!clipped && tips[i].spread > 2.0) { + /* only draw the outline if the spread + implies 3 or more pixels (so that we see 1 + white pixel in the middle). + */ + outline_context->move_to (i, tips[i].bot); + /* normal lower terminal dot; line moves up */ + outline_context->rel_line_to (0, -1.0); + + outline_context->move_to (i, tips[i].top); + /* normal upper terminal dot, line moves down */ + outline_context->rel_line_to (0, 1.0); + } + } else { + bool clipped = false; + /* outline/clip indicator */ + if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) { + clip_context->move_to (i, tips[i].top); + /* clip-indicating upper / lower terminal line */ + clip_context->rel_line_to (0, 1.0); + clipped = true; + } + + if (!clipped) { + /* special case where only 1 pixel of + * the waveform line is drawn (and + * nothing else). + * + * we draw a 1px "line", pretending + * that the span is 1.0 (whether it is + * zero or 1.0) + */ + wave_context->move_to (i, tips[i].top); + wave_context->rel_line_to (0, 1.0); + } + } + } + + wave_context->stroke (); + outline_context->stroke (); + clip_context->stroke (); + zero_context->stroke (); + } + + if (req->stopped()) { + return; + } + + Cairo::RefPtr context = Cairo::Context::create (image); + + /* Here we set a source colour and use the various components as a mask. */ + + const Color fill_color = req->image->props.fill_color; + const double gradient_depth = req->image->props.gradient_depth; + + if (gradient_depth != 0.0) { + + Cairo::RefPtr gradient (Cairo::LinearGradient::create (0, 0, 0, height)); + + double stops[3]; + + double r, g, b, a; + + + if (shape == Rectified) { + stops[0] = 0.1; + stops[1] = 0.3; + stops[2] = 0.9; + } else { + stops[0] = 0.1; + stops[1] = 0.5; + stops[2] = 0.9; + } + + color_to_rgba (fill_color, r, g, b, a); + gradient->add_color_stop_rgba (stops[1], r, g, b, a); + /* generate a new color for the middle of the gradient */ + double h, s, v; + color_to_hsv (fill_color, h, s, v); + /* change v towards white */ + v *= 1.0 - gradient_depth; + Color center = hsva_to_color (h, s, v, a); + color_to_rgba (center, r, g, b, a); + + gradient->add_color_stop_rgba (stops[0], r, g, b, a); + gradient->add_color_stop_rgba (stops[2], r, g, b, a); + + context->set_source (gradient); + } else { + set_source_rgba (context, fill_color); + } + + if (req->stopped()) { + return; + } + + context->mask (images.wave, 0, 0); + context->fill (); + + set_source_rgba (context, req->image->props.outline_color); + context->mask (images.outline, 0, 0); + context->fill (); + + set_source_rgba (context, req->image->props.clip_color); + context->mask (images.clip, 0, 0); + context->fill (); + + set_source_rgba (context, req->image->props.zero_color); + context->mask (images.zero, 0, 0); + context->fill (); +} + +framecnt_t +WaveView::optimal_image_width_samples () const +{ + /* Compute how wide the image should be in samples. + * + * The resulting image should be wider than the canvas width so that the + * image does not have to be redrawn each time the canvas offset changes, but + * drawing too much unnecessarily, for instance when zooming into the canvas + * the part of the image that is outside of the visible canvas area may never + * be displayed and will just increase apparent render time and reduce + * responsiveness in non-threaded rendering and cause "flashing" waveforms in + * threaded rendering mode. + * + * Another thing to consider is that if there are a number of waveforms on + * the canvas that are the width of the canvas then we don't want to have to + * draw the images for them all at once as it will cause a spike in render + * time, or in threaded rendering mode it will mean all the draw requests will + * the queued during the same frame/expose event. This issue can be + * alleviated by using an element of randomness in selecting the image width. + * + * If the value of samples per pixel is less than 1/10th of a second, use + * 1/10th of a second instead. + */ + + framecnt_t canvas_width_samples = _canvas->visible_area().width() * _props->samples_per_pixel; + const framecnt_t one_tenth_of_second = _region->session().frame_rate() / 10; + + /* If zoomed in where a canvas item interects with the canvas area but + * stretches for many pages either side, to avoid having draw all images when + * the canvas scrolls by a page width the multiplier would have to be a + * randomized amount centered around 3 times the visible canvas width, but + * for other operations like zooming or even with a stationary playhead it is + * a lot of extra drawing that can affect performance. + * + * So without making things too complicated with different widths for + * different operations, try to use a width that is a balance and will work + * well for scrolling(non-page width) so all the images aren't redrawn at the + * same time but also faster for sequential zooming operations. + * + * Canvas items that don't intersect with the edges of the visible canvas + * will of course only draw images that are the pixel width of the item. + * + * It is a perhaps a coincidence that these values are centered roughly + * around the golden ratio but they did work well in my testing. + */ + const double min_multiplier = 1.4; + const double max_multiplier = 1.8; + + /** + * A combination of high resolution screens, high samplerates and high + * zoom levels(1 sample per pixel) can cause 1/10 of a second(in + * pixels) to exceed the cairo image size limit. + */ + const double cairo_image_limit = 32767.0; + const double max_image_width = cairo_image_limit / max_multiplier; + + framecnt_t max_width_samples = floor (max_image_width / _props->samples_per_pixel); + + const framecnt_t one_tenth_of_second_limited = std::min (one_tenth_of_second, max_width_samples); + + framecnt_t new_sample_count = std::max (canvas_width_samples, one_tenth_of_second_limited); + + const double multiplier = g_random_double_range (min_multiplier, max_multiplier); + + return new_sample_count * multiplier; +} + +void +WaveView::set_image (boost::shared_ptr img) const +{ + get_cache_group ()->add_image (img); + _image = img; +} + +void +WaveView::process_draw_request (boost::shared_ptr req) +{ + boost::shared_ptr region = req->image->region.lock(); + + if (!region) { + return; + } + + if (req->stopped()) { + return; + } + + WaveViewProperties const& props = req->image->props; + + const int n_peaks = props.get_width_pixels (); + + assert (n_peaks > 0 && n_peaks < 32767); + + boost::scoped_array peaks (new PeakData[n_peaks]); + + /* Note that Region::read_peaks() takes a start position based on an + offset into the Region's **SOURCE**, rather than an offset into + the Region itself. + */ + + framecnt_t peaks_read = + region->read_peaks (peaks.get (), n_peaks, props.get_sample_start (), + props.get_length_samples (), props.channel, props.samples_per_pixel); + + if (req->stopped()) { + return; + } + + Cairo::RefPtr cairo_image = + Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, req->image->props.height); + + // http://cairographics.org/manual/cairo-Image-Surfaces.html#cairo-image-surface-create + // This function always returns a valid pointer, but it will return a pointer to a "nil" surface.. + // but there's some evidence that req->image can be NULL. + // http://tracker.ardour.org/view.php?id=6478 + assert (cairo_image); + + if (peaks_read > 0) { + + /* region amplitude will have been used to generate the + * peak values already, but not the visual-only + * amplitude_above_axis. So apply that here before + * rendering. + */ + + const double amplitude_above_axis = props.amplitude_above_axis; + + if (amplitude_above_axis != 1.0) { + for (framecnt_t i = 0; i < n_peaks; ++i) { + peaks[i].max *= amplitude_above_axis; + peaks[i].min *= amplitude_above_axis; + } + } + + draw_image (cairo_image, peaks.get(), n_peaks, req); + + } else { + draw_absent_image (cairo_image, peaks.get(), n_peaks); + } + + if (req->stopped ()) { + return; + } + + // Assign now that we are sure all drawing is complete as that is what + // determines whether a request was finished. + req->image->cairo_image = cairo_image; +} + +bool +WaveView::draw_image_in_gui_thread () const +{ + return _draw_image_in_gui_thread || _always_draw_image_in_gui_thread || !rendered () || + !WaveViewThreads::enabled (); +} + +void +WaveView::render (Rect const & area, Cairo::RefPtr context) const +{ + assert (_props->samples_per_pixel != 0); + + if (!_region) { // assert? + return; + } + + Rect draw; + Rect self; + + if (!get_item_and_draw_rect_in_window_coords (area, self, draw)) { + assert(true); + return; + } + + double const image_start_pixel_offset = draw.x0 - self.x0; + double const image_end_pixel_offset = draw.x1 - self.x0; + + if (image_start_pixel_offset == image_end_pixel_offset) { + // this may happen if zoomed very far out with a small region + return; + } + + WaveViewProperties required_props = *_props; + + required_props.set_sample_positions_from_pixel_offsets (image_start_pixel_offset, + image_end_pixel_offset); + + assert (required_props.is_valid()); + + boost::shared_ptr image_to_draw; + + if (current_request) { + if (!current_request->image->props.is_equivalent (required_props)) { + // The WaveView properties may have been updated during recording between + // prepare_for_render and render calls and the new required props have + // different end sample value. + current_request->cancel (); + current_request.reset (); + } else if (current_request->finished ()) { + image_to_draw = current_request->image; + current_request.reset (); + } + } else { + // No current Request + } + + if (!image_to_draw && _image) { + if (_image->props.is_equivalent (required_props)) { + // Image contains required properties + image_to_draw = _image; + } else { + // Image does not contain properties required + } + } + + if (!image_to_draw) { + image_to_draw = get_cache_group ()->lookup_image (required_props); + if (image_to_draw && !image_to_draw->finished ()) { + // Found equivalent but unfinished Image in cache + image_to_draw.reset (); + } + } + + if (!image_to_draw) { + // No existing image to draw + + boost::shared_ptr const request = create_draw_request (required_props); + + if (draw_image_in_gui_thread ()) { + // now that we have to draw something, draw more than required. + request->image->props.set_width_samples (optimal_image_width_samples ()); + + process_draw_request (request); + + image_to_draw = request->image; + + } else if (current_request) { + if (current_request->finished ()) { + // There is a chance the request is now finished since checking above + image_to_draw = current_request->image; + current_request.reset (); + } else if (_canvas->get_microseconds_since_render_start () < 15000) { + current_request->cancel (); + current_request.reset (); + + // Drawing image in GUI thread as we have time + + // now that we have to draw something, draw more than required. + request->image->props.set_width_samples (optimal_image_width_samples ()); + + process_draw_request (request); + + image_to_draw = request->image; + } else { + // Waiting for current request to finish + redraw (); + return; + } + } else { + // Defer the rendering to another thread or perhaps render pass if + // a thread cannot generate it in time. + queue_draw_request (request); + redraw (); + return; + } + } + + /* reset this so that future missing images can be generated in a worker thread. */ + _draw_image_in_gui_thread = false; + + assert (image_to_draw); + + /* compute the first pixel of the image that should be used when we + * render the specified range. + */ + + double image_origin_in_self_coordinates = + (image_to_draw->props.get_sample_start () - _props->region_start) / _props->samples_per_pixel; + + /* the image may only be a best-effort ... it may not span the entire + * range requested, though it is guaranteed to cover the start. So + * determine how many pixels we can actually draw. + */ + + const double draw_start_pixel = draw.x0; + const double draw_end_pixel = draw.x1; + + double draw_width_pixels = draw_end_pixel - draw_start_pixel; + + if (image_to_draw != _image) { + + /* the image is guaranteed to start at or before + * draw_start. But if it starts before draw_start, that reduces + * the maximum available width we can render with. + * + * so .. clamp the draw width to the smaller of what we need to + * draw or the available width of the image. + */ + draw_width_pixels = min ((double)image_to_draw->cairo_image->get_width (), draw_width_pixels); + + set_image (image_to_draw); + } + + context->rectangle (draw_start_pixel, draw.y0, draw_width_pixels, draw.height()); + + /* round image origin position to an exact pixel in device space to + * avoid blurring + */ + + double x = self.x0 + image_origin_in_self_coordinates; + double y = self.y0; + context->user_to_device (x, y); + x = round (x); + y = round (y); + context->device_to_user (x, y); + + /* the coordinates specify where in "user coordinates" (i.e. what we + * generally call "canvas coordinates" in this code) the image origin + * will appear. So specifying (10,10) will put the upper left corner of + * the image at (10,10) in user space. + */ + + context->set_source (image_to_draw->cairo_image, x, y); + context->fill (); +} + +void +WaveView::compute_bounding_box () const +{ + if (_region) { + _bounding_box = Rect (0.0, 0.0, region_length() / _props->samples_per_pixel, _props->height); + } else { + _bounding_box = Rect (); + } + + _bounding_box_dirty = false; +} + +void +WaveView::set_height (Distance height) +{ + if (_props->height != height) { + begin_change (); + + _props->height = height; + _draw_image_in_gui_thread = true; + + _bounding_box_dirty = true; + end_change (); + } +} + +void +WaveView::set_channel (int channel) +{ + if (_props->channel != channel) { + begin_change (); + _props->channel = channel; + reset_cache_group (); + _bounding_box_dirty = true; + end_change (); + } +} + +void +WaveView::set_logscaled (bool yn) +{ + if (_props->logscaled != yn) { + begin_visual_change (); + _props->logscaled = yn; + end_visual_change (); + } +} + +void +WaveView::set_gradient_depth (double) +{ + // TODO ?? +} + +double +WaveView::gradient_depth () const +{ + return _props->gradient_depth; +} + +void +WaveView::gain_changed () +{ + begin_visual_change (); + _props->amplitude = _region->scale_amplitude (); + _draw_image_in_gui_thread = true; + end_visual_change (); +} + +void +WaveView::set_zero_color (Color c) +{ + if (_props->zero_color != c) { + begin_visual_change (); + _props->zero_color = c; + end_visual_change (); + } +} + +void +WaveView::set_clip_color (Color c) +{ + if (_props->clip_color != c) { + begin_visual_change (); + _props->clip_color = c; + end_visual_change (); + } +} + +void +WaveView::set_show_zero_line (bool yn) +{ + if (_props->show_zero != yn) { + begin_visual_change (); + _props->show_zero = yn; + end_visual_change (); + } +} + +bool +WaveView::show_zero_line () const +{ + return _props->show_zero; +} + +void +WaveView::set_shape (Shape s) +{ + if (_props->shape != s) { + begin_visual_change (); + _props->shape = s; + end_visual_change (); + } +} + +void +WaveView::set_amplitude_above_axis (double a) +{ + if (fabs (_props->amplitude_above_axis - a) > 0.01) { + begin_visual_change (); + _props->amplitude_above_axis = a; + _draw_image_in_gui_thread = true; + end_visual_change (); + } +} + +double +WaveView::amplitude_above_axis () const +{ + return _props->amplitude_above_axis; +} + +void +WaveView::set_global_shape (Shape s) +{ + if (_global_shape != s) { + _global_shape = s; + WaveViewCache::get_instance()->clear_cache (); + VisualPropertiesChanged (); /* EMIT SIGNAL */ + } +} + +void +WaveView::set_global_logscaled (bool yn) +{ + if (_global_logscaled != yn) { + _global_logscaled = yn; + WaveViewCache::get_instance()->clear_cache (); + VisualPropertiesChanged (); /* EMIT SIGNAL */ + } +} + +framecnt_t +WaveView::region_length() const +{ + return _region->length() - (_props->region_start - _region->start()); +} + +framepos_t +WaveView::region_end() const +{ + return _props->region_start + region_length(); +} + +void +WaveView::set_region_start (frameoffset_t start) +{ + if (!_region) { + return; + } + + if (_props->region_start == start) { + return; + } + + begin_change (); + _props->region_start = start; + _bounding_box_dirty = true; + end_change (); +} + +void +WaveView::region_resized () +{ + /* Called when the region start or end (thus length) has changed. + */ + + if (!_region) { + return; + } + + begin_change (); + _props->region_start = _region->start(); + _props->region_end = _region->start() + _region->length(); + _bounding_box_dirty = true; + end_change (); +} + +void +WaveView::set_global_gradient_depth (double depth) +{ + if (_global_gradient_depth != depth) { + _global_gradient_depth = depth; + VisualPropertiesChanged (); /* EMIT SIGNAL */ + } +} + +void +WaveView::set_global_show_waveform_clipping (bool yn) +{ + if (_global_show_waveform_clipping != yn) { + _global_show_waveform_clipping = yn; + ClipLevelChanged (); + } +} + +void +WaveView::set_start_shift (double pixels) +{ + if (pixels < 0) { + return; + } + + begin_visual_change (); + //_start_shift = pixels; + end_visual_change (); +} + +void +WaveView::set_image_cache_size (uint64_t sz) +{ + WaveViewCache::get_instance()->set_image_cache_threshold (sz); +} + +boost::shared_ptr +WaveView::get_cache_group () const +{ + if (_cache_group) { + return _cache_group; + } + + boost::shared_ptr source = _region->audio_source (_props->channel); + assert (source); + + _cache_group = WaveViewCache::get_instance ()->get_cache_group (source); + + return _cache_group; +} + +void +WaveView::reset_cache_group () +{ + WaveViewCache::get_instance()->reset_cache_group (_cache_group); +} diff --git a/libs/waveview/wave_view_private.cc b/libs/waveview/wave_view_private.cc new file mode 100644 index 0000000000..716ef09bdd --- /dev/null +++ b/libs/waveview/wave_view_private.cc @@ -0,0 +1,458 @@ +/* + Copyright (C) 2017 Tim Mayberry + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#include +#include "ardour/lmath.h" + +#include "pbd/cpus.h" + +#include "ardour/audioregion.h" +#include "ardour/audiosource.h" + +#include "waveview/wave_view_private.h" + +namespace ArdourWaveView { + +WaveViewProperties::WaveViewProperties (boost::shared_ptr region) + : region_start (region->start ()) + , region_end (region->start () + region->length ()) + , channel (0) + , height (64) + , samples_per_pixel (0) + , amplitude (region->scale_amplitude ()) + , amplitude_above_axis (1.0) + , fill_color (0x000000ff) + , outline_color (0xff0000ff) + , zero_color (0xff0000ff) + , clip_color (0xff0000ff) + , show_zero (false) + , logscaled (WaveView::global_logscaled()) + , shape (WaveView::global_shape()) + , gradient_depth (WaveView::global_gradient_depth ()) + , start_shift (0.0) // currently unused + , sample_start (0) + , sample_end (0) +{ + +} + +/*-------------------------------------------------*/ + +WaveViewImage::WaveViewImage (boost::shared_ptr const& region_ptr, + WaveViewProperties const& properties) + : region (region_ptr) + , props (properties) + , timestamp (0) +{ + +} + +WaveViewImage::~WaveViewImage () +{ + +} + +/*-------------------------------------------------*/ + +WaveViewCacheGroup::WaveViewCacheGroup (WaveViewCache& parent_cache) + : _parent_cache (parent_cache) +{ + +} + +WaveViewCacheGroup::~WaveViewCacheGroup () +{ + clear_cache (); +} + +void +WaveViewCacheGroup::add_image (boost::shared_ptr image) +{ + if (!image) { + // Not adding invalid image to cache + return; + } + + ImageCache::iterator oldest_image_it = _cached_images.begin(); + ImageCache::iterator second_oldest_image_it = _cached_images.end(); + + for (ImageCache::iterator it = _cached_images.begin (); it != _cached_images.end (); ++it) { + if ((*it) == image) { + // Must never be more than one instance of the image in the cache + (*it)->timestamp = g_get_monotonic_time (); + return; + } else if ((*it)->props.is_equivalent (image->props)) { + // Equivalent Image already in cache, updating timestamp + (*it)->timestamp = g_get_monotonic_time (); + return; + } + + if ((*it)->timestamp < (*oldest_image_it)->timestamp) { + second_oldest_image_it = oldest_image_it; + oldest_image_it = it; + } + } + + // no duplicate or equivalent image so we are definitely adding it to cache + image->timestamp = g_get_monotonic_time (); + + if (_parent_cache.full () || full ()) { + if (oldest_image_it != _cached_images.end()) { + // Replacing oldest Image in cache + _parent_cache.decrease_size ((*oldest_image_it)->size_in_bytes ()); + *oldest_image_it = image; + _parent_cache.increase_size (image->size_in_bytes ()); + + if (second_oldest_image_it != _cached_images.end ()) { + // Removing second oldest Image in cache + _parent_cache.decrease_size ((*second_oldest_image_it)->size_in_bytes ()); + _cached_images.erase (second_oldest_image_it); + } + return; + } else { + /** + * Add the image to the cache even if the threshold is exceeded so that + * new WaveViews can still cache images with a full cache, the size of + * the cache will quickly equalize back to the threshold as new images + * are added and the size of the cache is reduced. + */ + } + } + + _cached_images.push_back (image); + _parent_cache.increase_size (image->size_in_bytes ()); +} + +boost::shared_ptr +WaveViewCacheGroup::lookup_image (WaveViewProperties const& props) +{ + for (ImageCache::iterator i = _cached_images.begin (); i != _cached_images.end (); ++i) { + if ((*i)->props.is_equivalent (props)) { + return (*i); + } + } + return boost::shared_ptr(); +} + +void +WaveViewCacheGroup::clear_cache () +{ + // Tell the parent cache about the images we are about to drop references to + for (ImageCache::iterator it = _cached_images.begin (); it != _cached_images.end (); ++it) { + _parent_cache.decrease_size ((*it)->size_in_bytes ()); + } + _cached_images.clear (); +} + +/*-------------------------------------------------*/ + +WaveViewCache::WaveViewCache () + : image_cache_size (0) + , _image_cache_threshold (100 * 1048576) /* bytes */ +{ + +} + +WaveViewCache::~WaveViewCache () +{ +} + +WaveViewCache* +WaveViewCache::get_instance () +{ + static WaveViewCache* instance = new WaveViewCache; + return instance; +} + +void +WaveViewCache::increase_size (uint64_t bytes) +{ + image_cache_size += bytes; +} + +void +WaveViewCache::decrease_size (uint64_t bytes) +{ + assert (image_cache_size - bytes < image_cache_size); + image_cache_size -= bytes; +} + +boost::shared_ptr +WaveViewCache::get_cache_group (boost::shared_ptr source) +{ + CacheGroups::iterator it = cache_group_map.find (source); + + if (it != cache_group_map.end()) { + // Found existing CacheGroup for AudioSource + return it->second; + } + + boost::shared_ptr new_group (new WaveViewCacheGroup (*this)); + + bool inserted = cache_group_map.insert (std::make_pair (source, new_group)).second; + + assert (inserted); + + return new_group; +} + +void +WaveViewCache::reset_cache_group (boost::shared_ptr& group) +{ + if (!group) { + return; + } + + CacheGroups::iterator it = cache_group_map.begin(); + + while (it != cache_group_map.end()) { + if (it->second == group) { + break; + } + ++it; + } + + assert (it != cache_group_map.end ()); + + group.reset(); + + if (it->second.unique()) { + cache_group_map.erase (it); + } +} + +void +WaveViewCache::clear_cache () +{ + for (CacheGroups::iterator it = cache_group_map.begin (); it != cache_group_map.end (); ++it) { + (*it).second->clear_cache (); + } +} + +void +WaveViewCache::set_image_cache_threshold (uint64_t sz) +{ + _image_cache_threshold = sz; +} + +/*-------------------------------------------------*/ + +WaveViewDrawRequest::WaveViewDrawRequest () : stop (0) +{ + +} + +WaveViewDrawRequest::~WaveViewDrawRequest () +{ + +} + +void +WaveViewDrawRequestQueue::enqueue (boost::shared_ptr& request) +{ + Glib::Threads::Mutex::Lock lm (_queue_mutex); + + _queue.push_back (request); + _cond.broadcast (); +} + +void +WaveViewDrawRequestQueue::wake_up () +{ + boost::shared_ptr null_ptr; + // hack!?...wake up the drawing thread + enqueue (null_ptr); +} + +boost::shared_ptr +WaveViewDrawRequestQueue::dequeue (bool block) +{ + if (block) { + _queue_mutex.lock(); + } else { + if (!_queue_mutex.trylock()) { + return boost::shared_ptr(); + } + } + + // _queue_mutex is always held at this point + + if (_queue.empty()) { + if (block) { + _cond.wait (_queue_mutex); + } else { + _queue_mutex.unlock(); + return boost::shared_ptr(); + } + } + + boost::shared_ptr req; + + if (!_queue.empty()) { + req = _queue.front (); + _queue.pop_front (); + } else { + // Queue empty, returning empty DrawRequest + } + + _queue_mutex.unlock(); + + return req; +} + +/*-------------------------------------------------*/ + +WaveViewThreads::WaveViewThreads () +{ + +} + +WaveViewThreads::~WaveViewThreads () +{ + +} + +uint32_t WaveViewThreads::init_count = 0; + +WaveViewThreads* WaveViewThreads::instance = 0; + +void +WaveViewThreads::initialize () +{ + // no need for atomics as only called from GUI thread + if (++init_count == 1) { + assert(!instance); + instance = new WaveViewThreads; + instance->start_threads(); + } +} + +void +WaveViewThreads::deinitialize () +{ + if (--init_count == 0) { + instance->stop_threads(); + delete instance; + instance = 0; + } +} + +void +WaveViewThreads::enqueue_draw_request (boost::shared_ptr& request) +{ + assert (instance); + instance->_request_queue.enqueue (request); +} + +boost::shared_ptr +WaveViewThreads::dequeue_draw_request () +{ + assert (instance); + return instance->_request_queue.dequeue (true); +} + +void +WaveViewThreads::wake_up () +{ + assert (instance); + return instance->_request_queue.wake_up (); +} + +void +WaveViewThreads::start_threads () +{ + assert (!_threads.size()); + + int num_cpus = hardware_concurrency (); + + uint32_t num_threads = std::max (1, num_cpus - 1); + + for (uint32_t i = 0; i != num_threads; ++i) { + boost::shared_ptr new_thread (new WaveViewDrawingThread ()); + _threads.push_back(new_thread); + } +} + +void +WaveViewThreads::stop_threads () +{ + assert (_threads.size()); + + _threads.clear (); +} + +/*-------------------------------------------------*/ + +WaveViewDrawingThread::WaveViewDrawingThread () + : _thread(0) + , _quit(0) +{ + start (); +} + +WaveViewDrawingThread::~WaveViewDrawingThread () +{ + quit (); +} + +void +WaveViewDrawingThread::start () +{ + assert (!_thread); + + _thread = Glib::Threads::Thread::create (sigc::mem_fun (*this, &WaveViewDrawingThread::run)); +} + +void +WaveViewDrawingThread::quit () +{ + assert (_thread); + + g_atomic_int_set (&_quit, 1); + WaveViewThreads::wake_up (); + _thread->join(); + _thread = 0; +} + +void +WaveViewDrawingThread::run () +{ + while (true) { + + if (g_atomic_int_get (&_quit)) { + break; + } + + // block until a request is available. + boost::shared_ptr req = WaveViewThreads::dequeue_draw_request (); + + if (req && !req->stopped()) { + try { + WaveView::process_draw_request (req); + } catch (...) { + /* just in case it was set before the exception, whatever it was */ + req->image->cairo_image.clear (); + } + } else { + // null or stopped Request, processing skipped + } + } +} + +} diff --git a/libs/waveview/waveview/debug.h b/libs/waveview/waveview/debug.h new file mode 100644 index 0000000000..df93085721 --- /dev/null +++ b/libs/waveview/waveview/debug.h @@ -0,0 +1,35 @@ +/* + Copyright (C) 2011-2013 Paul Davis + Author: Carl Hetherington + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef _WAVEVIEW_DEBUG_H_ +#define _WAVEVIEW_DEBUG_H_ + +#include +#include +#include "pbd/debug.h" + +#include "waveview/visibility.h" + +namespace PBD { + namespace DEBUG { + LIBWAVEVIEW_API extern DebugBits WaveView; + } +} + +#endif diff --git a/libs/waveview/waveview/visibility.h b/libs/waveview/waveview/visibility.h new file mode 100644 index 0000000000..827ffd5f0e --- /dev/null +++ b/libs/waveview/waveview/visibility.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2013 Paul Davis + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef __libwaveview_visibility_h__ +#define __libwaveview_visibility_h__ + +#if defined(COMPILER_MSVC) + #define LIBWAVEVIEW_DLL_IMPORT __declspec(dllimport) + #define LIBWAVEVIEW_DLL_EXPORT __declspec(dllexport) + #define LIBWAVEVIEW_DLL_LOCAL +#else + #define LIBWAVEVIEW_DLL_IMPORT __attribute__ ((visibility ("default"))) + #define LIBWAVEVIEW_DLL_EXPORT __attribute__ ((visibility ("default"))) + #define LIBWAVEVIEW_DLL_LOCAL __attribute__ ((visibility ("hidden"))) +#endif + +#ifdef LIBWAVEVIEW_STATIC // libcanvas is not a DLL +#define LIBWAVEVIEW_API +#define LIBWAVEVIEW_LOCAL +#else + #ifdef LIBWAVEVIEW_DLL_EXPORTS // defined if we are building the libcanvas DLL (instead of using it) + #define LIBWAVEVIEW_API LIBWAVEVIEW_DLL_EXPORT + #else + #define LIBWAVEVIEW_API LIBWAVEVIEW_DLL_IMPORT + #endif + #define LIBWAVEVIEW_LOCAL LIBWAVEVIEW_DLL_LOCAL +#endif + +#endif /* __libwaveview_visibility_h__ */ diff --git a/libs/waveview/waveview/wave_view.h b/libs/waveview/waveview/wave_view.h new file mode 100644 index 0000000000..4b1c8eb42b --- /dev/null +++ b/libs/waveview/waveview/wave_view.h @@ -0,0 +1,268 @@ +/* + Copyright (C) 2011-2013 Paul Davis + Copyright (C) 2017 Tim Mayberry + Author: Carl Hetherington + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef _WAVEVIEW_WAVE_VIEW_H_ +#define _WAVEVIEW_WAVE_VIEW_H_ + +#include +#include + +#include + +#include "ardour/types.h" +#include "canvas/item.h" +#include "waveview/visibility.h" + +namespace ARDOUR { + class AudioRegion; +} + +namespace Gdk { + class Pixbuf; +} + +namespace ArdourWaveView { + +class WaveViewCacheGroup; +class WaveViewDrawRequest; +class WaveViewDrawRequestQueue; +class WaveViewImage; +class WaveViewProperties; +class WaveViewDrawingThread; + +class LIBWAVEVIEW_API WaveView : public ArdourCanvas::Item, public sigc::trackable +{ +public: + enum Shape { Normal, Rectified }; + + std::string debug_name () const; + + /* Displays a single channel of waveform data for the given Region. + + x = 0 in the waveview corresponds to the first waveform datum taken + from region->start() samples into the source data. + + x = N in the waveview corresponds to the (N * spp)'th sample + measured from region->start() into the source data. + + when drawing, we will map the zeroth-pixel of the waveview + into a window. + + The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces + that cache sections of the display. This is filled on-demand and + never cleared until something explicitly marks the cache invalid + (such as a change in samples_per_pixel, the log scaling, rectified or + other view parameters). + */ + + WaveView (ArdourCanvas::Canvas*, boost::shared_ptr); + WaveView (Item*, boost::shared_ptr); + ~WaveView (); + + virtual void prepare_for_render (ArdourCanvas::Rect const& window_area) const; + + virtual void render (ArdourCanvas::Rect const & area, Cairo::RefPtr) const; + + void compute_bounding_box () const; + + void set_samples_per_pixel (double); + void set_height (ArdourCanvas::Distance); + void set_channel (int); + void set_region_start (ARDOUR::frameoffset_t); + + /** Change the first position drawn by @param pixels. + * @param pixels must be positive. This is used by + * AudioRegionViews in Ardour to avoid drawing the + * first pixel of a waveform, and exists in case + * there are uses for WaveView where we do not + * want this behaviour. + */ + void set_start_shift (double pixels); + + void set_fill_color (Gtkmm2ext::Color); + void set_outline_color (Gtkmm2ext::Color); + + void region_resized (); + void gain_changed (); + + void set_show_zero_line (bool); + bool show_zero_line () const; + + void set_zero_color (Gtkmm2ext::Color); + void set_clip_color (Gtkmm2ext::Color); + void set_logscaled (bool); + + void set_gradient_depth (double); + double gradient_depth () const; + + void set_shape (Shape); + + void set_always_get_image_in_thread (bool yn); + + /* currently missing because we don't need them (yet): + * set_shape_independent(); + * set_logscaled_independent(); + */ + + static void set_global_gradient_depth (double); + static void set_global_logscaled (bool); + static void set_global_shape (Shape); + static void set_global_show_waveform_clipping (bool); + + static double global_gradient_depth () { return _global_gradient_depth; } + + static bool global_logscaled () { return _global_logscaled; } + + static Shape global_shape () { return _global_shape; } + + void set_amplitude_above_axis (double v); + + double amplitude_above_axis () const; + + static void set_clip_level (double dB); + static PBD::Signal0 ClipLevelChanged; + + static void start_drawing_thread (); + static void stop_drawing_thread (); + + static void set_image_cache_size (uint64_t); + +#ifdef CANVAS_COMPATIBILITY + void*& property_gain_src () { + return _foo_void; + } + void*& property_gain_function () { + return _foo_void; + } + +private: + void* _foo_void; +#endif + +private: + friend class WaveViewThreadClient; + friend class WaveViewDrawingThread; + + boost::shared_ptr _region; + + boost::scoped_ptr _props; + + mutable boost::shared_ptr _image; + + mutable boost::shared_ptr _cache_group; + + bool _shape_independent; + bool _logscaled_independent; + bool _gradient_depth_independent; + + /** Under almost conditions, this is going to return _region->length(), + * but if region_start has been reset, then we need to use this modified + * computation. + */ + ARDOUR::framecnt_t region_length () const; + + /** Under almost conditions, this is going to return _region->start() + + * _region->length(), but if region_start has been reset, then we need to use + * this modified computation. + */ + ARDOUR::framepos_t region_end () const; + + /** + * _image stays non-null after the first time it is set + */ + bool rendered () const { return _image.get(); } + + bool draw_image_in_gui_thread () const; + + /** If true, calls to render() will render a missing wave image in the GUI + * thread. Generally set to false, but true after a call to set_height(). + */ + mutable bool _draw_image_in_gui_thread; + + /** If true, calls to render() will render a missing wave image in the GUI + * thread. Set true for waveviews we expect to keep updating (e.g. while + * recording) + */ + bool _always_draw_image_in_gui_thread; + + void init(); + + mutable boost::shared_ptr current_request; + + PBD::ScopedConnectionList invalidation_connection; + + static double _global_gradient_depth; + static bool _global_logscaled; + static Shape _global_shape; + static bool _global_show_waveform_clipping; + static double _global_clip_level; + + static PBD::Signal0 VisualPropertiesChanged; + + void handle_visual_property_change (); + void handle_clip_level_change (); + + struct LineTips { + double top; + double bot; + double spread; + bool clip_max; + bool clip_min; + + LineTips () : top (0.0), bot (0.0), clip_max (false), clip_min (false) {} + }; + + static ArdourCanvas::Coord y_extent (double, Shape const, double const height); + + static void compute_tips (ARDOUR::PeakData const& peak, LineTips& tips, double const effective_height); + + static void draw_image (Cairo::RefPtr&, ARDOUR::PeakData*, int n_peaks, + boost::shared_ptr); + static void draw_absent_image (Cairo::RefPtr&, ARDOUR::PeakData*, int); + + ARDOUR::framecnt_t optimal_image_width_samples () const; + + void set_image (boost::shared_ptr img) const; + + // @return true if item area intersects with draw area + bool get_item_and_draw_rect_in_window_coords (ArdourCanvas::Rect const& canvas_rect, + ArdourCanvas::Rect& item_area, + ArdourCanvas::Rect& draw_rect) const; + + boost::shared_ptr create_draw_request (WaveViewProperties const&) const; + + void queue_draw_request (boost::shared_ptr const&) const; + + static void process_draw_request (boost::shared_ptr); + + boost::shared_ptr get_cache_group () const; + + /** + * Notify the Cache that we are dropping our reference to the + * CacheGroup so it can also do so if it is the only reference holder + * of the cache group. + */ + void reset_cache_group (); +}; + +} /* namespace */ + +#endif diff --git a/libs/waveview/waveview/wave_view_private.h b/libs/waveview/waveview/wave_view_private.h new file mode 100644 index 0000000000..b1e5ace844 --- /dev/null +++ b/libs/waveview/waveview/wave_view_private.h @@ -0,0 +1,365 @@ +/* + Copyright (C) 2017 Tim Mayberry + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +#ifndef _WAVEVIEW_WAVE_VIEW_PRIVATE_H_ +#define _WAVEVIEW_WAVE_VIEW_PRIVATE_H_ + +#include + +#include "waveview/wave_view.h" + +namespace ARDOUR { + class AudioRegion; +} + +namespace ArdourWaveView { + +struct WaveViewProperties +{ +public: // ctors + WaveViewProperties (boost::shared_ptr region); + + // WaveViewProperties (WaveViewProperties const& other) = default; + + // WaveViewProperties& operator=(WaveViewProperties const& other) = default; + +public: // member variables + + framepos_t region_start; + framepos_t region_end; + uint16_t channel; + double height; + double samples_per_pixel; + double amplitude; + double amplitude_above_axis; + Gtkmm2ext::Color fill_color; + Gtkmm2ext::Color outline_color; + Gtkmm2ext::Color zero_color; + Gtkmm2ext::Color clip_color; + bool show_zero; + bool logscaled; + WaveView::Shape shape; + double gradient_depth; + double start_shift; + +private: // member variables + + framepos_t sample_start; + framepos_t sample_end; + +public: // methods + + bool is_valid () const + { + return (sample_end != 0 && samples_per_pixel != 0); + } + + void set_width_samples (ARDOUR::framecnt_t const width_samples) + { + assert (is_valid()); + assert (width_samples != 0); + ARDOUR::framecnt_t half_width = width_samples / 2; + framepos_t new_sample_start = std::max (region_start, get_center_sample () - half_width); + framepos_t new_sample_end = std::min (get_center_sample () + half_width, region_end); + assert (new_sample_start <= new_sample_end); + sample_start = new_sample_start; + sample_end = new_sample_end; + } + + uint64_t get_width_pixels () const + { + return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel))); + } + + + void set_sample_offsets (framepos_t const start, framepos_t const end) + { + assert (start <= end); + + // sample_start and sample_end are bounded by the region limits. + if (start < region_start) { + sample_start = region_start; + } else if (start > region_end) { + sample_start = region_end; + } else { + sample_start = start; + } + + if (end > region_end) { + sample_end = region_end; + } else if (end < region_start) { + sample_end = region_start; + } else { + sample_end = end; + } + + assert (sample_start <= sample_end); + } + + framepos_t get_sample_start () const + { + return sample_start; + } + + framepos_t get_sample_end () const + { + return sample_end; + } + + void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel) + { + assert (start_pixel <= end_pixel); + /** + * It is possible for the new sample positions to be past the region_end, + * so we have to do bounds checking/adjustment for this in set_sample_offsets. + */ + framepos_t new_sample_start = region_start + (start_pixel * samples_per_pixel); + framepos_t new_sample_end = region_start + (end_pixel * samples_per_pixel); + set_sample_offsets (new_sample_start, new_sample_end); + } + + ARDOUR::framecnt_t get_length_samples () const + { + assert (sample_start <= sample_end); + return sample_end - sample_start; + } + + framepos_t get_center_sample () + { + return sample_start + (get_length_samples() / 2); + } + + bool is_equivalent (WaveViewProperties const& other) + { + return (samples_per_pixel == other.samples_per_pixel && + contains (other.sample_start, other.sample_end) && channel == other.channel && + height == other.height && amplitude == other.amplitude && + amplitude_above_axis == other.amplitude_above_axis && fill_color == other.fill_color && + outline_color == other.outline_color && zero_color == other.zero_color && + clip_color == other.clip_color && show_zero == other.show_zero && + logscaled == other.logscaled && shape == other.shape && + gradient_depth == other.gradient_depth); + // region_start && start_shift?? + } + + bool contains (framepos_t start, framepos_t end) + { + return (sample_start <= start && end <= sample_end); + } +}; + +struct WaveViewImage { +public: // ctors + WaveViewImage (boost::shared_ptr const& region_ptr, + WaveViewProperties const& properties); + + ~WaveViewImage (); + +public: // member variables + boost::weak_ptr region; + WaveViewProperties props; + Cairo::RefPtr cairo_image; + uint64_t timestamp; + +public: // methods + bool finished() { return static_cast(cairo_image); } + + bool + contains_image_with_properties (WaveViewProperties const& other_props) + { + return cairo_image && props.is_equivalent (other_props); + } + + bool is_valid () { + return props.is_valid (); + } + + size_t size_in_bytes () + { + // 4 = bytes per FORMAT_ARGB32 pixel + return props.height * props.get_width_pixels() * 4; + } +}; + +struct WaveViewDrawRequest +{ +public: + WaveViewDrawRequest (); + ~WaveViewDrawRequest (); + + bool stopped() const { return (bool) g_atomic_int_get (const_cast(&stop)); } + void cancel() { g_atomic_int_set (&stop, 1); } + bool finished() { return image->finished(); } + + boost::shared_ptr image; + + bool is_valid () { + return (image && image->is_valid()); + } + +private: + gint stop; /* intended for atomic access */ +}; + +class WaveViewCache; + +class WaveViewCacheGroup +{ +public: + WaveViewCacheGroup (WaveViewCache& parent_cache); + + ~WaveViewCacheGroup (); + +public: + + // @return image with matching properties or null + boost::shared_ptr lookup_image (WaveViewProperties const&); + + void add_image (boost::shared_ptr); + + bool full () const { return _cached_images.size() > max_size(); } + + static uint32_t max_size () { return 16; } + + void clear_cache (); + +private: + + /** + * At time of writing we don't strictly need a reference to the parent cache + * as there is only a single global cache but if the image cache ever becomes + * a per canvas cache then a using a reference is handy. + */ + WaveViewCache& _parent_cache; + + typedef std::list > ImageCache; + ImageCache _cached_images; +}; + +class WaveViewCache +{ +public: + static WaveViewCache* get_instance (); + + uint64_t image_cache_threshold () const { return _image_cache_threshold; } + void set_image_cache_threshold (uint64_t); + + void clear_cache (); + + boost::shared_ptr get_cache_group (boost::shared_ptr); + + void reset_cache_group (boost::shared_ptr&); + +private: + WaveViewCache(); + ~WaveViewCache(); + +private: + typedef std::map, boost::shared_ptr > + CacheGroups; + + CacheGroups cache_group_map; + + uint64_t image_cache_size; + uint64_t _image_cache_threshold; + +private: + friend class WaveViewCacheGroup; + + void increase_size (uint64_t bytes); + void decrease_size (uint64_t bytes); + + bool full () { return image_cache_size > _image_cache_threshold; } +}; + +class WaveViewDrawRequestQueue +{ +public: + + void enqueue (boost::shared_ptr&); + + // @return valid request or null if non-blocking or no request is available + boost::shared_ptr dequeue (bool block); + + void wake_up (); + +private: + + mutable Glib::Threads::Mutex _queue_mutex; + Glib::Threads::Cond _cond; + + typedef std::deque > DrawRequestQueueType; + DrawRequestQueueType _queue; +}; + +class WaveViewDrawingThread +{ +public: + WaveViewDrawingThread (); + ~WaveViewDrawingThread (); + +private: + void start (); + void quit (); + void run (); + +private: + Glib::Threads::Thread* _thread; + gint _quit; +}; + +class WaveViewThreads { +private: + WaveViewThreads (); + ~WaveViewThreads (); + +public: + static void initialize (); + static void deinitialize (); + + static bool enabled () { return (instance); } + + static void enqueue_draw_request (boost::shared_ptr&); + +private: + friend class WaveViewDrawingThread; + + static void wake_up (); + + // will block until a request is available + static boost::shared_ptr dequeue_draw_request (); + + void start_threads (); + void stop_threads (); + +private: + static uint32_t init_count; + static WaveViewThreads* instance; + + // TODO use std::unique_ptr when possible + typedef std::vector > WaveViewThreadList; + + WaveViewThreadList _threads; + WaveViewDrawRequestQueue _request_queue; +}; + + +} /* namespace */ + +#endif diff --git a/libs/waveview/wscript b/libs/waveview/wscript new file mode 100644 index 0000000000..35311fac17 --- /dev/null +++ b/libs/waveview/wscript @@ -0,0 +1,64 @@ +#!/usr/bin/env python +from waflib.extras import autowaf as autowaf +from waflib import Options +from waflib import TaskGen +import os + +# Version of this package (even if built as a child) +MAJOR = '0' +MINOR = '0' +MICRO = '0' +WAVEVIEW_VERSION = "%s.%s.%s" % (MAJOR, MINOR, MICRO) + +# Library version (UNIX style major, minor, micro) +# major increment <=> incompatible changes +# minor increment <=> compatible changes (additions) +# micro increment <=> no interface changes +WAVEVIEW_LIB_VERSION = '0.0.0' + +# Variables for 'waf dist' +APPNAME = 'waveview' +VERSION = WAVEVIEW_VERSION +I18N_PACKAGE = 'libwaveview' + +# Mandatory variables +top = '.' +out = 'build' + +waveview_sources = [ + 'debug.cc', + 'wave_view.cc', + 'wave_view_private.cc', +] + +def options(opt): + autowaf.set_options(opt) + +def configure(conf): + conf.load ('compiler_cxx') + autowaf.configure(conf) + autowaf.check_pkg(conf, 'cairomm-1.0', uselib_store='CAIROMM', atleast_version='1.8.4') + +def build(bld): + # Library + if bld.is_defined ('INTERNAL_SHARED_LIBS'): + obj = bld.shlib(features = 'cxx cxxshlib', source=waveview_sources) + obj.defines = [ 'LIBWAVEVIEW_DLL_EXPORTS=1' ] + else: + obj = bld.stlib(features = 'cxx cxxstlib', source=waveview_sources) + obj.cxxflags = [ '-fPIC' ] + obj.cflags = [ '-fPIC' ] + obj.defines = [ ] + + obj.export_includes = ['.'] + obj.includes = ['.'] + obj.uselib = 'SIGCPP CAIROMM GTKMM BOOST XML' + obj.use = [ 'libpbd', 'libcanvas', 'libardour', 'libgtkmm2ext' ] + obj.name = 'libwaveview' + obj.target = 'waveview' + obj.vnum = WAVEVIEW_LIB_VERSION + obj.install_path = bld.env['LIBDIR'] + obj.defines += [ 'PACKAGE="' + I18N_PACKAGE + '"' ] + +def shutdown(): + autowaf.shutdown() diff --git a/wscript b/wscript index c0ee26f87d..0a8a7460ae 100644 --- a/wscript +++ b/wscript @@ -221,6 +221,7 @@ children = [ 'libs/audiographer', 'libs/canvas', 'libs/widgets', + 'libs/waveview', 'libs/plugins/reasonablesynth.lv2', 'libs/plugins/a-comp.lv2', 'libs/plugins/a-delay.lv2', -- cgit v1.2.3