-
Notifications
You must be signed in to change notification settings - Fork 2
/
AudioApplication.py
215 lines (167 loc) · 6.27 KB
/
AudioApplication.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
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
import sys
import thread
from PyQt4 import QtGui
from PyQt4.QtCore import QThread
import pyaudio
import pyqtgraph as pg
import neuralModel
import numpy as np
import wave
from scipy.fftpack import fft
from scipy import fromstring
FORMAT = pyaudio.paInt16
CHANNELS = 1
FS = 8000
CHUNK = 1024
terminated = False
recording = False
switch = 0
def nengo_thread_func():
global recording, switch
last_switch = 0
# while not terminated:
# if not recording and (switch != last_switch):
# time.sleep(0.1) # If there is no delay, Nengo tries to open the file while the other is writing
last_switch = switch
nengo_thread = NengoThread()
nengo_thread.start()
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUI()
self.mainLoop()
def setupUI(self):
self.setWindowTitle("Audio Application") # Title
self.resize(1300, 500) # Size
### Widgets
centralWid = QtGui.QWidget()
self.setCentralWidget(centralWid)
layV = QtGui.QVBoxLayout()
centralWid.setLayout(layV)
btn_record = QtGui.QPushButton('Record')
def clickedfunc():
global recording, switch
if recording:
btn_record.setStyleSheet("background-color: None")
btn_record.setText('Record')
recording = False
switch += 1
else:
btn_record.setStyleSheet("background-color: red")
btn_record.setText('Recording')
recording = True
btn_record.clicked.connect(clickedfunc)
topWidget = QtGui.QWidget()
layV.addWidget(topWidget)
layV.addWidget(btn_record)
layH = QtGui.QHBoxLayout()
topWidget.setLayout(layH)
LeftWidget = QtGui.QWidget()
RightWidget = QtGui.QWidget()
layH.addWidget(LeftWidget)
layH.addWidget(RightWidget)
LeftlayV = QtGui.QVBoxLayout()
LeftWidget.setLayout(LeftlayV)
RightlayV = QtGui.QVBoxLayout()
RightWidget.setLayout(RightlayV)
### Original Wave display widget
waveWid = pg.PlotWidget(title="Original Wave")
self.origWave = waveWid.getPlotItem()
self.origWave.setMouseEnabled(y=False) # to not be moved to the y-axis direction
self.origWave.setYRange(-10000, 10000)
self.origWave.setXRange(0, 512, padding=0)
### Axis
specAxis = self.origWave.getAxis("bottom")
specAxis.setLabel("Samples")
LeftlayV.addWidget(waveWid)
### Spectrum display widget
fftWid = pg.PlotWidget(title="FFT")
self.fftItem = fftWid.getPlotItem()
self.fftItem.setMouseEnabled(y=False) # to not be moved to the y-axis direction
self.fftItem.setYRange(0, 3000)
self.fftItem.setXRange(0, FS / 2, padding=0)
### Axis
specAxis = self.fftItem.getAxis("bottom")
specAxis.setLabel("Frequency [Hz]")
LeftlayV.addWidget(fftWid)
### Spectogram
specWid = pg.PlotWidget()
self.specItem = pg.ImageItem()
specWid.addItem(self.specItem)
self.img_array = np.zeros((100, CHUNK / 2))
# bipolar colormap
pos = np.array([0., 1., 0.5, 0.25, 0.75])
color = np.array([[0, 255, 255, 255], [255, 255, 0, 255], [0, 0, 0, 255], (0, 0, 255, 255), (255, 0, 0, 255)],
dtype=np.ubyte)
cmap = pg.ColorMap(pos, color)
lut = cmap.getLookupTable(0.0, 1.0, 256)
# set colormap
self.specItem.setLookupTable(lut)
self.specItem.setLevels([-50, 40])
# setup the correct scaling for y-axis
freq = np.arange((CHUNK / 2) + 1) / (float(CHUNK) / FS)
yscale = 1.0 / (self.img_array.shape[1] / freq[-1])
self.specItem.scale((1. / FS) * CHUNK, yscale)
specWid.setLabel('left', 'Frequency', units='Hz')
RightlayV.addWidget(specWid)
self.show()
def mainLoop(self):
global terminated, switch
# start Recording
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=FS, input=True,
frames_per_buffer=CHUNK)
# Cleaning the file. In case of a force quit, the file keep the last run data
open('out.bin', 'wb').close()
last_switch = 0
sound = []
while not terminated:
if recording:
# get audio samples
data = stream.read(CHUNK)
orig = fromstring(data, dtype="int16")
sound = np.append(sound, orig)
# Transform to frequency domain (FFT)
# reduce noise and transform back to time
originalfft = np.array(fft(orig))
fftSpec = abs(originalfft) / (CHUNK / 2)
fftSpec = fftSpec[:int(CHUNK / 2)]
xf = 1.0 * np.arange(0, FS / 2., FS / (1. * CHUNK))
# Spectrogram
self.img_array = np.roll(self.img_array, -1, 0)
self.img_array[-1:] = 10.0 * np.log10(fftSpec)
# Plotting Graphs
self.origWave.plot(orig, clear=True)
self.fftItem.plot(xf, fftSpec, clear=True)
self.specItem.setImage(self.img_array, autoLevels=False)
else:
if last_switch != switch:
last_switch = switch
frames = []
# Write in file
f = open('out.bin', 'ab')
for i in range(len(sound)):
f.write('%d ' % sound[i])
f.write('\n')
f.close()
sound = []
QtGui.QApplication.processEvents()
stream.stop_stream()
stream.close()
audio.terminate()
def closeEvent(self, event):
global terminated
terminated = True
event.accept()
class NengoThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
neuralModel.run()
if __name__ == '__main__':
thread.start_new_thread(nengo_thread_func, ())
app = QtGui.QApplication(sys.argv)
win = MainWindow()