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']) :
195 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
197 self
.quarterNoteDuration
= song
.quarterNoteDuration
198 self
.currentColumn
= None
199 self
.noteIterator
= self
.song
.iterNotes()
201 self
._plugListeners
(mode
)
203 def _plugListeners(self
, mode
) :
204 "initialisation des gestionnaires d'événements en fonction du mode"
206 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
207 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
209 elif mode
== PLAYING_MODES_DICT
['EASY'] :
210 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
211 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
213 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
214 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
215 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
216 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
218 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
219 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
220 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
222 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
223 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
224 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
227 # --- HID listeners ---
228 def handleBeginnerColumnOver(self
, event
) :
230 if col
.state
and not self
.currentNotePlayed
:
231 self
.playnote(col
, event
.pos
)
232 SongPlayingScreen
.setNoteTimeout(
233 int(self
.currentNote
.duration
* \
234 self
.quarterNoteDuration
)
236 self
.currentNotePlayed
= True
238 def handleEasyColumnOver(self
, event
) :
241 self
.cursor
.pressed
and \
242 not self
.currentNotePlayed
:
243 self
.playnote(col
, event
.pos
)
244 SongPlayingScreen
.setNoteTimeout(
245 int(self
.currentNote
.duration
* \
246 self
.quarterNoteDuration
)
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 SongPlayingScreen
.setNoteTimeout(
271 int(self
.currentNote
.duration
* \
272 self
.quarterNoteDuration
)
274 self
.currentNotePlayed
= True
277 def handleExpertColumnDown(self
, event
) :
280 self
.playnote(col
, event
.pos
)
281 self
.currentNotePlayed
= True
283 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
285 self
.playnote(col
, event
.pos
, vel
=vel
)
286 self
.alternateColumn
= col
287 self
.currentNotePlayed
= False
289 def handleColumnUp(self
, event
) :
290 if self
.currentNotePlayed
:
291 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
294 def handleExpertColumnUp(self
, event
) :
295 if self
.currentNotePlayed
:
296 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
299 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
301 # --- End HID listeners ---
304 def displayNext(self
, event
=None) :
305 if self
.currentColumn
:
306 self
.currentColumn
.update(False)
308 note
, verseIndex
= self
.noteIterator
.next()
309 except StopIteration :
310 self
.noteIterator
= self
.song
.iterNotes()
311 note
, verseIndex
= self
.noteIterator
.next()
312 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
314 syllabus
= note
.lyrics
[verseIndex
].syllabus()
318 column
= self
.columns
[note
.midi
]
319 column
.update(True, syllabus
)
320 self
.currentColumn
= column
321 self
.currentNote
= note
322 self
.currentNotePlayed
= False
324 @event_handler(events
.NOTEEND
)
325 def clearTimeOutAndDisplayNext(self
, evt
) :
326 pygame
.time
.set_timer(evt
.type, 0)
327 self
.synth
.noteoff(0, self
.currentNote
.midi
)
331 def setNoteTimeout(delay
) :
332 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
335 pygame
.time
.set_timer(events
.NOTEEND
, 0)
336 super(SongPlayingScreen
, self
).stop()