The fucking 3rd time i had to upload this project to git
This commit is contained in:
@ -0,0 +1,276 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
|
||||
* Contributors: https://github.com/Tayx94/graphy/graphs/contributors
|
||||
* Project: Graphy - Ultimate Stats Monitor
|
||||
* Date: 15-Dec-17
|
||||
* Studio: Tayx
|
||||
*
|
||||
* Git repo: https://github.com/Tayx94/graphy
|
||||
*
|
||||
* This project is released under the MIT license.
|
||||
* Attribution is not required, but it is always welcomed!
|
||||
* -------------------------------------*/
|
||||
|
||||
using Tayx.Graphy.Graph;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
public class G_AudioGraph : G_Graph
|
||||
{
|
||||
#region Methods -> Public
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
if (m_shaderGraph == null)
|
||||
// TODO: While Graphy is disabled (e.g. by default via Ctrl+H) and while in Editor after a Hot-Swap,
|
||||
// the OnApplicationFocus calls this while m_shaderGraph == null, throwing a NullReferenceException
|
||||
return;
|
||||
switch (m_graphyManager.GraphyMode)
|
||||
{
|
||||
case GraphyManager.Mode.FULL:
|
||||
m_shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeFull;
|
||||
m_shaderGraph.Image.material = new Material(ShaderFull);
|
||||
|
||||
m_shaderGraphHighestValues.ArrayMaxSize = G_GraphShader.ArrayMaxSizeFull;
|
||||
m_shaderGraphHighestValues.Image.material = new Material(ShaderFull);
|
||||
break;
|
||||
|
||||
case GraphyManager.Mode.LIGHT:
|
||||
m_shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeLight;
|
||||
m_shaderGraph.Image.material = new Material(ShaderLight);
|
||||
|
||||
m_shaderGraphHighestValues.ArrayMaxSize = G_GraphShader.ArrayMaxSizeLight;
|
||||
m_shaderGraphHighestValues.Image.material = new Material(ShaderLight);
|
||||
break;
|
||||
}
|
||||
|
||||
m_shaderGraph.InitializeShader();
|
||||
m_shaderGraphHighestValues.InitializeShader();
|
||||
|
||||
m_resolution = m_graphyManager.AudioGraphResolution;
|
||||
|
||||
CreatePoints();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
private void Init()
|
||||
{
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
|
||||
m_shaderGraph = new G_GraphShader
|
||||
{
|
||||
Image = m_imageGraph
|
||||
};
|
||||
|
||||
m_shaderGraphHighestValues = new G_GraphShader
|
||||
{
|
||||
Image = m_imageGraphHighestValues
|
||||
};
|
||||
|
||||
UpdateParameters();
|
||||
|
||||
m_isInitialized = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables -> Serialized Private
|
||||
|
||||
[SerializeField] private Image m_imageGraph;
|
||||
[SerializeField] private Image m_imageGraphHighestValues;
|
||||
|
||||
[SerializeField] private Shader ShaderFull;
|
||||
[SerializeField] private Shader ShaderLight;
|
||||
|
||||
[SerializeField] private bool m_isInitialized;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables -> Private
|
||||
|
||||
private GraphyManager m_graphyManager;
|
||||
|
||||
private G_AudioMonitor m_audioMonitor;
|
||||
|
||||
private int m_resolution = 40;
|
||||
|
||||
private G_GraphShader m_shaderGraph;
|
||||
private G_GraphShader m_shaderGraphHighestValues;
|
||||
|
||||
private float[] m_graphArray;
|
||||
private float[] m_graphArrayHighestValue;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
/* ----- NOTE: ----------------------------
|
||||
* We used to Init() here regardless of
|
||||
* whether this module was enabled.
|
||||
* The reason we don't Init() here
|
||||
* anymore is that some users are on
|
||||
* platforms that do not support the arrays
|
||||
* in the Shaders.
|
||||
*
|
||||
* See: https://github.com/Tayx94/graphy/issues/17
|
||||
*
|
||||
* Even though we don't Init() competely
|
||||
* here anymore, we still need
|
||||
* m_audioMonitor for in Update()
|
||||
* --------------------------------------*/
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (m_audioMonitor.SpectrumDataAvailable) UpdateGraph();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Protected Override
|
||||
|
||||
protected override void UpdateGraph()
|
||||
{
|
||||
// Since we no longer initialize by default OnEnable(),
|
||||
// we need to check here, and Init() if needed
|
||||
if (!m_isInitialized) Init();
|
||||
|
||||
var incrementPerIteration = Mathf.FloorToInt(m_audioMonitor.Spectrum.Length / (float)m_resolution);
|
||||
|
||||
// Current values -------------------------
|
||||
|
||||
for (var i = 0; i <= m_resolution - 1; i++)
|
||||
{
|
||||
float currentValue = 0;
|
||||
|
||||
for (var j = 0; j < incrementPerIteration; j++)
|
||||
currentValue += m_audioMonitor.Spectrum[i * incrementPerIteration + j];
|
||||
|
||||
// Uses 3 values for each bar to accomplish that look
|
||||
|
||||
if ((i + 1) % 3 == 0 && i > 1)
|
||||
{
|
||||
var value =
|
||||
(
|
||||
m_audioMonitor.dBNormalized(m_audioMonitor.lin2dB(currentValue / incrementPerIteration))
|
||||
+ m_graphArray[i - 1]
|
||||
+ m_graphArray[i - 2]
|
||||
) / 3;
|
||||
|
||||
m_graphArray[i] = value;
|
||||
m_graphArray[i - 1] = value;
|
||||
m_graphArray[i - 2] =
|
||||
-1; // Always set the third one to -1 to leave gaps in the graph and improve readability
|
||||
}
|
||||
else
|
||||
{
|
||||
m_graphArray[i] =
|
||||
m_audioMonitor.dBNormalized(m_audioMonitor.lin2dB(currentValue / incrementPerIteration));
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i <= m_resolution - 1; i++) m_shaderGraph.ShaderArrayValues[i] = m_graphArray[i];
|
||||
|
||||
m_shaderGraph.UpdatePoints();
|
||||
|
||||
|
||||
// Highest values -------------------------
|
||||
|
||||
for (var i = 0; i <= m_resolution - 1; i++)
|
||||
{
|
||||
float currentValue = 0;
|
||||
|
||||
for (var j = 0; j < incrementPerIteration; j++)
|
||||
currentValue += m_audioMonitor.SpectrumHighestValues[i * incrementPerIteration + j];
|
||||
|
||||
// Uses 3 values for each bar to accomplish that look
|
||||
|
||||
if ((i + 1) % 3 == 0 && i > 1)
|
||||
{
|
||||
var value =
|
||||
(
|
||||
m_audioMonitor.dBNormalized(m_audioMonitor.lin2dB(currentValue / incrementPerIteration))
|
||||
+ m_graphArrayHighestValue[i - 1]
|
||||
+ m_graphArrayHighestValue[i - 2]
|
||||
) / 3;
|
||||
|
||||
m_graphArrayHighestValue[i] = value;
|
||||
m_graphArrayHighestValue[i - 1] = value;
|
||||
m_graphArrayHighestValue[i - 2] =
|
||||
-1; // Always set the third one to -1 to leave gaps in the graph and improve readability
|
||||
}
|
||||
else
|
||||
{
|
||||
m_graphArrayHighestValue[i] =
|
||||
m_audioMonitor.dBNormalized(m_audioMonitor.lin2dB(currentValue / incrementPerIteration));
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i <= m_resolution - 1; i++)
|
||||
m_shaderGraphHighestValues.ShaderArrayValues[i] = m_graphArrayHighestValue[i];
|
||||
|
||||
m_shaderGraphHighestValues.UpdatePoints();
|
||||
}
|
||||
|
||||
protected override void CreatePoints()
|
||||
{
|
||||
// Init Arrays
|
||||
if (m_shaderGraph.ShaderArrayValues == null || m_shaderGraph.ShaderArrayValues.Length != m_resolution)
|
||||
{
|
||||
m_graphArray = new float[m_resolution];
|
||||
m_graphArrayHighestValue = new float[m_resolution];
|
||||
m_shaderGraph.ShaderArrayValues = new float[m_resolution];
|
||||
m_shaderGraphHighestValues.ShaderArrayValues = new float[m_resolution];
|
||||
}
|
||||
|
||||
for (var i = 0; i < m_resolution; i++)
|
||||
{
|
||||
m_shaderGraph.ShaderArrayValues[i] = 0;
|
||||
m_shaderGraphHighestValues.ShaderArrayValues[i] = 0;
|
||||
}
|
||||
|
||||
// Color
|
||||
m_shaderGraph.GoodColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraph.CautionColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraph.CriticalColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraph.UpdateColors();
|
||||
|
||||
m_shaderGraphHighestValues.GoodColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraphHighestValues.CautionColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraphHighestValues.CriticalColor = m_graphyManager.AudioGraphColor;
|
||||
m_shaderGraphHighestValues.UpdateColors();
|
||||
|
||||
// Threshold
|
||||
m_shaderGraph.GoodThreshold = 0;
|
||||
m_shaderGraph.CautionThreshold = 0;
|
||||
m_shaderGraph.UpdateThresholds();
|
||||
|
||||
m_shaderGraphHighestValues.GoodThreshold = 0;
|
||||
m_shaderGraphHighestValues.CautionThreshold = 0;
|
||||
m_shaderGraphHighestValues.UpdateThresholds();
|
||||
|
||||
// Update Array
|
||||
m_shaderGraph.UpdateArray();
|
||||
m_shaderGraphHighestValues.UpdateArray();
|
||||
|
||||
// Average
|
||||
m_shaderGraph.Average = 0;
|
||||
m_shaderGraph.UpdateAverage();
|
||||
|
||||
m_shaderGraphHighestValues.Average = 0;
|
||||
m_shaderGraphHighestValues.UpdateAverage();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2d6ca19dafe21b4b983441274e7f12a
|
||||
timeCreated: 1513169449
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,214 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
|
||||
* Contributors: https://github.com/Tayx94/graphy/graphs/contributors
|
||||
* Project: Graphy - Ultimate Stats Monitor
|
||||
* Date: 03-Jan-18
|
||||
* Studio: Tayx
|
||||
*
|
||||
* Git repo: https://github.com/Tayx94/graphy
|
||||
*
|
||||
* This project is released under the MIT license.
|
||||
* Attribution is not required, but it is always welcomed!
|
||||
* -------------------------------------*/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Tayx.Graphy.UI;
|
||||
using Tayx.Graphy.Utils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
public class G_AudioManager : MonoBehaviour, IMovable, IModifiableState
|
||||
{
|
||||
#region Variables -> Serialized Private
|
||||
|
||||
[SerializeField] private GameObject m_audioGraphGameObject;
|
||||
[SerializeField] private Text m_audioDbText;
|
||||
|
||||
[SerializeField] private List<Image> m_backgroundImages = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables -> Private
|
||||
|
||||
private GraphyManager m_graphyManager;
|
||||
|
||||
private G_AudioGraph m_audioGraph;
|
||||
private G_AudioMonitor m_audioMonitor;
|
||||
private G_AudioText m_audioText;
|
||||
|
||||
private RectTransform m_rectTransform;
|
||||
|
||||
private readonly List<GameObject> m_childrenGameObjects = new();
|
||||
|
||||
private GraphyManager.ModuleState m_previousModuleState = GraphyManager.ModuleState.FULL;
|
||||
private GraphyManager.ModuleState m_currentModuleState = GraphyManager.ModuleState.FULL;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UpdateParameters();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Public
|
||||
|
||||
public void SetPosition(GraphyManager.ModulePosition newModulePosition)
|
||||
{
|
||||
var xSideOffset = Mathf.Abs(m_rectTransform.anchoredPosition.x);
|
||||
var ySideOffset = Mathf.Abs(m_rectTransform.anchoredPosition.y);
|
||||
|
||||
m_audioDbText.alignment = TextAnchor.UpperRight;
|
||||
|
||||
switch (newModulePosition)
|
||||
{
|
||||
case GraphyManager.ModulePosition.TOP_LEFT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.up;
|
||||
m_rectTransform.anchorMin = Vector2.up;
|
||||
m_rectTransform.anchoredPosition = new Vector2(xSideOffset, -ySideOffset);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.TOP_RIGHT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.one;
|
||||
m_rectTransform.anchorMin = Vector2.one;
|
||||
m_rectTransform.anchoredPosition = new Vector2(-xSideOffset, -ySideOffset);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.BOTTOM_LEFT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.zero;
|
||||
m_rectTransform.anchorMin = Vector2.zero;
|
||||
m_rectTransform.anchoredPosition = new Vector2(xSideOffset, ySideOffset);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.BOTTOM_RIGHT:
|
||||
|
||||
m_rectTransform.anchorMax = Vector2.right;
|
||||
m_rectTransform.anchorMin = Vector2.right;
|
||||
m_rectTransform.anchoredPosition = new Vector2(-xSideOffset, ySideOffset);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModulePosition.FREE:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetState(GraphyManager.ModuleState state, bool silentUpdate = false)
|
||||
{
|
||||
if (!silentUpdate) m_previousModuleState = m_currentModuleState;
|
||||
|
||||
m_currentModuleState = state;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case GraphyManager.ModuleState.FULL:
|
||||
gameObject.SetActive(true);
|
||||
m_childrenGameObjects.SetAllActive(true);
|
||||
SetGraphActive(true);
|
||||
|
||||
if (m_graphyManager.Background)
|
||||
m_backgroundImages.SetOneActive(0);
|
||||
else
|
||||
m_backgroundImages.SetAllActive(false);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModuleState.TEXT:
|
||||
case GraphyManager.ModuleState.BASIC:
|
||||
gameObject.SetActive(true);
|
||||
m_childrenGameObjects.SetAllActive(true);
|
||||
SetGraphActive(false);
|
||||
|
||||
if (m_graphyManager.Background)
|
||||
m_backgroundImages.SetOneActive(1);
|
||||
else
|
||||
m_backgroundImages.SetAllActive(false);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModuleState.BACKGROUND:
|
||||
gameObject.SetActive(true);
|
||||
SetGraphActive(false);
|
||||
m_childrenGameObjects.SetAllActive(false);
|
||||
|
||||
m_backgroundImages.SetAllActive(false);
|
||||
|
||||
break;
|
||||
|
||||
case GraphyManager.ModuleState.OFF:
|
||||
gameObject.SetActive(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void RestorePreviousState()
|
||||
{
|
||||
SetState(m_previousModuleState);
|
||||
}
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
foreach (var image in m_backgroundImages) image.color = m_graphyManager.BackgroundColor;
|
||||
|
||||
m_audioGraph.UpdateParameters();
|
||||
m_audioMonitor.UpdateParameters();
|
||||
m_audioText.UpdateParameters();
|
||||
|
||||
SetState(m_graphyManager.AudioModuleState);
|
||||
}
|
||||
|
||||
public void RefreshParameters()
|
||||
{
|
||||
foreach (var image in m_backgroundImages) image.color = m_graphyManager.BackgroundColor;
|
||||
|
||||
m_audioGraph.UpdateParameters();
|
||||
m_audioMonitor.UpdateParameters();
|
||||
m_audioText.UpdateParameters();
|
||||
|
||||
SetState(m_currentModuleState, true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
private void Init()
|
||||
{
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
m_rectTransform = GetComponent<RectTransform>();
|
||||
|
||||
m_audioGraph = GetComponent<G_AudioGraph>();
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
m_audioText = GetComponent<G_AudioText>();
|
||||
|
||||
foreach (Transform child in transform)
|
||||
if (child.parent == transform)
|
||||
m_childrenGameObjects.Add(child.gameObject);
|
||||
}
|
||||
|
||||
private void SetGraphActive(bool active)
|
||||
{
|
||||
m_audioGraph.enabled = active;
|
||||
m_audioGraphGameObject.SetActive(active);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c0448d8db852b54480670d291c04f1a
|
||||
timeCreated: 1514998347
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,195 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
|
||||
* Contributors: https://github.com/Tayx94/graphy/graphs/contributors
|
||||
* Project: Graphy - Ultimate Stats Monitor
|
||||
* Date: 15-Dec-17
|
||||
* Studio: Tayx
|
||||
*
|
||||
* Git repo: https://github.com/Tayx94/graphy
|
||||
*
|
||||
* This project is released under the MIT license.
|
||||
* Attribution is not required, but it is always welcomed!
|
||||
* -------------------------------------*/
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
/// <summary>
|
||||
/// Note: this class only works with Unity's AudioListener.
|
||||
/// If you're using a custom audio engine (like FMOD or WWise) it won't work,
|
||||
/// although you can always adapt it.
|
||||
/// </summary>
|
||||
public class G_AudioMonitor : MonoBehaviour
|
||||
{
|
||||
#region Variables -> Private
|
||||
|
||||
private const float m_refValue = 1f;
|
||||
|
||||
private GraphyManager m_graphyManager;
|
||||
|
||||
private AudioListener m_audioListener;
|
||||
|
||||
private GraphyManager.LookForAudioListener m_findAudioListenerInCameraIfNull =
|
||||
GraphyManager.LookForAudioListener.ON_SCENE_LOAD;
|
||||
|
||||
private FFTWindow m_FFTWindow = FFTWindow.Blackman;
|
||||
|
||||
private int m_spectrumSize = 512;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties -> Public
|
||||
|
||||
/// <summary>
|
||||
/// Current audio spectrum from the specified AudioListener.
|
||||
/// </summary>
|
||||
public float[] Spectrum { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Highest audio spectrum from the specified AudioListener in the last few seconds.
|
||||
/// </summary>
|
||||
public float[] SpectrumHighestValues { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum DB registered in the current spectrum.
|
||||
/// </summary>
|
||||
public float MaxDB { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if there is a reference to the audio listener.
|
||||
/// </summary>
|
||||
public bool SpectrumDataAvailable => m_audioListener != null;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (m_audioListener != null)
|
||||
{
|
||||
// Use this data to calculate the dB value
|
||||
|
||||
AudioListener.GetOutputData(Spectrum, 0);
|
||||
|
||||
float sum = 0;
|
||||
|
||||
for (var i = 0; i < Spectrum.Length; i++) sum += Spectrum[i] * Spectrum[i]; // sum squared samples
|
||||
|
||||
var rmsValue = Mathf.Sqrt(sum / Spectrum.Length); // rms = square root of average
|
||||
|
||||
MaxDB = 20 * Mathf.Log10(rmsValue / m_refValue); // calculate dB
|
||||
|
||||
if (MaxDB < -80) MaxDB = -80; // clamp it to -80dB min
|
||||
|
||||
// Use this data to draw the spectrum in the graphs
|
||||
|
||||
AudioListener.GetSpectrumData(Spectrum, 0, m_FFTWindow);
|
||||
|
||||
for (var i = 0; i < Spectrum.Length; i++)
|
||||
// Update the highest value if its lower than the current one
|
||||
if (Spectrum[i] > SpectrumHighestValues[i])
|
||||
SpectrumHighestValues[i] = Spectrum[i];
|
||||
|
||||
// Slowly lower the value
|
||||
else
|
||||
SpectrumHighestValues[i] = Mathf.Clamp
|
||||
(
|
||||
SpectrumHighestValues[i] - SpectrumHighestValues[i] * Time.deltaTime * 2,
|
||||
0,
|
||||
1
|
||||
);
|
||||
}
|
||||
else if (m_audioListener == null
|
||||
&& m_findAudioListenerInCameraIfNull == GraphyManager.LookForAudioListener.ALWAYS)
|
||||
{
|
||||
m_audioListener = FindAudioListener();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Public
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
m_findAudioListenerInCameraIfNull = m_graphyManager.FindAudioListenerInCameraIfNull;
|
||||
|
||||
m_audioListener = m_graphyManager.AudioListener;
|
||||
m_FFTWindow = m_graphyManager.FftWindow;
|
||||
m_spectrumSize = m_graphyManager.SpectrumSize;
|
||||
|
||||
if (m_audioListener == null
|
||||
&& m_findAudioListenerInCameraIfNull != GraphyManager.LookForAudioListener.NEVER)
|
||||
m_audioListener = FindAudioListener();
|
||||
|
||||
Spectrum = new float[m_spectrumSize];
|
||||
SpectrumHighestValues = new float[m_spectrumSize];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts spectrum values to decibels using logarithms.
|
||||
/// </summary>
|
||||
/// <param name="linear"></param>
|
||||
/// <returns></returns>
|
||||
public float lin2dB(float linear)
|
||||
{
|
||||
return Mathf.Clamp(Mathf.Log10(linear) * 20.0f, -160.0f, 0.0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Normalizes a value in decibels between 0-1.
|
||||
/// </summary>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
public float dBNormalized(float db)
|
||||
{
|
||||
return (db + 160f) / 160f;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
/// <summary>
|
||||
/// Tries to find an audio listener in the main camera.
|
||||
/// </summary>
|
||||
private AudioListener FindAudioListener()
|
||||
{
|
||||
var mainCamera = Camera.main;
|
||||
|
||||
if (mainCamera != null && mainCamera.TryGetComponent(out AudioListener audioListener)) return audioListener;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
|
||||
{
|
||||
if (m_findAudioListenerInCameraIfNull == GraphyManager.LookForAudioListener.ON_SCENE_LOAD)
|
||||
m_audioListener = FindAudioListener();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
UpdateParameters();
|
||||
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2216f4eff6a7a8a43b38b180fdd2fd9e
|
||||
timeCreated: 1513377074
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,90 @@
|
||||
/* ---------------------------------------
|
||||
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
|
||||
* Contributors: https://github.com/Tayx94/graphy/graphs/contributors
|
||||
* Project: Graphy - Ultimate Stats Monitor
|
||||
* Date: 15-Dec-17
|
||||
* Studio: Tayx
|
||||
*
|
||||
* Git repo: https://github.com/Tayx94/graphy
|
||||
*
|
||||
* This project is released under the MIT license.
|
||||
* Attribution is not required, but it is always welcomed!
|
||||
* -------------------------------------*/
|
||||
|
||||
using Tayx.Graphy.Utils.NumString;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Tayx.Graphy.Audio
|
||||
{
|
||||
public class G_AudioText : MonoBehaviour
|
||||
{
|
||||
#region Variables -> Serialized Private
|
||||
|
||||
[SerializeField] private Text m_DBText;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Public
|
||||
|
||||
public void UpdateParameters()
|
||||
{
|
||||
m_updateRate = m_graphyManager.AudioTextUpdateRate;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Private
|
||||
|
||||
private void Init()
|
||||
{
|
||||
G_IntString.Init(-80, 0); // dB range
|
||||
|
||||
m_graphyManager = transform.root.GetComponentInChildren<GraphyManager>();
|
||||
|
||||
m_audioMonitor = GetComponent<G_AudioMonitor>();
|
||||
|
||||
UpdateParameters();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Variables -> Private
|
||||
|
||||
private GraphyManager m_graphyManager;
|
||||
|
||||
private G_AudioMonitor m_audioMonitor;
|
||||
|
||||
private int m_updateRate = 4;
|
||||
|
||||
private float m_deltaTimeOffset;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods -> Unity Callbacks
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (m_audioMonitor.SpectrumDataAvailable)
|
||||
{
|
||||
if (m_deltaTimeOffset > 1f / m_updateRate)
|
||||
{
|
||||
m_deltaTimeOffset = 0f;
|
||||
|
||||
m_DBText.text = Mathf.Clamp((int)m_audioMonitor.MaxDB, -80, 0).ToStringNonAlloc();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_deltaTimeOffset += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 766a588f9a6cb55499c66ea772072e11
|
||||
timeCreated: 1513377063
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user