naev 0.12.6
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 "log.h"
#include "music.h"
#include "ndata.h"
#include "nlua_spfx.h"
#include "nopenal.h"
#include "pilot.h"
Include dependency graph for sound.c:

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.
void rg_filter (float **pcm, long channels, long samples, void *filter_param)
 This is the filter function for the decoded Ogg Vorbis stream.
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
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 55 of file sound.c.

◆ SOUND_SUFFIX_OGG

#define SOUND_SUFFIX_OGG   ".ogg"

Suffix of sounds.

Definition at line 61 of file sound.c.

◆ SOUND_SUFFIX_WAV

#define SOUND_SUFFIX_WAV   ".wav"

Suffix of sounds.

Definition at line 60 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 56 of file sound.c.

◆ voiceLock

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

Definition at line 63 of file sound.c.

◆ voiceUnlock

#define voiceUnlock ( )
Value:
SDL_UnlockMutex( voice_mutex )

Definition at line 64 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 84 of file sound.c.

Function Documentation

◆ al_enableEFX()

int al_enableEFX ( void )
static

Enables the OpenAL EFX extension.

Returns
0 on success.

Definition at line 507 of file sound.c.

◆ al_getGroup()

alGroup_t * al_getGroup ( int group)
static

Gets a group by ID.

Definition at line 1856 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 2124 of file sound.c.

◆ al_loadOgg()

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 2003 of file sound.c.

◆ al_loadWav()

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 1875 of file sound.c.

◆ al_pausev()

void al_pausev ( ALint n,
ALuint * s )
static

Acts like alSourcePausev but with proper checks.

Definition at line 1830 of file sound.c.

◆ al_playVoice()

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

Plays a voice.

Definition at line 2183 of file sound.c.

◆ al_resumev()

void al_resumev ( ALint n,
ALuint * s )
static

Acts like alSourcePlayv but with proper checks to just resume.

Definition at line 1843 of file sound.c.

◆ al_updateVoice()

void al_updateVoice ( alVoice * v)
static

Updates the voice.

Parameters
vVoice to update.

Definition at line 2262 of file sound.c.

◆ al_volumeUpdate()

void al_volumeUpdate ( void )
static

Internal volume update function.

Definition at line 2158 of file sound.c.

◆ groupSpeedReset()

void groupSpeedReset ( alGroup_t * g)
static

Definition at line 1545 of file sound.c.

◆ ovpack_close()

int ovpack_close ( void * datasource)
static

Definition at line 259 of file sound.c.

◆ ovpack_closeFake()

int ovpack_closeFake ( void * datasource)
static

Definition at line 264 of file sound.c.

◆ ovpack_read()

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

Definition at line 248 of file sound.c.

◆ ovpack_seek()

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

Definition at line 254 of file sound.c.

◆ ovpack_tell()

long ovpack_tell ( void * datasource)
static

Definition at line 269 of file sound.c.

◆ rg_filter()

void rg_filter ( float ** pcm,
long channels,
long samples,
void * filter_param )

This is the filter function for the decoded Ogg Vorbis stream.

base on: vgfilter.c (c) 2007,2008 William Poetra Yoga Hadisoeseno based on: vgplay.c 1.0 (c) 2003 John Morton

Definition at line 1965 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 2083 of file sound.c.

◆ sound_al_init()

int sound_al_init ( void )
static

Initializes the sound subsystem.

Returns
0 on success.

Definition at line 291 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 2248 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 1338 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 1658 of file sound.c.

◆ sound_exit()

void sound_exit ( void )

Cleans up after the sound subsytem.

Definition at line 670 of file sound.c.

◆ sound_free()

void sound_free ( alSound * snd)
static

Frees the sound.

Parameters
sndSound to free.

Definition at line 1317 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 761 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 780 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 1291 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 1304 of file sound.c.

◆ sound_init()

int sound_init ( void )

Initializes the sound subsystem.

Returns
0 on success.

Definition at line 609 of file sound.c.

◆ sound_makeList()

int sound_makeList ( void )
static

Makes the list of available sounds.

Definition at line 1208 of file sound.c.

◆ sound_pause()

void sound_pause ( void )

Pauses all the sounds.

Definition at line 1015 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 1506 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 1614 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 794 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 1410 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 832 of file sound.c.

◆ sound_resume()

void sound_resume ( void )

Resumes all the sounds.

Definition at line 1032 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 1528 of file sound.c.

◆ sound_setAbsorption()

void sound_setAbsorption ( double value)

Definition at line 1632 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 1158 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 1560 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 1080 of file sound.c.

◆ sound_stopAll()

void sound_stopAll ( void )

Stops all the playing voices.

Definition at line 1049 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 1486 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 914 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 1115 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 890 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 1268 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 1584 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 1819 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 1795 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 1742 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 1776 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 1719 of file sound.c.

◆ vorbis_getErr()

const char * vorbis_getErr ( int err)
static

Gets the vorbisfile error in human readable form..

Definition at line 1927 of file sound.c.

Variable Documentation

◆ al_context

ALCcontext* al_context = NULL
static

OpenAL context.

Definition at line 170 of file sound.c.

◆ al_device

ALCdevice* al_device = NULL
static

OpenAL device.

Definition at line 171 of file sound.c.

◆ al_groupidgen

int al_groupidgen = 0
static

Used to create group IDs.

Definition at line 206 of file sound.c.

◆ al_groups

alGroup_t* al_groups = NULL
static

Created groups.

Definition at line 204 of file sound.c.

◆ al_info

alInfo_t al_info

OpenAL context info.

Definition at line 176 of file sound.c.

◆ al_ngroups

int al_ngroups = 0
static

Number of created groups.

Definition at line 205 of file sound.c.

◆ efx_echo

ALuint efx_echo = 0
static

Echo effect.

Definition at line 194 of file sound.c.

◆ efx_reverb

ALuint efx_reverb = 0
static

Reverb effect.

Definition at line 193 of file sound.c.

◆ snd_compression

int snd_compression = -1
static

Compression sound.

Definition at line 149 of file sound.c.

◆ snd_compression_gain

double snd_compression_gain = 0.
static

Current compression gain.

Definition at line 151 of file sound.c.

◆ snd_compressionG

int snd_compressionG = -1
static

Compression sound group.

Definition at line 150 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 274 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 279 of file sound.c.

◆ sound_disabled

int sound_disabled = 0

Whether sound is disabled.

Definition at line 130 of file sound.c.

◆ sound_efx_directSlot

ALuint sound_efx_directSlot = 0

Direct 3d source slot.

Definition at line 192 of file sound.c.

◆ sound_initialized

int sound_initialized = 0
static

Whether or not sound is initialized.

Definition at line 131 of file sound.c.

◆ sound_list

alSound* sound_list = NULL
static

List of available sounds.

Definition at line 136 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 164 of file sound.c.

◆ sound_speed

double sound_speed = 1.
static

Sound speed.

Definition at line 199 of file sound.c.

◆ source_all

ALuint* source_all = NULL
static

All the sources.

Definition at line 183 of file sound.c.

◆ source_mstack

int source_mstack = 0
static

Memory allocated for sources in the pool.

Definition at line 187 of file sound.c.

◆ source_nall

int source_nall = 0
static

Total number of sources.

Definition at line 186 of file sound.c.

◆ source_nstack

int source_nstack = 0
static

Number of free sources in the pool.

Definition at line 184 of file sound.c.

◆ source_ntotal

int source_ntotal = 0
static

Number of general use sources.

Definition at line 185 of file sound.c.

◆ source_stack

ALuint* source_stack = NULL
static

Free source pool.

Definition at line 181 of file sound.c.

◆ source_total

ALuint* source_total = NULL
static

Total source pool.

Definition at line 182 of file sound.c.

◆ svolume

ALfloat svolume = 1.
static

Sound global volume (logarithmic).

Definition at line 172 of file sound.c.

◆ svolume_lin

ALfloat svolume_lin = 1.
static

Sound global volume (linear).

Definition at line 173 of file sound.c.

◆ svolume_speed

ALfloat svolume_speed
static
Initial value:
=
1.

Sound global volume modulator for speed.

Definition at line 174 of file sound.c.

◆ voice_active

alVoice* voice_active = NULL
static

Active voices.

Definition at line 142 of file sound.c.

◆ voice_genid

int voice_genid = 0
static

Voice identifier generator.

Definition at line 141 of file sound.c.

◆ voice_mutex

SDL_mutex* voice_mutex = NULL
static

Lock for voices.

Definition at line 144 of file sound.c.

◆ voice_pool

alVoice* voice_pool = NULL
static

Pool of free voices.

Definition at line 143 of file sound.c.