Sound

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






Sound

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






Audacity

http://audacity.sourceforge.net/

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

pygame.mixer.init()
s = pygame.mixer.Sound("lion.wav")
s.play()
while pygame.mixer.get_busy() > 0:
        time.sleep(1)





pygame Sound

Important functions:

pygame.mixer.init()
called once, at startup
snd = pygame.mixer.Sound(filename)
loads a WAV file and returns a Sound object
snd.play()
starts a sound object playing, on a free channel
snd.stop()
stops a sound object playing
snd.set_volume(val)
changes playback volume of sound object
snd.fadeout(millisec)
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

pygame.mixer.music - 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

  pygame.mixer.music.load('musicfile.mp3')
  pygame.mixer.music.play()
  pygame.mixer.music.stop()
  pygame.mixer.music.fadeout(1000)





fmod

http://www.fmod.org/
http://www.fmod.org/docs/

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

Some advantages:






OpenAL

http://openal.org/


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.