-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundlight_Color.py
184 lines (153 loc) · 5.26 KB
/
Soundlight_Color.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Python 2.7 code to analyze sound and interface with Arduino
import pyaudio # from http://people.csail.mit.edu/hubert/pyaudio/
import numpy # from http://numpy.scipy.org/
from pylab import *
import audioop
import sys
import math
import struct
import ArduinoLED
from datetime import datetime
'''
Sources
http://www.swharden.com/blog/2010-03-05-realtime-fft-graph-of-audio-wav-file-or-microphone-input-with-python-scipy-and-wckgraph/
http://macdevcenter.com/pub/a/python/2001/01/31/numerically.html?page=2
http://julip.co/2012/05/arduino-python-soundlight-spectrum/
'''
MAX = 0
def list_devices():
# List all audio input devices
p = pyaudio.PyAudio()
i = 0
n = p.get_device_count()
while i < n:
dev = p.get_device_info_by_index(i)
if dev['maxInputChannels'] > 0:
print str(i)+'. '+dev['name']
i += 1
def arduino_soundlight(dev):
#Set Graphing
graph = 2 #0, No graph. 1, Color. 2, Waveform
if(graph==1):
ch_v=14
ion()
if(graph==2):
ch_v=12
ion()
if(graph==0):
ch_v=11
chunk = 2**ch_v # Change if too fast/slow, never less than 2**11
scale = 30 # Change if too dim/bright
exponent = 6 # Change if too little/too much difference between loud and quiet sounds
samplerate = 44100
divs = 15 #Defintion of wave form. Higher number = more definition
alpha = 0.5 #Exponential Average, Levels Change Rate
beta = 0.75 #Exponential Average, 'Value' Change Rate
hueThresh = 0.5e13 #Difference needed in Low frequency value to count as a beat
hueJump = 0.2 #Amount of colors to "skip" when beat detected
colorWrap = 35 #Approximately the time (s) to come back to the same color - assuming no Beats
h=0
s=1
v=0
max_lvls=1
device = dev
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.paInt16,
channels = 1,
rate = 44100,
input = True,
frames_per_buffer = chunk,
input_device_index = device)
print "Starting, use Ctrl+C to stop"
t=1
prevLowF =0
try:
while True:
t1 = datetime.now()
data = stream.read(chunk)
# Do FFT
levels = calculate_levels(data, chunk, samplerate, divs)
#Process and filter levels to obtain modified waveform
idx=0
plotLev = zeros(divs)
for level in levels:
level = level**exponent
plotLev[idx] = level*alpha+(1-alpha)*plotLev[idx]
idx = idx+1
print "------"
currLowF = (plotLev[0])
if((currLowF-prevLowF)>hueThresh):
h =h + hueJump
print "BEAT"
print
print
print
prevLowF = currLowF
t2 = datetime.now()
delta = t2-t1
Hue_Fraction = (delta.microseconds/1e6)/colorWrap
#Obtain value for 'Value' from levls
avg_lvls = sum(levels)/len(levels)
if(avg_lvls>max_lvls):
max_lvls = avg_lvls
temp_v = avg_lvls*(1.0/max_lvls)
if (temp_v<0):
temp_v=0
v = temp_v*beta+v*(1-beta)
v=v**2
if (v>1):
v=1
elif (v<0.2):
v=0.2
h = h+Hue_Fraction
if(h>1):
h=0
ArduinoLED.HSVtoSerial((h,s,v**2)) #Convert RGB to string for Arduino
#Post Processing Steps
if (graph==1): #Graph Color
rgb_tuple = ArduinoLED.convRGB((h,s,v**2))
gca().add_patch(Rectangle((0,0),1,1, color=rgb_tuple))
draw()
if (graph==2): #Graph Waves
if(t==1):
hl, = plot(plotLev)
axis([0, divs, 0, 200**exponent/4])
draw()
t = t+1
else:
hl.set_ydata(plotLev)
draw()
except KeyboardInterrupt:
pass
finally:
print "\nStopping"
stream.close()
p.terminate()
def calculate_levels(data, chunk, samplerate, divs):
# Use FFT to calculate volume for each frequency
global MAX
# Convert raw sound data to Numpy array
fmt = "%dH"%(len(data)/2)
data2 = struct.unpack(fmt, data)
data2 = numpy.array(data2, dtype='h')
# Apply FFT
fourier = numpy.fft.fft(data2)
ffty = numpy.abs(fourier[0:len(fourier)/2])/1000
ffty1=ffty[:len(ffty)/2]
ffty2=ffty[len(ffty)/2::]+2
ffty2=ffty2[::-1]
ffty=ffty1+ffty2
ffty=numpy.log(ffty)-2
fourier = list(ffty)[4:-4]
fourier = fourier[:len(fourier)/2]
size = len(fourier)
levels = [sum(fourier[i:(i+size/divs)]) for i in xrange(0, size, size/divs)][:divs]
return levels
def SelectAudio():
print"Available Audio Devices:"
list_devices()
print
return raw_input("Select Audio Device:")
if __name__ == '__main__':
dev = int(SelectAudio())
arduino_soundlight(dev)