Decode Midi Timecode with SwiftUI and CoreMIDI

105 views Asked by At

In my previous question i was able to get the hosts in to my MACOS app and see a list. pretty easy and straight forward code. I am now looking to retrieve Midi Timecode over network and decode it in to a clock. The decoding part is there, however, getting the app to connect to the session and retrieve the clock information is somewhat difficult, since there is no info available online almost.

    MIDIInputPortCreate(client, "MIDI Timecode Receiver Input Port" as CFString, { packetList, _  in
          let packets = packetList.pointee
          let packet = packets.packet
          let statusByte = packet.data.0



          // Check if the MIDI message is a Timecode message
          if statusByte == 0xF1 {
              // Extract the timecode values from the MIDI message
              let frames = Int(packet.data.1 & 0x1F)
              let seconds = Int(packet.data.2 & 0x3F)
              let minutes = Int(packet.data.3 & 0x3F)
              let hours = Int(packet.data.4 & 0x1F)

              // Update the timecode message with the new timecode values
              DispatchQueue.main.async {
                  midiTimecode = "\(hours):\(minutes):\(seconds):\(frames)"
              }
          }
      }, nil, &endpoint)

        let networkSession = MIDINetworkSession.default()
       networkSession.addConnection(nil, port: outputPort, to: networkSession.sourceEndpoint())

Anyone any luck with this or know how to get timecode out of a session?

EDIT: found this link, but does not handle the incoming timecode https://developer.apple.com/documentation/coremidi/midi_services/incorporating_midi_2_into_your_apps

1

There are 1 answers

0
edonv On

If it’s not too late to help, this could be useful. I’m working on a project involving MIDI and found this package that wraps CoreMIDI with much more robust and manageable code (including some stuff with SwiftUI): https://github.com/orchetect/MIDIKit

Not only does it include subpackages for all sorts of things (MIDI IO, MIDI types, etc), but there’s a subpackage for syncing that uses Timecode (MIDIKitSync). It has types and classes for generating streams in either direction of MIDI timecode, as well as encoding/decoding said streams. The timecode related code I believe also relies on another package from the same developer: https://github.com/orchetect/TimecodeKit

It seems like you might not necessarily want/need to migrate to MIDIKit, but its source code could be helpful to see how they decode MIDI timecode messages. To clarify, it seems like you’re already successfully connecting via MIDINetworkSesson?

P.S. If so, I would separately love to see some of that code, as I'm having some trouble with that myself.