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

Creating visuals with Music Understanding analysis results

At a glance

Item Summary
Purpose Create a multiplatform app that presents analysis results from the Music Understanding framework.
App architecture A Swift sample with the source-visible chain MusicUnderstandingLabContentViewSongAnalysisDocumentAssetPlayerMusicUnderstanding APIs.
Main patterns No named application pattern supported by the extracted structure
Project style 22 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, async declaration or closure, Task closure isolated to MainActor, Task, Sendable or @Sendable; none alone proves a background thread.
State/event model Source-visible mechanisms: @Observable, NotificationCenter, SwiftUI state property wrapper.
Key frameworks/packages SwiftUI, MusicUnderstanding, CoreMedia, AVFoundation, UniformTypeIdentifiers; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── MusicUnderstandingLab/
    └── MusicUnderstandingLab/
        ├── MusicUnderstandingLab.swift
        └── Views/
            ├── LoudnessView.swift
            ├── StructureRoundedBarView.swift
            ├── VisualizationView.swift
            ├── ContentView.swift
            ├── InstrumentActivityView.swift
            ├── InstrumentRangesView.swift
            ├── KeyView.swift
            ├── PaceView.swift
            ├── PlayheadView.swift
            ├── RhythmView.swift
            └── SongSelectionView.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 4 project/configuration file(s) and 39 source declaration(s).

Overall architecture

Reference code

MusicUnderstandingLab/MusicUnderstandingLab/MusicUnderstandingLab.swift:10 — architecture anchor

@main
struct MusicUnderstandingLab: App {
    var body: some Scene {
        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 Music Understanding.

Ownership and state

Ownership evidence

MusicUnderstandingLab/MusicUnderstandingLab/Views/LoudnessView.swift:14 — stored dependency or nearest verified ownership anchor

    private enum Constants {
        static let contentSpacing: CGFloat = 12
        // ...
    }
Owner Object or state Relationship Mutation authority
LoudnessView CGFloat (contentSpacing) owns value state Initialized by the owner; the binding is immutable
LoudnessView CGFloat (statsSpacing) owns value state Initialized by the owner; the binding is immutable
LoudnessView CGFloat (chartSpacing) owns value state Initialized by the owner; the binding is immutable
LoudnessView Float (loudnessMin) 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. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:12
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:47
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:121
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:121
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. MusicUnderstandingLab/MusicUnderstandingLab/App/VideoComposer.swift:11

@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

MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:12 — representative execution boundary

@Observable @MainActor
final class AssetAnalyzer {
    // ...
    let asset: AVURLAsset
    // ...
}

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. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:12
State propagation NotificationCenter NotificationCenter distributes named process-local events. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:122
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. MusicUnderstandingLab/MusicUnderstandingLab/Views/ContentView.swift:18
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. MusicUnderstandingLab/MusicUnderstandingLab/App/SongAnalysisDocument.swift:7
Source import MusicUnderstanding The cited file imports this module; runtime use and architectural role are not inferred. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:8
Source import CoreMedia The cited file imports this module; runtime use and architectural role are not inferred. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:8
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:7
Source import UniformTypeIdentifiers The cited file imports this module; runtime use and architectural role are not inferred. MusicUnderstandingLab/MusicUnderstandingLab/App/SongAnalysisDocument.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

MusicUnderstandingLab/MusicUnderstandingLab/Views/LoudnessView.swift:11 — representative type boundary

struct LoudnessView: View {
    // ...
        static let contentSpacing: CGFloat = 12
    // ...
}
Type Responsibility Depends on or conforms to
LoudnessView User-interface presentation and input forwarding View
StructureRoundedBarView User-interface presentation and input forwarding View
VisualizationView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
InstrumentActivityView User-interface presentation and input forwarding View
InstrumentRangesView User-interface presentation and input forwarding View
KeyView User-interface presentation and input forwarding View
PaceView User-interface presentation and input forwarding View
PlayheadView User-interface presentation and input forwarding View
RhythmView 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
result (MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:19) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
isAnalyzing (MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:22) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
isPlaying (MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:18) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
currentTime (MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:21) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.

Reference code

MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift:19 — representative boundary

@Observable @MainActor
final class AssetAnalyzer {
    // ...
    private(set) var result: MusicUnderstandingSession.SessionResult?
    // ...
}

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
Owns document data or lifecycle behavior SongAnalysisDocument The source’s Document suffix makes this role explicit.
Owns media or timeline playback AssetPlayer The source’s Player suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, InstrumentActivityView, InstrumentRangesView, KeyView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
No named application pattern MusicUnderstandingLab/MusicUnderstandingLab/MusicUnderstandingLab.swift:10 The verified source directly composes concrete framework types; this document avoids forcing a pattern name.

Main application flow

Reference code

MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift:74load()

    func load(_ item: AVPlayerItem, preservePlayback: Bool = false) async {
        let wasPlaying = isPlaying
        let time = player.currentTime()

        replaceItem(item)

        if preservePlayback {
            await player.seek(to: time, toleranceBefore: .zero, toleranceAfter: .zero)
            if wasPlaying { player.play() }
        }

        async let durationLoad = item.asset.load(.duration)
        async let tracksLoad = item.asset.loadTracks(withMediaType: .audio)

        if let assetDuration = try? await durationLoad, assetDuration.isNumeric {
            duration = assetDuration.seconds
        }

        // Load the audio sample rate from the asset's first audio track.
        if let audioTrack = try? await tracksLoad.first,
           let formatDescription = try? await audioTrack.load(.formatDescriptions).first,
           let audioStreamBasicDescription = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription) {
            sampleRate = CMTimeScale(audioStreamBasicDescription.pointee.mSampleRate)
        }
    }

Naming conventions

  • Types: Document: SongAnalysisDocument; Player: AssetPlayer; View: ContentView, InstrumentActivityView, InstrumentRangesView, KeyView, LoudnessView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: statsRow, statItem, stopAccessingSecurityScopedResource.
  • Files: MusicUnderstandingLab/MusicUnderstandingLab/MusicUnderstandingLab.swift, MusicUnderstandingLab/MusicUnderstandingLab/Views/LoudnessView.swift, MusicUnderstandingLab/MusicUnderstandingLab/Views/StructureRoundedBarView.swift, MusicUnderstandingLab/MusicUnderstandingLab/Views/VisualizationView.swift, MusicUnderstandingLab/MusicUnderstandingLab/Views/ContentView.swift, MusicUnderstandingLab/MusicUnderstandingLab/Views/InstrumentActivityView.swift.

Architecture takeaways

  • MusicUnderstandingLab is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, MusicUnderstanding, CoreMedia, AVFoundation 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
MusicUnderstandingLab/MusicUnderstandingLab/MusicUnderstandingLab.swift Cited implementation, MusicUnderstandingLab
MusicUnderstandingLab/MusicUnderstandingLab/Views/LoudnessView.swift Cited implementation, LoudnessView, Constants, ChartSection
MusicUnderstandingLab/MusicUnderstandingLab/App/AssetAnalyzer.swift Cited implementation, @MainActor, async declaration or closure, @Observable, MusicUnderstanding, AVFoundation, AssetAnalyzer, AnalysisError
MusicUnderstandingLab/MusicUnderstandingLab/App/AssetPlayer.swift Cited implementation, Task closure isolated to MainActor, Task, NotificationCenter, CoreMedia, AssetPlayer
MusicUnderstandingLab/MusicUnderstandingLab/App/VideoComposer.swift Sendable or @Sendable, VideoComposer
MusicUnderstandingLab/MusicUnderstandingLab/Views/ContentView.swift SwiftUI state property wrapper, ContentView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/App/SongAnalysisDocument.swift SwiftUI, UniformTypeIdentifiers, SongAnalysisDocument
MusicUnderstandingLab/MusicUnderstandingLab/Views/StructureRoundedBarView.swift Constants, StructureRoundedBarView, SegmentBar
MusicUnderstandingLab/MusicUnderstandingLab/Views/VisualizationView.swift VisualizationView, Constants, AnalysisTiles
MusicUnderstandingLab/MusicUnderstandingLab/Views/InstrumentActivityView.swift InstrumentActivityView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/Views/InstrumentRangesView.swift InstrumentRangesView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/Views/KeyView.swift KeyView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/Views/PaceView.swift PaceView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/Views/PlayheadView.swift PlayheadView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/Views/RhythmView.swift RhythmView, Constants
MusicUnderstandingLab/MusicUnderstandingLab/Views/SongSelectionView.swift SongSelectionView, Constants