Ajout commentaires.
[minwii.git] / src / logging.disabled / LogPGUPlayer.py
1 '''
2 Created on 25 janv. 2010
3
4 @author: Samuel Benveniste
5 '''
6
7 import pygame
8 import pickle
9
10 from pgu import gui as pguGui
11
12 from gui import constants
13
14 from instruments.Instrument import Instrument
15 from songs.Song import Song
16 from cursor.WarpingCursor import *
17 from gui.StaticFamiliarizer import StaticFamiliarizer
18 from gui.SongFamiliarizer import SongFamiliarizer
19 from gui.SongPlayingScreen import SongPlayingScreen
20 from gui.DummyInstrumentChoice import DummyInstrumentChoice
21 from controllers.Wiimote import Wiimote
22
23 class LogPGUPlayer(pguGui.Desktop):
24 '''
25 classdocs
26 '''
27
28
29 def __init__(self,rate):
30 '''
31 Constructor
32 '''
33 self.firstStepDurations = []
34 self.firstStepClicks = []
35 self.firstStepClicksIn = []
36
37 self.secondStepDurations = []
38 self.secondStepClicks = []
39 self.secondStepClicksIn = []
40
41 self.thirdStepDurations = []
42 self.thirdStepClicks = []
43 self.thirdStepClicksIn = []
44
45 self.songDurations = []
46 self.songClicks = []
47 self.songClicksIn = []
48 self.songClicksPerMinute = []
49 self.songClicksInPerMinute = []
50 self.songTotalDurations = []
51
52 self.meanTimeBetweenNotes = []
53
54 pguGui.Desktop.__init__(self)
55
56 self.replayRate = rate
57 #pguGui.theme.load('../data/themes/default')
58
59 self.connect(pguGui.QUIT,self.quit,None)
60
61 main = pguGui.Container(width=500, height=400) #, background=(220, 220, 220) )
62
63
64 main.add(pguGui.Label("File Dialog Example", cls="h1"), 20, 20)
65
66
67 td_style = {'padding_right': 10}
68 t = pguGui.Table()
69 t.tr()
70 t.td( pguGui.Label('File Name:') , style=td_style )
71 self.input_file = pguGui.Input()
72 t.td( self.input_file, style=td_style )
73 self.browseButton = pguGui.Button("Browse...")
74 t.td( self.browseButton, style=td_style )
75 self.browseButton.connect(pguGui.CLICK, self.open_file_browser, None)
76
77 self.goButton = pguGui.Button("Go")
78
79 self.goButton.connect(pguGui.CLICK, self.goButtonClicked,None)
80
81 self.quitButton = pguGui.Button("Fin")
82 self.quitButton.connect(pguGui.CLICK,self.quit,None)
83
84 t.td( self.browseButton, style=td_style )
85 t.td( self.goButton, style=td_style )
86 t.td( self.quitButton, style=td_style )
87
88 main.add(t, 20, 100)
89
90 self.run(main)
91 #import profile
92 #profile.run('app.run(main)')
93
94 def open_file_browser(self,data=None):
95 d = pguGui.FileDialog(path = "../../../saves")
96 d.connect(pguGui.CHANGE, self.handle_file_browser_closed, d)
97 d.open()
98
99
100 def handle_file_browser_closed(self,dlg):
101 if dlg.value: self.input_file.value = dlg.value
102
103 def goButtonClicked(self,data=None):
104 if self.input_file.value.endswith(".fmwi"):
105 f = file(self.input_file.value, 'r')
106 unpickler = pickle.Unpickler(f)
107 log = unpickler.load()
108 f.close()
109
110 log.eventLog.setReplayRate(self.replayRate)
111
112 pygame.midi.init()
113 instruments = [Instrument(log.scale, i + 1, "".join(["../instruments/instrumentImages/", constants.instrumentImagePathList[i], ".jpg"]), constants.octaves[i]) for i in range(9)]
114
115 joys = [[id,pygame.joystick.Joystick(id).get_name()] for id in range(pygame.joystick.get_count())]
116 for joy in joys:
117 if joy[1] in constants.joyNames:
118 pygame.joystick.Joystick(joy[0]).init()
119
120 ports = [pygame.midi.get_device_info(id)[1] for id in range(pygame.midi.get_count())]
121 portOffset = ports.index(constants.portNames[0])
122 print(portOffset)
123
124 screen = pygame.display.get_surface()
125 clock = pygame.time.Clock()
126 cursorImages=[['../cursor/cursorImages/black/10.png'],['../cursor/cursorImages/red/10.png'],['../cursor/cursorImages/blue/10.png'],['../cursor/cursorImages/green/10.png']]
127 durations = [75 for i in range(len(cursorImages))]
128
129 extsc = True
130 casc = False
131 easyMode = True
132
133 song = Song(constants.scaleDict["majorScale"],[3,9,6,4,1,8,5,7,2,10],True)
134
135 wiimoteCount = 4
136
137 cursors = [WarpingCursor(None, cursorImages[i], durations, (300 * i, 300 * i),'../cursor/cursorImages/black/flash.png') for i in range(wiimoteCount)]
138 wiimotes = [Wiimote(i, i + portOffset, None, instruments[0], cursors[i]) for i in range(wiimoteCount)]
139 dummyInstrumentChoice = DummyInstrumentChoice(wiimotes, window, screen, clock, joys, portOffset, log.activeWiimotes)
140 if log.level < 2 :
141 familiarize = StaticFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,log.activeWiimotes,level = log.level,eventLog = log.eventLog,replay = True)
142 self.fillStaticFamiliarizerStats(familiarize)
143 elif log.level == 2 :
144 familiarize = SongFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,song,log.activeWiimotes,casc,extsc,easyMode,eventLog = log.eventLog,replay = True)
145 self.fillSongFamiliarizerStats(familiarize)
146 else :
147 familiarize = SongPlayingScreen(dummyInstrumentChoice,constants.songDict["clairdelalune"],easyMode = True,eventLog = log.eventLog,replay = True)
148 self.fillSongStats(familiarize)
149
150 while familiarize.nextLevel != None :
151 if familiarize.nextLevel < 2 :
152 familiarize = StaticFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,log.activeWiimotes,level = familiarize.nextLevel,eventLog = familiarize.eventLog,replay = True)
153 self.fillStaticFamiliarizerStats(familiarize)
154 elif familiarize.nextLevel == 2 :
155 familiarize = SongFamiliarizer(wiimotes, window, screen, clock, joys, portOffset,song,log.activeWiimotes,casc,extsc,easyMode,eventLog = familiarize.eventLog,replay = True)
156 self.fillSongFamiliarizerStats(familiarize)
157 else :
158 familiarize = SongPlayingScreen(dummyInstrumentChoice,constants.songDict["clairdelalune"],easyMode = True,eventLog = familiarize.eventLog,replay = True)
159 self.fillSongStats(familiarize)
160
161 for wiimote in wiimotes:
162 del wiimote.port
163
164 pygame.midi.quit()
165 self.printStatsToFile()
166
167 def fillStaticFamiliarizerStats(self,familiarizer):
168 if familiarizer.level == 0 :
169 self.firstStepClicks.append(familiarizer.clicks)
170 self.firstStepClicksIn.append(familiarizer.clicksIn)
171 self.firstStepDurations.append(familiarizer.duration)
172
173 if familiarizer.level == 1 :
174 self.secondStepClicks.append(familiarizer.clicks)
175 self.secondStepClicksIn.append(familiarizer.clicksIn)
176 self.secondStepDurations.append(familiarizer.duration)
177
178 def fillSongFamiliarizerStats(self,familiarizer):
179 self.thirdStepClicks.append(familiarizer.clicks)
180 self.thirdStepClicksIn.append(familiarizer.clicksIn)
181 self.thirdStepDurations.append(familiarizer.duration)
182
183 def fillSongStats(self,familiarizer):
184 self.songClicks.append(familiarizer.clicks)
185 self.songClicksIn.append(familiarizer.clicksIn)
186 self.songClicksPerMinute.append(familiarizer.clicksPerMinute)
187 self.songClicksInPerMinute.append(familiarizer.clicksInPerMinute)
188 self.songDurations.append(familiarizer.songDurations)
189 self.songTotalDurations.append(familiarizer.totalDuration)
190
191 def statsToFormattedString(self):
192 return("First step durations :\n"+"\n"+
193 str(self.firstStepDurations)+"\n"+"\n"+
194 "First step clicks :\n"+"\n"+
195 str(self.firstStepClicks)+"\n"+"\n"+
196 "First step clicksIn :\n"+"\n"+
197 str(self.firstStepClicksIn)+"\n"+"\n"+
198 "Second step durations :\n"+"\n"+
199 str(self.secondStepDurations)+"\n"+"\n"+
200 "Second step clicks :\n"+"\n"+
201 str(self.secondStepClicks)+"\n"+"\n"+
202 "Second step clicksIn :\n"+"\n"+
203 str(self.secondStepClicksIn)+"\n"+"\n"+
204 "Third step durations :\n"+"\n"+
205 str(self.thirdStepDurations)+"\n"+"\n"+
206 "Third step clicks :\n"+"\n"+
207 str(self.thirdStepClicks)+"\n"+"\n"+
208 "Third step clicksIn :\n"+"\n"+
209 str(self.thirdStepClicksIn)+"\n"+"\n"+
210 "song durations :\n"+"\n"+
211 str(self.songDurations)+"\n"+"\n"+
212 "song clicks :\n"+"\n"+
213 str(self.songClicks)+"\n"+"\n"+
214 "song clicksIn :\n"+"\n"+
215 str(self.songClicksIn)+"\n"+"\n"+
216 "song clicks per minute:\n"+"\n"+
217 str(self.songClicksPerMinute)+"\n"+"\n"+
218 "song clicksIn per minute :\n"+"\n"+
219 str(self.songClicksInPerMinute)+"\n"+"\n"+
220 "song total durations :\n"+"\n"+
221 str(self.songTotalDurations)+"\n"+"\n")
222
223 def printStatsToFile(self,path=None):
224 if path == None :
225 path = self.input_file.value.replace(".fmwi",".txt")
226 file = open(path,"w")
227 file.write("Log ID : "+self.input_file.value+"\n"+"\n")
228 file.write(self.statsToFormattedString())
229 file.close()
230
231 if __name__ == "__main__":
232 pygame.init()
233 modeResolution = (1024,768)
234 window = pygame.display.set_mode(modeResolution,pygame.FULLSCREEN)
235 logConfig = LogPGUPlayer(10000)
236 pygame.quit()
237