Skip to content

Commit

Permalink
#12 Clipping prevention UI
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Aug 20, 2024
1 parent f9fb311 commit 463dc0d
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 98 deletions.
Binary file not shown.
42 changes: 32 additions & 10 deletions Source/Core/AudioGraph/EffectsUnits/ReplayGain/ReplayGainUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,53 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {
}
}

var preAmp: Float {

didSet {
node.preAmp = preAmp
}
}

var preventClipping: Bool {

didSet {
parmsChanged()
}
}

private var computedTrackGain: Float? {

preventClipping ?
replayGain?.trackGainToPreventClipping ?? replayGain?.trackGain :
replayGain?.trackGain
}

private var computedAlbumGain: Float? {

preventClipping ?
replayGain?.albumGainToPreventClipping ?? replayGain?.albumGain :
replayGain?.albumGain
}

private func parmsChanged() {

let replayGainDB: Float?

switch mode {

case .preferAlbumGain:
replayGainDB = replayGain?.albumGain ?? replayGain?.trackGain
replayGainDB = computedAlbumGain ?? computedTrackGain

case .preferTrackGain:
replayGainDB = replayGain?.trackGain ?? replayGain?.albumGain
replayGainDB = computedTrackGain ?? computedAlbumGain

case .trackGainOnly:
replayGainDB = replayGain?.trackGain
replayGainDB = computedTrackGain
}

node.replayGain = replayGainDB ?? 0
}

var preAmp: Float {

didSet {
node.preAmp = preAmp
}
}

var appliedGain: Float {
node.replayGain
}
Expand All @@ -71,6 +92,7 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {
mode = persistentState?.mode ?? AudioGraphDefaults.replayGainMode
replayGain = nil
preAmp = persistentState?.preAmp ?? AudioGraphDefaults.replayGainPreAmp
preventClipping = false

presets = ReplayGainPresets(persistentState: persistentState)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class ReplayGainUnitDelegate: EffectsUnitDelegate<ReplayGainUnit>, ReplayGainUni
set {unit.preAmp = newValue}
}

var preventClipping: Bool {

get {unit.preventClipping}
set {unit.preventClipping = newValue}
}

func applyGain(_ replayGain: ReplayGain?) {
unit.replayGain = replayGain
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ protocol ReplayGainUnitDelegateProtocol: EffectsUnitDelegateProtocol {

var preAmp: Float {get set}

var preventClipping: Bool {get set}

func applyGain(_ replayGain: ReplayGain?)

var appliedGain: Float {get}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ protocol ReplayGainUnitProtocol: EffectsUnitProtocol {

var preAmp: Float {get set}

var preventClipping: Bool {get set}

var appliedGain: Float {get}

var effectiveGain: Float {get}
Expand Down
27 changes: 15 additions & 12 deletions Source/Core/TrackIO/Model/ReplayGain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,38 @@ struct ReplayGain {

let albumGain: Float?
let albumPeak: Float?
let albumGainToPreventClipping: Float?

private static let maxPeak: Float = 1

init?(trackGain: Float? = nil, trackPeak: Float? = nil, albumGain: Float? = nil, albumPeak: Float? = nil) {

guard trackGain != nil || albumGain != nil else {return nil}

func gainToPreventClipping(gain: Float, peak: Float) -> Float {

let newPeak = pow(10.0, gain / 20) * peak
return newPeak > Self.maxPeak ? gain - (20 * log10(newPeak / Self.maxPeak)) : gain
}

self.trackGain = trackGain
self.trackPeak = trackPeak

var trackGainToPreventClipping: Float

if let theTrackGain = trackGain, let theTrackPeak = trackPeak {

trackGainToPreventClipping = theTrackGain

let newPeak = pow(10.0, theTrackGain / 20) * theTrackPeak

if newPeak > Self.maxPeak {
trackGainToPreventClipping -= (20 * log10(newPeak / Self.maxPeak))
}

self.trackGainToPreventClipping = trackGainToPreventClipping
self.trackGainToPreventClipping = gainToPreventClipping(gain: theTrackGain, peak: theTrackPeak)

} else {
self.trackGainToPreventClipping = nil
}

self.albumGain = albumGain
self.albumPeak = albumPeak

if let theAlbumGain = albumGain, let theAlbumPeak = albumPeak {
self.albumGainToPreventClipping = gainToPreventClipping(gain: theAlbumGain, peak: theAlbumPeak)
} else {
self.albumGainToPreventClipping = nil
}
}

init(ebur128AnalysisResult: EBUR128AnalysisResult) {
Expand All @@ -58,5 +60,6 @@ struct ReplayGain {

self.albumGain = nil
self.albumPeak = nil
self.albumGainToPreventClipping = nil
}
}
Loading

0 comments on commit 463dc0d

Please sign in to comment.