|
| 1 | +using NAudio.Wave; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace QPlayer.Audio; |
| 11 | + |
| 12 | +public class AudioLimiterSampleProvider : ISamplePositionProvider |
| 13 | +{ |
| 14 | + private readonly ISamplePositionProvider source; |
| 15 | + private readonly DelaySampleProvider delay; |
| 16 | + private readonly int channelCount; |
| 17 | + private readonly int fs; |
| 18 | + private readonly float[] envBuff; |
| 19 | + private int attack = 1; |
| 20 | + private int release = 1; |
| 21 | + private float threshold; |
| 22 | + private readonly Lock lockObj; |
| 23 | + |
| 24 | + // State |
| 25 | + private float minHoldVal = 1; |
| 26 | + private int minHoldTime = int.MaxValue; |
| 27 | + private float relLastMin = 0; |
| 28 | + private float relLastRes = 1; |
| 29 | + private float smoothV1 = 1; |
| 30 | + private float smoothV2 = 1; |
| 31 | + private int meterSampleCount; |
| 32 | + private float meterLastG = 1; |
| 33 | + |
| 34 | + const int BLOCK_SIZE = 1024; |
| 35 | + const int MAX_ATTACK = 4096; |
| 36 | + |
| 37 | + public float AttackTime |
| 38 | + { |
| 39 | + get => attack / (float)(fs * channelCount); |
| 40 | + set => attack = Math.Clamp((int)(value * (fs * channelCount)), 5, MAX_ATTACK); |
| 41 | + } |
| 42 | + |
| 43 | + public float ReleaseTime |
| 44 | + { |
| 45 | + get => release / (float)(fs * channelCount); |
| 46 | + set => release = Math.Max(5, (int)(value * (fs * channelCount))); |
| 47 | + } |
| 48 | + |
| 49 | + public float Threshold |
| 50 | + { |
| 51 | + get => threshold; |
| 52 | + set => threshold = value; |
| 53 | + } |
| 54 | + |
| 55 | + public bool Enabled { get; set; } = true; |
| 56 | + public float InputGain { get; set; } = 1; |
| 57 | + |
| 58 | + public long Position |
| 59 | + { |
| 60 | + get => Math.Max(0, source.Position - attack); |
| 61 | + set => source.Position = Math.Max(0, source.Position - attack); |
| 62 | + } |
| 63 | + |
| 64 | + public WaveFormat WaveFormat => source.WaveFormat; |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// The number of samples to read between each metering event. |
| 68 | + /// </summary> |
| 69 | + public int SamplesPerNotification { get; set; } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// An event raised every <see cref="SamplesPerNotification"/> samples with metering information. |
| 73 | + /// </summary> |
| 74 | + public event Action<float>? OnMeter; |
| 75 | + |
| 76 | + public AudioLimiterSampleProvider(ISamplePositionProvider source) |
| 77 | + { |
| 78 | + this.source = source; |
| 79 | + this.channelCount = source.WaveFormat.Channels; |
| 80 | + this.fs = source.WaveFormat.SampleRate; |
| 81 | + lockObj = new(); |
| 82 | + envBuff = new float[BLOCK_SIZE]; |
| 83 | + delay = new(source, MAX_ATTACK); |
| 84 | + } |
| 85 | + |
| 86 | + public int Read(float[] buffer, int offset, int count) |
| 87 | + { |
| 88 | + if (!Enabled) |
| 89 | + { |
| 90 | + return source.Read(buffer, offset, count); |
| 91 | + } |
| 92 | + |
| 93 | + using var _ = lockObj.EnterScope(); |
| 94 | + |
| 95 | + count = Math.Min(count, BLOCK_SIZE); |
| 96 | + int read = delay.Read(envBuff, 0, count); |
| 97 | + |
| 98 | + // Clip |
| 99 | + VectorExtensions.ClipGR(envBuff.AsSpan(0, read), InputGain, threshold); |
| 100 | + |
| 101 | + // Moving min --> vey slow, replaced with min-hold |
| 102 | + /*movingMinDelay.Push(envBuff.AsSpan(0, read)); |
| 103 | + for (int i = 0; i < read; i++) |
| 104 | + { |
| 105 | + var first = movingMinDelay.GetDelayed(attack, read - i, out var second); |
| 106 | + float min = VectorExtensions.Min(first); |
| 107 | + if (second.Length > 0) |
| 108 | + min = MathF.Min(min, VectorExtensions.Min(second)); |
| 109 | + envBuff[i] = min; |
| 110 | + }*/ |
| 111 | + |
| 112 | + // Min hold |
| 113 | + float minVal = minHoldVal; |
| 114 | + int minTime = minHoldTime; |
| 115 | + for (int i = 0; i < read; i++) |
| 116 | + { |
| 117 | + float v = envBuff[i]; |
| 118 | + if (v < minVal || minTime >= attack) |
| 119 | + { |
| 120 | + minTime = 0; |
| 121 | + minVal = v; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + minTime++; |
| 126 | + envBuff[i] = v; |
| 127 | + } |
| 128 | + } |
| 129 | + minHoldVal = minVal; |
| 130 | + minHoldTime = minTime; |
| 131 | + |
| 132 | + // Exponential release stage |
| 133 | + float gradientFactor = 1f / (release + 1); |
| 134 | + // releaseDelay.Push(envBuff.AsSpan(0, read)); |
| 135 | + float last = relLastMin; |
| 136 | + float output = relLastRes; |
| 137 | + for (int i = 0; i < read; i++) |
| 138 | + { |
| 139 | + float min = envBuff[i]; |
| 140 | + output += (min - output) * gradientFactor; |
| 141 | + output = MathF.Min(output, min); |
| 142 | + last = min; |
| 143 | + envBuff[i] = output; |
| 144 | + } |
| 145 | + relLastMin = last; |
| 146 | + relLastRes = output; |
| 147 | + |
| 148 | + // Smoothing - A weighted moving average (a convolution of a window function) --> tooo slowww |
| 149 | + /*smootherDelay.Push(envBuff.AsSpan(0, read)); |
| 150 | + float recip = 1f/windowSum; |
| 151 | + for (int i = 0; i < read; i++) |
| 152 | + { |
| 153 | + var first = smootherDelay.GetDelayed(attack, read - i, out var second); |
| 154 | + float sum = VectorExtensions.Convolve(first, smootherWindow); |
| 155 | + if (second.Length > 0) |
| 156 | + sum += VectorExtensions.Convolve(second, smootherWindow.AsSpan(first.Length)); |
| 157 | + envBuff[i] = sum * recip; |
| 158 | + }*/ |
| 159 | + // Smoothing - Exponential/Constant rate hybrid |
| 160 | + float rate = 5 / (float)attack; |
| 161 | + float v1 = smoothV1; |
| 162 | + float v2 = smoothV2; |
| 163 | + for (int i = 0; i < read; i++) |
| 164 | + { |
| 165 | + var v = envBuff[i]; |
| 166 | + v1 = Math.Max(v, v1 + (v - 1) * rate); |
| 167 | + var n = v2 + (v1 - v2) * rate * 3; |
| 168 | + v2 = Math.Clamp(n, v1, 1); |
| 169 | + envBuff[i] = v2; |
| 170 | + } |
| 171 | + smoothV1 = v1; |
| 172 | + smoothV2 = v2; |
| 173 | + |
| 174 | + // TODO: > 2 channel support? |
| 175 | + if (channelCount == 2) |
| 176 | + { |
| 177 | + // Link stereo limiting by computing the min per 2-samples |
| 178 | + VectorExtensions.StereoMin(envBuff); |
| 179 | + } |
| 180 | + |
| 181 | + int k = 0; |
| 182 | + float meterMin = meterLastG; |
| 183 | + while (k < read && SamplesPerNotification > 0) |
| 184 | + { |
| 185 | + int toTake = Math.Min(SamplesPerNotification - meterSampleCount, read - k); |
| 186 | + meterMin = MathF.Min(meterMin, VectorExtensions.Min(envBuff.AsSpan(k, toTake))); |
| 187 | + k += toTake; |
| 188 | + meterSampleCount += toTake; |
| 189 | + if (meterSampleCount >= SamplesPerNotification) |
| 190 | + { |
| 191 | + NotifySample(meterMin);//(1 / meterMin - 1); |
| 192 | + meterMin = 1; |
| 193 | + } |
| 194 | + } |
| 195 | + meterLastG = meterMin; |
| 196 | + |
| 197 | + if (MathF.Abs(envBuff[0]) > 1.5f) |
| 198 | + Debugger.Break(); |
| 199 | + |
| 200 | + read = delay.ReadDelayed(buffer, offset, read, attack); |
| 201 | + VectorExtensions.Multiply(buffer.AsSpan(offset, read), envBuff.AsSpan(0, read)); |
| 202 | + VectorExtensions.Multiply(buffer.AsSpan(offset, read), InputGain); |
| 203 | + return read; |
| 204 | + |
| 205 | + void NotifySample(float meterMin) |
| 206 | + { |
| 207 | + float gr = 1 - meterMin; |
| 208 | + OnMeter?.Invoke(gr); |
| 209 | + meterSampleCount = 0; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + /*[MemberNotNull(nameof(smootherWindow))] |
| 214 | + private void CreateSmootherWindow() |
| 215 | + { |
| 216 | + smootherWindow = new float[attack]; |
| 217 | + // Blackmann window |
| 218 | + float alpha = 0.16f; |
| 219 | + float a0 = (1 - alpha) / 2; |
| 220 | + float a1 = 0.5f; |
| 221 | + float a2 = alpha / 2; |
| 222 | + windowSum = 0; |
| 223 | + for (int i = 0; i < smootherWindow.Length; i++) |
| 224 | + { |
| 225 | + float t = MathF.Tau * i / (float)smootherWindow.Length; |
| 226 | + float x = a0 - a1 * MathF.Cos(t) + a2 * MathF.Cos(2 * t); |
| 227 | + smootherWindow[i] = x; |
| 228 | + windowSum += x; |
| 229 | + } |
| 230 | + }*/ |
| 231 | +} |
0 commit comments