icculus quake2 doxygen  1.0 dev
snd_mem.c File Reference
#include "client.h"
#include "snd_loc.h"

Go to the source code of this file.

Functions

byteS_Alloc (int size)
 
void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data)
 
sfxcache_tS_LoadSound (sfx_t *s)
 
short GetLittleShort (void)
 
int GetLittleLong (void)
 
void FindNextChunk (char *name)
 
void FindChunk (char *name)
 
void DumpChunks (void)
 
wavinfo_t GetWavinfo (char *name, byte *wav, int wavlength)
 

Variables

int cache_full_cycle
 
bytedata_p
 
byteiff_end
 
bytelast_chunk
 
byteiff_data
 
int iff_chunk_len
 

Function Documentation

◆ DumpChunks()

void DumpChunks ( void  )

Definition at line 245 of file snd_mem.c.

246 {
247  char str[5];
248 
249  str[4] = 0;
251  do
252  {
253  memcpy (str, data_p, 4);
254  data_p += 4;
256  Com_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
257  data_p += (iff_chunk_len + 1) & ~1;
258  } while (data_p < iff_end);
259 }

◆ FindChunk()

void FindChunk ( char *  name)

Definition at line 238 of file snd_mem.c.

239 {
242 }

Referenced by GetWavinfo().

◆ FindNextChunk()

void FindNextChunk ( char *  name)

Definition at line 210 of file snd_mem.c.

211 {
212  while (1)
213  {
215 
216  if (data_p >= iff_end)
217  { // didn't find the chunk
218  data_p = NULL;
219  return;
220  }
221 
222  data_p += 4;
224  if (iff_chunk_len < 0)
225  {
226  data_p = NULL;
227  return;
228  }
229 // if (iff_chunk_len > 1024*1024)
230 // Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
231  data_p -= 8;
232  last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
233  if (!strncmp((char *)data_p, name, 4))
234  return;
235  }
236 }

Referenced by FindChunk(), and GetWavinfo().

◆ GetLittleLong()

int GetLittleLong ( void  )

Definition at line 199 of file snd_mem.c.

200 {
201  int val = 0;
202  val = *data_p;
203  val = val + (*(data_p+1)<<8);
204  val = val + (*(data_p+2)<<16);
205  val = val + (*(data_p+3)<<24);
206  data_p += 4;
207  return val;
208 }

Referenced by DumpChunks(), FindNextChunk(), and GetWavinfo().

◆ GetLittleShort()

short GetLittleShort ( void  )

Definition at line 190 of file snd_mem.c.

191 {
192  short val = 0;
193  val = *data_p;
194  val = val + (*(data_p+1)<<8);
195  data_p += 2;
196  return val;
197 }

Referenced by GetWavinfo().

◆ GetWavinfo()

wavinfo_t GetWavinfo ( char *  name,
byte wav,
int  wavlength 
)

Definition at line 266 of file snd_mem.c.

267 {
268  wavinfo_t info;
269  int i;
270  int format;
271  int samples;
272 
273  memset (&info, 0, sizeof(info));
274 
275  if (!wav)
276  return info;
277 
278  iff_data = wav;
279  iff_end = wav + wavlength;
280 
281 // find "RIFF" chunk
282  FindChunk("RIFF");
283  if (!(data_p && !strncmp((char *)data_p+8, "WAVE", 4)))
284  {
285  Com_Printf("Missing RIFF/WAVE chunks\n");
286  return info;
287  }
288 
289 // get "fmt " chunk
290  iff_data = data_p + 12;
291 // DumpChunks ();
292 
293  FindChunk("fmt ");
294  if (!data_p)
295  {
296  Com_Printf("Missing fmt chunk\n");
297  return info;
298  }
299  data_p += 8;
301  if (format != 1)
302  {
303  Com_Printf("Microsoft PCM format only\n");
304  return info;
305  }
306 
307  info.channels = GetLittleShort();
308  info.rate = GetLittleLong();
309  data_p += 4+2;
310  info.width = GetLittleShort() / 8;
311 
312 // get cue chunk
313  FindChunk("cue ");
314  if (data_p)
315  {
316  data_p += 32;
317  info.loopstart = GetLittleLong();
318 // Com_Printf("loopstart=%d\n", sfx->loopstart);
319 
320  // if the next chunk is a LIST chunk, look for a cue length marker
321  FindNextChunk ("LIST");
322  if (data_p)
323  {
324  if (!strncmp ((char *)data_p + 28, "mark", 4))
325  { // this is not a proper parse, but it works with cooledit...
326  data_p += 24;
327  i = GetLittleLong (); // samples in loop
328  info.samples = info.loopstart + i;
329 // Com_Printf("looped length: %i\n", i);
330  }
331  }
332  }
333  else
334  info.loopstart = -1;
335 
336 // find data chunk
337  FindChunk("data");
338  if (!data_p)
339  {
340  Com_Printf("Missing data chunk\n");
341  return info;
342  }
343 
344  data_p += 4;
345  samples = GetLittleLong () / info.width;
346 
347  if (info.samples)
348  {
349  if (samples < info.samples)
350  Com_Error (ERR_DROP, "Sound %s has a bad loop length", name);
351  }
352  else
353  info.samples = samples;
354 
355  info.dataofs = data_p - wav;
356 
357  return info;
358 }

Referenced by S_LoadSound().

◆ ResampleSfx()

void ResampleSfx ( sfx_t sfx,
int  inrate,
int  inwidth,
byte data 
)

Definition at line 34 of file snd_mem.c.

35 {
36  int outcount;
37  int srcsample;
38  float stepscale;
39  int i;
40  int sample, samplefrac, fracstep;
41  sfxcache_t *sc;
42 
43  sc = sfx->cache;
44  if (!sc)
45  return;
46 
47  stepscale = (float)inrate / dma.speed; // this is usually 0.5, 1, or 2
48 
49  outcount = sc->length / stepscale;
50  sc->length = outcount;
51  if (sc->loopstart != -1)
52  sc->loopstart = sc->loopstart / stepscale;
53 
54  sc->speed = dma.speed;
55  if (s_loadas8bit->value)
56  sc->width = 1;
57  else
58  sc->width = inwidth;
59  sc->stereo = 0;
60 
61 // resample / decimate to the current source rate
62 
63  if (stepscale == 1 && inwidth == 1 && sc->width == 1)
64  {
65 // fast special case
66  for (i=0 ; i<outcount ; i++)
67  ((signed char *)sc->data)[i]
68  = (int)( (unsigned char)(data[i]) - 128);
69  }
70  else
71  {
72 // general case
73  samplefrac = 0;
74  fracstep = stepscale*256;
75  for (i=0 ; i<outcount ; i++)
76  {
77  srcsample = samplefrac >> 8;
78  samplefrac += fracstep;
79  if (inwidth == 2)
80  sample = LittleShort ( ((short *)data)[srcsample] );
81  else
82  sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8;
83  if (sc->width == 2)
84  ((short *)sc->data)[i] = sample;
85  else
86  ((signed char *)sc->data)[i] = sample >> 8;
87  }
88  }
89 }

Referenced by S_LoadSound().

◆ S_Alloc()

byte* S_Alloc ( int  size)

◆ S_LoadSound()

sfxcache_t* S_LoadSound ( sfx_t s)

Definition at line 98 of file snd_mem.c.

99 {
100  char namebuffer[MAX_QPATH];
101  byte *data;
102  wavinfo_t info;
103  int len;
104  float stepscale;
105  sfxcache_t *sc;
106  int size;
107  char *name;
108 
109  if (s->name[0] == '*')
110  return NULL;
111 
112 // see if still in memory
113  sc = s->cache;
114  if (sc)
115  return sc;
116 
117 //Com_Printf ("S_LoadSound: %x\n", (int)stackbuf);
118 // load it in
119  if (s->truename)
120  name = s->truename;
121  else
122  name = s->name;
123 
124  if (name[0] == '#')
125  strcpy(namebuffer, &name[1]);
126  else
127  Com_sprintf (namebuffer, sizeof(namebuffer), "sound/%s", name);
128 
129 // Com_Printf ("loading %s\n",namebuffer);
130 
131  size = FS_LoadFile (namebuffer, (void **)&data);
132 
133  if (!data)
134  {
135  Com_DPrintf ("Couldn't load %s\n", namebuffer);
136  return NULL;
137  }
138 
139  info = GetWavinfo (s->name, data, size);
140  if (info.channels != 1)
141  {
142  Com_Printf ("%s is a stereo sample\n",s->name);
143  FS_FreeFile (data);
144  return NULL;
145  }
146 
147  stepscale = (float)info.rate / dma.speed;
148  len = info.samples / stepscale;
149 
150  len = len * info.width * info.channels;
151 
152  sc = s->cache = Z_Malloc (len + sizeof(sfxcache_t));
153  if (!sc)
154  {
155  FS_FreeFile (data);
156  return NULL;
157  }
158 
159  sc->length = info.samples;
160  sc->loopstart = info.loopstart;
161  sc->speed = info.rate;
162  sc->width = info.width;
163  sc->stereo = info.channels;
164 
165  ResampleSfx (s, sc->speed, sc->width, data + info.dataofs);
166 
167  FS_FreeFile (data);
168 
169  return sc;
170 }

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

Variable Documentation

◆ cache_full_cycle

int cache_full_cycle

Definition at line 25 of file snd_mem.c.

◆ data_p

byte* data_p

◆ iff_chunk_len

int iff_chunk_len

Definition at line 187 of file snd_mem.c.

Referenced by DumpChunks(), and FindNextChunk().

◆ iff_data

byte* iff_data

Definition at line 186 of file snd_mem.c.

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

◆ iff_end

byte* iff_end

Definition at line 184 of file snd_mem.c.

Referenced by DumpChunks(), FindNextChunk(), and GetWavinfo().

◆ last_chunk

byte* last_chunk

Definition at line 185 of file snd_mem.c.

Referenced by FindChunk(), and FindNextChunk().

MAX_QPATH
#define MAX_QPATH
Definition: q_shared.h:73
LittleShort
short LittleShort(short l)
Definition: q_shared.c:946
int
CONST PIXELFORMATDESCRIPTOR int
Definition: qgl_win.c:35
wavinfo_t::width
int width
Definition: snd_loc.h:95
sfxcache_t::length
int length
Definition: snd_loc.h:31
FindChunk
void FindChunk(char *name)
Definition: snd_mem.c:238
sfx_s::truename
char * truename
Definition: snd_loc.h:44
wavinfo_t::channels
int channels
Definition: snd_loc.h:96
data_p
byte * data_p
Definition: snd_mem.c:183
wavinfo_t::samples
int samples
Definition: snd_loc.h:98
i
int i
Definition: q_shared.c:305
sfxcache_t::stereo
int stereo
Definition: snd_loc.h:35
FindNextChunk
void FindNextChunk(char *name)
Definition: snd_mem.c:210
GetWavinfo
wavinfo_t GetWavinfo(char *name, byte *wav, int wavlength)
Definition: snd_mem.c:266
sfxcache_t::speed
int speed
Definition: snd_loc.h:33
GetLittleShort
short GetLittleShort(void)
Definition: snd_mem.c:190
sfx_s::cache
sfxcache_t * cache
Definition: snd_loc.h:43
ResampleSfx
void ResampleSfx(sfx_t *sfx, int inrate, int inwidth, byte *data)
Definition: snd_mem.c:34
FS_LoadFile
int FS_LoadFile(char *path, void **buffer)
Definition: files.c:394
cvar_s::value
float value
Definition: q_shared.h:324
dma_t::speed
int speed
Definition: snd_loc.h:70
NULL
#define NULL
Definition: q_shared.h:60
dma
dma_t dma
Definition: snd_dma.c:47
Com_Error
void Com_Error(int code, char *fmt,...)
Definition: common.c:203
wavinfo_t::rate
int rate
Definition: snd_loc.h:94
Z_Malloc
void * Z_Malloc(int size)
Definition: common.c:1223
ERR_DROP
#define ERR_DROP
Definition: qcommon.h:736
name
cvar_t * name
Definition: cl_main.c:94
iff_data
byte * iff_data
Definition: snd_mem.c:186
iff_end
byte * iff_end
Definition: snd_mem.c:184
s_loadas8bit
cvar_t * s_loadas8bit
Definition: snd_dma.c:76
wavinfo_t::loopstart
int loopstart
Definition: snd_loc.h:97
format
GLsizei GLenum format
Definition: qgl_win.c:131
Com_DPrintf
void Com_DPrintf(char *fmt,...)
Definition: common.c:155
sfxcache_t
Definition: snd_loc.h:29
iff_chunk_len
int iff_chunk_len
Definition: snd_mem.c:187
FS_FreeFile
void FS_FreeFile(void *buffer)
Definition: files.c:433
sfxcache_t::data
byte data[1]
Definition: snd_loc.h:36
sfxcache_t::width
int width
Definition: snd_loc.h:34
GetLittleLong
int GetLittleLong(void)
Definition: snd_mem.c:199
Com_Printf
void Com_Printf(char *fmt,...)
Definition: common.c:102
last_chunk
byte * last_chunk
Definition: snd_mem.c:185
wavinfo_t::dataofs
int dataofs
Definition: snd_loc.h:99
Com_sprintf
void Com_sprintf(char *dest, int size, char *fmt,...)
Definition: q_shared.c:1236
sfxcache_t::loopstart
int loopstart
Definition: snd_loc.h:32
wavinfo_t
Definition: snd_loc.h:92
sfx_s::name
char name[MAX_QPATH]
Definition: snd_loc.h:41