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. |
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 {
// ...
}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
@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.
Class and protocol design
Shared Source/App/FoodMathApp.swift:11 — representative type boundary
struct FoodMathApp: App {
// ...
}| 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 — the matcher installs the streaming callback, starts capture after permission, and publishes delegate results on the main queue.
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()
}
}
func session(_ session: SHSession, didFind match: SHMatch) {
DispatchQueue.main.async {
let newQuestion = Question.allQuestions.last { question in
(match.mediaItems.first?.predictedCurrentMatchOffset ?? 0) > question.offset
}
self.result = MatchResult(mediaItem: match.mediaItems.first, question: newQuestion)
}
}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 |
FoodMathApp |
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/App/ContentView.swift |
ContentView, ContentView_Previews |
Shared Source/Views/CardView.swift |
CardView, CardView_Previews |
Shared Source/Views/NumberPadView.swift |
NumberPadView, NumberPadView_Previews |
Part 1 - Matching Audio/FoodMath/CatalogProvider.swift |
CatalogProvider |
Part 2 - Content Synchronization/FoodMath/CatalogProvider.swift |
CatalogProvider |
Part 3 - Final/FoodMath/CatalogProvider.swift |
CatalogProvider |
Part 3 - Final/FoodMath/Matcher.swift |
Matcher, MatchResult, SHSessionDelegate callback |