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
=[], displayNotes
=True) :
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
.displayNotes
= displayNotes
39 self
.keyboardLength
= 0
40 self
.keyboardRects
= []
46 self
.draw(pygame
.display
.get_surface())
49 def _initRects(self
) :
50 """ création des espaces réservés pour
51 afficher les colonnes.
53 self
.keyboardLength
= len(self
.distinctNotes
)
55 screen
= pygame
.display
.get_surface()
57 self
.dispWidth
= dispWidth
= screen
.get_width()
58 self
.dispHeight
= dispHeight
= screen
.get_height()
60 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
63 for i
in range(self
.keyboardLength
- 1) :
64 upperLeftCorner
= (i
*columnWidth
, 0)
65 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
68 # la dernière colonne à la largeur du reste
69 upperLeftCorner
= ((i
+1) * columnWidth
, 0)
70 rect
= pygame
.Rect(upperLeftCorner
, (dispWidth
- (self
.keyboardLength
- 1) * columnWidth
, dispHeight
))
73 self
.keyboardRects
= rects
75 def _initColumns(self
) :
77 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
78 for i
, rect
in enumerate(self
.keyboardRects
) :
79 hue
= FIRST_HUE
- hueStep
* i
80 tone
= self
.distinctNotes
[i
]
81 c
= Column(self
, i
, hue
, rect
, tone
, displayNote
=self
.displayNotes
)
82 self
.add(c
, layer
=BACKGROUND_LAYER
)
83 self
.columns
[tone
.midi
] = c
86 def _initCursor(self
) :
87 self
.cursor
= WarpingCursor(blinkMode
=True)
88 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
92 clock
= pygame
.time
.Clock()
94 pygame
.mouse
.set_visible(False)
96 EventDispatcher
.dispatchEvents()
97 dirty
= self
.draw(pygame
.display
.get_surface())
98 pygame
.display
.update(dirty
)
102 self
._running
= False
103 self
.synth
.system_reset()
104 pygame
.mouse
.set_visible(True)
105 self
.cursor
._stopBlink
()
107 @event_handler(pygame
.KEYDOWN
)
108 def handleKeyDown(self
, event
) :
109 if event
.key
== pygame
.K_q
or \
110 event
.unicode == u
'q' or \
114 @event_handler(pygame
.MOUSEBUTTONDOWN
)
115 def onMouseDown(self
, event
) :
116 # TODO à cleaner : on vire le dernier élément
117 # parce qu'il s'agit du curseur
118 for col
in reversed(self
.sprites()[:-1]) :
119 if col
.rect
.collidepoint(*event
.pos
):
120 self
.raiseColDown(col
, event
)
123 @event_handler(pygame
.MOUSEBUTTONUP
)
124 def onMouseUp(self
, event
) :
125 for col
in reversed(self
.sprites()[:-1]) :
126 if col
.rect
.collidepoint(*event
.pos
) :
127 self
.raiseColUp(col
, event
)
130 @event_handler(pygame
.MOUSEMOTION
)
131 def onMouseMove(self
, event
) :
132 for col
in reversed(self
.sprites()[:-1]) :
133 if col
.rect
.collidepoint(*event
.pos
) :
134 self
.raiseColOver(col
, event
)
137 def raiseColDown(self
, col
, mouseEvent
) :
138 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=mouseEvent
.pos
)
139 pygame
.event
.post(evt
)
141 def raiseColUp(self
, col
, mouseEvent
) :
142 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=mouseEvent
.pos
)
143 pygame
.event
.post(evt
)
145 def raiseColOver(self
, col
, mouseEvent
) :
146 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=mouseEvent
.pos
, mouseEvent
=mouseEvent
)
147 pygame
.event
.post(evt
)
149 def getVelocity(self
, pos
) :
150 vel
= (float(self
.dispWidth
) - pos
[1]) / self
.dispWidth
151 vel
= int(vel
* (MIDI_VELOCITY_RANGE
[1] - MIDI_VELOCITY_RANGE
[0])) + MIDI_VELOCITY_RANGE
[0]
154 def getPan(self
, index
) :
155 pan
= float(index
) / (self
.keyboardLength
-1)
156 pan
= int(pan
* (MIDI_PAN_RANGE
[1] - MIDI_PAN_RANGE
[0])) + MIDI_PAN_RANGE
[0]
159 def playnote(self
, col
, pos
, vel
=None) :
160 pan
= self
.getPan(col
.index
)
161 self
.synth
.cc(0, 10, pan
)
162 vel
= vel
or self
.getVelocity(pos
)
163 self
.synth
.noteon(0, col
.tone
.midi
, vel
)
165 class PlayingScreen(PlayingScreenBase
) :
166 "fenêtre de jeu pour improvisation"
168 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
170 def __init__(self
, synth
, displayNotes
=True) :
172 self
.currentColumn
= None
173 for midi
in self
.scale
:
175 distinctNotes
.append(tone
)
177 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
, displayNotes
=displayNotes
)
179 @event_handler(events
.COLDOWN
)
180 def noteon(self
, event
) :
183 self
.currentColumn
= col
184 self
.playnote(col
, event
.pos
)
186 @event_handler(events
.COLUP
)
187 def noteoff(self
, event
) :
188 if self
.currentColumn
:
189 self
.currentColumn
.update(False)
190 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
193 class SongPlayingScreen(PlayingScreenBase
) :
195 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['NORMAL'], displayNotes
=True, tempoTrim
=0) :
196 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
, displayNotes
=displayNotes
)
198 self
.quarterNoteDuration
= song
.quarterNoteDuration
199 self
.tempoTrim
= tempoTrim
200 self
.currentColumn
= None
201 self
.noteIterator
= self
.song
.iterNotes()
203 self
._plugListeners
(mode
)
205 def _plugListeners(self
, mode
) :
206 "initialisation des gestionnaires d'événements en fonction du mode"
208 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
209 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
211 elif mode
== PLAYING_MODES_DICT
['EASY'] :
212 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
213 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
215 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
216 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
217 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
218 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
220 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
221 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
222 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
224 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
225 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
226 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
229 # --- HID listeners ---
230 def handleBeginnerColumnOver(self
, event
) :
232 if col
.state
and not self
.currentNotePlayed
:
233 self
.playnote(col
, event
.pos
)
234 self
.setNoteTimeout()
235 self
.currentNotePlayed
= True
237 def handleEasyColumnOver(self
, event
) :
240 self
.cursor
.pressed
and \
241 not self
.currentNotePlayed
:
242 self
.playnote(col
, event
.pos
)
243 self
.setNoteTimeout()
244 self
.currentNotePlayed
= True
247 def handleNormalColumnOver(self
, event
) :
250 self
.cursor
.pressed
and \
251 not self
.currentNotePlayed
:
252 self
.playnote(col
, event
.pos
)
253 self
.currentNotePlayed
= True
255 def handleColumnDown(self
, event
) :
258 self
.playnote(col
, event
.pos
)
259 self
.currentNotePlayed
= True
261 def handleEasyColumnDown(self
, event
) :
264 not self
.currentNotePlayed
:
265 self
.playnote(col
, event
.pos
)
266 self
.setNoteTimeout()
267 self
.currentNotePlayed
= True
270 def handleExpertColumnDown(self
, event
) :
273 self
.playnote(col
, event
.pos
)
274 self
.currentNotePlayed
= True
276 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
278 self
.playnote(col
, event
.pos
, vel
=vel
)
279 self
.alternateColumn
= col
280 self
.currentNotePlayed
= False
282 def handleColumnUp(self
, event
) :
283 if self
.currentNotePlayed
:
284 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
287 def handleExpertColumnUp(self
, event
) :
288 if self
.currentNotePlayed
:
289 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
292 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
294 # --- End HID listeners ---
297 def displayNext(self
, event
=None) :
298 if self
.currentColumn
:
299 self
.currentColumn
.update(False)
301 note
, verseIndex
= self
.noteIterator
.next()
302 except StopIteration :
303 self
.noteIterator
= self
.song
.iterNotes()
304 note
, verseIndex
= self
.noteIterator
.next()
305 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
307 syllabus
= note
.lyrics
[verseIndex
].syllabus()
311 column
= self
.columns
[note
.midi
]
312 column
.update(True, syllabus
)
313 self
.currentColumn
= column
314 self
.currentNote
= note
315 self
.currentNotePlayed
= False
317 @event_handler(events
.NOTEEND
)
318 def clearTimeOutAndDisplayNext(self
, evt
) :
319 pygame
.time
.set_timer(evt
.type, 0)
320 self
.synth
.noteoff(0, self
.currentNote
.midi
)
323 def setNoteTimeout(self
) :
324 delay
= self
.currentNote
.duration
* self
.quarterNoteDuration
325 delay
= delay
+ delay
* self
.tempoTrim
327 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
329 def tempoTrimUp(self
, step
=0.1) :
330 self
.tempoTrim
= round(self
.tempoTrim
- step
, 1)
332 def tempoTrimDown(self
, step
=0.1) :
333 self
.tempoTrim
= round(self
.tempoTrim
+ step
, 1)
336 pygame
.time
.set_timer(events
.NOTEEND
, 0)
337 super(SongPlayingScreen
, self
).stop()