-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealtime_sim.py
349 lines (296 loc) · 12.2 KB
/
realtime_sim.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import pyaudio
import wave
import math
import sofa
import scipy
import threading
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
from pydub import AudioSegment
from scipy.io.wavfile import write
from pynput import keyboard
#import pandas as pd
outputData = np.array([])
class OutputStream:
def __init__(self):
""" Initialize """
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format = 8, #typically 16 bit SIGNED int
channels = 2,
rate = 44100,
input = False,
output = True
)
def close(self):
""" Close stream """
self.stream.close()
self.p.terminate()
class InputStream:
def __init__(self, file):
""" Initialize """
self.wf = wave.open(file, 'rb')
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format = self.p.get_format_from_width(self.wf.getsampwidth()), #typically 16 bit SIGNED int
channels = self.wf.getnchannels(),
rate = self.wf.getframerate(),
input = True,
output = False
)
def close(self):
""" Close stream """
self.stream.close()
self.p.terminate()
class HRTFFile:
emitter = 0
def __init__(self, file):
""" Initialize """
self.HRTFPath = file
self.HRTF = sofa.Database.open(self.HRTFPath)
self.HRTF.Metadata.dump()
self.sphericalPositions = self.HRTF.Source.Position.get_values(system="spherical")
self.measurement = 0
def findMeasurement(self, azimuth, elevation):
""" Find closest IR measurement to target azimuth and elevation """
# TODO try hashmap or binary search
bestIndex = 0
bestError = abs(azimuth - self.sphericalPositions[0][0]) + abs(elevation - self.sphericalPositions[0][1])
for i in range(1, len(self.sphericalPositions)):
azDiff = abs(azimuth - self.sphericalPositions[i][0])
eleDiff = abs(elevation - self.sphericalPositions[i][1])
currError = azDiff + eleDiff
if(currError < bestError):
bestIndex = i
bestError = currError
return bestIndex
def getIR(self, azimuth, elevation):
""" Access IR measurements """
self.measurement = self.findMeasurement(azimuth, elevation)
hrtf1 = self.HRTF.Data.IR.get_values(indices={"M":self.measurement, "R":0, "E":self.emitter})
hrtf2 = self.HRTF.Data.IR.get_values(indices={"M":self.measurement, "R":1, "E":self.emitter})
return [hrtf1, hrtf2]
class Listener:
def __init__(self):
""" Initialilze """
self.xPos = 0
self.yPos = 0
self.zPos = 0
self.azimuthTilt = 0
self.elevationTilt = 0
def getAngles(self):
""" Access head tilt info """
#TODO Add roll
#azimuth tilt = yaw, elevation tilt = pitch
return[self.azimuthTilt, self.elevationTilt]
def getPos(self):
""" Access position info """
return [self.xPos, self.yPos, self.zPos]
def update(self, x, y, z, az, el):
""" Update position """
self.xPos = self.xPos + x
self.yPos = self.yPos + y
self.zPos = self.zPos + z
self.azimuthTilt = self.azimuthTilt + az
if(self.azimuthTilt < 0):
self.azimuthTilt = 360 + self.azimuthTilt
if(self.azimuthTilt >=360):
self.azimuthTilt = self.azimuthTilt - 360
self.elevationTilt = self.elevationTilt + el
if(self.elevationTilt < -90):
self.elevationTilt = -180 - self.elevationTilt
if(self.elevationTilt >90):
self.elevationTilt = 180 - self.elevationTilt
class Scene:
def __init__(self, sources, HRTFFilename, global_listener):
""" Initialize """
self.listener = global_listener
self.HRTF = HRTFFile(HRTFFilename)
self.sources = sources
self.stream = OutputStream()
self.chunkSize = 4096
self.timeIndex = 0
self.fs = 44100
self.exit = False
#self.lastChunk = None
def begin(self):
""" Continuously generate and queue next chunk """
while self.exit==False:
[x, y, z] = self.listener.getPos()
[az, el] = self.listener.getAngles()
#print("POSITION x=", x, " y=", y, " z=", z)
#print("ANGLES az = ", az, " el = ", el)
convolved = self.generateChunk()
if convolved == 'flag':
continue
self.stream.stream.write(convolved)
def quit(self):
""" Exit the Scene """
self.exit = True
global outputData
self.stream.close()
scipy.io.wavfile.write('audio_output/realtime_output.wav', 44100, outputData)
self.stream.p.terminate()
def generateChunk(self):
"""" Generate an audio chunk """
global outputData
flag = 0
original_max = 0
for currSource in self.sources:
data = currSource.getNextChunk(self.chunkSize)
if data == b'':
self.quit()
return 'flag'
data_np = np.frombuffer(data, dtype=np.int16)
temp_max = np.max(abs(data_np))
if(temp_max > original_max):
original_max = temp_max
[azimuth, elevation, attenuation] = self.getAngles(currSource)
[hrtf1, hrtf2] = self.HRTF.getIR(azimuth, elevation)
convolved1 = np.array(signal.fftconvolve(data_np, hrtf1, mode='same')) * attenuation
convolved2 = np.array(signal.fftconvolve(data_np, hrtf2, mode='same')) * attenuation
# convolved1 = np.array(convolved1 * signal.hamming(len(convolved1)))
# convolved2 = np.array(convolved2 * signal.hamming(len(convolved2)))
convolved = np.array([convolved1, convolved2]).T
if(flag==0):
summed = convolved
flag = 1
else:
summed = summed + convolved
norm = np.linalg.norm(summed)
convolved_normalized = (summed / norm)
num_bit = 16
bit_depth = 2 ** (num_bit-1)
convolved_normalized_scaled = convolved_normalized * (original_max / (bit_depth - 1))
convolved_final = np.int16( (convolved_normalized_scaled) / np.max(np.abs(convolved_normalized)) * (bit_depth-1))
# if (self.lastChunk is not None):
# combined = np.append(self.lastChunk, convolved_final)
# #b, a = signal.butter(5, [44100/2], 'lowpass', False)
# #convolved1 = signal.lfilter(b, a, combined[1, :])
# #convolved2 = signal.lfilter(b, a, combined[2, :])
# #convolved_final = np.array([convolved1, convolved2]).T
# self.lastChunk = convolved_final
if outputData.size == 0:
outputData = convolved_final
else:
outputData = np.append(outputData, convolved_final, axis=0)
interleaved = convolved_final.flatten()
return interleaved.tobytes()
def getAngles(self, source):
""" Calculate azimuth and elevation angle from listener to object """
[sourceX, sourceY, sourceZ] = source.getPos()
[listenerX, listenerY, listenerZ] = self.listener.getPos()
[listenerAz, listenerEl] = self.listener.getAngles()
numerator = sourceY - listenerY
denominator = sourceX - listenerX
#Calculate Azimuth
if(denominator == 0):
if(sourceY >= listenerY):
azimuth = 0
else:
azimuth = 180
elif(numerator == 0):
if(sourceX >= listenerX):
azimuth = 90
else:
azimuth = 270
else:
if(listenerY > sourceY):
azimuth = math.degrees(math.atan(numerator / denominator) - math.pi)
else:
azimuth = math.degrees(math.atan(numerator / denominator))
if (azimuth < 0):
azimuth = 360 + azimuth
azimuth = azimuth - listenerAz
#Calculate Elevation
numerator = sourceZ - listenerZ
denominator = math.sqrt( ((sourceX - listenerX)**2) + ((sourceY - listenerY)**2) )
if(numerator == 0):
elevation = 0
elif(denominator == 0):
if(sourceZ<listenerZ):
elevation = -90
else:
elevation = 90
else:
elevation = math.degrees(math.atan(numerator / denominator))
if(elevation > 90):
elevation = 180 - elevation
if(elevation <-90):
elevation = -180 - elevation
elevation = elevation - listenerEl
distance = math.sqrt((sourceX - listenerX)**2 + (sourceY - listenerY)**2 + (sourceZ - listenerZ)**2)
if distance == 0:
attenuation = 1.0
else:
attenuation = 1.0 / (distance**2)
return [azimuth, elevation, attenuation]
class Source:
def __init__(self, x, y, z, filename):
""" Initialize """
self.xPos = x
self.yPos = y
self.zPos = z
self.index = 0
segment = AudioSegment.from_file(filename)
channel_sounds = segment.split_to_mono()
samples = [s.get_array_of_samples() for s in channel_sounds]
self.audioArray = np.array(samples).T
self.stream = InputStream(filename)
def getPos(self):
""" Access position data """
return [self.xPos, self.yPos, self.zPos]
def getSound(self):
""" Access audio info """
return self.audioArray
def getNextChunk(self, chunkSize):
""" Access next chunk """
return self.stream.wf.readframes(chunkSize)
def on_press(key):
""" Add key listeners to main """
"""
Arrow Keys move user around the horizontal plane (X and Y directions)
Space moves user up in space, Shift moves user down in space (Z direction)
W tilts users head up, S tilts users head down (Pitch)
A tilts users head to the left, D tilts users head to the right (Yaw)
"""
global global_listener
try:
if(key.char == 'w'):
global_listener.update(0, 0, 0, 0, 10)
if(key.char == 'a'):
global_listener.update(0, 0, 0, -10, 0)
if(key.char == 's'):
global_listener.update(0, 0, 0, 0, -10)
if(key.char == 'd'):
global_listener.update(0, 0, 0, 10, 0)
else:
print("unknown input")
except:
if(key == keyboard.Key.up):
global_listener.update(0, 1, 0, 0, 0)
elif(key == keyboard.Key.right):
global_listener.update(-1, 0, 0, 0, 0)
elif(key == keyboard.Key.left):
global_listener.update(1, 0, 0, 0, 0)
elif(key == keyboard.Key.down):
global_listener.update(0, -1, 0, 0, 0)
elif(key == keyboard.Key.space):
global_listener.update(0, 0, 1, 0, 0)
elif(key == keyboard.Key.shift):
global_listener.update(0, 0, -1, 0, 0)
else:
print("unknown input")
# azimuth - 0 to 360 counterclockwise, 0 in front
# elevation - -90 to 0 to 90
#TODO Current version only accepts sources that are all the same length, add in something to handle if this is not the case?
if __name__ == "__main__":
global_listener = Listener()
listener = keyboard.Listener(on_press=on_press)
listener.start()
#sources = [Source(0, 0, 0, "audio_sources/sin_440.wav"), Source(5, 0, 0, "audio_sources/sweep.wav"), Source(-3, -3, 0, "audio_sources/sin_600Hz.wav")]
sources = [Source(-5, -5, 0, "audio_sources/sin_500.wav"), Source(5, 5, 0, "audio_sources/sin_300.wav")]
#sources = [Source(0, 0, -5, "audio_sources/piano_mono.wav")]
currentScene = Scene(sources, "hrtf/mit_kemar_normal_pinna.sofa", global_listener)
currentScene.begin()