fc6c801b519abf54fdf337e347e0c5c32f84d922
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())
47 def _initRects(self
) :
48 """ création des espaces réservés pour
49 afficher les colonnes.
51 #ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
53 # self.keyboardLength = 8
55 # self.keyboardLength = 11
56 self
.keyboardLength
= len(self
.distinctNotes
)
58 screen
= pygame
.display
.get_surface()
60 # taille de la zone d'affichage utile (bordure autour)
61 dispWidth
= screen
.get_width() - 2 * BORDER
62 dispHeight
= screen
.get_height() - 2 * BORDER
64 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
67 for i
in range(self
.keyboardLength
) :
68 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
69 rect
= pygame
.Rect(upperLeftCorner
, (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
, 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
)
100 pygame
.mouse
.set_visible(True)
101 self
.cursor
._stopBlink
()
103 @event_handler(pygame
.KEYDOWN
)
104 def handleKeyDown(self
, event
) :
105 if event
.key
== pygame
.K_q
:
106 self
._running
= False
109 class PlayingScreen(_PlayingScreenBase
) :
110 "fenêtre de jeu pour improvisation"
112 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
114 def __init__(self
, synth
) :
116 for midi
in self
.scale
:
118 distinctNotes
.append(tone
)
120 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
122 @event_handler(events
.NOTEON
)
123 def noteon(self
, evt
) :
125 self
.synth
.noteon(0, tone
.midi
, DEFAULT_MIDI_VELOCITY
)
127 @event_handler(events
.NOTEOFF
)
128 def noteoff(self
, evt
) :
130 self
.synth
.noteoff(0, tone
.midi
)
133 class SongPlayingScreen(_PlayingScreenBase
) :
135 def __init__(self
, synth
, song
, mode
=PLAYING_MODES
['NORMAL']) :
136 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
138 self
.noteIterator
= self
.song
.iterNotes()
142 note
, verseIndex
= self
.noteIterator
.next()
143 syllabus
= note
.lyrics
[verseIndex
].syllabus()
144 column
= self
.columns
[note
.midi
]
145 column
.update(True, syllabus
)
148 @event_handler(events
.NOTEON
)
149 def noteon(self
, evt
) :
151 self
.synth
.noteon(0, tone
.midi
, DEFAULT_MIDI_VELOCITY
)
153 @event_handler(events
.NOTEOFF
)
154 def noteoff(self
, evt
) :
156 self
.synth
.noteoff(0, tone
.midi
)
160 #class Column(pygame.sprite.DirtySprite, EventHandlerMixin) :
162 # def __init__(self, group, hue, rect, tone) :
163 # pygame.sprite.DirtySprite.__init__(self, group)
166 # # nom de l'intonation
168 # toneName = FONT.render(tone.nom, True, (0,0,0))
170 # # état off : surface unie et nom de l'intonation
171 # sur = pygame.surface.Surface(rect.size)
172 # rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
174 # w, h = rect.w, rect.h
175 # tw, th, = toneName.get_size()
176 # toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
177 # sur.blit(toneName, toneRect)
179 # self.rectOff = rect
182 # # état on : surface dégradée avec nom de la note avec largeur agrandie
183 # topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
184 # bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
185 # onWidth = rect.width * ON_COLUMN_OVERSIZING
186 # onLeft = rect.centerx - onWidth / 2
187 # rectOn = pygame.Rect((onLeft, 0),
188 # (onWidth, rect.height))
189 # self.surOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
190 # w, h = rectOn.w, rectOn.h
191 # toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
192 # self.surOn.blit(toneName, toneRect)
193 # self.rectOn = rectOn
195 # self.image = self.surOff
197 # #EventDispatcher.addEventListener(pygame.MOUSEBUTTONDOWN, self.onMouseDown)
198 # #EventDispatcher.addEventListener(pygame.MOUSEBUTTONUP, self.onMouseUp)
200 # def update(self, state, syllabus='') :
201 # group = self.groups()[0]
202 # if state == self.state :
206 # group.change_layer(self, FOREGROUND_LAYER)
210 # renderedSyl = FONT.render(syllabus, True, (0,0,0))
211 # sw, sh, = renderedSyl.get_size()
212 # w, h = self.rectOn.w, self.rectOn.h
213 # sylRect = pygame.Rect(((w - sw) / 2, (h - sh) / 2), (sw, sh))
214 # sur.blit(renderedSyl, sylRect)
217 # self.rect = self.rectOn
219 # group.change_layer(self, BACKGROUND_LAYER)
220 # self.image = self.surOff
221 # self.rect = self.rectOff
225 # @event_handler(pygame.MOUSEBUTTONDOWN)
226 # def onMouseDown(self, event) :
227 # if self.rect.collidepoint(*event.pos) :
231 # @event_handler(pygame.MOUSEBUTTONUP)
232 # def onMouseUp(self, event) :
234 # self.raiseNoteOff()
236 # def raiseNoteOn(self) :
237 # evt = pygame.event.Event(events.NOTEON, tone=self.tone)
238 # pygame.event.post(evt)
240 # def raiseNoteOff(self) :
241 # evt = pygame.event.Event(events.NOTEOFF, tone=self.tone)
242 # pygame.event.post(evt)
246 #def hls_to_rgba_8bits(h, l, s, a=1) :
247 # #convert to rgb ranging from 0 to 255
248 # rgba = [floor(255 * i) for i in hls_to_rgb(h, l, s) + (a,)]