From 31b51e561a02b9cf2a74857ab39bc3fce3580b1d Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Wed, 30 Nov 2016 16:22:19 +0100 Subject: [PATCH] Added Tweening but the library uses too much CPU with many layers --- Artemis/Artemis/Artemis.csproj | 5 + .../Profiles/Layers/Models/LayerModel.cs | 15 ++- .../Profiles/Layers/Models/TweenModel.cs | 94 +++++++++++++++++++ .../Profiles/Layers/Types/Audio/AudioType.cs | 27 ++++-- Artemis/Artemis/packages.config | 1 + 5 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 Artemis/Artemis/Profiles/Layers/Models/TweenModel.cs diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index 198390572..1d77f6b3f 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -127,6 +127,10 @@ LocalIntranet + + ..\packages\Betwixt.1.4.1\lib\net35\Betwixt.dll + True + ..\packages\Caliburn.Micro.Core.3.0.1\lib\net45\Caliburn.Micro.dll True @@ -403,6 +407,7 @@ + diff --git a/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs b/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs index f0da902c0..f7c833247 100644 --- a/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs +++ b/Artemis/Artemis/Profiles/Layers/Models/LayerModel.cs @@ -19,7 +19,7 @@ namespace Artemis.Profiles.Layers.Models public LayerModel() { Children = new ChildItemCollection(this); - + TweenModel = new TweenModel(this, 200); var model = Properties as KeyboardPropertiesModel; if (model != null) GifImage = new GifImage(model.GifFile); @@ -27,6 +27,8 @@ namespace Artemis.Profiles.Layers.Models [JsonIgnore] public ImageSource LayerImage => LayerType.DrawThumbnail(this); + [JsonIgnore] + public TweenModel TweenModel { get; set; } /// /// Checks whether this layers conditions are met. @@ -51,6 +53,8 @@ namespace Artemis.Profiles.Layers.Models LayerType.Update(this, dataModel, preview); LayerAnimation?.Update(this, updateAnimations); + TweenModel.Update(); + LastRender = DateTime.Now; } @@ -220,7 +224,14 @@ namespace Artemis.Profiles.Layers.Models public Rect LayerRect(int scale = 4) { - return new Rect(X* scale, Y* scale, Width*scale, Height*scale); + var width = Width; + var height = Height; + if (width < 0) + width = 0; + if (height < 0) + height = 0; + + return new Rect(X * scale, Y * scale, width * scale, height * scale); } /// diff --git a/Artemis/Artemis/Profiles/Layers/Models/TweenModel.cs b/Artemis/Artemis/Profiles/Layers/Models/TweenModel.cs new file mode 100644 index 000000000..47e68220d --- /dev/null +++ b/Artemis/Artemis/Profiles/Layers/Models/TweenModel.cs @@ -0,0 +1,94 @@ +using System; +using Betwixt; + +namespace Artemis.Profiles.Layers.Models +{ + public class TweenModel + { + private readonly LayerModel _layerModel; + private float _height; + private Tweener _heightTweener; + private float _opacity; + private Tweener _opacityTweener; + private float _width; + private Tweener _widthTweener; + private float _x; + private Tweener _xTweener; + private float _y; + private Tweener _yTweener; + + public TweenModel(LayerModel layerModel, double defaultDuration) + { + _layerModel = layerModel; + + XDuration = defaultDuration; + YDuration = defaultDuration; + WidthDuration = defaultDuration; + HeightDuration = defaultDuration; + OpacityDuration = defaultDuration; + + XFunc = Ease.Quad.InOut; + YFunc = Ease.Quad.InOut; + WidthFunc = Ease.Quad.InOut; + HeightFunc = Ease.Quad.InOut; + OpacityFunc = Ease.Quad.InOut; + + _xTweener = new Tweener(0, (float) layerModel.X, XDuration, XFunc); + _yTweener = new Tweener(0, (float) layerModel.Y, YDuration, YFunc); + _widthTweener = new Tweener(0, (float) layerModel.Width, WidthDuration, WidthFunc); + _heightTweener = new Tweener(0, (float) layerModel.Height, HeightDuration, HeightFunc); + _opacityTweener = new Tweener(0, (float) layerModel.Opacity, OpacityDuration, OpacityFunc); + + StoreCurrentValues(); + } + + public double XDuration { get; set; } + public double YDuration { get; set; } + public double WidthDuration { get; set; } + public double HeightDuration { get; set; } + public double OpacityDuration { get; set; } + + public EaseFunc XFunc { get; set; } + public EaseFunc YFunc { get; set; } + public EaseFunc WidthFunc { get; set; } + public EaseFunc HeightFunc { get; set; } + public EaseFunc OpacityFunc { get; set; } + + private void StoreCurrentValues() + { + _x = (float) _layerModel.X; + _y = (float) _layerModel.Y; + _width = (float) _layerModel.Width; + _height = (float) _layerModel.Height; + _opacity = (float) _layerModel.Opacity; + } + + public void Update() + { + if (Math.Abs(_layerModel.X - _x) > 0.001) + _xTweener = new Tweener(_x, (float) _layerModel.X, XDuration, XFunc); + if (Math.Abs(_layerModel.Y - _y) > 0.001) + _yTweener = new Tweener(_y, (float) _layerModel.Y, YDuration, YFunc); + if (Math.Abs(_layerModel.Width - _width) > 0.001) + _widthTweener = new Tweener(_width, (float) _layerModel.Width, WidthDuration, WidthFunc); + if (Math.Abs(_layerModel.Height - _height) > 0.001) + _heightTweener = new Tweener(_height, (float) _layerModel.Height, HeightDuration, HeightFunc); + if (Math.Abs(_layerModel.Opacity - _opacity) > 0.001) + _opacityTweener = new Tweener(_opacity, (float) _layerModel.Opacity, OpacityDuration, OpacityFunc); + + _xTweener.Update(40); + _yTweener.Update(40); + _widthTweener.Update(40); + _heightTweener.Update(40); + _opacityTweener.Update(40); + + StoreCurrentValues(); + + _layerModel.X = _xTweener.Value; + _layerModel.Y = _yTweener.Value; + _layerModel.Width = _widthTweener.Value; + _layerModel.Height = _heightTweener.Value; + _layerModel.Opacity = _opacityTweener.Value; + } + } +} \ No newline at end of file diff --git a/Artemis/Artemis/Profiles/Layers/Types/Audio/AudioType.cs b/Artemis/Artemis/Profiles/Layers/Types/Audio/AudioType.cs index b64983c10..7ad428a31 100644 --- a/Artemis/Artemis/Profiles/Layers/Types/Audio/AudioType.cs +++ b/Artemis/Artemis/Profiles/Layers/Types/Audio/AudioType.cs @@ -28,6 +28,7 @@ namespace Artemis.Profiles.Layers.Types.Audio private readonly WasapiLoopbackCapture _waveIn; private int _lines; private AudioPropertiesModel _previousSettings; + private DateTime _lastAudioUpdate; public AudioType() { @@ -36,7 +37,7 @@ namespace Artemis.Profiles.Layers.Types.Audio _sampleAggregator.FftCalculated += FftCalculated; _sampleAggregator.PerformFFT = true; - + // Start listening for sound data _waveIn = new WasapiLoopbackCapture(); _waveIn.DataAvailable += OnDataAvailable; @@ -107,14 +108,15 @@ namespace Artemis.Profiles.Layers.Types.Audio { UpdateLayers(layerModel); - if (!SpectrumData.Any()) - return; - - var settings = (AudioPropertiesModel) layerModel.Properties; - if ((settings.Direction == Direction.TopToBottom) || (settings.Direction == Direction.BottomToTop)) - ApplyVertical(settings); - else if ((settings.Direction == Direction.LeftToRight) || (settings.Direction == Direction.RightToLeft)) - ApplyHorizontal(settings); + if (SpectrumData.Any()) + { + var settings = (AudioPropertiesModel) layerModel.Properties; + if ((settings.Direction == Direction.TopToBottom) || (settings.Direction == Direction.BottomToTop)) + ApplyVertical(settings); + else if ((settings.Direction == Direction.LeftToRight) || + (settings.Direction == Direction.RightToLeft)) + ApplyHorizontal(settings); + } } } @@ -222,6 +224,8 @@ namespace Artemis.Profiles.Layers.Types.Audio audioLayer.Properties.Height = settings.Height; audioLayer.LayerAnimation?.Update(audioLayer, true); + audioLayer.TweenModel.Update(); + // Restore the height and width audioLayer.Properties.Height = oldHeight; audioLayer.Properties.Width = oldWidth; @@ -325,6 +329,11 @@ namespace Artemis.Profiles.Layers.Types.Audio // TODO: Check how often this is called private void OnDataAvailable(object sender, WaveInEventArgs e) { + if ((DateTime.Now - _lastAudioUpdate) < TimeSpan.FromMilliseconds(200)) + return; + + _lastAudioUpdate = DateTime.Now; + var buffer = e.Buffer; var bytesRecorded = e.BytesRecorded; var bufferIncrement = _waveIn.WaveFormat.BlockAlign; diff --git a/Artemis/Artemis/packages.config b/Artemis/Artemis/packages.config index 0e4350b48..6faac38d1 100644 --- a/Artemis/Artemis/packages.config +++ b/Artemis/Artemis/packages.config @@ -1,5 +1,6 @@  +