502b3328b74426ec83f28eb8a4cd40a1960fe91c
1 # -*- coding: utf-8 -*-
3 Module de lecture des fichiers de log minwii
9 from widgets
.playingscreen
import PlayingScreenBase
10 from eventutils
import EventDispatcher
11 from events
import eventCodes
12 from synth
import Synth
13 from musicxml
import musicXml2Song
16 SUPPORTED_FILE_HEADER
= 'ENV winwii log format version : 1.0-alpha'
18 class LogFileReader(object) :
20 def __init__(self
, logfile
) :
21 if isinstance(logfile
, str) :
22 self
.logfile
= open(logfile
, 'r')
24 self
.logfile
= logfile
26 firstline
= self
.next()
27 assert firstline
== SUPPORTED_FILE_HEADER
30 def getSongFile(self
) :
36 if l
.startswith('APP chanson :') :
38 songfile
= l
.split(':', 1)[1].strip()
42 def getSoundFontFile(self
) :
47 if l
.startswith('ENV soundfont :') :
49 soundFontFile
= l
.split(':', 1)[1].strip()
52 def getScreenResolution(self
) :
57 if l
.startswith('ENV résolution écran :') :
59 screenResolution
= eval(l
.split(':', 1)[1].strip())
60 return screenResolution
62 def getFirstEventTicks(self
) :
67 if l
.startswith('EVT ') :
69 firstTicks
= int(l
.split(None, 2)[1])
79 line
= self
.logfile
.next().strip()
82 def getEventsIterator(self
) :
86 except StopIteration :
89 if not l
.startswith('EVT ') :
92 ticks
, eventName
, message
= l
.split(None, 3)[1:]
93 yield ticks
, eventName
, message
95 ticks
, eventName
= l
.split(None, 3)[1:]
96 yield ticks
, eventName
, ''
99 class LogFilePlayer(PlayingScreenBase
) :
101 ré-exécution d'une chanson sur la base de son fichier de log.
104 def __init__(self
, logfile
) :
105 lfr
= self
.lfr
= LogFileReader(logfile
)
106 songFile
= lfr
.getSongFile()
107 soundFontFile
= lfr
.getSoundFontFile()
108 sfPath
= lfr
.getSoundFontFile()
109 synth
= Synth(sfPath
=sfPath
)
110 self
.song
= musicXml2Song(songFile
)
111 screenResolution
= lfr
.getScreenResolution()
113 pygame
.display
.set_mode(screenResolution
)
115 super(LogFilePlayer
, self
).__init
__(synth
, self
.song
.distinctNotes
)
119 clock
= pygame
.time
.Clock()
120 pygame
.display
.flip()
121 pygame
.mouse
.set_visible(False)
123 previousTicks
= self
.lfr
.getFirstEventTicks()
124 eIter
= self
.lfr
.getEventsIterator()
126 for ticks
, eventName
, message
in eIter
:
128 if eventName
== 'COLSTATECHANGE' :
129 parts
= message
.split(None, 4)
132 index
, state
, midi
, name
, syllabus
= parts
135 state
= state
== 'True'
136 col
= self
.columns
[midi
]
137 col
.update(state
, syllabus
=syllabus
.decode('utf-8'))
139 pygame
.event
.clear() # à virer
140 #EventDispatcher.dispatchEvents()
142 dirty
= self
.draw(pygame
.display
.get_surface())
143 pygame
.display
.update(dirty
)
144 execTime
= clock
.tick()
146 delay
= ticks
- previousTicks
- execTime
148 pygame
.time
.wait(delay
)
150 previousTicks
= ticks
151 #print ticks, eventName, message
153 #while self._running :
154 # EventDispatcher.dispatchEvents()
155 # dirty = self.draw(pygame.display.get_surface())
156 # pygame.display.update(dirty)