Custom AVAudioUnitMIDIInstrument subclass

Howdy

I'm trying to subclass the AVAudioUnitMIDIInstrument 'abstract class', so I can have another type of synth to respond to an AVAudioSequencer.


What exactly do I need to override to create my own behavior for a sequencer who is pointing at this instrument?


I've tried overriding the startNote and stopNote methods, but they don't ever get called.


public override func startNote(note: UInt8, withVelocity velocity: UInt8, onChannel channel: UInt8) {
        super.startNote(note, withVelocity: velocity, onChannel: channel)
        print("Play \(note) with velocity \(velocity)") //I never see this
   
    }


I've got the unit connected and created just fine, the sequencer outputs sinewaves so I know it is triggering something, somewhere, I'm just not sure how to get into that functionality.


Thank yall


For what it's worth, I init the custom class like this:

var description = AudioComponentDescription()
        description.componentType         = kAudioUnitType_MusicDevice
        description.componentSubType      = kAudioUnitSubType_MIDISynth
        description.componentManufacturer = kAudioUnitManufacturer_Apple
        description.componentFlags        = 0
        description.componentFlagsMask    = 0
      
super.init(audioComponentDescription: description)
Answered by in 100484022

In general, these classes are customized by initializing them with the AudioComponentDescription for a different Audio Unit (one which shares the componentType). The outer AVAudioUnitMIDIInstrument functions primarily as a wrapper around the full functionality built into the AU. Your example of "init'ing the custom class" initialized the AVAudioUnitMIDIInstrument to contain an instance of the MIDISynth Audio Unit.


I am not sure what you mean by "getting into that functionality". Could you describe in more detail what you are trying to do, please?


-DS

Accepted Answer

In general, these classes are customized by initializing them with the AudioComponentDescription for a different Audio Unit (one which shares the componentType). The outer AVAudioUnitMIDIInstrument functions primarily as a wrapper around the full functionality built into the AU. Your example of "init'ing the custom class" initialized the AVAudioUnitMIDIInstrument to contain an instance of the MIDISynth Audio Unit.


I am not sure what you mean by "getting into that functionality". Could you describe in more detail what you are trying to do, please?


-DS

Thank you for your reply.

So if the component type is kAudioUnitType_MusicDevice, and I'm setting subtype to kAudioUnitSubType_MIDISynth, what could I change that to? I can only find that the

kAudioUnitSubType_Sampler is a valid subtype.

I'm trying to create my own instrument that will react to a AVMusicSequence if I set it as a destination for a music track. Basically, I am trying to do the same thing AVAudioUnitSampler does, but using my own generator.


I'm not sure what subtype to use, if midiSynth is not the correct one.


To re-iterate my question, I'm not sure what function the sequencer triggers on AVAudioUnitMidiInstrument that I would need to override...if that's how it's done at all. I can get the sequencer to trigger my sub-classed instrument, but it only plays sine waves, and I'm not sure how to say, inject a print to the terminal into it so I could see the data being passed (or in reality, do some new functionality to generate sound).


(please pardong the formatting of this message - I can't find a way to remove it in the editor).

I wrote an example on Github and blog post on how to do this.

Custom AVAudioUnitMIDIInstrument subclass
 
 
Q