Creating a MIDI device driver
At a glance
| Item | Summary |
|---|---|
| Purpose | Implement a configurable virtual MIDI driver as a driver extension that runs in user space in macOS and iPadOS. |
| App architecture | A C++, C/Objective-C header, Objective-C++, Swift sample with the source-visible chain CreatingMIDIDriverSampleApp → CreatingMIDIDriverSampleAppView → CreatingMIDIDriverSampleAppViewModel → CreatingMIDIDriverSampleAppUserClient → DriverKit / MIDIDriverKit APIs. |
| Main patterns | Model-View-ViewModel, Delegate or data-source callbacks, Publisher-backed observable state |
| Project style | 10 scanned source file(s) across C++, C/Objective-C header, Objective-C++, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | No structured execution marker indexed; callback threading requires source review. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, @Published, ObservableObject. |
| Key frameworks/packages | DriverKit, MIDIDriverKit, Foundation, os, SwiftUI; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── CreatingMIDIDriverSampleApp/
│ ├── CreatingMIDIDriverSampleApp.swift
│ ├── CreatingMIDIDriverSampleAppViewModel.swift
│ ├── CreatingMIDIDriverSampleAppUserClient.mm
│ ├── CreatingMIDIDriverSampleAppView.swift
│ ├── CreatingMIDIDriverSampleAppUserClient.h
│ ├── CreatingMIDIDriverSampleAppDriverKeys.h
│ └── CreatingMIDIDriverSampleApp.entitlements
├── CreatingMIDIDriverSampleAppExtension/
│ ├── CreatingMIDIDriverSampleAppDriverUserClient.cpp
│ ├── CreatingMIDIDriverSampleAppDevice.cpp
│ ├── CreatingMIDIDriverSampleAppDriver.cpp
│ └── CreatingMIDIDriverSampleAppDriverKeys.h
└── Configuration/
└── SampleCode.xcconfig
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C++, C/Objective-C header, Objective-C++, Swift.
- The verified tree contains 7 project/configuration file(s) and 10 source declaration(s).
Overall architecture
flowchart LR
N1["CreatingMIDIDriverSampleApp"]
N2["CreatingMIDIDriverSampleAppView"]
N3["CreatingMIDIDriverSampleAppViewModel"]
N4["CreatingMIDIDriverSampleAppUserClient"]
N5["DriverKit / MIDIDriverKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleApp.swift:10 — architecture anchor
@main
struct CreatingMIDIDriverSampleApp: App {
var body: some Scene {
WindowGroup {
CreatingMIDIDriverSampleAppView()
}
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into MIDIDriverKit.
Ownership and state
classDiagram
CreatingMIDIDriverSampleAppViewModel o-- State : state
CreatingMIDIDriverSampleAppViewModel *-- String : dextIdentifier
CreatingMIDIDriverSampleAppUserClient o-- io_object_t : ioObject
CreatingMIDIDriverSampleAppUserClient o-- io_connect_t : ioConnection
Ownership evidence
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:94 — stored dependency or nearest verified ownership anchor
class CreatingMIDIDriverSampleAppViewModel: NSObject {
// ...
@Published private var state: CreatingMIDIDriverSampleAppStateMachine.State = .unloaded
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
CreatingMIDIDriverSampleAppViewModel |
State (state) |
stores or receives | Owning lexical scope |
CreatingMIDIDriverSampleAppViewModel |
String (dextIdentifier) |
owns value state | Initialized by the owner; the binding is immutable |
CreatingMIDIDriverSampleAppUserClient |
io_object_t (ioObject) |
stores or receives | Owning Objective-C implementation |
CreatingMIDIDriverSampleAppUserClient |
io_connect_t (ioConnection) |
stores or receives | Owning Objective-C implementation |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppView.swift:11 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:94 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:114 |
| Source import | DriverKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CreatingMIDIDriverSampleAppExtension/CreatingMIDIDriverSampleAppDriver.cpp:12 |
| Source import | MIDIDriverKit |
The cited file imports this module; runtime use and architectural role are not inferred. | CreatingMIDIDriverSampleAppExtension/CreatingMIDIDriverSampleAppDevice.cpp:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.h:10 |
| Source import | os |
The cited file imports this module; runtime use and architectural role are not inferred. | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:9 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleApp.swift:8 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleApp.swift:11 — representative type boundary
@main
struct CreatingMIDIDriverSampleApp: App {
// ...
CreatingMIDIDriverSampleAppView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
CreatingMIDIDriverSampleApp |
Application entry and top-level composition | App |
CreatingMIDIDriverSampleAppViewModel |
UI-facing state and feature coordination | NSObject |
CreatingMIDIDriverSampleAppView |
User-interface presentation and input forwarding | View |
CreatingMIDIDriverSampleAppUserClient |
External-system or framework client | NSObject |
CreatingMIDIDriverSampleAppStateMachine |
Owns feature behavior and collaborator lifecycle | Concrete collaborators/imported frameworks |
State |
Represents mutable feature state | Concrete collaborators/imported frameworks |
Event |
Defines a closed set of feature states or choices | Concrete collaborators/imported frameworks |
CreatingMIDIDriverSampleAppDriverUserClient_IVars |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
CreatingMIDIDriverSampleAppDevice_IVars |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
CreatingMIDIDriverSampleAppDriver_IVars |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
ioObject (CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.mm:13) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
ioConnection (CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.mm:14) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
state (CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:94) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
dextIdentifier (CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:96) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.mm:13 — representative boundary
@interface CreatingMIDIDriverSampleAppUserClient()
@property io_object_t ioObject;
@property io_connect_t ioConnection;
@endSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | CreatingMIDIDriverSampleApp |
The source’s App suffix makes this role explicit. |
| External-system or framework client | CreatingMIDIDriverSampleAppUserClient |
The source’s Client suffix makes this role explicit. |
| User-interface presentation and input forwarding | CreatingMIDIDriverSampleAppView |
The source’s View suffix makes this role explicit. |
| UI-facing state and feature coordination | CreatingMIDIDriverSampleAppViewModel |
The source’s ViewModel suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:91 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
| Delegate or data-source callbacks | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:150 |
Callback protocols invert event delivery back into the sample’s owner. |
| Publisher-backed observable state | CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift:94 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: CreatingMIDIDriverSampleApp; Client: CreatingMIDIDriverSampleAppUserClient; View: CreatingMIDIDriverSampleAppView; ViewModel: CreatingMIDIDriverSampleAppViewModel.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
onUnloaded,onActivatingOrNeedsApproval,onActivated,onActivationError,process,activateMyDext,activateExtension,deactivateExtension. - Files:
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleApp.swift,CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift,CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.mm,CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppView.swift,CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.h.
Architecture takeaways
CreatingMIDIDriverSampleAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches DriverKit, MIDIDriverKit, SwiftUI, IOKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleApp.swift |
Cited implementation, CreatingMIDIDriverSampleApp, SwiftUI |
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppViewModel.swift |
Cited implementation, CreatingMIDIDriverSampleAppViewModel, @Published, ObservableObject, os, CreatingMIDIDriverSampleAppStateMachine, State, Event |
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.mm |
ioObject, ioConnection, CreatingMIDIDriverSampleAppUserClient |
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppView.swift |
SwiftUI state property wrapper, CreatingMIDIDriverSampleAppView, CreatingMIDIDriverSampleAppView_Previews |
CreatingMIDIDriverSampleAppExtension/CreatingMIDIDriverSampleAppDriver.cpp |
DriverKit, CreatingMIDIDriverSampleAppDriver_IVars |
CreatingMIDIDriverSampleAppExtension/CreatingMIDIDriverSampleAppDevice.cpp |
MIDIDriverKit, CreatingMIDIDriverSampleAppDevice_IVars |
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppUserClient.h |
Foundation, CreatingMIDIDriverSampleAppUserClient |
CreatingMIDIDriverSampleAppExtension/CreatingMIDIDriverSampleAppDriverUserClient.cpp |
CreatingMIDIDriverSampleAppDriverUserClient_IVars |
CreatingMIDIDriverSampleApp/CreatingMIDIDriverSampleAppDriverKeys.h |
Feature implementation |
CreatingMIDIDriverSampleAppExtension/CreatingMIDIDriverSampleAppDriverKeys.h |
Feature implementation |