-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_visualizer.py
132 lines (104 loc) · 3.72 KB
/
audio_visualizer.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
import collections
import colorsys
import time
from threading import *
import numpy as np
import pyaudio
from microphone_recorder import MicrophoneRecorder
class AudioVisualizer(Thread):
def __init__(self, event):
Thread.__init__(self)
self.stopped = event
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 44100
self.CHUNKSIZE = 1024
self.N_FFT = 2048
self.previous_hue = None
self.hue_offset = 0
self.previous_spectrum = collections.deque(maxlen=10)
self.recorder = MicrophoneRecorder(sample_rate=self.RATE, chunksize=self.CHUNKSIZE)
self.recorder.start()
def run(self):
while not self.stopped.wait(0.1):
self.update()
def stop(self):
self.recorder.close()
self.stopped.set()
self.join()
def update(self):
frames = self.recorder.get_frames()
if len(frames) == 0:
data = np.zeros((self.recorder.chunksize,), dtype=np.int)
else:
data = frames[-1]
if data.max() > 1:
self.get_spectrum_data(data)
def get_spectrum_data(self, data):
spectrum = np.fft.fft(np.hanning(data.size) * data, n=self.N_FFT)
self.get_bark_split(spectrum)
# spectrum_magnitude = np.sqrt(np.real(spectrum) ** 2 + np.imag(spectrum) ** 2)
# spectrum_magnitude = spectrum_magnitude[:self.CHUNKSIZE] * 2 / (128 * self.CHUNKSIZE)
def get_bark_split(self, data):
bark_scale = [0, 100, 200, 300, 400, 510, 630, 770, 920, 1080, 1270, 1480, 1720, 2000,
2320, 2700, 3150, 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500]
bark_scale_vector = [
{'freq': 100, 'data': [], 'value': 0},
{'freq': 200, 'data': [], 'value': 0},
{'freq': 300, 'data': [], 'value': 0},
{'freq': 400, 'data': [], 'value': 0},
{'freq': 510, 'data': [], 'value': 0},
{'freq': 630, 'data': [], 'value': 0},
{'freq': 770, 'data': [], 'value': 0},
{'freq': 920, 'data': [], 'value': 0},
{'freq': 1080, 'data': [], 'value': 0},
{'freq': 1270, 'data': [], 'value': 0},
{'freq': 1480, 'data': [], 'value': 0},
{'freq': 1720, 'data': [], 'value': 0},
{'freq': 2000, 'data': [], 'value': 0},
{'freq': 2320, 'data': [], 'value': 0},
{'freq': 2700, 'data': [], 'value': 0},
{'freq': 3150, 'data': [], 'value': 0},
{'freq': 3700, 'data': [], 'value': 0},
{'freq': 4400, 'data': [], 'value': 0},
{'freq': 5300, 'data': [], 'value': 0},
{'freq': 6400, 'data': [], 'value': 0},
{'freq': 7700, 'data': [], 'value': 0},
{'freq': 9500, 'data': [], 'value': 0},
{'freq': 12000, 'data': [], 'value': 0},
{'freq': 15500, 'data': [], 'value': 0},
]
step = self.RATE // self.N_FFT
for i in range(self.CHUNKSIZE):
freq = i * step
value = data[i]
for bark in bark_scale_vector[::-1]:
if freq >= bark['freq']:
bark['data'].append(value)
break
y = []
for bark in bark_scale_vector:
magnitude = np.sqrt(np.real(bark['data']) ** 2 + np.imag(bark['data']) ** 2)
d = np.linalg.norm(magnitude)
bark['value'] = int(d)
y.append(bark['value'])
y = np.array(y)
y = (y - y.min()) / (y.max() - y.min())
self.set_color(y)
def set_color(self, data):
blue = np.linalg.norm(data[:8]) * 0.8
green = np.linalg.norm(data[8:16])
red = np.linalg.norm(data[16:])
sum = blue + green + red
red_ratio = red / sum
green_ratio = green / sum
blue_ratio = blue / sum
hue, saturation, value = colorsys.rgb_to_hsv(red_ratio, green_ratio, blue_ratio)
r = colorsys.hsv_to_rgb(hue, 1, 1)
print(r)
# print(hue)
print(hue, hex(int(hue * 255)))
if __name__ == '__main__':
stopFlag = Event()
thread = AudioVisualizer(stopFlag)
thread.start()