-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPNstreamsetup.py
168 lines (143 loc) · 5.15 KB
/
PNstreamsetup.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
import pyaudio
import numpy as np
import sys
import threading
import time
# Calculate FFT of input signal
def calculateFFT(data, rate):
# Hamming window for FFT
data = data * np.hamming(len(data))
# abs for only magnitude(ignoring phase)
fft = np.abs(np.fft.fft(data))
# freq = 1/rate
freq = np.fft.fftfreq(len(fft), 1.0 / rate)
# Only positive frequency components
return freq[:int(len(freq)/2)], fft[:int(len(fft)/2)]
class PNstreamsetup():
""""
This class provides access to continuously recorded
data from the microphone.
"""
def __init__(self, device=None, rate=None, updates=10):
self.p = pyaudio.PyAudio()
# Stream parameters
self.chunk = 1024 * 4
self.updates = updates
self.device = device
self.rate = rate
def valid_low_rate(self, device):
"""
Set the rate to the lowest supported audio rate.
"""
for testrate in [44100]:
if self.valid_test(device, testrate):
return testrate
print("SOMETHING'S WRONG! I can't figure out how to use DEV", device)
return None
def valid_test(self, device, rate=44100):
"""
Given a device ID and a rate, return TRUE/False if it's valid.
"""
try:
self.info = self.p.get_device_info_by_index(device)
if not self.info["maxInputChannels"] > 0:
return False
stream = self.p.open(
format=pyaudio.paInt16,
channels=1,
input_device_index=device,
frames_per_buffer=self.chunk,
rate=int(self.info["defaultSampleRate"]),
input=True)
stream.close()
return True
except:
return False
def valid_input_devices(self):
"""
See which devices can be opened for microphone input.
call this when no PyAudio object is loaded.
"""
mics = []
for device in range(self.p.get_device_count()):
if self.valid_test(device):
mics.append(device)
if len(mics) == 0:
print("No microphone devices found!")
else:
print("Found %d microphone devices: %s" % (len(mics), mics))
return mics
def initiate(self):
"""
Run this after changing settings (like rate) before recording
"""
if self.device is None:
self.device = self.valid_input_devices()[0] # Pick the first one
if self.rate is None:
self.rate = self.valid_low_rate(self.device)
self.chunk = int(self.rate/self.updates) # Hold one tenth of a second in memory
if not self.valid_test(self.device, self.rate):
print("guessing a valid microphone device/rate...")
self.device = self.valid_input_devices()[0] #pick the first one
self.rate = self.valid_low_rate(self.device)
self.datax = np.arange(self.chunk)/float(self.rate)
msg = 'recording from "%s" ' % self.info["name"]
msg += '(device %d) ' % self.device
msg += 'at %d Hz' % self.rate
print(msg)
def close(self):
"""gently detach from things."""
print(" -- sending stream termination command...")
self.keepRecording = False #the threads should self-close
while(self.t.isAlive()): #wait for all threads to close
time.sleep(.1)
self.stream.stop_stream()
self.p.terminate()
def stream_readchunk(self):
"""
Reads some audio and re-launches itself
"""
try:
self.data = np.fromstring(self.stream.read(self.chunk), dtype=np.int16)
self.fftx, self.fft = calculateFFT(self.data, self.rate)
except Exception as E:
print(" -- exception! terminating...")
print(E, "\n"*5)
self.keepRecording = False
if self.keepRecording:
self.stream_thread_new()
else:
self.stream.close()
self.p.terminate()
print(" -- stream STOPPED")
self.chunksRead += 1
def stream_thread_new(self):
self.t = threading.Thread(target=self.stream_readchunk)
self.t.start()
def stream_start(self):
"""
Adds data to self.data until termination signal
"""
self.initiate()
print(" -- starting stream")
self.keepRecording = True # set this to False later to terminate stream
self.data = None # will fill up with threaded recording data
self.fft = None
self.dataFiltered = None #same
self.stream = self.p.open(
format=pyaudio.paInt16,
channels=1,
rate=self.rate,
input=True,
frames_per_buffer=self.chunk)
self.stream_thread_new()
if __name__ == "__main__":
hear = PNstreamsetup(updates=10) # optionally set sample rate here
hear.stream_start() # goes forever
lastRead = hear.chunksRead
while True:
while lastRead == hear.chunksRead:
time.sleep(.01)
print(hear.chunksRead, len(hear.data))
lastRead = hear.chunksRead
print("DONE")