Cross-Platform Game Development for C++ Developers, Part II: The Allegro Platform, Page 3
Audio for Allegro
The snooker demo looked at some of the most rudimentary graphics and I/O functions but did not touch on Allegro's audio toolkit. The MIDI mixing, sound effects, and recording API meet or exceed almost every other dedicated sound library I've ever run across. Allegro audio apps abound, including WinCab, an MP3 and OGG Vorbis music jukebox, and LouTronic Rhythm Box, a drumbeat generator synth with fully tweakable snare, bass drum, and hi hat. I'll review just a small portion of the Allegro audio API here.
Every program that uses audio should call reserve_voices() to specify the number of voices that the digital and MIDI sound drivers use, respectively. Later on, you can control the mixing of these audio channels.
You can insert an audio track as easily as this:
MIDI *midFile = load_midi("myfile.mid'); play_midi(midFile, TRUE); // loop continuously
For more sophisticated needs, you can install one of three hook functions that allow you to intercept MIDI player events. If set to anything other than NULL, these routines will be called for each MIDI message, meta-event, and system-exclusive data block, respectively.
Allegro's digital audio system is designed from the ground up to be extensible. You can easily install readers and writers to handle new or variant audio file types, for example:
register_sample_file_type("mp3", load_mp3, NULL); // install my // MP3 reader
Digital audio can be edited on the fly, while it is playing. The following code alters the parameters of a sample while it is playing (useful for manipulating looped sounds):
void adjust_sample(const SAMPLE *spl, int vol, int pan, int freq, int loop);
You can alter the volume, pan, and frequency, and clear the loop flag, which will stop the sample when it next reaches the end of its loop. If several copies of the same sample are playing, this will adjust the first one it comes across. If the sample is not playing, it has no effect.
Learn More About Allegro
If you prefer a step-by-step approach to learning Allegro, you can't do better than Game Programming All in One, 2nd Ed by Jonathan Harbour (ISBN 1-59200-383-4). It works through the core features of the Allegro game library and demonstrates writing code to load images, manipulate sprites, scroll the background, use double-buffering, read a joystick, detect collisions, and implement other core features of any game. Each new chapter builds on the game already in progress.
Along the way, you'll learn about game theory, including artificial intelligence, game physics, mathematics, algorithms, and multiplayer programming, which are integrated into the featured game. The publisher even includes a CD-ROM so you have everything you need to get started.
About the Author
Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.