571a3d9d0d8186197a4824e41fc65d3ff528bd05
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
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 dispWidth
= screen
.get_width() - 2 * BORDER
61 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
)
99 pygame
.mouse
.set_visible(True)
100 self
.cursor
._stopBlink
()
102 @event_handler(pygame
.KEYDOWN
)
103 def handleKeyDown(self
, event
) :
104 if event
.key
== pygame
.K_q
:
105 self
._running
= False
107 @event_handler(pygame
.MOUSEBUTTONDOWN
)
108 def onMouseDown(self
, event
) :
109 # TODO à cleaner : on vire le dernier élément
110 # parce qu'il s'agit du curseur
111 for col
in reversed(self
.sprites()[:-1]) :
112 if col
.rect
.collidepoint(*event
.pos
):
113 self
.raiseKeyDown(col
)
116 @event_handler(pygame
.MOUSEBUTTONUP
)
117 def onMouseUp(self
, event
) :
118 for col
in reversed(self
.sprites()[:-1]) :
119 if col
.rect
.collidepoint(*event
.pos
) :
123 def raiseKeyDown(self
, col
) :
124 evt
= pygame
.event
.Event(events
.COLDOWN
, column
=col
)
125 pygame
.event
.post(evt
)
127 def raiseKeyUp(self
, col
) :
128 evt
= pygame
.event
.Event(events
.COLUP
, column
=col
)
129 pygame
.event
.post(evt
)
132 class PlayingScreen(_PlayingScreenBase
) :
133 "fenêtre de jeu pour improvisation"
135 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
137 def __init__(self
, synth
) :
139 for midi
in self
.scale
:
141 distinctNotes
.append(tone
)
143 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
145 @event_handler(events
.NOTEON
)
146 def noteon(self
, evt
) :
148 self
.synth
.noteon(0, tone
.midi
, DEFAULT_MIDI_VELOCITY
)
150 @event_handler(events
.NOTEOFF
)
151 def noteoff(self
, evt
) :
153 self
.synth
.noteoff(0, tone
.midi
)
156 class SongPlayingScreen(_PlayingScreenBase
) :
158 def __init__(self
, synth
, song
, mode
=PLAYING_MODES
['NORMAL']) :
159 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
161 self
.currentColumn
= None
162 self
.noteIterator
= self
.song
.iterNotes()
165 def displayNext(self
) :
166 if self
.currentColumn
:
167 self
.currentColumn
.update(False)
168 note
, verseIndex
= self
.noteIterator
.next()
169 syllabus
= note
.lyrics
[verseIndex
].syllabus(encoding
="iso-8859-1")
170 column
= self
.columns
[note
.midi
]
171 column
.update(True, syllabus
)
172 self
.currentColumn
= column
173 self
.currentNotePlayed
= False
175 @event_handler(events
.COLDOWN
)
176 def handleColumnDown(self
, event
) :
179 self
.synth
.noteon(0, col
.tone
.midi
, DEFAULT_MIDI_VELOCITY
)
180 self
.currentNotePlayed
= True
182 @event_handler(events
.COLUP
)
183 def handleColumnUp(self
, event
) :
184 if self
.currentNotePlayed
:
185 self
.synth
.noteoff(0, self
.currentColumn
.tone
.midi
)