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

Creating an audio device driver

At a glance

Item Summary
Purpose Implement a configurable audio input source as a driver extension that runs in user space in macOS and iPadOS.
App architecture A C++, C/Objective-C header, Objective-C++, Swift sample with the source-visible chain SimpleAudioAppSimpleAudioViewSimpleAudioViewModelSimpleAudioUserClientDriverKit / AudioDriverKit APIs.
Main patterns Model-View-ViewModel, Delegate or data-source callbacks, Publisher-backed observable state
Project style 10 scanned source file(s) across C++, C/Objective-C header, Objective-C++, Swift, organized around ranked entry, type, and file boundaries.
Execution model No structured execution marker indexed; callback threading requires source review.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Published, ObservableObject.
Key frameworks/packages DriverKit, AudioDriverKit, CoreAudio, Foundation, SwiftUI; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Shared/
│   ├── SimpleAudioApp.swift
│   ├── SimpleAudioViewModel.swift
│   ├── SimpleAudioUserClient.mm
│   ├── SimpleAudioView.swift
│   ├── SimpleAudioUserClient.h
│   └── SimpleAudioDriverKeys.h
├── SimpleAudioDriverExtension/
│   ├── SimpleAudioDriverUserClient.cpp
│   ├── SimpleAudioDevice.cpp
│   ├── SimpleAudioDriver.cpp
│   └── SimpleAudioDriverKeys.h
├── Configuration/
│   └── SampleCode.xcconfig
└── SimpleAudioDriverExtension.xcodeproj/
    └── .xcodesamplecode.plist

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: C++, C/Objective-C header, Objective-C++, Swift.
  • The verified tree contains 7 project/configuration file(s) and 11 source declaration(s).

Overall architecture

Reference code

Shared/SimpleAudioApp.swift:10 — architecture anchor

@main
struct SimpleAudioApp: App {
    var body: some Scene {
        WindowGroup {
            SimpleAudioView()
        }
    }
}

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 AudioDriverKit.

Ownership and state

Ownership evidence

Shared/SimpleAudioViewModel.swift:78 — stored dependency or nearest verified ownership anchor

class SimpleAudioViewModel: NSObject {
    // ...
    @Published private var state: SimpleAudioDriverLoadingStateMachine.State = .unloaded
    // ...
}
Owner Object or state Relationship Mutation authority
SimpleAudioViewModel State (state) stores or receives Owning lexical scope
SimpleAudioViewModel String (dextIdentifier) owns value state Initialized by the owner; the binding is immutable
SimpleAudioUserClient IONotificationPortRef (mIOKitNotificationPort) stores or receives Owning Objective-C implementation
SimpleAudioUserClient io_object_t (ioObject) stores or receives Owning Objective-C implementation

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.

No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.

@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.

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 SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. Shared/SimpleAudioView.swift:11
State propagation @Published A published property can emit owner-controlled changes. Shared/SimpleAudioViewModel.swift:78
State propagation ObservableObject ObservableObject supplies an observation contract. Shared/SimpleAudioViewModel.swift:98
Source import DriverKit The cited file imports this module; runtime use and architectural role are not inferred. SimpleAudioDriverExtension/SimpleAudioDevice.cpp:19
Source import AudioDriverKit The cited file imports this module; runtime use and architectural role are not inferred. SimpleAudioDriverExtension/SimpleAudioDevice.cpp:15
Source import CoreAudio The cited file imports this module; runtime use and architectural role are not inferred. Shared/SimpleAudioUserClient.mm:21
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Shared/SimpleAudioUserClient.h:9
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. Shared/SimpleAudioApp.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

Shared/SimpleAudioApp.swift:11 — representative type boundary

@main
struct SimpleAudioApp: App {
    // ...
            SimpleAudioView()
    // ...
}
Type Responsibility Depends on or conforms to
SimpleAudioApp Application entry and top-level composition App
SimpleAudioViewModel UI-facing state and feature coordination NSObject
SimpleAudioView User-interface presentation and input forwarding View
SimpleAudioUserClient External-system or framework client NSObject
SimpleAudioDriverLoadingStateMachine Owns feature behavior and collaborator lifecycle Concrete collaborators/imported frameworks
State Represents mutable feature state Concrete collaborators/imported frameworks
Event Defines a closed set of feature states or choices Concrete collaborators/imported frameworks
SimpleAudioDriverUserClient_IVars Represents a feature value or composable behavior Concrete collaborators/imported frameworks
SimpleAudioDevice_IVars Represents a feature value or composable behavior Concrete collaborators/imported frameworks
mach_timebase_info Represents a feature value or composable behavior 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
mIOKitNotificationPort (Shared/SimpleAudioUserClient.mm:13) implementation Visibility follows header/implementation and language linkage rules. Inference: keep the declaration in the Objective-C implementation boundary.
ioObject (Shared/SimpleAudioUserClient.mm:14) implementation Visibility follows header/implementation and language linkage rules. Inference: keep the declaration in the Objective-C implementation boundary.
ioConnection (Shared/SimpleAudioUserClient.mm:15) implementation Visibility follows header/implementation and language linkage rules. Inference: keep the declaration in the Objective-C implementation boundary.
state (Shared/SimpleAudioViewModel.swift:78) 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

Shared/SimpleAudioUserClient.mm:13 — representative boundary

@interface SimpleAudioUserClient()
@property IONotificationPortRef mIOKitNotificationPort;
@property io_object_t ioObject;
@property io_connect_t ioConnection;
@end

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 SimpleAudioApp The source’s App suffix makes this role explicit.
External-system or framework client SimpleAudioUserClient The source’s Client suffix makes this role explicit.
User-interface presentation and input forwarding SimpleAudioView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination SimpleAudioViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel Shared/SimpleAudioViewModel.swift:75 Role-named view models keep UI-facing state or coordination outside view declarations.
Delegate or data-source callbacks Shared/SimpleAudioViewModel.swift:134 Callback protocols invert event delivery back into the sample’s owner.
Publisher-backed observable state Shared/SimpleAudioViewModel.swift:78 Published properties notify observers while mutation remains with the state object.

Naming conventions

  • Types: App: SimpleAudioApp; Client: SimpleAudioUserClient; View: SimpleAudioView; ViewModel: SimpleAudioViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: process, activateMyDext, activateExtension, deactivateExtension, request, requestNeedsUserApproval, checkDeviceCustomProperties, open.
  • Files: Shared/SimpleAudioApp.swift, Shared/SimpleAudioViewModel.swift, Shared/SimpleAudioUserClient.mm, Shared/SimpleAudioView.swift, Shared/SimpleAudioUserClient.h.

Architecture takeaways

  • SimpleAudioApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches DriverKit, AudioDriverKit, CoreAudio, SwiftUI 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/SimpleAudioApp.swift Cited implementation, SimpleAudioApp, SwiftUI
Shared/SimpleAudioViewModel.swift Cited implementation, SimpleAudioViewModel, @Published, ObservableObject, SimpleAudioDriverLoadingStateMachine, State, Event
Shared/SimpleAudioUserClient.mm mIOKitNotificationPort, ioObject, ioConnection, CoreAudio, SimpleAudioUserClient
Shared/SimpleAudioView.swift SwiftUI state property wrapper, SimpleAudioView, SimpleAudioDriverView_Previews
SimpleAudioDriverExtension/SimpleAudioDevice.cpp DriverKit, AudioDriverKit, SimpleAudioDevice_IVars, mach_timebase_info
Shared/SimpleAudioUserClient.h Foundation, SimpleAudioUserClient
SimpleAudioDriverExtension/SimpleAudioDriverUserClient.cpp SimpleAudioDriverUserClient_IVars
SimpleAudioDriverExtension/SimpleAudioDriver.cpp SimpleAudioDriver_IVars
Shared/SimpleAudioDriverKeys.h Feature implementation
SimpleAudioDriverExtension/SimpleAudioDriverKeys.h Feature implementation