Giter VIP home page Giter VIP logo

gong's Introduction

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:

  • The files in the /Sources/Gong/Core directory are straightforward, unopinionated wrappers around CoreMIDI's C APIs.
  • The files outside of the /Sources/Gong/Core directory are slightly more opinionated, but let you perform common tasks with a minimum of setup.

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.

Installation

The entire library:

pod 'Gong', '~> 1.2'

Just the CoreMIDI wrapper:

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

Just the CoreMIDI wrapper, plus MIDINote events:

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

Virtual MIDI buses

One of Gong's core use cases is sending MIDI messages into DAW software such as Ableton Live.

This necessitates setting up a virtual MIDI bus. The Ableton folks have a tutorial on how to do this.

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(pitch: c5)

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
        }
    }
    
}

gong's People

Contributors

dclelland avatar joebayld avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.