0

I have a Swift class that extends AudioKit's MIDIListener to handle receiving MIDI messages from external and virtual devices. It receives noteOn and noteOff messages like so:

func receivedMIDINoteOn(noteNumber: MIDINoteNumber,
                        velocity: MIDIVelocity,
                        channel: MIDIChannel,
                        portID: MIDIUniqueID? = nil,
                        offset: MIDITimeStamp = 0) {

  DispatchQueue.main.async {

    self.data.noteOn = Int(noteNumber)
    self.data.velocity = Int(velocity)
    self.data.channel = Int(channel)

  }

}

func receivedMIDINoteOff(noteNumber: MIDINoteNumber,
                         velocity: MIDIVelocity,
                         channel: MIDIChannel,
                         portID: MIDIUniqueID? = nil,
                         offset: MIDITimeStamp = 0) {

  DispatchQueue.main.async {

    self.data.noteOff = Int(noteNumber)
    self.data.channel = Int(channel)

  }

}

I'd like this class to also handle broadcasting or sending out MIDI messages created within the application out to these external and virtual devices. And I'm doing so with these calls:

midi.sendNoteOnMessage(noteNumber: MIDINoteNumber(note), velocity: MIDIVelocity(velocity))

And:

midi.sendNoteOffMessage(noteNumber: MIDINoteNumber(note), velocity: MIDIVelocity(velocity))

But, when I send messages the received methods also fire. This duplicates the intended behavior for the end user in the application. Is there a preferred way to not send messages out, but not have them received internal to the application? Or a different method I should be calling?

jonobr1
  • 1,013
  • 2
  • 11
  • 22
  • 2
    Sounds like you're opening up all MIDI inputs, probably with an AudioKit.MIDI.openInput() somewhere. I'm not exactly sure what it will look like, but you'll want to see if you can loop over available inputs and not open to internal comm. Alternatively, you can filter the incoming by the port and skip the internal messages. I'm commenting rather than leaving an answer because I don't know enough about your implementation to definitely say this is the right approach. – Aurelius Prochazka Jan 31 '21 at 02:59
  • 1
    That makes sense. Thanks for this! I think iterating through the inputs makes the most sense in my case. All the listening code is from the cookbook: https://github.com/AudioKit/Cookbook/blob/main/Cookbook/Cookbook/Recipes/MIDIMonitor.swift So, isolating out my internal listening when iterating through inputs makes sense to me. – jonobr1 Jan 31 '21 at 17:47

0 Answers0