Swiftpack.co -  dnadoba/MIDIKit as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
dnadoba/MIDIKit
MIDI message decoder/encoder and typesafe wrapper around CoreMIDI in Swift
.package(url: "https://github.com/dnadoba/MIDIKit.git", from: "v1.1.2")

MIDIKit Documentation

MIDIKit is a Swift Package for decoding and encoding MIDI Packages and a Swifty Wrapper for CoreMIDI on iOS and macOS.

Playground

There is a playground in the root folder of this repository. You should be able to run it by first opening this Swift Package in Xcode by double-clicking the Package.swift and then opening the Playground in the Project Navigator.

The Playground lists all connected devices, logs all MIDI messages received. You can also send MIDI messages back to all connected devices by using the send(_:) function at the end of the Playground. Make sure you connect your MIDI device to your Mac before you start the Playground.

Connection basics

Create and start the MIDI Client

let midiClient = MIDIClient(name: "My Client")

do {
    try midiClient.start()
} catch {
    // catch error
}

Send messages

do {
    let outputPort = try midiClient.makeOutputPort(name: "Output") 
    outputPort.send(MIDIMessage.controlChange(channel: 1, controller: 11, value: 64), to: destination)
} catch {
    // catch error
}

Receive messages

let midiInputPort = try midiClient.makeInputPort(name: "Input", callback: { (result) in
    
    do {
        let packet = try result.get()
        let message = packet.message
        
        // Handle message
    } catch {
        // catch error
    }
})

MIDI Messages

MIDIKit supports the creation of all common MIDI Messages via dedicated enums. Here some examples:

    // Control change
    let messsage = MIDIMessage.controlChange(channel: 1, controller: 11, value: 64), to: destination)
    
    // Note on
    let message = MIDIMessage.noteOnEvent(channel: 1, key: 20, velocity: 100)
    
    // Sysex
    let data: [UInt8] = [0xF0, 0x01, 0x02, 0xF7]
    let message = MIDIMessage.systemExclusivMessage(data)

MIDI Network support

To connect your client to a MIDI network session, create a specialized MIDINetworkClient

let connection = MIDINetworkConnection(host: MIDINetworkHost(name: "Session 1", address: "192.168.0.100", port: 5006))
let midiClient = try MIDINetworkClient(name: "My Client", connection: connection)

do {
    try midiClient.start()
} catch {
    // catch error
}

When connected to a MIDI Network, you can use the dedicated source and destination endpoints for sending and receiving MIDI messages:

...

let source = midiClient.sourceEndpoint
let destination = midiClient.destinationEndpoint

// Send a MIDI message
do {
    let outputPort = try midiClient.makeOutputPort(name: "Output") 
    outputPort.send(MIDIMessage.controlChange(channel: 1, controller: 11, value: 64), to: destination)
} catch {
    // catch error
}

// Receive MIDI messages
let midiInputPort = try midiClient.makeInputPort(name: "Input", callback: { (result) in
    
    do {
        let packet = try result.get()
        let message = packet.message
        
        // Handle message
    } catch {
        // catch error
    }
})

midiInputPort.connect(to: source)

GitHub

link
Stars: 7
Last commit: 9 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Playground Update
25 weeks ago

The playground is now mentioned in the README.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API