Search code examples
iosobjective-ccore-audioaudiounitcoremidi

CoreAudio: Ausampler Unit polyphony


I'm currently trying to use the Ausampler Unit efficiently. I used the code from apple's sample: https://developer.apple.com/library/ios/#samplecode/LoadPresetDemo/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011214-Intro-DontLinkElementID_2

The downside seems to be that it instantly stops a note/sampe if the same note comes in again. So if I send e.g. midi note 60 with the trombone preset loaded. And then while holding this I send note 60 again. It cuts off the first note event. Does anyone know if/how it is possible to hold every single note till a note off event (of this specific note) comes in?

Other question: How can I change the volume of a note after it has been send? E.g. I sent midi note 60 and 70 with 100 velocity. While note 60 still plays I want to reduce the volue but keep note 70 up. This would be called midi aftertouch.

Hope you can help :)


Solution

  • The AUSampler (kAudioUnitSubType_Sampler) is monotimbral (reference and here) so in order to play two notes of the same pitch simultaneously, you will need two "instruments" - i.e. to instances of the AUSampler. In my projects, I have found that this doesn't add much to the memory overhead since the sound resources get shared.

    As for aftertouch - I haven't explored that but it seems like the property you want is kAUGroupParameterID_KeyPressure_FirstKey (References here, and maybe here).

    Supporting the kAUGroupParameterID_KeyPressure parameter indicates to 
    hosts that your audio unit supports polyphonic "aftertouch" key pressure.
    
    Each of the 128 MIDI key numbers can have its own value for polyphonic 
    aftertouch. To respond to aftertouch for a particular key, your audio 
    unit needs to support an additional parameter specifically for that key. 
    The aftertouch parameter ID for a given MIDI key is equal to the MIDI
    key number plus 256. For example, the aftertouch parameter ID for MIDI 
    key #60 (middle C) is:
    
    60 + kAUGroupParameterID_KeyPressure_FirstKey = 316
    

    If you get aftertouch working, post back some sample code.