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 //For the adjust-audio-volume sexp
32 #define AAV_MUSIC		0
33 #define AAV_VOICE		1
34 #define AAV_EFFECTS		2
35 
36 /**
37  * Game level sound entities
38  */
39 class game_snd
40 {
41 public:
42 	SCP_string name;				//!< The name of the sound
43 	char filename[MAX_FILENAME_LEN];
44 	uint signature;					//!< Unique signature of this sound
45 	float default_volume;			//!<range: 0.0 -> 1.0
46 	int	min;						//!<distance at which sound will stop getting louder
47 	int max;						//!<distance at which sound is inaudible
48 	bool preload;					//!< preload sound (ie read from disk before mission starts)
49 	int	id;							//!< index into Sounds[], where sound data is stored
50 	int	id_sig;						//!< signature of Sounds[] element
51 	int	flags;
52 
53 	game_snd( );
54 };
55 
56 typedef struct sound_env
57 {
58 	int id;
59 	float volume;
60 	float damping;
61 	float decay;
62 } sound_env;
63 
64 extern int		Sound_enabled;
65 extern float	Master_sound_volume;		// 0 -> 1.0
66 extern float	Master_voice_volume;		// 0 -> 1.0
67 extern int		Snd_sram;					// System memory consumed by sound data
68 extern int		Snd_hram;					// Soundcard memory consumed by sound data
69 extern float aav_voice_volume;
70 extern float aav_music_volume;
71 extern float aav_effect_volume;
72 
73 //int	snd_load( char *filename, int hardware=0, int three_d=0, int *sig=NULL );
74 int	snd_load( game_snd *gs, int allow_hardware_load = 0);
75 
76 int	snd_unload( int sndnum );
77 void	snd_unload_all();
78 
79 // Plays a sound with volume between 0 and 1.0, where 0 is the
80 // inaudible and 1.0 is the loudest sound in the game.
81 // Pan goes from -1.0 all the way left to 0.0 in center to 1.0 all the way right.
82 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 );
83 
84 // Play a sound directly from index returned from snd_load().  Bypasses
85 // the sound management process of using game_snd.
86 int snd_play_raw( int soundnum, float pan, float vol_scale=1.0f, int priority = SND_PRIORITY_MUST_PLAY );
87 
88 // Plays a sound with volume between 0 and 1.0, where 0 is the
89 // inaudible and 1.0 is the loudest sound in the game.  It scales
90 // the pan and volume relative to the current viewer's location.
91 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 );
92 
93 // update the given 3d sound with a new position
94 void snd_update_3d_pos(int soudnnum, game_snd *gs, vec3d *new_pos, float radius = 0.0f, float range_factor = 1.0f);
95 
96 // Use these for looping sounds.
97 // Returns the handle of the sound. -1 if failed.
98 // If startloop or stoploop are not -1, then then are used.
99 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);
100 
101 void	snd_stop( int snd_handle );
102 
103 // Sets the volume of a sound that is already playing.
104 // The volume is between 0 and 1.0, where 0 is the
105 // inaudible and 1.0 is the loudest sound in the game.
106 void snd_set_volume( int snd_handle, float volume );
107 
108 // Sets the panning location of a sound that is already playing.
109 // Pan goes from -1.0 all the way left to 0.0 in center to 1.0 all the way right.
110 void snd_set_pan( int snd_handle, float pan );
111 
112 // Sets the pitch (frequency) of a sound that is already playing
113 // Valid values for pitch are between 100 and 100000
114 void	snd_set_pitch( int snd_handle, int pitch );
115 int	snd_get_pitch( int snd_handle );
116 
117 // Stops all sounds from playing, even looping ones.
118 void	snd_stop_all();
119 
120 // determines if the sound handle is still palying
121 int	snd_is_playing( int snd_handle );
122 
123 // change the looping status of a sound that is playing
124 void	snd_chg_loop_status(int snd_handle, int loop);
125 
126 // return the time in ms for the duration of the sound
127 int snd_get_duration(int snd_id);
128 
129 // get a 3D vol and pan for a particular sound
130 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);
131 
132 int	snd_init();
133 void	snd_close();
134 
135 // Return 1 or 0 to show that sound system is inited ok
136 int	snd_is_inited();
137 
138 void	snd_update_listener(vec3d *pos, vec3d *vel, matrix *orient);
139 
140 void 	snd_use_lib(int lib_id);
141 
142 int snd_num_playing();
143 
144 int snd_get_data(int handle, char *data);
145 int snd_size(int handle, int *size);
146 void snd_do_frame();
147 void snd_adjust_audio_volume(int type, float percent, int time);
148 
149 // repositioning of the sound buffer pointer
150 void snd_rewind(int snd_handle, game_snd *sg, float seconds);					// rewind N seconds from the current position
151 void snd_ffwd(int snd_handle, game_snd *sg, float seconds);						// fast forward N seconds from the current position
152 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
153 
154 void snd_get_format(int handle, int *bits_per_sample, int *frequency);
155 int snd_time_remaining(int handle);
156 
157 int snd_get_samples_per_measure(char *filename, float num_measures);
158 
159 // sound environment
160 extern unsigned int SND_ENV_DEFAULT;
161 
162 int sound_env_set(sound_env *se);
163 int sound_env_get(sound_env *se, int preset = -1);
164 int sound_env_disable();
165 int sound_env_supported();
166 
167 // adjust-audio-volume
168 void snd_aav_init();
169 
170 #endif
171