/
light_project.py
155 lines (128 loc) · 3.33 KB
/
light_project.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
import time
from datetime import datetime
import pyaudio
import wave
import threading
import signal
import sys
import os
import collections
from gracenote import MoodEvent
from mood import grace_to_hsb
from lifx import setBulbColor
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 256
INTERVAL = 1
WINDOW = 20
MIN_WINDOW = 5
CONSECUTIVE_ERROR_LIMIT = 150
FILENAME = "output.wav"
aok = True
audio = pyaudio.PyAudio()
NUM_FRAMES = RATE / CHUNK * WINDOW
MIN_NUM_FRAMES = RATE / CHUNK * MIN_WINDOW
frames = collections.deque([],NUM_FRAMES)
lastUpdateTimeStamp = datetime.now()
def export(dataList):
# start writing
f = wave.open(FILENAME, 'wb')
f.setnchannels(CHANNELS)
f.setsampwidth(audio.get_sample_size(FORMAT))
f.setframerate(RATE)
# write data to wave file
f.writeframes(b''.join(dataList))
# finish writing
f.close()
def playAudio(fn):
#open a wav format music
f = wave.open(fn,"rb")
#instantiate PyAudio
p = pyaudio.PyAudio()
#open stream
stream = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
#read data
data = f.readframes(CHUNK)
#paly stream
while data != '':
stream.write(data)
data = f.readframes(CHUNK)
#stop stream
stream.stop_stream()
stream.close()
#close PyAudio
p.terminate()
def gracenote_receive_thread(**kwargs):
me = kwargs['MoodEvent']
def update():
while aok:
dataList = list(frames)
# Don't bother with GraceNote unless there are
# MIN_WINDOW seconds or more of audio
if len(dataList) < MIN_NUM_FRAMES:
time.sleep(0.1)
export(dataList)
# do GraceNote and Lightbulb stuff
me = MoodEvent(FILENAME)
me.submit()
global lastUpdateTimeStamp
timeStamp = datetime.now()
try :
me.retrieve_results()
if timeStamp < lastUpdateTimeStamp:
continue
except ValueError as e:
print e, 'with', me.filename
continue
lastUpdateTimeStamp = timeStamp
print me.get_bpm(), me.get_mood_label()
hsb = grace_to_hsb(me.get_mood_label())
setBulbColor(*hsb)
# play audio for testing
# playAudio(FILENAME)
def doInExternalThread(func, **kwargs):
t = threading.Thread(target = func, kwargs = kwargs)
t.daemon = True
t.start()
def main():
def sigintHandler(signal, frame):
global aok
aok = False
signal.signal(signal.SIGINT, sigintHandler)
# print out input devices and corresponding indeces
for i in range(audio.get_device_count()):
dev = audio.get_device_info_by_index(i)
print((i,dev['name'],dev['maxInputChannels']))
# start Recording
stream = audio.open(format=FORMAT, rate=RATE, input=True,
channels=CHANNELS,
frames_per_buffer=CHUNK)
doInExternalThread(update)
consecutiveErrors = 0
while aok:
try:
for i in range(0, int(RATE / CHUNK * INTERVAL)):
data = stream.read(CHUNK)
frames.append(data)
consecutiveErrors = 0
except IOError:
consecutiveErrors += 1
if consecutiveErrors > CONSECUTIVE_ERROR_LIMIT:
print 'Too many consecutive errors. Exiting program.'
break
print 'Encountered error while recording. Moving on and trying again.'
continue
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
# remove file we were working with
os.remove(FILENAME)
# exit
sys.exit(0)
if __name__ == "__main__":
main()