Touchscreen audio controller for presentation sound
The background Class
The background
class (lines 4-87) handles tracks designed for ambient audio. Normally, their volume will be adjusted to a fairly low level. The class takes as arguments caption
and path
(line 5). This is the label to use on the button and the location of the sound file itself. Lines 6 and 7 copy the initial arguments into class variables. Line 8 initializes self.state
to PAUSED
, and line 9 initializes the background sound's volume to 100.
Next, I initialize a font to draw the labels (line 11). By calling pygame.font.SysFont
without a font name (""
as the first argument), I select the default system font. A value of 30
sets the font height to 30 pixels.
Line 13 initializes self.oldButtonState
to False
, then line 14 draws the play button by calling self.drawPlayButton
. Lines 16-24 drawPlayButton
draws the play button with a light or dark background depending on whether or not the sound is currently playing.
Line 17 checks to see if active
was passed as an argument and sets it if not. Line 19 checks the current state of active
and will use either lines 20-24 or lines 27-31 to draw the play button.
Line 20 renders the button's caption in black. Line 21 initializes a PyGame surface to 100 pixels wide by 50 pixels tall. Line 22 uses fill
to paint the entire surface a dark shade of red, then line 23 copies the text from line 20 onto the button. 50 - ( playText.get_width() / 2 )
centers the text on the button. Lines 27-31 are the same as 20-24, but line 29 uses a brighter background color because the sound is currently playing.
Lines 33-35 draw the volume bargraph. First, I divide the volume in half. Volume is a percentage from 0 to 100, and the play button is 50 pixels high. Thus, half of the volume will scale to the proper height of the button.
Line 34 determines the starting point of the bar graph. I want to draw from the bottom up, but PyGame counts pixels from the top down. Thus, I subtract halfVolume
from 50 (the button height) so I know where to start drawing.
Line 35 draws the bargraph with the pygame.draw.rect
function. self.playButton
is the surface I want to draw onto, and (0 , 0 , 255)
is the color I want to draw with (blue). I'll look at the last argument piece by piece. The 4-part tuple is the starting x and y coordinates, then the rectangle width and height.
I want to start at 0 (the left side of the PyGame surface, and top
pixels from the top (from line 34). My width should be 3 pixels, and the height is halfVolume
from line 33. The last argument,
says to draw a filled rectangle. If I had supplied a larger number, the rectangle would be drawn that many pixels wide.
The toggle
function (lines 37-43) switches between playing and pausing the audio. It maintains its state in self.state
, which is a human-readable string. Line 38 checks to see if the audio is currently playing. If it is, then self.stop
is called to stop playback (line 39) and then the internal state is updated (line 40). Lines 41-43 do the opposite if the audio was paused.
The play Function
Background sounds are not preloaded, so line 46 loads the sound file with pygame.mixer.Sound
. Then self.track.play
starts playback. The -1
argument says to loop forever (line 47). Line 48 calls self.track.set_volume
to set the volume to the class's level (self.volume
). Finally, line 50 calls self.drawPlayButton
to redraw the button in the "playing" state.
Buy this article as PDF
Pages: 2
(incl. VAT)