9aeef24004bfec784c9636c2891f77a998856dd9
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
14 from eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
15 from math
import floor
17 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 FONT
31 from config
import FONT_COLOR
33 class _PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
35 def __init__(self
, synth
, distinctNotes
=[]) :
37 distinctNotes : notes disctinctes présentes dans la chanson
38 triées du plus grave au plus aigu.
40 super(_PlayingScreenBase
, self
).__init
__()
42 self
.distinctNotes
= distinctNotes
43 self
.keyboardLength
= 0
44 self
.keyboardRects
= []
49 self
.draw(pygame
.display
.get_surface())
53 def _initRects(self
) :
54 """ création des espaces réservés pour
55 afficher les colonnes.
57 ambitus
= self
.distinctNotes
[-1].midi
- self
.distinctNotes
[0].midi
59 self
.keyboardLength
= 8
61 self
.keyboardLength
= 11
63 screen
= pygame
.display
.get_surface()
65 # taille de la zone d'affichage utile (bordure autour)
66 dispWidth
= screen
.get_width() - 2 * BORDER
67 dispHeight
= screen
.get_height() - 2 * BORDER
69 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
72 for i
in range(self
.keyboardLength
) :
73 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
74 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
77 self
.keyboardRects
= rects
79 def _initColumns(self
) :
81 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
82 for i
, rect
in enumerate(self
.keyboardRects
) :
83 hue
= FIRST_HUE
- hueStep
* i
84 tone
= self
.distinctNotes
[i
]
85 c
= Column(self
, hue
, rect
, tone
)
88 def _initCursor(self
) :
89 self
.cursor
= WarpingCursor(blinkMode
=False)
90 self
.add(self
.cursor
, layer
=2)
94 clock
= pygame
.time
.Clock()
96 pygame
.mouse
.set_visible(False)
98 EventDispatcher
.dispatchEvents()
99 dirty
= self
.draw(pygame
.display
.get_surface())
100 pygame
.display
.update(dirty
)
101 clock
.tick(FRAMERATE
)
103 pygame
.mouse
.set_visible(True)
104 self
.cursor
._stopBlink
()
106 @event_handler(pygame
.KEYDOWN
)
107 def handleKeyDown(self
, event
) :
108 if event
.key
== pygame
.K_q
:
109 self
._running
= False
112 @event_handler(pygame
.MOUSEMOTION
)
113 def handleMouseMotion(self
, event
) :
117 class PlayingScreen(_PlayingScreenBase
) :
118 "fenêtre de jeu pour improvisation"
119 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
121 def __init__(self
, synth
) :
123 for midi
in self
.scale
:
125 distinctNotes
.append(tone
)
127 super(PlayingScreen
, self
).__init
__(synth
, distinctNotes
)
129 @event_handler(events
.NOTEON
)
130 def noteon(self
, evt
) :
132 self
.synth
.noteon(0, tone
.midi
, 64)
134 @event_handler(events
.NOTEOFF
)
135 def noteoff(self
, evt
) :
137 self
.synth
.noteoff(0, tone
.midi
)
140 class SongPlayingScreen(_PlayingScreenBase
) :
142 def __init__(self
, song
) :
143 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
147 class Column(pygame
.sprite
.DirtySprite
, EventHandlerMixin
) :
149 def __init__(self
, group
, hue
, rect
, tone
) :
150 pygame
.sprite
.DirtySprite
.__init
__(self
, group
)
152 toneName
= FONT
.render(tone
.nom
, True, (0,0,0))
153 sur
= pygame
.surface
.Surface(rect
.size
)
154 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
156 w
, h
= rect
.w
, rect
.h
157 tw
, th
, = toneName
.get_size()
158 toneRect
= pygame
.Rect(((w
- tw
) / 2, h
- th
), (tw
, th
))
159 sur
.blit(toneName
, toneRect
)
163 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
, ON_COLUMN_ALPHA
)
164 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
, ON_COLUMN_ALPHA
)
165 onWidth
= rect
.width
* ON_COLUMN_OVERSIZING
166 onLeft
= rect
.centerx
- onWidth
/ 2
167 rectOn
= pygame
.Rect((onLeft
, 0),
168 (onWidth
, rect
.height
))
169 self
.stateOn
= gradients
.vertical(rectOn
.size
, topRgba
, bottomRgba
)
170 w
, h
= rectOn
.w
, rectOn
.h
171 toneRect
= pygame
.Rect(((w
- tw
) / 2, h
- th
), (tw
, th
))
172 self
.stateOn
.blit(toneName
, toneRect
)
175 self
.image
= self
.stateOff
178 def update(self
, state
) :
179 group
= self
.groups()[0]
181 group
.change_layer(self
, 1)
182 self
.image
= self
.stateOn
183 self
.rect
= self
.rectOn
185 group
.change_layer(self
, 0)
186 self
.image
= self
.stateOff
187 self
.rect
= self
.rectOff
189 @event_handler(pygame
.MOUSEBUTTONDOWN
)
190 def onMouseDown(self
, event
) :
191 if self
.rect
.collidepoint(*event
.pos
) :
195 @event_handler(pygame
.MOUSEBUTTONUP
)
196 def onMouseUp(self
, event
) :
200 def raiseNoteOn(self
) :
201 evt
= pygame
.event
.Event(events
.NOTEON
, tone
=self
.tone
)
202 pygame
.event
.post(evt
)
204 def raiseNoteOff(self
) :
205 evt
= pygame
.event
.Event(events
.NOTEOFF
, tone
=self
.tone
)
206 pygame
.event
.post(evt
)
210 def hls_to_rgba_8bits(h
, l
, s
, a
=1) :
211 #convert to rgb ranging from 0 to 255
212 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (a
,)]