forked from ianhalpern/Impulse
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimpulse.py
executable file
·61 lines (51 loc) · 1.53 KB
/
impulse.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
#!/usr/bin/env python
import curses
import time
import impulse
import wiringPy
def bar(window, x, y, height, value):
stop = value * height
for i in range(height, -1, -1):
if i < stop:
label = "****"
color = (i * 4 / height) + 1
else:
label = " "
color = 0
window.addstr(y + height - i, x, label, curses.color_pair(color))
def draw(window):
h,w = window.getmaxyx()
audio_sample_array = impulse.getSnapshot(True)
i = 0
l = len(audio_sample_array) / 4
sum = 0
step = l / ((w - 10) / 5)
for x in range(3, w - 5, 5):
value = audio_sample_array[i]
bar(window, x, 3, h - 6 , value)
sum += value
i += step
leds = 8 - min(8, int(sum * 2))
wiringPy.digital_write_byte((0xFF * (2 ** leds)) & 0xFF)
def main(window):
wiringPy.setup();
for pin in range(0,8):
wiringPy.pin_mode(pin, 1)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_GREEN)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW)
curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_RED)
curses.curs_set(0)
window.nodelay(True)
window.clear()
window.border(0)
window.addstr(2, 2, "Raspberry Ladder VU meter")
window.refresh()
while window.getch() < 0:
draw(window)
window.refresh()
time.sleep(0.05)
window.clear()
window.refresh()
if __name__ == "__main__":
curses.wrapper(main)