-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_visualization.py
More file actions
executable file
·76 lines (62 loc) · 2.21 KB
/
audio_visualization.py
File metadata and controls
executable file
·76 lines (62 loc) · 2.21 KB
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
#!/usr/bin/env python
''' audio_visualization.py V1.
A Realtime Audio Visualization in python using a Raspberrypi + Sense HAT and a USB microphone.
Launch with root user.
Press Control + C, to exit.'''
from sense_hat import SenseHat
import alsaaudio, audioop
from time import sleep
from colour import Color
def hex_to_rgb(colors_list):
"""Return (red, green, blue) for the color given as #rrggbb list."""
rgb_colors_list=[]
for colors in colors_list:
value = str(colors.hex_l)
value = value.lstrip('#')
lv = len(value)
rgb_colors_list.append(tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)))
return rgb_colors_list
if __name__ == "__main__":
card = 'sysdefault:CARD=Device' # Microphone card (check with alsaaudio.cards())
max_audioop = 32754 # Max value
black = (0, 0, 0) # Empty
ini_gradient = Color("yellow") # Initial color gradient
end_gradient = Color("red") # End color gradient
granularity = 512 # Granularity level
gradient = [black] + hex_to_rgb(list(ini_gradient.range_to(end_gradient,granularity+1)))
power = 32
# Initialize SenseHat
sense = SenseHat()
# Initialize Alsa Audio
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card)
inp.setchannels(1)
inp.setrate(8000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(160)
# Clean screen
screen = [black] * ( 8 * 8 )
sense.set_pixels(screen)
# Read From microphone
try:
while True:
l,data = inp.read()
if l:
# Avoid exceptions, audioop.error: not a whole number of frames
try:
value = audioop.max(data, 2)
except:
continue
else:
# Move from Right to Left
screen = screen[1:] + [black]
# Calcule sound level
sound_level = ((granularity * value)/max_audioop)
for x in range(7,-1,-1):
color = max(min(granularity,sound_level-((7-x)*power)),0)
screen[(x*8)+7] = gradient[color]
sense.set_pixels(screen)
sleep(.001)
except (KeyboardInterrupt, SystemExit):
# Clean screen
screen = [black] *( 8 * 8 )
sense.set_pixels(screen)