Hi everyone
While building a multiplayer AR horror game recently at the Apple Developer Academy, we relied heavily on spatial audio to build tension. RealityKit's native LiDAR occlusion is incredible for this—but the boilerplate required to actually get a 3D sound to physically muffle behind real-world walls was a major friction point.
Setting up a single spatial audio emitter required dealing with simd_float4x4 matrix math, manual AnchorEntity configuration, and setting up the SpatialAudioComponent just right.
To solve this, I decoupled our game's audio engine and open-sourced it as a lightweight Swift Package: RealityAudio.
It abstracts all the boilerplate down to a single, stateless method.
Before (Standard RealityKit):
Swift
var matrix = matrix_identity_float4x4
matrix.columns.3 = simd_float4(x, y, z, 1)
let anchor = AnchorEntity(world: matrix)
let emitter = Entity()
anchor.addChild(emitter)
emitter.components.set(SpatialAudioComponent(
gain: 0,
directivity: .beam(focus: 0),
distanceAttenuation: .rolloff(factor: 1.0)
))
scene.addAnchor(anchor)
let resource = try await AudioFileResource(contentsOf: url)
emitter.playAudio(resource)
After (Using RealityAudio):
Swift
import RealityAudio
try await RealityAudio.play(url, in: scene, at: simd_float3(x, y, z), occluded: true)
Key Features:
-
Stateless & Zero Bloat: It doesn't hijack your
ARViewor session state. It simply handles the math, spawns the entity, and hands it back to you. -
Native Occlusion: Passing
occluded: trueconfigures the correct distance attenuation so that (if you have scene understanding enabled) the sound physically muffles behind LiDAR-scanned walls. -
Tested: Includes a Swift Testing suite for the underlying SIMD transforms.
If you are building for visionOS or ARKit, I'd love for you to try it out and let me know what you think!
GitHub Repository: GitHub - bhanage-viraj/RealityAudio · GitHub