-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauvdiovis_Qt.py
131 lines (117 loc) · 4.97 KB
/
auvdiovis_Qt.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
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import pyaudio
import sys
import time
from scipy.fftpack import fft
import struct
class AudioStream(object):
def __init__(self):
# pyqtgraph stuff
#########################################################
pg.setConfigOptions(antialias=True)
self.traces = dict()
self.app = QtGui.QApplication(sys.argv)
self.win = pg.GraphicsWindow(title='Spectrum Analyzer')
self.win.setWindowTitle('Spectrum Analyzer')
self.win.setGeometry(5, 115, 1910, 1070)
##########################################################
# Waveform plot parameters/settings.
##############################################################
waveform_xlabels = [(0, '0'), (2048, '2048'), (4096, '4096')]
waveform_xaxis = pg.AxisItem(orientation='bottom')
waveform_xaxis.setTicks([waveform_xlabels])
waveform_ylabels = [(0, '0'), (127, '128'), (255, '255')]
waveform_yaxis = pg.AxisItem(orientation='left')
waveform_yaxis.setTicks([waveform_ylabels])
###############################################################
# Spectrum plot parameters/settings.
##########################################################
spectrum_xlabels = [
(np.log10(10), '10'), (np.log10(100), '100'),
(np.log10(1000), '1000'), (np.log10(22050), '22050')
]
spectrum_xaxis = pg.AxisItem(orientation='bottom')
spectrum_xaxis.setTicks([spectrum_xlabels])
# Adding plots to the main window.
#####################################################################################################
self.waveform = self.win.addPlot(
title='WAVEFORM', row=1, col=1, axisItems={'bottom': waveform_xaxis, 'left': waveform_yaxis},
)
self.spectrum = self.win.addPlot(
title='SPECTRUM', row=2, col=1, axisItems={'bottom': spectrum_xaxis},
)
#######################################################################################################
###############################
# PyAudio stuff for input
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 44100
self.CHUNK = 1024 * 4
self.DEVICE = 2
###############################
# Create the object for PyAudio class
###########################
self.p = pyaudio.PyAudio()
############################
# Open the stream
#####################################
self.stream = self.p.open(
format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
output=True,
frames_per_buffer=self.CHUNK,
input_device_index=self.DEVICE
)
######################################
# Waveform and spectrum x points
#######################################################
self.x = np.arange(0, 2 * self.CHUNK, 2)
self.f = np.linspace(0, self.RATE / 2, self.CHUNK / 2)
#######################################################
def start(self):
"""
Start the execution of application.
"""
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
def set_plotdata(self, name, data_x, data_y):
"""
Set parameters for the waveform and spectrum.
"""
if name in self.traces:
self.traces[name].setData(data_x, data_y)
else:
if name == 'waveform':
self.traces[name] = self.waveform.plot(pen='g', width=3)
self.waveform.setYRange(0, 255, padding=0)
self.waveform.setXRange(0, 2 * self.CHUNK, padding=0.005)
if name == 'spectrum':
self.traces[name] = self.spectrum.plot(pen='b', width=3)
self.spectrum.setLogMode(x=True, y=True)
self.spectrum.setYRange(-4, 0, padding=0)
self.spectrum.setXRange(
np.log10(20), np.log10(self.RATE / 2), padding=0.005)
def update(self):
"""
Update the plots.
"""
wf_data = self.stream.read(self.CHUNK, exception_on_overflow=False)
wf_data = struct.unpack(str(2 * self.CHUNK) + 'B', wf_data)
wf_data = np.array(wf_data, dtype='b')[::2] + 128
self.set_plotdata(name='waveform', data_x=self.x, data_y=wf_data,)
sp_data = fft(np.array(wf_data, dtype='int8') - 128)
sp_data = np.abs(sp_data[0:int(self.CHUNK / 2)]
) * 2 / (128 * self.CHUNK)
self.set_plotdata(name='spectrum', data_x=self.f, data_y=sp_data)
def animation(self):
timer = QtCore.QTimer()
timer.timeout.connect(self.update)
timer.start(10)
self.start()
if __name__ == '__main__':
audio_app = AudioStream()
audio_app.animation()