Touchscreen audio controller for presentation sound

The playlist Class

The playlist class (lines 98-243) handles directories of tracks that are played as a set. It starts by copying arguments to class variables on lines 100 and 101. Line 102 initializes self.tracks to a new list. Lines 104 and 105 initialize fonts that will be used later for buttons and labels.

Lines 107-115 load the sound files and show progress by printing the file names. The y=10 on line 107 is the y value where the file name will be printed. Next, self.screen.fill clears the screen, and pygame.display.flip updates the actual display so the changes are seen.

Line 110 starts a loop that will get each filename in the path argument passed to the class. nameSurf (line 111) is a rendered text string of the filename. Line 112 blits it to the screen, then line 113 increments y so the next filename doesn't display on top of the previous one. Line 114 flips the display so the filenames are visible on screen, and then line 115 adds the sound file to self.tracks. Lines 117-123 initialize class variables to keep track of the playlist state.

The play Function

Line 126 checks to see if self.channel is set; if so, it clears its end event (by calling self.channel.set_endevent with no arguments). PyGame assigns playing sounds to virtual channels. A channel is where a sound enters the computer sound system. You can think of it like a mixer channel (one of the sliders) on a traditional sound board. A channel has some built in notifications that I'll get back to on line 132.

Line 127 copies the current track from self.tracks to self.currentTrack. Line 128 sets the track volume to the playlist's stored volume (self.volume). Line 129 uses self.currentTrack.get_length to set self.trackLength. Line 130 starts the track playing, and line 131 sets self.state to PLAYING.

Finally, line 132 sets the channel's end event to pygame.USEREVENT. This will notify me when the track finishes playing so I can start the next one.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content