git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / snd_mem.c
blob494928ae05cb58613b9a5899f18b485f7b5a5914
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // snd_mem.c: sound caching
22 #include "quakedef.h"
24 int cache_full_cycle;
26 byte *S_Alloc (int size);
29 ================
30 ResampleSfx
31 ================
33 void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data)
35 int outcount;
36 int srcsample;
37 float stepscale;
38 int i;
39 int sample, samplefrac, fracstep;
40 sfxcache_t *sc;
42 sc = Cache_Check (&sfx->cache);
43 if (!sc)
44 return;
46 stepscale = (float)inrate / shm->speed; // this is usually 0.5, 1, or 2
48 outcount = sc->length / stepscale;
49 sc->length = outcount;
50 if (sc->loopstart != -1)
51 sc->loopstart = sc->loopstart / stepscale;
53 sc->speed = shm->speed;
54 if (loadas8bit.value)
55 sc->width = 1;
56 else
57 sc->width = inwidth;
58 sc->stereo = 0;
60 // resample / decimate to the current source rate
62 if (stepscale == 1 && inwidth == 1 && sc->width == 1)
64 // fast special case
65 for (i=0 ; i<outcount ; i++)
66 ((signed char *)sc->data)[i]
67 = (int)( (unsigned char)(data[i]) - 128);
69 else
71 // general case
72 samplefrac = 0;
73 fracstep = stepscale*256;
74 for (i=0 ; i<outcount ; i++)
76 srcsample = samplefrac >> 8;
77 samplefrac += fracstep;
78 if (inwidth == 2)
79 sample = LittleShort ( ((short *)data)[srcsample] );
80 else
81 sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8;
82 if (sc->width == 2)
83 ((short *)sc->data)[i] = sample;
84 else
85 ((signed char *)sc->data)[i] = sample >> 8;
90 //=============================================================================
93 ==============
94 S_LoadSound
95 ==============
97 sfxcache_t *S_LoadSound (sfx_t *s)
99 char namebuffer[256];
100 byte *data;
101 wavinfo_t info;
102 int len;
103 float stepscale;
104 sfxcache_t *sc;
105 byte stackbuf[1*1024]; // avoid dirtying the cache heap
107 // see if still in memory
108 sc = Cache_Check (&s->cache);
109 if (sc)
110 return sc;
112 //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
113 // load it in
114 Q_strcpy(namebuffer, "sound/");
115 Q_strcat(namebuffer, s->name);
117 // Con_Printf ("loading %s\n",namebuffer);
119 data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf));
121 if (!data)
123 Con_Printf ("Couldn't load %s\n", namebuffer);
124 return NULL;
127 info = GetWavinfo (s->name, data, com_filesize);
128 if (info.channels != 1)
130 Con_Printf ("%s is a stereo sample\n",s->name);
131 return NULL;
134 stepscale = (float)info.rate / shm->speed;
135 len = info.samples / stepscale;
137 len = len * info.width * info.channels;
139 sc = Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
140 if (!sc)
141 return NULL;
143 sc->length = info.samples;
144 sc->loopstart = info.loopstart;
145 sc->speed = info.rate;
146 sc->width = info.width;
147 sc->stereo = info.channels;
149 ResampleSfx (s, sc->speed, sc->width, data + info.dataofs);
151 return sc;
157 ===============================================================================
159 WAV loading
161 ===============================================================================
165 byte *data_p;
166 byte *iff_end;
167 byte *last_chunk;
168 byte *iff_data;
169 int iff_chunk_len;
172 short GetLittleShort(void)
174 short val = 0;
175 val = *data_p;
176 val = val + (*(data_p+1)<<8);
177 data_p += 2;
178 return val;
181 int GetLittleLong(void)
183 int val = 0;
184 val = *data_p;
185 val = val + (*(data_p+1)<<8);
186 val = val + (*(data_p+2)<<16);
187 val = val + (*(data_p+3)<<24);
188 data_p += 4;
189 return val;
192 void FindNextChunk(char *name)
194 while (1)
196 data_p=last_chunk;
198 if (data_p >= iff_end)
199 { // didn't find the chunk
200 data_p = NULL;
201 return;
204 data_p += 4;
205 iff_chunk_len = GetLittleLong();
206 if (iff_chunk_len < 0)
208 data_p = NULL;
209 return;
211 // if (iff_chunk_len > 1024*1024)
212 // Sys_Error ("FindNextChunk: %i length is past the 1 meg sanity limit", iff_chunk_len);
213 data_p -= 8;
214 last_chunk = data_p + 8 + ( (iff_chunk_len + 1) & ~1 );
215 if (!Q_strncmp(data_p, name, 4))
216 return;
220 void FindChunk(char *name)
222 last_chunk = iff_data;
223 FindNextChunk (name);
227 void DumpChunks(void)
229 char str[5];
231 str[4] = 0;
232 data_p=iff_data;
235 memcpy (str, data_p, 4);
236 data_p += 4;
237 iff_chunk_len = GetLittleLong();
238 Con_Printf ("0x%x : %s (%d)\n", (int)(data_p - 4), str, iff_chunk_len);
239 data_p += (iff_chunk_len + 1) & ~1;
240 } while (data_p < iff_end);
244 ============
245 GetWavinfo
246 ============
248 wavinfo_t GetWavinfo (char *name, byte *wav, int wavlength)
250 wavinfo_t info;
251 int i;
252 int format;
253 int samples;
255 memset (&info, 0, sizeof(info));
257 if (!wav)
258 return info;
260 iff_data = wav;
261 iff_end = wav + wavlength;
263 // find "RIFF" chunk
264 FindChunk("RIFF");
265 if (!(data_p && !Q_strncmp(data_p+8, "WAVE", 4)))
267 Con_Printf("Missing RIFF/WAVE chunks\n");
268 return info;
271 // get "fmt " chunk
272 iff_data = data_p + 12;
273 // DumpChunks ();
275 FindChunk("fmt ");
276 if (!data_p)
278 Con_Printf("Missing fmt chunk\n");
279 return info;
281 data_p += 8;
282 format = GetLittleShort();
283 if (format != 1)
285 Con_Printf("Microsoft PCM format only\n");
286 return info;
289 info.channels = GetLittleShort();
290 info.rate = GetLittleLong();
291 data_p += 4+2;
292 info.width = GetLittleShort() / 8;
294 // get cue chunk
295 FindChunk("cue ");
296 if (data_p)
298 data_p += 32;
299 info.loopstart = GetLittleLong();
300 // Con_Printf("loopstart=%d\n", sfx->loopstart);
302 // if the next chunk is a LIST chunk, look for a cue length marker
303 FindNextChunk ("LIST");
304 if (data_p)
306 if (!strncmp (data_p + 28, "mark", 4))
307 { // this is not a proper parse, but it works with cooledit...
308 data_p += 24;
309 i = GetLittleLong (); // samples in loop
310 info.samples = info.loopstart + i;
311 // Con_Printf("looped length: %i\n", i);
315 else
316 info.loopstart = -1;
318 // find data chunk
319 FindChunk("data");
320 if (!data_p)
322 Con_Printf("Missing data chunk\n");
323 return info;
326 data_p += 4;
327 samples = GetLittleLong () / info.width;
329 if (info.samples)
331 if (samples < info.samples)
332 Sys_Error ("Sound %s has a bad loop length", name);
334 else
335 info.samples = samples;
337 info.dataofs = data_p - wav;
339 return info;