0

I want to calculate pow() in the following context:

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
}

and get the error

Cannot convert value of type 'AKOperation' to expected argument type 'Double'

I have build my generator like in the filter envelope example. How could I cast AKOperation to its Double Value? thnx!

headkit
  • 3,307
  • 4
  • 53
  • 99
  • If you're following that example, `PitchEnvVCO.frequency` is an `AKOperation`, and you're trying to use that as a multiplier to the result of your `pow()` operation. What are you expecting `PitchEnvVCO.frequency` to be? – DonMag Mar 12 '18 at 18:37
  • Yes, that I am aware of and that is my problem. I need a Double to operate a pow() on it. How could I cast the AKOperation value to a Double? The error is thown because depth is an AKOperation. And I need to calculate pow() of the value the AKOperation is holding. The multiplication would be the second step. – headkit Mar 12 '18 at 20:06
  • I have a feeling I do not understand the real way AKOperation is meant to work. – headkit Mar 12 '18 at 20:11
  • I mean: in the filter cutoff example the value changes write to synth.cutoff = frequency, where frequency is a Double, and this Double I need to read, too. – headkit Mar 13 '18 at 11:19
  • What is the type of `PitchEnvVCO.frequency`? Is it a Double? Set a breakpoint on `let depth = ...` and inspect it in the debugger. Or, add this line *before* the `let width = ...` line: `print(PitchEnvVCO.frequency, type(of: PitchEnvVCO.frequency))` and see what the output is. – DonMag Mar 13 '18 at 12:48
  • The type is AKOperation, as expected. I am not sure I understand the construct of AKOperation correctly. – headkit Mar 13 '18 at 14:22
  • OK, then you'll need to step in with debug and inspect that object, to see how you can access a `Double` (or float) value of `.frequency`. – DonMag Mar 13 '18 at 14:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166762/discussion-between-headkit-and-donmag). – headkit Mar 13 '18 at 14:25

1 Answers1

0

After rethinking the concept I did the calculation outside the AKOperationGenerator and added more AKOperation.parameters to store the results:

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()
}
headkit
  • 3,307
  • 4
  • 53
  • 99