-
Notifications
You must be signed in to change notification settings - Fork 0
/
pykplayer.py
292 lines (236 loc) · 10.2 KB
/
pykplayer.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
# Copyright (C) 2007 Kelvin Lawson ([email protected])
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""This module defines the class pykPlayer, which is a base class used
by the modules pykar.py, pycdg.py, and pympg.py. This collects
together some common interfaces used by these different
implementations for different types of Karaoke files."""
from mock import Mock
from pykconstants import *
#from pykmanager import manager
#from pykenv import env
import pygame
import sys
import types
import os
manager = Mock()
class pykPlayer:
def __init__(
self,
songfile,
errorNotifyCallback = None,
doneCallback = None,
windowTitle = None
):
"""The first parameter, song, may be either a pykdb.SongStruct
instance, or it may be a filename. """
# Set the global command-line options if they have not already
# been set.
if manager.options == None:
parser = self.SetupOptions()
(manager.options, args) = parser.parse_args()
manager.ApplyOptions(self.songDb)
if song is None:
if (len(args) != 1):
parser.print_help()
sys.exit(2)
song = args[0]
# Unfortunately, we can't capture sound when dumping. There
# are two reasons for this. (1) pymedia doesn't currently
# support multiplexing audio with a video stream, so when
# you're dumping an mpeg file, it has to be video-only. (2)
# pygame doesn't provide a way for us to programmatically
# convert a midi file to sound samples anyway--all you can do
# with a midi file is route it through the speakers.
# So, for these reasons, we always just disable sound when
# dumping images or movies.
# Store the parameters
self.songfile = songfile
self.WindowTitle = windowTitle
# And look up the actual files corresponding to this SongStruct.
if windowTitle is None:
self.WindowTitle = songfile
# Caller can register a callback by which we
# print out error information, use stdout if none registered
if errorNotifyCallback:
self.ErrorNotifyCallback = errorNotifyCallback
else:
self.ErrorNotifyCallback = self.__defaultErrorPrint
# Caller can register a callback by which we
# let them know when the song is finished
if doneCallback:
self.SongFinishedCallback = doneCallback
else:
self.SongFinishedCallback = None
self.State = STATE_INIT
self.InternalOffsetTime = 0
# These values are used to keep track of the current position
# through the song based on pygame's get_ticks() interface.
# It's used only when get_pos() cannot be used or is
# unreliable for some reason.
self.PlayTime = 0
self.PlayStartTime = 0
self.PlayFrame = 0
# self.PlayStartTime is valid while State == STATE_PLAYING; it
# indicates the get_ticks() value at which the song started
# (adjusted for any pause intervals that occurred during
# play). self.PlayTime is valid while State != STATE_PLAYING;
# it indicates the total number of ticks (milliseconds) that
# have elapsed in the song so far.
# self.PlayFrame starts at 0 and increments once for each
# frame. It's not very meaningful, except in STATE_CAPTURING
# mode.
# Keep track of the set of modifier buttons that are held
# down. This is currently used only for the GP2X interface.
self.ShoulderLHeld = False
self.ShoulderRHeld = False
# Set this true if the player can zoom font sizes.
self.SupportsFontZoom = False
# The following methods are part of the public API and intended to
# be exported from this class.
def Validate(self):
""" Returns True if the karaoke file appears to be playable
and contains lyrics, or False otherwise. """
return self.doValidate()
def Play(self):
self.doPlay()
self.PlayStartTime = pygame.time.get_ticks()
self.State = STATE_PLAYING
# Pause the song - Use Pause() again to unpause
def Pause(self):
if self.State == STATE_PLAYING:
self.doPause()
self.PlayTime = pygame.time.get_ticks() - self.PlayStartTime
self.State = STATE_PAUSED
elif self.State == STATE_PAUSED:
self.doUnpause()
self.PlayStartTime = pygame.time.get_ticks() - self.PlayTime
self.State = STATE_PLAYING
# Close the whole thing down
def Close(self):
self.State = STATE_CLOSING
# you must call Play() to restart. Blocks until pygame is initialised
def Rewind(self):
self.doRewind()
self.PlayTime = 0
self.PlayStartTime = 0
self.PlayFrame = 0
self.State = STATE_NOT_PLAYING
# Stop the song and go back to the start. As you would
# expect Stop to do on a CD player. Play() restarts from
# the beginning
def Stop(self):
self.Rewind()
# Get the song length (in seconds)
def GetLength(self):
ErrorString = "GetLength() not supported"
self.ErrorNotifyCallback (ErrorString)
return None
# Get the current time (in milliseconds).
def GetPos(self):
if self.State == STATE_PLAYING:
return pygame.time.get_ticks() - self.PlayStartTime
else:
return self.PlayTime
def SetupOptions(self, usage = None):
""" Initialise and return optparse OptionParser object,
suitable for parsing the command line options to this
application. """
if usage == None:
usage = "%prog [options] <Karaoke file>"
return manager.SetupOptions(usage, self.songDb)
def doValidate(self):
return True
def doPlay(self):
pass
def doPause(self):
pass
def doUnpause(self):
pass
def doRewind(self):
pass
def doStuff(self):
# Override this in a derived class to do some useful per-frame
# activity.
# Common handling code for a close request or if the
# pygame window was quit
if self.State == STATE_CLOSING:
self.display.fill((0,0,0))
pygame.display.flip()
self.shutdown()
elif self.State == STATE_CAPTURING:
# We are capturing a video file.
self.doFrameDump()
# Set the frame time for the next frame.
self.PlayTime = 1000.0 * self.PlayFrame / self.dumpFrameRate
self.PlayFrame += 1
def doResize(self, newSize):
# This will be called internally whenever the window is
# resized for any reason, either due to an application resize
# request being processed, or due to the user dragging the
# window handles.
pass
def doResizeBegin(self):
# This will be called internally before the screen is resized
# by pykmanager and doResize() is called. Not all players need
# to do anything here.
pass
def doResizeEnd(self):
# This will be called internally after the screen is resized
# by pykmanager and doResize() is called. Not all players need
# to do anything here.
pass
def handleEvent(self, event):
if event.type == pygame.USEREVENT \
or pygame.mixer.music.get_busy() == False:
print "Done playing, exiting..."
self.Close()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.Close()
elif event.key == pygame.K_PAUSE:
self.Pause()
# Use control-left/right arrow to offset the current
# graphics time by 1/4 sec. Use control-down arrow to
# restore them to sync.
elif self.State == STATE_PLAYING and event.key == pygame.K_RIGHT and event.mod & (pygame.KMOD_LCTRL | pygame.KMOD_RCTRL):
manager.settings.SyncDelayMs += 250
print "sync %s" % manager.settings.SyncDelayMs
elif self.State == STATE_PLAYING and event.key == pygame.K_LEFT and event.mod & (pygame.KMOD_LCTRL | pygame.KMOD_RCTRL):
manager.settings.SyncDelayMs -= 250
print "sync %s" % manager.settings.SyncDelayMs
elif self.State == STATE_PLAYING and event.key == pygame.K_DOWN and event.mod & (pygame.KMOD_LCTRL | pygame.KMOD_RCTRL):
manager.settings.SyncDelayMs = 0
print "sync %s" % manager.settings.SyncDelayMs
if self.SupportsFontZoom:
if event.key == pygame.K_PLUS or event.key == pygame.K_EQUALS or \
event.key == pygame.K_KP_PLUS:
manager.ZoomFont(1.0/0.9)
elif event.key == pygame.K_MINUS or event.key == pygame.K_UNDERSCORE or \
event.key == pygame.K_KP_MINUS:
manager.ZoomFont(0.9)
elif event.type == pygame.QUIT:
self.Close()
def shutdown(self):
# This will be called by the pykManager to shut down the thing
# immediately.
# If the caller gave us a callback, let them know we're finished
if self.State != STATE_CLOSED:
self.State = STATE_CLOSED
if self.SongFinishedCallback != None:
self.SongFinishedCallback()
def __defaultErrorPrint(self, ErrorString):
print (ErrorString)