GithubHelp home page GithubHelp logo

fcanas / combinemidi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkj-is/combinemidi

0.0 0.0 0.0 64 KB

Connect MIDI using Combine to SwiftUI (or UIKit)

Home Page: https://youtu.be/2jTtqoYwQF0

License: MIT License

Swift 100.00%

combinemidi's Introduction

CombineMIDI

Build

Swift package made for easy connection of MIDI controllers to SwiftUI (or UIKit) using Combine and async-await.

This package was created as a part of UIKonf 2020 talk Combine: Connect MIDI signals to SwiftUI. It's main goal is to read messages from all MIDI sources and to be able to present these values in the user interface.

For more guidance, demos and history see materials for the talk:

Installation

Add this package to your Xcode project or add following line to your Package.swift file:

.package(url: "https://github.com/mkj-is/CombineMIDI.git", from: "0.2.0")

Features

  • Supports macOS 10.15+ and iOS 13.0+.
  • Type-safe wrapper for MIDI messages (events).
  • Wrapper for the C-style MIDI client in CoreMIDI.
  • Combine Publisher for listening to MIDI messages.
  • MIDI AsyncStream for processing MIDI messages.

Usage

First you need to create a MIDI client, it should be sufficient to create only one client per app (you can optionally pass name of the client as the initializer parameter):

let client = MIDIClient(name: "My app MIDI client")

Async-await

The first thing you probably want to do is to filter the messages which are relevant to you and get the values.

let stream = client.stream
    .filter { $0.status == .controlChange }
    .map(\.data2)

The you can process messages in a simple for-loop. The loop will be running indefinitely until the task enclosing it will be cancelled.

for await message in stream {
    ...
}

Combine

When using Combine, you create a publisher and it automatically connects to all sources and listens to all messages on subscribtion.

Do not forget to receive those events on the main thread when subscribing on the user interface. To prevent dispatching too soon the publisher is emitting on the thread used by CoreMIDI, so you can quickly filter all the messages.

cancellable = client
    .publisher()
    .filter { $0.status == .controlChange }
    .map { $0.data2 }
    .receive(on: RunLoop.main)
    .sink { value in
        ...
    }

Next steps

This library is small on purpose and there is a large potential for improvement:

  • New MIDI controllers are not detected when the subscription was already made.
  • All MIDI sources are connected during the first subscription, there is currently no way to select a particular device.
  • Sending messages back to other destinations using the client is not possible.

Contributing

All contributions are welcome.

Project was created by Matěj Kašpar Jirásek.

Project is licensed under MIT license.

combinemidi's People

Contributors

fcanas avatar maxdesiatov avatar mkj-is 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.