2008-07-03 04:20:52 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../layout/layout.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
/* Westwood Studios .aud (WS-AUD) */
|
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
VGMSTREAM* init_vgmstream_ws_aud(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
2008-07-03 04:20:52 +02:00
|
|
|
coding_t coding_type = -1;
|
2008-07-03 23:21:01 +02:00
|
|
|
off_t format_offset;
|
2023-06-24 16:58:44 +02:00
|
|
|
int channels;
|
|
|
|
bool new_type = false;
|
2008-07-03 04:20:52 +02:00
|
|
|
int bytes_per_sample = 0;
|
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
|
|
|
|
/* checks **/
|
|
|
|
if (!check_extensions(sf, "aud") )
|
|
|
|
return NULL;
|
2008-07-03 04:20:52 +02:00
|
|
|
|
|
|
|
/* check for 0x0000DEAF chunk marker for first chunk */
|
2023-06-24 16:58:44 +02:00
|
|
|
if (read_u32le(0x10,sf) == 0x0000DEAF) { /* new */
|
|
|
|
new_type = true;
|
|
|
|
format_offset = 0x0A;
|
|
|
|
}
|
|
|
|
else if (read_u32le(0x0C,sf) == 0x0000DEAF) { /* old */
|
|
|
|
new_type = false;
|
|
|
|
format_offset = 0x06;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* blocked format with a mini-header */
|
2008-07-03 04:20:52 +02:00
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
if (read_u8(format_offset + 0x00, sf) & 1)
|
|
|
|
channels = 2;
|
2008-07-03 04:20:52 +02:00
|
|
|
else
|
2023-06-24 16:58:44 +02:00
|
|
|
channels = 1;
|
2008-07-03 04:20:52 +02:00
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
if (channels == 2)
|
|
|
|
goto fail; /* not seen */
|
2008-07-03 04:20:52 +02:00
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
if (read_u8(format_offset + 0x01,sf) & 2)
|
2008-07-03 04:20:52 +02:00
|
|
|
bytes_per_sample = 2;
|
|
|
|
else
|
|
|
|
bytes_per_sample = 1;
|
|
|
|
|
|
|
|
/* check codec type */
|
2023-06-24 16:58:44 +02:00
|
|
|
switch (read_u8(format_offset + 0x01,sf)) {
|
2008-07-03 04:20:52 +02:00
|
|
|
case 1: /* Westwood custom */
|
2008-07-03 23:21:01 +02:00
|
|
|
coding_type = coding_WS;
|
2023-06-24 16:58:44 +02:00
|
|
|
if (bytes_per_sample != 1) goto fail; /* shouldn't happen? */
|
2008-07-03 04:20:52 +02:00
|
|
|
break;
|
|
|
|
case 99: /* IMA ADPCM */
|
2017-11-19 03:34:25 +01:00
|
|
|
coding_type = coding_IMA_int;
|
2008-07-03 04:20:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2023-06-24 16:58:44 +02:00
|
|
|
vgmstream = allocate_vgmstream(channels, 0);
|
2008-07-03 04:20:52 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
2008-07-03 23:21:01 +02:00
|
|
|
if (new_type) {
|
2023-06-24 16:58:44 +02:00
|
|
|
vgmstream->num_samples = read_32bitLE(0x06,sf)/bytes_per_sample/channels;
|
|
|
|
}
|
|
|
|
else {
|
2008-07-03 23:21:01 +02:00
|
|
|
/* Doh, no output size in old type files. We have to read through the
|
|
|
|
* file looking at chunk headers! Crap! */
|
|
|
|
int32_t out_size = 0;
|
|
|
|
off_t current_offset = 0x8;
|
2023-06-24 16:58:44 +02:00
|
|
|
off_t file_size = get_streamfile_size(sf);
|
2008-07-03 23:21:01 +02:00
|
|
|
|
|
|
|
while (current_offset < file_size) {
|
|
|
|
int16_t chunk_size;
|
2023-06-24 16:58:44 +02:00
|
|
|
chunk_size = read_16bitLE(current_offset,sf);
|
|
|
|
out_size += read_16bitLE(current_offset+2,sf);
|
2008-07-03 23:21:01 +02:00
|
|
|
/* while we're here might as well check for valid chunks */
|
2023-06-24 16:58:44 +02:00
|
|
|
if (read_32bitLE(current_offset+4,sf) != 0x0000DEAF) goto fail;
|
2008-07-03 23:21:01 +02:00
|
|
|
current_offset+=8+chunk_size;
|
|
|
|
}
|
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
vgmstream->num_samples = out_size/bytes_per_sample/channels;
|
2008-07-03 23:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* they tend to not actually have data for the last odd sample */
|
2008-07-03 22:09:36 +02:00
|
|
|
if (vgmstream->num_samples & 1) vgmstream->num_samples--;
|
2023-06-24 16:58:44 +02:00
|
|
|
vgmstream->sample_rate = (uint16_t)read_16bitLE(0x00,sf);
|
2008-07-03 04:20:52 +02:00
|
|
|
|
|
|
|
vgmstream->coding_type = coding_type;
|
|
|
|
if (new_type) {
|
|
|
|
vgmstream->meta_type = meta_WS_AUD;
|
|
|
|
}
|
|
|
|
|
2018-03-29 19:00:04 +02:00
|
|
|
vgmstream->layout_type = layout_blocked_ws_aud;
|
2008-07-03 04:20:52 +02:00
|
|
|
|
2023-06-24 16:58:44 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream, sf, 0x00) )
|
|
|
|
goto fail;
|
2008-07-03 04:20:52 +02:00
|
|
|
|
|
|
|
if (new_type) {
|
2023-06-24 16:58:44 +02:00
|
|
|
block_update(0x0c, vgmstream);
|
2008-07-03 04:20:52 +02:00
|
|
|
} else {
|
2023-06-24 16:58:44 +02:00
|
|
|
block_update(0x08, vgmstream);
|
2008-07-03 04:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|