Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I control an oscillator's frequency with a sequencer

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.

Question:

What is the proper set-up that will allow me to control an AKOscillator with the AKSequencer?

like image 383
Dan Beaulieu Avatar asked Mar 13 '23 09:03

Dan Beaulieu


2 Answers

@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.

  • Your oscillator should be contained in a class that subclasses AKVoice, you can find an example of this here.
  • Next you'll create your wrapping "midi instrument"(aka. 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

like image 72
Dan Beaulieu Avatar answered Apr 09 '23 12:04

Dan Beaulieu


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.

like image 35
Aurelius Prochazka Avatar answered Apr 09 '23 12:04

Aurelius Prochazka