AudioKit:如何使用 AKOperation 参数值作为 Double 进行操作?
AudioKit: How to operate with AKOperation parameter values as Double?
我想在以下上下文中计算 pow():
let generator = AKOperationGenerator { parameters in
let depth = PitchEnvVCO.freqDecayDepth.times(PitchEnvVCO.freqDecayAmount)
let wdth = pow(2.0, depth/12.0) * PitchEnvVCO.frequency // throws error
let ptch = AKOperation.exponentialSegment(
trigger: PitchEnvVCO.gate,
start: wdth,
end: PitchEnvVCO.frequency,
duration: PitchEnvVCO.freqDecayTime
)
let oscillator = AKOperation.squareWave(
frequency: ptch,
amplitude: PitchEnvVCO.amplitude.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.0,
release: PitchEnvVCO.ampDecayTime
)
)
return oscillator
}
并得到错误
Cannot convert value of type 'AKOperation' to expected argument type
'Double'
我已经像 filter envelope example 那样构建了我的发电机。我如何将 AKOperation 转换为它的 Double Value?谢谢!
重新思考这个概念后,我在 AKOperationGenerator 之外进行了计算,并添加了更多 AKOperation.parameters 来存储结果:
let generator = AKOperationGenerator { parameters in
let ptch = AKOperation.exponentialSegment(
trigger: PitchEnvVCO.gate,
start: PitchEnvVCO.freqDecayDepth,
end: PitchEnvVCO.frequency.plus(PitchEnvVCO.frequencyOffset),
duration: PitchEnvVCO.freqDecayTime
)
let oscillator = AKOperation.morphingOscillator(
frequency: ptch,
amplitude: PitchEnvVCO.amplitude.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.0,
release: PitchEnvVCO.ampDecayTime
),
index: 1 // square
)
return oscillator
}
func getFrequencyOffsetInHz() -> Double { // converts frequency in Hz from semitones
// change n semitones relative to baseFreq : fn = 2power(n/12) × Fb
return (pow(2, vco1FreqOffset/12) * vco1Freq) - vco1Freq
}
func getFreqModDepthInHz() -> Double{ // converts semintone depth to Hz
let currentFrequency = vco1Freq + getFrequencyOffsetInHz()
let powow = pow(2.0, (vco1EGAmnt * vco1DecayDepth)/12.0) * currentFrequency
if powow != 0.0 {
return powow
}
return currentFrequency
}
func setOscParamsForBaseFreq() {
generator.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] = vco1Freq
generator.parameters[PitchEnvVCOSynthParameter.frequencyOffset.rawValue] = getFrequencyOffsetInHz()
generator.parameters[PitchEnvVCOSynthParameter.freqDecayDepth.rawValue] = getFreqModDepthInHz()
}
我想在以下上下文中计算 pow():
let generator = AKOperationGenerator { parameters in
let depth = PitchEnvVCO.freqDecayDepth.times(PitchEnvVCO.freqDecayAmount)
let wdth = pow(2.0, depth/12.0) * PitchEnvVCO.frequency // throws error
let ptch = AKOperation.exponentialSegment(
trigger: PitchEnvVCO.gate,
start: wdth,
end: PitchEnvVCO.frequency,
duration: PitchEnvVCO.freqDecayTime
)
let oscillator = AKOperation.squareWave(
frequency: ptch,
amplitude: PitchEnvVCO.amplitude.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.0,
release: PitchEnvVCO.ampDecayTime
)
)
return oscillator
}
并得到错误
Cannot convert value of type 'AKOperation' to expected argument type 'Double'
我已经像 filter envelope example 那样构建了我的发电机。我如何将 AKOperation 转换为它的 Double Value?谢谢!
重新思考这个概念后,我在 AKOperationGenerator 之外进行了计算,并添加了更多 AKOperation.parameters 来存储结果:
let generator = AKOperationGenerator { parameters in
let ptch = AKOperation.exponentialSegment(
trigger: PitchEnvVCO.gate,
start: PitchEnvVCO.freqDecayDepth,
end: PitchEnvVCO.frequency.plus(PitchEnvVCO.frequencyOffset),
duration: PitchEnvVCO.freqDecayTime
)
let oscillator = AKOperation.morphingOscillator(
frequency: ptch,
amplitude: PitchEnvVCO.amplitude.triggeredWithEnvelope(
trigger: PitchEnvVCO.gate,
attack: 0.01,
hold: 0.0,
release: PitchEnvVCO.ampDecayTime
),
index: 1 // square
)
return oscillator
}
func getFrequencyOffsetInHz() -> Double { // converts frequency in Hz from semitones
// change n semitones relative to baseFreq : fn = 2power(n/12) × Fb
return (pow(2, vco1FreqOffset/12) * vco1Freq) - vco1Freq
}
func getFreqModDepthInHz() -> Double{ // converts semintone depth to Hz
let currentFrequency = vco1Freq + getFrequencyOffsetInHz()
let powow = pow(2.0, (vco1EGAmnt * vco1DecayDepth)/12.0) * currentFrequency
if powow != 0.0 {
return powow
}
return currentFrequency
}
func setOscParamsForBaseFreq() {
generator.parameters[PitchEnvVCOSynthParameter.frequency.rawValue] = vco1Freq
generator.parameters[PitchEnvVCOSynthParameter.frequencyOffset.rawValue] = getFrequencyOffsetInHz()
generator.parameters[PitchEnvVCOSynthParameter.freqDecayDepth.rawValue] = getFreqModDepthInHz()
}