summaryrefslogtreecommitdiff
path: root/src/client/sound/mem.c
blob: 57767a2abd804f5795967e42ee29fec61f3548a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// snd_mem.c: sound caching

#include "sound.h"

wavinfo_t s_info;

#if USE_SNDDMA
/*
================
ResampleSfx
================
*/
static sfxcache_t *ResampleSfx(sfx_t *sfx)
{
    int         outcount;
    int         srcsample;
    float       stepscale;
    int         i;
    int         samplefrac, fracstep;
    sfxcache_t  *sc;

    stepscale = (float)s_info.rate / dma.speed;      // this is usually 0.5, 1, or 2

    outcount = s_info.samples / stepscale;
    if (!outcount) {
        Com_DPrintf("%s resampled to zero length\n", s_info.name);
        sfx->error = Q_ERR_TOO_FEW;
        return NULL;
    }

    sc = sfx->cache = S_Malloc(outcount * s_info.width + sizeof(sfxcache_t) - 1);

    sc->length = outcount;
    sc->loopstart = s_info.loopstart == -1 ? -1 : s_info.loopstart / stepscale;
    sc->width = s_info.width;

// resample / decimate to the current source rate
//Com_Printf("%s: %f, %d\n",sfx->name,stepscale,sc->width);
    if (stepscale == 1) {
// fast special case
        if (sc->width == 1) {
            memcpy(sc->data, s_info.data, outcount);
        } else {
#if __BYTE_ORDER == __LITTLE_ENDIAN
            memcpy(sc->data, s_info.data, outcount << 1);
#else
            for (i = 0; i < outcount; i++) {
                ((uint16_t *)sc->data)[i] = LittleShort(((uint16_t *)s_info.data)[i]);
            }
#endif
        }
    } else {
// general case
        samplefrac = 0;
        fracstep = stepscale * 256;
        if (sc->width == 1) {
            for (i = 0; i < outcount; i++) {
                srcsample = samplefrac >> 8;
                samplefrac += fracstep;
                sc->data[i] = s_info.data[srcsample];
            }
        } else {
            for (i = 0; i < outcount; i++) {
                srcsample = samplefrac >> 8;
                samplefrac += fracstep;
                ((uint16_t *)sc->data)[i] = LittleShort(((uint16_t *)s_info.data)[srcsample]);
            }
        }
    }

    return sc;
}
#endif

/*
===============================================================================

WAV loading

===============================================================================
*/

static byte     *data_p;
static byte     *iff_end;
static byte     *iff_data;
static uint32_t iff_chunk_len;

static int GetLittleShort(void)
{
    int val;

    if (data_p + 2 > iff_end) {
        return -1;
    }

    val = LittleShortMem(data_p);
    data_p += 2;
    return val;
}

static int GetLittleLong(void)
{
    int val;

    if (data_p + 4 > iff_end) {
        return -1;
    }

    val = LittleLongMem(data_p);
    data_p += 4;
    return val;
}

static void FindNextChunk(uint32_t search)
{
    uint32_t chunk, len;
    size_t remaining;

    while (data_p + 8 < iff_end) {
        chunk = RawLongMem(data_p); data_p += 4;
        len = LittleLongMem(data_p); data_p += 4;
        remaining = (size_t)(iff_end - data_p);
        if (len > remaining) {
            len = remaining;
        }
        if (chunk == search) {
            iff_chunk_len = len;
            return;
        }
        data_p += (len + 1) & ~1;
    }

    // didn't find the chunk
    data_p = NULL;
}

static void FindChunk(uint32_t search)
{
    data_p = iff_data;
    FindNextChunk(search);
}

#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')

static qboolean GetWavinfo(void)
{
    int format;
    int samples, width;
    uint32_t chunk;

// find "RIFF" chunk
    FindChunk(TAG_RIFF);
    if (!data_p) {
        Com_DPrintf("%s has missing/invalid RIFF chunk\n", s_info.name);
        return qfalse;
    }
    chunk = GetLittleLong();
    if (chunk != TAG_WAVE) {
        Com_DPrintf("%s has missing/invalid WAVE chunk\n", s_info.name);
        return qfalse;
    }

    iff_data = data_p;

// get "fmt " chunk
    FindChunk(TAG_fmt);
    if (!data_p) {
        Com_DPrintf("%s has missing/invalid fmt chunk\n", s_info.name);
        return qfalse;
    }
    format = GetLittleShort();
    if (format != 1) {
        Com_DPrintf("%s has non-Microsoft PCM format\n", s_info.name);
        return qfalse;
    }

    format = GetLittleShort();
    if (format != 1) {
        Com_DPrintf("%s has bad number of channels\n", s_info.name);
        return qfalse;
    }

    s_info.rate = GetLittleLong();
    if (s_info.rate < 8000 || s_info.rate > 48000) {
        Com_DPrintf("%s has bad rate\n", s_info.name);
        return qfalse;
    }

    data_p += 4 + 2;

    width = GetLittleShort();
    switch (width) {
    case 8:
        s_info.width = 1;
        break;
    case 16:
        s_info.width = 2;
        break;
    default:
        Com_DPrintf("%s has bad width\n", s_info.name);
        return qfalse;
    }

// get cue chunk
    FindChunk(TAG_cue);
    if (data_p) {
        data_p += 24;
        s_info.loopstart = GetLittleLong();
        if (s_info.loopstart < 0 || s_info.loopstart > INT_MAX) {
            Com_DPrintf("%s has bad loop start\n", s_info.name);
            return qfalse;
        }

        FindNextChunk(TAG_LIST);
        if (data_p) {
            data_p += 20;
            chunk = GetLittleLong();
            if (chunk == TAG_MARK) {
                // this is not a proper parse, but it works with cooledit...
                data_p += 16;
                samples = GetLittleLong();    // samples in loop
                if (samples < 0 || samples > INT_MAX - s_info.loopstart) {
                    Com_DPrintf("%s has bad loop length\n", s_info.name);
                    return qfalse;
                }
                s_info.samples = s_info.loopstart + samples;
            }
        }
    } else {
        s_info.loopstart = -1;
    }

// find data chunk
    FindChunk(TAG_data);
    if (!data_p) {
        Com_DPrintf("%s has missing/invalid data chunk\n", s_info.name);
        return qfalse;
    }

    samples = iff_chunk_len / s_info.width;
    if (!samples) {
        Com_DPrintf("%s has zero length\n", s_info.name);
        return qfalse;
    }

    if (s_info.samples) {
        if (samples < s_info.samples) {
            Com_DPrintf("%s has bad loop length\n", s_info.name);
            return qfalse;
        }
    } else {
        s_info.samples = samples;
    }

    s_info.data = data_p;

    return qtrue;
}

/*
==============
S_LoadSound
==============
*/
sfxcache_t *S_LoadSound(sfx_t *s)
{
    byte        *data;
    sfxcache_t  *sc;
    ssize_t     len;
    char        *name;

    if (s->name[0] == '*')
        return NULL;

// see if still in memory
    sc = s->cache;
    if (sc)
        return sc;

// don't retry after error
    if (s->error)
        return NULL;

// load it in
    if (s->truename)
        name = s->truename;
    else
        name = s->name;

    len = FS_LoadFile(name, (void **)&data);
    if (!data) {
        s->error = len;
        return NULL;
    }

    memset(&s_info, 0, sizeof(s_info));
    s_info.name = name;

    iff_data = data;
    iff_end = data + len;
    if (!GetWavinfo()) {
        s->error = Q_ERR_INVALID_FORMAT;
        goto fail;
    }

#if USE_OPENAL
    if (s_started == SS_OAL)
        sc = AL_UploadSfx(s);
#endif

#if USE_SNDDMA
    if (s_started == SS_DMA)
        sc = ResampleSfx(s);
#endif

fail:
    FS_FreeFile(data);
    return sc;
}