Sound is vibration in a medium - e.g. rapidly varying pressure of air against the eardrum

Diagram by Tim Gollisch, Andreas M. V. Herz

Digital Sound

Digital sound is a set of samples of that vibration (similar to pixels)

Represented as a 1D array - varying amplitude over time

Stereo sound is 2 such arrays in parallel

Sound Frequencies

A "pure tone" is a sine wave of a particular frequency
e.g. 1 KHz = 1000 Hz = 1000 cycles per second

All sounds can be mathematically reduced to a combination of many such sine waves (Fourier transform)

Humans are capable of hearing frequencies up to ~ 20,000 Hz


Digital sound quality depends on the sample rate & sample resolution

To reproduce a particular sound frequency, we must sample at twice that frequency or better

Telephone quality sound is 8 KHz (8000 samples/second), 8 bits/sample

CD quality sound is 44KHz, 16 bits per sample

Playing sound requires constantly feeding new data to the sound hardware

Sound is therefore usually handled by a separate process or thread


A free, open-source audio editor

Works under Windows, MacOS, and Linux

Can record, edit, import and export WAV, AIFF, Ogg Vorbis, and MP3 files

pygame Sound

pygame includes modules for graphics, sound, movies, devices, etc.

Sound is managed by the mixer system

One can load sound files, play them, and adjust their volume

pygame.mixer runs a separate process, to perform sound playback in parallel with graphics

pygame Sound

import pygame, time

s = pygame.mixer.Sound("lion.wav")
while pygame.mixer.get_busy() > 0:

pygame Sound

Important functions:

called once, at startup
snd = pygame.mixer.Sound(filename)
loads a WAV file and returns a Sound object
starts a sound object playing, on a free channel
stops a sound object playing
changes playback volume of sound object
fades out the sound, then stops it

pygame Sound

pygame.mixer.init(freq, size, stereo, buffersize)

freq - frequency (sample rate) of sound playback; defaults to 22050

size - 8 or 16, for 8- or 16-bit unsigned data; -8 or -16 for 8- or 16-bit signed; defaults to -16

buffersize - number of samples to send to hardware at one time; defaults to 1024

Note: a larger sample size improves playback (less chance of dropouts), but increases latency

pygame Sound - intended for background music

music functions are similar to Sound functions, except that they don't load all the data into memory at once

This makes a difference for very large sound files, such as background music

pygame.mixer supports exactly 1 music track at a time'musicfile.mp3')


fmod is a cross-platform (Linux, Windows, Mac, & gaming consoles) C/C++ sound toolkit, marketed for game development.

Some advantages:


OpenAL is another cross-platform 3D audio system

It is an API, modelled after OpenGL

Open source implementations exist for Windows, MacOS, Linux, BSD, Solaris, IRIX
There are also implementations for game consoles

Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons BY-2.0 License.