|
@@ -0,0 +1,77 @@
|
|
1
|
+#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+# ----------------------------------------------------------------------------
|
|
4
|
+# "THE BEER-WARE LICENSE" (Revision 42):
|
|
5
|
+# <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
6
|
+# you can do whatever you want with this stuff. If we meet some day, and you
|
|
7
|
+# think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
8
|
+# ----------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+import time
|
|
11
|
+import pyaudio
|
|
12
|
+import numpy as np
|
|
13
|
+
|
|
14
|
+class AudioVisualizer:
|
|
15
|
+ def __init__(self, g, f = 20, t = 60.0):
|
|
16
|
+ self.gui = g
|
|
17
|
+ self.interval = 1.0 / f
|
|
18
|
+ self.timeout = t
|
|
19
|
+
|
|
20
|
+ self.chunk = 1024 * 2 # stereo
|
|
21
|
+ self.do_fft = False
|
|
22
|
+
|
|
23
|
+ self.p = pyaudio.PyAudio()
|
|
24
|
+ self.s = self.p.open(format=pyaudio.paInt16, channels=2, rate=44100, input=True)
|
|
25
|
+ print(self.s)
|
|
26
|
+
|
|
27
|
+ def restart(self):
|
|
28
|
+ self.data = self.init()
|
|
29
|
+ self.start = time.time()
|
|
30
|
+
|
|
31
|
+ def finished(self):
|
|
32
|
+ if (time.time() - self.start) >= self.timeout:
|
|
33
|
+ return True
|
|
34
|
+ return FALSE
|
|
35
|
+
|
|
36
|
+ def draw_line(self, x, val, y_off):
|
|
37
|
+ h = int(val) / 32768.0 * self.gui.height / 2
|
|
38
|
+ self.gui.set_pixel(x, y_off + (16 - h), (255, 255, 255))
|
|
39
|
+
|
|
40
|
+ def draw_bar(self, x, val, y_off):
|
|
41
|
+ h = int(val) / (32768.0 * 2) * (self.gui.height / 2)
|
|
42
|
+ for y in range(0, int(h)):
|
|
43
|
+ self.gui.set_pixel(x, y_off + (32 - y), (255, 255, 255))
|
|
44
|
+
|
|
45
|
+ def draw(self):
|
|
46
|
+ frames = self.s.read(self.chunk)
|
|
47
|
+
|
|
48
|
+ left = frames[::4] # start from first left sample (index 0), skip 4 bytes (2 * 2, int16 and stereo)
|
|
49
|
+ left = np.frombuffer(left, dtype=np.int16)
|
|
50
|
+
|
|
51
|
+ right = frames[2::4] # start from first right sample (index 2), skip 4 bytes
|
|
52
|
+ right = np.frombuffer(right, dtype=np.int16)
|
|
53
|
+
|
|
54
|
+ if self.do_fft:
|
|
55
|
+ # FFT
|
|
56
|
+ left = np.fft.fft(left)
|
|
57
|
+ right = np.fft.fft(right)
|
|
58
|
+
|
|
59
|
+ # average down to fit screen
|
|
60
|
+ m = self.chunk / 2 / self.gui.width # stereo
|
|
61
|
+ left = left.reshape(-1, int(m)).mean(axis=1)
|
|
62
|
+ right = right.reshape(-1, int(m)).mean(axis=1)
|
|
63
|
+
|
|
64
|
+ for x in range(0, self.gui.width):
|
|
65
|
+ if self.do_fft:
|
|
66
|
+ self.draw_bar(x, left[x], 0)
|
|
67
|
+ self.draw_bar(x, right[x], self.gui.height / 2)
|
|
68
|
+ else:
|
|
69
|
+ self.draw_line(x, left[x], 0)
|
|
70
|
+ self.draw_line(x, right[x], self.gui.height / 2)
|
|
71
|
+
|
|
72
|
+if __name__ == "__main__":
|
|
73
|
+ import util
|
|
74
|
+ i = util.getInput()
|
|
75
|
+ t = util.getTarget(i)
|
|
76
|
+ a = AudioVisualizer(t)
|
|
77
|
+ util.loop(t, a.draw)
|