ofxMidi

repository·master·Indexed 43 days ago

https://github.com/danomatika/ofxmidi

An openFrameworks addon providing MIDI input and output capabilities. It utilizes RtMidi for macOS, Windows, and Linux, and PGMidi for iOS. Core components include ofxMidiIn for receiving messages via ofxMidiListener, ofxMidiOut for sending data using a stream interface, and ofxMidiMessage for representing received MIDI data.

Tokens
2.3K
Snippets
6
Records
14
Agent score
55%

What's inside ofxMidi

  1. What is PGMidi?

    master
    PGMidi is a simplified library for accessing MIDI devices via the CoreMidi framework on iOS. It provides a high-level interface to handle MIDI device attachment, detachment, and MIDI input reception. It is designed to be easily incorporated into iOS applications, with support for both ARC and non-ARC projects.
  2. Overview of ofxMidi core components

    master

    ofxMidi provides MIDI I/O capabilities for openFrameworks applications. It consists of three primary components:

    • ofxMidiIn: A single MIDI input port. To receive messages, you must derive a class from ofxMidiListener.
    • ofxMidiMessage: Represents a single received MIDI message.
    • ofxMidiOut: A single MIDI output port that supports a stream << interface for sending data.

    The addon uses RtMidi for macOS, Windows, and Linux, and PGMidi for iOS.

  3. Overview of RtMidi C++ classes

    master

    RtMidi provides a common C++ API for real-time MIDI input and output across multiple platforms (Linux, macOS, Windows, iOS, and Android).

    Core abstractions:

    • RtMidiIn: Handles MIDI input functionality. Each instance supports a single MIDI connection.
    • RtMidiOut: Handles MIDI output functionality. Each instance supports a single MIDI connection.

    Key technical details:

    • Data Format: MIDI data is passed to the user as raw bytes using std::vector<unsigned char>.
    • Timing: RtMidi does not provide internal timing functionality; output messages are sent immediately. Input messages are timestamped with delta times in seconds using a double floating point type.
    • Portability: Designed to be easily included in projects via a single header and a single source file.
  4. How to use the PGMidi class

    master
    The core functionality is contained in the PGMidi class located in the Sources/PGMidi directory. This class handles the heavy lifting of MIDI management and uses a delegate interface to report events (such as device attachment/detachment or incoming MIDI data) to your application. To use it, you should implement the provided delegate interface to react to these events.
  5. Install ofxMidi into openFrameworks

    master

    To use ofxMidi, ensure you have openFrameworks installed. Download the ofxMidi source and place the addon directory into your openFrameworks addons folder:

    openFrameworks/addons/ofxMidi/

    git clone git://github.com/danomatika/ofxMidi.git
  6. Run ofxMidi example projects

    master

    Example projects are located in ofxMidi/midiInputExample, ofxMidi/midiOutputExample, and ofxMidi/midiExampleIOS.

    Since project files are not included, you must use the openFrameworks ProjectGenerator to create them:

    1. Click Import in ProjectGenerator.
    2. Select the parent folder (ofxMidi) and then the specific example folder (e.g., midiOutputExample).
    3. Click Open, then click Update.

    Platform-specific execution:

    • macOS: Open the Xcode project, select the Debug scheme, and run.
    • Linux: Open the QT Creator project or use the Makefile:
      make
      make run
    • Windows: Open the Visual Studio project, build, and run.
    make
    make run
  7. Configure MIDI routing by platform

    master

    Depending on your operating system, you may need additional tools to route MIDI signals between applications:

    • macOS: Use MIDI Patchbay.
    • Linux: Use ALSA utilities like aconnect or aconnectgui, or the qjackctl GUI.
    • Windows: Windows lacks a built-in virtual MIDI routing system. Use loopMIDI to create virtual ports for connecting to synths or DAWs.
  8. Compile RtMidi test programs on Unix

    master

    To compile the example programs included in the tests/ directory on Unix-based systems (Linux/macOS):

    1. If you cloned the repository via git, run ./autogen.sh first.
    2. Run ./configure in the top-level directory.
    3. Navigate to the tests/ directory and run make to compile the test programs.
    ./autogen.sh
    ./configure
    cd tests && make
  9. Configure MIDI functionality and compatibility

    master

    To integrate PGMidi into your iOS project:

    1. Enable MIDI functionality: Use the header file provided in the PGMidi directory to selectively enable MIDI features in your application.
    2. Handle CoreMidi availability: PGMidi requires iOS 4.2 or higher to function because it relies on the CoreMidi framework.
    3. Weak Linking: If your application needs to support devices running iOS versions earlier than 4.2, you must link against CoreMidi weakly to prevent crashes on older devices where the framework is unavailable.
  10. Add ofxMidi to an existing project (Manual)

    master

    If you are not using the ProjectGenerator, follow these manual steps to integrate ofxMidi:

    Xcode

    1. Create a group ofxMidi in your addons group.
    2. Drag ofxMidi/src and ofxMidi/libs into this group.
      • For macOS: Remove src/ios and libs/pgmidi references.
      • For iOS: Remove src/desktop and libs/rtmidi references.
    3. Add the CoreMIDI.framework via the Project Summary tab -> Linked Frameworks & Libraries.
    4. Add these paths to your Project.xconfig:
      ../../../addons/ofxMidi/src
      ../../../addons/ofxMidi/libs/rtmidi

    Linux (Makefiles/CodeBlocks)

    Add ofxMidi to the end of your addons.make file.

    Windows (CodeBlocks & Visual Studio)

    1. Add ofxMidi/src and ofxMidi/libs/rtmidi to your project tree.
    2. Add the following search paths to your project settings:
      ..\..\..\addons\ofxMidi\src
      ..\..\..\addons\ofxMidi\libs\rtmidi
  11. Avoid segmentation faults with static ofxMidi objects on Linux

    master

    On Linux, creating static ofxMidiIn or ofxMidiOut objects can cause segmentation faults because they may be initialized before the ALSA library is ready.

    Solution: Use a std::shared_ptr and initialize the object inside your application's setup() function rather than in a constructor or as a global static object.

    Example:

    In .h:

    class MyClass {
        static std::shared_ptr<ofxMidiOut> s_midiOut;
    };

    In .cpp:

    std::shared_ptr<ofxMidiOut> MyClass::s_midiOut;
    
    void MyClass::setup() {
      if(s_midiOut == nullptr) {
          s_midiOut = std::shared_ptr<ofxMidiOut>(new ofxMidiOut("ofxMidi Client"));
      }
    }
    class MyClass {
        ...
        static std::shared_ptr<ofxMidiOut> s_midiOut;
    }
    
    std::shared_ptr<ofxMidiOut> MyClass::s_midiOut;
    
    // initialize somewhere else
    void MyClass::setup() {
      if(s_midiOut == NULL) {
          s_midiOut = std::shared_ptr<ofxMidiOut>(new ofxMidiOut("ofxMidi Client"));
      }
    }