Quake II RTX doxygen  1.0 dev
mem.c File Reference
#include "sound.h"

Go to the source code of this file.

Macros

#define TAG_RIFF   MakeRawLong('R', 'I', 'F', 'F')
 
#define TAG_WAVE   MakeRawLong('W', 'A', 'V', 'E')
 
#define TAG_fmt   MakeRawLong('f', 'm', 't', ' ')
 
#define TAG_cue   MakeRawLong('c', 'u', 'e', ' ')
 
#define TAG_LIST   MakeRawLong('L', 'I', 'S', 'T')
 
#define TAG_MARK   MakeRawLong('M', 'A', 'R', 'K')
 
#define TAG_data   MakeRawLong('d', 'a', 't', 'a')
 

Functions

static int GetLittleShort (void)
 
static int GetLittleLong (void)
 
static void FindNextChunk (uint32_t search)
 
static void FindChunk (uint32_t search)
 
static qboolean GetWavinfo (void)
 
sfxcache_tS_LoadSound (sfx_t *s)
 

Variables

wavinfo_t s_info
 
static byte * data_p
 
static byte * iff_end
 
static byte * iff_data
 
static uint32_t iff_chunk_len
 

Macro Definition Documentation

◆ TAG_cue

#define TAG_cue   MakeRawLong('c', 'u', 'e', ' ')

Definition at line 163 of file mem.c.

◆ TAG_data

#define TAG_data   MakeRawLong('d', 'a', 't', 'a')

Definition at line 166 of file mem.c.

◆ TAG_fmt

#define TAG_fmt   MakeRawLong('f', 'm', 't', ' ')

Definition at line 162 of file mem.c.

◆ TAG_LIST

#define TAG_LIST   MakeRawLong('L', 'I', 'S', 'T')

Definition at line 164 of file mem.c.

◆ TAG_MARK

#define TAG_MARK   MakeRawLong('M', 'A', 'R', 'K')

Definition at line 165 of file mem.c.

◆ TAG_RIFF

#define TAG_RIFF   MakeRawLong('R', 'I', 'F', 'F')

Definition at line 160 of file mem.c.

◆ TAG_WAVE

#define TAG_WAVE   MakeRawLong('W', 'A', 'V', 'E')

Definition at line 161 of file mem.c.

Function Documentation

◆ FindChunk()

static void FindChunk ( uint32_t  search)
static

Definition at line 154 of file mem.c.

155 {
156  data_p = iff_data;
157  FindNextChunk(search);
158 }

Referenced by GetWavinfo().

◆ FindNextChunk()

static void FindNextChunk ( uint32_t  search)
static

Definition at line 131 of file mem.c.

132 {
133  uint32_t chunk, len;
134  size_t remaining;
135 
136  while (data_p + 8 < iff_end) {
137  chunk = RawLongMem(data_p); data_p += 4;
138  len = LittleLongMem(data_p); data_p += 4;
139  remaining = (size_t)(iff_end - data_p);
140  if (len > remaining) {
141  len = remaining;
142  }
143  if (chunk == search) {
144  iff_chunk_len = len;
145  return;
146  }
147  data_p += (len + 1) & ~1;
148  }
149 
150  // didn't find the chunk
151  data_p = NULL;
152 }

Referenced by FindChunk(), and GetWavinfo().

◆ GetLittleLong()

static int GetLittleLong ( void  )
static

Definition at line 118 of file mem.c.

119 {
120  int val;
121 
122  if (data_p + 4 > iff_end) {
123  return -1;
124  }
125 
126  val = LittleLongMem(data_p);
127  data_p += 4;
128  return val;
129 }

Referenced by GetWavinfo().

◆ GetLittleShort()

static int GetLittleShort ( void  )
static

Definition at line 105 of file mem.c.

106 {
107  int val;
108 
109  if (data_p + 2 > iff_end) {
110  return -1;
111  }
112 
113  val = LittleShortMem(data_p);
114  data_p += 2;
115  return val;
116 }

Referenced by GetWavinfo().

◆ GetWavinfo()

static qboolean GetWavinfo ( void  )
static

Definition at line 168 of file mem.c.

169 {
170  int format;
171  int samples, width;
172  uint32_t chunk;
173 
174 // find "RIFF" chunk
176  if (!data_p) {
177  Com_DPrintf("%s has missing/invalid RIFF chunk\n", s_info.name);
178  return qfalse;
179  }
180  chunk = GetLittleLong();
181  if (chunk != TAG_WAVE) {
182  Com_DPrintf("%s has missing/invalid WAVE chunk\n", s_info.name);
183  return qfalse;
184  }
185 
186  iff_data = data_p;
187 
188 // get "fmt " chunk
190  if (!data_p) {
191  Com_DPrintf("%s has missing/invalid fmt chunk\n", s_info.name);
192  return qfalse;
193  }
194  format = GetLittleShort();
195  if (format != 1) {
196  Com_DPrintf("%s has non-Microsoft PCM format\n", s_info.name);
197  return qfalse;
198  }
199 
200  format = GetLittleShort();
201  if (format != 1) {
202  Com_DPrintf("%s has bad number of channels\n", s_info.name);
203  return qfalse;
204  }
205 
207  if (s_info.rate < 8000 || s_info.rate > 48000) {
208  Com_DPrintf("%s has bad rate\n", s_info.name);
209  return qfalse;
210  }
211 
212  data_p += 4 + 2;
213 
214  width = GetLittleShort();
215  switch (width) {
216  case 8:
217  s_info.width = 1;
218  break;
219  case 16:
220  s_info.width = 2;
221  break;
222  default:
223  Com_DPrintf("%s has bad width\n", s_info.name);
224  return qfalse;
225  }
226 
227 // get cue chunk
229  if (data_p) {
230  data_p += 24;
232  if (s_info.loopstart < 0 || s_info.loopstart > INT_MAX) {
233  Com_DPrintf("%s has bad loop start\n", s_info.name);
234  return qfalse;
235  }
236 
238  if (data_p) {
239  data_p += 20;
240  chunk = GetLittleLong();
241  if (chunk == TAG_MARK) {
242  // this is not a proper parse, but it works with cooledit...
243  data_p += 16;
244  samples = GetLittleLong(); // samples in loop
245  if (samples < 0 || samples > INT_MAX - s_info.loopstart) {
246  Com_DPrintf("%s has bad loop length\n", s_info.name);
247  return qfalse;
248  }
250  }
251  }
252  } else {
253  s_info.loopstart = -1;
254  }
255 
256 // find data chunk
258  if (!data_p) {
259  Com_DPrintf("%s has missing/invalid data chunk\n", s_info.name);
260  return qfalse;
261  }
262 
264  if (!samples) {
265  Com_DPrintf("%s has zero length\n", s_info.name);
266  return qfalse;
267  }
268 
269  if (s_info.samples) {
270  if (samples < s_info.samples) {
271  Com_DPrintf("%s has bad loop length\n", s_info.name);
272  return qfalse;
273  }
274  } else {
276  }
277 
278  s_info.data = data_p;
279 
280  return qtrue;
281 }

Referenced by S_LoadSound().

◆ S_LoadSound()

sfxcache_t* S_LoadSound ( sfx_t s)

Definition at line 288 of file mem.c.

289 {
290  byte *data;
291  sfxcache_t *sc;
292  ssize_t len;
293  char *name;
294 
295  if (s->name[0] == '*')
296  return NULL;
297 
298 // see if still in memory
299  sc = s->cache;
300  if (sc)
301  return sc;
302 
303 // don't retry after error
304  if (s->error)
305  return NULL;
306 
307 // load it in
308  if (s->truename)
309  name = s->truename;
310  else
311  name = s->name;
312 
313  len = FS_LoadFile(name, (void **)&data);
314  if (!data) {
315  s->error = len;
316  return NULL;
317  }
318 
319  memset(&s_info, 0, sizeof(s_info));
320  s_info.name = name;
321 
322  iff_data = data;
323  iff_end = data + len;
324  if (!GetWavinfo()) {
325  s->error = Q_ERR_INVALID_FORMAT;
326  goto fail;
327  }
328 
329 #if USE_OPENAL
330  if (s_started == SS_OAL)
331  sc = AL_UploadSfx(s);
332 #endif
333 
334 #if USE_SNDDMA
335  if (s_started == SS_DMA)
336  sc = ResampleSfx(s);
337 #endif
338 
339 fail:
340  FS_FreeFile(data);
341  return sc;
342 }

Referenced by S_EndRegistration(), S_IssuePlaysound(), S_PaintChannels(), S_RegisterSexedSound(), S_RegisterSound(), and S_StartSound().

Variable Documentation

◆ data_p

byte* data_p
static

◆ iff_chunk_len

uint32_t iff_chunk_len
static

Definition at line 103 of file mem.c.

Referenced by FindNextChunk(), and GetWavinfo().

◆ iff_data

byte* iff_data
static

Definition at line 102 of file mem.c.

Referenced by FindChunk(), GetWavinfo(), and S_LoadSound().

◆ iff_end

byte* iff_end
static

Definition at line 101 of file mem.c.

Referenced by FindNextChunk(), GetLittleLong(), GetLittleShort(), and S_LoadSound().

◆ s_info

wavinfo_t s_info

Definition at line 22 of file mem.c.

Referenced by AL_UploadSfx(), GetWavinfo(), and S_LoadSound().

edict_s::s
entity_state_t s
Definition: g_local.h:954
s_info
wavinfo_t s_info
Definition: mem.c:22
wavinfo_t::width
int width
Definition: sound.h:91
AL_UploadSfx
sfxcache_t * AL_UploadSfx(sfx_t *s)
Definition: al.c:201
FindChunk
static void FindChunk(uint32_t search)
Definition: mem.c:154
TAG_MARK
#define TAG_MARK
Definition: mem.c:165
data_p
static byte * data_p
Definition: mem.c:100
GetLittleLong
static int GetLittleLong(void)
Definition: mem.c:118
iff_end
static byte * iff_end
Definition: mem.c:101
wavinfo_t::samples
int samples
Definition: sound.h:93
iff_data
static byte * iff_data
Definition: mem.c:102
TAG_WAVE
#define TAG_WAVE
Definition: mem.c:161
TAG_fmt
#define TAG_fmt
Definition: mem.c:162
width
static int width
Definition: physical_sky.c:38
wavinfo_t::data
byte * data
Definition: sound.h:94
GetWavinfo
static qboolean GetWavinfo(void)
Definition: mem.c:168
TAG_cue
#define TAG_cue
Definition: mem.c:163
wavinfo_t::rate
int rate
Definition: sound.h:90
sfxcache_s
Definition: sound.h:33
FindNextChunk
static void FindNextChunk(uint32_t search)
Definition: mem.c:131
TAG_LIST
#define TAG_LIST
Definition: mem.c:164
wavinfo_t::name
char * name
Definition: sound.h:89
TAG_data
#define TAG_data
Definition: mem.c:166
iff_chunk_len
static uint32_t iff_chunk_len
Definition: mem.c:103
TAG_RIFF
#define TAG_RIFF
Definition: mem.c:160
wavinfo_t::loopstart
int loopstart
Definition: sound.h:92
samples
unsigned samples[LAG_WIDTH]
Definition: screen.c:528
s_started
sndstarted_t s_started
Definition: main.c:32
GetLittleShort
static int GetLittleShort(void)
Definition: mem.c:105