Building a Custom Catalog and Matching Audio
At a glance
| Item | Summary |
|---|---|
| Purpose | Display lesson content that’s synchronized to a learning video by matching the audio to a custom reference signature and associated metadata. |
| App architecture | A Swift sample with the source-visible chain FoodMathApp → ContentView → CatalogProvider → ShazamKit APIs. |
| Main patterns | Delegate or data-source callbacks, SwiftUI environment injection, Binding-based state propagation, Publisher-backed observable state |
| Project style | 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: ObservableObject, @Published, SwiftUI state property wrapper. |
| Key frameworks/packages | SwiftUI, ShazamKit, Combine, Foundation, AVFAudio; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Shared Source/
│ ├── App/
│ │ ├── FoodMathApp.swift
│ │ └── ContentView.swift
│ ├── Views/
│ │ ├── QuestionView.swift
│ │ ├── EpisodeListView.swift
│ │ ├── EpisodeView.swift
│ │ ├── CardView.swift
│ │ ├── NumberPadView.swift
│ │ └── BlurView.swift
│ └── Data/
│ └── Equation.swift
├── Part 1 - Matching Audio/
│ └── FoodMath/
│ └── CatalogProvider.swift
├── Part 2 - Content Synchronization/
│ └── FoodMath/
│ └── CatalogProvider.swift
└── Part 3 - Final/
└── FoodMath/
└── CatalogProvider.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 10 project/configuration file(s) and 31 source declaration(s).
Overall architecture
flowchart LR
N1["FoodMathApp"]
N2["ContentView"]
N3["CatalogProvider"]
N4["ShazamKit APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
Shared Source/App/FoodMathApp.swift:10 — architecture anchor
@main
struct FoodMathApp: App {
@StateObject private var matcher = Matcher()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(matcher)
}
}
}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 ShazamKit.
Ownership and state
classDiagram
FoodMathApp *-- Matcher : matcher
QuestionView o-- Equation : equation
AppleGridView o-- EquationType : equation
AppleGridView o-- Rows : rows
Ownership evidence
Shared Source/App/FoodMathApp.swift:12 — stored dependency or nearest verified ownership anchor
@main
struct FoodMathApp: App {
@StateObject private var matcher = Matcher()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
FoodMathApp |
Matcher (matcher) |
owns wrapper-managed state | Owning lexical scope |
QuestionView |
Equation (equation) |
stores or receives | Initialized by the owner; the binding is immutable |
AppleGridView |
EquationType (equation) |
stores or receives | Initialized by the owner; the binding is immutable |
AppleGridView |
Rows (rows) |
stores or receives | 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 |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Part 1 - Matching Audio/FoodMath/Matcher.swift:46 |
@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
Part 1 - Matching Audio/FoodMath/Matcher.swift:46 — representative execution boundary
DispatchQueue.main.async {
self.result = MatchResult(mediaItem: match.mediaItems.first, question: nil)
}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 | ObservableObject |
ObservableObject supplies an observation contract. | Part 1 - Matching Audio/FoodMath/Matcher.swift:21 |
| State propagation | @Published |
A published property can emit owner-controlled changes. | Part 1 - Matching Audio/FoodMath/Matcher.swift:22 |
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | Shared Source/App/ContentView.swift:12 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared Source/App/ContentView.swift:8 |
| Source import | ShazamKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Part 1 - Matching Audio/FoodMath/CatalogProvider.swift:8 |
| Source import | Combine |
The cited file imports this module; runtime use and architectural role are not inferred. | Part 1 - Matching Audio/FoodMath/Matcher.swift:10 |
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Shared Source/Data/Episode.swift:8 |
| Source import | AVFAudio |
The cited file imports this module; runtime use and architectural role are not inferred. | Part 1 - Matching Audio/FoodMath/Matcher.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
Shared Source/App/FoodMathApp.swift:11 — representative type boundary
@main
struct FoodMathApp: App {
@StateObject private var matcher = Matcher()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
FoodMathApp |
Application entry and top-level composition | App |
QuestionView |
User-interface presentation and input forwarding | View |
AppleGridView |
User-interface presentation and input forwarding | View |
AppleGroupView |
User-interface presentation and input forwarding | View |
OperatorView |
User-interface presentation and input forwarding | View |
EpisodeListView |
User-interface presentation and input forwarding | View |
BackgroundView |
User-interface presentation and input forwarding | View |
EpisodeView |
User-interface presentation and input forwarding | View |
BoardView |
User-interface presentation and input forwarding | View |
BackgroundView |
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 |
|---|---|---|---|
session (Part 1 - Matching Audio/FoodMath/Matcher.swift:24) |
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. |
audioEngine (Part 1 - Matching Audio/FoodMath/Matcher.swift:25) |
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. |
session (Part 2 - Content Synchronization/FoodMath/Matcher.swift:24) |
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. |
audioEngine (Part 2 - Content Synchronization/FoodMath/Matcher.swift:25) |
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
Part 1 - Matching Audio/FoodMath/Matcher.swift:24 — representative boundary
class Matcher: NSObject, ObservableObject, SHSessionDelegate {
// ...
private var session: SHSession?
// ...
}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 | FoodMathApp |
The source’s App suffix makes this role explicit. |
| Encapsulates one executable operation | EquationOperation |
The source’s Operation suffix makes this role explicit. |
| Supplies a capability or framework resource | CatalogProvider |
The source’s Provider suffix makes this role explicit. |
| User-interface presentation and input forwarding | AppleGridView, AppleGroupView, BackgroundView, BoardView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | Part 1 - Matching Audio/FoodMath/Matcher.swift:21 |
Callback protocols invert event delivery back into the sample’s owner. |
| SwiftUI environment injection | Shared Source/App/ContentView.swift:12 |
The environment supplies state or a capability without threading it through every initializer. |
| Binding-based state propagation | Shared Source/Views/CardView.swift:15 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
| Publisher-backed observable state | Part 1 - Matching Audio/FoodMath/Matcher.swift:22 |
Published properties notify observers while mutation remains with the state object. |
Main application flow
The final target’s audio-matching path crosses UI, catalog construction, microphone capture, and ShazamKit delegate delivery, so the callback order is easier to verify as a sequence.
sequenceDiagram
actor User
participant View as ContentView
participant Catalog as CatalogProvider
participant Matcher
participant Audio as AVAudioEngine
participant Shazam as SHSession
User->>View: Open lesson
View->>Catalog: catalog()
Catalog-->>View: SHCustomCatalog
View->>Matcher: match(catalog)
Matcher->>Shazam: Create session and set delegate
Matcher->>Audio: Install input tap and request permission
Audio-->>Shazam: matchStreamingBuffer(buffer, time)
Shazam-->>Matcher: session(didFind:)
Matcher-->>View: Publish MatchResult on main queue
Reference code
Part 3 - Final/FoodMath/Matcher.swift:27 — match
func match(catalog: SHCustomCatalog) throws {
session = SHSession(catalog: catalog)
session?.delegate = self
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: audioEngine.inputNode.outputFormat(forBus: 0).sampleRate,
channels: 1)
audioEngine.inputNode.installTap(onBus: 0, bufferSize: 2048, format: audioFormat) { [weak session] buffer, audioTime in
session?.matchStreamingBuffer(buffer, at: audioTime)
}
try AVAudioSession.sharedInstance().setCategory(.record)
AVAudioSession.sharedInstance().requestRecordPermission { [weak self] success in
guard success, let self = self else { return }
try? self.audioEngine.start()
}
}Naming conventions
- Types: App: FoodMathApp; Operation: EquationOperation; Provider: CatalogProvider; View: AppleGridView, AppleGroupView, BackgroundView, BoardView, CardView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
onSelect,catalog. - Files:
Shared Source/App/FoodMathApp.swift,Shared Source/Views/QuestionView.swift,Shared Source/Views/EpisodeListView.swift,Shared Source/Views/EpisodeView.swift,Shared Source/App/ContentView.swift,Shared Source/Views/CardView.swift.
Architecture takeaways
FoodMathAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, ShazamKit, AVFAudio 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 |
|---|---|
Shared Source/App/FoodMathApp.swift |
Cited implementation, FoodMathApp |
Part 1 - Matching Audio/FoodMath/Matcher.swift |
Cited implementation, DispatchQueue.main.async, ObservableObject, @Published, Combine, AVFAudio, MatchResult, Matcher |
Part 2 - Content Synchronization/FoodMath/Matcher.swift |
Cited implementation, MatchResult, Matcher |
Shared Source/App/ContentView.swift |
Cited implementation, SwiftUI state property wrapper, SwiftUI, ContentView, ContentView_Previews |
Shared Source/Views/CardView.swift |
Cited implementation, CardView, CardView_Previews |
Part 1 - Matching Audio/FoodMath/CatalogProvider.swift |
ShazamKit, CatalogProvider |
Shared Source/Data/Episode.swift |
Foundation, Episode |
Shared Source/Views/QuestionView.swift |
QuestionView, AppleGridView, AppleGroup, AppleGroupView, OperatorView, QuestionView_Previews |
Shared Source/Views/EpisodeListView.swift |
EpisodeListView, EpisodeList, EpisodeCell, BackgroundView, EpisodeListView_Previews |
Shared Source/Views/EpisodeView.swift |
EpisodeView, BoardView, BackgroundView, EpisodeView_Previews |
Shared Source/Views/NumberPadView.swift |
NumberPadView, NumberPadView_Previews |
Part 2 - Content Synchronization/FoodMath/CatalogProvider.swift |
CatalogProvider |
Part 3 - Final/FoodMath/CatalogProvider.swift |
CatalogProvider |
Shared Source/Views/BlurView.swift |
VisualEffectView |
Shared Source/Data/Equation.swift |
EquationType, EquationOperation, Equation |
Part 3 - Final/FoodMath/Matcher.swift |
MatchResult, Matcher |