Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this package are documented here. The format follows
- Mark SFX clips cut short by voice stealing with a red edge in the Mix Monitor ([#133](https://github.com/Ambiguous-Interactive/DxAudio/issues/133)).
- Show live stereo sample peaks with peak hold for the listener bus in the Mix Monitor ([#133](https://github.com/Ambiguous-Interactive/DxAudio/issues/133)).
- Show estimated master true peaks in dBTP in the Mix Monitor, including peaks between samples ([#133](https://github.com/Ambiguous-Interactive/DxAudio/issues/133)).
- Show short-term and integrated master loudness in LUFS on the Mix Monitor listener bus ([#133](https://github.com/Ambiguous-Interactive/DxAudio/issues/133)).
- Add DxAudio Problems with named validation rules, project severities, and shared or personal silences ([#135](https://github.com/Ambiguous-Interactive/DxAudio/issues/135)).
- Add a Stereo Width effect for DSP chains and graphs to narrow or widen stereo signals ([#27](https://github.com/Ambiguous-Interactive/DxAudio/issues/27)).
- Add a room Reverb effect for DSP chains and graphs with adjustable decay and wet level ([#27](https://github.com/Ambiguous-Interactive/DxAudio/issues/27)).
Expand Down
12 changes: 12 additions & 0 deletions Editor/DxAudioMixMonitorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ private static void DrawWaveform(in Rect rect, float[] peaks, float start, float
}
}

private static string FormatLoudness(double? lufs)
{
if (!lufs.HasValue)
{
return "-";
}

return double.IsNegativeInfinity(lufs.Value) ? "-\u221e" : lufs.Value.ToString("0.0");
}

public void CreateGUI()
{
DxAudioEditorTheme.Apply(rootVisualElement);
Expand Down Expand Up @@ -434,6 +444,8 @@ private void DrawMaster()
reading = (20f * Mathf.Log10(truePeak.Value)).ToString("0.0");
}
EditorGUILayout.LabelField("True peak (dBTP)", reading);
EditorGUILayout.LabelField("Short-term (LUFS)", FormatLoudness(_masterMeter.ShortTermLufs));
EditorGUILayout.LabelField("Integrated (LUFS)", FormatLoudness(_masterMeter.IntegratedLufs));
}

private void DrawMasterChannel(string label, MixMonitorMasterMeter.Channel channel)
Expand Down
21 changes: 18 additions & 3 deletions Editor/MixMonitorMasterMeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ internal sealed class MixMonitorMasterMeter

internal Channel Left => _left;
internal Channel Right => _right;
internal double? ShortTermLufs => HasSignal ? _loudness.ShortTerm : null;
internal double? IntegratedLufs => _loudness.Integrated;
internal float? TruePeak { get; private set; }
internal bool TruePeakOverrun { get; private set; }
internal bool IsAttached => _tap != null;
internal bool HasSignal { get; private set; }

private readonly MasterLoudness _loudness = new();
private AudioMixBus _bus;
private StereoPeakTap _tap;
private Channel _left;
Expand Down Expand Up @@ -49,17 +52,28 @@ internal void Sample(AudioMixBus bus, double now)
_bus = bus;
}

bool received = _tap.TryRead(out float left, out float right);
ReadCapturedAudio(_tap, now);
}

internal void ReadCapturedAudio(StereoPeakTap tap, double now)
{
bool received = tap.TryRead(out float left, out float right);
if (received)
{
_lastSignalTime = now;
}

HasSignal = received || (HasSignal && now < _lastSignalTime + SignalTimeoutSeconds);
bool hasSignal = received || (HasSignal && now < _lastSignalTime + SignalTimeoutSeconds);
if (HasSignal && !hasSignal)
{
_loudness.Reset();
}

HasSignal = hasSignal;
_left.Sample(received ? left : null, HasSignal, now);
_right.Sample(received ? right : null, HasSignal, now);

bool receivedTruePeak = _tap.TryReadTruePeak(out float peak, out bool overrun);
bool receivedTruePeak = tap.TryReadTruePeak(out float peak, out bool overrun, _loudness);
if (overrun)
{
TruePeakOverrun = true;
Expand Down Expand Up @@ -89,6 +103,7 @@ internal void Clear()
_left = default;
_right = default;
_truePeak = default;
_loudness.Reset();
TruePeak = null;
TruePeakOverrun = false;
}
Expand Down
60 changes: 45 additions & 15 deletions Runtime/Core/TruePeakTap.cs → Runtime/Core/ListenerMeterTap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,72 @@ namespace WallstopStudios.DxAudio
using System;
using System.Threading;

/// <summary>Moves listener blocks from the audio callback to the editor's true-peak filter.</summary>
internal sealed class TruePeakTap
/// <summary>Moves listener blocks from the audio callback to the editor meters.</summary>
internal sealed class ListenerMeterTap
{
// Over 300 ms at 48 kHz and eight channels, with a normal read every 50 ms.
private const int Capacity = 1 << 17;
private const int Mask = Capacity - 1;
private const int HeaderSize = 2;
private const int HeaderSize = 3;
private readonly float[] _queue = new float[Capacity];
private readonly TruePeakFilter _filter = new();
private long _format;
private int _writing;
private int _written;
private int _read;
private int _readRevision;
private int _reset;
private int _overrun;
private bool _gap;

internal void RequestReset()
internal ListenerMeterTap(int sampleRate)
{
Volatile.Write(ref _reset, 1);
_format = sampleRate;
}

internal void SetSampleRate(int sampleRate)
{
// Publish the rate and its revision together, including resets at the same rate.
int revision = unchecked((int)(Volatile.Read(ref _format) >> 32) + 1);
Volatile.Write(ref _format, ((long)revision << 32) | (uint)sampleRate);
}

internal void Enqueue(float[] samples, int channels)
{
int revision = (int)(Volatile.Read(ref _format) >> 32);
// ProcessBuffer can also be driven explicitly while Unity is delivering audio.
if (Interlocked.CompareExchange(ref _writing, 1, 0) != 0)
{
RequestReset();
Volatile.Write(ref _reset, 1);
Volatile.Write(ref _overrun, 1);
return;
}

try
{
WriteBlock(samples, channels);
WriteBlock(samples, channels, revision);
}
finally
{
Volatile.Write(ref _writing, 0);
}
}

internal bool TryRead(out float peak, out bool overrun)
internal bool TryRead(out float peak, out bool overrun, MasterLoudness loudness)
{
int end = Volatile.Read(ref _written);
long format = Volatile.Read(ref _format);
int revision = (int)(format >> 32);
int sampleRate = unchecked((int)format);
if (_readRevision != revision)
{
_filter.Reset();
loudness?.Reset();
_readRevision = revision;
}

int read = _read;
bool received = read != end;
bool received = false;
float maximum = 0f;
while (read != end)
{
Expand All @@ -60,20 +80,28 @@ internal bool TryRead(out float peak, out bool overrun)
continue;
}

if (channels < 0)
int count = (int)_queue[start + 1];
if (revision == BitConverter.SingleToInt32Bits(_queue[start + 2]))
{
_filter.Reset();
channels = -channels;
if (channels < 0)
{
_filter.Reset();
loudness?.Reset();
channels = -channels;
}

maximum = Math.Max(maximum, _filter.Observe(_queue, start + HeaderSize, count, channels));
loudness?.Observe(_queue, start + HeaderSize, count, channels, sampleRate);
received = true;
}

int count = (int)_queue[start + 1];
maximum = Math.Max(maximum, _filter.Observe(_queue, start + HeaderSize, count, channels));
read = unchecked(read + HeaderSize + count);
Volatile.Write(ref _read, read);
}

if (Interlocked.Exchange(ref _overrun, 0) == 1)
{
loudness?.Reset();
peak = 0f;
overrun = true;
return false;
Expand All @@ -84,11 +112,12 @@ internal bool TryRead(out float peak, out bool overrun)
return received;
}

private void WriteBlock(float[] samples, int channels)
private void WriteBlock(float[] samples, int channels, int revision)
{
if (TruePeakFilter.MaxChannels < channels)
{
_gap = true;
Volatile.Write(ref _overrun, 1);
return;
}

Expand Down Expand Up @@ -117,6 +146,7 @@ private void WriteBlock(float[] samples, int channels)
_gap = false;
_queue[start] = reset ? -channels : channels;
_queue[start + 1] = count;
_queue[start + 2] = BitConverter.Int32BitsToSingle(revision);
Array.Copy(samples, 0, _queue, start + HeaderSize, count);
Volatile.Write(ref _written, unchecked(written + padding + HeaderSize + count));
}
Expand Down
File renamed without changes.
Loading
Loading