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
.globals import BACKGROUND_LAYER
24 from minwii
.globals import CURSOR_LAYER
25 from minwii
.globals import PLAYING_MODES_DICT
27 from cursors
import WarpingCursor
28 from column
import Column
30 class PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
32 def __init__(self
, synth
, distinctNotes
=[], displayNotes
=True) :
34 distinctNotes : notes disctinctes présentes dans la chanson
35 triées du plus grave au plus aigu.
37 super(PlayingScreenBase
, self
).__init
__()
39 self
.distinctNotes
= distinctNotes
40 self
.displayNotes
= displayNotes
41 self
.keyboardLength
= 0
42 self
.keyboardRects
= []
48 self
.kinectRgb
= kinect
.RGBSprite(alpha
=128)
49 self
.add(self
.kinectRgb
, layer
=CURSOR_LAYER
)
53 def _initRects(self
) :
54 """ création des espaces réservés pour
55 afficher les colonnes.
57 self
.keyboardLength
= len(self
.distinctNotes
)
59 screen
= pygame
.display
.get_surface()
61 self
.dispWidth
= dispWidth
= screen
.get_width()
62 self
.dispHeight
= dispHeight
= screen
.get_height()
64 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
67 for i
in range(self
.keyboardLength
- 1) :
68 upperLeftCorner
= (i
*columnWidth
, 0)
69 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
72 # la dernière colonne à la largeur du reste
73 upperLeftCorner
= ((i
+1) * columnWidth
, 0)
74 rect
= pygame
.Rect(upperLeftCorner
, (dispWidth
- (self
.keyboardLength
- 1) * columnWidth
, dispHeight
))
77 self
.keyboardRects
= rects
79 def _initColumns(self
) :
81 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
82 for i
, rect
in enumerate(self
.keyboardRects
) :
83 hue
= FIRST_HUE
- hueStep
* i
84 tone
= self
.distinctNotes
[i
]
85 c
= Column(self
, i
, hue
, rect
, tone
, displayNote
=self
.displayNotes
)
86 self
.add(c
, layer
=BACKGROUND_LAYER
)
87 self
.columns
[tone
.midi
] = c
90 def _initCursor(self
) :
91 self
.cursor
= WarpingCursor(blinkMode
=True)
92 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
96 clock
= pygame
.time
.Clock()
98 pygame
.mouse
.set_visible(False)
100 EventDispatcher
.dispatchEvents()
101 self
.kinectRgb
.update()
102 dirty
= self
.draw(pygame
.display
.get_surface())
103 pygame
.display
.update(dirty
)
104 clock
.tick(FRAMERATE
)
107 self
._running
= False
108 self
.synth
.system_reset()
109 pygame
.mouse
.set_visible(True)
110 self
.cursor
._stopBlink
()
112 @event_handler(pygame
.KEYDOWN
)
113 def handleKeyDown(self
, event
) :
114 if event
.key
in (pygame
.K_q
, pygame
.K_ESCAPE
) or \
115 event
.unicode == u
'q' :
118 @event_handler(pygame
.MOUSEBUTTONDOWN
)
119 def onMouseDown(self
, event
) :
120 # TODO à cleaner : on vire le dernier élément
121 # parce qu'il s'agit du curseur
122 for col
in reversed(self
.sprites()[:-1]) :
123 if col
.rect
.collidepoint(*event
.pos
):
124 self
.raiseColDown(col
, event
)
127 @event_handler(pygame
.MOUSEBUTTONUP
)
128 def onMouseUp(self
, event
) :
129 for col
in reversed(self
.sprites()[:-1]) :
130 if col
.rect
.collidepoint(*event
.pos
) :
131 self
.raiseColUp(col
, event
)
134 @event_handler(pygame
.MOUSEMOTION
)
135 def onMouseMove(self
, event
) :
136 for col
in reversed(self
.sprites()[:-1]) :
137 if col
.rect
.collidepoint(*event
.pos
) :
138 self
.raiseColOver(col
, event
)
141 def raiseColDown(self
, col
, mouseEvent
) :
142 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=mouseEvent
.pos
)
143 pygame
.event
.post(evt
)
145 def raiseColUp(self
, col
, mouseEvent
) :
146 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=mouseEvent
.pos
)
147 pygame
.event
.post(evt
)
149 def raiseColOver(self
, col
, mouseEvent
) :
150 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=mouseEvent
.pos
, mouseEvent
=mouseEvent
)
151 pygame
.event
.post(evt
)
153 def getVelocity(self
, pos
) :
154 vel
= (float(self
.dispWidth
) - pos
[1]) / self
.dispWidth
155 vel
= int(vel
* (MIDI_VELOCITY_RANGE
[1] - MIDI_VELOCITY_RANGE
[0])) + MIDI_VELOCITY_RANGE
[0]
158 def getPan(self
, index
) :
159 pan
= float(index
) / (self
.keyboardLength
-1)
160 pan
= int(pan
* (MIDI_PAN_RANGE
[1] - MIDI_PAN_RANGE
[0])) + MIDI_PAN_RANGE
[0]
163 def playnote(self
, col
, pos
, vel
=None) :
164 pan
= self
.getPan(col
.index
)
165 self
.synth
.cc(0, 10, pan
)
166 vel
= vel
or self
.getVelocity(pos
)
167 self
.synth
.noteon(0, col
.tone
.midi
, vel
)
169 class PlayingScreen(PlayingScreenBase
) :
170 "fenêtre de jeu pour improvisation"
172 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
174 def __init__(self
, synth
, displayNotes
=True) :
176 self
.currentColumn
= None
177 for midi
in self
.scale
:
179 distinctNotes
.append(tone
)
181 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
, displayNotes
=displayNotes
)
183 @event_handler(events
.COLDOWN
)
184 def noteon(self
, event
) :
187 self
.currentColumn
= col
188 self
.playnote(col
, event
.pos
)
190 @event_handler(events
.COLUP
)
191 def noteoff(self
, event
) :
192 if self
.currentColumn
:
193 self
.currentColumn
.update(False)
194 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
197 class SongPlayingScreen(PlayingScreenBase
) :
199 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['NORMAL'], displayNotes
=True, tempoTrim
=0) :
200 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
, displayNotes
=displayNotes
)
202 self
.quarterNoteDuration
= song
.quarterNoteDuration
203 self
.tempoTrim
= tempoTrim
204 self
.currentColumn
= None
205 self
.noteIterator
= self
.song
.iterNotes()
207 self
._plugListeners
(mode
)
209 def _plugListeners(self
, mode
) :
210 "initialisation des gestionnaires d'événements en fonction du mode"
212 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
213 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
215 elif mode
== PLAYING_MODES_DICT
['EASY'] :
216 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
217 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
219 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
220 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
221 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
222 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
224 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
225 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
226 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
228 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
229 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
230 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
233 # --- HID listeners ---
234 def handleBeginnerColumnOver(self
, event
) :
236 if col
.state
and not self
.currentNotePlayed
:
237 self
.playnote(col
, event
.pos
)
238 self
.setNoteTimeout()
239 self
.currentNotePlayed
= True
241 def handleEasyColumnOver(self
, event
) :
244 self
.cursor
.pressed
and \
245 not self
.currentNotePlayed
:
246 self
.playnote(col
, event
.pos
)
247 self
.setNoteTimeout()
248 self
.currentNotePlayed
= True
251 def handleNormalColumnOver(self
, event
) :
254 self
.cursor
.pressed
and \
255 not self
.currentNotePlayed
:
256 self
.playnote(col
, event
.pos
)
257 self
.currentNotePlayed
= True
259 def handleColumnDown(self
, event
) :
262 self
.playnote(col
, event
.pos
)
263 self
.currentNotePlayed
= True
265 def handleEasyColumnDown(self
, event
) :
268 not self
.currentNotePlayed
:
269 self
.playnote(col
, event
.pos
)
270 self
.setNoteTimeout()
271 self
.currentNotePlayed
= True
274 def handleExpertColumnDown(self
, event
) :
277 self
.playnote(col
, event
.pos
)
278 self
.currentNotePlayed
= True
280 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
282 self
.playnote(col
, event
.pos
, vel
=vel
)
283 self
.alternateColumn
= col
284 self
.currentNotePlayed
= False
286 def handleColumnUp(self
, event
) :
287 if self
.currentNotePlayed
:
288 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
291 def handleExpertColumnUp(self
, event
) :
292 if self
.currentNotePlayed
:
293 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
296 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
298 # --- End HID listeners ---
301 def displayNext(self
, event
=None) :
302 if self
.currentColumn
:
303 self
.currentColumn
.update(False)
305 note
, verseIndex
= self
.noteIterator
.next()
306 except StopIteration :
307 self
.noteIterator
= self
.song
.iterNotes()
308 note
, verseIndex
= self
.noteIterator
.next()
309 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
311 syllabus
= note
.lyrics
[verseIndex
].syllabus()
315 column
= self
.columns
[note
.midi
]
316 column
.update(True, syllabus
)
317 self
.currentColumn
= column
318 self
.currentNote
= note
319 self
.currentNotePlayed
= False
321 @event_handler(events
.NOTEEND
)
322 def clearTimeOutAndDisplayNext(self
, evt
) :
323 pygame
.time
.set_timer(evt
.type, 0)
324 self
.synth
.noteoff(0, self
.currentNote
.midi
)
327 def setNoteTimeout(self
) :
328 delay
= self
.currentNote
.duration
* self
.quarterNoteDuration
329 delay
= delay
+ delay
* self
.tempoTrim
332 delay
= 1 # durée minimale, car 0 désactiverait le timer.
333 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
335 def tempoTrimUp(self
, step
=0.1) :
336 self
.tempoTrim
= round(self
.tempoTrim
- step
, 1)
338 def tempoTrimDown(self
, step
=0.1) :
339 self
.tempoTrim
= round(self
.tempoTrim
+ step
, 1)
342 pygame
.time
.set_timer(events
.NOTEEND
, 0)
343 super(SongPlayingScreen
, self
).stop()