-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime_specgram.py
More file actions
216 lines (185 loc) · 6.63 KB
/
realtime_specgram.py
File metadata and controls
216 lines (185 loc) · 6.63 KB
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
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
Created on 2015/04/23
@author: drumichiro
'''
import numpy as np
import pylab as plt
import threading
import recorder as rec
import synthesizer as syn
from matplotlib import gridspec
from matplotlib.mlab import specgram
from matplotlib.colors import LogNorm
import matplotlib.animation as animation
import time
###########################################
BUFFER_LENGTH = 64*1024
BUFFER_MARGIN = 1024
###########################################
PushedPosition = 0
PopedPosition = 0
SignalBuffer = np.zeros(BUFFER_LENGTH)
BufferSemaphore = threading.Semaphore()
BufferPushEvent = threading.Event()
BufferPopEvent = threading.Event()
###########################################
class AudioListener(threading.Thread):
def __init__(self, samplingRate, frameLength):
super(AudioListener, self).__init__()
self.SAMPLING_RATE = 44100
self.FRAME_LENGTH = 1024
self.running = True
self.audio = rec.Recoder(self.SAMPLING_RATE, self.FRAME_LENGTH)
# self.audio = syn.Synthesizer(self.SAMPLING_RATE,
# np.ones((self.FRAME_LENGTH, 1)))
def run(self):
while self.running:
signal = self.audio.getStream()
length = len(signal)
if 0 == length:
# Wait for Microphone to be ready
time.sleep(0.1)
continue
elif self.isNotSignalBufferReleased(length):
# Wait for accumulating.
BufferPopEvent.clear()
BufferPopEvent.wait()
# Retry to check.
continue
BufferSemaphore.acquire() # Lock.
self.accumulateSignalBuffer(signal)
BufferSemaphore.release() # Unlock.
if not BufferPushEvent.is_set():
# Suppose accumulating enough buffer in the above.
BufferPushEvent.set()
def stop(self):
self.running = False
self.join()
del self.audio
def isNotSignalBufferReleased(self, length):
rest = PopedPosition - PushedPosition
if rest <= 0:
rest += BUFFER_LENGTH
return length > (rest - BUFFER_MARGIN)
# releaseSignalBuffer() is a brother.
def accumulateSignalBuffer(self, signal):
global PushedPosition
global SignalBuffer
begin = PushedPosition
end = (PushedPosition + len(signal)) % BUFFER_LENGTH
assert BufferPopEvent.is_set()
PushedPosition = end
if begin < end:
SignalBuffer[begin:end] = signal
else:
rest = BUFFER_LENGTH - begin
SignalBuffer[begin:] = signal[:rest]
SignalBuffer[:end] = signal[rest:]
class SpecgramListener(threading.Thread):
def __init__(self, length, frames):
super(SpecgramListener, self).__init__()
self.FRAME_LENGTH = length
self.running = True
bins = self.FRAME_LENGTH/2 + 1
self.spectrogram = np.zeros((bins, frames), np.float)
self.spectrum = np.zeros(bins, np.float)
self.envelope = np.zeros(frames, np.float)
self.window = np.hamming(self.FRAME_LENGTH)
self.frames = frames
def run(self):
pos = 0
while self.running:
if self.isNotSignalBufferAccumulated():
# Wait for accumulating.
BufferPushEvent.clear()
BufferPushEvent.wait()
# Retry to check.
continue
BufferSemaphore.acquire() # Lock.
signal = self.releaseSignalBuffer()
self.spectrum = self.transformToSpectrum(signal)
self.spectrogram[:, pos:pos+1] = self.spectrum.reshape((-1, 1))
self.envelope = np.sum(self.spectrogram, axis=0)
BufferSemaphore.release() # Unlock.
pos = (pos + 1) % self.frames
if not BufferPopEvent.is_set():
# Suppose releasing enough buffer in the above.
BufferPopEvent.set()
def stop(self):
self.running = False
self.join()
def isNotSignalBufferAccumulated(self):
rest = PushedPosition - PopedPosition
if rest < 0:
rest += BUFFER_LENGTH
return self.FRAME_LENGTH > (rest - BUFFER_MARGIN)
# accumulateSignalBuffer() is a brother.
def releaseSignalBuffer(self):
global PopedPosition
begin = PopedPosition
end = (PopedPosition + self.FRAME_LENGTH) % BUFFER_LENGTH
# PopedPosition is changed only here, so is_set() is always true.
assert BufferPushEvent.is_set()
PopedPosition = end
if begin < end:
return SignalBuffer[begin:end]
else:
return np.append(SignalBuffer[begin:], SignalBuffer[:end])
def transformToSpectrum(self, signal):
return specgram(signal, NFFT=self.FRAME_LENGTH,
noverlap=0, window=self.window)[0]
def getSpecgramReference(self):
return self.spectrogram
def getSpectrumReference(self):
return self.spectrum
def getEnvelopeReference(self):
return self.envelope
def updatePlot(i1, spec, axes, img, line):
axl = axes[1]
axb = axes[2]
# Ignore drawing first frame.
if 0 == i1:
return []
img.set_data(spec.getSpecgramReference())
if 1 < i1:
axl.lines.pop(0)
axb.lines.pop(0)
BufferSemaphore.acquire()
left, = axl.plot(spec.getSpectrumReference(), line, "b")
bottom, = axb.plot(spec.getEnvelopeReference(), "b")
BufferSemaphore.release()
return [left, bottom, img]
if __name__ == '__main__':
samplingRate = 44100
frameLength = 1024
bins = frameLength/2 + 1
frames = 150
fig = plt.figure(figsize=(20, 10))
gs = gridspec.GridSpec(2, 2, width_ratios=[1, 5], height_ratios=[5, 1])
axs = plt.subplot(gs[0, 1])
axl = plt.subplot(gs[0, 0], sharey=axs)
axb = plt.subplot(gs[1, 1], sharex=axs)
axs.set_xlim([0, frames])
axs.set_ylim([0, bins])
axl.set_xlim([0, 0.3])
axb.set_ylim([0, 3.0])
axes = [axs, axl, axb]
line = np.linspace(0.0, bins - 1, bins)
audio = AudioListener(samplingRate, 1024)
spec = SpecgramListener(frameLength, frames)
img = axs.matshow(spec.getSpecgramReference(), aspect="auto",
origin='lower', norm=LogNorm(vmin=1e-10, vmax=1))
BufferPushEvent.set()
BufferPopEvent.set()
# Start plot.
anime = animation.FuncAnimation(fig, updatePlot, interval=50, blit=True,
fargs=(spec, axes, img, line))
audio.start()
spec.start()
print "Drawing."
plt.show()
audio.stop()
spec.stop()
print "Done."