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

Bringing advanced speech-to-text capabilities to your app

At a glance

Item Summary
Purpose Learn how to incorporate live speech-to-text transcription into your app with SpeechAnalyzer.
App architecture A Swift sample with the source-visible chain SwiftTranscriptionSampleAppAppContentViewBufferConverterSpeech APIs.
Main patterns Binding-based state propagation
Project style 8 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: Sendable or @Sendable, async declaration or closure, Task; none alone proves a background thread.
State/event model Source-visible mechanisms: @Observable, SwiftUI state property wrapper.
Key frameworks/packages Foundation, SwiftUI, AVFoundation, Speech, SwiftData; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── SwiftTranscriptionSampleApp/
│   ├── Helpers/
│   │   ├── SwiftTranscriptionSampleAppApp.swift
│   │   ├── Helpers.swift
│   │   └── BufferConversion.swift
│   ├── Models/
│   │   └── StoryModel.swift
│   ├── Recording and Transcription/
│   │   ├── Recorder.swift
│   │   └── Transcription.swift
│   └── Views/
│       ├── ContentView.swift
│       └── TranscriptView.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── SwiftTranscriptionSampleApp.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

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

Overall architecture

Reference code

SwiftTranscriptionSampleApp/Helpers/SwiftTranscriptionSampleAppApp.swift:11 — architecture anchor

@main
struct SwiftTranscriptionSampleApp: 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 Speech.

Ownership and state

Ownership evidence

SwiftTranscriptionSampleApp/Models/StoryModel.swift:16 — stored dependency or nearest verified ownership anchor

@Observable
class Story: Identifiable {
    // ...
    let id: UUID
    // ...
}
Owner Object or state Relationship Mutation authority
Story UUID (id) owns value state Initialized by the owner; the binding is immutable
Story String (title) owns value state App/module collaborators
Story AttributedString (text) stores or receives App/module collaborators
Story URL (url) owns value state App/module collaborators

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
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. SwiftTranscriptionSampleApp/Helpers/Helpers.swift:67
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. SwiftTranscriptionSampleApp/Helpers/Helpers.swift:74
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. SwiftTranscriptionSampleApp/Recording and Transcription/Recorder.swift:54

@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

SwiftTranscriptionSampleApp/Helpers/Helpers.swift:67 — representative execution boundary

public struct AudioData: @unchecked Sendable {
    var buffer: AVAudioPCMBuffer
    var time: AVAudioTime
}

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. SwiftTranscriptionSampleApp/Models/StoryModel.swift:12
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. SwiftTranscriptionSampleApp/Views/ContentView.swift:13
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. SwiftTranscriptionSampleApp/Helpers/BufferConversion.swift:8
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. SwiftTranscriptionSampleApp/Helpers/Helpers.swift:10
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. SwiftTranscriptionSampleApp/Helpers/BufferConversion.swift:9
Source import Speech The cited file imports this module; runtime use and architectural role are not inferred. SwiftTranscriptionSampleApp/Recording and Transcription/Transcription.swift:9
Source import SwiftData The cited file imports this module; runtime use and architectural role are not inferred. SwiftTranscriptionSampleApp/Helpers/SwiftTranscriptionSampleAppApp.swift:9
Source import FoundationModels The cited file imports this module; runtime use and architectural role are not inferred. SwiftTranscriptionSampleApp/Models/StoryModel.swift:10

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

SwiftTranscriptionSampleApp/Helpers/SwiftTranscriptionSampleAppApp.swift:12 — representative type boundary

@main
struct SwiftTranscriptionSampleApp: App {
    // ...
            ContentView()
    // ...
}
Type Responsibility Depends on or conforms to
SwiftTranscriptionSampleApp Application entry and top-level composition App
Recorder Owns capture or recording work Concrete collaborators/imported frameworks
ContentView User-interface presentation and input forwarding View
TranscriptView User-interface presentation and input forwarding View
BufferConverter Transforms between feature representations Concrete collaborators/imported frameworks
Story Owns feature behavior and collaborator lifecycle Identifiable
TranscriptionState Represents mutable feature state Concrete collaborators/imported frameworks
TranscriptionError Represents feature failure conditions Error
RecordingState Represents mutable feature state Equatable
PlaybackState Represents mutable feature state Equatable

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
converter (SwiftTranscriptionSampleApp/Helpers/BufferConversion.swift:18) 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.
outputContinuation (SwiftTranscriptionSampleApp/Recording and Transcription/Recorder.swift:13) 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 (SwiftTranscriptionSampleApp/Recording and Transcription/Recorder.swift:14) 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.
transcriber (SwiftTranscriptionSampleApp/Recording and Transcription/Recorder.swift:15) 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

SwiftTranscriptionSampleApp/Helpers/BufferConversion.swift:18 — representative boundary

class BufferConverter {
    // ...
    private var converter: AVAudioConverter?
    // ...
}

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 SwiftTranscriptionSampleApp The source’s App suffix makes this role explicit.
Transforms between feature representations BufferConverter The source’s Converter suffix makes this role explicit.
Owns capture or recording work Recorder The source’s Recorder suffix makes this role explicit.
User-interface presentation and input forwarding ContentView, TranscriptView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Binding-based state propagation SwiftTranscriptionSampleApp/Views/TranscriptView.swift:14 A binding exposes controlled read/write access while the upstream owner remains authoritative.

Main application flow

Reference code

SwiftTranscriptionSampleApp/Recording and Transcription/Transcription.swift:63setUpTranscriber()

        recognizerTask = Task {
            do {
                for try await case let result in transcriber.results {
                    let text = result.text
                    if result.isFinal {
                        finalizedTranscript += text
                        volatileTranscript = ""
                        updateStoryWithNewText(withFinal: text)
                    } else {
                        volatileTranscript = text
                        volatileTranscript.foregroundColor = .purple.opacity(0.4)
                    }
                }
            } catch {
                print("speech recognition failed")
            }
        }

Naming conventions

  • Types: App: SwiftTranscriptionSampleApp; Converter: BufferConverter; Recorder: Recorder; View: ContentView, TranscriptView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: suggestedTitle, blank, storyBrokenUpByLines, hash, isAuthorized, writeBufferToDisk, handlePlayback, handleRecordingButtonTap.
  • Files: SwiftTranscriptionSampleApp/Recording and Transcription/Recorder.swift, SwiftTranscriptionSampleApp/Views/ContentView.swift, SwiftTranscriptionSampleApp/Views/TranscriptView.swift.

Architecture takeaways

  • SwiftTranscriptionSampleAppApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, AVFoundation, Speech, SwiftData 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
SwiftTranscriptionSampleApp/Helpers/SwiftTranscriptionSampleAppApp.swift Cited implementation, SwiftTranscriptionSampleApp, SwiftData
SwiftTranscriptionSampleApp/Models/StoryModel.swift Cited implementation, @Observable, FoundationModels, Story
SwiftTranscriptionSampleApp/Helpers/BufferConversion.swift Cited implementation, Foundation, AVFoundation, BufferConverter, Error
SwiftTranscriptionSampleApp/Recording and Transcription/Recorder.swift Cited implementation, Task, Recorder
SwiftTranscriptionSampleApp/Views/TranscriptView.swift Cited implementation, TranscriptView
SwiftTranscriptionSampleApp/Helpers/Helpers.swift Sendable or @Sendable, async declaration or closure, SwiftUI, TranscriptionState, TranscriptionError, RecordingState, PlaybackState, AudioData
SwiftTranscriptionSampleApp/Views/ContentView.swift SwiftUI state property wrapper, ContentView
SwiftTranscriptionSampleApp/Recording and Transcription/Transcription.swift Speech, SpokenWordTranscriber