Skip to content

Commit

Permalink
Correct dBFS calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Jul 19, 2017
1 parent d78cc66 commit 477c01a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
12 changes: 10 additions & 2 deletions Assets/Lasp/AudioInput.cs
Expand Up @@ -21,7 +21,8 @@ public static float GetPeakLevel(FilterType filter)
// Returns the peak level during the last frame in dBFS.
public static float GetPeakLevelDecibel(FilterType filter)
{
return Mathf.Log10(GetPeakLevel(filter)) * 10;
// Full scale square wave = 0 dBFS : refLevel = 1
return ConvertToDecibel(GetPeakLevel(filter), 1);
}

// Calculates the RMS level of the last frame.
Expand All @@ -34,7 +35,8 @@ public static float CalculateRMS(FilterType filter)
// Calculates the RMS level of the last frame in dBFS.
public static float CalculateRMSDecibel(FilterType filter)
{
return Mathf.Log10(CalculateRMS(filter)) * 10;
// Full scale sin wave = 0 dBFS : refLevel = 1/sqrt(2)
return ConvertToDecibel(CalculateRMS(filter), 0.7071f);
}

// Retrieve and copy the waveform.
Expand All @@ -52,6 +54,12 @@ public static void RetrieveWaveform(FilterType filter, float[] dest)
static FilterBlock[] _filterBank;
static int _lastUpdateFrame;

static float ConvertToDecibel(float level, float refLevel)
{
const float zeroOffset = 1.5849e-13f;
return 20 * Mathf.Log10(level / refLevel + zeroOffset);
}

static void Initialize()
{
_stream = new Lasp.LaspStream();
Expand Down
10 changes: 5 additions & 5 deletions Assets/Test/Particles.unity
Expand Up @@ -138,7 +138,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_filterType: 3
_amplify: 12
_amplify: 20
--- !u!199 &144958675
ParticleSystemRenderer:
serializedVersion: 4
Expand Down Expand Up @@ -250,7 +250,7 @@ ParticleSystem:
moveWithTransform: 0
moveWithCustomTransform: {fileID: 0}
scalingMode: 1
randomSeed: -1897777151
randomSeed: -240977648
InitialModule:
serializedVersion: 3
enabled: 1
Expand Down Expand Up @@ -3607,7 +3607,7 @@ ParticleSystem:
moveWithTransform: 0
moveWithCustomTransform: {fileID: 0}
scalingMode: 1
randomSeed: 1531819271
randomSeed: 949519521
InitialModule:
serializedVersion: 3
enabled: 1
Expand Down Expand Up @@ -6847,7 +6847,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_filterType: 1
_amplify: 4
_amplify: 8
--- !u!1 &739515039
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -7032,7 +7032,7 @@ ParticleSystem:
moveWithTransform: 0
moveWithCustomTransform: {fileID: 0}
scalingMode: 1
randomSeed: -87322421
randomSeed: 1170090217
InitialModule:
serializedVersion: 3
enabled: 1
Expand Down
6 changes: 3 additions & 3 deletions Assets/Test/Test.unity
Expand Up @@ -210,7 +210,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_filterType: 3
_amplify: 10
_amplify: 16
_peakIndicator: {fileID: 182632196}
_rmsIndicator: {fileID: 462222369}
_lineMaterial: {fileID: 10306, guid: 0000000000000000f000000000000000, type: 0}
Expand Down Expand Up @@ -346,7 +346,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_filterType: 2
_amplify: 5
_amplify: 4
_peakIndicator: {fileID: 2026675033}
_rmsIndicator: {fileID: 1680186477}
_lineMaterial: {fileID: 10306, guid: 0000000000000000f000000000000000, type: 0}
Expand Down Expand Up @@ -1140,7 +1140,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_filterType: 1
_amplify: 5
_amplify: 8
_peakIndicator: {fileID: 1106074908}
_rmsIndicator: {fileID: 1070605825}
_lineMaterial: {fileID: 10306, guid: 0000000000000000f000000000000000, type: 0}
Expand Down
14 changes: 9 additions & 5 deletions Assets/Test/Tester.cs
Expand Up @@ -4,11 +4,12 @@
public class Tester : MonoBehaviour
{
[SerializeField] Lasp.FilterType _filterType;
[SerializeField] float _amplify = 1;
[SerializeField] Transform _peakIndicator;
[SerializeField] Transform _rmsIndicator;
[SerializeField] Material _lineMaterial;

const float kSilence = -40; // -40 dBFS = silence

float[] _waveform;
List<Vector3> _vertices;
Mesh _mesh;
Expand All @@ -30,12 +31,15 @@ void OnDestroy()

void Update()
{
var peak = Lasp.AudioInput.GetPeakLevelDecibel(_filterType) + _amplify;
var rms = Lasp.AudioInput.CalculateRMSDecibel(_filterType) + _amplify;
var peak = Lasp.AudioInput.GetPeakLevelDecibel(_filterType);
var rms = Lasp.AudioInput.CalculateRMSDecibel(_filterType);
Lasp.AudioInput.RetrieveWaveform(_filterType, _waveform);

_peakIndicator.localScale = new Vector3(1, Mathf.Clamp01(1 + peak / 10), 1);
_rmsIndicator.localScale = new Vector3(1, Mathf.Clamp01(1 + rms / 10), 1);
peak = Mathf.Clamp01(1 - peak / kSilence);
rms = Mathf.Clamp01(1 - rms / kSilence);

_peakIndicator.localScale = new Vector3(1, peak, 1);
_rmsIndicator.localScale = new Vector3(1, rms, 1);

UpdateMeshWithWaveform();
Graphics.DrawMesh(_mesh, transform.localToWorldMatrix, _lineMaterial, gameObject.layer);
Expand Down

0 comments on commit 477c01a

Please sign in to comment.