Skip to content

Commit

Permalink
#12 RG - Settings menu
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-venugopal committed Aug 21, 2024
1 parent 2f697c4 commit 4720d91
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 155 deletions.
Binary file not shown.
1 change: 0 additions & 1 deletion Source/Core/AudioGraph/AudioGraphDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ struct AudioGraphDefaults {
static let replayGainPreventClipping: Bool = true

static let replayGainDataSource: ReplayGainDataSource = .metadataOrAnalysis
static let replayGainTargetLoudness: ReplayGainTargetLoudness = .minus18
static let replayGainMaxPeakLevel: ReplayGainMaxPeakLevel = .zero

static let auState: EffectsUnitState = .active
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ class ReplayGainPreset: EffectsUnitPreset {

let mode: ReplayGainMode
let preAmp: Float
let preventClipping: Bool

init(name: String, state: EffectsUnitState, mode: ReplayGainMode, preAmp: Float, systemDefined: Bool) {
init(name: String, state: EffectsUnitState, mode: ReplayGainMode, preAmp: Float, preventClipping: Bool, systemDefined: Bool) {

self.mode = mode
self.preAmp = preAmp
self.preventClipping = preventClipping

super.init(name: name, state: state, systemDefined: systemDefined)
}
Expand All @@ -40,11 +42,12 @@ class ReplayGainPreset: EffectsUnitPreset {

self.mode = mode
self.preAmp = preAmp
self.preventClipping = persistentState.preventClipping ?? AudioGraphDefaults.replayGainPreventClipping

super.init(name: name, state: unitState, systemDefined: false)
}

func equalToOtherPreset(mode: ReplayGainMode, preAmp: Float) -> Bool {
self.mode == mode && Float.valuesEqual(self.preAmp, preAmp, tolerance: 0.001)
func equalToOtherPreset(mode: ReplayGainMode, preAmp: Float, preventClipping: Bool) -> Bool {
self.mode == mode && Float.valuesEqual(self.preAmp, preAmp, tolerance: 0.001) && self.preventClipping == preventClipping
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {

var dataSource: ReplayGainDataSource

var targetLoudness: ReplayGainTargetLoudness {

didSet {
parmsChanged()
}
}

var maxPeakLevel: ReplayGainMaxPeakLevel {

didSet {
Expand Down Expand Up @@ -109,7 +102,6 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {
replayGain = nil
preventClipping = persistentState?.preventClipping ?? AudioGraphDefaults.replayGainPreventClipping

targetLoudness = persistentState?.targetLoudness ?? AudioGraphDefaults.replayGainTargetLoudness
maxPeakLevel = persistentState?.maxPeakLevel ?? AudioGraphDefaults.replayGainMaxPeakLevel
dataSource = persistentState?.dataSource ?? AudioGraphDefaults.replayGainDataSource

Expand Down Expand Up @@ -142,7 +134,9 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {

override func savePreset(named presetName: String) {

let newPreset = ReplayGainPreset(name: presetName, state: .active, mode: mode, preAmp: preAmp, systemDefined: false)
let newPreset = ReplayGainPreset(name: presetName, state: .active,
mode: mode, preAmp: preAmp, preventClipping: preventClipping,
systemDefined: false)
presets.addObject(newPreset)
currentPreset = newPreset
}
Expand All @@ -163,7 +157,9 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {
}

var settingsAsPreset: ReplayGainPreset {
ReplayGainPreset(name: "replayGainSettings", state: state, mode: mode, preAmp: preAmp, systemDefined: false)
ReplayGainPreset(name: "replayGainSettings", state: state,
mode: mode, preAmp: preAmp, preventClipping: preventClipping,
systemDefined: false)
}

private func invalidateCurrentPreset() {
Expand All @@ -178,7 +174,7 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {

guard let matchingPreset = presets.object(named: presetName) else {return}

if matchingPreset.equalToOtherPreset(mode: mode, preAmp: preAmp) {
if matchingPreset.equalToOtherPreset(mode: mode, preAmp: preAmp, preventClipping: preventClipping) {
self.currentPreset = matchingPreset
}
}
Expand All @@ -201,7 +197,6 @@ class ReplayGainUnit: EffectsUnit, ReplayGainUnitProtocol {
preAmp: preAmp,
preventClipping: preventClipping,
dataSource: dataSource,
targetLoudness: targetLoudness,
maxPeakLevel: maxPeakLevel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class ReplayGainUnitDelegate: EffectsUnitDelegate<ReplayGainUnit>, ReplayGainUni
set {unit.dataSource = newValue}
}

var targetLoudness: ReplayGainTargetLoudness {

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

var maxPeakLevel: ReplayGainMaxPeakLevel {

get {unit.maxPeakLevel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ protocol ReplayGainUnitDelegateProtocol: EffectsUnitDelegateProtocol {

var dataSource: ReplayGainDataSource {get set}

var targetLoudness: ReplayGainTargetLoudness {get set}

var maxPeakLevel: ReplayGainMaxPeakLevel {get set}

var hasAppliedGain: Bool {get}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ protocol ReplayGainUnitProtocol: EffectsUnitProtocol {

var dataSource: ReplayGainDataSource {get set}

var targetLoudness: ReplayGainTargetLoudness {get set}

var maxPeakLevel: ReplayGainMaxPeakLevel {get set}

/*
Expand Down Expand Up @@ -70,26 +68,6 @@ enum ReplayGainDataSource: Int, Codable {
case metadataOrAnalysis, metadataOnly, analysisOnly
}

enum ReplayGainTargetLoudness: Codable {

case minus18, minus14, custom(targetLoudness: Float)

var decibels: Float {

switch self {

case .minus18:
return -18

case .minus14:
return -14

case .custom(let customDecibels):
return customDecibels
}
}
}

enum ReplayGainMaxPeakLevel: Codable {

case zero, custom(maxPeakLevel: Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ struct ReplayGainUnitPersistentState: Codable {
let preventClipping: Bool?

let dataSource: ReplayGainDataSource?
let targetLoudness: ReplayGainTargetLoudness?
let maxPeakLevel: ReplayGainMaxPeakLevel?

init(state: EffectsUnitState?, userPresets: [ReplayGainPresetPersistentState]?, currentPresetName: String?, renderQuality: Int?, mode: ReplayGainMode?, preAmp: Float?, preventClipping: Bool?, dataSource: ReplayGainDataSource?, targetLoudness: ReplayGainTargetLoudness?, maxPeakLevel: ReplayGainMaxPeakLevel?) {
init(state: EffectsUnitState?, userPresets: [ReplayGainPresetPersistentState]?, currentPresetName: String?, renderQuality: Int?, mode: ReplayGainMode?, preAmp: Float?, preventClipping: Bool?, dataSource: ReplayGainDataSource?, maxPeakLevel: ReplayGainMaxPeakLevel?) {

self.state = state
self.userPresets = userPresets
Expand All @@ -37,7 +36,6 @@ struct ReplayGainUnitPersistentState: Codable {
self.preventClipping = preventClipping

self.dataSource = dataSource
self.targetLoudness = targetLoudness
self.maxPeakLevel = maxPeakLevel
}
}
Expand All @@ -49,6 +47,7 @@ struct ReplayGainPresetPersistentState: Codable {

let mode: ReplayGainMode?
let preAmp: Float?
let preventClipping: Bool?

init(preset: ReplayGainPreset) {

Expand All @@ -57,5 +56,6 @@ struct ReplayGainPresetPersistentState: Codable {

self.mode = preset.mode
self.preAmp = preset.preAmp
self.preventClipping = preset.preventClipping
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ class DecibelSelectorView: NSView {
@IBOutlet weak var btnCustomDecibel: CheckBox!
@IBOutlet weak var lblDecibel: NSTextField!

var replayGainUnit: ReplayGainUnitDelegateProtocol!
lazy var replayGainUnit: ReplayGainUnitDelegateProtocol = audioGraphDelegate.replayGainUnit

///
/// Stepper value is 10x target value, so divide by 10.
///
var decibelValue: Float {

get {
decibelStepper.floatValue / 10.0
}

set {
decibelStepper.floatValue = newValue * 10.0
}
}

var formattedDecibelString: String {
String(format: "%.2f dB", decibelStepper.floatValue)
String(format: "%.2f dB", decibelValue)
}

override func awakeFromNib() {
Expand All @@ -43,36 +57,32 @@ class DecibelSelectorView: NSView {
}
}

class TargetLoudnessSelectorView: DecibelSelectorView {

override func prepareToAppear() {

decibelStepper.floatValue = replayGainUnit.targetLoudness.decibels
lblDecibel.stringValue = formattedDecibelString
}

@IBAction override func decibelStepperAction(_ sender: NSStepper) {

super.decibelStepperAction(sender)

replayGainUnit.targetLoudness = .custom(targetLoudness: decibelStepper.floatValue)
Messenger.publish(.Effects.ReplayGainUnit.targetLoudnessChanged)
}
}

class MaxPeakLevelSelectorView: DecibelSelectorView {

override func prepareToAppear() {

decibelStepper.floatValue = replayGainUnit.maxPeakLevel.decibels
switch replayGainUnit.maxPeakLevel {

case .zero:

btnCustomDecibel.off()
decibelStepper.disable()

case .custom(_):

btnCustomDecibel.on()
decibelStepper.enable()
}

decibelValue = replayGainUnit.maxPeakLevel.decibels
lblDecibel.stringValue = formattedDecibelString
}

@IBAction override func decibelStepperAction(_ sender: NSStepper) {

super.decibelStepperAction(sender)

replayGainUnit.maxPeakLevel = .custom(maxPeakLevel: decibelStepper.floatValue)
replayGainUnit.maxPeakLevel = .custom(maxPeakLevel: decibelValue)
Messenger.publish(.Effects.ReplayGainUnit.maxPeakLevelChanged)
}
}
Loading

0 comments on commit 4720d91

Please sign in to comment.