sync capture type with preferences
This commit is contained in:
1 parent
8e06a0467a
commit
e78d9c9d48
13 files changed
+900
-971
No files matched your search
@@ -10,10 +10,18 @@ public class AudioSource : MonoBehaviour {
|
||||
public ScalingStrategy ScalingStrategy = ScalingStrategy.Sqrt;
|
||||
public WasapiAudioFilter[] Filters;
|
||||
|
||||
public Preferences preferences;
|
||||
|
||||
private WasapiAudio _wasapiAudio;
|
||||
private float[] _spectrumData;
|
||||
|
||||
public void Awake() {
|
||||
public void Start() {
|
||||
CaptureType = preferences.data.useLoopback
|
||||
? WasapiCaptureType.Loopback
|
||||
: WasapiCaptureType.Microphone;
|
||||
|
||||
Debug.Log($"Pref: {preferences.data.useLoopback} {CaptureType}");
|
||||
|
||||
Initialise();
|
||||
}
|
||||
|
||||
@@ -39,8 +47,10 @@ public class AudioSource : MonoBehaviour {
|
||||
public void ToggleCaptureType() {
|
||||
if (CaptureType == WasapiCaptureType.Loopback) {
|
||||
CaptureType = WasapiCaptureType.Microphone;
|
||||
preferences.SetUseLoopback(false);
|
||||
} else {
|
||||
CaptureType = WasapiCaptureType.Loopback;
|
||||
preferences.SetUseLoopback(true);
|
||||
}
|
||||
|
||||
Initialise();
|
||||
|
||||
@@ -5,6 +5,7 @@ using UnityEngine;
|
||||
public class PreferenceData {
|
||||
// Preference Fields
|
||||
public int themeIndex;
|
||||
public bool useLoopback;
|
||||
|
||||
// Others
|
||||
string _path;
|
||||
@@ -12,6 +13,7 @@ public class PreferenceData {
|
||||
public PreferenceData(string path) {
|
||||
_path = path;
|
||||
themeIndex = 0;
|
||||
useLoopback = true;
|
||||
}
|
||||
|
||||
public PreferenceData Load() {
|
||||
|
||||
@@ -19,6 +19,11 @@ public class Preferences : MonoBehaviour {
|
||||
data.Save();
|
||||
}
|
||||
|
||||
public void SetUseLoopback(bool v) {
|
||||
data.useLoopback = v;
|
||||
data.Save();
|
||||
}
|
||||
|
||||
public Gradient Theme() {
|
||||
return themeSelector.CurrentTheme();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.VFX;
|
||||
using UnityEngine.VFX.Utility;
|
||||
@@ -5,52 +6,56 @@ using UnityEngine.VFX.Utility;
|
||||
namespace Assets.WasapiAudio.Scripts.Unity
|
||||
{
|
||||
[AddComponentMenu("VFX/Property Binders/Spectre - VFX SDF Binder")]
|
||||
[VFXBinder("Audio/Audio Spectrum Data to 3d SDF AttributeMap")]
|
||||
public class SpectreVfxSdfBinder : VfxAudioVisualizationEffect
|
||||
{
|
||||
[VFXPropertyBinding("System.UInt32"), SerializeField]
|
||||
protected ExposedProperty SpectrumSizeParameter = "SpectrumSize";
|
||||
[VFXBinder("Kaiju/Kaiju - Spectrum to 3d SDF Binder")]
|
||||
public class SpectreVfxSdfBinder : VFXBinderBase {
|
||||
[SerializeReference]
|
||||
[SerializeReferenceButton]
|
||||
public List<SpectrumTransformer> Transformers = new List<SpectrumTransformer>();
|
||||
|
||||
[VFXPropertyBinding("UnityEngine.Texture3D"), SerializeField]
|
||||
protected ExposedProperty SpectrumDataTextureParameter = "SpectrumDataTexture";
|
||||
[SpectrumDataPreview]
|
||||
public SpectrumData Preview;
|
||||
|
||||
public AudioSource source;
|
||||
public bool symmetric = false;
|
||||
|
||||
private Texture3D _texture;
|
||||
private Color[] _colorCache;
|
||||
|
||||
private int z = 0;
|
||||
|
||||
private void UpdateTexture()
|
||||
{
|
||||
private void UpdateTexture() {
|
||||
if (_texture == null)
|
||||
{
|
||||
_texture = new Texture3D(SpectrumSize * 2, SpectrumSize * 2, SpectrumSize * 2, TextureFormat.RFloat, false);
|
||||
_texture.name = $"AudioSpectrumDataSDF{SpectrumSize}";
|
||||
_texture = new Texture3D(source.SpectrumSize * 2, source.SpectrumSize * 2, source.SpectrumSize * 2, TextureFormat.RFloat, false);
|
||||
_texture.wrapMode = TextureWrapMode.Clamp;
|
||||
_texture.name = $"AudioSpectrumDataSDF{source.SpectrumSize}";
|
||||
_colorCache = new Color[
|
||||
SpectrumSize * 2 *
|
||||
SpectrumSize * 2 *
|
||||
SpectrumSize * 2
|
||||
source.SpectrumSize * 2 *
|
||||
source.SpectrumSize * 2 *
|
||||
source.SpectrumSize * 2
|
||||
];
|
||||
}
|
||||
|
||||
var spectrumData = GetSpectrumData();
|
||||
if (spectrumData == null) return;
|
||||
|
||||
// go from 0 to end of spectrum
|
||||
for (int x = 0; x < SpectrumSize; x++) {
|
||||
float height = spectrumData[x] * SpectrumSize * 2;
|
||||
for (int x = 0; x < source.SpectrumSize; x++) {
|
||||
float height = spectrumData[x] * source.SpectrumSize * 2;
|
||||
|
||||
// go from 0 to double the spectrum height
|
||||
for (int y = 0; y < SpectrumSize * 2; y++) {
|
||||
for (int y = 0; y < source.SpectrumSize * 2; y++) {
|
||||
float distance = (y - height);
|
||||
|
||||
var z_part = z * (SpectrumSize * 2 * SpectrumSize * 2);
|
||||
var y_part = y * SpectrumSize * 2;
|
||||
var z_part = z * (source.SpectrumSize * 2 * source.SpectrumSize * 2);
|
||||
var y_part = y * source.SpectrumSize * 2;
|
||||
|
||||
_colorCache[z_part + y_part + (SpectrumSize + x)] = new Color(distance, 0, 0, 0);
|
||||
_colorCache[z_part + y_part + (SpectrumSize - (x + 1))] = new Color(distance, 0, 0, 0);
|
||||
_colorCache[z_part + y_part + (source.SpectrumSize + x)] = new Color(distance, 0, 0, 0);
|
||||
_colorCache[z_part + y_part + (source.SpectrumSize - (x + 1))] = new Color(distance, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
z = (z + 1) % (SpectrumSize * 2);
|
||||
z = (z + 1) % (source.SpectrumSize * 2);
|
||||
|
||||
// Debug.Log($"{z}");
|
||||
|
||||
@@ -58,16 +63,32 @@ namespace Assets.WasapiAudio.Scripts.Unity
|
||||
_texture.Apply();
|
||||
}
|
||||
|
||||
public override void UpdateBinding(VisualEffect component)
|
||||
{
|
||||
base.UpdateBinding(component);
|
||||
UpdateTexture();
|
||||
component.SetUInt(SpectrumSizeParameter, (uint) SpectrumSize);
|
||||
component.SetTexture(SpectrumDataTextureParameter, _texture);
|
||||
public override bool IsValid(VisualEffect component) {
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
protected float[] GetSpectrumData() {
|
||||
// Get raw / unmodified spectrum data
|
||||
var spectrumData = source.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;
|
||||
}
|
||||
|
||||
public override void UpdateBinding(VisualEffect component) {
|
||||
UpdateTexture();
|
||||
component.SetUInt("SpectrumSize", (uint) source.SpectrumSize);
|
||||
component.SetTexture("SpectrumDataTexture", _texture);
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return "Audio Spectrum Data";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ public class SpectrumShaderBinder : MonoBehaviour
|
||||
private void InitialiseTexture() {
|
||||
Debug.Log("Reinitialising spectrum texture...");
|
||||
_texture = new Texture2D(source.SpectrumSize, 1, TextureFormat.RFloat, false);
|
||||
_texture.wrapMode = TextureWrapMode.Clamp;
|
||||
_colorCache = new Color[source.SpectrumSize];
|
||||
_texture.name = $"AudioSpectrum{source.SpectrumSize}";
|
||||
}
|
||||
@@ -62,13 +63,13 @@ public class SpectrumShaderBinder : MonoBehaviour
|
||||
if (_texture == null) InitialiseTexture();
|
||||
|
||||
var spectrumData = GetSpectrumData();
|
||||
if (spectrumData == null) return;
|
||||
|
||||
for (int i = 0; i < source.SpectrumSize; i++) {
|
||||
_colorCache[i] = new Color(spectrumData[i], 0, 0, 0);
|
||||
}
|
||||
|
||||
_texture.SetPixels(_colorCache);
|
||||
_texture.wrapMode = TextureWrapMode.Clamp;
|
||||
_texture.Apply();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,9 @@ public class VfxSpectrumBinder : VFXBinderBase {
|
||||
|
||||
var spectrumData = GetSpectrumData();
|
||||
|
||||
// not sure why this can happen, something to do with source being loaded still maybe
|
||||
if (spectrumData == null) return;
|
||||
|
||||
if (symmetric) {
|
||||
for (int i = 0; i < source.SpectrumSize; i++) {
|
||||
_colorCache[source.SpectrumSize + i] = new Color(spectrumData[i], 0, 0, 0);
|
||||
@@ -73,6 +76,7 @@ public class VfxSpectrumBinder : VFXBinderBase {
|
||||
// Run spectrum data through all configured transformers
|
||||
if (Transformers != null && Transformers.Count > 0) {
|
||||
foreach (var transformer in Transformers) {
|
||||
if (spectrumData == null) continue;
|
||||
spectrumData = transformer.Transform(spectrumData);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user