S07_Sound
S07_Sound
S07_Sound
• TCP vs UDP
• Network Programming
• Client-Server Topology
• Starsiege: Tribes
What do we want?
segments
Transport Transport
• In each turn, all peers send player input to all other peers
➙ sequence of commands
• Known as
Server
Deterministic
lockstep Full
Update
Today
• Sound Theory
• Sound Technology
• Sound in Python
Sound is Good!
• Games are better with sound
• Sounds:
• Provide Guidance
Mood
• Music has emotional content
• Tense
• Relax
all together
sin(2πf + θ)
Sound Localization
Source
• or explosion of a grenade
• Listen to Space_Invaders_Music.mp3
Same graph
as before
y-axis is now
voltage
ADC
• Analog to Digital Converter
10 samples per
period sampling
5x faster than
required
Music Sampling
• Human ears have a range of ~ 40Hz - 15,000Hz
pygame.mixer
Sound(filename) Sound.play()
WAV file pygame.mixer.Sound
Sound
Sound.get_length() Hardware
Sound.set_volume(.8)
Sound.stop()
Sound.fadeout(500)
Sound(filename) Sound.play()
WAV file pygame.mixer.Sound
Sound
Sound.get_length() Hardware
Sound.set_volume(.8)
Sound.stop()
Sound.fadeout(500)
pygame.init()
snd = pygame.mixer.Sound('JDB - Mysterious.wav')
snd.play()
running = True
while running: mixer needs the event
for event in pygame.event.get(): loop running
if event.type == pygame.KEYDOWN:
running = False
demo of sound0.py
while True: Each time around game loop
clock.tick(30)
quit, up, down = check_events()
audio.update(up, down) update( ) and draw( )
surface.fill((0,0,0))
audio.draw(surface)
pygame.display.flip()
class AudioController:
make sound object (from le data)
def __init__(self):
self.volume = 0.5
self.mix = pygame.mixer.Sound('assets/sounds/JDB - Traveling.wav')
self.mix.play()
self.mix.set_volume(self.volume) start playing it
def update(self, up, down):
if down:
self.volume = max(0.0, self.volume - 0.1)
update( ) just changes
if up: volume on sound object
self.volume = min(1.0, self.volume + 0.1)
self.mix.set_volume(self.volume)
Sound(filename)
WAV file pygame.mixer.Sound Sound.play()
+ Sound
Hardware
Sound(filename)
WAV file pygame.mixer.Sound Sound.play()
Sound.get_length() + Sound
Hardware
Sound.set_volume(.8)
a_channel.set_volume(.2,.7)
a_channel.set_endevent(pygame.locals.USEREVENT)
Sound.get_length() + Sound
Hardware
Sound.set_volume(.8)
a_channel.set_volume(.2,.7)
a_channel.set_endevent(pygame.locals.USEREVENT)
Sound.get_length() + Sound
Hardware
Sound.set_volume(.8)
a_channel.set_volume(.2,.7)
a_channel.set_endevent(pygame.locals.USEREVENT)
• Call a_channel.get_busy( )
• "Polling"
When is it done? (version 2)
pygame.mixer
a_sound a_channel.play(a_sound)
Sound(filename)
WAV file pygame.mixer.Sound pygame.mixer.Channel
Sound.get_length() + Sound
Hardware
Sound.set_volume(.8)
a_channel.set_volume(.2,.7)
a_channel.set_endevent(pygame.locals.USEREVENT)
• Call a_channel.set_endevent(my_event_type)
• When the sound is done, an event of this type will be put on the
event queue
1 sec 1m
25cm × × = 0.000735 sec
340 m 100 cm
• Tough to synchronize in Pygame to within 735 μsec
"Fake" Localization
• Your brain interprets loud sounds as if they are
close and quiet sounds as if they are far
def set_pan(leftness):
rightness = 1-leftness
self.channel.set_volume(leftness, rightness)
demo of sound1.py
class AudioController: Create a sound object
def __init__(self):
self.volume = 0.5 Get a channel object
self.right_channel = 0.5
self.mix = pygame.mixer.Sound('assets/sounds/JDB - Traveling.wav')
self.channel = pygame.mixer.find_channel()
self.channel.play(self.mix)
self.set_volume_pan() play the object in the channel
y-axis: 16-bit
integer value
of the sample
y-axis: 16-bit
integer value
of the sample
snd = pygame.mixer.Sound('Space_Invaders_Music.ogg')
arr = pygame.sndarray.array(snd)
print_array_info(arr)
plt.plot(arr[ ,0]) This is the rst plot
plt.show()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
running = False
Needs an event loop to play a Sound
preparing sound les
fi
Sound Editing Apps
• There are tons of possibilities here
• Probably very
overpowered
for simple
hobby game
makers
Free Game Sound Effects
• opengameart.org/content/library-of-game-
sounds
• soundbible.com
demo of sound2.py
What did you learn today?
• Sounds are very useful in games
• Sounds in pygame
• Localization
• Mathematical primitives