1 # -*- coding: utf-8 -*-
4 bandes arc-en-ciel représentant un clavier.
12 import minwii
.events
as events
13 from minwii
.log
import eventLogger
14 from minwii
.eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
15 from minwii
.musicxml
import Tone
16 from minwii
.config
import FRAMERATE
17 from minwii
.config
import FIRST_HUE
18 from minwii
.config
import MIDI_VELOCITY_RANGE
19 from minwii
.config
import MIDI_PAN_RANGE
20 from minwii
.config
import MIDI_VELOCITY_WRONG_NOTE_ATTN
21 from minwii
.globals import BACKGROUND_LAYER
22 from minwii
.globals import CURSOR_LAYER
23 from minwii
.globals import PLAYING_MODES_DICT
25 from cursors
import WarpingCursor
26 from column
import Column
28 class PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
30 def __init__(self
, synth
, distinctNotes
=[]) :
32 distinctNotes : notes disctinctes présentes dans la chanson
33 triées du plus grave au plus aigu.
35 super(PlayingScreenBase
, self
).__init
__()
37 self
.distinctNotes
= distinctNotes
38 self
.keyboardLength
= 0
39 self
.keyboardRects
= []
45 self
.draw(pygame
.display
.get_surface())
48 def _initRects(self
) :
49 """ création des espaces réservés pour
50 afficher les colonnes.
52 self
.keyboardLength
= len(self
.distinctNotes
)
54 screen
= pygame
.display
.get_surface()
56 self
.dispWidth
= dispWidth
= screen
.get_width()
57 self
.dispHeight
= dispHeight
= screen
.get_height()
59 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
62 for i
in range(self
.keyboardLength
- 1) :
63 upperLeftCorner
= (i
*columnWidth
, 0)
64 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
67 # la dernière colonne à la largeur du reste
68 upperLeftCorner
= ((i
+1) * columnWidth
, 0)
69 rect
= pygame
.Rect(upperLeftCorner
, (dispWidth
- (self
.keyboardLength
- 1) * columnWidth
, dispHeight
))
72 self
.keyboardRects
= rects
74 def _initColumns(self
) :
76 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
77 for i
, rect
in enumerate(self
.keyboardRects
) :
78 hue
= FIRST_HUE
- hueStep
* i
79 tone
= self
.distinctNotes
[i
]
80 c
= Column(self
, i
, hue
, rect
, tone
)
81 self
.add(c
, layer
=BACKGROUND_LAYER
)
82 self
.columns
[tone
.midi
] = c
85 def _initCursor(self
) :
86 self
.cursor
= WarpingCursor(blinkMode
=True)
87 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
91 clock
= pygame
.time
.Clock()
93 pygame
.mouse
.set_visible(False)
95 EventDispatcher
.dispatchEvents()
96 dirty
= self
.draw(pygame
.display
.get_surface())
97 pygame
.display
.update(dirty
)
101 self
._running
= False
102 self
.synth
.system_reset()
103 pygame
.mouse
.set_visible(True)
104 self
.cursor
._stopBlink
()
106 @event_handler(pygame
.KEYDOWN
)
107 def handleKeyDown(self
, event
) :
108 if event
.key
== pygame
.K_q
or \
109 event
.unicode == u
'q' or \
113 @event_handler(pygame
.MOUSEBUTTONDOWN
)
114 def onMouseDown(self
, event
) :
115 # TODO à cleaner : on vire le dernier élément
116 # parce qu'il s'agit du curseur
117 for col
in reversed(self
.sprites()[:-1]) :
118 if col
.rect
.collidepoint(*event
.pos
):
119 self
.raiseColDown(col
, event
)
122 @event_handler(pygame
.MOUSEBUTTONUP
)
123 def onMouseUp(self
, event
) :
124 for col
in reversed(self
.sprites()[:-1]) :
125 if col
.rect
.collidepoint(*event
.pos
) :
126 self
.raiseColUp(col
, event
)
129 @event_handler(pygame
.MOUSEMOTION
)
130 def onMouseMove(self
, event
) :
131 for col
in reversed(self
.sprites()[:-1]) :
132 if col
.rect
.collidepoint(*event
.pos
) :
133 self
.raiseColOver(col
, event
)
136 def raiseColDown(self
, col
, mouseEvent
) :
137 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=mouseEvent
.pos
)
138 pygame
.event
.post(evt
)
140 def raiseColUp(self
, col
, mouseEvent
) :
141 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=mouseEvent
.pos
)
142 pygame
.event
.post(evt
)
144 def raiseColOver(self
, col
, mouseEvent
) :
145 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=mouseEvent
.pos
, mouseEvent
=mouseEvent
)
146 pygame
.event
.post(evt
)
148 def getVelocity(self
, pos
) :
149 vel
= (float(self
.dispWidth
) - pos
[1]) / self
.dispWidth
150 vel
= int(vel
* (MIDI_VELOCITY_RANGE
[1] - MIDI_VELOCITY_RANGE
[0])) + MIDI_VELOCITY_RANGE
[0]
153 def getPan(self
, index
) :
154 pan
= float(index
) / (self
.keyboardLength
-1)
155 pan
= int(pan
* (MIDI_PAN_RANGE
[1] - MIDI_PAN_RANGE
[0])) + MIDI_PAN_RANGE
[0]
158 def playnote(self
, col
, pos
, vel
=None) :
159 pan
= self
.getPan(col
.index
)
160 self
.synth
.cc(0, 10, pan
)
161 vel
= vel
or self
.getVelocity(pos
)
162 self
.synth
.noteon(0, col
.tone
.midi
, vel
)
164 class PlayingScreen(PlayingScreenBase
) :
165 "fenêtre de jeu pour improvisation"
167 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
169 def __init__(self
, synth
) :
171 self
.currentColumn
= None
172 for midi
in self
.scale
:
174 distinctNotes
.append(tone
)
176 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
178 @event_handler(events
.COLDOWN
)
179 def noteon(self
, event
) :
182 self
.currentColumn
= col
183 self
.playnote(col
, event
.pos
)
185 @event_handler(events
.COLUP
)
186 def noteoff(self
, event
) :
187 if self
.currentColumn
:
188 self
.currentColumn
.update(False)
189 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
192 class SongPlayingScreen(PlayingScreenBase
) :
194 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['NORMAL'], tempoTrim
=0) :
195 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
197 self
.quarterNoteDuration
= song
.quarterNoteDuration
198 self
.tempoTrim
= tempoTrim
199 self
.currentColumn
= None
200 self
.noteIterator
= self
.song
.iterNotes()
202 self
._plugListeners
(mode
)
204 def _plugListeners(self
, mode
) :
205 "initialisation des gestionnaires d'événements en fonction du mode"
207 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
208 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
210 elif mode
== PLAYING_MODES_DICT
['EASY'] :
211 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
212 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
214 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
215 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
216 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
217 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
219 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
220 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
221 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
223 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
224 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
225 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
228 # --- HID listeners ---
229 def handleBeginnerColumnOver(self
, event
) :
231 if col
.state
and not self
.currentNotePlayed
:
232 self
.playnote(col
, event
.pos
)
233 self
.setNoteTimeout()
234 self
.currentNotePlayed
= True
236 def handleEasyColumnOver(self
, event
) :
239 self
.cursor
.pressed
and \
240 not self
.currentNotePlayed
:
241 self
.playnote(col
, event
.pos
)
242 self
.setNoteTimeout()
243 self
.currentNotePlayed
= True
246 def handleNormalColumnOver(self
, event
) :
249 self
.cursor
.pressed
and \
250 not self
.currentNotePlayed
:
251 self
.playnote(col
, event
.pos
)
252 self
.currentNotePlayed
= True
254 def handleColumnDown(self
, event
) :
257 self
.playnote(col
, event
.pos
)
258 self
.currentNotePlayed
= True
260 def handleEasyColumnDown(self
, event
) :
263 not self
.currentNotePlayed
:
264 self
.playnote(col
, event
.pos
)
265 self
.setNoteTimeout()
266 self
.currentNotePlayed
= True
269 def handleExpertColumnDown(self
, event
) :
272 self
.playnote(col
, event
.pos
)
273 self
.currentNotePlayed
= True
275 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
277 self
.playnote(col
, event
.pos
, vel
=vel
)
278 self
.alternateColumn
= col
279 self
.currentNotePlayed
= False
281 def handleColumnUp(self
, event
) :
282 if self
.currentNotePlayed
:
283 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
286 def handleExpertColumnUp(self
, event
) :
287 if self
.currentNotePlayed
:
288 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
291 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
293 # --- End HID listeners ---
296 def displayNext(self
, event
=None) :
297 if self
.currentColumn
:
298 self
.currentColumn
.update(False)
300 note
, verseIndex
= self
.noteIterator
.next()
301 except StopIteration :
302 self
.noteIterator
= self
.song
.iterNotes()
303 note
, verseIndex
= self
.noteIterator
.next()
304 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
306 syllabus
= note
.lyrics
[verseIndex
].syllabus()
310 column
= self
.columns
[note
.midi
]
311 column
.update(True, syllabus
)
312 self
.currentColumn
= column
313 self
.currentNote
= note
314 self
.currentNotePlayed
= False
316 @event_handler(events
.NOTEEND
)
317 def clearTimeOutAndDisplayNext(self
, evt
) :
318 pygame
.time
.set_timer(evt
.type, 0)
319 self
.synth
.noteoff(0, self
.currentNote
.midi
)
322 def setNoteTimeout(self
) :
323 delay
= self
.currentNote
.duration
* self
.quarterNoteDuration
324 delay
= delay
+ delay
* self
.tempoTrim
325 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
327 def tempoTrimUp(self
, step
=0.1) :
328 self
.tempoTrim
= round(self
.tempoTrim
- step
, 1)
330 def tempoTrimDown(self
, step
=0.1) :
331 self
.tempoTrim
= round(self
.tempoTrim
+ step
, 1)
334 pygame
.time
.set_timer(events
.NOTEEND
, 0)
335 super(SongPlayingScreen
, self
).stop()