Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


13. November 2017


In the first tutorial we looked at creating our initial application, while in the second tutorial we looked at events and a cleaner game loop, in the third tutorial we looked at drawing bitmaps on screen.  We then moved on to handling the keyboard and mouse while in this tutorial we are going to cover handling audio in Allegro.


Once again, lets jump straight into the code example.

#include "stdafx.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>

int main()
{
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_SAMPLE *sample = NULL;
	ALLEGRO_SAMPLE_INSTANCE *sampleInstance = NULL;

	al_init();
	al_install_audio();
	al_init_acodec_addon();
	
	al_reserve_samples(1);

	sample = al_load_sample("explode.wav");

	// The commented out version below is much easier
	//al_play_sample(sample, 1.0, 0, 1, ALLEGRO_PLAYMODE_LOOP,NULL);

	//However if you need access to additional control, such as currently playing position, you need
	//to attach it to a mixer, like the following example
	sampleInstance = al_create_sample_instance(sample);
	al_attach_sample_instance_to_mixer(sampleInstance, al_get_default_mixer());
	al_play_sample_instance(sampleInstance);

	// Loop until sound effect is done playing
	while (al_get_sample_instance_playing(sampleInstance)) {}

	al_destroy_sample_instance(sampleInstance);
	al_destroy_sample(sample);
	al_uninstall_audio();
}


At this point it should come as no surprise that audio support in Allegro is implemented as an add-on.  In fact it’s implemented as a pair of add-ons.

image


The audio add-on implements the audio playback functionality, while the Codec add-on is what supports loading the various different audio file formats.  Speak of file formats, Allegro supports the following file formats: .wav, .flac, .ogg, .opus, .it, .mod, .s3m, .xm and .voc.  Since they are implemented as add-ons, we once again need to call the corresponding init or install function before we can use audio functions.


Audio files are loading into memory as samples.  Before we go to far, we need to reserve resources for the samples we are going to use.  In this simple example we have only a single example, thus we call al_reserve_samples(1).  Now that our resources are allocated, we actually load the sample from disk with a call to al_load_sample(), in this case I am passing in a sample named “explode.wav”, which I have copied into my debug directory. 


Now that our sample is loaded into memory there are two ways to access it.  One is very simple, a fire and forget function you can see commented in the example above.

	al_play_sample(sample, 1.0, 0, 1, ALLEGRO_PLAYMODE_LOOP,NULL);

This is the easiest way by far to play audio in Allegro.  This example will play the sample “sample” at normal volume (gain), centered, at regular 1x speed, in a constant loop.


If you want a bit more control over the audio playback however, this function will no longer suffice.  So for example if you want to be able to determine when a sample is done playing, you instead need to create a SAMPLE_INSTANCE, which is done by passing our sample to al_create_sample_instance().  We also need a mixer to actually play the instance, which thankfully there is one already available for us, accessible using the function al_get_default_mixer(). 


Notice that both the sample and instance need to be cleaned up after use or they will leak resources.  Not shown in this tutorial, there are also streaming functions for breaking larger audio files up into smaller chunks for streaming.  In this day of abundant RAM, these functions are less used, so I have not covered them here.


Back to Table Of Contents

Programming , ,

blog comments powered by Disqus

Month List

Popular Comments