-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalsaold.py
153 lines (140 loc) · 3.45 KB
/
alsaold.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import alsaaudio as aa
from os import listdir
import decoder
import numpy as np
from scipy.fftpack import rfft, irfft
import sys
import time
import thread
from bibliopixel.led import *
from bibliopixel.drivers.APA102 import *
driver = DriverAPA102(60, use_py_spi = True, c_order = ChannelOrder.BGR, SPISpeed = 16)
led = LEDStrip(driver, masterBrightness = 128)
def show(size, color):
while color > 1.0:
color -= 1.0
if size < .08:
size = .08
'''
buffer = []
dead = []
tuple = (0, 0, 255)
count = 0
lit = size * 30
while count < lit:
buffer += tuple
count += 1
while count < 30:
dead += (0,0,0)
count += 1
buffer = dead + buffer + buffer + dead
led.setBuffer(buffer)
'''
filled = 60 * size
dead = 60 - filled
led.fillHSV((0,0,0), 0, int(dead / 2))
led.fillHSV((int(color * 255), 255, 255), int(dead / 2), int(60 - dead / 2))
led.fillHSV((0,0,0), int(60 - dead / 2), 60)
led.update()
queue = []
data = ""
chunk = 1024
song = ""
curSong = ""
skip = False
pause = False
startHue = 0.5
prev = 0
history = [0.0, 1.0]
prevHue = startHue
Hue = 0.0
def queueThread():
global queue
global skip
global pause
while True:
command = raw_input("Enter a command (skip, add <songname>, current, remove): ")
if command.startswith('s'): #skip cursong
skip = True
elif command.startswith('a'): #add song
queue.append(command.split()[1])
elif command.startswith('c'): #view current queue
print ""
print "Current song: ", curSong
print "Queue: "
for i in range(0, len(queue)):
print (i + 1), queue[i]
print ""
elif command.startswith('r'): #remove song
queue.pop(int(command.split()[1]) - 1)
elif command.startswith('p'): #toggle play or pause
pause = not pause
elif command.startswith('n'): #play this song next
queue.insert(0, command.split()[1])
def songThread():
global data
global queue
global song
global skip
global curSong
while True:
stream = aa.PCM(aa.PCM_PLAYBACK, aa.PCM_NORMAL)
while len(queue) == 0:
time.sleep(1)
curSong = queue.pop(0)
song = decoder.open(curSong)
stream.setperiodsize(chunk)
data = song.readframes(chunk)
while data != '':
while pause:
time.sleep(.1)
if skip:
skip = False
break
stream.write(data)
data = song.readframes(chunk)
print "escaped songThread loop"
#main
if len(sys.argv) > 1:
queue.append(sys.argv[1])
queue.extend("music/" + f for f in listdir("./music/"))
thread.start_new_thread(queueThread, ())
thread.start_new_thread(songThread, ())
time.sleep(.1)
while True:
Hue += .01
raw = np.fromstring(data, dtype = np.int16)
if len(raw) == 0:
continue
left = np.divide(raw[1::2], 2)
right = np.divide(raw[0::2], 2)
both = np.add(left, right)
if song.getnchannels() == 1:
fft = rfft(raw)
else:
fft = rfft(both)
hue = 1.0 * np.argmax(fft) / len(fft)
if np.argmax(fft) != 0:
hue = 1.0 * np.log10(np.argmax(fft)) / np.log10(len(fft))
else:
hue = 0.0
hue += startHue
hue = prevHue + (hue - prevHue) * np.absolute(hue - prevHue)
prevHue = hue
'''
print "inverse fft"
for i in range(1, 4):
fft[i] *= .1
clean = irfft(fft)
'''
cur = 20 * (np.average(np.absolute(both)))
if cur < 0:
cur = 0
history.append(cur)
if len(history) > 44100 / chunk:
history.pop(0)
showVal = 1.0 * (cur - min(history)) / (max(history) - min(history)) if max(history) != min(history) else 0.0
prev = prev + (showVal - prev) * .4 if showVal > prev else prev - (prev - showVal) * .1
show(prev, hue)
time.sleep(chunk / 44100.0)
print "escaped analysis loop"