1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Added Tweening but the library uses too much CPU with many layers

This commit is contained in:
SpoinkyNL 2016-11-30 16:22:19 +01:00
parent 188e1ddfe4
commit 31b51e561a
5 changed files with 131 additions and 11 deletions

View File

@ -127,6 +127,10 @@
<TargetZone>LocalIntranet</TargetZone>
</PropertyGroup>
<ItemGroup>
<Reference Include="Betwixt, Version=1.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Betwixt.1.4.1\lib\net35\Betwixt.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Caliburn.Micro, Version=3.0.1.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f, processorArchitecture=MSIL">
<HintPath>..\packages\Caliburn.Micro.Core.3.0.1\lib\net45\Caliburn.Micro.dll</HintPath>
<Private>True</Private>
@ -403,6 +407,7 @@
<Compile Include="Profiles\Layers\Animations\NoneAnimation.cs" />
<Compile Include="Profiles\Layers\Models\EventPropertiesModel.cs" />
<Compile Include="Profiles\Layers\Models\KeyboardEventPropertiesModel.cs" />
<Compile Include="Profiles\Layers\Models\TweenModel.cs" />
<Compile Include="Profiles\Layers\Types\AmbientLight\AmbienceCreator\AmbienceCreatorExtend.cs" />
<Compile Include="Profiles\Layers\Types\AmbientLight\AmbienceCreator\AmbienceCreatorMirror.cs" />
<Compile Include="Profiles\Layers\Types\AmbientLight\AmbienceCreator\AvgColor.cs" />

View File

@ -19,7 +19,7 @@ namespace Artemis.Profiles.Layers.Models
public LayerModel()
{
Children = new ChildItemCollection<LayerModel, LayerModel>(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; }
/// <summary>
/// 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);
}
/// <summary>

View File

@ -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<float> _heightTweener;
private float _opacity;
private Tweener<float> _opacityTweener;
private float _width;
private Tweener<float> _widthTweener;
private float _x;
private Tweener<float> _xTweener;
private float _y;
private Tweener<float> _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<float>(0, (float) layerModel.X, XDuration, XFunc);
_yTweener = new Tweener<float>(0, (float) layerModel.Y, YDuration, YFunc);
_widthTweener = new Tweener<float>(0, (float) layerModel.Width, WidthDuration, WidthFunc);
_heightTweener = new Tweener<float>(0, (float) layerModel.Height, HeightDuration, HeightFunc);
_opacityTweener = new Tweener<float>(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<float>(_x, (float) _layerModel.X, XDuration, XFunc);
if (Math.Abs(_layerModel.Y - _y) > 0.001)
_yTweener = new Tweener<float>(_y, (float) _layerModel.Y, YDuration, YFunc);
if (Math.Abs(_layerModel.Width - _width) > 0.001)
_widthTweener = new Tweener<float>(_width, (float) _layerModel.Width, WidthDuration, WidthFunc);
if (Math.Abs(_layerModel.Height - _height) > 0.001)
_heightTweener = new Tweener<float>(_height, (float) _layerModel.Height, HeightDuration, HeightFunc);
if (Math.Abs(_layerModel.Opacity - _opacity) > 0.001)
_opacityTweener = new Tweener<float>(_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;
}
}
}

View File

@ -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;

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Betwixt" version="1.4.1" targetFramework="net461" />
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Castle.Core" version="3.3.3" targetFramework="net452" />