Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Foundation
struct SoundSample {}
class Effect: Codable {
// Properties we are intersted in
var name: String = ""
var active: Bool = true
init() {
self.name = ""
self.active = true
}
func doEffect(_ s: SoundSample) -> Void {}
// stuff for encoding
private enum CodingKeys: CodingKey {
case name, active
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
self.active = try container.decode(Bool.self, forKey: .active)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(active, forKey: .active)
}
}
class ReverbEffect: Effect {
var delay = 3.2
var feedback = 40.0
override init() {
super.init()
self.name = "Reverb"
}
override func doEffect(_ s: SoundSample) -> Void { /* some custom reverb code here */ }
// stuff for encoding
private enum CodingKeys: CodingKey {
case delay, feedback
}
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
let container = try decoder.container(keyedBy: CodingKeys.self)
self.delay = try container.decode(Double.self, forKey: .delay)
self.feedback = try container.decode(Double.self, forKey: .feedback)
}
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(delay, forKey: .delay)
try container.encode(feedback, forKey: .feedback)
}
}
class DistortionEffect: Effect {
var gain = 1.5
var tone = 6.0
override init() {
super.init()
self.name = "Distortion"
}
override func doEffect(_ s: SoundSample) -> Void { /* some custom distortion code here */ }
// stuff for encoding
private enum CodingKeys: CodingKey {
case gain, tone
}
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
let container = try decoder.container(keyedBy: CodingKeys.self)
self.gain = try container.decode(Double.self, forKey: .gain)
self.tone = try container.decode(Double.self, forKey: .tone)
}
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(gain, forKey: .gain)
try container.encode(tone, forKey: .tone)
}
}
let effectsChain = [ReverbEffect(), DistortionEffect()]
// normal operations will be things like this:
let s = SoundSample()
for effect in effectsChain {
effect.doEffect(s)
}
// Then play sample
// Save our chain as JSON for saving and restoring
do {
let jsonData = try JSONEncoder().encode(effectsChain)
let jsonString = String(data: jsonData, encoding: .utf8)
if let json = jsonString {
print(json)
}
// now restore
let newChain = try? JSONDecoder().decode([Effect].self, from:jsonData)
if let new = newChain {
print("We restored the chain: %@", new)
}
} catch {
// blah
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment