FS2_Open
Open source remastering of the Freespace 2 engine
ds.cpp File Reference
#include "cfile/cfile.h"
#include "cmdline/cmdline.h"
#include "globalincs/pstypes.h"
#include "osapi/osapi.h"
#include "sound/acm.h"
#include "sound/audiostr.h"
#include "sound/channel.h"
#include "sound/ds.h"
#include "sound/ds3d.h"
#include "sound/dscap.h"
#include "sound/openal.h"
#include "sound/sound.h"

Go to the source code of this file.

Classes

struct  sound_buffer
 

Typedefs

typedef struct sound_buffer sound_buffer
 
typedef ALuint *typedef ALvoid(AL_APIENTRY *ALDELETEFILTERS) (ALsizei
 
typedef ALuint *typedef ALuint *typedef ALenum
 
typedef ALuint *typedef ALuint *typedef ALint
 
typedef ALfloat *typedef ALfloat *typedef ALuint *typedef ALuint *typedef ALboolean(AL_APIENTRY *ALISAUXILIARYEFFECTSLOT) (ALuint)
 
typedef ALint *typedef ALfloat
 
typedef ALfloat *ALGENFILTERS v_alGenFilters = NULL
 

Functions

typedef ALvoid (AL_APIENTRY *ALGENFILTERS)(ALsizei
 
int ds_parse_sound (CFILE *fp, ubyte **dest, uint *dest_size, WAVEFORMATEX **header, bool ogg, OggVorbis_File *ovf)
 Parse a wave file. More...
 
int ds_parse_sound_info (char *real_filename, sound_info *s_info)
 
int ds_get_sid ()
 
int ds_load_buffer (int *sid, int *final_size, void *header, sound_info *si, int flags)
 Load a secondary buffer with sound data. More...
 
void ds_init_channels ()
 
void ds_init_buffers ()
 
bool ds_check_for_openal_soft ()
 
int ds_init ()
 
void ds_close_channel (int i)
 
void ds_close_channel_fast (int i)
 
void ds_close_all_channels ()
 
void ds_unload_buffer (int sid)
 
void ds_close_buffers ()
 
void ds_close ()
 
int ds_get_free_channel_retail (float new_volume, int snd_id, int priority)
 
int ds_get_free_channel_enhanced (float new_volume, int snd_id, int enhanced_priority, unsigned int enhanced_limit)
 
int ds_get_free_channel (float volume, int snd_id, int priority, int &enhanced_priority, const EnhancedSoundData &enhanced_sound_data)
 
int ds_create_buffer (int frequency, int bits_per_sample, int nchannels, int nseconds)
 
int ds_lock_data (int sid, unsigned char *data, int size)
 
void ds_stop_easy (int sid)
 
int ds_play_easy (int sid, float volume)
 
int ds_play (int sid, int snd_id, int priority, const EnhancedSoundData *enhanced_sound_data, float volume, float pan, int looping, bool is_voice_msg)
 
int ds_get_channel (int sig)
 
int ds_is_channel_playing (int channel_id)
 
void ds_stop_channel (int channel_id)
 
void ds_stop_channel_all ()
 
void ds_set_volume (int channel_id, float vol)
 Set the volume for a channel. The volume is expected to be in linear scale. More...
 
void ds_set_pan (int channel_id, float pan)
 
int ds_get_pitch (int channel_id)
 
void ds_set_pitch (int channel_id, int pitch)
 
void ds_chg_loop_status (int channel_id, int loop)
 
int ds3d_play (int sid, int snd_id, vec3d *pos, vec3d *vel, float min, float max, int looping, float max_volume, float estimated_vol, const EnhancedSoundData *enhanced_sound_data, int priority, bool is_ambient)
 
void ds_set_position (int channel_id, unsigned int offset)
 
unsigned int ds_get_play_position (int channel_id)
 
unsigned int ds_get_write_position (int channel_id)
 
int ds_get_channel_size (int channel_id)
 
int ds_get_number_channels ()
 
int ds_get_data (int sid, char *data)
 
int ds_get_size (int sid, int *size)
 
int ds_eax_set_volume (float volume)
 
int ds_eax_set_decay_time (float seconds)
 
int ds_eax_set_damping (float damp)
 
int ds_eax_set_environment (unsigned long envid)
 
int ds_eax_set_all (unsigned long id, float vol, float damping, float decay)
 
int ds_eax_get_preset_id (const char *name)
 
int ds_eax_get_prop (EFXREVERBPROPERTIES **props, const char *name, const char *template_name)
 
int ds_eax_get_all (EAX_REVERBPROPERTIES *er, int id)
 
void ds_eax_close ()
 
int ds_eax_init ()
 
int ds_eax_is_inited ()
 
void ds_do_frame ()
 
int ds_get_sound_id (int channel_id)
 

Variables

channelChannels = NULL
 
const int BUFFER_BUMP = 50
 
SCP_vector< sound_buffersound_buffers
 
int Snd_sram
 
int Ds_sound_quality = DS_SQ_MEDIUM
 
int Ds_float_supported = 0
 
SCP_vector< EFXREVERBPROPERTIESEFX_presets
 
ALDELETEFILTERS v_alDeleteFilters = NULL
 
ALFILTERI v_alFilteri = NULL
 
ALGENEFFECTS v_alGenEffecs = NULL
 
ALDELETEEFFECTS v_alDeleteEffects = NULL
 
ALEFFECTI v_alEffecti = NULL
 
ALEFFECTF v_alEffectf = NULL
 
ALEFFECTFV v_alEffectfv = NULL
 
ALGETEFFECTF v_alGetEffectf = NULL
 
ALGENAUXILIARYEFFECTSLOTS v_alGenAuxiliaryEffectSlots = NULL
 
ALDELETEAUXILIARYEFFECTSLOTS v_alDeleteAuxiliaryEffectSlots = NULL
 
ALISAUXILIARYEFFECTSLOT v_alIsAuxiliaryEffectSlot = NULL
 
ALAUXILIARYEFFECTSLOTI v_alAuxiliaryEffectSloti = NULL
 
ALAUXILIARYEFFECTSLOTIV v_alAuxiliaryEffectSlotiv = NULL
 
ALAUXILIARYEFFECTSLOTF v_alAuxiliaryEffectSlotf = NULL
 
ALAUXILIARYEFFECTSLOTFV v_alAuxiliaryEffectSlotfv = NULL
 
ALCdevice * ds_sound_device = NULL
 
ALCcontext * ds_sound_context = NULL
 
ALuint AL_EFX_aux_id = 0
 
int ds_initialized = FALSE
 

Typedef Documentation

typedef ALfloat* typedef ALfloat* typedef ALuint* typedef ALuint* typedef ALboolean(AL_APIENTRY *ALISAUXILIARYEFFECTSLOT)(ALuint)

Definition at line 142 of file ds.cpp.

typedef ALenum

Definition at line 133 of file ds.cpp.

typedef ALfloat

Definition at line 145 of file ds.cpp.

typedef ALint

Definition at line 133 of file ds.cpp.

typedef ALvoid

Definition at line 132 of file ds.cpp.

typedef struct sound_buffer sound_buffer
typedef ALfloat* ALGENFILTERS v_alGenFilters = NULL

Definition at line 149 of file ds.cpp.

Function Documentation

typedef ALvoid ( AL_APIENTRY *  ALGENFILTERS)
int ds3d_play ( int  sid,
int  snd_id,
vec3d pos,
vec3d vel,
float  min,
float  max,
int  looping,
float  max_volume,
float  estimated_vol,
const EnhancedSoundData enhanced_sound_data,
int  priority,
bool  is_ambient 
)

Starts a ds3d sound playing

Parameters
sidSoftware id for sound to play
snd_idIdentifies what type of sound is playing
posWorld pos of sound
velVelocity of object emitting sound
minDistance at which sound doesn't get any louder
maxDistance at which sound becomes inaudible
loopingWhether to loop the sound or not
max_volumeVolume (0 to 1) for 3d sound at maximum
estimated_volManual estimated volume
priorityDS_MUST_PLAY, DS_LIMIT_ONE, DS_LIMIT_TWO, DS_LIMIT_THREE
Returns
0 if sound started successfully, -1 if sound could not be played

Definition at line 2044 of file ds.cpp.

bool ds_check_for_openal_soft ( )

Check if the player is using OpenAL Soft, which is required to use enhanced sound. Returns true on success, false otherwise.

Definition at line 1038 of file ds.cpp.

void ds_chg_loop_status ( int  channel_id,
int  loop 
)
Todo:
Documentation

Definition at line 2017 of file ds.cpp.

void ds_close ( )

Close the sound system

Definition at line 1344 of file ds.cpp.

void ds_close_all_channels ( )

Free all the channel buffers

Definition at line 1295 of file ds.cpp.

void ds_close_buffers ( )

Unload all the channel buffers

Definition at line 1330 of file ds.cpp.

void ds_close_channel ( int  i)

Free a single channel

Definition at line 1241 of file ds.cpp.

void ds_close_channel_fast ( int  i)

Definition at line 1268 of file ds.cpp.

int ds_create_buffer ( int  frequency,
int  bits_per_sample,
int  nchannels,
int  nseconds 
)

Create a sound buffer in software, without locking any data in

Definition at line 1667 of file ds.cpp.

void ds_do_frame ( )

Called once per game frame to make sure voice messages aren't looping

Definition at line 2606 of file ds.cpp.

void ds_eax_close ( )

Close down EAX, freeing any allocated resources

Definition at line 2500 of file ds.cpp.

int ds_eax_get_all ( EAX_REVERBPROPERTIES er,
int  id 
)

Get up the parameters for the current environment

Parameters
er(output) Hold environment parameters
idIf set will get specified preset env, otherwise current env
Returns
0 if successful, otherwise return -1

Definition at line 2468 of file ds.cpp.

int ds_eax_get_preset_id ( const char *  name)

Definition at line 2376 of file ds.cpp.

int ds_eax_get_prop ( EFXREVERBPROPERTIES **  props,
const char *  name,
const char *  template_name 
)

Definition at line 2393 of file ds.cpp.

int ds_eax_init ( )

Initialize EAX

Returns
0 if initialization is successful, otherwise return -1

Definition at line 2521 of file ds.cpp.

int ds_eax_is_inited ( )
Todo:
Documentation

Definition at line 2598 of file ds.cpp.

int ds_eax_set_all ( unsigned long  id,
float  vol,
float  damping,
float  decay 
)

Set up all the parameters for an environment

Parameters
idValue from the EAX_ENVIRONMENT_* enumeration
volVolume for the environment (0 to 1.0)
dampingDamp value for the environment (0 to 2.0)
decayDecay time in seconds (0.1 to 20.0)
Returns
0 if successful, otherwise return -1

Definition at line 2348 of file ds.cpp.

int ds_eax_set_damping ( float  damp)

Set the damping value for the EAX environment (ie all sound sources)

Parameters
dampDamp value from 0 to 2.0
Returns
0 if the damp value is successfully set, otherwise return -1

Definition at line 2310 of file ds.cpp.

int ds_eax_set_decay_time ( float  seconds)

Set the decay time for the EAX environment (ie all sound sources)

Parameters
secondsDecay time in seconds
Returns
0 if decay time is successfully set, otherwise return -1

Definition at line 2289 of file ds.cpp.

int ds_eax_set_environment ( unsigned long  envid)

Set up the environment type for all sound sources.

Parameters
envidValue from the EAX_ENVIRONMENT_* enumeration in ds_eax.h
Returns
Always returns 0.
Todo:
Proper error reporting, otherwise make a void return type.

Definition at line 2332 of file ds.cpp.

int ds_eax_set_volume ( float  volume)

Set the master volume for the reverb added to all sound sources.

Parameters
volumeVolume, range from 0 to 1.0
Returns
0 if the volume is set successfully, otherwise return -1

Definition at line 2268 of file ds.cpp.

int ds_get_channel ( int  sig)

Return the channel number that is playing the sound identified by sig.

Returns
Channel number, if not playing, return -1.

Definition at line 1870 of file ds.cpp.

int ds_get_channel_size ( int  channel_id)
Todo:
Documentation

Definition at line 2177 of file ds.cpp.

int ds_get_data ( int  sid,
char *  data 
)

Retreive raw data from a sound buffer

Definition at line 2225 of file ds.cpp.

int ds_get_free_channel ( float  volume,
int  snd_id,
int  priority,
int enhanced_priority,
const EnhancedSoundData enhanced_sound_data 
)

Generic function for getting a free channel If enhanced soudn is used, set priority to the computed priority. Returns -1 if no free channel could be found. Find a free channel to play a sound on. If no free channels exists, free up one based on volume levels. This is the new generic version of ds_get_free_channel().

Parameters
new_volumeVolume for sound to play at
snd_idWhich kind of sound to play
priorityFrom retail :DS_MUST_PLAY, DS_LIMIT_ONE, DS_LIMIT_TWO, DS_LIMIT_THREE
enhanced_priorityOutput param that's updated with correct priority if enhanced sound is enabled
Returns
Channel number to play sound on, or -1 if no channel could be found

NOTE: snd_id is needed since we limit the number of concurrent samples

Definition at line 1641 of file ds.cpp.

int ds_get_free_channel_enhanced ( float  new_volume,
int  snd_id,
int  enhanced_priority,
unsigned int  enhanced_limit 
)

Find a free channel to play a sound on. If no free channels exists, free up one based on priority and volume levels. Special version for enhanced mode.

Parameters
new_volumeVolume for sound to play at
snd_idWhich kind of sound to play
enhanced_priorityPriority level, see EnhancedSoundPriority enum in gamesnd.h
enhanced_limitPer-sound concurrency limit
Returns
Channel number to play sound on, or -1 if no channel could be found

NOTE: snd_id is needed since we limit the number of concurrent samples

Definition at line 1514 of file ds.cpp.

int ds_get_free_channel_retail ( float  new_volume,
int  snd_id,
int  priority 
)

Find a free channel to play a sound on. If no free channels exists, free up one based on volume levels. This is the original retail version of ds_get_free_channel().

Parameters
new_volumeVolume for sound to play at
snd_idWhich kind of sound to play
priorityDS_MUST_PLAY, DS_LIMIT_ONE, DS_LIMIT_TWO, DS_LIMIT_THREE
Returns
Channel number to play sound on, or -1 if no channel could be found

NOTE: snd_id is needed since we limit the number of concurrent samples

Definition at line 1380 of file ds.cpp.

int ds_get_number_channels ( )

Returns the number of channels that are actually playing

Definition at line 2202 of file ds.cpp.

int ds_get_pitch ( int  channel_id)

Get the pitch of a channel

Definition at line 1965 of file ds.cpp.

unsigned int ds_get_play_position ( int  channel_id)
Todo:
Documentation

Definition at line 2127 of file ds.cpp.

int ds_get_sid ( )

Definition at line 644 of file ds.cpp.

int ds_get_size ( int  sid,
int size 
)

Return the size of the raw sound data

Definition at line 2233 of file ds.cpp.

int ds_get_sound_id ( int  channel_id)

Given a valid channel return the sound id

Definition at line 2639 of file ds.cpp.

unsigned int ds_get_write_position ( int  channel_id)
Todo:
Documentation

Definition at line 2169 of file ds.cpp.

int ds_init ( )

Sound initialisation

Returns
-1 if init failed, 0 if init success

Definition at line 1060 of file ds.cpp.

void ds_init_buffers ( )

Initialise the both the software and hardware buffers

Definition at line 1025 of file ds.cpp.

void ds_init_channels ( )

Initialise the Channels[] array

Definition at line 1011 of file ds.cpp.

int ds_is_channel_playing ( int  channel_id)
Todo:
Documentation

Definition at line 1888 of file ds.cpp.

int ds_load_buffer ( int sid,
int final_size,
void header,
sound_info si,
int  flags 
)

Load a secondary buffer with sound data.

The sounds data for game sounds are stored in the DirectSound secondary buffers, and are duplicated as needed and placed in the Channels[] array to be played.

Parameters
sidPointer to software id for sound ( output parm)
final_sizePointer to storage to receive uncompressed sound size (output parm)
headerPointer to a WAVEFORMATEX structure
sisound_info structure, contains details on the sound format
flagsBuffer properties ( DS_HARDWARE , DS_3D )
Returns
1 if sound effect could not loaded into a secondary buffer, 0 if sound effect successfully loaded into a secondary buffer

NOTE: this function is slow, especially when sounds are loaded into hardware. Don't call this function from within gameplay.

Definition at line 676 of file ds.cpp.

int ds_lock_data ( int  sid,
unsigned char *  data,
int  size 
)

Lock data into an existing buffer

Definition at line 1698 of file ds.cpp.

int ds_parse_sound ( CFILE fp,
ubyte **  dest,
uint dest_size,
WAVEFORMATEX **  header,
bool  ogg,
OggVorbis_File *  ovf 
)

Parse a wave file.

Parameters
fpFile of sound to parse
destAddress of pointer of where to store raw sound data (output parm)
dest_sizeNumber of bytes of sound data stored (output parm)
headerAddress of pointer to a WAVEFORMATEX struct (output parm)
oggBoolean to indicate OGG vorbis file, if false assume Wave file
ovfPointer to a OggVorbis_File struct, OGG vorbis only (output parm)
Returns
0 if wave file successfully parsed, -1 if an error occurred
 NOTE: memory is malloced for the header and dest (if not OGG) in this function.  It is the responsibility
 of the caller to free this memory later.

Definition at line 246 of file ds.cpp.

int ds_parse_sound_info ( char *  real_filename,
sound_info s_info 
)

Parse a sound file, any format, and store the info in s_info.

Parameters
real_filenameFilename to parse
s_infoStorage for the sound file info

Definition at line 457 of file ds.cpp.

int ds_play ( int  sid,
int  snd_id,
int  priority,
const EnhancedSoundData enhanced_sound_data,
float  volume,
float  pan,
int  looping,
bool  is_voice_msg 
)

Play a sound secondary buffer.

Parameters
sidSoftware id of sound
snd_idWhat kind of sound this is
priorityDS_MUST_PLAY, DS_LIMIT_ONE, DS_LIMIT_TWO, DS_LIMIT_THREE
volumeVolume of sound effect in DirectSound units
panPan of sound in sound units
loopingWhether the sound effect is looping or not
is_voice_msgIf a voice message
Returns
1 if sound effect could not be started, >=0 sig for sound effect successfully started

Definition at line 1794 of file ds.cpp.

int ds_play_easy ( int  sid,
float  volume 
)

Play a sound without the usual baggage (used for playing back real-time voice)

Parameters
sidSoftware id of sound
volumeVolume of sound effect in linear scale

Definition at line 1738 of file ds.cpp.

void ds_set_pan ( int  channel_id,
float  pan 
)

Set the pan for a channel. The pan is expected to be in DirectSound units

Definition at line 1946 of file ds.cpp.

void ds_set_pitch ( int  channel_id,
int  pitch 
)

Set the pitch of a channel

Definition at line 1990 of file ds.cpp.

void ds_set_position ( int  channel_id,
unsigned int  offset 
)
Todo:
Documentation

Definition at line 2115 of file ds.cpp.

void ds_set_volume ( int  channel_id,
float  vol 
)

Set the volume for a channel. The volume is expected to be in linear scale.

If the sound is a 3D sound buffer, this is like re-establishing the maximum volume.

Definition at line 1929 of file ds.cpp.

void ds_stop_channel ( int  channel_id)
Todo:
Documentation

Definition at line 1904 of file ds.cpp.

void ds_stop_channel_all ( )
Todo:
Documentation

Definition at line 1914 of file ds.cpp.

void ds_stop_easy ( int  sid)

Stop a buffer from playing directly

Definition at line 1720 of file ds.cpp.

void ds_unload_buffer ( int  sid)

Unload a buffer

Definition at line 1307 of file ds.cpp.

Variable Documentation

ALuint AL_EFX_aux_id = 0

Definition at line 174 of file ds.cpp.

const int BUFFER_BUMP = 50

Definition at line 49 of file ds.cpp.

channel* Channels = NULL

Definition at line 46 of file ds.cpp.

int Ds_float_supported = 0

Definition at line 62 of file ds.cpp.

int ds_initialized = FALSE

Definition at line 229 of file ds.cpp.

ALCcontext* ds_sound_context = NULL

Definition at line 172 of file ds.cpp.

ALCdevice* ds_sound_device = NULL

Definition at line 171 of file ds.cpp.

int Ds_sound_quality = DS_SQ_MEDIUM

Definition at line 61 of file ds.cpp.

Definition at line 128 of file ds.cpp.

int Snd_sram

Definition at line 52 of file sound.cpp.

SCP_vector<sound_buffer> sound_buffers

Definition at line 50 of file ds.cpp.

ALAUXILIARYEFFECTSLOTF v_alAuxiliaryEffectSlotf = NULL

Definition at line 168 of file ds.cpp.

ALAUXILIARYEFFECTSLOTFV v_alAuxiliaryEffectSlotfv = NULL

Definition at line 169 of file ds.cpp.

ALAUXILIARYEFFECTSLOTI v_alAuxiliaryEffectSloti = NULL

Definition at line 166 of file ds.cpp.

ALAUXILIARYEFFECTSLOTIV v_alAuxiliaryEffectSlotiv = NULL

Definition at line 167 of file ds.cpp.

ALDELETEAUXILIARYEFFECTSLOTS v_alDeleteAuxiliaryEffectSlots = NULL

Definition at line 163 of file ds.cpp.

ALDELETEEFFECTS v_alDeleteEffects = NULL

Definition at line 155 of file ds.cpp.

ALDELETEFILTERS v_alDeleteFilters = NULL

Definition at line 150 of file ds.cpp.

ALEFFECTF v_alEffectf = NULL

Definition at line 158 of file ds.cpp.

ALEFFECTFV v_alEffectfv = NULL

Definition at line 159 of file ds.cpp.

ALEFFECTI v_alEffecti = NULL

Definition at line 157 of file ds.cpp.

ALFILTERI v_alFilteri = NULL

Definition at line 152 of file ds.cpp.

ALGENAUXILIARYEFFECTSLOTS v_alGenAuxiliaryEffectSlots = NULL

Definition at line 162 of file ds.cpp.

ALGENEFFECTS v_alGenEffecs = NULL

Definition at line 154 of file ds.cpp.

ALGETEFFECTF v_alGetEffectf = NULL

Definition at line 160 of file ds.cpp.

ALISAUXILIARYEFFECTSLOT v_alIsAuxiliaryEffectSlot = NULL

Definition at line 165 of file ds.cpp.