Game Development Reference
In-Depth Information
Audio Handler Changes
To implement your own audio handler code for Android, you must take a look at the sound
data structure defined in sound.h (see Listing 6-14). Quake uses a direct memory access
(DMA) technique, where the most relevant values are as follows:
channels : The number of audio channels (1 for mono, 2 for stereo).
samples : The size of the audio buffer.
submission_chunk : This has something to do with how Quake mixed the
audio buffer internally, but it is not relevant in this case.
samplepos : This is the position or the current audio byte being played.
samplebits : This is the audio resolution. Quake uses 16-bit WAV audio.
speed : This is the audio frequency. It defaults to 22 kHz.
Note that there are also two sound-related variables: shm and sn . The first one is a pointer
used to do all the behind-the-scenes work and it must point to sn , which is the real audio
data structure. This is done on audio initialization, as seen in the “Fixing the Game Loop”
section later in this chapter.
Listing 6-14. Quake Audio Data Structure
// In sound.h
// Internal Audio data structure
typedef struct
{
qboolean gamealive;
qboolean soundalive;
qboolean splitbuffer;
int channels;
int samples; // mono samples in buffer
int submission_chunk; // don't mix less than this #
int samplepos; // in mono samples
int samplebits;
int speed;
unsigned char *buffer;
} dma_t;
extern volatile dma_t *shm;
extern volatile dma_t sn;
With this information, you can easily implement a custom audio handler for Android. In
Listing 6-15, you have the file snd_android.c , which implements the following functions:
SNDDMA_Init : Required to initialize the audio handler. It defined the
audio parameters described in Listing 6-14: audio resolution (16 bit),
frequency (22kHz), number of channels (2 for stereo), the size of the
samples (audio buffer); it also tells Quake that audio has been initialized
successfully.
SNDDMA_GetDMAPos : Tells Quake the current position in the audio buffer.
 
Search WWH ::




Custom Search