Touchscreen audio controller for presentation sound

The music Class

The music class (lines 244-361) is the main entry point of the program. __init__ starts by copying screen (the PyGame surface representing the actual screen) to the class variable self.screen. Lines 247-249 initialize self.playlists, self.announcements, and self.background as dictionaries.

Line 251 and 252 initialize the PyGame audio module (called pygame.mixer). I call it twice because sometimes it doesn't initialize the first time, and it doesn't hurt anything to call it twice. Lines 254-255 start the PyGame font module and then create a system default font object.

Lines 257-262 load the config file. Line 257 opens the file itself, then line 258 loops through each line of the file. Line 259 splits the line on each tab character, then the tree of if/elif's checks the third column of each file for the type of sound this line represents. Once the program finds the appropriate sound type, it initializes the corresponding class.

The drawControls Function

The drawControls function (lines 265-316) takes care of actually drawing each button on the screen. It starts by clearing the screen using self.screen.fill (line 266). self.clickZones on line 267 is a list of PyGame rectangles that need to respond to touches/clicks. y=10 (line 268) is the starting point for drawing the screen.

Lines 269-278 handle drawing the playlist controls. The loop on line 269 steps through each playlist setup in the config file. For each playlist, a label is generated (line 271) and blitted to screen (line 272). The volume and track time remaining are drawn from the playlist.drawVolume function called inside the self.screen.blit on line 273.

Line 274 loops over each of the buttons returned from playlist.controls. These are blitted to the screen (line 275), then x is incremented (line 276), and self.clickZones is appended with the button information (line 277). self.clickZones will be checked in the event loop whenever a MOUSEBUTTONDOWN event is received. Finally, line 278 increments y for the next playlist.

Lines 280-294 and 296-313 repeat the same process for announcements and background sounds. Lines 290-294 and 309-313 check to see if buttons have run off the screen and reset x and y accordingly. Finally, line 316 calls pygame.display.flip to show everything on the screen.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content