Skip to content

Instantly share code, notes, and snippets.

@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:32
Cenatus PHASE blog post snippet 15
import CoreMotion
// [ Elided other imports ]
class ViewController: UIViewController, ARSession { // [ Elided other protocols ]
let hmm = CMHeadphoneMotionManager()
var deviceTransform: simd_float4x4 = matrix_identity_float4x4;
var headphoneTransform: simd_float4x4 = matrix_identity_float4x4;
override func viewDidLoad() {
hmm.startDeviceMotionUpdates(to: OperationQueue.current!, withHandler: {[weak self] motion, error in
@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:32
Cenatus PHASE blog post snippet 14
func session(_ session: ARSession, didUpdate frame: ARFrame) {
let transform = frame.camera.transform
phaseListener.transform = transform
}
@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:30
Cenatus PHASE blog post snippet 12
let distanceModelParameters = PHASEGeometricSpreadingDistanceModelParameters()
distanceModelParameters.fadeOutParameters =
PHASEDistanceModelFadeOutParameters(cullDistance: cull_distance_for_this_source) // For you to specify...
distanceModelParameters.rolloffFactor = rolloff_factor_for_this_source // ... as is this.
@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:29
Cenatus PHASE blog post snippet 11
let spatialPipelineFlags : PHASESpatialPipeline.Flags = [.directPathTransmission, .lateReverb]
let spatialPipeline = PHASESpatialPipeline(flags: spatialPipelineFlags)!
spatialPipeline.entries[PHASESpatialCategory.lateReverb]!.sendLevel = send_level_for_this_source; // Likewise, this is for you to decide on :-)
@timcowlishaw
timcowlishaw / ARViewController.swift
Last active March 1, 2022 17:44
Cenatus PHASE blog post snippet 10
let spatialMixerDefinition = PHASESpatialMixerDefinition(spatialPipeline: spatialPipeline) //spatial Pipeline not yet defined
spatialMixerDefinition.distanceModelParameters = distanceModelParameters // distanceModelParameters not yet defined
@timcowlishaw
timcowlishaw / ARViewController.swift
Last active March 1, 2022 17:26
Cenatus PHASE blog post snippet 9
let mesh = MDLMesh.newIcosahedron(withRadius: 2.0, inwardNormals: false, allocator: nil)
let shape = PHASEShape(engine: phaseEngine, mesh: mesh)
let source = PHASESource(engine: phaseEngine, shapes: [shape])
source.transform = the_position_of_the_object_in_our_world // you'll need to provide this :-)
try! phaseEngine.rootObject.addChild(source)
@timcowlishaw
timcowlishaw / ARViewController.swift
Last active March 1, 2022 17:42
Cenatus PHASE blog post snippet 8
let mixerParameters = PHASEMixerParameters()
mixerParameters.addSpatialMixerParameters(
identifier: phaseSpatialMixerDefinition.identifier, //phaseSpatialMixerDefinition not yet defined
source: source, listener: phaseListener // source not yet defined
)
@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:23
Cenatus PHASE blog post snippet 7
let samplerNodeDefinition = PHASESamplerNodeDefinition(
soundAssetIdentifier: "a_unique_asset_identifier",
mixerDefinition: phaseSpatialMixerDefinition! // As yet undefined
)
samplerNodeDefinition.playbackMode = .looping
samplerNodeDefinition.setCalibrationMode(
calibrationMode: .relativeSpl, level: 12
)
samplerNodeDefinition.cullOption = .sleepWakeAtRealtimeOffset
@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:20
Cenatus PHASE blog post snippet 6
try! phaseEngine.assetRegistry.registerSoundAsset(
url: url, identifier: "a_unique_asset_identifier",
assetType: .resident, channelLayout: nil,
normalizationMode: .dynamic
)
@timcowlishaw
timcowlishaw / ARViewController.swift
Created March 1, 2022 17:18
Cenatus PHASE blog post snippet 5
let fileName = "piano" // piano.mp3 in the app bundle root.
let url = Bundle.main.url(forResource: fileName, withExtension: "mp3")!