1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 
5 This file is part of Quake III Arena source code.
6 
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11 
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22 // snd_local.h -- private sound definations
23 
24 
25 #include "../qcommon/q_shared.h"
26 #include "../qcommon/qcommon.h"
27 #include "snd_public.h"
28 
29 #define	PAINTBUFFER_SIZE		4096					// this is in samples
30 
31 #define SND_CHUNK_SIZE			1024					// samples
32 #define SND_CHUNK_SIZE_FLOAT	(SND_CHUNK_SIZE/2)		// floats
33 #define SND_CHUNK_SIZE_BYTE		(SND_CHUNK_SIZE*2)		// floats
34 
35 typedef struct {
36 	int			left;	// the final values will be clamped to +/- 0x00ffff00 and shifted down
37 	int			right;
38 } portable_samplepair_t;
39 
40 typedef struct adpcm_state {
41     short	sample;		/* Previous output value */
42     char	index;		/* Index into stepsize table */
43 } adpcm_state_t;
44 
45 typedef	struct sndBuffer_s {
46 	short					sndChunk[SND_CHUNK_SIZE];
47 	struct sndBuffer_s		*next;
48     int						size;
49 	adpcm_state_t			adpcm;
50 } sndBuffer;
51 
52 typedef struct sfx_s {
53 	sndBuffer		*soundData;
54 	qboolean		defaultSound;			// couldn't be loaded, so use buzz
55 	qboolean		inMemory;				// not in Memory
56 	qboolean		soundCompressed;		// not in Memory
57 	int				soundCompressionMethod;
58 	int 			soundLength;
59 	char 			soundName[MAX_QPATH];
60 	int				lastTimeUsed;
61 	struct sfx_s	*next;
62 } sfx_t;
63 
64 typedef struct {
65 	int			channels;
66 	int			samples;				// mono samples in buffer
67 	int			submission_chunk;		// don't mix less than this #
68 	int			samplebits;
69 	int			speed;
70 	byte		*buffer;
71 } dma_t;
72 
73 #define START_SAMPLE_IMMEDIATE	0x7fffffff
74 
75 #define MAX_DOPPLER_SCALE 50.0f //arbitrary
76 
77 typedef struct loopSound_s {
78 	vec3_t		origin;
79 	vec3_t		velocity;
80 	sfx_t		*sfx;
81 	int			mergeFrame;
82 	qboolean	active;
83 	qboolean	kill;
84 	qboolean	doppler;
85 	float		dopplerScale;
86 	float		oldDopplerScale;
87 	int			framenum;
88 } loopSound_t;
89 
90 typedef struct
91 {
92 	int			allocTime;
93 	int			startSample;	// START_SAMPLE_IMMEDIATE = set immediately on next mix
94 	int			entnum;			// to allow overriding a specific sound
95 	int			entchannel;		// to allow overriding a specific sound
96 	int			leftvol;		// 0-255 volume after spatialization
97 	int			rightvol;		// 0-255 volume after spatialization
98 	int			master_vol;		// 0-255 volume before spatialization
99 	float		dopplerScale;
100 	float		oldDopplerScale;
101 	vec3_t		origin;			// only use if fixed_origin is set
102 	qboolean	fixed_origin;	// use origin instead of fetching entnum's origin
103 	sfx_t		*thesfx;		// sfx structure
104 	qboolean	doppler;
105 } channel_t;
106 
107 
108 #define	WAV_FORMAT_PCM		1
109 
110 
111 typedef struct {
112 	int			format;
113 	int			rate;
114 	int			width;
115 	int			channels;
116 	int			samples;
117 	int			dataofs;		// chunk starts this many bytes from file start
118 } wavinfo_t;
119 
120 // Interface between Q3 sound "api" and the sound backend
121 typedef struct
122 {
123 	void (*Shutdown)(void);
124 	void (*StartSound)( vec3_t origin, int entnum, int entchannel, sfxHandle_t sfx );
125 	void (*StartLocalSound)( sfxHandle_t sfx, int channelNum );
126 	void (*StartBackgroundTrack)( const char *intro, const char *loop );
127 	void (*StopBackgroundTrack)( void );
128 	void (*RawSamples)(int stream, int samples, int rate, int width, int channels, const byte *data, float volume);
129 	void (*StopAllSounds)( void );
130 	void (*ClearLoopingSounds)( qboolean killall );
131 	void (*AddLoopingSound)( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx );
132 	void (*AddRealLoopingSound)( int entityNum, const vec3_t origin, const vec3_t velocity, sfxHandle_t sfx );
133 	void (*StopLoopingSound)(int entityNum );
134 	void (*Respatialize)( int entityNum, const vec3_t origin, vec3_t axis[3], int inwater );
135 	void (*UpdateEntityPosition)( int entityNum, const vec3_t origin );
136 	void (*Update)( void );
137 	void (*DisableSounds)( void );
138 	void (*BeginRegistration)( void );
139 	sfxHandle_t (*RegisterSound)( const char *sample, qboolean compressed );
140 	void (*ClearSoundBuffer)( void );
141 	void (*SoundInfo)( void );
142 	void (*SoundList)( void );
143 #if USE_VOIP
144 	void (*StartCapture)( void );
145 	int (*AvailableCaptureSamples)( void );
146 	void (*Capture)( int samples, byte *data );
147 	void (*StopCapture)( void );
148 	void (*MasterGain)( float gain );
149 #endif
150 } soundInterface_t;
151 
152 
153 /*
154 ====================================================================
155 
156   SYSTEM SPECIFIC FUNCTIONS
157 
158 ====================================================================
159 */
160 
161 // initializes cycling through a DMA buffer and returns information on it
162 qboolean SNDDMA_Init(void);
163 
164 // gets the current DMA position
165 int		SNDDMA_GetDMAPos(void);
166 
167 // shutdown the DMA xfer.
168 void	SNDDMA_Shutdown(void);
169 
170 void	SNDDMA_BeginPainting (void);
171 
172 void	SNDDMA_Submit(void);
173 
174 //====================================================================
175 
176 #define	MAX_CHANNELS			96
177 
178 extern	channel_t   s_channels[MAX_CHANNELS];
179 extern	channel_t   loop_channels[MAX_CHANNELS];
180 extern	int		numLoopChannels;
181 
182 extern	int		s_paintedtime;
183 extern	vec3_t	listener_forward;
184 extern	vec3_t	listener_right;
185 extern	vec3_t	listener_up;
186 extern	dma_t	dma;
187 
188 #define	MAX_RAW_SAMPLES	16384
189 #define MAX_RAW_STREAMS 128
190 extern	portable_samplepair_t s_rawsamples[MAX_RAW_STREAMS][MAX_RAW_SAMPLES];
191 extern	int		s_rawend[MAX_RAW_STREAMS];
192 
193 extern cvar_t *s_volume;
194 extern cvar_t *s_musicVolume;
195 extern cvar_t *s_doppler;
196 
197 extern cvar_t *s_testsound;
198 
199 qboolean S_LoadSound( sfx_t *sfx );
200 
201 void		SND_free(sndBuffer *v);
202 sndBuffer*	SND_malloc( void );
203 void		SND_setup( void );
204 
205 void S_PaintChannels(int endtime);
206 
207 void S_memoryLoad(sfx_t *sfx);
208 
209 // spatializes a channel
210 void S_Spatialize(channel_t *ch);
211 
212 // adpcm functions
213 int  S_AdpcmMemoryNeeded( const wavinfo_t *info );
214 void S_AdpcmEncodeSound( sfx_t *sfx, short *samples );
215 void S_AdpcmGetSamples(sndBuffer *chunk, short *to);
216 
217 // wavelet function
218 
219 #define SENTINEL_MULAW_ZERO_RUN 127
220 #define SENTINEL_MULAW_FOUR_BIT_RUN 126
221 
222 void S_FreeOldestSound( void );
223 
224 #define	NXStream byte
225 
226 void encodeWavelet(sfx_t *sfx, short *packets);
227 void decodeWavelet( sndBuffer *stream, short *packets);
228 
229 void encodeMuLaw( sfx_t *sfx, short *packets);
230 extern short mulawToShort[256];
231 
232 extern short *sfxScratchBuffer;
233 extern sfx_t *sfxScratchPointer;
234 extern int	   sfxScratchIndex;
235 
236 qboolean S_Base_Init( soundInterface_t *si );
237 
238 // OpenAL stuff
239 typedef enum
240 {
241 	SRCPRI_AMBIENT = 0,	// Ambient sound effects
242 	SRCPRI_ENTITY,			// Entity sound effects
243 	SRCPRI_ONESHOT,			// One-shot sounds
244 	SRCPRI_LOCAL,				// Local sounds
245 	SRCPRI_STREAM				// Streams (music, cutscenes)
246 } alSrcPriority_t;
247 
248 typedef int srcHandle_t;
249 
250 qboolean S_AL_Init( soundInterface_t *si );
251