-
Notifications
You must be signed in to change notification settings - Fork 2
/
pygame_output.py
73 lines (50 loc) · 1.96 KB
/
pygame_output.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
import pygame
import pygame.event
import pygame.mixer
import pygame.locals
import ansi
import mainLoop
DONE_PLAYING_CHUNK = pygame.locals.USEREVENT
class SampleOutput(object):
def __init__(self, sampleGen):
self.sampleGen = sampleGen
self.channel = pygame.mixer.Channel(0)
self.channel.set_endevent(DONE_PLAYING_CHUNK)
mainLoop.currentProcess.eventHandlers[DONE_PLAYING_CHUNK] = self.queueNextSound
def play(self):
self.queueNextSound() # Start playing the first chunk.
self.queueNextSound() # Queue the next chunk.
def queueNextSound(self, event=None):
ansi.stdout(
"{cursor.col.0}{clear.line.all}Current time:"
" {style.bold}{file.elapsedTime: >7.2f}{style.none} / {file.duration: <7.2f}",
file=self.sampleGen,
suppressNewline=True
)
chunk = pygame.mixer.Sound(buffer(self.sampleGen.nextChunk()))
chunk.play()
class PyGameProcess(mainLoop.QueueHandlerProcess):
def __init__(self, messageQueue, nice=None):
super(PyGameProcess, self).__init__(messageQueue, nice)
self.eventHandlers = {
pygame.locals.QUIT: self.quit,
}
pygame.init()
def unhandledEvent(self, event):
ansi.warn("Unhandled event! {!r}", event)
def processEvent(self, event):
if event.type == pygame.locals.NOEVENT:
return
handler = self.eventHandlers.get(event.type, self.unhandledEvent)
handler(event)
def eachLoop(self):
super(PyGameProcess, self).eachLoop()
#self.processEvent(pygame.event.wait())
self.afterEachCallback()
def afterEachCallback(self):
# Process waiting events before moving on to the next callback.
for event in pygame.event.get():
self.processEvent(event)
def onShutdown(self):
super(PyGameProcess, self).onShutdown()
pygame.quit()