问题
I am new to AudioKit. I want to build an VCO who's pitch is controlled by a re-triggerable A(H)R-envelope (with adjustable attenuation) and I am looking for the best or most common way to do it.
After some research (and wrong directions) it looks to me that I have to go with AKOperationGenerator and AKOperation.
Best example to follow was the Filter Envelope example from the playgrounds.
I tried:
enum PitchEnvVCOSynthParameter: Int {
case frequency, decayTime, gate
}
struct PitchEnvVCO {
static var frequency: AKOperation {
return AKOperation.parameters[PitchEnvVCOSynthParameter.frequency.rawValue]
}
static var decayTime: AKOperation {
return AKOperation.parameters[PitchEnvVCOSynthParameter.decayTime.rawValue]
}
static var gate: AKOperation {
return AKOperation.parameters[PitchEnvVCOSynthParameter.gate.rawValue]
}
}
extension AKOperationGenerator {
var frequency: Double {
get { return self.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] }
set(newValue) { self.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] = newValue }
}
var decayTime: Double {
get { return self.parameters[PitchEnvVCOSynthParameter.decayTime.rawValue] }
set(newValue) { self.parameters[PitchEnvVCOSynthParameter.decayTime.rawValue] = newValue }
}
var gate: Double {
get { return self.parameters[PitchEnvVCOSynthParameter.gate.rawValue] }
set(newValue) { self.parameters[PitchEnvVCOSynthParameter.gate.rawValue] = newValue }
}
}
with
class SimpleGenerator: AKNode {
var generator = AKOperationGenerator { parameters in
var oscillator = AKOperation.squareWave(frequency: PitchEnvVCO.frequency.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.01,
release: 0.2
)).triggeredWithEnvelope(trigger: PitchEnvVCO.gate, attack: 0.1, hold: 0.01, release: 0.5)
return oscillator
}
var vco1Freq: Double = 440.0 {
didSet {
generator.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] = vco1Freq
}
}
var pitchDecay: Double = 0.1 {
didSet {
generator.parameters[PitchEnvVCOSynthParameter.decayTime.rawValue] = pitchDecay
}
}
var sourceMixer = AKMixer()
func start() {
generator.parameters = [vco1Freq, pitchDecay, 0] // Initialize the array
generator.start()
}
func triggerGate(_ freq: Double) {
vco1FreqOffset = freq
generator.parameters[PitchEnvVCOSynthParameter.gate.rawValue] = 1
// or generator.trigger(1)
}
override init() {
sourceMixer = AKMixer(generator)
super.init()
avAudioNode = sourceMixer.avAudioNode
}
}
I trigger the generator via a sequencer track, triggering a calback
let seqPositioncallbackTrack = sequencer.newTrack()
seqPositioncallbackTrack?.setMIDIOutput(sequencerCallbackInst.midiIn)
sequencerCallbackInst.callback = { status, noteNumber, velocity in
self.onSequencerStepChange(status: status, noteNumber: noteNumber, velocity: velocity)
}
//...for i in 0 ..< numberOfSteps...etc.
seqPositioncallbackTrack?.add(noteNumber: MIDINoteNumber(i),
velocity: 100,
position: AKDuration(beats: Double(i)),
duration: AKDuration(beats: 0.5)
)
which works fine. But I just hear an endless tone (with the correct frequency). Only the first trigger swells in pitch. The other triggers don't re-trigger the envelopes. How comes - what get I wrong? THNX
回答1:
It seems like I needed to find the correct length of the gate-on time (note length of sequencer step duration) and also send a gate-off event to set the gate to zero.
...duration: AKDuration(seconds: 0.03)
private func onSequencerStepChange(status: AKMIDIStatus, noteNumber: MIDINoteNumber, velocity: MIDIVelocity) {
if status == .noteOn {
core.triggerGateOn(seq1Values[Int(noteNumber)])
}
else if status == .noteOff {
core.triggerGateOff()
}
else { return }
}
...
// splitting func triggerGate(_ freq: Double) into on/off:
func triggerGateOn(_ freq: Double) {
generator.parameters[PitchEnvVCOSynthParameter.gate.rawValue] = 1
}
func triggerGateOff() {
generator.parameters[PitchEnvVCOSynthParameter.gate.rawValue] = 0
}
it also seems not to work when the duration time is too short, means < 0.03 - then I guess the gate-on is faster set to off than the attack time was finished. also the decay time I had to simulate using the release time instead
let generator = AKOperationGenerator { parameters in
let oscillator = AKOperation.squareWave(
frequency: PitchEnvVCO.frequency.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.0,
release: PitchEnvVCO.freqDecayTime
),
amplitude: PitchEnvVCO.amplitude.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.0,
release: PitchEnvVCO.ampDecayTime
)
)
return oscillator
}
EDIT: ok - the correct answer I found I posted here
来源:https://stackoverflow.com/questions/49140013/swift-audiokit-how-to-build-an-vco-whos-pitch-is-controlled-by-an-ar-envelope