forked from xiongyihui/tdoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
94 lines (67 loc) · 2.17 KB
/
view.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
import sys
import threading
import Queue
import audioop
import math
import pyaudio
import numpy as np
from gcc_phat import gcc_phat
RATE = 48000
FRAMES = int(RATE / 4)
window = np.hanning(FRAMES)
sound_speed = 343.2
distance = 0.14
max_tau = distance / sound_speed
direction_n = int(max_tau * RATE)
class DOA:
def __init__(self):
self.pyaudio_instance = pyaudio.PyAudio()
self.queue = Queue.Queue()
self.event = threading.Event()
def start(self, quit_event=None, show=None):
stream = self.pyaudio_instance.open(
rate=RATE,
frames_per_buffer=FRAMES,
format=pyaudio.paInt16,
channels=2,
input=True,
# output_device_index=1,
stream_callback=self._callback)
self.event.clear()
if not quit_event:
quit_event = threading.Event()
phat = [0] * (2 * direction_n + 1)
while not (quit_event.is_set() or self.event.is_set()):
try:
data = self.queue.get()
buf = np.fromstring(data, dtype='int16')
tau, cc = gcc_phat(buf[0::2] * window, buf[1::2] * window, fs=RATE, max_tau=max_tau, interp=1)
theta = math.asin(tau / max_tau) * 180 / math.pi
print('\ntheta: {}'.format(int(theta)))
for i, v in enumerate(cc):
phat[i] = int(v * 512)
if show:
show(phat)
# print [l for l in level]
except KeyboardInterrupt:
break
stream.close()
def _callback(self, in_data, frame_count, time_info, status):
self.queue.put(in_data)
return None, pyaudio.paContinue
def main():
from PySide import QtGui
from bar_widget import BarWidget
app = QtGui.QApplication(sys.argv)
widget = BarWidget()
widget.setWindowTitle('Direction Of Arrival')
widget.show()
doa = DOA()
quit_event = threading.Event()
thread = threading.Thread(target=doa.start, args=(quit_event, widget.setBars))
thread.start()
app.exec_()
quit_event.set()
thread.join()
if __name__ == '__main__':
main()