summaryrefslogtreecommitdiff
path: root/src/libs/sound/decoders/wav.c
blob: c22f63f77004d2ee94e6c96d8a6b6d1524a90c80 (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Wave decoder (.wav adapter)
 * Code is based on Creative's Win32 OpenAL implementation.
 */

#include <stdio.h>
#include <errno.h>
#include "port.h"
#include "types.h"
#include "libs/uio.h"
#include "endian_uqm.h"
#include "libs/log.h"
#include "wav.h"

#define wave_MAKE_ID(x1, x2, x3, x4) \
		(((x4) << 24) | ((x3) << 16) | ((x2) << 8) | (x1))

#define wave_RiffID   wave_MAKE_ID('R', 'I', 'F', 'F')
#define wave_WaveID   wave_MAKE_ID('W', 'A', 'V', 'E')
#define wave_FmtID    wave_MAKE_ID('f', 'm', 't', ' ')
#define wave_DataID   wave_MAKE_ID('d', 'a', 't', 'a')

typedef struct
{
	uint32 id;
	uint32 size;
	uint32 type;
} wave_FileHeader;

typedef struct
{
	uint16 format;
	uint16 channels;
	uint32 samplesPerSec;
	uint32 bytesPerSec;
	uint16 blockAlign;
	uint16 bitsPerSample;
} wave_FormatHeader;

typedef struct
{
	uint32 id;
	uint32 size;
} wave_ChunkHeader;


#define THIS_PTR TFB_SoundDecoder* This

static const char* wava_GetName (void);
static bool wava_InitModule (int flags, const TFB_DecoderFormats*);
static void wava_TermModule (void);
static uint32 wava_GetStructSize (void);
static int wava_GetError (THIS_PTR);
static bool wava_Init (THIS_PTR);
static void wava_Term (THIS_PTR);
static bool wava_Open (THIS_PTR, uio_DirHandle *dir, const char *filename);
static void wava_Close (THIS_PTR);
static int wava_Decode (THIS_PTR, void* buf, sint32 bufsize);
static uint32 wava_Seek (THIS_PTR, uint32 pcm_pos);
static uint32 wava_GetFrame (THIS_PTR);

TFB_SoundDecoderFuncs wava_DecoderVtbl = 
{
	wava_GetName,
	wava_InitModule,
	wava_TermModule,
	wava_GetStructSize,
	wava_GetError,
	wava_Init,
	wava_Term,
	wava_Open,
	wava_Close,
	wava_Decode,
	wava_Seek,
	wava_GetFrame,
};

typedef struct tfb_wavesounddecoder
{
	// always the first member
	TFB_SoundDecoder decoder;

	// private
	sint32 last_error;
	uio_Stream *fp;
	wave_FormatHeader fmtHdr;
	uint32 data_ofs;
	uint32 data_size;
	uint32 max_pcm;
	uint32 cur_pcm;

} TFB_WaveSoundDecoder;

static const TFB_DecoderFormats* wava_formats = NULL;


static const char*
wava_GetName (void)
{
	return "Wave";
}

static bool
wava_InitModule (int flags, const TFB_DecoderFormats* fmts)
{
	wava_formats = fmts;
	return true;
	
	(void)flags;	// laugh at compiler warning
}

static void
wava_TermModule (void)
{
	// no specific module term
}

static uint32
wava_GetStructSize (void)
{
	return sizeof (TFB_WaveSoundDecoder);
}

static int
wava_GetError (THIS_PTR)
{
	TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;
	int ret = wava->last_error;
	wava->last_error = 0;
	return ret;
}

static bool
wava_Init (THIS_PTR)
{
	//TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;
	This->need_swap = wava_formats->want_big_endian;
	return true;
}

static void
wava_Term (THIS_PTR)
{
	//TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;
	wava_Close (This); // ensure cleanup
}

static bool
read_le_16 (uio_Stream *fp, uint16 *v)
{
	if (!uio_fread (v, sizeof(*v), 1, fp))
		return false;
	*v = UQM_SwapLE16 (*v);
	return true;
}

static bool
read_le_32 (uio_Stream *fp, uint32 *v)
{
	if (!uio_fread (v, sizeof(*v), 1, fp))
		return false;
	*v = UQM_SwapLE32 (*v);
	return true;
}

static bool
wava_readFileHeader (TFB_WaveSoundDecoder* wava, wave_FileHeader* hdr)
{
	if (!read_le_32 (wava->fp, &hdr->id) ||
			!read_le_32 (wava->fp, &hdr->size) ||
			!read_le_32 (wava->fp, &hdr->type))
	{
		wava->last_error = errno;
		return false;
	}
	return true;
}

static bool
wava_readChunkHeader (TFB_WaveSoundDecoder* wava, wave_ChunkHeader* chunk)
{
	if (!read_le_32 (wava->fp, &chunk->id) ||
			!read_le_32 (wava->fp, &chunk->size))
	{
		wava->last_error = errno;
		return false;
	}
	return true;
}

static bool
wava_readFormatHeader (TFB_WaveSoundDecoder* wava, wave_FormatHeader* fmt)
{
	if (!read_le_16 (wava->fp, &fmt->format) ||
			!read_le_16 (wava->fp, &fmt->channels) ||
			!read_le_32 (wava->fp, &fmt->samplesPerSec) ||
			!read_le_32 (wava->fp, &fmt->bytesPerSec) ||
			!read_le_16 (wava->fp, &fmt->blockAlign) ||
			!read_le_16 (wava->fp, &fmt->bitsPerSample))
	{
		wava->last_error = errno;
		return false;
	}
	return true;
}

static bool
wava_Open (THIS_PTR, uio_DirHandle *dir, const char *filename)
{
	TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;
	wave_FileHeader fileHdr;
	wave_ChunkHeader chunkHdr;
	long dataLeft; 

	wava->fp = uio_fopen (dir, filename, "rb");
	if (!wava->fp)
	{
		wava->last_error = errno;
		return false;
	}

	wava->data_size = 0;
	wava->data_ofs = 0;

	// read wave header
	if (!wava_readFileHeader (wava, &fileHdr))
	{
		wava->last_error = errno;
		wava_Close (This);
		return false;
	}
	if (fileHdr.id != wave_RiffID || fileHdr.type != wave_WaveID)
	{
		log_add (log_Warning, "wava_Open(): "
				"not a wave file, ID 0x%08x, Type 0x%08x",
				fileHdr.id, fileHdr.type);
		wava_Close (This);
		return false;
	}

	for (dataLeft = ((fileHdr.size + 1) & ~1) - 4; dataLeft > 0;
			dataLeft -= (((chunkHdr.size + 1) & ~1) + 8))
	{
		if (!wava_readChunkHeader (wava, &chunkHdr))
		{
			wava_Close (This);
			return false;
		}

		if (chunkHdr.id == wave_FmtID)
		{
			if (!wava_readFormatHeader (wava, &wava->fmtHdr))
			{
				wava_Close (This);
				return false;
			}
			uio_fseek (wava->fp, chunkHdr.size - 16, SEEK_CUR);
		}
		else
		{
			if (chunkHdr.id == wave_DataID)
			{
				wava->data_size = chunkHdr.size;
				wava->data_ofs = uio_ftell (wava->fp);
			}
			uio_fseek (wava->fp, chunkHdr.size, SEEK_CUR);
		}

		// 2-align the file ptr
		// XXX: I do not think this is necessary in WAVE files;
		//   possibly a remnant of ported AIFF reader
		uio_fseek (wava->fp, chunkHdr.size & 1, SEEK_CUR);
	}

	if (!wava->data_size || !wava->data_ofs)
	{
		log_add (log_Warning, "wava_Open(): bad wave file,"
				" no DATA chunk found");
		wava_Close (This);
		return false;
	}

	if (wava->fmtHdr.format != 0x0001)
	{	// not a PCM format
		log_add (log_Warning, "wava_Open(): unsupported format %x",
				wava->fmtHdr.format);
		wava_Close (This);
		return false;
	}
	if (wava->fmtHdr.channels != 1 && wava->fmtHdr.channels != 2)
	{
		log_add (log_Warning, "wava_Open(): unsupported number of channels %u",
				(unsigned)wava->fmtHdr.channels);
		wava_Close (This);
		return false;
	}

	if (dataLeft != 0)
		log_add (log_Warning, "wava_Open(): bad or unsupported wave file, "
				"size in header does not match read chunks");

	This->format = (wava->fmtHdr.channels == 1 ?
			(wava->fmtHdr.bitsPerSample == 8 ?
				wava_formats->mono8 : wava_formats->mono16)
			:
			(wava->fmtHdr.bitsPerSample == 8 ?
				wava_formats->stereo8 : wava_formats->stereo16)
			);
	This->frequency = wava->fmtHdr.samplesPerSec;

	uio_fseek (wava->fp, wava->data_ofs, SEEK_SET);
	wava->max_pcm = wava->data_size / wava->fmtHdr.blockAlign;
	wava->cur_pcm = 0;
	This->length = (float) wava->max_pcm / wava->fmtHdr.samplesPerSec;
	wava->last_error = 0;

	return true;
}

static void
wava_Close (THIS_PTR)
{
	TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;

	if (wava->fp)
	{
		uio_fclose (wava->fp);
		wava->fp = NULL;
	}
}

static int
wava_Decode (THIS_PTR, void* buf, sint32 bufsize)
{
	TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;
	uint32 dec_pcm;

	dec_pcm = bufsize / wava->fmtHdr.blockAlign;
	if (dec_pcm > wava->max_pcm - wava->cur_pcm)
		dec_pcm = wava->max_pcm - wava->cur_pcm;

	dec_pcm = uio_fread (buf, wava->fmtHdr.blockAlign, dec_pcm, wava->fp);
	wava->cur_pcm += dec_pcm;
	
	return dec_pcm * wava->fmtHdr.blockAlign;
}

static uint32
wava_Seek (THIS_PTR, uint32 pcm_pos)
{
	TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;

	if (pcm_pos > wava->max_pcm)
		pcm_pos = wava->max_pcm;
	wava->cur_pcm = pcm_pos;
	uio_fseek (wava->fp,
			wava->data_ofs + pcm_pos * wava->fmtHdr.blockAlign,
			SEEK_SET);

	return pcm_pos;
}

static uint32
wava_GetFrame (THIS_PTR)
{
	//TFB_WaveSoundDecoder* wava = (TFB_WaveSoundDecoder*) This;
	return 0; // only 1 frame for now

	(void)This;	// laugh at compiler warning
}