Skip to content

Commit

Permalink
Generation of Multiband PEQs by sampling a curve
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Jun 1, 2024
1 parent a88b2ab commit 1b23545
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Cavern.QuickEQ.Format/FilterSet/MultibandPEQFilterSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ public MultibandPEQFilterSet(string path, int sampleRate) : base(0, sampleRate)
}

for (int i = 0, c = lastChannel.Count - 1; i < c; i++) {
lastChannel[i].Q = QFactor.FromBandwidth(Math.Log(lastChannel[i + 1].CenterFreq / lastChannel[i].CenterFreq, 2));
lastChannel[i].Q =
1.5 * QFactor.FromBandwidth(Math.Log(lastChannel[i + 1].CenterFreq / lastChannel[i].CenterFreq, 2));
}
lastChannel[^1].Q = lastChannel[^2].Q;
channels.Add(new IIRChannelData {
Expand Down Expand Up @@ -188,6 +189,34 @@ public PeakingEQ[] CalculateFilters(Equalizer targetToReach, bool lfe) {
return result;
}

/// <summary>
/// Sample the filters that should be used when setting up a channel from the <paramref name="targetToReach"/>'s band center points.
/// This can provide better and faster results than brute force when the smoothing is high enough
/// (about the bandwidth of this filter set).
/// </summary>
public PeakingEQ[] SampleFilters(Equalizer targetToReach, bool lfe) {
int bands = lfe ? LFEBands : bandCount;
PeakingEQ[] result = new PeakingEQ[bands];
double bandwidth = 1.0 / bandsPerOctave;
double q = QFactor.FromBandwidth(bandwidth);
for (int i = 0; i < bands; i++) {
double freq = firstBand * Math.Pow(2, i * bandwidth);
if (RoundedBands) {
int magnitude = (int)Math.Pow(10, Math.Floor(Math.Log10(freq)) - 1);
freq = Math.Round(freq / magnitude) * magnitude;
}

double freqOverride;
if (FreqOverrides != null && (freqOverride = FreqOverrides.FirstOrDefault(x => x.oldFreq == freq).newFreq) != default) {
freq = freqOverride;
}

result[i] = new PeakingEQ(SampleRate, freq, q, Math.Clamp(targetToReach[freq], MinGain, MaxGain));
}

return result;
}

/// <inheritdoc/>
public override string Export() => Export(true);

Expand Down

0 comments on commit 1b23545

Please sign in to comment.