1 # -*- coding: utf-8 -*-
4 bandes arc-en-ciel représentant un clavier.
12 import kinect
.pygamedisplay
as kinect
14 import minwii
.events
as events
15 from minwii
.log
import eventLogger
16 from minwii
.eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
17 from minwii
.musicxml
import Tone
18 from minwii
.config
import FRAMERATE
19 from minwii
.config
import FIRST_HUE
20 from minwii
.config
import MIDI_VELOCITY_RANGE
21 from minwii
.config
import MIDI_PAN_RANGE
22 from minwii
.config
import MIDI_VELOCITY_WRONG_NOTE_ATTN
23 from minwii
.config
import SCREEN_RESOLUTION
24 from minwii
.globals import BACKGROUND_LAYER
25 from minwii
.globals import CURSOR_LAYER
26 from minwii
.globals import PLAYING_MODES_DICT
28 from cursors
import WarpingCursor
29 from column
import Column
31 class PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
33 def __init__(self
, synth
, distinctNotes
=[], displayNotes
=True) :
35 distinctNotes : notes disctinctes présentes dans la chanson
36 triées du plus grave au plus aigu.
38 super(PlayingScreenBase
, self
).__init
__()
40 self
.distinctNotes
= distinctNotes
41 self
.displayNotes
= displayNotes
42 self
.keyboardLength
= 0
43 self
.keyboardRects
= []
49 self
.kinectRgb
= kinect
.RGBSprite(alpha
=128, size
=SCREEN_RESOLUTION
)
50 self
.add(self
.kinectRgb
, layer
=CURSOR_LAYER
)
54 def _initRects(self
) :
55 """ création des espaces réservés pour
56 afficher les colonnes.
58 self
.keyboardLength
= len(self
.distinctNotes
)
60 screen
= pygame
.display
.get_surface()
62 self
.dispWidth
= dispWidth
= screen
.get_width()
63 self
.dispHeight
= dispHeight
= screen
.get_height()
65 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
68 for i
in range(self
.keyboardLength
- 1) :
69 upperLeftCorner
= (i
*columnWidth
, 0)
70 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
73 # la dernière colonne à la largeur du reste
74 upperLeftCorner
= ((i
+1) * columnWidth
, 0)
75 rect
= pygame
.Rect(upperLeftCorner
, (dispWidth
- (self
.keyboardLength
- 1) * columnWidth
, dispHeight
))
78 self
.keyboardRects
= rects
80 def _initColumns(self
) :
82 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
83 for i
, rect
in enumerate(self
.keyboardRects
) :
84 hue
= FIRST_HUE
- hueStep
* i
85 tone
= self
.distinctNotes
[i
]
86 c
= Column(self
, i
, hue
, rect
, tone
, displayNote
=self
.displayNotes
)
87 self
.add(c
, layer
=BACKGROUND_LAYER
)
88 self
.columns
[tone
.midi
] = c
91 def _initCursor(self
) :
92 self
.cursor
= WarpingCursor(blinkMode
=True)
93 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
97 clock
= pygame
.time
.Clock()
99 pygame
.mouse
.set_visible(False)
100 while self
._running
:
101 EventDispatcher
.dispatchEvents()
102 self
.kinectRgb
.update()
103 dirty
= self
.draw(pygame
.display
.get_surface())
104 pygame
.display
.update(dirty
)
105 clock
.tick(FRAMERATE
)
108 self
._running
= False
109 self
.synth
.system_reset()
110 pygame
.mouse
.set_visible(True)
111 self
.cursor
._stopBlink
()
113 @event_handler(pygame
.KEYDOWN
)
114 def handleKeyDown(self
, event
) :
115 if event
.key
in (pygame
.K_q
, pygame
.K_ESCAPE
) or \
116 event
.unicode == u
'q' :
119 @event_handler(pygame
.MOUSEBUTTONDOWN
)
120 def onMouseDown(self
, event
) :
121 # TODO à cleaner : on vire le dernier élément
122 # parce qu'il s'agit du curseur
123 for col
in reversed(self
.sprites()[:-1]) :
124 if col
.rect
.collidepoint(*event
.pos
):
125 self
.raiseColDown(col
, event
)
128 @event_handler(pygame
.MOUSEBUTTONUP
)
129 def onMouseUp(self
, event
) :
130 for col
in reversed(self
.sprites()[:-1]) :
131 if col
.rect
.collidepoint(*event
.pos
) :
132 self
.raiseColUp(col
, event
)
135 @event_handler(pygame
.MOUSEMOTION
)
136 def onMouseMove(self
, event
) :
137 for col
in reversed(self
.sprites()[:-1]) :
138 if col
.rect
.collidepoint(*event
.pos
) :
139 self
.raiseColOver(col
, event
)
142 def raiseColDown(self
, col
, mouseEvent
) :
143 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=mouseEvent
.pos
)
144 pygame
.event
.post(evt
)
146 def raiseColUp(self
, col
, mouseEvent
) :
147 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=mouseEvent
.pos
)
148 pygame
.event
.post(evt
)
150 def raiseColOver(self
, col
, mouseEvent
) :
151 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=mouseEvent
.pos
, mouseEvent
=mouseEvent
)
152 pygame
.event
.post(evt
)
154 def getVelocity(self
, pos
) :
155 vel
= (float(self
.dispWidth
) - pos
[1]) / self
.dispWidth
156 vel
= int(vel
* (MIDI_VELOCITY_RANGE
[1] - MIDI_VELOCITY_RANGE
[0])) + MIDI_VELOCITY_RANGE
[0]
159 def getPan(self
, index
) :
160 pan
= float(index
) / (self
.keyboardLength
-1)
161 pan
= int(pan
* (MIDI_PAN_RANGE
[1] - MIDI_PAN_RANGE
[0])) + MIDI_PAN_RANGE
[0]
164 def playnote(self
, col
, pos
, vel
=None) :
165 pan
= self
.getPan(col
.index
)
166 self
.synth
.cc(0, 10, pan
)
167 vel
= vel
or self
.getVelocity(pos
)
168 self
.synth
.noteon(0, col
.tone
.midi
, vel
)
170 class PlayingScreen(PlayingScreenBase
) :
171 "fenêtre de jeu pour improvisation"
173 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
175 def __init__(self
, synth
, displayNotes
=True) :
177 self
.currentColumn
= None
178 for midi
in self
.scale
:
180 distinctNotes
.append(tone
)
182 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
, displayNotes
=displayNotes
)
184 @event_handler(events
.COLDOWN
)
185 def noteon(self
, event
) :
188 self
.currentColumn
= col
189 self
.playnote(col
, event
.pos
)
191 @event_handler(events
.COLUP
)
192 def noteoff(self
, event
) :
193 if self
.currentColumn
:
194 self
.currentColumn
.update(False)
195 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
198 class SongPlayingScreen(PlayingScreenBase
) :
200 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['NORMAL'], displayNotes
=True, tempoTrim
=0) :
201 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
, displayNotes
=displayNotes
)
203 self
.quarterNoteDuration
= song
.quarterNoteDuration
204 self
.tempoTrim
= tempoTrim
205 self
.currentColumn
= None
206 self
.noteIterator
= self
.song
.iterNotes()
208 self
._plugListeners
(mode
)
210 def _plugListeners(self
, mode
) :
211 "initialisation des gestionnaires d'événements en fonction du mode"
213 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
214 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
216 elif mode
== PLAYING_MODES_DICT
['EASY'] :
217 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
218 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
220 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
221 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
222 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
223 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
225 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
226 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
227 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
229 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
230 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
231 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
234 # --- HID listeners ---
235 def handleBeginnerColumnOver(self
, event
) :
237 if col
.state
and not self
.currentNotePlayed
:
238 self
.playnote(col
, event
.pos
)
239 self
.setNoteTimeout()
240 self
.currentNotePlayed
= True
242 def handleEasyColumnOver(self
, event
) :
245 self
.cursor
.pressed
and \
246 not self
.currentNotePlayed
:
247 self
.playnote(col
, event
.pos
)
248 self
.setNoteTimeout()
249 self
.currentNotePlayed
= True
252 def handleNormalColumnOver(self
, event
) :
255 self
.cursor
.pressed
and \
256 not self
.currentNotePlayed
:
257 self
.playnote(col
, event
.pos
)
258 self
.currentNotePlayed
= True
260 def handleColumnDown(self
, event
) :
263 self
.playnote(col
, event
.pos
)
264 self
.currentNotePlayed
= True
266 def handleEasyColumnDown(self
, event
) :
269 not self
.currentNotePlayed
:
270 self
.playnote(col
, event
.pos
)
271 self
.setNoteTimeout()
272 self
.currentNotePlayed
= True
275 def handleExpertColumnDown(self
, event
) :
278 self
.playnote(col
, event
.pos
)
279 self
.currentNotePlayed
= True
281 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
283 self
.playnote(col
, event
.pos
, vel
=vel
)
284 self
.alternateColumn
= col
285 self
.currentNotePlayed
= False
287 def handleColumnUp(self
, event
) :
288 if self
.currentNotePlayed
:
289 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
292 def handleExpertColumnUp(self
, event
) :
293 if self
.currentNotePlayed
:
294 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
297 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
299 # --- End HID listeners ---
302 def displayNext(self
, event
=None) :
303 if self
.currentColumn
:
304 self
.currentColumn
.update(False)
306 note
, verseIndex
= self
.noteIterator
.next()
307 except StopIteration :
308 self
.noteIterator
= self
.song
.iterNotes()
309 note
, verseIndex
= self
.noteIterator
.next()
310 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
312 syllabus
= note
.lyrics
[verseIndex
].syllabus()
316 column
= self
.columns
[note
.midi
]
317 column
.update(True, syllabus
)
318 self
.currentColumn
= column
319 self
.currentNote
= note
320 self
.currentNotePlayed
= False
322 @event_handler(events
.NOTEEND
)
323 def clearTimeOutAndDisplayNext(self
, evt
) :
324 pygame
.time
.set_timer(evt
.type, 0)
325 self
.synth
.noteoff(0, self
.currentNote
.midi
)
328 def setNoteTimeout(self
) :
329 delay
= self
.currentNote
.duration
* self
.quarterNoteDuration
330 delay
= delay
+ delay
* self
.tempoTrim
333 delay
= 1 # durée minimale, car 0 désactiverait le timer.
334 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
336 def tempoTrimUp(self
, step
=0.1) :
337 self
.tempoTrim
= round(self
.tempoTrim
- step
, 1)
339 def tempoTrimDown(self
, step
=0.1) :
340 self
.tempoTrim
= round(self
.tempoTrim
+ step
, 1)
343 pygame
.time
.set_timer(events
.NOTEEND
, 0)
344 super(SongPlayingScreen
, self
).stop()