1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2026-03-24 10:18:47 +00:00

Added AudioType brush drawing and animations back in

This commit is contained in:
SpoinkyNL 2017-01-12 14:21:27 +01:00
parent 0656687b88
commit de0ff1ebad
2 changed files with 91 additions and 59 deletions

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows; using System.Windows;
using CSCore.DSP; using CSCore.DSP;
@ -26,36 +28,17 @@ namespace Artemis.Profiles.Layers.Types.Audio.AudioCapturing
} }
} }
public void UpdateLinesVertical(double height, Point[] points) public List<double> GetLineValues(double height)
{ {
var fftBuffer = new float[(int) FftSize]; var fftBuffer = new float[(int) FftSize];
// get the fft result from the spectrum provider // get the fft result from the spectrum provider
if (!SpectrumProvider.GetFftData(fftBuffer, this)) if (!SpectrumProvider.GetFftData(fftBuffer, this))
return; return null;
var spectrumPoints = CalculateSpectrumPoints(height, fftBuffer); var spectrumPoints = CalculateSpectrumPoints(height, fftBuffer);
for (var index = 0; index < spectrumPoints.Length; index++) return spectrumPoints?.Select(s => s.Value).ToList();
{
var spectrumPointData = spectrumPoints[index];
points[index].Y = spectrumPointData.Value;
}
}
public void UpdateLinesHorizontal(double width, Point[] points)
{
var fftBuffer = new float[(int) FftSize];
// get the fft result from the spectrum provider
if (!SpectrumProvider.GetFftData(fftBuffer, this))
return;
var spectrumPoints = CalculateSpectrumPoints(width, fftBuffer);
for (var index = 0; index < spectrumPoints.Length; index++)
{
var spectrumPointData = spectrumPoints[index];
points[index].X = spectrumPointData.Value;
}
} }
} }
} }

View File

@ -1,7 +1,9 @@
using System.Windows; using System.Collections.Generic;
using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using Artemis.Modules.Abstract; using Artemis.Modules.Abstract;
using Artemis.Profiles.Layers.Abstract; using Artemis.Profiles.Layers.Abstract;
using Artemis.Profiles.Layers.Animations;
using Artemis.Profiles.Layers.Interfaces; using Artemis.Profiles.Layers.Interfaces;
using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Layers.Models;
using Artemis.Profiles.Layers.Types.Audio.AudioCapturing; using Artemis.Profiles.Layers.Types.Audio.AudioCapturing;
@ -13,10 +15,11 @@ namespace Artemis.Profiles.Layers.Types.Audio
{ {
public class AudioType : ILayerType public class AudioType : ILayerType
{ {
private const GeometryCombineMode CombineMode = GeometryCombineMode.Union;
private readonly AudioCapture _audioCapture; private readonly AudioCapture _audioCapture;
private int _lines; private int _lines;
private LineSpectrum _lineSpectrum; private LineSpectrum _lineSpectrum;
private Point[] _points; private List<double> _lineValues;
public AudioType(AudioCaptureManager audioCaptureManager) public AudioType(AudioCaptureManager audioCaptureManager)
{ {
@ -42,31 +45,56 @@ namespace Artemis.Profiles.Layers.Types.Audio
public void Draw(LayerModel layerModel, DrawingContext c) public void Draw(LayerModel layerModel, DrawingContext c)
{ {
var parentX = layerModel.X * 4; if (_lineValues == null)
var parentY = layerModel.Y * 4; return;
var pen = new Pen(layerModel.Brush, 4);
var parentX = layerModel.X;
var parentY = layerModel.Y;
var direction = ((AudioPropertiesModel) layerModel.Properties).Direction; var direction = ((AudioPropertiesModel) layerModel.Properties).Direction;
if (direction == Direction.BottomToTop || direction == Direction.TopToBottom)
// Create a geometry that will be formed by all the bars
Geometry barGeometry = new RectangleGeometry();
switch (direction)
{ {
for (var index = 0; index < _points.Length; index++) case Direction.BottomToTop:
for (var index = 0; index < _lineValues.Count; index++)
{ {
var startPoint = new Point(index * 4 + 2 + parentX, _points[index].Y * 4 + parentY); var clipRect = new Rect((parentX + index)*4, parentY*4, 4, _lineValues[index]*4);
var endPoint = new Point(index * 4 + 2 + parentX, parentY); var barRect = new RectangleGeometry(clipRect);
var clip = new Rect(startPoint, endPoint); barGeometry = Geometry.Combine(barGeometry, barRect, CombineMode, Transform.Identity);
clip.Width = 4;
c.PushClip(new RectangleGeometry(new Rect(startPoint, endPoint)));
var point = new Point(index * 4 + 2 + parentX, _points[index].Y * 4 + parentY);
c.DrawLine(pen, startPoint, endPoint);
} }
} break;
else case Direction.TopToBottom:
for (var index = 0; index < _lineValues.Count; index++)
{ {
for (var index = 0; index < _points.Length; index++) var clipRect = new Rect((parentX + index)*4, parentY*4, 4, _lineValues[index]*4);
var barRect = new RectangleGeometry(clipRect);
barGeometry = Geometry.Combine(barGeometry, barRect, CombineMode, Transform.Identity);
}
break;
case Direction.LeftToRight:
for (var index = 0; index < _lineValues.Count; index++)
{ {
var point = new Point(_points[index].X * 4 + parentX, index * 4 + 2 + parentY); var clipRect = new Rect((parentX + index)*4, parentY*4, 4, _lineValues[index]*4);
c.DrawLine(pen, point, new Point(parentX, index * 4 + 2 + parentY)); var barRect = new RectangleGeometry(clipRect);
barGeometry = Geometry.Combine(barGeometry, barRect, CombineMode, Transform.Identity);
} }
break;
default:
for (var index = 0; index < _lineValues.Count; index++)
{
var clipRect = new Rect((parentX + index)*4, parentY*4, 4, _lineValues[index]*4);
var barRect = new RectangleGeometry(clipRect);
barGeometry = Geometry.Combine(barGeometry, barRect, CombineMode, Transform.Identity);
} }
break;
}
// Push the created geometry
c.PushClip(barGeometry);
BrushDraw(layerModel, c);
c.Pop();
} }
public void Update(LayerModel layerModel, ModuleDataModel dataModel, bool isPreview = false) public void Update(LayerModel layerModel, ModuleDataModel dataModel, bool isPreview = false)
@ -88,23 +116,18 @@ namespace Artemis.Profiles.Layers.Types.Audio
currentHeight = layerModel.Width; currentHeight = layerModel.Width;
} }
if (_lines != currentLines)
{
_lines = currentLines;
_points = new Point[_lines];
_lineSpectrum = _audioCapture.GetLineSpectrum(_lines, ScalingStrategy.Decibel);
}
// Let audio capture know it is being listened to // Let audio capture know it is being listened to
_audioCapture.Pulse(); _audioCapture.Pulse();
if (_lineSpectrum == null) if (_lines != currentLines || _lineSpectrum == null)
return; {
_lines = currentLines;
_lineSpectrum = _audioCapture.GetLineSpectrum(_lines, ScalingStrategy.Decibel);
}
if (direction == Direction.BottomToTop || direction == Direction.TopToBottom) var newLineValues = _lineSpectrum?.GetLineValues(currentHeight);
_lineSpectrum.UpdateLinesVertical(currentHeight, _points); if (newLineValues != null)
else _lineValues = newLineValues;
_lineSpectrum.UpdateLinesHorizontal(currentHeight, _points);
} }
public void SetupProperties(LayerModel layerModel) public void SetupProperties(LayerModel layerModel)
@ -126,5 +149,31 @@ namespace Artemis.Profiles.Layers.Types.Audio
return layerPropertiesViewModel; return layerPropertiesViewModel;
return new AudioPropertiesViewModel(layerEditorViewModel); return new AudioPropertiesViewModel(layerEditorViewModel);
} }
public void BrushDraw(LayerModel layerModel, DrawingContext c)
{
// If an animation is present, let it handle the drawing
if (layerModel.LayerAnimation != null && !(layerModel.LayerAnimation is NoneAnimation))
{
layerModel.LayerAnimation.Draw(layerModel, c);
return;
}
// Otherwise draw the rectangle with its layer.AppliedProperties dimensions and brush
var rect = layerModel.Properties.Contain
? layerModel.LayerRect()
: new Rect(layerModel.Properties.X*4, layerModel.Properties.Y*4,
layerModel.Properties.Width*4, layerModel.Properties.Height*4);
var clip = layerModel.LayerRect();
// Can't meddle with the original brush because it's frozen.
var brush = layerModel.Brush.Clone();
brush.Opacity = layerModel.Opacity;
c.PushClip(new RectangleGeometry(clip));
c.DrawRectangle(brush, null, rect);
c.Pop();
}
} }
} }