Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio3D head tracking #149

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cookbook/Cookbook/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<true/>
<key>NSMicrophoneUsageDescription</key>
<string>We use the microphone to demonstrate processing input.</string>
<key>NSMotionUsageDescription</key>
<string>We use the headphone motion tracker if available during the Audio3D demo to pan the audio.</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,41 @@ import SoundpipeAudioKit
import Tonic
import SceneKit
import AVFoundation

import CoreMotion

extension CMQuaternion {
var angle: Double {
return acos(w) * 2
}

var axis: SCNVector3 {
let length = sqrt(1 - w * w)
if length < 0.001 {
return SCNVector3(1, 0, 0)
}
return SCNVector3(x / length, y / length, z / length)
}
}
final class AudioKit3DVM: ObservableObject {
@Published var conductor = AudioEngine3DConductor()
@Published var coordinator = SceneCoordinator()
private let headphoneMotionManager = CMHeadphoneMotionManager()

init() {
coordinator.updateAudioSourceNodeDelegate = conductor
headphoneMotionManager.delegate = coordinator
}
func startHeadTracking() {
if let updateDeviceMotion = coordinator.updateDeviceMotion,
headphoneMotionManager.isDeviceMotionAvailable && !headphoneMotionManager.isDeviceMotionActive {
headphoneMotionManager.startDeviceMotionUpdates(to: .main,
withHandler: updateDeviceMotion)
}
}
func stopHeadTracking() {
headphoneMotionManager.stopDeviceMotionUpdates()
coordinator.motionData = nil
}
}

protocol UpdateAudioSourceNodeDelegate: AnyObject {
Expand Down Expand Up @@ -82,12 +109,14 @@ class AudioEngine3DConductor: ObservableObject, ProcessesPlayerInput, UpdateAudi
}
}

class SceneCoordinator: NSObject, SCNSceneRendererDelegate, ObservableObject {
class SceneCoordinator: NSObject, SCNSceneRendererDelegate, ObservableObject, CMHeadphoneMotionManagerDelegate {

var showsStatistics: Bool = false
var debugOptions: SCNDebugOptions = []

weak var updateAudioSourceNodeDelegate: UpdateAudioSourceNodeDelegate?
var updateDeviceMotion: CMHeadphoneMotionManager.DeviceMotionHandler?
var motionData: CMDeviceMotion?

lazy var theScene: SCNScene = {
// create a new scene
Expand All @@ -102,12 +131,30 @@ class SceneCoordinator: NSObject, SCNSceneRendererDelegate, ObservableObject {
return cameraNode
}

override init() {
super.init()
updateDeviceMotion = { motionData, error in
if let motionData = motionData {
self.motionData = motionData
} else if let error = error {
print(error.localizedDescription)
} else {
print("No motion data to process")
}
}
}
func moveRight() {

}

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

if let motionData = motionData, let pointOfView = renderer.pointOfView {
pointOfView.rotation.w = Float(motionData.attitude.quaternion.angle)
pointOfView.rotation.x = Float(motionData.attitude.quaternion.axis.x)
pointOfView.rotation.y = Float(motionData.attitude.quaternion.axis.z)
pointOfView.rotation.z = -1.0 * Float(motionData.attitude.quaternion.axis.y)
}
if let pointOfView = renderer.pointOfView,
let soundSource = renderer.scene?.rootNode.childNode(withName: "soundSource", recursively: true) {

Expand Down Expand Up @@ -137,6 +184,13 @@ class SceneCoordinator: NSObject, SCNSceneRendererDelegate, ObservableObject {
renderer.showsStatistics = self.showsStatistics
renderer.debugOptions = self.debugOptions
}
func headphoneMotionManagerDidConnect(_ manager: CMHeadphoneMotionManager) {
print("Headphones Connected")
}
func headphoneMotionManagerDidDisconnect(_ manager: CMHeadphoneMotionManager) {
print("Headphones Disconnected")
motionData = nil
}
}

struct AudioKit3DView: View {
Expand Down Expand Up @@ -174,9 +228,11 @@ struct AudioKit3DView: View {
}.cookbookNavBarTitle("Audio 3D")
.onAppear {
viewModel.conductor.start()
viewModel.startHeadTracking()
}
.onDisappear {
viewModel.conductor.stop()
viewModel.stopHeadTracking()
}
}
}
Expand Down
Loading