As a C++ dev picking up Swift, I'm wondering how, in a struct or other class, to store a "pointer" or "reference" to a property of of another object of another class.
For example- a controlResponder
class (or struct) that listens for events to modify the value target
of a target in an effect
class.
Control responder:
struct controlResponder {
var ccNum : Int! // CC number to respond to
var target : Double! // Points to the variable of another object
var min : Double!
var max : Double!
}
Effects, in my case, can be of different classes but the thing that will be targeted for modification will always be a Double
(To be precise- I'm working with AudioKit and am targeting effects like AKVariableDelay.time
, or AKMoogLadderFilter.cutoff
)
Any insight would be greatly appreciated, thanks!
Below is an abridged version of some of my actual (non functioning) code:
import Foundation
import AudioKit
import SwiftyJSON
class Effect : AKNode, AKMIDIListener {
enum CustomError: Error {
case badEffectName
}
struct ccListener {
var ccNum : Int!
var target : Int!
var min : Double!
var max : Double!
}
var listeners : [ccListener]
var effectType = ""
var effect = AKNode()
var channel = 0
var midi : AKMIDI
init(connectionInput : AKNode, midi : AKMIDI, subJson : JSON, channel : Int) throws {
self.midi = midi
self.channel = channel
var insertType = subJson["type"]
if insertType == "distortion" {
print("Adding a DISTORTION")
effect = AKTanhDistortion(connectionInput)
if let efx = effect as? AKTanhDistortion {
let gainVal = random(subJson["gain random low"].doubleValue, subJson["gain random high"].doubleValue)
print("gainVal: \(gainVal)")
efx.pregain = gainVal
}
}
else if insertType == "moog" {
print("Adding a MOOG FILTER")
/// CUTOFF
let cutoffVal = random(subJson["cutoff random low"].doubleValue, subJson["cutoff random high"].doubleValue)
print("cutoffVal: \(cutoffVal)")
/// RESONANCE
let resonanceVal = random(subJson["resonance random low"].doubleValue, subJson["resonance random high"].doubleValue)
print("resonanceVal: \(resonanceVal)")
effect = AKMoogLadder(connectionInput,
cutoffFrequency: cutoffVal,
resonance: resonanceVal)
}
else {
print("BAD EFFECT TYPE: \(insertType)")
throw CustomError.badEffectName
}
/////// MIDIZ
midi.openInput("vIn")
super.init()
for (key, cc) in subJson["ccs"] as JSON {
let efx = effect as! AKMoogLadder
listeners.append(ccListener(ccNum: cc["cc number"].intValue, target: efx.cutoffFrequency, min: cc["min"].doubleValue, max: cc["max"].doubleValue))
}
midi.addListener(self)
print("End of Effect init()")
}
func receivedMIDIController(_ controller: MIDIByte, value: MIDIByte, channel: MIDIChannel) {
print("Self channel: \(self.channel), incoming channel: \(Int(channel))")
if self.channel == Int(channel){
print("Effect got a CC!")
}
}
func changeVal(ccNum: Int, newValue: Int) {
for listener in listeners {
if listener.ccNum == ccNum {
listener.target = newValue
}
}
}
}