Skip to content

Commit

Permalink
[server] fix normalization (regex) and set more reasonable values
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowsink committed Jun 5, 2024
1 parent 9cdb5c5 commit 3c886f6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions UwuRadio.Server/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public static Constants
/// The target Integrated LUFS
/// </summary>
public double AudioNormIntegrated { get; set; }

/// <summary>
/// Allow to clip the audio by at most this much dB True Peak
/// </summary>
public double AudioNormMaxClip { get; set; }
}
11 changes: 9 additions & 2 deletions UwuRadio.Server/DSP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace UwuRadio.Server;
/// <summary>
/// Re-encodes and applies processing to audio files
/// </summary>
// ReSharper disable once InconsistentNaming
public static class DSP
{
public record struct LoudnessMeasurement(
Expand All @@ -16,7 +17,7 @@ public record struct LoudnessMeasurement(
// dBFS
double TruePeak);

private static readonly Regex LoudnessMeasurementRegex = new(@"I:\s*(.*) LUFS[\s\S]*Threshold:\s*(.*) LUFS[\s\S]*LRA:\s*(.*) LU[\s\S]*Peak:\s*(.*) dBFS");
private static readonly Regex LoudnessMeasurementRegex = new(@"I:\s*(.*) LUFS\s*Threshold:\s*(.*) LUFS[\s\S]*LRA:\s*(.*) LU[\s\S]*Peak:\s*(.*) dBFS");

public static async Task<LoudnessMeasurement> MeasureLoudness(string path)
{
Expand Down Expand Up @@ -45,14 +46,20 @@ public static async Task<LoudnessMeasurement> MeasureLoudness(string path)
var thres = double.Parse(match.Groups[2].Value);
var lra = double.Parse(match.Groups[3].Value);
var peak = double.Parse(match.Groups[4].Value);
//Console.Error.WriteLine(stdErr);
//Console.Error.WriteLine($"measurement: {lufs} LUFS, {thres} threshold, {lra} LU range, {peak} dbFS peak");

return new LoudnessMeasurement(lufs, thres, lra, peak);
}

public static async Task Normalize(string inPath, string outPath, LoudnessMeasurement measurement)
{
var lufsLoudnessChange = Constants.C.AudioNormIntegrated - measurement.IntegratedLoudness;
var clampedLoudnessChange = Math.Min(lufsLoudnessChange, -measurement.TruePeak);
var clampedLoudnessChange = Math.Min(lufsLoudnessChange, -(measurement.TruePeak - Constants.C.AudioNormMaxClip));

//Console.Error.WriteLine($"wanted adjustment: {lufsLoudnessChange} (to hit target {Constants.C.AudioNormIntegrated} from measurment {measurement.IntegratedLoudness}");
//Console.Error.WriteLine($"would result in a true peak level of {lufsLoudnessChange + measurement.TruePeak}");
//Console.Error.WriteLine($"so actual adjustment: {clampedLoudnessChange} (max clip: {Constants.C.AudioNormMaxClip}db)");

var args = new List<string> { "-nostdin", "-i", inPath, "-filter:a",
$"volume={clampedLoudnessChange}dB",
Expand Down
2 changes: 1 addition & 1 deletion UwuRadio.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
// lol this makes the entire program wrapped in an async state machine in debug builds :)
await Task.Delay(100);

Helpers.Log("debug cache-all", $"downloaded: {idx} of {dataSrv.Songs.Length}");
Helpers.Log("debug cache-all", $"downloaded: {idx + 1} of {dataSrv.Songs.Length}");
}

Helpers.Log("debug cache-all", "successfully downloaded all songs");
Expand Down
3 changes: 2 additions & 1 deletion UwuRadio.Server/constants.debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"IngestFolder": "../ingest",
"AudioFormat": "mp3",
"AudioQScale": "6",
"AudioNormIntegrated": -16
"AudioNormIntegrated": -8,
"AudioNormMaxClip": 2
}
3 changes: 2 additions & 1 deletion UwuRadio.Server/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"IngestFolder": "ingest",
"AudioFormat": "mp3",
"AudioQScale": "4",
"AudioNormIntegrated": -16
"AudioNormIntegrated": -8,
"AudioNormMaxClip": 2
}

0 comments on commit 3c886f6

Please sign in to comment.