forked from LSDG/music-lights
/
player.py
253 lines (176 loc) · 6.4 KB
/
player.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/env python
from __future__ import print_function
import atexit
import ConfigParser
import collections
from itertools import cycle
from multiprocessing import Process, Queue
import os
from Queue import Full as QueueFull
import sys
import audioread
import pygame
import pygame.locals
import ansi
DONE_PLAYING_CHUNK = pygame.locals.USEREVENT
STOP = 0
CONTINUE = 1
gcp = ConfigParser.SafeConfigParser()
gcp.read('config.ini')
bytes_per_frame_per_channel = int(gcp.get('main', 'bytes_per_frame_per_channel', 2))
lightProcessNice = int(gcp.get('main', 'lightProcessNice', 0))
soundProcessNice = int(gcp.get('main', 'soundProcessNice', 0))
files = sys.argv[1:]
queuedCallbacks = collections.deque()
eventHandlers = dict()
if soundProcessNice:
os.nice(soundProcessNice)
# Initialize pygame.mixer
pygame.mixer.pre_init(frequency=44100)
pygame.init()
class SampleGen(object):
def __init__(self, filenames, **kwargs):
self.framesPerChunk = kwargs.get('framesPerChunk', 4096)
self.onSongChanged = set()
self.onStopped = set()
self.onSample = set()
self.totalFramesRead = 0.0
self.filenameIter = iter(filenames)
self.currentData = None
self.sampleIter = None
def _loadNextFile(self):
self.currentFilename = next(self.filenameIter)
queuedCallbacks.extend(self.onSongChanged)
self.file = audioread.audio_open(self.currentFilename)
blockSize = self.framesPerChunk * self.file.channels * bytes_per_frame_per_channel
try:
# MAD (pymad)
self.sampleIter = self.file.read_blocks(blockSize)
except AttributeError:
try:
# FFMpeg (command line)
self.sampleIter = self.file.read_data(blockSize)
except AttributeError:
# gstreamer (pygst)
self.sampleIter = iter(self.file)
@property
def elapsedTime(self):
return self.totalFramesRead / self.file.samplerate
@property
def channels(self):
return self.file.channels
@property
def samplerate(self):
return self.file.samplerate
@property
def duration(self):
return self.file.duration
def nextChunk(self):
if self.sampleIter is None:
self._loadNextFile()
try:
self.currentData = next(self.sampleIter)
except (StopIteration, AttributeError):
# Either we haven't loaded a song yet, or the one we were playing ended. Load another.
self._loadNextFile()
self.currentData = next(self.sampleIter)
self.totalFramesRead += self.framesPerChunk
queuedCallbacks.extend(self.onSample)
return pygame.mixer.Sound(buffer(self.currentData))
def close(self):
# Stop stream.
self.file.close()
class SpectrumLightController(object):
def __init__(self, sampleGen):
self.sampleGen = sampleGen
sampleGen.onSample.add(self._onSample)
sampleGen.onSongChanged.add(self._onSongChanged)
atexit.register(self._onExit)
self.messageQueue = Queue()
self.process = Process(target=self.lightControllerProcess, args=(self.messageQueue, ))
self.process.start()
def _onSongChanged(self):
try:
self.messageQueue.put_nowait(('songChange', self.sampleGen.currentFilename))
except QueueFull:
ansi.error("Message queue to light process full! Continuing...")
def _onSample(self):
try:
self.messageQueue.put_nowait(('chunk', self.sampleGen.currentData))
except QueueFull:
ansi.error("Message queue to light process full! Continuing...")
def _onExit(self):
if self.process.is_alive():
try:
self.messageQueue.put(('end', ))
except QueueFull:
ansi.error("Message queue to light process full! Continuing...")
@staticmethod
def lightControllerProcess(messageQueue):
import lights
if lightProcessNice:
os.nice(lightProcessNice)
analyzer = lights.SpectrumAnalyzer(messageQueue, gcp)
analyzer.loop()
class SampleOutput(object):
def __init__(self, sampleGen):
self.sampleGen = sampleGen
self.channel = pygame.mixer.Channel(0)
self.channel.set_endevent(DONE_PLAYING_CHUNK)
self.queueNextSound() # Start playing the first chunk.
self.queueNextSound() # Queue the next chunk.
eventHandlers[DONE_PLAYING_CHUNK] = self.queueNextSound
def queueNextSound(self, event=None):
chunk = sampleGen.nextChunk()
chunk.play()
ansi.stdout(
"{cursor.col.0}{clear.line.all}Current time:"
" {style.bold}{file.elapsedTime: >7.2f}{style.none} / {file.duration: <7.2f}",
file=sampleGen,
suppressNewline=True
)
def displayFileStarted():
print()
ansi.stdout(
"Playing audio file: {style.fg.blue}{file.currentFilename}{style.none}\n"
"{style.bold.fg.black}channels:{style.none} {file.channels}"
" {style.bold.fg.black}sample rate:{style.none} {file.samplerate} Hz"
" {style.bold.fg.black}duration:{style.none} {file.duration} s",
file=sampleGen
)
sampleGen = SampleGen(cycle(files))
sampleGen.onSongChanged.add(displayFileStarted)
output = SampleOutput(sampleGen)
lights = SpectrumLightController(sampleGen)
class QuitApplication(Exception):
pass
def quitApp(event):
raise QuitApplication
eventHandlers[pygame.locals.QUIT] = quitApp
def unhandledEvent(event):
ansi.warn("Unhandled event! {!r}", event)
def processEvent(event):
if event.type == pygame.locals.NOEVENT:
return
handler = eventHandlers.get(event.type, unhandledEvent)
handler(event)
# Wait for stream to finish.
try:
while True:
processEvent(pygame.event.wait())
# Process any queued callbacks.
while queuedCallbacks:
callback = queuedCallbacks.popleft()
callback()
# Process waiting events before moving on to the next callback.
for event in pygame.event.get():
processEvent(event)
except QuitApplication:
print()
print("Exiting application.")
except KeyboardInterrupt:
print()
print("User interrupted; exiting.")
ansi.info("Shutting down...")
pygame.quit()
ansi.done()