-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView.swift
188 lines (152 loc) · 6.09 KB
/
ContentView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// ContentView.swift
// ARApp
//
// Created by Mridang Sheth on 10/15/22.
//
import SwiftUI
import RealityKit
import ARKit
import AVFoundation
struct ContentView : View {
@State var sceneDepthStr: String
var body: some View {
ZStack {
ARViewContainer(sceneDepthStr: $sceneDepthStr).edgesIgnoringSafeArea(.all)
VStack {
Text("Distance: \(self.sceneDepthStr) cm")
.background(Color.gray.opacity(0.5))
.multilineTextAlignment(.center)
.padding()
.font(.title)
.bold()
Spacer()
}
Button(
action: {
Speaker.sharedInstance.speak(text: "Stop touching me.")
print("Picture taken")
}
) {
Text("Take a Picture").frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
}
struct ARViewContainer: UIViewRepresentable {
@Binding var sceneDepthStr: String
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
// Start AR session
let session = arView.session
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
if ARWorldTrackingConfiguration.supportsFrameSemantics(.smoothedSceneDepth) {
config.frameSemantics = .smoothedSceneDepth
} else {
// TODO: Raise Error
}
session.delegate = context.coordinator
session.run(config)
// Add coaching overlay
let coachingOverlay = ARCoachingOverlayView()
coachingOverlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
coachingOverlay.session = session
coachingOverlay.goal = .horizontalPlane
arView.addSubview(coachingOverlay)
return arView
}
func makeCoordinator() -> Coordinator {
return Coordinator(sceneDepthStr: $sceneDepthStr)
}
class Coordinator: NSObject, ARSessionDelegate, AVAudioPlayerDelegate {
@Binding var sceneDepthStr: String
var isAudioPlaying: Bool = false
var readDistance: Bool = false
var audioPlayer: AVAudioPlayer!
var repeatFreq:Float = 5.0
init(sceneDepthStr: Binding<String>) {
_sceneDepthStr = sceneDepthStr
let beepFile = URL(filePath: Bundle.main.path(forResource: "beep", ofType: "m4a")!)
audioPlayer = try? AVAudioPlayer(contentsOf: beepFile)
super.init()
audioPlayer.delegate = self
}
func session(_ session: ARSession, didUpdate frame: ARFrame) {
if let sceneDepth = frame.smoothedSceneDepth {
let depthData = sceneDepth.depthMap
let depthWidth = CVPixelBufferGetWidth(depthData) // 256
let depthHeight = CVPixelBufferGetHeight(depthData) // 192
CVPixelBufferLockBaseAddress(depthData, CVPixelBufferLockFlags(rawValue: 0))
let floatBuffer = unsafeBitCast(CVPixelBufferGetBaseAddress(depthData), to: UnsafeMutablePointer<Float32>.self)
var minDist: Float32 = 1000000
for x in 71...121 { //width (0 to 192-1)
for y in 103...178 { //height (0 to 256-1)
let distXY = floatBuffer[x * depthWidth + y]
if minDist > distXY {
minDist = distXY
}
}
}
let roundedDist = round(minDist * 100) / 100.0
DispatchQueue.main.async { [weak self] in
self?.sceneDepthStr = "\(Int(roundedDist * 100))"
var repeatFreq: Float = 0
if roundedDist <= 1 {
if !self!.readDistance {
self?.readDistance = true
Speaker.sharedInstance.speak(text: "Object 3 feet ahead")
}
repeatFreq = roundedDist * 3
if roundedDist < 0.25 {
repeatFreq *= 1/5
} else if roundedDist < 0.5 {
repeatFreq *= 1/2
}
self?.repeatFreq = repeatFreq
if !self!.isAudioPlaying {
self?.isAudioPlaying = true
self?.audioPlayer.prepareToPlay()
self?.playBeep()
}
} else {
if self!.readDistance {
self?.readDistance = false
}
if self!.isAudioPlaying {
self?.isAudioPlaying = false
self?.audioPlayer.stop()
}
}
}
}
}
@objc func playBeep() {
audioPlayer.play()
}
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if isAudioPlaying {
self.perform(#selector(playBeep), with: nil, afterDelay: Double(self.repeatFreq))
}
}
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView(sceneDepthStr: "")
}
}
#endif
class Speaker {
static let sharedInstance = Speaker()
let speechSynthesizer = AVSpeechSynthesizer()
func speak(text: String) {
let utterance = AVSpeechUtterance(string: text)
utterance.pitchMultiplier = 1.0
utterance.rate = 0.6
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
speechSynthesizer.speak(utterance)
}
}