FS2_Open
Open source remastering of the Freespace 2 engine
sound.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 
12 #ifndef __SOUND_H__
13 #define __SOUND_H__
14 
15 #include "globalincs/pstypes.h"
16 
17 // Used for keeping track which low-level sound library is being used
18 #define SOUND_LIB_DIRECTSOUND 0
19 #define SOUND_LIB_RSX 1
20 
21 #define GAME_SND_USE_DS3D (1<<1)
22 #define GAME_SND_VOICE (1<<2)
23 
24 // Priorities that can be passed to snd_play() functions to limit how many concurrent sounds of a
25 // given type are played.
26 #define SND_PRIORITY_MUST_PLAY 0
27 #define SND_PRIORITY_SINGLE_INSTANCE 1
28 #define SND_PRIORITY_DOUBLE_INSTANCE 2
29 #define SND_PRIORITY_TRIPLE_INSTANCE 3
30 
31 // jg18 - new priority system
33 {
41 };
42 
44 {
45  int priority;
46  unsigned int limit; // limit on how many instances of the sound can play concurrently
47 
49  priority(SND_ENHANCED_PRIORITY_INVALID), limit(0)
50  {
51  }
52 
53  EnhancedSoundData(const int new_priority, const unsigned int new_limit) :
54  priority(new_priority), limit(new_limit)
55  {
57  "EnhancedSoundData ctor given invalid priority %d", priority);
58  Assertion(limit > 0, "EnhancedSoundData ctor given invalid limit %d", limit);
59  }
60 };
61 
62 extern const unsigned int SND_ENHANCED_MAX_LIMIT;
63 
64 //For the adjust-audio-volume sexp
65 #define AAV_MUSIC 0
66 #define AAV_VOICE 1
67 #define AAV_EFFECTS 2
68 
72 class game_snd
73 {
74 public:
79  int min;
80  int max;
81  bool preload;
82  int id;
83  int id_sig;
84  int flags;
86 
87  game_snd( );
88 };
89 
90 typedef struct sound_env
91 {
92  int id;
93  float volume;
94  float damping;
95  float decay;
96 } sound_env;
97 
98 extern int Sound_enabled;
99 extern float Master_sound_volume; // 0 -> 1.0
100 extern float Master_voice_volume; // 0 -> 1.0
101 extern int Snd_sram; // System memory consumed by sound data
102 extern int Snd_hram; // Soundcard memory consumed by sound data
103 extern float aav_voice_volume;
104 extern float aav_music_volume;
105 extern float aav_effect_volume;
106 
107 //int snd_load( char *filename, int hardware=0, int three_d=0, int *sig=NULL );
108 int snd_load( game_snd *gs, int allow_hardware_load = 0);
109 
110 int snd_unload( int sndnum );
111 void snd_unload_all();
112 
113 // Plays a sound with volume between 0 and 1.0, where 0 is the
114 // inaudible and 1.0 is the loudest sound in the game.
115 // Pan goes from -1.0 all the way left to 0.0 in center to 1.0 all the way right.
116 int snd_play( game_snd *gs, float pan=0.0f, float vol_scale=1.0f, int priority = SND_PRIORITY_SINGLE_INSTANCE, bool voice_message = false );
117 
118 // Play a sound directly from index returned from snd_load(). Bypasses
119 // the sound management process of using game_snd.
120 int snd_play_raw( int soundnum, float pan, float vol_scale=1.0f, int priority = SND_PRIORITY_MUST_PLAY );
121 
122 // Plays a sound with volume between 0 and 1.0, where 0 is the
123 // inaudible and 1.0 is the loudest sound in the game. It scales
124 // the pan and volume relative to the current viewer's location.
125 int snd_play_3d(game_snd *gs, vec3d *source_pos, vec3d *listen_pos, float radius=0.0f, vec3d *vel = NULL, int looping = 0, float vol_scale=1.0f, int priority = SND_PRIORITY_SINGLE_INSTANCE, vec3d *sound_fvec = NULL, float range_factor = 1.0f, int force = 0, bool is_ambient = false );
126 
127 // update the given 3d sound with a new position
128 void snd_update_3d_pos(int soudnnum, game_snd *gs, vec3d *new_pos, float radius = 0.0f, float range_factor = 1.0f);
129 
130 // Use these for looping sounds.
131 // Returns the handle of the sound. -1 if failed.
132 // If startloop or stoploop are not -1, then then are used.
133 int snd_play_looping( game_snd *gs, float pan=0.0f, int start_loop=-1, int stop_loop=-1, float vol_scale=1.0f, int scriptingUpdateVolume = 1);
134 
135 void snd_stop( int snd_handle );
136 
137 // Sets the volume of a sound that is already playing.
138 // The volume is between 0 and 1.0, where 0 is the
139 // inaudible and 1.0 is the loudest sound in the game.
140 void snd_set_volume( int snd_handle, float volume );
141 
142 // Sets the panning location of a sound that is already playing.
143 // Pan goes from -1.0 all the way left to 0.0 in center to 1.0 all the way right.
144 void snd_set_pan( int snd_handle, float pan );
145 
146 // Sets the pitch (frequency) of a sound that is already playing
147 // Valid values for pitch are between 100 and 100000
148 void snd_set_pitch( int snd_handle, int pitch );
149 int snd_get_pitch( int snd_handle );
150 
151 // Stops all sounds from playing, even looping ones.
152 void snd_stop_all();
153 
154 // determines if the sound handle is still palying
155 int snd_is_playing( int snd_handle );
156 
157 // change the looping status of a sound that is playing
158 void snd_chg_loop_status(int snd_handle, int loop);
159 
160 // return the time in ms for the duration of the sound
161 int snd_get_duration(int snd_id);
162 
163 // Get the file name of the specified sound
164 const char *snd_get_filename(int snd_id);
165 
166 // get a 3D vol and pan for a particular sound
167 int snd_get_3d_vol_and_pan(game_snd *gs, vec3d *pos, float* vol, float *pan, float radius=0.0f, float range_factor=1.0f);
168 
169 int snd_init();
170 void snd_close();
171 
172 // Return 1 or 0 to show that sound system is inited ok
173 int snd_is_inited();
174 
176 
177 void snd_use_lib(int lib_id);
178 
179 int snd_num_playing();
180 
181 int snd_get_data(int handle, char *data);
182 int snd_size(int handle, int *size);
183 void snd_do_frame();
184 void snd_adjust_audio_volume(int type, float percent, int time);
185 
186 // repositioning of the sound buffer pointer
187 void snd_rewind(int snd_handle, game_snd *sg, float seconds); // rewind N seconds from the current position
188 void snd_ffwd(int snd_handle, game_snd *sg, float seconds); // fast forward N seconds from the current position
189 void snd_set_pos(int snd_handle, game_snd *sg, float val,int as_pct); // set the position val as either a percentage (if as_pct) or as a # of seconds into the sound
190 
191 void snd_get_format(int handle, int *bits_per_sample, int *frequency);
192 int snd_time_remaining(int handle);
193 
194 int snd_get_samples_per_measure(char *filename, float num_measures);
195 
196 // sound environment
197 extern unsigned int SND_ENV_DEFAULT;
198 
199 int sound_env_set(sound_env *se);
200 int sound_env_get(sound_env *se, int preset = -1);
201 int sound_env_disable();
202 int sound_env_supported();
203 
204 // adjust-audio-volume
205 void snd_aav_init();
206 
207 #endif
#define MAX_FILENAME_LEN
Definition: pstypes.h:324
void snd_do_frame()
Definition: sound.cpp:1411
struct sound_env sound_env
int snd_play_looping(game_snd *gs, float pan=0.0f, int start_loop=-1, int stop_loop=-1, float vol_scale=1.0f, int scriptingUpdateVolume=1)
Definition: sound.cpp:822
LOCAL state_stack gs[GS_STACK_SIZE]
uint signature
Unique signature of this sound.
Definition: sound.h:77
const unsigned int SND_ENHANCED_MAX_LIMIT
Definition: sound.cpp:35
bool preload
preload sound (ie read from disk before mission starts)
Definition: sound.h:81
int Snd_sram
Definition: sound.cpp:52
int min
distance at which sound will stop getting louder
Definition: sound.h:79
void snd_update_listener(vec3d *pos, vec3d *vel, matrix *orient)
Definition: sound.cpp:1113
Definition: pstypes.h:88
int snd_get_pitch(int snd_handle)
Definition: sound.cpp:992
int sound_env_set(sound_env *se)
Definition: sound.cpp:1357
void snd_unload_all()
Definition: sound.cpp:447
GLclampf f
Definition: Glext.h:7097
float aav_voice_volume
Definition: sound.cpp:78
#define Assertion(expr, msg,...)
Definition: clang.h:41
int Sound_enabled
Definition: sound.cpp:51
hull_check orient
Definition: lua.cpp:5049
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
int flags
Definition: sound.h:84
GLsizeiptr size
Definition: Glext.h:5496
int sound_env_supported()
Definition: sound.cpp:1402
float volume
Definition: sound.h:93
int sound_env_get(sound_env *se, int preset=-1)
Definition: sound.cpp:1372
void snd_set_pitch(int snd_handle, int pitch)
Definition: sound.cpp:1021
int snd_play(game_snd *gs, float pan=0.0f, float vol_scale=1.0f, int priority=SND_PRIORITY_SINGLE_INSTANCE, bool voice_message=false)
Definition: sound.cpp:517
GLenum type
Definition: Gl.h:1492
char filename[MAX_FILENAME_LEN]
Definition: sound.h:76
float aav_music_volume
Definition: sound.cpp:79
void snd_chg_loop_status(int snd_handle, int loop)
float Master_voice_volume
Definition: sound.cpp:54
void snd_set_volume(int snd_handle, float volume)
Definition: sound.cpp:920
unsigned int uint
Definition: pstypes.h:64
int snd_unload(int sndnum)
Definition: sound.cpp:415
int snd_init()
Definition: sound.cpp:131
float aav_effect_volume
Definition: sound.cpp:80
char * filename
int id
index into Sounds[], where sound data is stored
Definition: sound.h:82
EnhancedSoundPriority
Definition: sound.h:32
void snd_set_pos(int snd_handle, game_snd *sg, float val, int as_pct)
Definition: sound.cpp:1180
void snd_stop(int snd_handle)
Definition: sound.cpp:875
int snd_play_raw(int soundnum, float pan, float vol_scale=1.0f, int priority=SND_PRIORITY_MUST_PLAY)
Definition: sound.cpp:477
EnhancedSoundData enhanced_sound_data
Definition: sound.h:85
void snd_close()
Definition: sound.cpp:459
int snd_load(game_snd *gs, int allow_hardware_load=0)
Definition: sound.cpp:281
unsigned int limit
Definition: sound.h:46
int snd_is_inited()
Definition: sound.cpp:1073
void snd_stop_all()
Definition: sound.cpp:905
#define SND_PRIORITY_MUST_PLAY
Definition: sound.h:26
int max
distance at which sound is inaudible
Definition: sound.h:80
EnhancedSoundData()
Definition: sound.h:48
SCP_string name
The name of the sound.
Definition: sound.h:75
float decay
Definition: sound.h:95
GLuint GLfloat * val
Definition: Glext.h:6741
void snd_set_pan(int snd_handle, float pan)
Definition: sound.cpp:964
int snd_get_samples_per_measure(char *filename, float num_measures)
Definition: sound.cpp:1428
void snd_aav_init()
Definition: sound.cpp:1535
void snd_get_format(int handle, int *bits_per_sample, int *frequency)
Definition: sound.cpp:1256
EnhancedSoundData(const int new_priority, const unsigned int new_limit)
Definition: sound.h:53
void snd_rewind(int snd_handle, game_snd *sg, float seconds)
Definition: sound.cpp:1120
int snd_play_3d(game_snd *gs, vec3d *source_pos, vec3d *listen_pos, float radius=0.0f, vec3d *vel=NULL, int looping=0, float vol_scale=1.0f, int priority=SND_PRIORITY_SINGLE_INSTANCE, vec3d *sound_fvec=NULL, float range_factor=1.0f, int force=0, bool is_ambient=false)
Definition: sound.cpp:594
float damping
Definition: sound.h:94
GLenum GLsizei GLenum GLenum const GLvoid * data
Definition: Gl.h:1509
void snd_adjust_audio_volume(int type, float percent, int time)
Definition: sound.cpp:1465
game_snd()
Definition: sound.cpp:1551
#define SND_PRIORITY_SINGLE_INSTANCE
Definition: sound.h:27
int snd_get_data(int handle, char *data)
Definition: sound.cpp:1232
hull_check pos
Definition: lua.cpp:5050
int snd_get_3d_vol_and_pan(game_snd *gs, vec3d *pos, float *vol, float *pan, float radius=0.0f, float range_factor=1.0f)
Definition: sound.cpp:759
const char * snd_get_filename(int snd_id)
Definition: sound.cpp:1101
float Master_sound_volume
Definition: sound.cpp:53
void snd_update_3d_pos(int soudnnum, game_snd *gs, vec3d *new_pos, float radius=0.0f, float range_factor=1.0f)
Definition: sound.cpp:693
unsigned int SND_ENV_DEFAULT
Definition: sound.cpp:56
int snd_num_playing()
Definition: sound.cpp:1207
int id_sig
signature of Sounds[] element
Definition: sound.h:83
float default_volume
range: 0.0 -> 1.0
Definition: sound.h:78
void snd_use_lib(int lib_id)
int snd_size(int handle, int *size)
Definition: sound.cpp:1244
int snd_get_duration(int snd_id)
Definition: sound.cpp:1082
int id
Definition: sound.h:92
int snd_time_remaining(int handle)
Definition: sound.cpp:1291
int sound_env_disable()
Definition: sound.cpp:1389
void snd_ffwd(int snd_handle, game_snd *sg, float seconds)
Definition: sound.cpp:1150
int snd_is_playing(int snd_handle)
Definition: sound.cpp:1047
Definition: sound.h:72
int Snd_hram