223f4e54faea7307ff10554e2965382574743a5f
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
, distinctNotes
=[]) :
37 distinctNotes : notes disctinctes présentes dans la chanson
38 triées du plus grave au plus aigu.
40 super(_PlayingScreenBase
, self
).__init
__()
41 self
.distinctNotes
= distinctNotes
42 self
.keyboardLength
= 0
43 self
.keyboardRects
= []
48 self
.draw(pygame
.display
.get_surface())
52 def _initRects(self
) :
53 """ création des espaces réservés pour
54 afficher les colonnes.
56 ambitus
= self
.distinctNotes
[-1].midi
- self
.distinctNotes
[0].midi
58 self
.keyboardLength
= 8
60 self
.keyboardLength
= 11
62 screen
= pygame
.display
.get_surface()
64 # taille de la zone d'affichage utile (bordure autour)
65 dispWidth
= screen
.get_width() - 2 * BORDER
66 dispHeight
= screen
.get_height() - 2 * BORDER
68 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
71 for i
in range(self
.keyboardLength
) :
72 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
73 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
76 self
.keyboardRects
= rects
78 def _initColumns(self
) :
80 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
81 for i
, rect
in enumerate(self
.keyboardRects
) :
82 hue
= FIRST_HUE
- hueStep
* i
83 tone
= self
.distinctNotes
[i
]
84 c
= Column(self
, hue
, rect
, tone
)
87 def _initCursor(self
) :
88 self
.cursor
= WarpingCursor(blinkMode
=True)
89 self
.add(self
.cursor
, layer
=2)
93 clock
= pygame
.time
.Clock()
96 EventDispatcher
.dispatchEvents()
97 dirty
= self
.draw(pygame
.display
.get_surface())
98 pygame
.display
.update(dirty
)
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 @event_handler(pygame
.MOUSEMOTION
)
110 def handleMouseMotion(self
, event
) :
114 class PlayingScreen(_PlayingScreenBase
) :
115 "fenêtre de jeu pour improvisation"
116 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
120 for midi
in self
.scale
:
122 distinctNotes
.append(tone
)
124 super(PlayingScreen
, self
).__init
__(distinctNotes
)
127 soundFont
= '/Users/pinbe/dev/minwii/fluid-soundfont-3.1/FluidR3_GM.sf2'
130 self
.fs
= fs
= fluidsynth
.Synth()
132 self
.fsid
= fsid
= fs
.sfload(soundFont
)
133 fs
.program_select(0, fsid
, bank
, preset
)
136 print 'PlayingScreen.__del__'
139 @event_handler(events
.NOTEON
)
140 def noteon(self
, evt
) :
142 self
.fs
.noteon(0, tone
.midi
, 64)
144 @event_handler(events
.NOTEOFF
)
145 def noteoff(self
, evt
) :
147 self
.fs
.noteoff(0, tone
.midi
)
151 class SongPlayingScreen(_PlayingScreenBase
) :
153 def __init__(self
, song
) :
154 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
157 class SongPlayingScreenTest(_PlayingScreenBase
) :
162 super(SongPlayingScreenTest
, self
).__init
__([o
])
165 class Column(pygame
.sprite
.DirtySprite
, EventHandlerMixin
) :
167 def __init__(self
, group
, hue
, rect
, tone
) :
168 pygame
.sprite
.DirtySprite
.__init
__(self
, group
)
170 toneName
= FONT
.render(tone
.nom
, True, (0,0,0))
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
)
181 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
, ON_COLUMN_ALPHA
)
182 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
, ON_COLUMN_ALPHA
)
183 onWidth
= rect
.width
* ON_COLUMN_OVERSIZING
184 onLeft
= rect
.centerx
- onWidth
/ 2
185 rectOn
= pygame
.Rect((onLeft
, 0),
186 (onWidth
, rect
.height
))
187 self
.stateOn
= gradients
.vertical(rectOn
.size
, topRgba
, bottomRgba
)
188 w
, h
= rectOn
.w
, rectOn
.h
189 toneRect
= pygame
.Rect(((w
- tw
) / 2, h
- th
), (tw
, th
))
190 self
.stateOn
.blit(toneName
, toneRect
)
193 self
.image
= self
.stateOff
196 def update(self
, state
) :
197 group
= self
.groups()[0]
199 group
.change_layer(self
, 1)
200 self
.image
= self
.stateOn
201 self
.rect
= self
.rectOn
203 group
.change_layer(self
, 0)
204 self
.image
= self
.stateOff
205 self
.rect
= self
.rectOff
207 @event_handler(pygame
.MOUSEBUTTONDOWN
)
208 def onMouseDown(self
, event
) :
209 if self
.rect
.collidepoint(*event
.pos
) :
213 @event_handler(pygame
.MOUSEBUTTONUP
)
214 def onMouseUp(self
, event
) :
218 def raiseNoteOn(self
) :
219 evt
= pygame
.event
.Event(events
.NOTEON
, tone
=self
.tone
)
220 pygame
.event
.post(evt
)
222 def raiseNoteOff(self
) :
223 evt
= pygame
.event
.Event(events
.NOTEOFF
, tone
=self
.tone
)
224 pygame
.event
.post(evt
)
228 def hls_to_rgba_8bits(h
, l
, s
, a
=1) :
229 #convert to rgb ranging from 0 to 255
230 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (a
,)]