-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbtest.py
73 lines (61 loc) · 1.65 KB
/
rgbtest.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
import alsaaudio as aa
import wave
from struct import unpack
import numpy as np
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics
wavfile = wave.open('test.wav')
sample_rate = wavfile.getframerate()
no_channels = wavfile.getnchannels()
options = RGBMatrixOptions()
options.rows = 64
options.cols = 64
options.chain_length = 1
options.parallel = 1
options.hardware_mapping = 'adafruit-hat'
Dmatrix = RGBMatrix(options=options)
Dmatrix.Clear()
chunk = 4096
matrix = [0] * 64
weighting = [0]*64
weighting[0] = 2
power = []
for i in range(63):
if i%2==0:
weighting[i+1] = 2**(2+i)
else:
weighting[i+1] = 2**(1+i)
output = aa.PCM(aa.PCM_PLAYBACK,aa.PCM_NORMAL)
output.setchannels(2)
output.setrate(sample_rate)
output.setformat(aa.PCM_FORMAT_S16_LE)
output.setperiodsize(chunk)
def piff(val):
return int(2*chunk*val/sample_rate)
def calculate_levels(data, chunk, sample_rate):
data = unpack("%dh"%(len(data)/2),data)
data = np.array(data,dtype='h')
fourier = np.fft.rfft(data)
fourier = np.delete(fourier,len(fourier)-1)
power = np.log10(np.abs(fourier))**2
power = np.reshape(power,(64,chunk/64))
matrix = np.int_(np.average(power,axis=1))
return matrix
data = wavfile.readframes(chunk)
while data != '':
output.write(data)
matrix = calculate_levels(data,chunk,sample_rate)
#print(matrix)
Dmatrix.Clear()
for y in range(0,64):
for x in range(matrix[y]):
x *=2
if x < 32:
Dmatrix.SetPixel(y,x,0,200,0)
Dmatrix.SetPixel(y,x-1,0,200,0)
elif x < 50:
Dmatrix.SetPixel(y,x,150,150,0)
Dmatrix.SetPixel(y,x-1,150,150,0)
else:
Dmatrix.SetPixel(y,x,200,0,0)
Dmatrix.SetPixel(y,x-1,200,0,0)
data = wavfile.readframes(chunk)