init
This commit is contained in:
commit
e58a138bea
176 files changed
+6194
No files matched your search
@@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
public abstract class AudioVisualizationEffect : MonoBehaviour
|
||||
{
|
||||
// Inspector Properties
|
||||
public WasapiAudioSource WasapiAudioSource;
|
||||
|
||||
[SerializeReference]
|
||||
[SerializeReferenceButton]
|
||||
public List<SpectrumTransformer> Transformers = new List<SpectrumTransformer>();
|
||||
|
||||
[SpectrumDataPreview]
|
||||
public SpectrumData Preview;
|
||||
|
||||
protected int SpectrumSize { get; private set; }
|
||||
|
||||
public virtual void Awake()
|
||||
{
|
||||
if (WasapiAudioSource == null)
|
||||
{
|
||||
Debug.Log("You must set a WasapiAudioSource");
|
||||
return;
|
||||
}
|
||||
|
||||
SpectrumSize = WasapiAudioSource.SpectrumSize;
|
||||
Preview = new SpectrumData();
|
||||
}
|
||||
|
||||
public float[] GetSpectrumData()
|
||||
{
|
||||
if (WasapiAudioSource == null)
|
||||
{
|
||||
Debug.Log("You must set a WasapiAudioSource");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get raw / unmodified spectrum data
|
||||
var spectrumData = WasapiAudioSource.GetSpectrumData();
|
||||
|
||||
// Run spectrum data through all configured transformers
|
||||
if (Transformers != null && Transformers.Count > 0)
|
||||
{
|
||||
foreach (var transformer in Transformers)
|
||||
{
|
||||
spectrumData = transformer.Transform(spectrumData);
|
||||
}
|
||||
}
|
||||
|
||||
Preview.Values = spectrumData;
|
||||
|
||||
return spectrumData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba1b44ffd6d8856448e4ae29b67ca69d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
//using UnityEngine;
|
||||
|
||||
//namespace Assets.WasapiAudio.Scripts.Unity
|
||||
//{
|
||||
// [CreateAssetMenu(fileName = "Audio Visualization Profile", menuName = "Audio/Audio Visualization Profile", order = 1)]
|
||||
// public class AudioVisualizationProfile : ScriptableObject
|
||||
// {
|
||||
// // Inspector Fields
|
||||
// public AnimationCurve ScaleCurve;
|
||||
// [Range(1, 50)]
|
||||
// public int AudioSmoothingIterations = 5;
|
||||
// [Range(1, 2)]
|
||||
// public float MinMaxThreshold = 1.2f;
|
||||
// public float MinScale = 0.0f;
|
||||
// public float MaxScale = 1.0f;
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ceabdd331bee1a8459e8b85d942a772f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
public abstract class AudioVisualizationSpectrumBandEffect : AudioVisualizationEffect
|
||||
{
|
||||
// Inspector properties
|
||||
public int SpectrumBand;
|
||||
|
||||
protected float GetSpectrumBandData()
|
||||
{
|
||||
return GetSpectrumData()[SpectrumBand];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b411fd36109dfe746be0478532cc7bc4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
[Serializable]
|
||||
public class SpectrumData
|
||||
{
|
||||
public float[] Values;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c4ddc80a206711b4b8c119fec4626751
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class SpectrumDataPreviewAttribute : PropertyAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 87b43e36cb5d1a64bb463e84f5e104a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
[Serializable]
|
||||
public abstract class SpectrumTransformer
|
||||
{
|
||||
private float[] _input;
|
||||
private float[] _output;
|
||||
|
||||
public bool Enabled = true;
|
||||
|
||||
protected float InputMin { get; private set; }
|
||||
protected float InputMax { get; private set; }
|
||||
protected float InputAverage { get; private set; }
|
||||
protected float InputRange { get; private set; }
|
||||
|
||||
public float[] Transform(float[] input)
|
||||
{
|
||||
_input = input;
|
||||
|
||||
if (_output == null || _output.Length != _input.Length)
|
||||
{
|
||||
_output = new float[_input.Length];
|
||||
}
|
||||
|
||||
InputMin = _input.Min();
|
||||
InputMax = _input.Max();
|
||||
InputAverage = _input.Average();
|
||||
InputRange = InputMax - InputMin;
|
||||
|
||||
if (Enabled)
|
||||
{
|
||||
PerformTransform(_input, ref _output);
|
||||
}
|
||||
|
||||
return Enabled ? _output : _input;
|
||||
}
|
||||
|
||||
protected abstract void PerformTransform(float[] input, ref float[] output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1bf2ffb4b98288345ac7353d5ca83108
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 723fc4f6d47c6234fbe8232c91accc22
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity.Transformers
|
||||
{
|
||||
[Serializable]
|
||||
public class CurveScaledSpectrumTransformer : SpectrumTransformer
|
||||
{
|
||||
public AnimationCurve ScaleCurve;
|
||||
|
||||
protected override void PerformTransform(float[] input, ref float[] output)
|
||||
{
|
||||
if (ScaleCurve == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var scaleStep = 1.0f / input.Length;
|
||||
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
var scaledValue = ScaleCurve.Evaluate(scaleStep * i) * input[i];
|
||||
|
||||
output[i] = scaledValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 565fed8317271174792674bb2bc0e5af
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity.Transformers
|
||||
{
|
||||
[Serializable]
|
||||
public class PercentileBoostSpectrumTransformer : SpectrumTransformer
|
||||
{
|
||||
private float[] _sortedPercentileArray;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float Percentile = 0.5f;
|
||||
public float BelowPercentileMultiplier = 0.5f;
|
||||
public float AbovePercentileMultiplier = 2.0f;
|
||||
|
||||
protected override void PerformTransform(float[] input, ref float[] output)
|
||||
{
|
||||
if (_sortedPercentileArray == null || _sortedPercentileArray.Length != input.Length)
|
||||
{
|
||||
_sortedPercentileArray = new float[input.Length];
|
||||
}
|
||||
|
||||
input.CopyTo(_sortedPercentileArray, 0);
|
||||
|
||||
var percentile = GetPercentile(_sortedPercentileArray, Percentile);
|
||||
|
||||
for (int i = 0; i < input.Length; i++)
|
||||
{
|
||||
if (input[i] < percentile)
|
||||
{
|
||||
output[i] = input[i] * BelowPercentileMultiplier;
|
||||
}
|
||||
else
|
||||
{
|
||||
output[i] = input[i] * AbovePercentileMultiplier;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float GetPercentile(float[] sequence, float percentile)
|
||||
{
|
||||
Array.Sort(sequence);
|
||||
|
||||
var length = sequence.Length;
|
||||
var n = (length - 1) * percentile + 1;
|
||||
|
||||
// Another method: double n = (N + 1) * excelPercentile;
|
||||
if (n == 1.0f)
|
||||
{
|
||||
return sequence[0];
|
||||
}
|
||||
|
||||
if (n == length)
|
||||
{
|
||||
return sequence[length - 1];
|
||||
}
|
||||
|
||||
int k = (int)n;
|
||||
float d = n - k;
|
||||
|
||||
return sequence[k - 1] + d * (sequence[k] - sequence[k - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac3cd62c87d1ef54f90e71e71fe2000b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using Assets.WasapiAudio.Scripts.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity.Transformers
|
||||
{
|
||||
[Serializable]
|
||||
public class SmoothSpectrumTransformer : SpectrumTransformer
|
||||
{
|
||||
private SpectrumSmoother _smoother;
|
||||
|
||||
[Range(1, 50)]
|
||||
public byte Iterations = 1;
|
||||
|
||||
protected override void PerformTransform(float[] input, ref float[] output)
|
||||
{
|
||||
if (_smoother == null || _smoother.SmoothingIterations != Iterations)
|
||||
{
|
||||
_smoother = new SpectrumSmoother(input.Length, Iterations);
|
||||
}
|
||||
|
||||
output = _smoother.GetSpectrumData(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a86e967af4ca4f4ab907c1a6d5a77aa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.VFX;
|
||||
using UnityEngine.VFX.Utility;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
[AddComponentMenu("VFX/Property Binders/VFX Audio Spectrum Data Binder")]
|
||||
[VFXBinder("Audio/Audio Spectrum Data to AttributeMap")]
|
||||
public class VfxAudioSpectrumDataBinder : VfxAudioVisualizationEffect
|
||||
{
|
||||
[VFXPropertyBinding("System.UInt32"), SerializeField]
|
||||
protected ExposedProperty SpectrumSizeParameter = "SpectrumSize";
|
||||
|
||||
[VFXPropertyBinding("UnityEngine.Texture2D"), SerializeField]
|
||||
protected ExposedProperty SpectrumDataTextureParameter = "SpectrumDataTexture";
|
||||
|
||||
private Texture2D _texture;
|
||||
private Color[] _colorCache;
|
||||
|
||||
private void UpdateTexture()
|
||||
{
|
||||
if (_texture == null)
|
||||
{
|
||||
_texture = new Texture2D(SpectrumSize, 1, TextureFormat.RFloat, false);
|
||||
_texture.name = $"AudioSpectrumData{SpectrumSize}";
|
||||
_colorCache = new Color[SpectrumSize];
|
||||
}
|
||||
|
||||
var spectrumData = GetSpectrumData();
|
||||
|
||||
for (int i = 0; i < SpectrumSize; i++)
|
||||
{
|
||||
_colorCache[i] = new Color(spectrumData[i], 0, 0, 0);
|
||||
}
|
||||
|
||||
_texture.SetPixels(_colorCache);
|
||||
_texture.Apply();
|
||||
}
|
||||
|
||||
public override void UpdateBinding(VisualEffect component)
|
||||
{
|
||||
base.UpdateBinding(component);
|
||||
UpdateTexture();
|
||||
component.SetUInt(SpectrumSizeParameter, (uint) SpectrumSize);
|
||||
component.SetTexture(SpectrumDataTextureParameter, _texture);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "Audio Spectrum Data";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d899048990fdc6246a5f17ff39f840bb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.VFX;
|
||||
using UnityEngine.VFX.Utility;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
public abstract class VfxAudioVisualizationEffect : VFXBinderBase
|
||||
{
|
||||
// Inspector Properties
|
||||
public WasapiAudioSource WasapiAudioSource;
|
||||
|
||||
[SerializeReference]
|
||||
[SerializeReferenceButton]
|
||||
public List<SpectrumTransformer> Transformers = new List<SpectrumTransformer>();
|
||||
|
||||
[SpectrumDataPreview]
|
||||
public SpectrumData Preview;
|
||||
|
||||
protected int SpectrumSize { get; private set; }
|
||||
|
||||
public override bool IsValid(VisualEffect component)
|
||||
{
|
||||
return WasapiAudioSource != null;
|
||||
}
|
||||
|
||||
public override void UpdateBinding(VisualEffect component)
|
||||
{
|
||||
SpectrumSize = WasapiAudioSource.SpectrumSize;
|
||||
Preview = new SpectrumData();
|
||||
}
|
||||
|
||||
protected float[] GetSpectrumData()
|
||||
{
|
||||
// Get raw / unmodified spectrum data
|
||||
var spectrumData = WasapiAudioSource.GetSpectrumData();
|
||||
|
||||
// Run spectrum data through all configured transformers
|
||||
if (Transformers != null && Transformers.Count > 0)
|
||||
{
|
||||
foreach (var transformer in Transformers)
|
||||
{
|
||||
spectrumData = transformer.Transform(spectrumData);
|
||||
}
|
||||
}
|
||||
|
||||
Preview.Values = spectrumData;
|
||||
|
||||
return spectrumData;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4dc0fbf002e63e148b601b689dcc5ca7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
using Assets.WasapiAudio.Scripts.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
[ExecuteInEditMode]
|
||||
public class WasapiAudioSource : MonoBehaviour
|
||||
{
|
||||
private Core.WasapiAudio _wasapiAudio;
|
||||
private float[] _spectrumData;
|
||||
|
||||
// Inspector Properties
|
||||
public WasapiCaptureType CaptureType = WasapiCaptureType.Loopback;
|
||||
public int SpectrumSize = 32;
|
||||
public ScalingStrategy ScalingStrategy = ScalingStrategy.Sqrt;
|
||||
public int MinFrequency = 100;
|
||||
public int MaxFrequency = 20000;
|
||||
public WasapiAudioFilter[] Filters;
|
||||
|
||||
public void Awake()
|
||||
{
|
||||
// Setup loopback audio and start listening
|
||||
_wasapiAudio = new Core.WasapiAudio(CaptureType, SpectrumSize, ScalingStrategy, MinFrequency, MaxFrequency, Filters, spectrumData =>
|
||||
{
|
||||
_spectrumData = spectrumData;
|
||||
});
|
||||
|
||||
_wasapiAudio.StartListen();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public float[] GetSpectrumData()
|
||||
{
|
||||
return _spectrumData;
|
||||
}
|
||||
|
||||
public void OnApplicationQuit()
|
||||
{
|
||||
_wasapiAudio?.StopListen();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 120ca4c4f9442e14db8c72acac5da136
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user