ec965367efd1cb4ee0ff266bff90d548e99d64e7
1 # -*- coding: utf-8 -*-
4 bandes arc-en-ciel représentant un clavier.
10 from cursors
import WarpingCursor
11 from column
import Column
13 from eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
15 from musicxml
import Tone
17 from config
import FRAMERATE
18 from config
import BORDER
19 from config
import FIRST_HUE
20 from config
import DEFAULT_MIDI_VELOCITY
22 from globals import BACKGROUND_LAYER
23 from globals import CURSOR_LAYER
24 from globals import PLAYING_MODES_DICT
26 class _PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
28 def __init__(self
, synth
, distinctNotes
=[]) :
30 distinctNotes : notes disctinctes présentes dans la chanson
31 triées du plus grave au plus aigu.
33 super(_PlayingScreenBase
, self
).__init
__()
35 self
.distinctNotes
= distinctNotes
36 self
.keyboardLength
= 0
37 self
.keyboardRects
= []
43 self
.draw(pygame
.display
.get_surface())
46 def _initRects(self
) :
47 """ création des espaces réservés pour
48 afficher les colonnes.
50 #ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
52 # self.keyboardLength = 8
54 # self.keyboardLength = 11
55 self
.keyboardLength
= len(self
.distinctNotes
)
57 screen
= pygame
.display
.get_surface()
59 # taille de la zone d'affichage utile (bordure autour)
60 self
.dispWidth
= dispWidth
= screen
.get_width() - 2 * BORDER
61 self
.dispHeight
= dispHeight
= screen
.get_height() - 2 * BORDER
63 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
66 for i
in range(self
.keyboardLength
) :
67 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
68 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
71 self
.keyboardRects
= rects
73 def _initColumns(self
) :
75 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
76 for i
, rect
in enumerate(self
.keyboardRects
) :
77 hue
= FIRST_HUE
- hueStep
* i
78 tone
= self
.distinctNotes
[i
]
79 c
= Column(self
, hue
, rect
, tone
)
80 self
.add(c
, layer
=BACKGROUND_LAYER
)
81 self
.columns
[tone
.midi
] = c
84 def _initCursor(self
) :
85 self
.cursor
= WarpingCursor(blinkMode
=True)
86 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
90 clock
= pygame
.time
.Clock()
92 pygame
.mouse
.set_visible(False)
94 EventDispatcher
.dispatchEvents()
95 dirty
= self
.draw(pygame
.display
.get_surface())
96 pygame
.display
.update(dirty
)
100 self
._running
= False
101 self
.synth
.system_reset()
102 pygame
.mouse
.set_visible(True)
103 self
.cursor
._stopBlink
()
105 @event_handler(pygame
.KEYDOWN
)
106 def handleKeyDown(self
, event
) :
107 if event
.key
== pygame
.K_q
:
110 @event_handler(pygame
.MOUSEBUTTONDOWN
)
111 def onMouseDown(self
, event
) :
112 # TODO à cleaner : on vire le dernier élément
113 # parce qu'il s'agit du curseur
114 for col
in reversed(self
.sprites()[:-1]) :
115 if col
.rect
.collidepoint(*event
.pos
):
116 self
.raiseColDown(col
, event
.pos
)
119 @event_handler(pygame
.MOUSEBUTTONUP
)
120 def onMouseUp(self
, event
) :
121 for col
in reversed(self
.sprites()[:-1]) :
122 if col
.rect
.collidepoint(*event
.pos
) :
123 self
.raiseColUp(col
, event
.pos
)
126 @event_handler(pygame
.MOUSEMOTION
)
127 def onMouseMove(self
, event
) :
128 for col
in reversed(self
.sprites()[:-1]) :
129 if col
.rect
.collidepoint(*event
.pos
) :
130 self
.raiseColOver(col
, event
.pos
)
133 def raiseColDown(self
, col
, pos
) :
134 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
, pos
=pos
)
135 pygame
.event
.post(evt
)
137 def raiseColUp(self
, col
, pos
) :
138 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
, pos
=pos
)
139 pygame
.event
.post(evt
)
141 def raiseColOver(self
, col
, pos
) :
142 evt
= pygame
.event
.Event(events
.COLOVER
, column
=col
, pos
=pos
)
143 pygame
.event
.post(evt
)
146 class PlayingScreen(_PlayingScreenBase
) :
147 "fenêtre de jeu pour improvisation"
149 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
151 def __init__(self
, synth
) :
153 for midi
in self
.scale
:
155 distinctNotes
.append(tone
)
157 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
159 @event_handler(events
.NOTEON
)
160 def noteon(self
, evt
) :
162 self
.synth
.noteon(0, tone
.midi
, DEFAULT_MIDI_VELOCITY
)
164 @event_handler(events
.NOTEOFF
)
165 def noteoff(self
, evt
) :
167 self
.synth
.noteoff(0, tone
.midi
)
170 class SongPlayingScreen(_PlayingScreenBase
) :
172 def __init__(self
, synth
, song
, mode
=PLAYING_MODES_DICT
['EASY']) :
173 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
175 self
.quarterNoteDuration
= song
.quarterNoteDuration
176 self
.currentColumn
= None
177 self
.noteIterator
= self
.song
.iterNotes()
179 if mode
== PLAYING_MODES_DICT
['NORMAL'] :
180 EventDispatcher
.addEventListener(events
.COLDOWN
, self
.handleColumnDown
)
181 EventDispatcher
.addEventListener(events
.COLUP
, self
.handleColumnUp
)
182 elif mode
== PLAYING_MODES_DICT
['EASY'] :
183 EventDispatcher
.addEventListener(events
.COLOVER
, self
.handleColumnOver
)
186 def displayNext(self
, event
=None) :
187 if self
.currentColumn
:
188 self
.currentColumn
.update(False)
189 note
, verseIndex
= self
.noteIterator
.next()
190 syllabus
= note
.lyrics
[verseIndex
].syllabus()
191 column
= self
.columns
[note
.midi
]
192 column
.update(True, syllabus
)
193 self
.currentColumn
= column
194 self
.currentNote
= note
195 self
.currentNotePlayed
= False
197 def handleColumnDown(self
, event
) :
200 self
.synth
.noteon(0, col
.tone
.midi
, DEFAULT_MIDI_VELOCITY
)
201 self
.currentNotePlayed
= True
203 def handleColumnUp(self
, event
) :
204 if self
.currentNotePlayed
:
205 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)
208 def handleColumnOver(self
, event
) :
210 if col
.state
and not self
.currentNotePlayed
:
211 self
.synth
.noteon(0, col
.tone
.midi
, DEFAULT_MIDI_VELOCITY
)
212 SongPlayingScreen
.setNoteTimeout(
213 int(self
.currentNote
.duration
* \
214 self
.quarterNoteDuration
)
216 self
.currentNotePlayed
= True
218 @event_handler(events
.NOTEEND
)
219 def clearTimeOutAndDisplayNext(self
, evt
) :
220 pygame
.time
.set_timer(evt
.type, 0)
221 self
.synth
.noteoff(0, self
.currentNote
.midi
)
225 def setNoteTimeout(delay
) :
226 pygame
.time
.set_timer(events
.NOTEEND
, delay
)
229 pygame
.time
.set_timer(events
.NOTEEND
, 0)
230 super(SongPlayingScreen
, self
).stop()