Advertisement
Guest User

Untitled

a guest
Jul 17th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 53.96 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # pykar - KAR/MID Karaoke Player
  4. #
  5. # Copyright (C) 2010 Kelvin Lawson (kelvinl@users.sf.net)
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.  
  21.  
  22. # OVERVIEW
  23. #
  24. # pykar is a MIDI/KAR karaoke player built using python. It was written for
  25. # the PyKaraoke project but is in fact a general purpose KAR player that
  26. # could be used in other python projects requiring a KAR player.
  27. #
  28. # The player uses the pygame library (www.pygame.org), and can therefore
  29. # run on any operating system that runs pygame (currently Linux, Windows
  30. # and OSX).
  31. #
  32. # You can use this file as a standalone player, or together with
  33. # PyKaraoke. PyKaraoke provides a graphical user interface, playlists,
  34. # searchable song database etc.
  35. #
  36. # For those writing a media player or similar project who would like
  37. # KAR support, this module has been designed to be easily incorporated
  38. # into such projects and is released under the LGPL.
  39.  
  40.  
  41. # REQUIREMENTS
  42. #
  43. # pykar requires the following to be installed on your system:
  44. # . Python (www.python.org)
  45. # . Pygame (www.pygame.org)
  46.  
  47.  
  48. # LINUX REQUIREMENTS
  49. #
  50. # To play the MIDI songs on Linux, Timidity++ is also required:
  51. # . Timidity++ (timidity.sourceforge.net)
  52.  
  53. # OSX REQUIREMENTS
  54. #
  55. # On OSX, pygame will run MIDI natively by default, but if the GUS
  56. # patches are installed in /usr/local/lib/timidity, it will run MIDI
  57. # via Timidity instead, which appears to work better than the native
  58. # support, so we recommend this.
  59.  
  60. # USAGE INSTRUCTIONS
  61. #
  62. # To start the player, pass the KAR filename/path on the command line:
  63. #       python pykar.py /songs/theboxer.kar
  64. #
  65. # You can also incorporate a KAR player in your own projects by
  66. # importing this module. The class midPlayer is exported by the
  67. # module. You can import and start it as follows:
  68. #   import pykar
  69. #   player = pykar.midPlayer("/songs/theboxer.kar")
  70. #   player.Play()
  71. # If you do this, you must also arrange to call pycdg.manager.Poll()
  72. # from time to time, at least every 100 milliseconds or so, to allow
  73. # the player to do its work.
  74. #
  75. # The class also exports Close(), Pause(), Rewind(), GetPos().
  76. #
  77. # There are two optional parameters to the initialiser, errorNotifyCallback
  78. # and doneCallback:
  79. #
  80. # errorNotifyCallback, if provided, will be used to print out any error
  81. # messages (e.g. song file not found). This allows the module to fit
  82. # together well with GUI playlist managers by utilising the same GUI's
  83. # error popup window mechanism (or similar). If no callback is provided,
  84. # errors are printed to stdout. errorNotifyCallback should take one
  85. # parameter, the error string, e.g.:
  86. #   def errorPopup (ErrorString):
  87. #       msgBox (ErrorString)
  88. #
  89. # doneCallback can be used to register a callback so that the player
  90. # calls you back when the song is finished playing. The callback should
  91. # take no parameters, e.g.:
  92. #   def songFinishedCallback():
  93. #       msgBox ("Song is finished")
  94. #
  95. # To register callbacks, pass the functions in to the initialiser:
  96. #   midPlayer ("/songs/theboxer.kar", errorPopup, songFinishedCallback)
  97. # These parameters are optional and default to None.
  98. #
  99. # If the initialiser fails (e.g. the song file is not present), __init__
  100. # raises an exception.
  101.  
  102.  
  103. # IMPLEMENTATION DETAILS
  104. #
  105. # pykar is implemented as a handful of python modules. Pygame provides
  106. # support for playing MIDI files, so playing a MIDI song using Pygame
  107. # is very easy. However, in order to find the lyrics from the MIDI
  108. # file it was necessary to write a basic parser that understands the
  109. # MIDI file format. This opens the MIDI file and reads all tracks,
  110. # pulling out the lyric text and times. After this first parse of the
  111. # MIDI file, this module does not do any more MIDI decoding for
  112. # playing purposes - Pygame takes care of all actual music generation.
  113. #
  114. # Because a MIDI file might change tempo throughout the song, and
  115. # because tempo changes are technically allowed to appear within any
  116. # track and apply to all tracks, it is necessary to fully parse the
  117. # MIDI file before making observations of tempo, and thus before being
  118. # able to determine the precise time each lyric is to appear onscreen.
  119. # Thus, we initially save only the "click" count of each lyric's
  120. # appearance, and then once the file has been completely read, we can
  121. # convert clicks to milliseconds.
  122. #
  123. # There is an extra complication on Linux which is that the MIDI
  124. # support (provided by Timidity++, which is built into pygame) reports
  125. # the current song time using the first note being played as the
  126. # start. However on Windows the Pygame MIDI player returns the time
  127. # from the start of the actual song (even if there is no sound for a
  128. # few seconds). This meant that for Linux systems, it was necessary to
  129. # parse the whole MIDI file and calculate the time of the first note
  130. # from all tracks. This is then used as an offset in the calculation
  131. # of when to display the lyrics.
  132. #
  133. # Previous implementations ran the player within a thread; this is no
  134. # longer the case.  Instead, it is the caller's responsibility to call
  135. # pykar.manager.Poll() every once in a while to ensure that the player
  136. # gets enough CPU time to do its work.  Ideally, this should be at
  137. # least every 100 milliseconds or so to guarantee good video and audio
  138. # response time.
  139.  
  140.  
  141. from pykconstants import *
  142. from pykplayer import pykPlayer
  143. from pykenv import env
  144. from pykmanager import manager
  145. import pygame, sys, os, struct, cStringIO
  146.  
  147. # At what percentage of the screen height should we try to keep the
  148. # current singing cursor?  33% keeps it on the top third, 50% keeps it
  149. # centered.
  150. VIEW_PERCENT = 33
  151.  
  152. # Default font size at 480 pixels.
  153. FONT_SIZE = 40
  154.  
  155. # How much lead time before a new paragraph is scrolled up into view
  156. # (scrolling the old paragraph off), in milliseconds.  This only comes
  157. # into play when there is a large time gap between syllables.
  158. PARAGRAPH_LEAD_TIME = 5000
  159.  
  160. # text types.
  161. TEXT_LYRIC  = 0
  162. TEXT_INFO   = 1
  163. TEXT_TITLE  = 2
  164.  
  165. # Debug out MIDI messages as text
  166. debug = False
  167. #debug = True
  168.  
  169. class midiFile:
  170.     def __init__(self):
  171.         self.trackList = []         # List of TrackDesc track descriptors
  172.  
  173.         # Chosen lyric list from above.  It is converted by
  174.         # computeTiming() from a list of (clicks, text) into a list of
  175.         # (ms, text).
  176.         self.lyrics = []
  177.  
  178.         # self.text_encoding = "iso-8859-13"
  179.         self.text_encoding = ""      # The encoding of text in midi file
  180.  
  181.         self.ClickUnitsPerSMPTE = None
  182.         self.SMPTEFramesPerSec = None
  183.         self.ClickUnitsPerQuarter = None
  184.  
  185.         # The tempo of the song may change throughout, so we have to
  186.         # record the click at which each tempo change occurred, and
  187.         # the new tempo at that point.  Then, after we have read in
  188.         # all the tracks (and thus collected all the tempo changes),
  189.         # we can go back and apply this knowledge to the other tracks.
  190.         self.Tempo = [(0, 0)]
  191.  
  192.         self.Numerator = None               # Numerator
  193.         self.Denominator = None             # Denominator
  194.         self.ClocksPerMetronomeTick = None  # MIDI clocks per metronome tick
  195.         self.NotesPer24MIDIClocks = None    # 1/32 Notes per 24 MIDI clocks
  196.         self.earliestNoteMS = 0             # Start of earliest note in song
  197.         self.lastNoteMS = 0                 # End of latest note in song
  198.  
  199.  
  200. class TrackDesc:
  201.     def __init__(self, trackNum):
  202.         self.TrackNum = trackNum        # Track number
  203.         self.TotalClicksFromStart = 0   # Store number of clicks elapsed from start
  204.         self.BytesRead = 0              # Number of file bytes read for track
  205.         self.FirstNoteClick = None      # Start of first note in track
  206.         self.FirstNoteMs = None         # The same, in milliseconds
  207.         self.LastNoteClick = None       # End of last note in track
  208.         self.LastNoteMs = None          # In millseconds
  209.         self.LyricsTrack = False        # This track contains lyrics
  210.         self.RunningStatus = 0          # MIDI Running Status byte
  211.  
  212.         self.text_events = Lyrics()       # Lyrics (0x1 events)
  213.         self.lyric_events = Lyrics()      # Lyrics (0x5 events)
  214.  
  215.  
  216. class MidiTimestamp:
  217.     """ This class is used to apply the tempo changes to the click
  218.    count, thus computing a time in milliseconds for any number of
  219.    clicks from the beginning of the song. """
  220.  
  221.     def __init__(self, midifile):
  222.         self.ClickUnitsPerQuarter = midifile.ClickUnitsPerQuarter
  223.         self.Tempo = midifile.Tempo
  224.         self.ms = 0
  225.         self.click = 0
  226.         self.i = 0
  227.  
  228.     def advanceToClick(self, click):
  229.         # Moves time forward to the indicated click number.
  230.         clicks = click - self.click
  231.         if clicks < 0:
  232.             # Ignore jumps backward in time.
  233.             return
  234.  
  235.         while clicks > 0 and self.i < len(self.Tempo):
  236.             # How many clicks remain at the current tempo?
  237.             clicksRemaining = max(self.Tempo[self.i][0] - self.click, 0)
  238.             clicksUsed = min(clicks, clicksRemaining)
  239.             if clicksUsed != 0:
  240.                 self.ms += self.getTimeForClicks(clicksUsed, self.Tempo[self.i - 1][1])
  241.             self.click += clicksUsed
  242.             clicks -= clicksUsed
  243.             clicksRemaining -= clicksUsed
  244.             if clicksRemaining == 0:
  245.                 self.i += 1
  246.  
  247.         if clicks > 0:
  248.             # We have reached the last tempo mark of the song, so this
  249.             # tempo holds forever.
  250.             self.ms += self.getTimeForClicks(clicks, self.Tempo[-1][1])
  251.             self.click += clicks
  252.  
  253.     def getTimeForClicks(self, clicks, tempo):
  254.         microseconds = ( ( float(clicks) / self.ClickUnitsPerQuarter ) * tempo );
  255.         time_ms = microseconds / 1000
  256.         return (time_ms)
  257.  
  258. class LyricSyllable:
  259.     """ Each instance of this class records a single lyric event,
  260.    e.g. a syllable of a word to be displayed and change color at a
  261.    given time.  The Lyrics class stores a list of these. """
  262.  
  263.     def __init__(self, click, text, line, type = TEXT_LYRIC):
  264.         self.click = click
  265.         self.ms = None
  266.         self.text = text
  267.         self.line = line
  268.         self.type = type
  269.  
  270.         # This is filled in when the syllable is drawn onscreen.
  271.         self.left = None
  272.         self.right = None
  273.  
  274.     def makeCopy(self, text):
  275.         # Returns a new LyricSyllable, exactly like this one, with
  276.         # the text replaced by the indicated string
  277.         syllable = LyricSyllable(self.click, text, self.line, self.type)
  278.         syllable.ms = self.ms
  279.         return syllable
  280.  
  281.     def __repr__(self):
  282.         return "<%s %s>" % (self.ms, self.text)
  283.  
  284. class Lyrics:
  285.     """ This is the complete lyrics of a song, organized as a list of
  286.    syllables sorted by event time. """
  287.  
  288.     def __init__(self):
  289.         self.list = []
  290.         self.line = 0
  291.  
  292.     def hasAny(self):
  293.         # Returns true if there are any lyrics.
  294.         return bool(self.list)
  295.  
  296.     def recordText(self, click, text):
  297.         # Records a MIDI 0x1 text event (a syllable).
  298.  
  299.         # Make sure there are no stray null characters in the string.
  300.         text = text.replace('\x00', '')
  301.         # Or CR's.
  302.         text = text.replace('\r', '')
  303.  
  304.         if not text:
  305.             # Ignore blank lines.
  306.             return
  307.  
  308.         if text[0] == '@':
  309.             if text[1] == 'T':
  310.                 # A title.
  311.                 type = TEXT_TITLE
  312.             elif text[1] == 'I':
  313.                 # An info line.
  314.                 type = TEXT_INFO
  315.             else:
  316.                 # Any other comment we ignore.
  317.                 return
  318.  
  319.             # Put the comment onscreen.
  320.             for line in text[2:].split('\n'):
  321.                 line = line.strip()
  322.                 self.line += 1
  323.                 self.list.append(LyricSyllable(click, line, self.line, type))
  324.             return
  325.  
  326.         if text[0] == '\\':
  327.             # Paragraph break.  We treat it the same as line break,
  328.             # but with an extra blank line.
  329.             self.line += 2
  330.             text = text[1:]
  331.         elif text[0] == '/':
  332.             # Line break.
  333.             self.line += 1
  334.             text = text[1:]
  335.  
  336.         if text:
  337.             lines = text.split('\n')
  338.             self.list.append(LyricSyllable(click, lines[0], self.line))
  339.             for line in lines[1:]:
  340.                 self.line += 1
  341.                 self.list.append(LyricSyllable(click, line, self.line))
  342.  
  343.     def recordLyric(self, click, text):
  344.         # Records a MIDI 0x5 lyric event (a syllable).
  345.  
  346.         # Make sure there are no stray null characters in the string.
  347.         text = text.replace('\x00', '')
  348.  
  349.         if text == '\n':
  350.             # Paragraph break.  We treat it the same as line break,
  351.             # but with an extra blank line.
  352.             self.line += 2
  353.  
  354.         elif text == '\r' or text == '\r\n':
  355.             # Line break.
  356.             self.line += 1
  357.  
  358.         elif text:
  359.             text = text.replace('\r', '')
  360.  
  361.             if text[0] == '\\':
  362.                 # Paragraph break.  This is a text event convention, not a
  363.                 # lyric event convention, but some midi files don't play
  364.                 # by the rules.
  365.                 self.line += 2
  366.                 text = text[1:]
  367.             elif text[0] == '/':
  368.                 # Line break.  A text convention, but see above.
  369.                 self.line += 1
  370.                 text = text[1:]
  371.  
  372.             # Lyrics aren't supposed to include embedded newlines, but
  373.             # sometimes they do anyway.
  374.             lines = text.split('\n')
  375.             self.list.append(LyricSyllable(click, lines[0], self.line))
  376.             for line in lines[1:]:
  377.                 self.line += 1
  378.                 self.list.append(LyricSyllable(click, line, self.line))
  379.  
  380.     def computeTiming(self, midifile):
  381.         # Walk through the lyrics and convert the click information to
  382.         # elapsed time in milliseconds.
  383.  
  384.         ts = MidiTimestamp(midifile)
  385.         for syllable in self.list:
  386.             ts.advanceToClick(syllable.click)
  387.             syllable.ms = int(ts.ms)
  388.  
  389.         # Also change the firstNoteClick to firstNoteMs, for each track.
  390.         for track_desc in midifile.trackList:
  391.             ts = MidiTimestamp(midifile)
  392.             if track_desc.FirstNoteClick != None:
  393.                 ts.advanceToClick(track_desc.FirstNoteClick)
  394.                 track_desc.FirstNoteMs = ts.ms
  395.                 if debug:
  396.                     print "T%s first note at %s clicks, %s ms" % (
  397.                         track_desc.TrackNum, track_desc.FirstNoteClick,
  398.                         track_desc.FirstNoteMs)
  399.             if track_desc.LastNoteClick != None:
  400.                 ts.advanceToClick(track_desc.LastNoteClick)
  401.                 track_desc.LastNoteMs = ts.ms
  402.  
  403.     def analyzeSpaces(self):
  404.         """ Checks for a degenerate case: no (or very few) spaces
  405.        between words.  Sometimes Karaoke writers omit the spaces
  406.        between words, which makes the text very hard to read.  If we
  407.        detect this case, repair it by adding spaces back in. """
  408.  
  409.         # First, group the syllables into lines.
  410.         lineNumber = None
  411.         lines = []
  412.         currentLine = []
  413.  
  414.         for syllable in self.list:
  415.             if syllable.line != lineNumber:
  416.                 if currentLine:
  417.                     lines.append(currentLine)
  418.                 currentLine = []
  419.                 lineNumber = syllable.line
  420.             currentLine.append(syllable)
  421.  
  422.         if currentLine:
  423.             lines.append(currentLine)
  424.  
  425.         # Now, count the spaces between the syllables of the lines.
  426.         totalNumSyls = 0
  427.         totalNumGaps = 0
  428.         for line in lines:
  429.             numSyls = len(line) - 1
  430.             numGaps = 0
  431.             for i in range(numSyls):
  432.                 if line[i].text.rstrip() != line[i].text or \
  433.                    line[i + 1].text.lstrip() != line[i + 1].text:
  434.                     numGaps += 1
  435.  
  436.             totalNumSyls += numSyls
  437.             totalNumGaps += numGaps
  438.  
  439.         if totalNumSyls and float(totalNumGaps) / float(totalNumSyls) < 0.1:
  440.             # Too few spaces.  Insert more.
  441.             for line in lines:
  442.                 for syllable in line[:-1]:
  443.                     if syllable.text.endswith('-'):
  444.                         # Assume a trailing hyphen means to join syllables.
  445.                         syllable.text = syllable.text[:-1]
  446.                     else:
  447.                         syllable.text += ' '
  448.  
  449.  
  450.     def wordWrapLyrics(self, font):
  451.         # Walks through the lyrics and folds each line to the
  452.         # indicated width.  Returns the new lyrics as a list of lists
  453.         # of syllables; that is, each element in the returned list
  454.         # corresponds to a displayable line, and each line is a list
  455.         # of syllabels.
  456.  
  457.         if not self.list:
  458.             return []
  459.  
  460.         maxWidth = manager.displaySize[0] - X_BORDER * 2
  461.  
  462.         lines = []
  463.  
  464.         x = 0
  465.         currentLine = []
  466.         currentText = ''
  467.         lineNumber = self.list[0].line
  468.         for syllable in self.list:
  469.             # Ensure the screen position of the syllable is cleared,
  470.             # in case we are re-wrapping text that was already
  471.             # displayed.
  472.             syllable.left = None
  473.             syllable.right = None
  474.  
  475.             while lineNumber < syllable.line:
  476.                 # A newline.
  477.                 lines.append(currentLine)
  478.                 x = 0
  479.                 currentLine = []
  480.                 currentText = ''
  481.                 lineNumber += 1
  482.  
  483.             width, height = font.size(syllable.text)
  484.             currentLine.append(syllable)
  485.             currentText += syllable.text
  486.             x += width
  487.             while x > maxWidth:
  488.                 foldPoint = manager.FindFoldPoint(currentText, font, maxWidth)
  489.                 if foldPoint == len(currentText):
  490.                     # Never mind.  Must be just whitespace on the end of
  491.                     # the line; let it pass.
  492.                     break
  493.  
  494.                 # All the characters before foldPoint get output as the
  495.                 # first line.
  496.                 n = 0
  497.                 i = 0
  498.                 text = currentLine[i].text
  499.                 outputLine = []
  500.                 while n + len(text) <= foldPoint:
  501.                     outputLine.append(currentLine[i])
  502.                     n += len(text)
  503.                     i += 1
  504.                     text = currentLine[i].text
  505.  
  506.                 syllable = currentLine[i]
  507.                 if i == 0:
  508.                     # One long line.  Break it mid-phrase.
  509.                     a = syllable.makeCopy(syllable.text[:foldPoint])
  510.                     outputLine.append(a)
  511.                     b = syllable.makeCopy('  ' + syllable.text[foldPoint:])
  512.                     currentLine[i] = b
  513.  
  514.                 else:
  515.                     currentLine[i] = syllable.makeCopy('  ' + syllable.text)
  516.  
  517.                 # The remaining characters become the next line.
  518.                 lines.append(outputLine)
  519.                 currentLine = currentLine[i:]
  520.                 currentText = ''
  521.                 for syllable in currentLine:
  522.                     currentText += syllable.text
  523.                 x, height = font.size(currentText)
  524.  
  525.         lines.append(currentLine)
  526.  
  527.         # Indicated that the first syllable of each line is flush with
  528.         # the left edge of the screen.
  529.         for l in lines:
  530.             if l:
  531.                 l[0].left = X_BORDER
  532.  
  533.         #print lines
  534.         return lines
  535.  
  536.     def write(self):
  537.         # Outputs the lyrics, one line at a time.
  538.         for syllable in self.list:
  539.             print "%s(%s) %s %s" % (syllable.ms, syllable.click, syllable.line, repr(syllable.text))
  540.  
  541. def midiParseData(midiData, ErrorNotifyCallback, Encoding):
  542.  
  543.     # Create the midiFile structure
  544.     midifile = midiFile()
  545.     midifile.text_encoding = Encoding
  546.  
  547.     # Open the file
  548.     filehdl = cStringIO.StringIO(midiData)
  549.  
  550.     # Check it's a MThd chunk
  551.     packet = filehdl.read(8)
  552.     ChunkType, Length = struct.unpack('>4sL', packet)
  553.     if (ChunkType != "MThd"):
  554.         ErrorNotifyCallback ("No MIDI Header chunk at start")
  555.         return None
  556.  
  557.     # Read header
  558.     packet = filehdl.read(Length)
  559.     format, tracks, division = struct.unpack('>HHH', packet)
  560.     if (division & 0x8000):
  561.         midifile.ClickUnitsPerSMPTE = division & 0x00FF
  562.         midifile.SMPTEFramesPerSec = division & 0x7F00
  563.     else:
  564.         midifile.ClickUnitsPerQuarter = division & 0x7FFF
  565.  
  566.     # Loop through parsing all tracks
  567.     trackBytes = 1
  568.     trackNum = 0
  569.     while (trackBytes != 0):
  570.         # Read the next track header
  571.         packet = filehdl.read(8)
  572.         if packet == "" or len(packet) < 8:
  573.             # End of file, we're leaving
  574.             break
  575.         # Check it's a MTrk
  576.         ChunkType, Length = struct.unpack('>4sL', packet)
  577.         if (ChunkType != "MTrk"):
  578.             if debug:
  579.                 print ("Didn't find expected MIDI Track")
  580.  
  581.         # Process the track, getting a TrackDesc structure
  582.         track_desc = midiParseTrack(filehdl, midifile, trackNum, Length, ErrorNotifyCallback)
  583.         if track_desc:
  584.             trackBytes = track_desc.BytesRead
  585.             # Store the track descriptor with the others
  586.             midifile.trackList.append(track_desc)
  587.             # Debug out the first note for this track
  588.             if debug:
  589.                 print ("T%d: First note(%s)" % (trackNum, track_desc.FirstNoteClick))
  590.             trackNum = trackNum + 1
  591.  
  592.     # Close the open file
  593.     filehdl.close()
  594.  
  595.     # Get the lyrics from the best track.  We prefer any tracks that
  596.     # are "lyrics" tracks.  Failing that, we get the track with the
  597.     # most number of syllables.
  598.     bestSortKey = None
  599.     midifile.lyrics = None
  600.  
  601.     for track_desc in midifile.trackList:
  602.         lyrics = None
  603.  
  604.         # Decide which list of lyric events to choose. There may be
  605.         # text events (0x01), lyric events (0x05) or sometimes both
  606.         # for compatibility. If both are available, we choose the one
  607.         # with the most syllables, or text if they're the same.
  608.         if track_desc.text_events.hasAny() and track_desc.lyric_events.hasAny():
  609.             if len(track_desc.lyric_events.list) > len(track_desc.text_events.list):
  610.                 lyrics = track_desc.lyric_events
  611.             else:
  612.                 lyrics = track_desc.text_events
  613.         elif track_desc.text_events.hasAny():
  614.             lyrics = track_desc.text_events
  615.         elif track_desc.lyric_events.hasAny():
  616.             lyrics = track_desc.lyric_events
  617.  
  618.         if not lyrics:
  619.             continue
  620.         sortKey = (track_desc.LyricsTrack, len(lyrics.list))
  621.         if sortKey > bestSortKey:
  622.             bestSortKey = sortKey
  623.             midifile.lyrics = lyrics
  624.  
  625.     if not midifile.lyrics:
  626.         ErrorNotifyCallback ("No lyrics in the track")
  627.         return None
  628.  
  629.     midifile.lyrics.computeTiming(midifile)
  630.     midifile.lyrics.analyzeSpaces()
  631.  
  632.     # Calculate the song start (earliest note event in all tracks), as
  633.     # well as the song end (last note event in all tracks).
  634.     earliestNoteMS = None
  635.     lastNoteMS = None
  636.     for track in midifile.trackList:
  637.         if track.FirstNoteMs != None:
  638.             if (track.FirstNoteMs < earliestNoteMS) or (earliestNoteMS == None):
  639.                 earliestNoteMS = track.FirstNoteMs
  640.         if track.LastNoteMs != None:
  641.             if (track.LastNoteMs > lastNoteMS) or (lastNoteMS == None):
  642.                 lastNoteMS = track.LastNoteMs
  643.     midifile.earliestNoteMS = earliestNoteMS
  644.     midifile.lastNoteMS = lastNoteMS
  645.  
  646.     if debug:
  647.         print "first = %s" % (midifile.earliestNoteMS)
  648.         print "last = %s" % (midifile.lastNoteMS)
  649.  
  650.     # Return the populated midiFile structure
  651.     return midifile
  652.  
  653.  
  654. def midiParseTrack (filehdl, midifile, trackNum, Length, ErrorNotifyCallback):
  655.     # Create the new TrackDesc structure
  656.     track = TrackDesc(trackNum)
  657.     if debug:
  658.         print "Track %d" % trackNum
  659.     # Loop through all events in the track, recording salient meta-events and times
  660.     eventBytes = 0
  661.     while track.BytesRead < Length:
  662.         eventBytes = midiProcessEvent (filehdl, track, midifile, ErrorNotifyCallback)
  663.         if (eventBytes == None) or (eventBytes == -1) or (eventBytes == 0):
  664.             return None
  665.         track.BytesRead = track.BytesRead + eventBytes
  666.     return track
  667.  
  668.  
  669. def midiProcessEvent (filehdl, track_desc, midifile, ErrorNotifyCallback):
  670.     bytesRead = 0
  671.     running_status = 0
  672.     click, varBytes = varLength(filehdl)
  673.     if varBytes == 0:
  674.         return 0
  675.     bytesRead = bytesRead + varBytes
  676.     track_desc.TotalClicksFromStart += click
  677.     byteStr = filehdl.read(1)
  678.     bytesRead = bytesRead + 1
  679.     status_byte = ord(byteStr)
  680.  
  681.     # Handle the MIDI running status. This allows consecutive
  682.     # commands of the same event type to not bother sending
  683.     # the event type again. If the top bit isn't set it's a
  684.     # data byte using the last event type.
  685.     if (status_byte & 0x80):
  686.         # This is a new status byte, not a data byte using
  687.         # the running status. Set the current running status
  688.         # to this new status byte and use it as the event type.
  689.         event_type = status_byte
  690.         # Only save running status for voice messages
  691.         if (event_type & 0xF0) != 0xF0:
  692.             track_desc.RunningStatus = event_type
  693.  
  694.     else:
  695.         # Use the last event type, and seek back in the file
  696.         # as this byte is actual data, not an event code
  697.         event_type = track_desc.RunningStatus
  698.         filehdl.seek (-1, 1)
  699.         bytesRead = bytesRead - 1
  700.  
  701.     #print ("T%d: VarBytes = %d, event_type = 0x%X" % (track_desc.TrackNum, varBytes, event_type))
  702. ##     if debug:
  703. ##         print "Event: 0x%X" % event_type
  704.  
  705.     # Handle all event types
  706.     if event_type == 0xFF:
  707.         byteStr = filehdl.read(1)
  708.         bytesRead = bytesRead + 1
  709.         event = ord(byteStr)
  710.         if debug:
  711.             print "MetaEvent: 0x%X" % event
  712.         if event == 0x00:
  713.             # Sequence number (discarded)
  714.             packet = filehdl.read(2)
  715.             bytesRead = bytesRead + 2
  716.             zero, type = map(ord, packet)
  717.             if type == 0x02:
  718.                 # Discard next two bytes as well
  719.                 discard = filehdl.read(2)
  720.             elif type == 0x00:
  721.                 # Nothing left to discard
  722.                 pass
  723.             else:
  724.                 if debug:
  725.                     print ("Invalid sequence number (%d)" % type)
  726.         elif event == 0x01:
  727.             # Text Event
  728.             Length, varBytes = varLength(filehdl)
  729.             bytesRead = bytesRead + varBytes
  730.             text = filehdl.read(Length)
  731.             bytesRead = bytesRead + Length
  732.             if Length > 1000:
  733.                 # This must be a mistake.
  734.                 if debug:
  735.                     print ("Ignoring text of length %s" % (Length))
  736.             else:
  737.                 if (midifile.text_encoding != "") :
  738.                     text = text.decode(midifile.text_encoding, 'replace')
  739.                 # Take out any Sysex text events, and append to the lyrics list
  740.                 if (" SYX" not in text) and ("Track-" not in text) \
  741.                     and ("%-" not in text) and ("%+" not in text):
  742.                     track_desc.text_events.recordText(track_desc.TotalClicksFromStart, text)
  743.                 if debug:
  744.                     print ("Text: %s" % (repr(text)))
  745.         elif event == 0x02:
  746.             # Copyright (discard)
  747.             Length, varBytes = varLength(filehdl)
  748.             bytesRead = bytesRead + varBytes
  749.             discard = filehdl.read(Length)
  750.             bytesRead = bytesRead + Length
  751.         elif event == 0x03:
  752.             # Title of track
  753.             Length, varBytes = varLength(filehdl)
  754.             bytesRead = bytesRead + varBytes
  755.             title = filehdl.read(Length)
  756.             bytesRead = bytesRead + Length
  757.             if debug:
  758.                 print ("Track Title: " + repr(title))
  759.             if title == "Words":
  760.                 track_desc.LyricsTrack = True
  761.         elif event == 0x04:
  762.             # Instrument (discard)
  763.             Length, varBytes = varLength(filehdl)
  764.             bytesRead = bytesRead + varBytes
  765.             discard = filehdl.read(Length)
  766.             bytesRead = bytesRead + Length
  767.         elif event == 0x05:
  768.             # Lyric Event (a new style text record)
  769.             Length, varBytes = varLength(filehdl)
  770.             bytesRead = bytesRead + varBytes
  771.             lyric = filehdl.read(Length)
  772.             if (midifile.text_encoding != "") :
  773.                 lyric = lyric.decode(midifile.text_encoding, 'replace')
  774.             bytesRead = bytesRead + Length
  775.             # Take out any Sysex text events, and append to the lyrics list
  776.             if (" SYX" not in lyric) and ("Track-" not in lyric) \
  777.                 and ("%-" not in lyric) and ("%+" not in lyric):
  778.                 track_desc.lyric_events.recordLyric(track_desc.TotalClicksFromStart, lyric)
  779.             if debug:
  780.                 print ("Lyric: %s" % (repr(lyric)))
  781.         elif event == 0x06:
  782.             # Marker (discard)
  783.             Length, varBytes = varLength(filehdl)
  784.             bytesRead = bytesRead + varBytes
  785.             discard = filehdl.read(Length)
  786.             bytesRead = bytesRead + Length
  787.         elif event == 0x07:
  788.             # Cue point (discard)
  789.             Length, varBytes = varLength(filehdl)
  790.             bytesRead = bytesRead + varBytes
  791.             discard = filehdl.read(Length)
  792.             bytesRead = bytesRead + Length
  793.         elif event == 0x08:
  794.             # Program name (discard)
  795.             Length, varBytes = varLength(filehdl)
  796.             bytesRead = bytesRead + varBytes
  797.             discard = filehdl.read(Length)
  798.             bytesRead = bytesRead + Length
  799.         elif event == 0x09:
  800.             # Device (port) name (discard)
  801.             Length, varBytes = varLength(filehdl)
  802.             bytesRead = bytesRead + varBytes
  803.             discard = filehdl.read(Length)
  804.             bytesRead = bytesRead + Length
  805.         elif event == 0x20:
  806.             # MIDI Channel (discard)
  807.             packet = filehdl.read(2)
  808.             bytesRead = bytesRead + 2
  809.         elif event == 0x21:
  810.             # MIDI Port (discard)
  811.             packet = filehdl.read(2)
  812.             bytesRead = bytesRead + 2
  813.         elif event == 0x2F:
  814.             # End of track
  815.             byteStr = filehdl.read(1)
  816.             bytesRead = bytesRead + 1
  817.             valid = ord(byteStr)
  818.             if valid != 0:
  819.                 print ("Invalid End of track")
  820.         elif event == 0x51:
  821.             # Set Tempo
  822.             packet = filehdl.read(4)
  823.             bytesRead = bytesRead + 4
  824.             valid, tempoA, tempoB, tempoC = map(ord, packet)
  825.             if valid != 0x03:
  826.                 print ("Error: Invalid tempo")
  827.             tempo = (tempoA << 16) | (tempoB << 8) | tempoC
  828.             midifile.Tempo.append((track_desc.TotalClicksFromStart, tempo))
  829.             if debug:
  830.                 ms_per_quarter = (tempo/1000)
  831.                 print ("Tempo: %d (%d ms per quarter note)"% (tempo, ms_per_quarter))
  832.         elif event == 0x54:
  833.             # SMPTE (discard)
  834.             packet = filehdl.read(6)
  835.             bytesRead = bytesRead + 6
  836.         elif event == 0x58:
  837.             # Meta Event: Time Signature
  838.             packet = filehdl.read(5)
  839.             bytesRead = bytesRead + 5
  840.             valid, num, denom, clocks, notes = map(ord, packet)
  841.             if valid != 0x04:
  842.                 print ("Error: Invalid time signature (valid=%d, num=%d, denom=%d)" % (valid,num,denom))
  843.             midifile.Numerator = num
  844.             midifile.Denominator = denom
  845.             midifile.ClocksPerMetronomeTick = clocks
  846.             midifile.NotesPer24MIDIClocks = notes
  847.         elif event == 0x59:
  848.             # Key signature (discard)
  849.             packet = filehdl.read(3)
  850.             bytesRead = bytesRead + 3
  851.             valid, sf, mi = map(ord, packet)
  852.             if valid != 0x02:
  853.                 print ("Error: Invalid key signature (valid=%d, sf=%d, mi=%d)" % (valid,sf,mi))
  854.         elif event == 0x7F:
  855.             # Sequencer Specific Meta Event
  856.             Length, varBytes = varLength(filehdl)
  857.             bytesRead = bytesRead + varBytes
  858.             byteStr = filehdl.read(1)
  859.             bytesRead = bytesRead + 1
  860.             ID = ord(byteStr)
  861.             if ID == 0:
  862.                 packet = filehdl.read(2)
  863.                 bytesRead = bytesRead + 2
  864.                 ID = struct.unpack('>H', packet)[0]
  865.                 Length = Length - 3
  866.             else:
  867.                 Length = Length - 1
  868.             data = filehdl.read(Length)
  869.             bytesRead = bytesRead + Length
  870.             if debug:
  871.                 print ("Sequencer Specific Event (Data Length %d)"%Length)
  872.                 print ("Manufacturer's ID: " + str(ID))
  873.                 print ("Manufacturer Data: " + data)
  874.         else:
  875.             # Unknown event (discard)
  876.             if debug:
  877.                 print ("Unknown meta-event: 0x%X" % event)
  878.             Length, varBytes = varLength(filehdl)
  879.             bytesRead = bytesRead + varBytes
  880.             discard = filehdl.read(Length)
  881.             bytesRead = bytesRead + Length
  882.  
  883.     elif (event_type & 0xF0) == 0x80:
  884.         # Note off
  885.         packet = filehdl.read(2)
  886.         bytesRead = bytesRead + 2
  887.         track_desc.LastNoteClick = track_desc.TotalClicksFromStart
  888.     elif (event_type & 0xF0) == 0x90:
  889.         # Note on (discard but note if the start time of the first in the track)
  890.         packet = filehdl.read(2)
  891.         bytesRead = bytesRead + 2
  892.         #print ("T%d: 0x%X" % (track_desc.TrackNum, event_type))
  893.         if track_desc.FirstNoteClick == None:
  894.             track_desc.FirstNoteClick = track_desc.TotalClicksFromStart
  895.         track_desc.LastNoteClick = track_desc.TotalClicksFromStart
  896.     elif (event_type & 0xF0) == 0xA0:
  897.         # Key after-touch (discard)
  898.         packet = filehdl.read(2)
  899.         bytesRead = bytesRead + 2
  900.     elif (event_type & 0xF0) == 0xB0:
  901.         # Control change (discard)
  902.         packet = filehdl.read(2)
  903.         bytesRead = bytesRead + 2
  904.         if debug:
  905.             c, v = map(ord, packet)
  906.             print ("Control: C%d V%d" % (c,v))
  907.     elif (event_type & 0xF0) == 0xC0:
  908.         # Program (patch) change (discard)
  909.         packet = filehdl.read(1)
  910.         bytesRead = bytesRead + 1
  911.     elif (event_type & 0xF0) == 0xD0:
  912.         # Channel after-touch (discard)
  913.         packet = filehdl.read(1)
  914.         bytesRead = bytesRead + 1
  915.     elif (event_type & 0xF0) == 0xE0:
  916.         # Pitch wheel change (discard)
  917.         packet = filehdl.read(2)
  918.         bytesRead = bytesRead + 2
  919.     elif event_type == 0xF0:
  920.         # F0 Sysex Event (discard)
  921.         Length, varBytes = varLength(filehdl)
  922.         bytesRead = bytesRead + varBytes
  923.         discard = filehdl.read(Length - 1)
  924.         end_byte = filehdl.read(1)
  925.         end = ord(end_byte)
  926.         bytesRead = bytesRead + Length
  927.         if (end != 0xF7):
  928.             print ("Invalid F0 Sysex end byte (0x%X)" % end)
  929.     elif event_type == 0xF7:
  930.         # F7 Sysex Event (discard)
  931.         Length, varBytes = varLength(filehdl)
  932.         bytesRead = bytesRead + varBytes
  933.         discard = filehdl.read(Length)
  934.         bytesRead = bytesRead + Length
  935.     else:
  936.         # Unknown event (discard)
  937.         if debug:
  938.             print ("Unknown event: 0x%x" % event_type)
  939.         Length, varBytes = varLength(filehdl)
  940.         bytesRead = bytesRead + varBytes
  941.         discard = filehdl.read(Length)
  942.         bytesRead = bytesRead + Length
  943.     return bytesRead
  944.  
  945.  
  946. # Read a variable length quantity from the file's current read position.
  947. # Reads the file one byte at a time until the full value has been read,
  948. # and returns a tuple of the full integer and the number of bytes read
  949. def varLength(filehdl):
  950.     convertedInt = 0
  951.     bitShift = 0
  952.     bytesRead = 0
  953.     while (bitShift <= 42):
  954.         byteStr = filehdl.read(1)
  955.         bytesRead = bytesRead + 1
  956.         if byteStr:
  957.             byteVal = ord(byteStr)
  958.             convertedInt = (convertedInt << 7) | (byteVal & 0x7F)
  959.             #print ("<0x%X/0x%X>"% (byteVal, convertedInt))
  960.             if (byteVal & 0x80):
  961.                 bitShift = bitShift + 7
  962.             else:
  963.                 break
  964.         else:
  965.             return (0, 0)
  966.     return (convertedInt, bytesRead)
  967.  
  968.  
  969. class midPlayer(pykPlayer):
  970.     def __init__(self, song, songDb, errorNotifyCallback=None, doneCallback=None):
  971.         """The first parameter, song, may be either a pykdb.SongStruct
  972.        instance, or it may be a filename. """
  973.  
  974.         pykPlayer.__init__(self, song, songDb, errorNotifyCallback, doneCallback)
  975.         settings = self.songDb.Settings
  976.  
  977.         self.SupportsFontZoom = True
  978.         self.isValid = False
  979.  
  980.         # Parse the MIDI file
  981.         self.midifile = midiParseData(self.SongDatas[0].GetData(), self.ErrorNotifyCallback, settings.KarEncoding)
  982.         if (self.midifile == None):
  983.             ErrorString = "ERROR: Could not parse the MIDI file"
  984.             self.ErrorNotifyCallback (ErrorString)
  985.             return
  986.         elif (self.midifile.lyrics == None):
  987.             ErrorString = "ERROR: Could not get any lyric data from file"
  988.             self.ErrorNotifyCallback (ErrorString)
  989.             return
  990.  
  991.         self.isValid = True
  992.  
  993.         # Debug out the found lyrics
  994.         if debug:
  995.             self.midifile.lyrics.write()
  996.  
  997.         manager.setCpuSpeed('kar')
  998.         manager.InitPlayer(self)
  999.         manager.OpenDisplay()
  1000.  
  1001.         if not manager.options.nomusic:
  1002.             manager.OpenAudio(frequency = manager.settings.MIDISampleRate,
  1003.                               channels = 1)
  1004.  
  1005.         # Account for the size of the playback buffer in the lyrics
  1006.         # display.  Assume that the buffer will be mostly full.  On a
  1007.         # slower computer that's struggling to keep up, this may not
  1008.         # be the right amount of delay, but it should usually be
  1009.         # pretty close.
  1010.         self.InternalOffsetTime = -manager.GetAudioBufferMS()
  1011.  
  1012.         self.screenDirty = False
  1013.         self.initFont()
  1014.  
  1015.         # Windows reports the song time correctly (including period up
  1016.         # to the first note), so no need for the earliest note hack
  1017.         # there.  On timidity-based platforms, we anticipate our
  1018.         # lyrics display by the time of the first note.
  1019.  
  1020.         # Note: pygame on OSX can run MIDI natively, or if the GUS
  1021.         # patches are installed in /usr/local/lib/timidity, it will
  1022.         # run MIDI via Timidity instead, which appears to work better
  1023.         # than the native support, so we recommend this.
  1024.         if env != ENV_WINDOWS:
  1025.             self.InternalOffsetTime += self.midifile.earliestNoteMS
  1026.  
  1027.         # Now word-wrap the text to fit our window.
  1028.         self.lyrics = self.midifile.lyrics.wordWrapLyrics(self.font)
  1029.  
  1030.         # By default, we will use the get_pos() functionality returned
  1031.         # by pygame to get the current time through the song, to
  1032.         # synchronize lyric display with the music.
  1033.         self.useMidiTimer = True
  1034.  
  1035.         if env == ENV_WINDOWS:
  1036.             # Unless we're running on Windows (i.e., not timidity).
  1037.             # For some reason, hardware MIDI playback can report an
  1038.             # unreliable time.  To avoid that problem, we'll always
  1039.             # use the CPU timer instead of the MIDI timer.
  1040.             self.useMidiTimer = False
  1041.  
  1042.         # Load the MIDI player
  1043.         if manager.options.nomusic:
  1044.             # If we're not playing music, use the CPU timer instead of
  1045.             # the MIDI timer.
  1046.             self.useMidiTimer = False
  1047.  
  1048.         else:
  1049.             # Load the sound normally for playback.
  1050.             audio_path = self.SongDatas[0].GetFilepath()
  1051.             if type(audio_path) == unicode:
  1052.                 audio_path = audio_path.encode(sys.getfilesystemencoding())
  1053.             pygame.mixer.music.load(audio_path)
  1054.  
  1055.             # Set an event for when the music finishes playing
  1056.             pygame.mixer.music.set_endevent(pygame.USEREVENT)
  1057.  
  1058.         # Reset all the state (current lyric index etc) and
  1059.         # paint the first numRows lines.
  1060.         self.resetPlayingState()
  1061.  
  1062.     def GetPos(self):
  1063.         if self.useMidiTimer:
  1064.             return pygame.mixer.music.get_pos()
  1065.         else:
  1066.             return pykPlayer.GetPos(self)
  1067.  
  1068.     def SetupOptions(self):
  1069.         """ Initialise and return optparse OptionParser object,
  1070.        suitable for parsing the command line options to this
  1071.        application. """
  1072.  
  1073.         parser = pykPlayer.SetupOptions(self, usage = "%prog [options] <KAR file>")
  1074.  
  1075.         # Remove irrelevant options.
  1076.         parser.remove_option('--fps')
  1077.         parser.remove_option('--zoom')
  1078.  
  1079.         return parser
  1080.  
  1081.  
  1082.     def initFont(self):
  1083.         fontSize = int(FONT_SIZE * manager.GetFontScale() * manager.displaySize[1] / 480.)
  1084.         self.font = self.findPygameFont(self.songDb.Settings.KarFont, fontSize)
  1085.         self.lineSize = max(self.font.get_height(), self.font.get_linesize())
  1086.         self.numRows = int((manager.displaySize[1] - Y_BORDER * 2) / self.lineSize)
  1087.  
  1088.         # Put the current singing row at the specified fraction of the
  1089.         # screen.
  1090.         self.viewRow = int(self.numRows * VIEW_PERCENT / 100)
  1091.  
  1092.     def resetPlayingState(self):
  1093.  
  1094.         # Set the state variables
  1095.  
  1096.         # The current point the user was hearing within the song, as
  1097.         # of the last screen update.
  1098.         self.currentMs = 0
  1099.  
  1100.         # The line currently on display at the top of the screen.
  1101.         self.topLine = 0
  1102.  
  1103.         # The line on which the player is currently singing (that is,
  1104.         # the lowest line onscreen containing white syllables).
  1105.         self.currentLine = 0
  1106.  
  1107.         # The time at which this current syllable was sung.
  1108.         self.currentColourMs = 0
  1109.  
  1110.         # The next line with syllables that will need to be painted
  1111.         # white.
  1112.         self.nextLine = 0
  1113.  
  1114.         # The next syllable within the line that needs to be painted.
  1115.         self.nextSyllable = 0
  1116.  
  1117.         # The time at which the next syllable is to be painted.
  1118.         self.nextColourMs = 0
  1119.  
  1120.         # The time at which something is next scheduled to change
  1121.         # onscreen (usually the same as self.nextColourMs).
  1122.         self.nextChangeMs = 0
  1123.  
  1124.         self.repaintScreen()
  1125.  
  1126.     def repaintScreen(self):
  1127.         # Redraws the contents of the currently onscreen text.
  1128.  
  1129.         # Clear the screen
  1130.         settings = self.songDb.Settings
  1131.         manager.surface.fill(settings.KarBackgroundColour)
  1132.  
  1133.         # Paint the first numRows lines
  1134.         for i in range(self.numRows):
  1135.             l = self.topLine + i
  1136.             x = X_BORDER
  1137.             if l < len(self.lyrics):
  1138.                 for syllable in self.lyrics[l]:
  1139.                     syllable.left = x
  1140.                     self.drawSyllable(syllable, i, None)
  1141.                     x = syllable.right
  1142.  
  1143.         manager.Flip()
  1144.         self.screenDirty = False
  1145.  
  1146.     def drawSyllable(self, syllable, row, x):
  1147.         """Draws a new syllable on the screen in the appropriate
  1148.        color, either red or white, according to self.currentMs.  The
  1149.        syllable is draw on the screen at the specified row, numbering
  1150.        0 from the top of the screen.  The value x indicates the x
  1151.        position of the end of the previous syllable, which is used to
  1152.        fill in the syllable's x position if it is not already known.
  1153.        x may be none if the syllable's x position is already
  1154.        known."""
  1155.  
  1156.         if syllable.left == None:
  1157.             syllable.left = x
  1158.             if syllable.left == None:
  1159.                 return
  1160.  
  1161.         y = Y_BORDER + row * self.lineSize
  1162.  
  1163.         settings = self.songDb.Settings
  1164.  
  1165.         if syllable.type == TEXT_LYRIC:
  1166.             if self.currentMs < syllable.ms:
  1167.                 color = settings.KarReadyColour
  1168.             else:
  1169.                 color = settings.KarSweepColour
  1170.         elif syllable.type == TEXT_INFO:
  1171.             color = settings.KarInfoColour
  1172.         elif syllable.type == TEXT_TITLE:
  1173.             color = settings.KarTitleColour
  1174.  
  1175.         # Render text on a black background (instead of transparent)
  1176.         # to save a hair of CPU time.
  1177.         text = self.font.render(syllable.text, True, color,
  1178.                                 settings.KarBackgroundColour)
  1179.  
  1180.         width, height = text.get_size()
  1181.         syllable.right = syllable.left + width
  1182.  
  1183.         manager.surface.blit(text, (syllable.left, y, width, height))
  1184.  
  1185.     def __hasLyrics(self):
  1186.         """ Returns true if the midi file contains any lyrics at all,
  1187.        false if it doesn't (or contains only comments). """
  1188.  
  1189.         if not self.midifile or not self.midifile.lyrics:
  1190.             return False
  1191.  
  1192.         for syllable in self.midifile.lyrics.list:
  1193.             if syllable.type == TEXT_LYRIC:
  1194.                 return True
  1195.         return False
  1196.  
  1197.     def doValidate(self):
  1198.         if not self.__hasLyrics():
  1199.             return False
  1200.  
  1201.         return True
  1202.  
  1203.     def doPlay(self):
  1204.         if not manager.options.nomusic:
  1205.             pygame.mixer.music.play()
  1206.  
  1207.             # For some reason, timidity sometimes reports a bogus
  1208.             # get_pos() until the first few milliseconds have elapsed.  As
  1209.             # a cheesy way around this, we'll just wait a bit right up
  1210.             # front.
  1211.             pygame.time.wait(50)
  1212.  
  1213.     def doPause(self):
  1214.         if not manager.options.nomusic:
  1215.             pygame.mixer.music.pause()
  1216.  
  1217.     def doUnpause(self):
  1218.         if not manager.options.nomusic:
  1219.             pygame.mixer.music.unpause()
  1220.  
  1221.     def doRewind(self):
  1222.         # Reset all the state (current lyric index etc)
  1223.         self.resetPlayingState()
  1224.         # Stop the audio
  1225.         if not manager.options.nomusic:
  1226.             pygame.mixer.music.rewind()
  1227.             pygame.mixer.music.stop()
  1228.  
  1229.     def GetLength(self):
  1230.         """Give the number of seconds in the song."""
  1231.         return self.midifile.lastNoteMS / 1000
  1232.  
  1233.     def shutdown(self):
  1234.         # This will be called by the pykManager to shut down the thing
  1235.         # immediately.
  1236.         if not manager.options.nomusic:
  1237.             if manager.audioProps:
  1238.                 pygame.mixer.music.stop()
  1239.         pykPlayer.shutdown(self)
  1240.  
  1241.  
  1242.     def doStuff(self):
  1243.         pykPlayer.doStuff(self)
  1244.  
  1245.         if self.State == STATE_PLAYING or self.State == STATE_CAPTURING:
  1246.             self.currentMs = int(self.GetPos() + self.InternalOffsetTime + manager.settings.SyncDelayMs)
  1247.             self.colourUpdateMs()
  1248.  
  1249.             # If we're not using the automatic midi timer, we have to
  1250.             # know to when stop the song at the end ourselves.
  1251.             if self.currentMs > self.midifile.lastNoteMS:
  1252.                 self.Close()
  1253.  
  1254.     def handleEvent(self, event):
  1255.         if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN and (event.mod & (pygame.KMOD_LSHIFT | pygame.KMOD_RSHIFT | pygame.KMOD_LMETA | pygame.KMOD_RMETA)):
  1256.             # Shift/meta return: start/stop song.  Useful for keybinding apps.
  1257.             self.Close()
  1258.             return
  1259.        
  1260.         pykPlayer.handleEvent(self, event)
  1261.  
  1262.     def doResize(self, newSize):
  1263.         # This will be called internally whenever the window is
  1264.         # resized for any reason, either due to an application resize
  1265.         # request being processed, or due to the user dragging the
  1266.         # window handles.
  1267.         self.initFont()
  1268.         self.lyrics = self.midifile.lyrics.wordWrapLyrics(self.font)
  1269.  
  1270.         self.topLine = 0
  1271.         self.currentLine = 0
  1272.         self.currentColourMs = 0
  1273.         self.nextLine = 0
  1274.         self.nextSyllable = 0
  1275.         self.nextColourMs = 0
  1276.         self.nextChangeMs = 0
  1277.  
  1278.         self.screenDirty = True
  1279.         self.colourUpdateMs()
  1280.  
  1281.     def colourUpdateMs(self):
  1282.         # If there's nothing yet to happen, just return.
  1283.         if self.nextChangeMs == None or self.currentMs < self.nextChangeMs:
  1284.             return False
  1285.  
  1286.         syllables = self.getNewSyllables()
  1287.         self.nextChangeMs = self.nextColourMs
  1288.  
  1289.         # Is it time to scroll?
  1290.         syllables = self.considerScroll(syllables)
  1291.  
  1292.         if self.screenDirty:
  1293.             # If the whole screen needs to be redrawn anyway, just do
  1294.             # that.
  1295.             self.repaintScreen()
  1296.  
  1297.         else:
  1298.             # Otherwise, draw only the syllables that have changed.
  1299.             x = None
  1300.             for syllable, line in syllables:
  1301.                 self.drawSyllable(syllable, line - self.topLine, x)
  1302.                 x = syllable.right
  1303.  
  1304.             manager.Flip()
  1305.  
  1306.         return True
  1307.  
  1308.     def getNewSyllables(self):
  1309.         """Scans the list of syllables and returns a list of (syllable,
  1310.        line) tuples that represent the syllables that need to be
  1311.        updated (changed color) onscreen.
  1312.  
  1313.        Also updates self.currentLine, self.currentColourMs, self.nextLine,
  1314.        self.nextSyllable, and self.nextColourMs. """
  1315.  
  1316.         syllables = []
  1317.  
  1318.         while self.nextLine < len(self.lyrics):
  1319.             line = self.lyrics[self.nextLine]
  1320.             while self.nextSyllable < len(line):
  1321.                 syllable = line[self.nextSyllable]
  1322.                 if self.currentMs < syllable.ms:
  1323.                     # This is the first syllable we should *not*
  1324.                     # display.  Stop here.
  1325.                     self.nextColourMs = syllable.ms
  1326.                     return syllables
  1327.  
  1328.                 syllables.append((syllable, self.nextLine))
  1329.                 self.currentLine = self.nextLine
  1330.                 self.currentColourMs = syllable.ms
  1331.                 self.nextSyllable += 1
  1332.  
  1333.             self.nextLine += 1
  1334.             self.nextSyllable = 0
  1335.  
  1336.         # There are no more syllables to be displayed.
  1337.         self.nextColourMs = None
  1338.         return syllables
  1339.  
  1340.  
  1341.     def considerScroll(self, syllables):
  1342.         """Determines whether it is time to scroll the screen.  If it
  1343.        is, performs the scroll (without flipping the display yet),
  1344.        and returns the new list of syllables that need to be painted.
  1345.        If it is not yet time to scroll, does nothing and does not
  1346.        modify the syllable list. """
  1347.  
  1348.         # If the player's still singing the top line, we can't scroll
  1349.         # it off yet.
  1350.         if self.currentLine <= self.topLine:
  1351.             return syllables
  1352.  
  1353.         # If the rest of the lines fit onscreen, don't bother scrolling.
  1354.         if self.topLine + self.numRows >= len(self.lyrics):
  1355.             return syllables
  1356.  
  1357.         # But don't scroll unless we have less than
  1358.         # PARAGRAPH_LEAD_TIME milliseconds to go.
  1359.         timeGap = 0
  1360.         if self.nextColourMs != None:
  1361.             timeGap = self.nextColourMs - self.currentColourMs
  1362.             scrollTime = self.nextColourMs - PARAGRAPH_LEAD_TIME
  1363.             if self.currentMs < scrollTime:
  1364.                 self.nextChangeMs = scrollTime
  1365.                 return syllables
  1366.  
  1367.         # Put the current line on self.viewRow by choosing
  1368.         # self.topLine appropriately.  If there is a long gap between
  1369.         # lyrics, go straight to the next line.
  1370.         currentLine = self.currentLine
  1371.         if timeGap > PARAGRAPH_LEAD_TIME:
  1372.             currentLine = self.nextLine
  1373.         topLine = max(min(currentLine - self.viewRow, len(self.lyrics) - self.numRows), 0)
  1374.         if topLine == self.topLine:
  1375.             # No need to scroll.
  1376.             return syllables
  1377.  
  1378.         # OK, we have to scroll.  How many lines?
  1379.         linesScrolled = topLine - self.topLine
  1380.         self.topLine = topLine
  1381.         if linesScrolled < 0 or linesScrolled >= self.numRows:
  1382.             # Never mind; we'll need to repaint the whole screen anyway.
  1383.             self.screenDirty = True
  1384.             return []
  1385.  
  1386.         linesRemaining = self.numRows - linesScrolled
  1387.  
  1388.         # Blit the lower part of the screen to the top.
  1389.         y = Y_BORDER + linesScrolled * self.lineSize
  1390.         h = linesRemaining * self.lineSize
  1391.         rect = pygame.Rect(X_BORDER, y,
  1392.                            manager.displaySize[0] - X_BORDER * 2, h)
  1393.         manager.surface.blit(manager.surface, (X_BORDER, Y_BORDER), rect)
  1394.  
  1395.         # And now fill the lower part of the screen with black.
  1396.         y = Y_BORDER + linesRemaining * self.lineSize
  1397.         h = linesScrolled * self.lineSize
  1398.         rect = pygame.Rect(X_BORDER, y,
  1399.                            manager.displaySize[0] - X_BORDER * 2, h)
  1400.         settings = self.songDb.Settings
  1401.         manager.surface.fill(settings.KarBackgroundColour, rect)
  1402.  
  1403.         # We can remove any syllables from the list that might have
  1404.         # scrolled off the screen now.
  1405.         i = 0
  1406.         while i < len(syllables) and syllables[i][1] < self.topLine:
  1407.             i += 1
  1408.         if i:
  1409.             syllables = syllables[i:]
  1410.  
  1411.         # And furthermore, we need to draw all the syllables that are
  1412.         # found in the newly-appearing lines.
  1413.         for i in range(self.topLine + self.numRows - linesScrolled,
  1414.                        self.topLine + self.numRows):
  1415.             line = self.lyrics[i]
  1416.             for syllable in line:
  1417.                 syllables.append((syllable, i))
  1418.  
  1419.         return syllables
  1420.  
  1421.  
  1422. def usage():
  1423.     print "Usage:  %s <kar filename>" % os.path.basename(sys.argv[0])
  1424.  
  1425.  
  1426. # Can be called from the command line with the CDG filepath as parameter
  1427. def main():
  1428.     player = midPlayer(None, None)
  1429.     if player.isValid:
  1430.         player.Play()
  1431.         manager.WaitForPlayer()
  1432.  
  1433. if __name__ == "__main__":
  1434.     sys.exit(main())
  1435.     #import profile
  1436.     #result = profile.run('main()', 'pykar.prof')
  1437.     #sys.exit(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement