-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaoa_2.py~
executable file
·110 lines (83 loc) · 2.98 KB
/
aoa_2.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
import pyaudio
import numpy as np
import collections
import threading
import signal
import sys
import math
#import audioop
from gcc_phat import gcc_phat
import time
class Microphone:
def __init__(self, rate=48000, channels=2):
self.pyaudio_instance = pyaudio.PyAudio()
#self.queue = Queue.Queue()
self.queue = collections.deque(maxlen=10)
#self.quit_event = threading.Event()
self.channels = channels
self.sample_rate = rate
device_index=None
if device_index is None:
for i in range(self.pyaudio_instance.get_device_count()):
dev = self.pyaudio_instance.get_device_info_by_index(i)
name = dev['name'].encode('utf-8')
print(i, name, dev['maxInputChannels'], dev['maxOutputChannels'])
if dev['maxInputChannels'] == self.channels:
print('Use {}'.format(name))
device_index = i
break
if device_index is None:
print('can not find input device with {} channel(s)'.format(self.channels))
return
self.stream = self.pyaudio_instance.open(
start=True,
format=pyaudio.paInt16,
input_device_index = device_index,
channels=self.channels,
rate=self.sample_rate,
frames_per_buffer=320,
stream_callback=self._callback,
input=True,
)
#while not self.quit_event.is_set():
# frames = self.queue.get()
# if not frames:
# break
# yield frames
#stream.close()
def _callback(self, in_data, frame_count, time_info, status):
#self.queue.put(in_data)
self.queue.append(in_data)
return None, pyaudio.paContinue
#def close(self):
# self.quit_event.set()
# self.queue.put('')
def start(self):
self.stream.start_stream()
def stop(self):
self.stream.stop_stream()
def main():
sample_rate = 48000
channels = 2
mic = Microphone(sample_rate, channels)
max_tau = 0.000166667
#def signal_handler(sig, num):
# print('Quit')
# mic.stop()
#signal.signal(signal.SIGINT, signal_handler)
while True:
try:
time.sleep(1)
buf = b''.join(mic.queue)
buf = np.fromstring(buf, dtype='int16')
tau, _ = gcc_phat(buf[0::2], buf[1::2], fs=sample_rate, max_tau=max_tau,interp=1)
theta = np.arcsin(tau / max_tau) * 180 / np.pi
alpha = np.arccos(-tau / max_tau) * 180 / np.pi
a = int(alpha)
print "alpha:",str(a)," tau:",tau
#print "A:",np.max(buf[0::1]),"B:",np.max(buf[1::2])
except KeyboardInterrupt:
break
mic.stop()
if __name__ == '__main__':
main()