-
Notifications
You must be signed in to change notification settings - Fork 8
/
sound-spectrum.py
executable file
·123 lines (93 loc) · 2.81 KB
/
sound-spectrum.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
#!/usr/bin/env python
# Written by Yu-Jie Lin
# Public Domain
#
# Deps: PyAudio, NumPy, and Matplotlib
# Blog: https://yjlv.blogspot.com2012/11/frequency-spectrum-of-sound-using.html
import struct
import wave
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import pyaudio
SAVE = 0.0
TITLE = ''
WIDTH = 1280
HEIGHT = 720
FPS = 25.0
nFFT = 512
BUF_SIZE = 4 * nFFT
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
def animate(i, line, stream, wf, MAX_y):
# Read n*nFFT frames from stream, n > 0
N = max(stream.get_read_available() / nFFT, 1) * nFFT
data = stream.read(N)
if SAVE:
wf.writeframes(data)
# Unpack data, LRLRLR...
y = np.array(struct.unpack("%dh" % (N * CHANNELS), data)) / MAX_y
y_L = y[::2]
y_R = y[1::2]
Y_L = np.fft.fft(y_L, nFFT)
Y_R = np.fft.fft(y_R, nFFT)
# Sewing FFT of two channels together, DC part uses right channel's
Y = abs(np.hstack((Y_L[-nFFT / 2:-1], Y_R[:nFFT / 2])))
line.set_ydata(Y)
return line,
def init(line):
# This data is a clear frame for animation
line.set_ydata(np.zeros(nFFT - 1))
return line,
def main():
dpi = plt.rcParams['figure.dpi']
plt.rcParams['savefig.dpi'] = dpi
plt.rcParams["figure.figsize"] = (1.0 * WIDTH / dpi, 1.0 * HEIGHT / dpi)
fig = plt.figure()
# Frequency range
x_f = 1.0 * np.arange(-nFFT / 2 + 1, nFFT / 2) / nFFT * RATE
ax = fig.add_subplot(111, title=TITLE, xlim=(x_f[0], x_f[-1]),
ylim=(0, 2 * np.pi * nFFT ** 2 / RATE))
ax.set_yscale('symlog', linthreshy=nFFT ** 0.5)
line, = ax.plot(x_f, np.zeros(nFFT - 1))
# Change x tick labels for left channel
def change_xlabel(evt):
labels = [label.get_text().replace(u'\u2212', '')
for label in ax.get_xticklabels()]
ax.set_xticklabels(labels)
fig.canvas.mpl_disconnect(drawid)
drawid = fig.canvas.mpl_connect('draw_event', change_xlabel)
p = pyaudio.PyAudio()
# Used for normalizing signal. If use paFloat32, then it's already -1..1.
# Because of saving wave, paInt16 will be easier.
MAX_y = 2.0 ** (p.get_sample_size(FORMAT) * 8 - 1)
frames = None
wf = None
if SAVE:
frames = int(FPS * SAVE)
wf = wave.open('temp.wav', 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=BUF_SIZE)
ani = animation.FuncAnimation(
fig, animate, frames,
init_func=lambda: init(line), fargs=(line, stream, wf, MAX_y),
interval=1000.0 / FPS, blit=True
)
if SAVE:
ani.save('temp.mp4', fps=FPS)
else:
plt.show()
stream.stop_stream()
stream.close()
p.terminate()
if SAVE:
wf.close()
if __name__ == '__main__':
main()