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
) :
63 upperLeftCorner
= (i
*columnWidth
, 0)
64 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
67 self
.keyboardRects
= rects
69 def _initColumns(self
) :
71 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
72 for i
, rect
in enumerate(self
.keyboardRects
) :
73 hue
= FIRST_HUE
- hueStep
* i
74 tone
= self
.distinctNotes
[i
]
75 c
= Column(self
, i
, hue
, rect
, tone
)
76 self
.add(c
, layer
=BACKGROUND_LAYER
)
77 self
.columns
[tone
.midi
] = c
80 def _initCursor(self
) :
81 self
.cursor
= WarpingCursor(blinkMode
=True)
82 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
86 clock
= pygame
.time
.Clock()
88 pygame
.mouse
.set_visible(False)
90 EventDispatcher
.dispatchEvents()
91 dirty
= self
.draw(pygame
.display
.get_surface())
92 pygame
.display
.update(dirty
)
97 self
.synth
.system_reset()
98 pygame
.mouse
.set_visible(True)
99 self
.cursor
._stopBlink
()
101 @event_handler(pygame
.KEYDOWN
)
102 def handleKeyDown(self
, event
) :
103 if event
.key
== pygame
.K_q
or event
.unicode == u
'q':
106 @event_handler(pygame
.MOUSEBUTTONDOWN
)
107 def onMouseDown(self
, event
) :
108 # TODO à cleaner : on vire le dernier élément
109 # parce qu'il s'agit du curseur
110 for col
in reversed(self
.sprites()[:-1]) :
111 if col
.rect
.collidepoint(*event
.pos
):
112 self
.raiseColDown(col
, event
)
115 @event_handler(pygame
.MOUSEBUTTONUP
)
116 def onMouseUp(self
, event
) :
117 for col
in reversed(self
.sprites()[:-1]) :
118 if col
.rect
.collidepoint(*event
.pos
) :
119 self
.raiseColUp(col
, event
)
122 @event_handler(pygame
.MOUSEMOTION
)
123 def onMouseMove(self
, event
) :
124 for col
in reversed(self
.sprites()[:-1]) :
125 if col
.rect
.collidepoint(*event
.pos
) :
126 self
.raiseColOver(col
, event
)
129 def raiseColDown(self
, col
, mouseEvent
) :
130 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=mouseEvent
.pos
)
131 pygame
.event
.post(evt
)
133 def raiseColUp(self
, col
, mouseEvent
) :
134 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=mouseEvent
.pos
)
135 pygame
.event
.post(evt
)
137 def raiseColOver(self
, col
, mouseEvent
) :
138 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=mouseEvent
.pos
, mouseEvent
=mouseEvent
)
139 pygame
.event
.post(evt
)
141 def getVelocity(self
, pos
) :
142 vel
= (float(self
.dispWidth
) - pos
[1]) / self
.dispWidth
143 vel
= int(vel
* (MIDI_VELOCITY_RANGE
[1] - MIDI_VELOCITY_RANGE
[0])) + MIDI_VELOCITY_RANGE
[0]
146 def getPan(self
, index
) :
147 pan
= float(index
) / (self
.keyboardLength
-1)
148 pan
= int(pan
* (MIDI_PAN_RANGE
[1] - MIDI_PAN_RANGE
[0])) + MIDI_PAN_RANGE
[0]
151 def playnote(self
, col
, pos
, vel
=None) :
152 pan
= self
.getPan(col
.index
)
153 self
.synth
.cc(0, 10, pan
)
154 vel
= vel
or self
.getVelocity(pos
)
155 self
.synth
.noteon(0, col
.tone
.midi
, vel
)
157 class PlayingScreen(PlayingScreenBase
) :
158 "fenêtre de jeu pour improvisation"
160 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
162 def __init__(self
, synth
) :
164 for midi
in self
.scale
:
166 distinctNotes
.append(tone
)
168 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
170 @event_handler(events
.NOTEON
)
171 def noteon(self
, evt
) :
173 self
.synth
.noteon(0, tone
.midi
, 96)
175 @event_handler(events
.NOTEOFF
)
176 def noteoff(self
, evt
) :
178 self
.synth
.noteoff(0, tone
.midi
)
181 class SongPlayingScreen(PlayingScreenBase
) :
183 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['NORMAL']) :
184 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
186 self
.quarterNoteDuration
= song
.quarterNoteDuration
187 self
.currentColumn
= None
188 self
.noteIterator
= self
.song
.iterNotes()
190 self
._plugListeners
(mode
)
192 def _plugListeners(self
, mode
) :
193 "initialisation des gestionnaires d'événements en fonction du mode"
195 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
196 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
198 elif mode
== PLAYING_MODES_DICT
['EASY'] :
199 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
200 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
202 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
203 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
204 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
205 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
207 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
208 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
209 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
211 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
212 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
213 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
216 # --- HID listeners ---
217 def handleBeginnerColumnOver(self
, event
) :
219 if col
.state
and not self
.currentNotePlayed
:
220 self
.playnote(col
, event
.pos
)
221 SongPlayingScreen
.setNoteTimeout(
222 int(self
.currentNote
.duration
* \
223 self
.quarterNoteDuration
)
225 self
.currentNotePlayed
= True
227 def handleEasyColumnOver(self
, event
) :
230 any(event
.mouseEvent
.buttons
) and \
231 not self
.currentNotePlayed
:
232 self
.playnote(col
, event
.pos
)
233 SongPlayingScreen
.setNoteTimeout(
234 int(self
.currentNote
.duration
* \
235 self
.quarterNoteDuration
)
237 self
.currentNotePlayed
= True
240 def handleNormalColumnOver(self
, event
) :
243 any(event
.mouseEvent
.buttons
) and \
244 not self
.currentNotePlayed
:
245 self
.playnote(col
, event
.pos
)
246 self
.currentNotePlayed
= True
248 def handleColumnDown(self
, event
) :
251 self
.playnote(col
, event
.pos
)
252 self
.currentNotePlayed
= True
254 def handleEasyColumnDown(self
, event
) :
257 not self
.currentNotePlayed
:
258 self
.playnote(col
, event
.pos
)
259 SongPlayingScreen
.setNoteTimeout(
260 int(self
.currentNote
.duration
* \
261 self
.quarterNoteDuration
)
263 self
.currentNotePlayed
= True
266 def handleExpertColumnDown(self
, event
) :
269 self
.playnote(col
, event
.pos
)
270 self
.currentNotePlayed
= True
272 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
274 self
.playnote(col
, event
.pos
, vel
=vel
)
275 self
.alternateColumn
= col
276 self
.currentNotePlayed
= False
278 def handleColumnUp(self
, event
) :
279 if self
.currentNotePlayed
:
280 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
283 def handleExpertColumnUp(self
, event
) :
284 if self
.currentNotePlayed
:
285 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
288 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
290 # --- End HID listeners ---
293 def displayNext(self
, event
=None) :
294 if self
.currentColumn
:
295 self
.currentColumn
.update(False)
297 note
, verseIndex
= self
.noteIterator
.next()
298 except StopIteration :
299 self
.noteIterator
= self
.song
.iterNotes()
300 note
, verseIndex
= self
.noteIterator
.next()
301 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
303 syllabus
= note
.lyrics
[verseIndex
].syllabus()
307 column
= self
.columns
[note
.midi
]
308 column
.update(True, syllabus
)
309 self
.currentColumn
= column
310 self
.currentNote
= note
311 self
.currentNotePlayed
= False
313 @event_handler(events
.NOTEEND
)
314 def clearTimeOutAndDisplayNext(self
, evt
) :
315 pygame
.time
.set_timer(evt
.type, 0)
316 self
.synth
.noteoff(0, self
.currentNote
.midi
)
320 def setNoteTimeout(delay
) :
321 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
324 pygame
.time
.set_timer(events
.NOTEEND
, 0)
325 super(SongPlayingScreen
, self
).stop()