Building an audio sequencer to arrange and play clips
At a glance
| Item | Summary |
|---|---|
| Purpose | Synchronize audio loops with a main tempo by creating a real-time clip launcher. |
| App architecture | A Swift sample with the source-visible chain ClipLauncherApp → ContentView → ClipLauncherViewModel → ClipEngine → SwiftUI / AVFoundation APIs. |
| Main patterns | Model-View-ViewModel, View-controller organization, Actor isolation |
| Project style | 25 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task, await suspension point, Sendable or @Sendable, Task closure isolated to MainActor; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: @Observable, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, Foundation, AVFoundation, Accelerate, UniformTypeIdentifiers; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
└── ClipLauncher/
└── ClipLauncher/
├── ClipLauncherApp.swift
├── Models/
│ ├── Constants.swift
│ ├── ClipScene.swift
│ └── AudioClip.swift
├── Audio/
│ ├── ClipLoader.swift
│ └── ClipEngine.swift
├── ViewModel/
│ └── ClipLauncherViewModel.swift
├── Controller/
│ └── ClipLauncherController.swift
└── Views/
├── ContentView.swift
├── Controls/
│ └── FaderView.swift
├── Grid/
│ └── ClipSlotView.swift
└── WaveformView.swift
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 3 project/configuration file(s) and 37 source declaration(s).
Overall architecture
flowchart LR
N1["ClipLauncherApp"]
N2["ContentView"]
N3["ClipLauncherViewModel"]
N4["ClipEngine"]
N5["SwiftUI / AVFoundation APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5
Reference code
ClipLauncher/ClipLauncher/ClipLauncherApp.swift:9 — architecture anchor
@main
struct ClipLauncherApp: App {
// ...
WindowGroup {
ContentView()
}
// ...
}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 AVFAudio.
Ownership and state
classDiagram
Constants *-- Double : defaultTempo
Constants *-- Double : minTempo
Constants *-- Double : maxTempo
Constants *-- Double : minimumAnalysisDuration
Ownership evidence
ClipLauncher/ClipLauncher/Models/Constants.swift:28 — stored dependency or nearest verified ownership anchor
enum BPM {
// ...
static let defaultTempo: Double = 120
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
Constants |
Double (defaultTempo) |
owns value state | Initialized by the owner; the binding is immutable |
Constants |
Double (minTempo) |
owns value state | Initialized by the owner; the binding is immutable |
Constants |
Double (maxTempo) |
owns value state | Initialized by the owner; the binding is immutable |
Constants |
Double (minimumAnalysisDuration) |
owns value state | Initialized by the owner; the binding is immutable |
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 |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:21 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:79 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:81 |
| Transfer contract | Sendable or @Sendable |
The source declares a sendability boundary; this alone does not synchronize mutable state. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:109 |
| Main isolation | Task closure isolated to MainActor |
The cited operation explicitly enters a main-actor-isolated region. | ClipLauncher/ClipLauncher/Audio/ClipEngine+Metering.swift:23 |
@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
ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:21 — representative execution boundary
@Observable
@MainActor
public final class AudioLevelTap {
// ...
public private(set) var level: Float = 0
// ...
}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 | @Observable |
Observation macro publishes source-visible changes. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:20 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | ClipLauncher/ClipLauncher/Views/ContentView.swift:12 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | ClipLauncher/ClipLauncher/ClipLauncherApp.swift:7 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | ClipLauncher/ClipLauncher/Audio/ClipEngine+Metering.swift:9 |
| Source import | AVFoundation |
The cited file imports this module; runtime use and architectural role are not inferred. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:8 |
| Source import | Accelerate |
The cited file imports this module; runtime use and architectural role are not inferred. | ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:9 |
| Source import | UniformTypeIdentifiers |
The cited file imports this module; runtime use and architectural role are not inferred. | ClipLauncher/ClipLauncher/Views/ContentView.swift:9 |
| Source import | Observation |
The cited file imports this module; runtime use and architectural role are not inferred. | ClipLauncher/ClipLauncher/ViewModel/ClipLauncherViewModel.swift:9 |
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
ClipLauncher/ClipLauncher/ClipLauncherApp.swift:10 — representative type boundary
@main
struct ClipLauncherApp: App {
// ...
ContentView()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
ClipLauncherApp |
Application entry and top-level composition | App |
ClipLoader |
Loads and prepares feature data or resources | Concrete collaborators/imported frameworks |
ClipLauncherViewModel |
UI-facing state and feature coordination | Concrete collaborators/imported frameworks |
ClipEngine |
Owns processing or simulation work | Concrete collaborators/imported frameworks |
ClipLauncherController |
View lifecycle, callbacks, and feature coordination | Concrete collaborators/imported frameworks |
ClipScene |
Scene lifecycle or scene-level composition | Identifiable |
ContentView |
User-interface presentation and input forwarding | View |
FaderView |
User-interface presentation and input forwarding | View |
ClipSlotView |
User-interface presentation and input forwarding | View |
WaveformView |
User-interface presentation and input forwarding | View |
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 |
|---|---|---|---|
level (ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:24) |
private(set) |
Read access follows the declaration; writes remain in the private scope. | Inference: allow observation while reserving invariant-changing writes for the owner. |
state (ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:27) |
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. |
refreshTask (ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:30) |
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. |
minus140dB (ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:32) |
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
ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift:24 — representative boundary
@Observable
@MainActor
public final class AudioLevelTap {
// ...
public private(set) var level: Float = 0
// ...
}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 | ClipLauncherApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | ClipLauncherController |
The source’s Controller suffix makes this role explicit. |
| Owns processing or simulation work | ClipEngine |
The source’s Engine suffix makes this role explicit. |
| Loads and prepares feature data or resources | ClipLoader |
The source’s Loader suffix makes this role explicit. |
| Scene lifecycle or scene-level composition | ClipScene |
The source’s Scene suffix makes this role explicit. |
| User-interface presentation and input forwarding | ClipSlotView, ContentView, FaderView, WaveformView |
The source’s View suffix makes this role explicit. |
| UI-facing state and feature coordination | ClipLauncherViewModel |
The source’s ViewModel suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Model-View-ViewModel | ClipLauncher/ClipLauncher/ViewModel/ClipLauncherViewModel.swift:39 |
Role-named view models keep UI-facing state or coordination outside view declarations. |
| View-controller organization | ClipLauncher/ClipLauncher/Controller/ClipLauncherController.swift:44 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Actor isolation | ClipLauncher/ClipLauncher/Audio/ClipLoader.swift:21 |
A declared actor creates an explicit isolation boundary; its executor is not described as a background thread. |
Main application flow
sequenceDiagram
actor User
participant VM as ViewModel
participant C as Controller
participant E as ClipEngine
User->>VM: Launch scene
loop each populated track
VM->>C: queueClip(track, clipID)
C->>E: queue clip
end
VM->>C: flushLaunchQueue()
C->>E: schedule common quantized start
E-->>C: launch callbacks
C-->>VM: AsyncStream clipStarted events
Reference code
ClipLauncher/ClipLauncher/ViewModel/ClipLauncherViewModel.swift:215 — launchScene
func launchScene(sceneIndex: Int) {
guard sceneIndex >= 0 else { return }
// Stop any clips that are in other scenes.
for tIndex in tracks.indices {
for idx in tracks[tIndex].scenes.indices where idx != sceneIndex {
if tracks[tIndex].scenes[idx].isPlaying || tracks[tIndex].scenes[idx].isLaunching {
controller.stop(track: tracks[tIndex].id)
}
clearSceneState(trackIndex: tIndex, sceneIndex: idx)
}
}
// Queue all unscheduled clips in the target scene.
for tIndex in tracks.indices {
guard sceneIndex < tracks[tIndex].scenes.count,
let clip = tracks[tIndex].scenes[sceneIndex].clip,
!tracks[tIndex].scenes[sceneIndex].isPlaying else { continue }
try? controller.queueClip(track: tracks[tIndex].id, clipID: clip.id)
tracks[tIndex].scenes[sceneIndex].isLaunching = true
}
// Flush all queued clips with the same start time for sync.
controller.flushLaunchQueue()
updateBeatTimerState()
}Naming conventions
- Types: App: ClipLauncherApp; Controller: ClipLauncherController; Engine: ClipEngine; Loader: ClipLoader; Scene: ClipScene; View: ClipSlotView, ContentView, FaderView, WaveformView; ViewModel: ClipLauncherViewModel.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
load,analyzeRhythm,makeAsyncIterator,next,startEventConsumption,handleEvent,handleClipLoaded,handleClipStarted. - Files:
ClipLauncher/ClipLauncher/ClipLauncherApp.swift,ClipLauncher/ClipLauncher/Models/Constants.swift,ClipLauncher/ClipLauncher/Audio/ClipLoader.swift,ClipLauncher/ClipLauncher/ViewModel/ClipLauncherViewModel.swift,ClipLauncher/ClipLauncher/Audio/ClipEngine.swift,ClipLauncher/ClipLauncher/Controller/ClipLauncherController.swift.
Architecture takeaways
ClipLauncherAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, AVFoundation, Accelerate, UniformTypeIdentifiers 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 |
|---|---|
ClipLauncher/ClipLauncher/ClipLauncherApp.swift |
Cited implementation, ClipLauncherApp, SwiftUI |
ClipLauncher/ClipLauncher/Models/Constants.swift |
Cited implementation, Constants, Grid, BPM, Timing, Audio, Metering, Events, Waveform |
ClipLauncher/ClipLauncher/Audio/AudioLevelTap.swift |
Cited implementation, @MainActor, Task, await suspension point, Sendable or @Sendable, @Observable, AVFoundation, Accelerate, AudioLevelTap, LevelState |
ClipLauncher/ClipLauncher/ViewModel/ClipLauncherViewModel.swift |
ClipLauncherViewModel, Observation |
ClipLauncher/ClipLauncher/Controller/ClipLauncherController.swift |
ClipLauncherController, Event, TrackState |
ClipLauncher/ClipLauncher/Audio/ClipLoader.swift |
ClipLoader, LoadError, BufferSequence, Iterator |
ClipLauncher/ClipLauncher/Audio/ClipEngine+Metering.swift |
Task closure isolated to MainActor, Foundation, Feature implementation |
ClipLauncher/ClipLauncher/Views/ContentView.swift |
SwiftUI state property wrapper, UniformTypeIdentifiers, ContentView |
ClipLauncher/ClipLauncher/Audio/ClipEngine.swift |
ClipEngine, TrackID, LoadedClip |
ClipLauncher/ClipLauncher/Models/ClipScene.swift |
ClipScene |
ClipLauncher/ClipLauncher/Views/Controls/FaderView.swift |
FaderView |
ClipLauncher/ClipLauncher/Views/Grid/ClipSlotView.swift |
ClipSlotView |
ClipLauncher/ClipLauncher/Views/WaveformView.swift |
WaveformView |
ClipLauncher/ClipLauncher/Models/AudioClip.swift |
AudioClip |
ClipLauncher/ClipLauncher/Models/ClipTrack.swift |
ClipTrack |
ClipLauncher/ClipLauncher/Audio/TransportTiming.swift |
TransportTiming, QuantizedStart |