I'd like to know how to control an AKOscillator
's frequency using an AKSequencer
, however the few examples(1, 2) I've seen online only show how to control an AKSampler
with the AKSequencer
.
Here's a simplified example from AudioKit's GitHub page:
// relevant class properties
var seq: AKSequencer?
var syn1 = AKSampler()
// viewDidLoad
seq = AKSequencer(filename: "seqDemo", engine: AudioKit.engine)
seq?.enableLooping()
seq!.avTracks[1].destinationAudioUnit = syn1.samplerUnit
What I expected:
Based on the above example, I'd expect to be able to do something like this:
var voice1 = Voice() // replaces the AKSampler
seq = AKSequencer(filename: "seqDemo", engine: AudioKit.engine)
seq.loopOn()
//seq.avTracks[0].destinationAudioUnit = voice1.oscillator.avAudioNode
seq.note = voice1.oscillator.frequency // "note" doesn't actually exist in the API
This doesn't do the trick obviously.
What is the proper set-up that will allow me to control an AKOscillator with the AKSequencer?
@Aurelius's answer is obviously correct but it doesn't definitively answer the question. In an effort to help the next person who has this issue I'll share my solution.
The question is:
How do I control an AKOscillator
with the AKSequencer
.
The Answer is:
You can't control it directly. As Aurelius stated, you need to wrap your oscillator in a midi instrument, which takes a little work.
AKVoice
, you can find an example of this here.AKPolyphonicInstrument
) that @Aurelius described. This is what the sequencer has the ability to speak to! You can see an example of this here.Now that our oscillator has the facilities to speak to the sequencer, we can implement the MollyBChimes approach like so:
At the class level create an instance of your new AKPolyphonicInstrument, for me it looks like this:
var osc1 = SynthVoiceInstrument(voiceCount: 4)
var sequencer = AKSequencer()
var mixer = AKMixer()
var melody: AKMIDIInstrument?
Then inside your set-up function or viewDidLoad you'll do the following:
melody = AKMIDIInstrument(instrument: osc1)
melody?.enableMIDI(midi.midiClient, name: "melodicSound midi in")
mixer.connect(melody!)
AudioKit.output = pumper
AudioKit.start()
sequencer.newTrack()
sequencer.setLength(seqLen)
sequencer.tracks[0].setMIDIOutput((melody?.midiIn)!)
/*
melody generator function just to hear some sounds
see Melody Generator link below.
*/
genNewMelodicSequence(minor: true)
sequencer.loopEnabled = true
sequencer.setBPM(100)
sequencer.play()
Melody Generator
You'll want to wrap the oscillator in an midi instrument and use that. Check out the sequencer demo for an example using an fm oscillator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With