naev 0.11.5
sound.c File Reference

Handles all the sound details. More...

#include "physfsrwops.h"
#include "sound.h"
#include "array.h"
#include "camera.h"
#include "conf.h"
#include "env.h"
#include "log.h"
#include "music.h"
#include "ndata.h"
#include "nstring.h"
#include "physics.h"
#include "player.h"
#include "nopenal.h"
#include "nlua_spfx.h"

Go to the source code of this file.

Data Structures

struct  alSound
 Contains a sound buffer. More...
 
struct  alVoice
 Represents a voice in the game. More...
 
struct  alGroup_t
 

Macros

#define SOUND_FADEOUT   100
 
#define SOUND_VOICES   64
 
#define SOUND_SUFFIX_WAV   ".wav"
 
#define SOUND_SUFFIX_OGG   ".ogg"
 
#define voiceLock()
 
#define voiceUnlock()
 

Enumerations

enum  voice_state_t { VOICE_STOPPED , VOICE_PLAYING , VOICE_FADEOUT , VOICE_DESTROY }
 The state of a voice. More...
 

Functions

static int sound_makeList (void)
 Makes the list of available sounds.
 
static void sound_free (alSound *snd)
 Frees the sound.
 
static int sound_al_init (void)
 Initializes the sound subsystem.
 
static const char * vorbis_getErr (int err)
 Gets the vorbisfile error in human readable form..
 
static int al_enableEFX (void)
 Enables the OpenAL EFX extension.
 
static int al_playVoice (alVoice *v, alSound *s, ALfloat px, ALfloat py, ALfloat vx, ALfloat vy, ALint relative)
 Plays a voice.
 
static int al_load (alSound *snd, SDL_RWops *rw, const char *name)
 Loads the sound.
 
static int al_loadWav (ALuint *buf, SDL_RWops *rw)
 Loads a wav file from the rw if possible.
 
static int al_loadOgg (ALuint *buf, OggVorbis_File *vf)
 Loads an ogg file from a tested format if possible.
 
static void al_pausev (ALint n, ALuint *s)
 Acts like alSourcePausev but with proper checks.
 
static void al_resumev (ALint n, ALuint *s)
 Acts like alSourcePlayv but with proper checks to just resume.
 
static alGroup_tal_getGroup (int group)
 Gets a group by ID.
 
static alVoicevoice_new (void)
 Gets a new voice ready to be used.
 
static int voice_add (alVoice *v)
 Adds a voice to the active voice stack.
 
static alVoicevoice_get (int id)
 Gets a voice by identifier.
 
static void al_updateVoice (alVoice *v)
 Updates the voice.
 
static void al_volumeUpdate (void)
 Internal volume update function.
 
static size_t ovpack_read (void *ptr, size_t size, size_t nmemb, void *datasource)
 
static int ovpack_seek (void *datasource, ogg_int64_t offset, int whence)
 
static int ovpack_close (void *datasource)
 
static int ovpack_closeFake (void *datasource)
 
static long ovpack_tell (void *datasource)
 
int sound_init (void)
 Initializes the sound subsystem.
 
void sound_exit (void)
 Cleans up after the sound subsytem.
 
int sound_get (const char *name)
 Gets the buffer to sound of name.
 
double sound_getLength (int sound)
 Gets the length of the sound buffer.
 
int sound_play (int sound)
 Plays the sound in the first available channel.
 
int sound_playPos (int sound, double px, double py, double vx, double vy)
 Plays a sound based on position.
 
int sound_updatePos (int voice, double px, double py, double vx, double vy)
 Updates the position of a voice.
 
int sound_update (double dt)
 Updates the sounds removing obsolete ones and such.
 
void sound_pause (void)
 Pauses all the sounds.
 
void sound_resume (void)
 Resumes all the sounds.
 
void sound_stopAll (void)
 Stops all the playing voices.
 
void sound_stop (int voice)
 Stops a voice from playing.
 
int sound_updateListener (double dir, double px, double py, double vx, double vy)
 Updates the sound listener.
 
void sound_setSpeed (double s)
 Sets the speed to play the sound at.
 
int sound_volume (const double vol)
 Sets the volume.
 
double sound_getVolume (void)
 Gets the current sound volume (linear).
 
double sound_getVolumeLog (void)
 Gets the current sound volume (logarithmic).
 
int sound_createGroup (int size)
 Creates a sound group.
 
int sound_playGroup (int group, int sound, int once)
 Plays a sound in a group.
 
void sound_stopGroup (int group)
 Stops all the sounds in a group.
 
void sound_pauseGroup (int group)
 Pauses all the sounds in a group.
 
void sound_resumeGroup (int group)
 Resumes all the sounds in a group.
 
static void groupSpeedReset (alGroup_t *g)
 
void sound_speedGroup (int group, int enable)
 Sets whether or not the speed affects a group.
 
void sound_volumeGroup (int group, double volume)
 Sets the volume of a group.
 
void sound_pitchGroup (int group, double pitch)
 Sets the pitch of a group.
 
void sound_setAbsorption (double value)
 
int sound_env (SoundEnv_t env_type, double param)
 Sets up the sound environment.
 
int source_newRW (SDL_RWops *rw, const char *name, unsigned int flags)
 Loads a new sound source from a RWops.
 
int source_new (const char *filename, unsigned int flags)
 Loads a new source from a file.
 
int sound_al_buffer (ALuint *buf, SDL_RWops *rw, const char *name)
 Loads the sound.
 
int sound_al_updatePos (alVoice *v, double px, double py, double vx, double vy)
 Updates the position of the sound.
 

Variables

int sound_disabled = 0
 
static int sound_initialized = 0
 
static alSoundsound_list = NULL
 
static int voice_genid = 0
 
static alVoicevoice_active = NULL
 
static alVoicevoice_pool = NULL
 
static SDL_mutex * voice_mutex = NULL
 
static int snd_compression = -1
 
static int snd_compressionG = -1
 
static double snd_compression_gain = 0.
 
SDL_mutex * sound_lock = NULL
 
static ALCcontext * al_context = NULL
 
static ALCdevice * al_device = NULL
 
static ALfloat svolume = 1.
 
static ALfloat svolume_lin = 1.
 
static ALfloat svolume_speed = 1.
 
alInfo_t al_info
 
static ALuint * source_stack = NULL
 
static ALuint * source_total = NULL
 
static ALuint * source_all = NULL
 
static int source_nstack = 0
 
static int source_ntotal = 0
 
static int source_nall = 0
 
static int source_mstack = 0
 
ALuint sound_efx_directSlot = 0
 
static ALuint efx_reverb = 0
 
static ALuint efx_echo = 0
 
static double sound_speed = 1.
 
static alGroup_tal_groups = NULL
 
static int al_ngroups = 0
 
static int al_groupidgen = 0
 
ov_callbacks sound_al_ovcall
 
ov_callbacks sound_al_ovcall_noclose
 

Detailed Description

Handles all the sound details.

We use a priority virtual voice system with pre-allocated buffers.

Naming: buffer - sound sample source - openal object that plays sound voice - virtual object that wants to play sound

1) First we allocate all the buffers based on what we find inside the datafile. 2) Then we allocate all the possible sources (giving the music system what it needs). 3) Now we allow the user to dynamically create voices, these voices will always try to grab a source from the source pool. If they can't they will pretend to play the buffer. 4) Every so often we'll check to see if the important voices are being played and take away the sources from the lesser ones.

EFX

We use multiple effects, namely:

  • Air absorption factor
  • Reverb

Definition in file sound.c.

Macro Definition Documentation

◆ SOUND_FADEOUT

#define SOUND_FADEOUT   100

Definition at line 60 of file sound.c.

◆ SOUND_SUFFIX_OGG

#define SOUND_SUFFIX_OGG   ".ogg"

Suffix of sounds.

Definition at line 64 of file sound.c.

◆ SOUND_SUFFIX_WAV

#define SOUND_SUFFIX_WAV   ".wav"

Suffix of sounds.

Definition at line 63 of file sound.c.

◆ SOUND_VOICES

#define SOUND_VOICES   64

Maximum number of simultaneous sounds to play, must be at least 16.

Definition at line 61 of file sound.c.

◆ voiceLock

#define voiceLock ( )
Value:
SDL_LockMutex(voice_mutex)
static SDL_mutex * voice_mutex
Definition sound.c:147

Definition at line 66 of file sound.c.

◆ voiceUnlock

#define voiceUnlock ( )
Value:
SDL_UnlockMutex(voice_mutex)

Definition at line 67 of file sound.c.

Enumeration Type Documentation

◆ voice_state_t

The state of a voice.

See also
alVoice
Enumerator
VOICE_STOPPED 

Voice is stopped.

VOICE_PLAYING 

Voice is playing.

VOICE_FADEOUT 

Voice is fading out.

VOICE_DESTROY 

Voice should get destroyed asap.

Definition at line 87 of file sound.c.

Function Documentation

◆ al_enableEFX()

static int al_enableEFX ( void )
static

Enables the OpenAL EFX extension.

Returns
0 on success.

Definition at line 501 of file sound.c.

◆ al_getGroup()

static alGroup_t * al_getGroup ( int group)
static

Gets a group by ID.

Definition at line 1847 of file sound.c.

◆ al_load()

int al_load ( alSound * snd,
SDL_RWops * rw,
const char * name )
static

Loads the sound.

Parameters
sndSound to load.
rwFile to load from.
nameName for debugging purposes.

Definition at line 2042 of file sound.c.

◆ al_loadOgg()

static int al_loadOgg ( ALuint * buf,
OggVorbis_File * vf )
static

Loads an ogg file from a tested format if possible.

Parameters
bufBuffer to load ogg into.
vfVorbisfile containing the song.

Definition at line 1940 of file sound.c.

◆ al_loadWav()

static int al_loadWav ( ALuint * buf,
SDL_RWops * rw )
static

Loads a wav file from the rw if possible.

Note
Closes the rw.
Parameters
bufBuffer to load wav into.
rwData for the wave.

Definition at line 1866 of file sound.c.

◆ al_pausev()

static void al_pausev ( ALint n,
ALuint * s )
static

Acts like alSourcePausev but with proper checks.

Definition at line 1821 of file sound.c.

◆ al_playVoice()

static int al_playVoice ( alVoice * v,
alSound * s,
ALfloat px,
ALfloat py,
ALfloat vx,
ALfloat vy,
ALint relative )
static

Plays a voice.

Definition at line 2102 of file sound.c.

◆ al_resumev()

static void al_resumev ( ALint n,
ALuint * s )
static

Acts like alSourcePlayv but with proper checks to just resume.

Definition at line 1834 of file sound.c.

◆ al_updateVoice()

void al_updateVoice ( alVoice * v)
static

Updates the voice.

Parameters
vVoice to update.

Definition at line 2180 of file sound.c.

◆ al_volumeUpdate()

static void al_volumeUpdate ( void )
static

Internal volume update function.

Definition at line 2077 of file sound.c.

◆ groupSpeedReset()

static void groupSpeedReset ( alGroup_t * g)
static

Definition at line 1536 of file sound.c.

◆ ovpack_close()

static int ovpack_close ( void * datasource)
static

Definition at line 260 of file sound.c.

◆ ovpack_closeFake()

static int ovpack_closeFake ( void * datasource)
static

Definition at line 265 of file sound.c.

◆ ovpack_read()

static size_t ovpack_read ( void * ptr,
size_t size,
size_t nmemb,
void * datasource )
static

Definition at line 250 of file sound.c.

◆ ovpack_seek()

static int ovpack_seek ( void * datasource,
ogg_int64_t offset,
int whence )
static

Definition at line 255 of file sound.c.

◆ ovpack_tell()

static long ovpack_tell ( void * datasource)
static

Definition at line 270 of file sound.c.

◆ sound_al_buffer()

int sound_al_buffer ( ALuint * buf,
SDL_RWops * rw,
const char * name )

Loads the sound.

Parameters
bufBuffer to load.
rwFile to load from.
nameName for debugging purposes.

Definition at line 2001 of file sound.c.

◆ sound_al_init()

static int sound_al_init ( void )
static

Initializes the sound subsystem.

Returns
0 on success.

Definition at line 293 of file sound.c.

◆ sound_al_updatePos()

int sound_al_updatePos ( alVoice * v,
double px,
double py,
double vx,
double vy )

Updates the position of the sound.

Definition at line 2165 of file sound.c.

◆ sound_createGroup()

int sound_createGroup ( int size)

Creates a sound group.

Parameters
sizeSize of the group.
Returns
ID of the group created on success, 0 on error.

Definition at line 1329 of file sound.c.

◆ sound_env()

int sound_env ( SoundEnv_t env_type,
double param )

Sets up the sound environment.

Parameters
env_typeType of environment to set up.
paramEnvironment parameter.
Returns
0 on success.

Definition at line 1649 of file sound.c.

◆ sound_exit()

void sound_exit ( void )

Cleans up after the sound subsytem.

Definition at line 665 of file sound.c.

◆ sound_free()

static void sound_free ( alSound * snd)
static

Frees the sound.

Parameters
sndSound to free.

Definition at line 1308 of file sound.c.

◆ sound_get()

int sound_get ( const char * name)

Gets the buffer to sound of name.

Parameters
nameName of the sound to get the id of.
Returns
ID of the sound matching name.

Definition at line 756 of file sound.c.

◆ sound_getLength()

double sound_getLength ( int sound)

Gets the length of the sound buffer.

Parameters
soundID of the buffer to get the length of..
Returns
The length of the buffer.

Definition at line 775 of file sound.c.

◆ sound_getVolume()

double sound_getVolume ( void )

Gets the current sound volume (linear).

Returns
The current sound volume level.

Definition at line 1282 of file sound.c.

◆ sound_getVolumeLog()

double sound_getVolumeLog ( void )

Gets the current sound volume (logarithmic).

Returns
The current sound volume level.

Definition at line 1295 of file sound.c.

◆ sound_init()

int sound_init ( void )

Initializes the sound subsystem.

Returns
0 on success.

Definition at line 602 of file sound.c.

◆ sound_makeList()

static int sound_makeList ( void )
static

Makes the list of available sounds.

Definition at line 1202 of file sound.c.

◆ sound_pause()

void sound_pause ( void )

Pauses all the sounds.

Definition at line 1010 of file sound.c.

◆ sound_pauseGroup()

void sound_pauseGroup ( int group)

Pauses all the sounds in a group.

Parameters
groupGroup to pause sounds.

Definition at line 1497 of file sound.c.

◆ sound_pitchGroup()

void sound_pitchGroup ( int group,
double pitch )

Sets the pitch of a group.

Parameters
groupGroup to set the pitch of.
pitchPitch to set to.

Definition at line 1605 of file sound.c.

◆ sound_play()

int sound_play ( int sound)

Plays the sound in the first available channel.

Parameters
soundSound to play.
Returns
Voice identifier on success.

Definition at line 789 of file sound.c.

◆ sound_playGroup()

int sound_playGroup ( int group,
int sound,
int once )

Plays a sound in a group.

Parameters
groupGroup to play sound in.
soundSound to play.
onceWhether to play only once.
Returns
0 on success.

Definition at line 1401 of file sound.c.

◆ sound_playPos()

int sound_playPos ( int sound,
double px,
double py,
double vx,
double vy )

Plays a sound based on position.

Parameters
soundSound to play.
pxX position of the sound.
pyY position of the sound.
vxX velocity of the sound.
vyY velocity of the sound.
Returns
Voice identifier on success.

Definition at line 827 of file sound.c.

◆ sound_resume()

void sound_resume ( void )

Resumes all the sounds.

Definition at line 1027 of file sound.c.

◆ sound_resumeGroup()

void sound_resumeGroup ( int group)

Resumes all the sounds in a group.

Parameters
groupGroup to resume sounds.

Definition at line 1519 of file sound.c.

◆ sound_setAbsorption()

void sound_setAbsorption ( double value)

Definition at line 1623 of file sound.c.

◆ sound_setSpeed()

void sound_setSpeed ( double s)

Sets the speed to play the sound at.

Parameters
sSpeed to play sound at.

Definition at line 1152 of file sound.c.

◆ sound_speedGroup()

void sound_speedGroup ( int group,
int enable )

Sets whether or not the speed affects a group.

Parameters
groupGroup to set if speed affects it.
enableWhether or not speed affects the group.

Definition at line 1551 of file sound.c.

◆ sound_stop()

void sound_stop ( int voice)

Stops a voice from playing.

Parameters
voiceIdentifier of the voice to stop.

Definition at line 1074 of file sound.c.

◆ sound_stopAll()

void sound_stopAll ( void )

Stops all the playing voices.

Definition at line 1044 of file sound.c.

◆ sound_stopGroup()

void sound_stopGroup ( int group)

Stops all the sounds in a group.

Parameters
groupGroup to stop all its sounds.

Definition at line 1477 of file sound.c.

◆ sound_update()

int sound_update ( double dt)

Updates the sounds removing obsolete ones and such.

Returns
0 on success.

Definition at line 908 of file sound.c.

◆ sound_updateListener()

int sound_updateListener ( double dir,
double px,
double py,
double vx,
double vy )

Updates the sound listener.

Parameters
dirDirection listener is facing.
pxX position of the listener.
pyY position of the listener.
vxX velocity of the listener.
vyY velocity of the listener.
Returns
0 on success.
See also
sound_playPos

Definition at line 1109 of file sound.c.

◆ sound_updatePos()

int sound_updatePos ( int voice,
double px,
double py,
double vx,
double vy )

Updates the position of a voice.

Parameters
voiceIdentifier of the voice to update.
pxNew x position to update to.
pyNew y position to update to.
vxNew x velocity of the sound.
vyNew y velocity of the sound.

Definition at line 884 of file sound.c.

◆ sound_volume()

int sound_volume ( const double vol)

Sets the volume.

Parameters
volVolume to set to.
Returns
0 on success.

Definition at line 1259 of file sound.c.

◆ sound_volumeGroup()

void sound_volumeGroup ( int group,
double volume )

Sets the volume of a group.

Parameters
groupGroup to set the volume of.
volumeVolume to set to in the [0-1] range.

Definition at line 1575 of file sound.c.

◆ source_new()

int source_new ( const char * filename,
unsigned int flags )

Loads a new source from a file.

Definition at line 1810 of file sound.c.

◆ source_newRW()

int source_newRW ( SDL_RWops * rw,
const char * name,
unsigned int flags )

Loads a new sound source from a RWops.

Definition at line 1786 of file sound.c.

◆ voice_add()

int voice_add ( alVoice * v)
static

Adds a voice to the active voice stack.

Parameters
vVoice to add to the active voice stack.
Returns
0 on success.

Definition at line 1732 of file sound.c.

◆ voice_get()

alVoice * voice_get ( int id)
static

Gets a voice by identifier.

Parameters
idIdentifier to look for.
Returns
Voice matching identifier or NULL if not found.

Definition at line 1767 of file sound.c.

◆ voice_new()

alVoice * voice_new ( void )
static

Gets a new voice ready to be used.

Returns
New voice ready to use.

Definition at line 1710 of file sound.c.

◆ vorbis_getErr()

static const char * vorbis_getErr ( int err)
static

Gets the vorbisfile error in human readable form..

Definition at line 1917 of file sound.c.

Variable Documentation

◆ al_context

ALCcontext* al_context = NULL
static

OpenAL context.

Definition at line 173 of file sound.c.

◆ al_device

ALCdevice* al_device = NULL
static

OpenAL device.

Definition at line 174 of file sound.c.

◆ al_groupidgen

int al_groupidgen = 0
static

Used to create group IDs.

Definition at line 208 of file sound.c.

◆ al_groups

alGroup_t* al_groups = NULL
static

Created groups.

Definition at line 206 of file sound.c.

◆ al_info

alInfo_t al_info

OpenAL context info.

Definition at line 178 of file sound.c.

◆ al_ngroups

int al_ngroups = 0
static

Number of created groups.

Definition at line 207 of file sound.c.

◆ efx_echo

ALuint efx_echo = 0
static

Echo effect.

Definition at line 196 of file sound.c.

◆ efx_reverb

ALuint efx_reverb = 0
static

Reverb effect.

Definition at line 195 of file sound.c.

◆ snd_compression

int snd_compression = -1
static

Compression sound.

Definition at line 152 of file sound.c.

◆ snd_compression_gain

double snd_compression_gain = 0.
static

Current compression gain.

Definition at line 154 of file sound.c.

◆ snd_compressionG

int snd_compressionG = -1
static

Compression sound group.

Definition at line 153 of file sound.c.

◆ sound_al_ovcall

ov_callbacks sound_al_ovcall
Initial value:
= {
.read_func = ovpack_read,
.seek_func = ovpack_seek,
.close_func = ovpack_close,
.tell_func = ovpack_tell
}

Vorbis call structure to handle rwops.

Definition at line 275 of file sound.c.

◆ sound_al_ovcall_noclose

ov_callbacks sound_al_ovcall_noclose
Initial value:
= {
.read_func = ovpack_read,
.seek_func = ovpack_seek,
.close_func = ovpack_closeFake,
.tell_func = ovpack_tell
}

Vorbis call structure to handle rwops without closing.

Definition at line 281 of file sound.c.

◆ sound_disabled

int sound_disabled = 0

Whether sound is disabled.

Definition at line 133 of file sound.c.

◆ sound_efx_directSlot

ALuint sound_efx_directSlot = 0

Direct 3d source slot.

Definition at line 194 of file sound.c.

◆ sound_initialized

int sound_initialized = 0
static

Whether or not sound is initialized.

Definition at line 134 of file sound.c.

◆ sound_list

alSound* sound_list = NULL
static

List of available sounds.

Definition at line 139 of file sound.c.

◆ sound_lock

SDL_mutex* sound_lock = NULL

Global sound lock, always lock this before using any OpenAL functions.

Definition at line 167 of file sound.c.

◆ sound_speed

double sound_speed = 1.
static

Sound speed.

Definition at line 201 of file sound.c.

◆ source_all

ALuint* source_all = NULL
static

All the sources.

Definition at line 185 of file sound.c.

◆ source_mstack

int source_mstack = 0
static

Memory allocated for sources in the pool.

Definition at line 189 of file sound.c.

◆ source_nall

int source_nall = 0
static

Total number of sources.

Definition at line 188 of file sound.c.

◆ source_nstack

int source_nstack = 0
static

Number of free sources in the pool.

Definition at line 186 of file sound.c.

◆ source_ntotal

int source_ntotal = 0
static

Number of general use sources.

Definition at line 187 of file sound.c.

◆ source_stack

ALuint* source_stack = NULL
static

Free source pool.

Definition at line 183 of file sound.c.

◆ source_total

ALuint* source_total = NULL
static

Total source pool.

Definition at line 184 of file sound.c.

◆ svolume

ALfloat svolume = 1.
static

Sound global volume (logarithmic).

Definition at line 175 of file sound.c.

◆ svolume_lin

ALfloat svolume_lin = 1.
static

Sound global volume (linear).

Definition at line 176 of file sound.c.

◆ svolume_speed

ALfloat svolume_speed = 1.
static

Sound global volume modulator for speed.

Definition at line 177 of file sound.c.

◆ voice_active

alVoice* voice_active = NULL
static

Active voices.

Definition at line 145 of file sound.c.

◆ voice_genid

int voice_genid = 0
static

Voice identifier generator.

Definition at line 144 of file sound.c.

◆ voice_mutex

SDL_mutex* voice_mutex = NULL
static

Lock for voices.

Definition at line 147 of file sound.c.

◆ voice_pool

alVoice* voice_pool = NULL
static

Pool of free voices.

Definition at line 146 of file sound.c.