iOSDC2024

【iOSDC2024 #LT】1. About MIDI

【iOSDC2024 #LT】1. About MIDI

This article summarizes the content of the LT session presented at iOSDC2024, I Got an Electric Piano at Home, So I Made an App That Connects with It!, focusing on MIDI.
In this article, I will introduce an overview of the MIDI standard and the CoreMIDI framework used to handle it from Swift.

【iOSDC2024】I Got an Digital Piano at Home,So I Tried Making an App thatConnects with It!This article summarizes the contents of the announcement, "I Got an Digital Piano at Home, So I Tried Making an App that Connects with It!"...

What is MIDI?

MIDI (Musical Instruments Digital Interface) is a standard for exchanging musical information between electronic devices, such as an electric piano, and other digital musical instruments.
It is generally cross-compatible across different manufacturers, allowing for consistent input and output management across various devices.
In this presentation, I connected an electric piano to devices like iPhone, iPad, and VisionPro.
Note that MIDI is not a sound source itself, so even if a device supports the MIDI standard, it cannot directly produce sound on an iPhone or similar device (a sound source is required for sound to be produced on the iPhone).

There are different ways to connect, such as directly wiring the device and musical instrument or using Bluetooth or network connections.
However, these connection methods must be supported by both the device and the musical instrument.

By the way, when I inquired with YAMAHA support about whether I could connect the YAMAHA Clavinova CVP-905 that arrived at our house via Bluetooth MIDI, they informed me that it is possible using the MD-BT01, and I confirmed that it works.
If you want to test sound output with the app in an environment where you can’t bring the electric piano, you can connect via network to a Mac and use the Mac’s GarageBand app as a substitute for the electric piano.
For connection methods, refer to Apple’s documentation.
For how to use the piano keyboard in Mac’s GarageBand, see this guide.

Events That Can Be Captured with MIDI

Various events can be captured with MIDI, and this app uses the following types of events:

  1. Connection and disconnection events
  2. Key press events
    • Timestamp
    • Key pressed or released
    • Velocity (strength of key press)
    • Note number (pitch/position of the pressed key)

Key press events, except for the timestamp, are contained within 3 bytes of information, which can be extracted.
The note number is used to determine which key was pressed on the electric piano.
It can be obtained as a value from 0 to 127, with the middle key (middle C, fourth octave C, or C4) typically corresponding to 60 on a standard electric piano.
Note that some manufacturers treat the middle key as C4, while others treat it as C3, so if it differs from the device’s definition, you’ll need to adjust the setting with octave shifts, etc. (Reference).

What is Core MIDI?

Core MIDI is a framework that allows the use of MIDI on Apple Platforms.
With this, you can connect Apple devices, like an iPhone, to electronic instruments such as the electric piano in our home.
It can be implemented in Swift, but since Core MIDI is originally a C-language-based and low-level API, handling pointers and threads may be required.
For example, you might need to implement code using UnsafePointer, as shown below:

func MIDIReadProc(pktList: UnsafePointer<MIDIPacketList>, srcConnRefCon: UnsafeMutableRawPointer?) {
    let packetList: MIDIPacketList = pktList.pointee
    var packet: MIDIPacket = packetList.packet
    
    for _ in 0..<packetList.numPackets {
        let data = packet.data
        // Analysis data
        packet = MIDIPacketNext(&packet).pointee
    }
}

For those not familiar with low-level API operations, implementation might feel challenging (it was for me).
In that case, you might consider using the third-party Core MIDI wrapper MIDIKit.

What is MIDIKit?

MIDIKit is a modern Swift wrapper library for handling Core MIDI, supporting both MIDI 1.0 and MIDI 2.0.
This library simplifies and abstracts the complex low-level operations of the Core MIDI API, making it easier for developers to efficiently interact with MIDI devices.
It also has the following features:

  1. Multi-platform Support
    • MIDIKit supports macOS, iOS, and the newly introduced visionOS, providing consistent MIDI functionality across multiple Apple devices.
  2. MIDI 2.0 Support
    • The library automatically uses the appropriate Core MIDI API for the platform and defaults to MIDI 2.0 in environments that support it, enabling development with the latest MIDI standards.
  3. Strong Type Safety
    • MIDIKit enforces strong type safety in handling MIDI events, ensuring seamless interoperability between MIDI 1.0 and MIDI 2.0.
  4. User-friendly I/O
    • The library automates MIDI endpoint connection management and maintains the persistence of device connection states, allowing developers to focus on MIDI communication without worrying about complex connection management.
  5. Swift Playgrounds Support
    • MIDIKit can also be used in Swift Playgrounds on iPad and macOS, making learning and prototyping easier.

In addition, it provides Views and ObservableObjects to manage device connections in SwiftUI. With detailed documentation available from the developer, it might be easier to implement MIDI device connection functionality using MIDIKit, especially when implementing the UI with SwiftUI.
Many sample apps are also available, so if you encounter difficulties during implementation, you can often find solutions by referring to these samples.
MIDIKit Documentation

Conclusion

In this article, I summarized the MIDI standard and the CoreMIDI framework.
If you want to check the details, please refer to the MIDI and CoreMIDI documentation.
The next article is “2. Creating an App That Connects with an Electric Piano Using Core MIDI”.

【iOSDC2024 #LT】2. Using Core MIDI to create an app that works with a digital pianoThis article summarizes the part of the presentation "We got a digital piano at home, so we decided to make an app that works with the piano!", entitled "Using Core MIDI to create an app that works with a digital piano" ...

References

  1. What is MIDI? About MIDI Standards and Devices
  2. Core MIDI
  3. MIDIKit
  4. MIDIKit Documentation
0

COMMENT

Your email address will not be published. Required fields are marked *

CAPTCHA