Supporting real-time ML inference on the CPU
At a glance
| Item | Summary |
|---|---|
| Purpose | Add real-time digital signal processing to apps like Logic Pro X and GarageBand with the BNNS Graph API. |
| App architecture | A C/Objective-C header, Objective-C++, Python, Swift sample with the source-visible chain BNNSBitcrusherApp → ContentView → AudioUnitViewModel → SimplePlayEngine → SwiftUI / AudioToolbox APIs. |
| Main patterns | Model-View-ViewModel, View-controller organization, Protocol-oriented abstraction, Builder, Publisher-backed observable state |
| Project style | 23 scanned source file(s) across C/Objective-C header, Objective-C++, Python, Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── BNNSBitcrusher/
│ ├── BNNSBitcrusherApp.swift
│ ├── Model/
│ │ ├── AudioUnitViewModel.swift
│ │ └── AudioUnitHostModel.swift
│ ├── Common/
│ │ ├── Audio/
│ │ │ └── SimplePlayEngine.swift
│ │ ├── MIDI/
│ │ │ └── MIDIManager.swift
│ │ └── UI/
│ │ └── ViewControllerRepresentable.swift
│ └── ContentView.swift
└── BNNSBitcrusherExtension/
├── Common/
│ ├── Parameters/
│ │ └── ParameterSpecBase.swift
│ ├── UI/
│ │ ├── ObservableAUParameter.swift
│ │ └── AudioUnitViewController.swift
│ └── Audio Unit/
│ └── BNNSBitcrusherExtensionAudioUnit.mm
└── UI/
└── BNNSBitcrusherExtensionMainView.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Objective-C++, Python, Swift.
- The verified tree contains 6 project/configuration file(s) and 23 source declaration(s).
Overall architecture
flowchart LR
N1["BNNSBitcrusherApp"]
N2["ContentView"]
N3["AudioUnitViewModel"]
N4["SimplePlayEngine"]
N5["SwiftUI / AudioToolbox APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
BNNSBitcrusher/BNNSBitcrusherApp.swift:11 — architecture anchor
@main
struct BNNSBitcrusherApp: App {
// ...
}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 Accelerate.
Ownership and state
classDiagram
BNNSBitcrusherApp o-- AudioUnitHostModel : hostModel
AudioUnitViewModel *-- Bool : showAudioControls
AudioUnitViewModel *-- Bool : showMIDIContols
AudioUnitViewModel *-- String : title
Ownership evidence
BNNSBitcrusher/BNNSBitcrusherApp.swift:13 — stored dependency or nearest verified ownership anchor
@ObservedObject private var hostModel = AudioUnitHostModel()
var body: some Scene {
WindowGroup {
ContentView(hostModel: hostModel)
}
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
BNNSBitcrusherApp |
AudioUnitHostModel (hostModel) |
observes externally owned state | The observed object is authoritative |
AudioUnitViewModel |
Bool (showAudioControls) |
owns value state | App/module collaborators |
AudioUnitViewModel |
Bool (showMIDIContols) |
owns value state | App/module collaborators |
AudioUnitViewModel |
String (title) |
owns value state | App/module collaborators |
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.
Class and protocol design
BNNSBitcrusherExtension/Common/Parameters/ParameterSpecBase.swift:11 — representative type boundary
public protocol NodeSpec {}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
BNNSBitcrusherApp |
Application entry and top-level composition | App |
AudioUnitViewModel |
UI-facing state and feature coordination | Concrete collaborators/imported frameworks |
ParameterGroupBuilder |
Incrementally constructs a framework value or graph | Concrete collaborators/imported frameworks |
SimplePlayEngine |
Owns processing or simulation work | Concrete collaborators/imported frameworks |
MIDIManager |
Long-lived feature or framework coordination | Identifiable, ObservableObject |
ContentView |
User-interface presentation and input forwarding | View |
AudioUnitHostModel |
Feature data or observable state | ObservableObject |
AudioUnitViewController |
View lifecycle, callbacks, and feature coordination | AUViewController, AUAudioUnitFactory |
BNNSBitcrusherExtensionMainView |
User-interface presentation and input forwarding | View |
NodeSpec |
Defines a capability or collaboration contract | Concrete collaborators/imported frameworks |
The source explicitly defines local protocol relationships: ParameterGroupSpec → NodeSpec, ParameterTreeSpec → NodeSpec, ParameterSpec → NodeSpec.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
hostModel (BNNSBitcrusher/BNNSBitcrusherApp.swift:13) |
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. |
loadAudioUnitViewController (BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:36) |
fileprivate |
Use is restricted to this source file. | Inference: share with same-file helpers or extensions without exposing the symbol module-wide. |
avAudioUnit (BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:56) |
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. |
stateChangeQueue (BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:59) |
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
BNNSBitcrusher/BNNSBitcrusherApp.swift:13 — representative boundary
@ObservedObject private var hostModel = AudioUnitHostModel()Swift 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 | BNNSBitcrusherApp |
The source’s App suffix makes this role explicit. |
| Incrementally constructs a framework value or graph | ParameterGroupBuilder |
The source’s Builder suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | AudioUnitViewController |
The source’s Controller suffix makes this role explicit. |
| Owns processing or simulation work | SimplePlayEngine |
The source’s Engine suffix makes this role explicit. |
| Long-lived feature or framework coordination | MIDIManager |
The source’s Manager suffix makes this role explicit. |
| Feature data or observable state | AudioUnitHostModel |
The source’s Model suffix makes this role explicit. |
| User-interface presentation and input forwarding | BNNSBitcrusherExtensionMainView, ContentView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | BNNSBitcrusher/Model/AudioUnitViewModel.swift:12 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
| View-controller organization | BNNSBitcrusherExtension/Common/UI/AudioUnitViewController.swift:15 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | BNNSBitcrusherExtension/Common/Parameters/ParameterSpecBase.swift:28 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Builder | BNNSBitcrusherExtension/Common/Parameters/ParameterSpecBase.swift:22 |
A builder-named type owns incremental construction. |
| Publisher-backed observable state | BNNSBitcrusher/Model/AudioUnitHostModel.swift:17 |
Published properties notify observers while mutation remains with the state object. |
Naming conventions
- Types: App: BNNSBitcrusherApp; Builder: ParameterGroupBuilder; Controller: AudioUnitViewController; Engine: SimplePlayEngine; Manager: MIDIManager; Model: AudioUnitHostModel; View: BNNSBitcrusherExtensionMainView, ContentView; ViewModel: AudioUnitViewModel.
- Protocols:
NodeSpec. - Methods:
validateID,buildBlock,createNode,createParameterGroup,createParameter,createAUParameterNodes,createAUParameterTree,create. - Files:
BNNSBitcrusher/BNNSBitcrusherApp.swift,BNNSBitcrusher/Model/AudioUnitViewModel.swift,BNNSBitcrusherExtension/Common/UI/ObservableAUParameter.swift,BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift,BNNSBitcrusher/Common/MIDI/MIDIManager.swift,BNNSBitcrusher/ContentView.swift.
Architecture takeaways
BNNSBitcrusherAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, AudioToolbox, CoreAudioKit, CoreMIDI 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
BNNSBitcrusher/BNNSBitcrusherApp.swift |
BNNSBitcrusherApp |
BNNSBitcrusher/Model/AudioUnitViewModel.swift |
AudioUnitViewModel |
BNNSBitcrusherExtension/Common/Parameters/ParameterSpecBase.swift |
NodeSpec, ParameterGroupBuilder, ParameterGroupSpec, ParameterTreeSpec, ParameterSpec, func, func, func |
BNNSBitcrusherExtension/Common/UI/ObservableAUParameter.swift |
ObservableAUParameterNode, func, ObservableAUParameterGroup, ObservableAUParameter, EditingState |
BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift |
SimplePlayEngine |
BNNSBitcrusher/Common/MIDI/MIDIManager.swift |
MIDIManager |
BNNSBitcrusher/ContentView.swift |
ContentView |
BNNSBitcrusher/Model/AudioUnitHostModel.swift |
AudioUnitHostModel |
BNNSBitcrusherExtension/Common/UI/AudioUnitViewController.swift |
AudioUnitViewController |
BNNSBitcrusherExtension/UI/BNNSBitcrusherExtensionMainView.swift |
BNNSBitcrusherExtensionMainView |