How can I update the Sporth value inside AKOperationGenerator in real time?

Viewed 44

I have a class with sound that is generated with Sporth code. I want to change the pitch in the Sporth using the MIDI keyboard, but as a result value does not change and when I press any MIDI key I get only the default 440  Hz pitch.

var freq = 440

open class Synth: AKMIDIInstrument {
    var generator: AKOperationGenerator
    var sporth = "(\(freq) 0.5 sine) dup"
    @objc public override init(midiInputName: String? = nil) {
        generator = AKOperationGenerator(sporth: "")
        super.init(midiInputName: midiInputName)
        avAudioUnit = generator.avAudioUnit
        generator.start()
    }

    @objc open override func play(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel = 0) {
        freq = Int(noteNumber.midiNoteToFrequency())
        generator.sporth = sporth
        generator.trigger()
    }

    @objc open override func stop(noteNumber: MIDINoteNumber) {
        generator.stop()
    }
}

What am I doing wrong and how do I achieve a pitch change with MIDI?

0 Answers
Related