Latest 0.2.2
Homepage https://github.com/dclelland/Gong
License MIT
Platforms ios 8.0, osx 10.10
Authors

Gong

Gong is a simple library for sending and recieving MIDI messages to and from virtual and physical devices.

Gong aims to provide a fairly transparent Swift interface to Apple’s CoreMIDI library.

The library is built in two layers:

More specifically: there is a global MIDI singleton, which:

  • Creates a MIDIClient and subscribes to MIDINotice events (e.g., MIDI device connections and disconnections).
  • Creates a MIDIInput, connects it to all available MIDISource instances and subscribes to MIDIPacket events (e.g., MIDI note or control change messages).
  • Creates a MIDIOutput, which you can use to send MIDIPackets to devices.
  • Implements an observer pattern so classes implementing the MIDIObserver protocol can recieve MIDINotice and MIDIPacket messages.
  • Wraps any CoreMIDI wrapper calls that might throw in try ~ catch blocks and prints any exceptions that get thrown.

If you prefer to write this kind of thing yourself, the CoreMIDI wrapper can be installed independently of the opinionated code.

An example project is provided to help you get started.

Todo list

  • Swift 4 support
  • Test for memory leaks
  • MIDINotification/MIDINotice name issue

Installation

The entire library:

pod 'Gong', '~> 0.2'

Just the CoreMIDI wrapper:

pod 'Gong/Core', '~> 0.2'

Just the CoreMIDI wrapper, plus MIDINote events:

pod 'Gong/Events', '~> 0.2'

Core library architecture

Class hierarchy

MIDIObject <----+--+ MIDIClient
                |
                +--+ MIDIPort <------+--+ MIDIInput
MIDINotice      |                    |
                |                    +--+ MIDIOutput
MIDIPacket      +--+ MIDIDevice
                |
                +--+ MIDIEntity
MIDIError       |
                +--+ MIDIEndpoint <--+--+ MIDISource
                                     |
                                     +--+ MIDIDestination

Data flow

MIDIClient                                 MIDIDevice
 creates                                      owns
    +                                          +
    |                                          v
    |                                      MIDIEntity
    |                                         owns
    |                                          +
    v          receives packets from           v
MIDIInput <------------------------------+ MIDISource
   and           sends packets to             and
MIDIOutput +---------------------------> MIDIDestination

Common tasks

Starting the MIDI client:

MIDI.connect()

Stopping the MIDI client:

MIDI.disconnect()

Listing devices:

for device in MIDIDevice.all {
    print(device.name)
}

Sending MIDI events:

guard let device = MIDIDevice(named: "minilogue"), let output = MIDI.output else {
    return
}

let note = MIDINote(key: 60)

device.send(note, via: output)

Receiving MIDI packets:

class ViewController: NSViewController {

    override func viewWillAppear() {
        super.viewWillAppear()

        MIDI.addObserver(self)
    }

    override func viewDidDisappear() {
        super.viewDidDisappear()

        MIDI.removeObserver(self)
    }

}

extension ViewController: MIDIObserver {

    func receive(_ notice: MIDINotice) {
        print(notice)
    }

    func receive(_ packet: MIDIPacket, from source: MIDISource) {
        switch packet.message {
        case .noteOn, .noteOff, .controlChange, .pitchBendChange:
            print(packet.message, source)
        default:
            break
        }
    }

}

Latest podspec

{
    "name": "Gong",
    "version": "0.2.2",
    "summary": "Gong is a MIDI library for iOS and macOS.",
    "homepage": "https://github.com/dclelland/Gong",
    "license": {
        "type": "MIT"
    },
    "authors": {
        "Daniel Clelland": "[email protected]"
    },
    "source": {
        "git": "https://github.com/dclelland/Gong.git",
        "tag": "0.2.2"
    },
    "source_files": "Sources/*.swift",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10"
    },
    "pushed_with_swift_version": "3.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Sources/Core/**/*.swift",
            "frameworks": "CoreMIDI"
        },
        {
            "name": "Events",
            "source_files": "Sources/Events/**/*.swift",
            "dependencies": {
                "Gong/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This