Touchscreen audio controller for presentation sound

The loop Function

The loop function (lines 318-360) is the main PyGame event loop. When it is called, I set a timer to go off once a second (line 319) and call self.drawControls. This gets the touchscreen ready to go. looping = True on line 322 is the "keep going" variable – if anything sets looping to False, the main loop will exit and the program will end.

Line 323 starts the main event loop comprising the rest of the function. Line 324 fetches all of the events from the PyGame queue. Lines 325-341 check for mouse clicks or screen touches and handle them accordingly. Line 326 loops through self.clickZones and checks each one for the current click position (line 327). Each remaining if/elif checks the associated command or sound type for the click and passes it to the appropriate class instance (specified in zone [ 1 ]). If a command will change the screen appearance, then self.drawControls is called afterward to redraw everything.

Line 342 looks for pygame.USEREVENT. pygame.USEREVENT is generated when one of PyGame's audio channels has finished playing a track. When this happens, each member of self.playlists (line 343) is told to checkStopped (line 344). Ultimately, this will start the next track.

Line 345 checks for pygame.USEREVENT + 1, a 1-second timer setup on line 319. When it goes off, I start a redraw list and then loop through all of the playlists (line 348-351) redrawing the time remaining. By appending the result of self.screen.blit to redraw, I can call pygame.display.update (line 352) instead of pygame.display.flip. This only redraws the parts of the screen that have changed, which can be significantly faster than redrawing the entire screen.

Lines 354-360 repeat this process, checking for each playlist instance's playlist.volumeChange flag. If it is True, then the new volume is drawn and the screen is updated.

Starting Everything

Up until now, no code has actually done anything. Everything has just been definitions. Without these last four lines nothing would happen.

First, I initialize the PyGame display (line 362) and create my screen variable (line 363). Line 365 initializes the music class, and sounds.loop (line 366) starts everything running.

The Author

Scott Sumner is the Assistant Manager of the Charlie Noble Planetarium at the Fort Worth Museum of Science and History. He enjoys using Python to solve as many problems as possible.

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