1 # -*- coding: utf-8 -*-
4 bandes arc-en-ciel représentant un clavier.
10 #from colorsys import hls_to_rgb
11 #from gradients import gradients
12 from cursors
import WarpingCursor
13 from column
import Column
15 from eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
16 #from math import floor
18 from musicxml
import Tone
20 from config
import FRAMERATE
21 from config
import BORDER
22 from config
import FIRST_HUE
23 #from config import OFF_LUMINANCE
24 #from config import OFF_SATURATION
25 #from config import ON_TOP_LUMINANCE
26 #from config import ON_BOTTOM_LUMINANCE
27 #from config import ON_SATURATION
28 #from config import ON_COLUMN_OVERSIZING
29 #from config import ON_COLUMN_ALPHA
30 from config
import DEFAULT_MIDI_VELOCITY
32 from globals import BACKGROUND_LAYER
33 from globals import CURSOR_LAYER
34 from globals import PLAYING_MODES
36 class _PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
38 def __init__(self
, synth
, distinctNotes
=[]) :
40 distinctNotes : notes disctinctes présentes dans la chanson
41 triées du plus grave au plus aigu.
43 super(_PlayingScreenBase
, self
).__init
__()
45 self
.distinctNotes
= distinctNotes
46 self
.keyboardLength
= 0
47 self
.keyboardRects
= []
53 self
.draw(pygame
.display
.get_surface())
57 def _initRects(self
) :
58 """ création des espaces réservés pour
59 afficher les colonnes.
61 #ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
63 # self.keyboardLength = 8
65 # self.keyboardLength = 11
66 self
.keyboardLength
= len(self
.distinctNotes
)
68 screen
= pygame
.display
.get_surface()
70 # taille de la zone d'affichage utile (bordure autour)
71 dispWidth
= screen
.get_width() - 2 * BORDER
72 dispHeight
= screen
.get_height() - 2 * BORDER
74 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
77 for i
in range(self
.keyboardLength
) :
78 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
79 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
82 self
.keyboardRects
= rects
84 def _initColumns(self
) :
86 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
87 for i
, rect
in enumerate(self
.keyboardRects
) :
88 hue
= FIRST_HUE
- hueStep
* i
89 tone
= self
.distinctNotes
[i
]
90 c
= Column(self
, hue
, rect
, tone
)
91 self
.add(c
, layer
=BACKGROUND_LAYER
)
92 self
.columns
[tone
.midi
] = c
95 def _initCursor(self
) :
96 self
.cursor
= WarpingCursor(blinkMode
=True)
97 self
.add(self
.cursor
, layer
=CURSOR_LAYER
)
101 clock
= pygame
.time
.Clock()
102 pygame
.display
.flip()
103 pygame
.mouse
.set_visible(False)
104 while self
._running
:
105 EventDispatcher
.dispatchEvents()
106 dirty
= self
.draw(pygame
.display
.get_surface())
107 pygame
.display
.update(dirty
)
108 clock
.tick(FRAMERATE
)
110 pygame
.mouse
.set_visible(True)
111 self
.cursor
._stopBlink
()
113 @event_handler(pygame
.KEYDOWN
)
114 def handleKeyDown(self
, event
) :
115 if event
.key
== pygame
.K_q
:
116 self
._running
= False
119 class PlayingScreen(_PlayingScreenBase
) :
120 "fenêtre de jeu pour improvisation"
122 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
124 def __init__(self
, synth
) :
126 for midi
in self
.scale
:
128 distinctNotes
.append(tone
)
130 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
132 @event_handler(events
.NOTEON
)
133 def noteon(self
, evt
) :
135 self
.synth
.noteon(0, tone
.midi
, DEFAULT_MIDI_VELOCITY
)
137 @event_handler(events
.NOTEOFF
)
138 def noteoff(self
, evt
) :
140 self
.synth
.noteoff(0, tone
.midi
)
143 class SongPlayingScreen(_PlayingScreenBase
) :
145 def __init__(self
, synth
, song
, mode
=PLAYING_MODES
['NORMAL']) :
146 super(SongPlayingScreen
, self
).__init
__(synth
, song
.distinctNotes
)
148 self
.noteIterator
= self
.song
.iterNotes()
152 note
, verseIndex
= self
.noteIterator
.next()
153 syllabus
= note
.lyrics
[verseIndex
].syllabus()
154 column
= self
.columns
[note
.midi
]
155 column
.update(True, syllabus
)
158 @event_handler(events
.NOTEON
)
159 def noteon(self
, evt
) :
161 self
.synth
.noteon(0, tone
.midi
, DEFAULT_MIDI_VELOCITY
)
163 @event_handler(events
.NOTEOFF
)
164 def noteoff(self
, evt
) :
166 self
.synth
.noteoff(0, tone
.midi
)
170 #class Column(pygame.sprite.DirtySprite, EventHandlerMixin) :
172 # def __init__(self, group, hue, rect, tone) :
173 # pygame.sprite.DirtySprite.__init__(self, group)
176 # # nom de l'intonation
178 # toneName = FONT.render(tone.nom, True, (0,0,0))
180 # # état off : surface unie et nom de l'intonation
181 # sur = pygame.surface.Surface(rect.size)
182 # rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
184 # w, h = rect.w, rect.h
185 # tw, th, = toneName.get_size()
186 # toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
187 # sur.blit(toneName, toneRect)
189 # self.rectOff = rect
192 # # état on : surface dégradée avec nom de la note avec largeur agrandie
193 # topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
194 # bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
195 # onWidth = rect.width * ON_COLUMN_OVERSIZING
196 # onLeft = rect.centerx - onWidth / 2
197 # rectOn = pygame.Rect((onLeft, 0),
198 # (onWidth, rect.height))
199 # self.surOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
200 # w, h = rectOn.w, rectOn.h
201 # toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
202 # self.surOn.blit(toneName, toneRect)
203 # self.rectOn = rectOn
205 # self.image = self.surOff
207 # #EventDispatcher.addEventListener(pygame.MOUSEBUTTONDOWN, self.onMouseDown)
208 # #EventDispatcher.addEventListener(pygame.MOUSEBUTTONUP, self.onMouseUp)
210 # def update(self, state, syllabus='') :
211 # group = self.groups()[0]
212 # if state == self.state :
216 # group.change_layer(self, FOREGROUND_LAYER)
220 # renderedSyl = FONT.render(syllabus, True, (0,0,0))
221 # sw, sh, = renderedSyl.get_size()
222 # w, h = self.rectOn.w, self.rectOn.h
223 # sylRect = pygame.Rect(((w - sw) / 2, (h - sh) / 2), (sw, sh))
224 # sur.blit(renderedSyl, sylRect)
227 # self.rect = self.rectOn
229 # group.change_layer(self, BACKGROUND_LAYER)
230 # self.image = self.surOff
231 # self.rect = self.rectOff
235 # @event_handler(pygame.MOUSEBUTTONDOWN)
236 # def onMouseDown(self, event) :
237 # if self.rect.collidepoint(*event.pos) :
241 # @event_handler(pygame.MOUSEBUTTONUP)
242 # def onMouseUp(self, event) :
244 # self.raiseNoteOff()
246 # def raiseNoteOn(self) :
247 # evt = pygame.event.Event(events.NOTEON, tone=self.tone)
248 # pygame.event.post(evt)
250 # def raiseNoteOff(self) :
251 # evt = pygame.event.Event(events.NOTEOFF, tone=self.tone)
252 # pygame.event.post(evt)
256 #def hls_to_rgba_8bits(h, l, s, a=1) :
257 # #convert to rgb ranging from 0 to 255
258 # rgba = [floor(255 * i) for i in hls_to_rgb(h, l, s) + (a,)]