5 Playing Sound

Video

http://youtu.be/KENYA_Kz6hg


Table of Contents

  1. Introduction
  2. Initializing Allegro
  3. Keyboard Input
  4. Drawing Graphics
  5. Playing Sound
  6. Drawing Text
  7. Regulating FPS
  8. Enumerations and Random Numbers
  9. Bounding Box Collision Detection
  10. Planning the Game
  11. Super Basic Game Structure
  12. Wrapping up

Downloads

Download entire guide as ODT


 

Part 5 – Playing Sound

Most games have sound and music. Without it, many games would seem unpolished. You can find public domain sound effects and music by doing a Google search.

Usually, wave files are sound effects and midi files should be the background music since wave takes up a lot of room.

Allegro code for wave files

You can also find other libraries for use with Allegro to let you load in other formats of sound, but on it’s own Allegro just loads wave and midi files.

Creating a sample

SAMPLE *doggy;

‘Sample’ types are used for wave files in Allegro.

Loading in the sample

doggy = load_sample( “woof.wav” );

Playing the sample

play_sample( doggy, 255, 128, 1000, false );

The parameters for play_sample are:
play_sample(sample, volume, pan, frequency, loop)
(taken from Allegro Quick Reference)

Volume ranges from 0 to 255.

Pan is how much sound is played on the left/right speakers. 128 means equally (or “center”, 0 is left speaker, 255 is rights speaker).

Frequency is playback rate, with 1000 being normal speed. 500 will be half speed and 2000 will be double speed.

If the loop parameter is true, the sound will continue to loop until

stop_sample( doggy );

is called.

Destroying the sample

Always destroy your sounds afterwards!

destroy_sample( doggy );

Allegro code for midi files

Creating a midi

Midis are similar to samples:

MIDI *bgsong;

Loading in the midi

bgsong = load_midi( “waffles.mid” );

Playing the midi

play_midi( bgsong, true );

First parameter is the midi file, and second one is whether it loops or not.

Destroying the midi

destroy_midi( bgsong );

© Rachel J. Morris, 2009

Print Friendly