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

Classifying Live Audio Input with a Built-in Sound Classifier

At a glance

Item Summary
Purpose Detect and identify hundreds of sounds by using a trained classifier.
App architecture A Swift sample with the source-visible chain ClassifySoundAppContentViewSoundAnalysis APIs.
Main patterns Binding-based state propagation, Publisher-backed observable state
Project style 8 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.

Project structure

Source bundle/
├── ClassifySound/
│   ├── ClassifySoundApp.swift
│   ├── Support/
│   │   ├── ContentView.swift
│   │   ├── DetectSoundsView.swift
│   │   ├── SetupMonitoredSoundsView.swift
│   │   ├── ClassificationResultsSubject.swift
│   │   ├── DetectionState.swift
│   │   └── SoundIdentifier.swift
│   ├── SystemAudioClassifier.swift
│   └── Info.plist
├── ClassifySound.xcodeproj/
│   ├── .xcodesamplecode.plist
│   └── project.pbxproj
└── Configuration/
    └── SampleCode.xcconfig

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 11 source declaration(s).

Overall architecture

Reference code

ClassifySound/ClassifySoundApp.swift:130 — architecture anchor

@main
struct ClassifySoundApp: 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 Sound Analysis.

Ownership and state

Ownership evidence

ClassifySound/ClassifySoundApp.swift:15 — stored dependency or nearest verified ownership anchor

struct AppConfiguration {
    // ...
    var inferenceWindowSize = Double(1.5)
    // ...
}
Owner Object or state Relationship Mutation authority
AppConfiguration Double (inferenceWindowSize) owns value state App/module collaborators
AppConfiguration Double (overlapFactor) owns value state App/module collaborators
AppConfiguration Monitoredsounds (monitoredSounds) stores or receives App/module collaborators
AppState AnyCancellable (detectionCancellable) stores or receives Owning lexical scope

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

ClassifySound/ClassifySoundApp.swift:131 — representative type boundary

struct ClassifySoundApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
Type Responsibility Depends on or conforms to
ClassifySoundApp Application entry and top-level composition App
ContentView User-interface presentation and input forwarding View
DetectSoundsView User-interface presentation and input forwarding View
SetupMonitoredSoundsView User-interface presentation and input forwarding View
AppConfiguration Carries feature or framework configuration Concrete collaborators/imported frameworks
AppState Represents mutable feature state ObservableObject
SystemAudioClassifier Owns feature behavior and collaborator lifecycle NSObject
SystemAudioClassificationError Represents feature failure conditions Error
ClassificationResultsSubject Owns feature behavior and collaborator lifecycle NSObject, SNResultsObserving
DetectionState Represents mutable feature state Concrete collaborators/imported frameworks

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
detectionCancellable (ClassifySound/ClassifySoundApp.swift:52) 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.
appConfig (ClassifySound/ClassifySoundApp.swift:55) 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.
subject (ClassifySound/Support/ClassificationResultsSubject.swift:19) 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.
analysisQueue (ClassifySound/SystemAudioClassifier.swift:35) 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

ClassifySound/ClassifySoundApp.swift:52 — representative boundary

class AppState: ObservableObject {
    // ...
    private var detectionCancellable: AnyCancellable? = nil
    // ...
}

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 ClassifySoundApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, DetectSoundsView, SetupMonitoredSoundsView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Binding-based state propagation ClassifySound/Support/DetectSoundsView.swift:18 A binding exposes controlled read/write access while the upstream owner remains authoritative.
Publisher-backed observable state ClassifySound/ClassifySoundApp.swift:60 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: ClassifySoundApp; View: ContentView, DetectSoundsView, SetupMonitoredSoundsView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: listAllValidSoundIdentifiers, restartDetection, advanceDetectionStates, generateConfidenceMeterBarColors, cardify, generateMeter, generateLabeledMeter, generateMeterCard.
  • Files: ClassifySound/ClassifySoundApp.swift, ClassifySound/Support/ContentView.swift, ClassifySound/Support/DetectSoundsView.swift, ClassifySound/Support/SetupMonitoredSoundsView.swift, ClassifySound/SystemAudioClassifier.swift, ClassifySound/Support/ClassificationResultsSubject.swift.

Architecture takeaways

  • ClassifySoundApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, SoundAnalysis, 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
ClassifySound/ClassifySoundApp.swift AppConfiguration, AppState, ClassifySoundApp
ClassifySound/Support/ContentView.swift ContentView, ContentView_Previews
ClassifySound/Support/DetectSoundsView.swift DetectSoundsView
ClassifySound/Support/SetupMonitoredSoundsView.swift SetupMonitoredSoundsView
ClassifySound/SystemAudioClassifier.swift SystemAudioClassifier, SystemAudioClassificationError
ClassifySound/Support/ClassificationResultsSubject.swift ClassificationResultsSubject
ClassifySound/Support/DetectionState.swift DetectionState
ClassifySound/Support/SoundIdentifier.swift SoundIdentifier