0

I'd like to use my kAudioUnitType_MusicEffect AU in an AVAudioEngine graph. So I try to call:

[AVAudioUnitMIDIInstrument instantiateWithComponentDescription:desc options:kAudioComponentInstantiation_LoadInProcess completionHandler:

but that just yeilds a normal AVAudioUnit, so the midi selectors (like -[AVAudioUnit sendMIDIEvent:data1:data2:]:) are unrecognized. It seems AVAudioUnitMIDIInstrument instantiateWithComponentDescription only works with kAudioUnitType_MusicDevice.

Any way to do this? (Note: OS X 10.11)

Taylor
  • 5,871
  • 2
  • 30
  • 64

2 Answers2

0

Make a subclass and call instantiateWithComponentDescription from its init.

Gory details and github project in this blog post

http://www.rockhoppertech.com/blog/multi-timbral-avaudiounitmidiinstrument/#avfoundation

This uses Swift and kAudioUnitSubType_MIDISynth but you can see how to do it.

Gene De Lisa
  • 3,628
  • 1
  • 21
  • 36
  • I don't see how subclassing will help. I still have to call `instantiateWithComponentDescription` which doesn't work for `kAudioUnitType_MusicEffect`. If I switch it to `kAudioUnitType_MusicDevice` then I get an assertion failure when trying to connect the audio input. – Taylor May 17 '16 at 17:49
0

This works. It's a subclass. You add it to the engine and you route the signal through it.

class MyAVAudioUnitDistortionEffect: AVAudioUnitEffect {

override init() {
    var description                   = AudioComponentDescription()
    description.componentType         = kAudioUnitType_Effect
    description.componentSubType      = kAudioUnitSubType_Distortion
    description.componentManufacturer = kAudioUnitManufacturer_Apple
    description.componentFlags        = 0
    description.componentFlagsMask    = 0
    super.init(audioComponentDescription: description)
}

func setFinalMix(finalMix:Float) {

    let status = AudioUnitSetParameter(
        self.audioUnit,
        AudioUnitPropertyID(kDistortionParam_FinalMix),
        AudioUnitScope(kAudioUnitScope_Global),
        0,
        finalMix,
        0)

    if status != noErr {
        print("error \(status)")
    }
}
Gene De Lisa
  • 3,628
  • 1
  • 21
  • 36