| 1 |
// Copyright (c) 1993-2011 PrBoom developers (see AUTHORS) |
| 2 |
// Licence: GPLv2 or later (see COPYING) |
| 3 |
|
| 4 |
// Convert WAVE files to Doom sound format |
| 5 |
|
| 6 |
#include "rd_sound.h" |
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "rd_util.h" |
| 12 |
|
| 13 |
// |
| 14 |
// wav_to_doom |
| 15 |
// |
| 16 |
// loads a wav and converts it into Doom's sound format |
| 17 |
// makes lots of assumptions about the format of a wav file. |
| 18 |
// |
| 19 |
|
| 20 |
#ifdef _MSC_VER |
| 21 |
#pragma pack(push) |
| 22 |
#pragma pack(1) |
| 23 |
#endif // _MSC_VER |
| 24 |
|
| 25 |
struct wav_header |
| 26 |
{ |
| 27 |
char riff[4]; |
| 28 |
unsigned int size; |
| 29 |
char wave[4]; |
| 30 |
char fmt[4]; |
| 31 |
unsigned int fmtlen; |
| 32 |
unsigned short fmttag; |
| 33 |
unsigned short channels; |
| 34 |
unsigned int samplerate; |
| 35 |
unsigned int byterate; |
| 36 |
unsigned short blockalign; |
| 37 |
unsigned short bits; |
| 38 |
char data[4]; |
| 39 |
unsigned int datalen; |
| 40 |
char samples[1]; |
| 41 |
} ATTR((packed)); |
| 42 |
|
| 43 |
struct doom_sound_header |
| 44 |
{ |
| 45 |
unsigned short log2bits; |
| 46 |
unsigned short rate; |
| 47 |
unsigned int length; |
| 48 |
char samples[1]; |
| 49 |
} ATTR((packed)); |
| 50 |
|
| 51 |
#ifdef _MSC_VER |
| 52 |
#pragma pack(pop) |
| 53 |
#endif // _MSC_VER |
| 54 |
|
| 55 |
size_t wav_to_doom(void **lumpdata, const char *filename) |
| 56 |
{ |
| 57 |
void *data; |
| 58 |
size_t size = read_or_die(&data, filename); |
| 59 |
struct wav_header *header = data; |
| 60 |
struct doom_sound_header *out; |
| 61 |
|
| 62 |
if (size < sizeof(*header) - 1 |
| 63 |
|| memcmp(header->riff, "RIFF", 4) != 0 |
| 64 |
|| memcmp(header->wave, "WAVE", 4) != 0) |
| 65 |
die("Invalid WAV file: %s\n", filename); |
| 66 |
|
| 67 |
size = sizeof(*out) - 1 + LONG(header->datalen); |
| 68 |
out = xmalloc(size); |
| 69 |
|
| 70 |
out->log2bits = 3; |
| 71 |
out->rate = SHORT(LONG(header->samplerate)); |
| 72 |
out->length = header->datalen; |
| 73 |
memmove(out->samples, header->samples, LONG(header->datalen)); |
| 74 |
|
| 75 |
free(data); |
| 76 |
|
| 77 |
*lumpdata = out; |
| 78 |
return size; |
| 79 |
} |