get value of property returned by MIDIObjectPropertyChangeNotification

85 Views Asked by At

I'm using the MIDI libraries of AudioKit with an app. Whilst that framework is proprietary, the code in question is returning a native struct, namely MIDIObjectPropertyChangeNotification.

Here is the code, where MIDIListener is a protocol defined by AudioKit and MIDIConnectionManager is my class for managing MIDI. In this callback, I am listening for connections and disconnections via RTP-MIDI

extension MIDIConnectionManager: MIDIListener {
func receivedMIDIPropertyChange(propertyChangeInfo: MIDIObjectPropertyChangeNotification) {
let propertyName = Unmanaged.fromOpaque(propertyChangeInfo.propertyName.toOpaque()).takeUnretainedValue() as CFString
        if propertyChangeInfo.objectType == .entity && propertyName as String == "apple.midirtp.session" {
// do something in response
}
}

As I connect and disconnect my app, the callback fires. But all it seems to know about is the property that's changed, not its value. I assumed that would also be in the MIDIObjectPropertyChangeNotification struct. But it isn't (https://developer.apple.com/documentation/coremidi/midiobjectpropertychangenotification).

I see that it does expose MIDIObjectRef ("The object whose property changed.", https://developer.apple.com/documentation/coremidi/midiobjectpropertychangenotification/1495180-object) but that refers only to "var propertyName: Unmanaged < CFString > The name of the modified property."

So I figure there must be a way of getting all the properties of that object and sure enough func MIDIObjectGetProperties (https://developer.apple.com/documentation/coremidi/1495206-midiobjectgetproperties)

But I'm fairly new to Swift and here is where my skills grind to a halt! Where next? I don't understand how to use that function and I'm a bit spooked by UnsafeMutablePointer!

1

There are 1 best solutions below

0
Jim On

Ok partial answer here Need help converting (CFPropertyListRef *)nsdictionary to swift

From which I derive

var unmanagedProperties: Unmanaged<CFPropertyList>?
            MIDIObjectGetProperties(propertyChangeInfo.object, &unmanagedProperties, false)
            if let midiProperties: CFPropertyList = unmanagedProperties?.takeUnretainedValue() {
                let midiDictionary = midiProperties as! NSDictionary
                print("Midi properties: \n \(midiDictionary)");
            }

Hmm. All that funky close-to-the-metal pointer jazz makes me nervous :-|

But the object has a property "peers" that seems like a hook to detect a connection or otherwise.