Sample CodeiOS, iPadOS, Mac Catalyst, macOSReviewed 2026-07-21View on Apple Developer

Generating spatial audio from a multichannel audio stream

At a glance

Item Summary
Purpose Convert 8-channel audio to 2-channel spatial audio by using a spatial mixer audio unit.
App architecture A C/Objective-C header, Objective-C++, Swift sample with the source-visible chain SpatialAudioRendererAppContentViewChainViewModelAudioEngineAudioToolbox APIs.
Main patterns Model-View-ViewModel, SwiftUI environment injection, Publisher-backed observable state
Project style 16 scanned source file(s) across C/Objective-C header, Objective-C++, Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
└── SpatialAudioRenderer/
    ├── SpatialAudioRendererApp.swift
    └── Shared/
        ├── UI/
        │   ├── Models/
        │   │   └── ChainViewModel.swift
        │   ├── ChainView.swift
        │   └── ContentView.swift
        ├── Audio Engine/
        │   ├── AudioEngine.h
        │   ├── AudioEngine.mm
        │   ├── Nodes/
        │   │   ├── AUSMRenderer.h
        │   │   ├── AUSMRenderer.mm
        │   │   ├── AudioFileReader.mm
        │   │   └── AudioFileReader.h
        │   └── AudioKernel.h
        └── Helpers/
            └── AllocatedAudioBufferList.h

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++, Swift.
  • The verified tree contains 4 project/configuration file(s) and 12 source declaration(s).

Overall architecture

Reference code

SpatialAudioRenderer/SpatialAudioRendererApp.swift:9 — architecture anchor

@main
struct SpatialAudioRendererApp: 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 Audio Toolbox.

Ownership and state

Ownership evidence

SpatialAudioRenderer/SpatialAudioRendererApp.swift:11 — stored dependency or nearest verified ownership anchor

@main
struct SpatialAudioRendererApp: App {
    let model = ChainViewModel()
    // ...
}
Owner Object or state Relationship Mutation authority
SpatialAudioRendererApp ChainViewModel (model) creates and retains Initialized by the owner; the binding is immutable
NodeModel Int (id) owns value state App/module collaborators
NodeModel String (name) owns value state App/module collaborators
NodeModel String (subTitle) 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

SpatialAudioRenderer/SpatialAudioRendererApp.swift:10 — representative type boundary

struct SpatialAudioRendererApp: App {
    // ...
}
Type Responsibility Depends on or conforms to
SpatialAudioRendererApp Application entry and top-level composition App
NodeModel Feature data or observable state Identifiable
ChainViewModel UI-facing state and feature coordination ObservableObject
AudioEngine Owns processing or simulation work NSObject
AUSMRenderer Owns drawing, GPU, or presentation processing Concrete collaborators/imported frameworks
ChainView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
RealtimeInfo Represents feature data Concrete collaborators/imported frameworks
AudioKernel Owns feature behavior and collaborator lifecycle Concrete collaborators/imported frameworks
AudioFileReader Defines a feature-specific type boundary NSObject

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
fileReader (SpatialAudioRenderer/Shared/Audio Engine/AudioEngine.h:13) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
sampleRate (SpatialAudioRenderer/Shared/Audio Engine/Nodes/AudioFileReader.h:18) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
pullAudioBlock (SpatialAudioRenderer/Shared/Audio Engine/Nodes/AudioFileReader.h:19) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
selection (SpatialAudioRenderer/Shared/UI/ChainView.swift:12) 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

SpatialAudioRenderer/Shared/Audio Engine/AudioEngine.h:13 — representative boundary

@property (readonly) AudioFileReader * __nullable fileReader;

+(NSArray<NSString *> *)audioSamples;
-(BOOL)loadAudio:(NSString *)filePath;
-(void)handleRouteChange:(NSNotification *)notification;

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 SpatialAudioRendererApp The source’s App suffix makes this role explicit.
Owns processing or simulation work AudioEngine The source’s Engine suffix makes this role explicit.
Feature data or observable state NodeModel The source’s Model suffix makes this role explicit.
Owns drawing, GPU, or presentation processing AUSMRenderer The source’s Renderer suffix makes this role explicit.
User-interface presentation and input forwarding ChainView, ContentView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination ChainViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel SpatialAudioRenderer/Shared/UI/Models/ChainViewModel.swift:15 Role-named view models keep UI-facing state or coordination outside view declarations.
SwiftUI environment injection SpatialAudioRenderer/Shared/UI/ChainView.swift:11 The environment supplies state or a capability without threading it through every initializer.
Publisher-backed observable state SpatialAudioRenderer/Shared/UI/Models/ChainViewModel.swift:17 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: SpatialAudioRendererApp; Engine: AudioEngine; Model: NodeModel; Renderer: AUSMRenderer; View: ChainView, ContentView; ViewModel: ChainViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: audioSamples, loadAudio, handleRouteChange, init.
  • Files: SpatialAudioRenderer/SpatialAudioRendererApp.swift, SpatialAudioRenderer/Shared/UI/Models/ChainViewModel.swift, SpatialAudioRenderer/Shared/Audio Engine/AudioEngine.h, SpatialAudioRenderer/Shared/Audio Engine/AudioEngine.mm, SpatialAudioRenderer/Shared/Audio Engine/Nodes/AUSMRenderer.h, SpatialAudioRenderer/Shared/UI/ChainView.swift.

Architecture takeaways

  • SpatialAudioRendererApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, AudioToolbox, AVFoundation, AudioUnit 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
SpatialAudioRenderer/SpatialAudioRendererApp.swift SpatialAudioRendererApp
SpatialAudioRenderer/Shared/UI/Models/ChainViewModel.swift NodeModel, ChainViewModel
SpatialAudioRenderer/Shared/Audio Engine/AudioEngine.h AudioEngine
SpatialAudioRenderer/Shared/Audio Engine/AudioEngine.mm AudioEngine
SpatialAudioRenderer/Shared/Audio Engine/Nodes/AUSMRenderer.h AUSMRenderer
SpatialAudioRenderer/Shared/UI/ChainView.swift ChainView
SpatialAudioRenderer/Shared/UI/ContentView.swift ContentView
SpatialAudioRenderer/Shared/Audio Engine/Nodes/AUSMRenderer.mm Feature implementation
SpatialAudioRenderer/Shared/Audio Engine/Nodes/AudioFileReader.mm RealtimeInfo, AudioFileReader
SpatialAudioRenderer/Shared/Audio Engine/AudioKernel.h AudioKernel