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. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async, DispatchQueue(label:); none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, ObservableObject, @Published. |
| Key frameworks/packages | SwiftUI, AudioToolbox, Foundation, CoreAudioKit, CoreMIDI; these are source dependencies, not architecture labels. |
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 {
@ObservedObject private var hostModel = AudioUnitHostModel()
var body: some Scene {
WindowGroup {
ContentView(hostModel: hostModel)
}
}
}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
@main
struct BNNSBitcrusherApp: App {
@ObservedObject private var hostModel = AudioUnitHostModel()
// ...
}| 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.
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.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:38 |
| Queue scheduling | DispatchQueue(label:) |
The source constructs a dispatch queue; its label alone does not prove a thread. | BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:59 |
@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.
Reference code
BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:38 — representative execution boundary
DispatchQueue.main.async {
// ...
let genericViewController = AUGenericViewController()
// ...
}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. | BNNSBitcrusher/BNNSBitcrusherApp.swift:13 |
| State propagation | ObservableObject |
ObservableObject supplies an observation contract. | BNNSBitcrusher/Common/MIDI/MIDIManager.swift:11 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | BNNSBitcrusher/Model/AudioUnitHostModel.swift:17 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | BNNSBitcrusher/BNNSBitcrusherApp.swift:9 |
| Source import | AudioToolbox |
The cited file imports this module; runtime use and architectural role are not inferred. | BNNSBitcrusher/Common/TypeAliases.swift:9 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:8 |
| Source import | CoreAudioKit |
The cited file imports this module; runtime use and architectural role are not inferred. | BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift:9 |
| Source import | CoreMIDI |
The cited file imports this module; runtime use and architectural role are not inferred. | BNNSBitcrusher/BNNSBitcrusherApp.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
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
@main
struct BNNSBitcrusherApp: App {
@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 |
Cited implementation, SwiftUI state property wrapper, SwiftUI, CoreMIDI, BNNSBitcrusherApp |
BNNSBitcrusherExtension/Common/Parameters/ParameterSpecBase.swift |
NodeSpec, Cited implementation, ParameterGroupBuilder, ParameterGroupSpec, ParameterTreeSpec, ParameterSpec, func |
BNNSBitcrusher/Common/Audio/SimplePlayEngine.swift |
Cited implementation, DispatchQueue.main.async, DispatchQueue(label:), Foundation, CoreAudioKit, SimplePlayEngine |
BNNSBitcrusher/Model/AudioUnitViewModel.swift |
AudioUnitViewModel |
BNNSBitcrusherExtension/Common/UI/AudioUnitViewController.swift |
AudioUnitViewController |
BNNSBitcrusher/Model/AudioUnitHostModel.swift |
Cited implementation, @Published, AudioUnitHostModel |
BNNSBitcrusher/Common/MIDI/MIDIManager.swift |
ObservableObject, MIDIManager |
BNNSBitcrusher/Common/TypeAliases.swift |
AudioToolbox, Feature implementation |
BNNSBitcrusherExtension/Common/UI/ObservableAUParameter.swift |
ObservableAUParameterNode, func, ObservableAUParameterGroup, ObservableAUParameter, EditingState |
BNNSBitcrusher/ContentView.swift |
ContentView |
BNNSBitcrusherExtension/UI/BNNSBitcrusherExtensionMainView.swift |
BNNSBitcrusherExtensionMainView |
BNNSBitcrusher/Common/UI/ViewControllerRepresentable.swift |
WrapperVC, AUViewControllerUI |
BNNSBitcrusherExtension/Common/Audio Unit/BNNSBitcrusherExtensionAudioUnit.mm |
BNNSBitcrusherExtensionAudioUnit |
BNNSBitcrusherExtension/Common/Audio Unit/BNNSBitcrusherExtensionAudioUnit.h |
BNNSBitcrusherExtensionAudioUnit |
BNNSBitcrusherExtension/UI/ParameterSlider.swift |
ParameterSlider |
BNNSBitcrusherExtension/UI/WaveformDisplay.swift |
WaveformDisplay |