AKOscillator或AKAmplitudeEnvelope 4.1中有什么根本改变了吗?我的简单编钟声音的代码就像4.0中的魅力一样,但现在在4.1中是一种奇怪的异类。我不确定如何继续而不回到4.0。
以下是我设置和播放音符的代码:
var oscillators = [AKOscillator]()
var envelopes = [AKAmplitudeEnvelope]()
var mix: AKMixer?
let chime1Partials: [PartialAttributes] =
[
PartialAttributes(partial: 0.5, amp: 0.25, attack: 0.25, decay: 0.35, sustain: 0.0, release: 1.0),
PartialAttributes(partial: 1.0, amp: 0.75, attack: 0.025, decay: 0.135, sustain: 0.0, release: 1.0),
PartialAttributes(partial: 2.69, amp: 0.35, attack: 0.025, decay: 0.25, sustain: 0.0, release: 1.0),
PartialAttributes(partial: 5.15, amp: 0.15, attack: 0.05, decay: 0.25, sustain: 0.0, release: 0.5)
]
class ChimeOscillator: NSObject, PlayerProtocol
{
var fundamental: Double = 0.0
var oscillators = [AKOscillator]()
var envelopes = [AKAmplitudeEnvelope]()
var mix: AKMixer?
init(_ fundamental: Double, partials: [PartialAttributes])
{
oscillators.removeAll()
envelopes.removeAll()
self.fundamental = fundamental
for attrib in partials
{
let osc = AKOscillator(waveform: AKTable(.sine))
osc.frequency = fundamental * attrib.partial
osc.amplitude = attrib.amp
osc.start()
oscillators.append(osc)
let env = AKAmplitudeEnvelope(osc)
env.attackDuration = attrib.attack
env.decayDuration = attrib.decay
env.sustainLevel = attrib.sustain
env.releaseDuration = attrib.release
envelopes.append(env)
}
mix = AKMixer(envelopes)
super.init()
}
func play(velocity: MIDIVelocity)
{
guard let mix = mix else{ return }
let amp = Double(velocity) / 127.0
// Mixer Output Volume (Default 1.0)
mix.volume = amp * amp
_ = envelopes.map{$0.start()}
}
func stop()
{
_ = envelopes.map{$0.stop()}
}
}