File: pykmanager.py

package info (click to toggle)
pykaraoke 0.7.5-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,608 kB
  • ctags: 1,168
  • sloc: python: 6,696; ansic: 577; makefile: 69; sh: 46
file content (634 lines) | stat: -rw-r--r-- 25,147 bytes parent folder | download | duplicates (5)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#******************************************************************************
#****                                                                      ****
#**** Copyright (C) 2010  Kelvin Lawson (kelvinl@users.sourceforge.net)    ****
#**** Copyright (C) 2010  PyKaraoke Development Team                       ****
#****                                                                      ****
#**** 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                                          ****
#******************************************************************************

from pykconstants import *
from pykenv import env
import pykversion
import pygame
import os
import sys

# Python 2.3 and newer ship with optparse; older Python releases need "Optik"
# installed (optik.sourceforge.net)
try:
    import optparse
except:
    import Optik as optparse

if env == ENV_GP2X:
    import _cpuctrl as cpuctrl

class pykManager:

    """ There is only one instance of this class in existence during
    program execution, and it is never destructed until program
    termination.  This class manages the pygame interface, keeping
    interfaces open or closed as necessary; it also provides callbacks
    into handling pygame events. """

    def __init__(self):
        self.initialized = False
        self.player = None
        self.options = None
        self.display = None
        self.surface = None
        self.audioProps = None

        self.displaySize = None
        self.displayFlags = 0
        self.displayDepth = 0
        self.cpuSpeed = None

        # Find the correct font path. If fully installed on Linux this
        # will be sys.prefix/share/pykaraoke/fonts. Otherwise look for
        # it in the current directory.
        if (os.path.isfile("fonts/DejaVuSans.ttf")):
            self.FontPath = "fonts"
            self.IconPath = "icons"
        else:
            self.FontPath = os.path.join(sys.prefix, "share/pykaraoke/fonts")
            self.IconPath = os.path.join(sys.prefix, "share/pykaraoke/icons")

        if env == ENV_GP2X:
            speed = cpuctrl.get_FCLK()
            print "Initial CPU speed is %s" % (speed)
            x, y, tvout = cpuctrl.get_screen_info()
            print "Initial screen size is %s, %s" % (x, y)
            if tvout:
                print "TV-Out mode is enabled."

        # This factor may be changed by the user to make text bigger
        # or smaller on those players that support it.
        self.fontScale = None

    def setCpuSpeed(self, activityName):
        """ Sets the CPU speed appropriately according to what the
        current activity is.  At the moment, this is used only for the
        GP2X. """

        if self.cpuSpeed == activityName:
            # No change.
            return
        self.cpuSpeed = activityName

        # The activityName directly hooks into a CPU speed indicated
        # in the user settings.

        attr = 'CPUSpeed_%s' % (activityName)
        speed = getattr(self.settings, attr, None)
        if speed is not None:
            self.OpenCPUControl()
            if env == ENV_GP2X:
                cpuctrl.set_FCLK(speed)
                pass

    def VolumeUp(self):
        try:
            volume = pygame.mixer.music.get_volume()
        except pygame.error:
            print "Failed to raise music volume!"
            return
        volume = min(volume + 0.1, 1.0)

        pygame.mixer.music.set_volume(volume)

    def VolumeDown(self):
        try:
            volume = pygame.mixer.music.get_volume()
        except pygame.error:
            print "Failed to lower music volume!"
            return
        volume = max(volume - 0.1, 0.0)

        pygame.mixer.music.set_volume(volume)

    def GetVolume(self):
        """ Gives the current volume level. """
        if vars().has_key('music'):
            return pygame.mixer.music.get_volume()
        else:
            return 0.50 # 75% is the industry recommended maximum value

    def SetVolume(self, volume):
        """ Sets the volume of the music playback. """
        volume = min(volume, 1.0)
        volume = max(volume, 0.0)
        pygame.mixer.music.set_volume(volume)

    def GetFontScale(self):
        """ Returns the current font scale. """
        if self.fontScale == None:
            self.fontScale = self.options.font_scale
        return self.fontScale

    def ZoomFont(self, factor):
        """ Zooms the font scale by the indicated factor.  This is
        treated like a resize event, even though the window is not
        changing size; player.doResize() will be called. """
        self.GetFontScale()
        self.fontScale *= factor
        if self.player:
            self.player.doResize(self.displaySize)

    def InitPlayer(self, player):

        """ A pykPlayer will call this when it constructs.  This
        registers the player with the pykManager, so that it will get
        callbacks and control of the display.  This call also ensures
        that pygame has been initialized. """

        if self.player:
            self.player.shutdown()
            self.player = None

        # Ensure we have been initialized.
        if not self.initialized:
            self.pygame_init()

        self.player = player
        self.player.State = STATE_NOT_PLAYING

        if self.display != None and self.displayTitle == None:
            try:
                pygame.display.set_caption(player.WindowTitle)
            except UnicodeError:
                pygame.display.set_caption(player.WindowTitle.encode('UTF-8', 'replace'))


    def OpenDisplay(self, displaySize = None, flags = None, depth = None):
        """ Use this method to open a pygame display or set the
        display to a specific mode. """

        self.getDisplayDefaults()

        if displaySize == None:
            displaySize = self.displaySize
        if flags == None:
            flags = self.displayFlags
        if depth == None:
            depth = self.displayDepth

        if self.options.dump:
            # We're just capturing frames offscreen.  In that case,
            # just open an offscreen buffer as the "display".
            self.display = None
            self.surface = pygame.Surface(self.displaySize)
            self.mouseVisible = False
            self.displaySize = self.surface.get_size()
            self.displayFlags = self.surface.get_flags()
            self.displayDepth = self.surface.get_bitsize()
        else:
            # Open the onscreen display normally.
            pygame.display.init()

            self.mouseVisible = not (env == ENV_GP2X or self.options.hide_mouse or (self.displayFlags & pygame.FULLSCREEN))
            pygame.mouse.set_visible(self.mouseVisible)

            if self.displayTitle != None:
                pygame.display.set_caption(self.displayTitle)
            elif self.player != None:
                try:
                    pygame.display.set_caption(self.player.WindowTitle)
                except UnicodeError:
                    pygame.display.set_caption(self.player.WindowTitle.encode('UTF-8', 'replace'))

            if self.display == None or \
               (self.displaySize, self.displayFlags, self.displayDepth) != (displaySize, flags, depth):
                self.display = pygame.display.set_mode(displaySize, flags, depth)
                self.displaySize = self.display.get_size()
                self.displayFlags = flags
                self.displayDepth = depth

            self.surface = self.display

        self.displayTime = pygame.time.get_ticks()

    def Flip(self):
        """ Call this method to make the displayed frame visible. """
        if self.display:
            pygame.display.flip()

    def CloseDisplay(self):
        """ Use this method to close the pygame window if it has been
        opened. """

        if self.display:
            pygame.display.quit()
            pygame.display.init()
            self.display = None

        self.surface = None

    def OpenAudio(self, frequency = None, size = None, channels = None):
        """ Use this method to initialize or change the audio
        parameters."""

        # We shouldn't mess with the CPU control while the audio is
        # open.
        self.CloseCPUControl()

        if frequency == None:
            frequency = self.settings.SampleRate

        if size == None:
            size = -16

        if channels == None:
            channels = self.settings.NumChannels

        bufferMs = self.settings.BufferMs

        # Compute the number of samples that would fill the indicated
        # buffer time.
        bufferSamples = bufferMs * (frequency * channels) / 1000

        # This needs to be a power of 2, so find the first power of 2
        # larger, up to 2^15.
        p = 1
        while p < bufferSamples and p < 32768:
            p <<= 1
        # Now choose the power of 2 closest.

        if (abs(bufferSamples - (p >> 1)) < abs(p - bufferSamples)):
            bufferSamples = p >> 1
        else:
            bufferSamples = p

        audioProps = (frequency, size, channels, bufferSamples)
        if audioProps != self.audioProps:
            # If the audio properties have changed, we have to shut
            # down and re-start the audio subsystem.
            pygame.mixer.quit()
            pygame.mixer.init(*audioProps)
            self.audioProps = audioProps

    def CloseAudio(self):
        pygame.mixer.quit()
        self.audioProps = None

    def OpenCPUControl(self):
        self.CloseAudio()
        if env == ENV_GP2X:
            cpuctrl.init()

    def CloseCPUControl(self):
        if env == ENV_GP2X:
            cpuctrl.shutdown()

    def GetAudioBufferMS(self):
        """ Returns the number of milliseconds it will take to
        completely empty a full audio buffer with the current
        settings. """
        if self.audioProps:
            frequency, size, channels, bufferSamples = self.audioProps
            return bufferSamples * 1000 / (frequency * channels)
        return 0

    def Quit(self):
        if self.player:
            self.player.shutdown()
            self.player = None

        if not self.initialized:
            return
        self.initialized = False

        pygame.quit()

    def __errorCallback(self, message):
        self.songValid = False
        print message
    def __doneCallback(self):
        pass

    def ValidateDatabase(self, songDb):
        """ Validates all of the songs in the database, to ensure they
        are playable and contain lyrics. """

        self.CloseDisplay()
        invalidFile = open('invalid.txt', 'w')

        songDb.SelectSort('filename')
        for song in songDb.SongList[:1074]:
            self.songValid = True
            player = song.MakePlayer(songDb, self.__errorCallback, self.__doneCallback)
            if not player:
                self.songValid = False
            else:
                if not player.Validate():
                    self.songValid = False

            if self.songValid:
                print '%s ok' % (song.DisplayFilename)
            else:
                print '%s invalid' % (song.DisplayFilename)
                print >> invalidFile, '%s\t%s' % (song.Filepath, song.ZipStoredName)
                invalidFile.flush()

    def Poll(self):
        """ Your application must call this method from time to
        time--ideally, within a hundred milliseconds or so--to perform
        the next quantum of activity. Alternatively, if the
        application does not require any cycles, you may just call
        WaitForPlayer() instead. """

        if not self.initialized:
            self.pygame_init()

        self.handleEvents()

        if self.player:
            if self.player.State == STATE_CLOSED:
                self.player = None
            else:
                self.player.doStuff()

        # Wait a bit to save on wasteful CPU usage.
        pygame.time.wait(1)

    def WaitForPlayer(self):
        """ The interface may choose to call this method in lieu of
        repeatedly calling Poll().  It will block until the currently
        active player has finished, and then return. """

        while self.player and self.player.State != STATE_CLOSED:
            self.Poll()

    def SetupOptions(self, usage, songDb):
        """ Initialise and return optparse OptionParser object,
        suitable for parsing the command line options to this
        application.  This version of this method returns the options
        that are likely to be useful for any karaoke application. """

        version = "%prog " + pykversion.PYKARAOKE_VERSION_STRING

        settings = songDb.Settings

        parser = optparse.OptionParser(usage = usage, version = version,
                                       conflict_handler = "resolve")

        if env != ENV_OSX and env != ENV_GP2X:
            pos_x = None
            pos_y = None
            if settings.PlayerPosition:
                pos_x, pos_y = settings.PlayerPosition
            parser.add_option('-x', '--window-x', dest = 'pos_x', type = 'int', metavar='X',
                              help = 'position song window X pixels from the left edge of the screen', default = pos_x)
            parser.add_option('-y', '--window-y', dest = 'pos_y', type = 'int', metavar='Y',
                              help = 'position song window Y pixels from the top edge of the screen', default = pos_y)

        if env != ENV_GP2X:
            parser.add_option('-w', '--width', dest = 'size_x', type = 'int', metavar='X',
                              help = 'draw song window X pixels wide', default = settings.PlayerSize[0])
            parser.add_option('-h', '--height', dest = 'size_y', type = 'int', metavar='Y',
                              help = 'draw song window Y pixels high', default = settings.PlayerSize[1])
            parser.add_option('-t', '--title', dest = 'title', type = 'string', metavar='TITLE',
                              help = 'set song window title to TITLE', default = None)
            parser.add_option('-f', '--fullscreen', dest = 'fullscreen', action = 'store_true',
                              help = 'make song window fullscreen', default = settings.FullScreen)
            parser.add_option('', '--hide-mouse', dest = 'hide_mouse', action = 'store_true',
                              help = 'hide the mouse pointer', default = False)

        parser.add_option('-s', '--fps', dest = 'fps', metavar='N', type = 'int',
                          help = 'restrict visual updates to N frames per second',
                          default = 30)
        parser.add_option('-r', '--sample-rate', dest = 'sample_rate', type = 'int',
                          help = 'specify the audio sample rate.  Ideally, this should match the recording.  For MIDI files, higher is better but consumes more CPU.',
                          default = settings.SampleRate)
        parser.add_option('', '--num-channels', dest = 'num_channels', type = 'int',
                          help = 'specify the number of audio channels: 1 for mono, 2 for stereo.',
                          default = settings.NumChannels)
        parser.add_option('', '--font-scale', metavar='SCALE', dest = 'font_scale', type = 'float',
                          help = 'specify the font scale factor; small numbers (between 0 and 1) make text smaller so more fits on the screen, while large numbers (greater than 1) make text larger so less fits on the screen.',
                          default = 1)

        parser.add_option('', '--zoom', metavar='MODE', dest = 'zoom_mode', type = 'choice',
                          choices = settings.Zoom,
                          help = 'specify the way in which graphics are scaled to fit the window.  The choices are %s.' % (', '.join(map(lambda z: '"%s"' % z, settings.Zoom))),
                          default = settings.CdgZoom)

        parser.add_option('', '--buffer', dest = 'buffer', metavar = 'MS', type = 'int',
                          help = 'buffer audio by the indicated number of milliseconds',
                          default = settings.BufferMs)
        parser.add_option('-n', '--nomusic', dest = 'nomusic', action = 'store_true',
                          help = 'disable music playback, just display graphics', default = False)

        parser.add_option('', '--dump', dest = 'dump',
                          help = 'dump output as a sequence of frame images, for converting to video',
                          default = '')
        parser.add_option('', '--dump-fps', dest = 'dump_fps', type = 'float',
                          help = 'specify the number of frames per second of the sequence output by --dump',
                          default = 29.97)

        parser.add_option('', '--validate', dest = 'validate', action = 'store_true',
                          help = 'validate that all songs contain lyrics and are playable')

        return parser

    def ApplyOptions(self, songDb):
        """ Copies the user-specified command-line options in
        self.options to the settings in songDb.Settings. """

        self.settings = songDb.Settings

        self.settings.CdgZoom = self.options.zoom_mode

        if hasattr(self.options, 'fullscreen'):
            self.settings.FullScreen = self.options.fullscreen
            self.settings.PlayerSize = (self.options.size_x, self.options.size_y)
        if hasattr(self.options, 'pos_x') and \
           self.options.pos_x != None and self.options.pos_y != None:
            self.settings.PlayerPosition = (self.options.pos_x, self.options.pos_y)

        self.settings.NumChannels = self.options.num_channels
        self.settings.SampleRate = self.options.sample_rate
        self.settings.BufferMs = self.options.buffer

    def WordWrapText(self, text, font, maxWidth):
        """Folds the line (or lines) of text into as many lines as
        necessary to fit within the indicated width (when rendered by
        the given font), word-wrapping at spaces.  Returns a list of
        strings, one string for each separate line."""


        lines = []

        for line in text.split('\n'):
            fold = self.FindFoldPoint(line, font, maxWidth)
            while line:
                lines.append(line[:fold])
                line = line[fold:]
                fold = self.FindFoldPoint(line, font, maxWidth)

        return lines

    def FindFoldPoint(self, line, font, maxWidth):
        """Returns the index of the character within line which should
        begin the next line: the first non-space before maxWidth."""

        if maxWidth <= 0 or line == '':
            return len(line)

        fold = len(line.rstrip())
        width, height = font.size(line[:fold])
        while fold > 0 and width > maxWidth:
            sp = line[:fold].rfind(' ')
            if sp == -1:
                fold -= 1
            else:
                fold = sp
            width, height = font.size(line[:fold])

        while fold < len(line) and line[fold] == ' ':
            fold += 1

        if fold == 0:
            # Couldn't even get one character in.  Put it in anyway.
            fold = 1

        if line[:fold].strip() == '':
            # Oops, nothing but whitespace in front of the fold.  Try
            # again without the whitespace.
            ws = line[:fold]
            line = line[fold:]
            wsWidth, height = font.size(ws)
            return self.FindFoldPoint(line, font, maxWidth - wsWidth) + len(ws)

        return fold


    # The remaining methods are internal.

    def handleEvents(self):
        """ Handles the events returned from pygame. """

        if self.display:
            # check for Pygame events
            for event in pygame.event.get():
                self.handleEvent(event)


    def handleEvent(self, event):
        # Only handle resize events 250ms after opening the
        # window. This is to handle the bizarre problem of SDL making
        # the window small automatically if you set
        # SDL_VIDEO_WINDOW_POS and move the mouse around while the
        # window is opening. Give it some time to settle.
        player = self.player
        if event.type == pygame.VIDEORESIZE and pygame.time.get_ticks() - self.displayTime > 250:

            # Tell the player we are about to resize. This is required
            # for pympg.
            if player:
                player.doResizeBegin()

            # Do the resize
            self.displaySize = event.size
            self.settings.PlayerSize = tuple(self.displaySize)
            pygame.display.set_mode(event.size, self.displayFlags, self.displayDepth)
            # Call any player-specific resize
            if player:
                player.doResize(event.size)

            # Tell the player we have finished resizing
            if player:
                player.doResizeEnd()

        elif env == ENV_GP2X and event.type == pygame.JOYBUTTONDOWN:
            if event.button == GP2X_BUTTON_VOLUP:
                self.VolumeUp()
            elif event.button == GP2X_BUTTON_VOLDOWN:
                self.VolumeDown()

        if player:
            player.handleEvent(event)

    def pygame_init(self):
        """ This method is called only once, the first time an
        application requests a pygame window. """

        pygame.init()

        if env == ENV_GP2X:
            num_joysticks = pygame.joystick.get_count()
            if num_joysticks > 0:
                stick = pygame.joystick.Joystick(0)
                stick.init() # now we will receive events for the GP2x joystick and buttons

        self.initialized = True

    def getDisplayDefaults(self):
        if env == ENV_GP2X:
            # The GP2x has no control over its window size or
            # placement.  You'll get fullscreen and like it.

            # Unfortunately, it appears that pygame--or maybe our SDL,
            # even though we'd compiled with paeryn's HW SDL--doesn't
            # allow us to open a TV-size window, so we have to settle
            # for the standard (320, 240) size and let the hardware
            # zooming scale it for TV out.
            self.displaySize = (320, 240)
            self.displayFlags = pygame.HWSURFACE | pygame.FULLSCREEN
            self.displayDepth = 0
            self.displayTitle = None
            self.mouseVisible = False
            return

        # Fix the position at top-left of window. Note when doing
        # this, if the mouse was moving around as the window opened,
        # it made the window tiny.  Have stopped doing anything for
        # resize events until 1sec into the song to work around
        # this. Note there appears to be no way to find out the
        # current window position, in order to bring up the next
        # window in the same place. Things seem to be different in
        # development versions of pygame-1.7 - it appears to remember
        # the position, and it is the only version for which fixing
        # the position works on MS Windows.

        # Don't set the environment variable on OSX.
        if env != ENV_OSX:
            if self.settings.PlayerPosition:
                x, y = self.settings.PlayerPosition
                os.environ['SDL_VIDEO_WINDOW_POS'] = "%s,%s" % (x, y)

        w, h = self.settings.PlayerSize
        self.displaySize = (w, h)

        self.displayFlags = pygame.RESIZABLE
        if self.settings.DoubleBuf:
            self.displayFlags |= pygame.DOUBLEBUF
        if self.settings.HardwareSurface:
            self.displayFlags |= pygame.HWSURFACE
        
        if self.settings.NoFrame:
            self.displayFlags |= pygame.NOFRAME
        if self.settings.FullScreen:
            self.displayFlags |= pygame.FULLSCREEN

        self.displayDepth = 0
        self.displayTitle = self.options.title

        self.mouseVisible = not (env == ENV_GP2X or self.options.hide_mouse or (self.displayFlags & pygame.FULLSCREEN))


# Now instantiate a global pykManager object.
manager = pykManager()