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 \
104 event
.unicode == u
'q' or \
108 @event_handler(pygame
.MOUSEBUTTONDOWN
)
109 def onMouseDown(self
, event
) :
110 # TODO à cleaner : on vire le dernier élément
111 # parce qu'il s'agit du curseur
112 for col
in reversed(self
.sprites()[:-1]) :
113 if col
.rect
.collidepoint(*event
.pos
):
114 self
.raiseColDown(col
, event
)
117 @event_handler(pygame
.MOUSEBUTTONUP
)
118 def onMouseUp(self
, event
) :
119 for col
in reversed(self
.sprites()[:-1]) :
120 if col
.rect
.collidepoint(*event
.pos
) :
121 self
.raiseColUp(col
, event
)
124 @event_handler(pygame
.MOUSEMOTION
)
125 def onMouseMove(self
, event
) :
126 for col
in reversed(self
.sprites()[:-1]) :
127 if col
.rect
.collidepoint(*event
.pos
) :
128 self
.raiseColOver(col
, event
)
131 def raiseColDown(self
, col
, mouseEvent
) :
132 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=mouseEvent
.pos
)
133 pygame
.event
.post(evt
)
135 def raiseColUp(self
, col
, mouseEvent
) :
136 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=mouseEvent
.pos
)
137 pygame
.event
.post(evt
)
139 def raiseColOver(self
, col
, mouseEvent
) :
140 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=mouseEvent
.pos
, mouseEvent
=mouseEvent
)
141 pygame
.event
.post(evt
)
143 def getVelocity(self
, pos
) :
144 vel
= (float(self
.dispWidth
) - pos
[1]) / self
.dispWidth
145 vel
= int(vel
* (MIDI_VELOCITY_RANGE
[1] - MIDI_VELOCITY_RANGE
[0])) + MIDI_VELOCITY_RANGE
[0]
148 def getPan(self
, index
) :
149 pan
= float(index
) / (self
.keyboardLength
-1)
150 pan
= int(pan
* (MIDI_PAN_RANGE
[1] - MIDI_PAN_RANGE
[0])) + MIDI_PAN_RANGE
[0]
153 def playnote(self
, col
, pos
, vel
=None) :
154 pan
= self
.getPan(col
.index
)
155 self
.synth
.cc(0, 10, pan
)
156 vel
= vel
or self
.getVelocity(pos
)
157 self
.synth
.noteon(0, col
.tone
.midi
, vel
)
159 class PlayingScreen(PlayingScreenBase
) :
160 "fenêtre de jeu pour improvisation"
162 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
164 def __init__(self
, synth
) :
166 for midi
in self
.scale
:
168 distinctNotes
.append(tone
)
170 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
172 @event_handler(events
.NOTEON
)
173 def noteon(self
, evt
) :
175 self
.synth
.noteon(0, tone
.midi
, 96)
177 @event_handler(events
.NOTEOFF
)
178 def noteoff(self
, evt
) :
180 self
.synth
.noteoff(0, tone
.midi
)
183 class SongPlayingScreen(PlayingScreenBase
) :
185 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['NORMAL']) :
186 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
188 self
.quarterNoteDuration
= song
.quarterNoteDuration
189 self
.currentColumn
= None
190 self
.noteIterator
= self
.song
.iterNotes()
192 self
._plugListeners
(mode
)
194 def _plugListeners(self
, mode
) :
195 "initialisation des gestionnaires d'événements en fonction du mode"
197 if mode
== PLAYING_MODES_DICT
['BEGINNER'] :
198 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleBeginnerColumnOver
)
200 elif mode
== PLAYING_MODES_DICT
['EASY'] :
201 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleEasyColumnDown
)
202 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleEasyColumnOver
)
204 elif mode
== PLAYING_MODES_DICT
['NORMAL'] :
205 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleNormalColumnOver
)
206 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
207 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
209 elif mode
== PLAYING_MODES_DICT
['ADVANCED'] :
210 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
211 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
213 elif mode
== PLAYING_MODES_DICT
['EXPERT'] :
214 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleExpertColumnDown
)
215 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleExpertColumnUp
)
218 # --- HID listeners ---
219 def handleBeginnerColumnOver(self
, event
) :
221 if col
.state
and not self
.currentNotePlayed
:
222 self
.playnote(col
, event
.pos
)
223 SongPlayingScreen
.setNoteTimeout(
224 int(self
.currentNote
.duration
* \
225 self
.quarterNoteDuration
)
227 self
.currentNotePlayed
= True
229 def handleEasyColumnOver(self
, event
) :
232 any(event
.mouseEvent
.buttons
) and \
233 not self
.currentNotePlayed
:
234 self
.playnote(col
, event
.pos
)
235 SongPlayingScreen
.setNoteTimeout(
236 int(self
.currentNote
.duration
* \
237 self
.quarterNoteDuration
)
239 self
.currentNotePlayed
= True
242 def handleNormalColumnOver(self
, event
) :
245 any(event
.mouseEvent
.buttons
) and \
246 not self
.currentNotePlayed
:
247 self
.playnote(col
, event
.pos
)
248 self
.currentNotePlayed
= True
250 def handleColumnDown(self
, event
) :
253 self
.playnote(col
, event
.pos
)
254 self
.currentNotePlayed
= True
256 def handleEasyColumnDown(self
, event
) :
259 not self
.currentNotePlayed
:
260 self
.playnote(col
, event
.pos
)
261 SongPlayingScreen
.setNoteTimeout(
262 int(self
.currentNote
.duration
* \
263 self
.quarterNoteDuration
)
265 self
.currentNotePlayed
= True
268 def handleExpertColumnDown(self
, event
) :
271 self
.playnote(col
, event
.pos
)
272 self
.currentNotePlayed
= True
274 vel
= self
.getVelocity(event
.pos
) * MIDI_VELOCITY_WRONG_NOTE_ATTN
276 self
.playnote(col
, event
.pos
, vel
=vel
)
277 self
.alternateColumn
= col
278 self
.currentNotePlayed
= False
280 def handleColumnUp(self
, event
) :
281 if self
.currentNotePlayed
:
282 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
285 def handleExpertColumnUp(self
, event
) :
286 if self
.currentNotePlayed
:
287 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
290 self
.synth
.noteoff(0, self
.alternateColumn
.tone
.midi
)
292 # --- End HID listeners ---
295 def displayNext(self
, event
=None) :
296 if self
.currentColumn
:
297 self
.currentColumn
.update(False)
299 note
, verseIndex
= self
.noteIterator
.next()
300 except StopIteration :
301 self
.noteIterator
= self
.song
.iterNotes()
302 note
, verseIndex
= self
.noteIterator
.next()
303 eventLogger
.info(pygame
.event
.Event(events
.SONGEND
))
305 syllabus
= note
.lyrics
[verseIndex
].syllabus()
309 column
= self
.columns
[note
.midi
]
310 column
.update(True, syllabus
)
311 self
.currentColumn
= column
312 self
.currentNote
= note
313 self
.currentNotePlayed
= False
315 @event_handler(events
.NOTEEND
)
316 def clearTimeOutAndDisplayNext(self
, evt
) :
317 pygame
.time
.set_timer(evt
.type, 0)
318 self
.synth
.noteoff(0, self
.currentNote
.midi
)
322 def setNoteTimeout(delay
) :
323 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
326 pygame
.time
.set_timer(events
.NOTEEND
, 0)
327 super(SongPlayingScreen
, self
).stop()