forked from davircarvalho/multiHRTFrenderer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer_gui_functions.py
100 lines (79 loc) · 2.71 KB
/
renderer_gui_functions.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 26 19:46:07 2023
@author: felip
"""
import sofar
import warnings
import numpy as np
import pyaudio
from copy import deepcopy
import keyboard
import time
def sofa_setup(SOFAfiles):
Objs = []
samplingRate = []
for n, name in enumerate(SOFAfiles):
Objs.append(sofar.read_sofa(name))
samplingRate.append(Objs[n].Data_SamplingRate)
if not np.allclose(samplingRate, samplingRate):
warnings.warn('SOFA Sampling Rates do not match\n >>>>You should NOT continue!<<<<<')
else:
fs = int(samplingRate[0])
return Objs, samplingRate, fs
def start_audio(fs, buffer_sz, audio_in, sofaIDXmanager, SOFAfiles,
isHeadTracker, PosManager, HTreceiver, FIRfilt, Objs,
stop_event, idxSOFA):
# instantiate PyAudio (1)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
channels=2,
rate=fs,
output=True,
frames_per_buffer=buffer_sz)
# play stream (3)
sigLen = audio_in.shape[0]
data_out = np.zeros((buffer_sz, 2))
frame_start = 0
frame_end = frame_start + buffer_sz
'''
Changed by Felipe
'''
while True:
# check if dataset has changed
idxSOFA_tmp = idxSOFA
if idxSOFA_tmp < len(SOFAfiles): # only update if index is within range
idxSOFA = deepcopy(idxSOFA_tmp)
# get head tracker position
if isHeadTracker:
idxPos = PosManager[idxSOFA].closestPosIdx(HTreceiver.yaw, HTreceiver.pitch, -1*HTreceiver.roll)
# process data
data_out = FIRfilt.process(audio_in[frame_start:frame_end, :],
h=Objs[idxSOFA].Data_IR[idxPos, :, :].T)
# output data
data_out = np.ascontiguousarray(data_out, dtype=np.float32)
stream.write(data_out, buffer_sz)
# # update reading positions
frame_start = deepcopy(frame_end)
frame_end = frame_start + buffer_sz
if frame_end >= sigLen:
frame_start = 0
frame_end = frame_start + buffer_sz
if stop_event.is_set():
break
# stop stream (4)
stream.stop_stream()
stream.close()
# close PyAudio (5)
p.terminate()
# Not actually used...
# def savePosition(data):
# savepos = False
# while True:
# time.sleep(0.1)
# # For using with the pointer/slide controler
# if keyboard.is_pressed('right'):
# savepos = True
# # take action after key release
# if (not keyboard.is_pressed('right') and savepos): # capture position
# savepos = False