1
1
mirror of https://github.com/bitgapp/eqMac.git synced 2024-12-02 10:24:09 +03:00

simplified equalizer logic

This commit is contained in:
nodeful 2020-05-30 18:07:04 +01:00
parent fddbb4e500
commit 6ff600bc4f
2 changed files with 22 additions and 52 deletions

View File

@ -10,74 +10,53 @@ import Foundation
import AVFoundation
class Equalizer: Effect {
let numberOfBands: Int!
var eqs: [AVAudioUnitEQ] = []
var eq: AVAudioUnitEQ
var globalGain: Double {
get {
return Double(eqs[0].globalGain)
return Double(eq.globalGain)
}
set {
eqs[0].globalGain = Float(newValue)
eq.globalGain = Float(newValue)
}
}
var gains: [Double] {
var gains: [Double] = []
for eq in eqs {
for band in eq.bands {
gains.append(Double(band.gain))
}
for band in eq.bands {
gains.append(Double(band.gain))
}
return gains
}
init (numberOfBands: Int) {
self.numberOfBands = numberOfBands
let numberOfEQs = Int(ceil(Double(numberOfBands) / 16))
let remainder = numberOfBands % 16
for i in 1...numberOfEQs {
let eq = AVAudioUnitEQ(numberOfBands: i == numberOfEQs ? remainder : 16)
eq.globalGain = 0
for band in eq.bands {
band.filterType = .parametric
band.bandwidth = 0.5
band.bypass = false
}
eqs.append(eq)
eq = AVAudioUnitEQ(numberOfBands: numberOfBands)
eq.globalGain = 0
for band in eq.bands {
band.filterType = .parametric
band.bandwidth = 0.5
band.bypass = false
}
}
func getFrequency (index: Int) -> Double {
return Double(getBandFromIndex(index: index)!.frequency)
return Double(eq.bands[index].frequency)
}
func setFrequency (index: Int, frequency: Double) {
let band = getBandFromIndex(index: index)
band!.frequency = Float(frequency)
let band = eq.bands[index]
band.frequency = Float(frequency)
}
func getGain (index: Int) -> Double {
return Double(getBandFromIndex(index: index)!.gain)
return Double(eq.bands[index].gain)
}
func setGain (index: Int, gain: Double) {
let band = getBandFromIndex(index: index)
band!.gain = Float(gain)
let band = eq.bands[index]
band.gain = Float(gain)
}
override func enabledDidSet() {
for eq in eqs {
eq.bypass = !enabled
}
}
private func getBandFromIndex (index: Int) -> AVAudioUnitEQFilterParameters? {
if (index >= numberOfBands) {
Console.log("Trying to get out of bounds AppleEqualizer Band")
return nil
}
let eqIndex = Int(floor(Double(index / 16)))
let bandIndex = index % 16
return eqs[eqIndex].bands[bandIndex]
eq.bypass = !enabled
}
}

View File

@ -98,10 +98,7 @@ class Engine {
private func attachEqualizers () {
equalizerNodes = []
for eq in effects.equalizers.active.eqs {
engine.attach(eq)
equalizerNodes.append(eq)
}
engine.attach(effects.equalizers.active.eq)
}
private func detachEqualizers () {
@ -134,26 +131,20 @@ class Engine {
}
private func chainVolumeToEffects () {
engine.connect(volume.booster.avAudioNode, to: effects.equalizers.active.eqs.first!, format: format)
engine.connect(volume.booster.avAudioNode, to: effects.equalizers.active.eq, format: format)
}
private func chainEffects () {
Console.log("Chaining Effects")
for (i, eq) in effects.equalizers.active.eqs.enumerated() {
let nextIndex = i + 1
if (nextIndex < effects.equalizers.active.eqs.count) {
engine.connect(eq, to: effects.equalizers.active.eqs[nextIndex], format: format)
}
}
}
private func chainEffectsToSink () {
engine.connect(effects.equalizers.active.eqs.last!, to: engine.mainMixerNode, format: format)
engine.connect(effects.equalizers.active.eq, to: engine.mainMixerNode, format: format)
}
private func setupRenderCallback () {
Console.log("Setting up Input Render Callback")
let lastAVUnit = effects.equalizers.active.eqs.last! as AVAudioUnit
let lastAVUnit = effects.equalizers.active.eq as AVAudioUnit
if let err = checkErr(AudioUnitAddRenderNotify(lastAVUnit.audioUnit,
inputRenderedNotification,
UnsafeMutableRawPointer(Unmanaged<Engine>.passUnretained(self).toOpaque()))) {