2008-07-17 03:22:23 +02:00
|
|
|
#include "meta.h"
|
2019-08-03 23:35:10 +02:00
|
|
|
#include "../coding/coding.h"
|
|
|
|
|
|
|
|
typedef enum { MFX, MFX_BANK, SFX_BANK, SBNK, FORM } musx_form;
|
2021-01-08 00:20:04 +01:00
|
|
|
typedef enum { PSX, DSP, XBOX, IMA, DAT, NGCA, PCM } musx_codec;
|
2019-08-03 23:35:10 +02:00
|
|
|
typedef struct {
|
|
|
|
int big_endian;
|
|
|
|
int version;
|
|
|
|
size_t file_size;
|
|
|
|
|
|
|
|
int total_subsongs;
|
|
|
|
int is_old;
|
|
|
|
|
|
|
|
off_t tables_offset;
|
|
|
|
off_t loops_offset;
|
|
|
|
off_t stream_offset;
|
|
|
|
size_t stream_size;
|
|
|
|
off_t coefs_offset;
|
|
|
|
|
|
|
|
musx_form form;
|
|
|
|
musx_codec codec;
|
|
|
|
uint32_t platform;
|
|
|
|
|
|
|
|
int channels;
|
|
|
|
int sample_rate;
|
|
|
|
int loop_flag;
|
|
|
|
int32_t loop_start;
|
|
|
|
int32_t loop_end;
|
|
|
|
int32_t num_samples;
|
|
|
|
int32_t loop_start_sample;
|
|
|
|
int32_t loop_end_sample;
|
|
|
|
} musx_header;
|
2008-07-17 03:22:23 +02:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
static int parse_musx(STREAMFILE* sf, musx_header* musx);
|
2010-05-02 21:16:35 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* MUSX - from Eurocom's games */
|
2021-01-08 00:20:04 +01:00
|
|
|
VGMSTREAM* init_vgmstream_musx(STREAMFILE* sf) {
|
|
|
|
VGMSTREAM* vgmstream = NULL;
|
2008-07-17 03:22:23 +02:00
|
|
|
off_t start_offset;
|
2019-08-03 23:35:10 +02:00
|
|
|
musx_header musx = {0};
|
2008-07-17 03:22:23 +02:00
|
|
|
|
|
|
|
|
2018-03-10 18:51:57 +01:00
|
|
|
/* checks */
|
2019-08-03 23:35:10 +02:00
|
|
|
/* .sfx: actual extension (extracted from bigfiles with sometimes encrypted names)
|
|
|
|
* .musx: header id */
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!check_extensions(sf, "sfx,musx"))
|
2018-03-10 18:51:57 +01:00
|
|
|
goto fail;
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!is_id32be(0x00,sf, "MUSX"))
|
2018-03-10 18:51:57 +01:00
|
|
|
goto fail;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!parse_musx(sf, &musx))
|
2018-03-10 18:51:57 +01:00
|
|
|
goto fail;
|
2010-04-20 22:26:10 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
start_offset = musx.stream_offset;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2018-03-10 18:51:57 +01:00
|
|
|
/* build the VGMSTREAM */
|
2019-08-03 23:35:10 +02:00
|
|
|
vgmstream = allocate_vgmstream(musx.channels, musx.loop_flag);
|
2008-07-17 03:22:23 +02:00
|
|
|
if (!vgmstream) goto fail;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
vgmstream->meta_type = meta_MUSX;
|
|
|
|
vgmstream->sample_rate = musx.sample_rate;
|
|
|
|
vgmstream->num_streams = musx.total_subsongs;
|
|
|
|
vgmstream->stream_size = musx.stream_size;
|
|
|
|
|
|
|
|
switch (musx.codec) {
|
|
|
|
case PSX:
|
|
|
|
vgmstream->num_samples = ps_bytes_to_samples(musx.stream_size, musx.channels);
|
|
|
|
vgmstream->loop_start_sample = ps_bytes_to_samples(musx.loop_start, musx.channels);
|
|
|
|
vgmstream->loop_end_sample = ps_bytes_to_samples(musx.loop_end, musx.channels);
|
|
|
|
|
2018-03-10 18:51:57 +01:00
|
|
|
vgmstream->coding_type = coding_PSX;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x80;
|
|
|
|
break;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
case DAT:
|
|
|
|
vgmstream->num_samples = dat4_ima_bytes_to_samples(musx.stream_size, musx.channels);
|
|
|
|
vgmstream->loop_start_sample = dat4_ima_bytes_to_samples(musx.loop_start, musx.channels);
|
|
|
|
vgmstream->loop_end_sample = dat4_ima_bytes_to_samples(musx.loop_end, musx.channels);
|
|
|
|
|
2018-03-10 18:51:57 +01:00
|
|
|
vgmstream->coding_type = coding_DAT4_IMA;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x20;
|
|
|
|
break;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
case IMA:
|
|
|
|
vgmstream->num_samples = ima_bytes_to_samples(musx.stream_size, musx.channels);
|
|
|
|
vgmstream->loop_start_sample = musx.loop_start / 4; /* weird but needed */
|
|
|
|
vgmstream->loop_end_sample = musx.loop_end / 4;
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_DVI_IMA_int;
|
2018-03-10 18:51:57 +01:00
|
|
|
vgmstream->layout_type = layout_interleave;
|
2019-08-03 23:35:10 +02:00
|
|
|
vgmstream->interleave_block_size = 0x01;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XBOX:
|
|
|
|
vgmstream->num_samples = xbox_ima_bytes_to_samples(musx.stream_size, musx.channels);
|
|
|
|
vgmstream->loop_start_sample = xbox_ima_bytes_to_samples(musx.loop_start, musx.channels);
|
|
|
|
vgmstream->loop_end_sample = xbox_ima_bytes_to_samples(musx.loop_end, musx.channels);
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_XBOX_IMA;
|
|
|
|
vgmstream->layout_type = layout_none;
|
2018-03-10 18:51:57 +01:00
|
|
|
break;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
case DSP:
|
|
|
|
vgmstream->num_samples = dsp_bytes_to_samples(musx.stream_size, musx.channels);
|
|
|
|
vgmstream->loop_start_sample = dsp_bytes_to_samples(musx.loop_start, musx.channels);
|
|
|
|
vgmstream->loop_end_sample = dsp_bytes_to_samples(musx.loop_end, musx.channels);
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x08;
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
dsp_read_coefs(vgmstream,sf,musx.coefs_offset+0x1c, 0x60, musx.big_endian);
|
|
|
|
dsp_read_hist(vgmstream,sf,musx.coefs_offset+0x40, 0x60, musx.big_endian);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PCM:
|
|
|
|
//vgmstream->num_samples = pcm_bytes_to_samples(musx.stream_size, musx.channels, 16);
|
|
|
|
//vgmstream->loop_start_sample = pcm_bytes_to_samples(musx.loop_start, musx.channels, 16);
|
|
|
|
//vgmstream->loop_end_sample = pcm_bytes_to_samples(musx.loop_end, musx.channels, 16);
|
|
|
|
|
|
|
|
vgmstream->coding_type = musx.big_endian ? coding_PCM16BE : coding_PCM16LE; /* only seen on PC but just in case */
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = 0x02;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGCA:
|
|
|
|
if (!is_id32be(start_offset,sf, "NGCA"))
|
|
|
|
goto fail;
|
|
|
|
/* 0x04: size (stereo full-interleaves 2 NGCA headers but sizes count all) */
|
|
|
|
/* 0x08: channels */
|
|
|
|
/* 0x0c: coefs */
|
|
|
|
musx.coefs_offset = start_offset;
|
|
|
|
start_offset += 0x40;
|
|
|
|
//musx.stream_size -= 0x40 * musx.channels; /* needed for interleave */
|
|
|
|
|
|
|
|
//vgmstream->num_samples = dsp_bytes_to_samples(musx.stream_size - 0x40 * musx.channels, musx.channels);
|
|
|
|
//vgmstream->loop_start_sample = dsp_bytes_to_samples(musx.loop_start + 0x40, musx.channels);
|
|
|
|
//vgmstream->loop_end_sample = dsp_bytes_to_samples(musx.loop_end - 0x40 * musx.channels, musx.channels);
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
vgmstream->interleave_block_size = musx.stream_size / musx.channels;
|
|
|
|
|
|
|
|
dsp_read_coefs(vgmstream, sf, musx.coefs_offset+0x0c, vgmstream->interleave_block_size, musx.big_endian);
|
|
|
|
//dsp_read_hist(vgmstream, sf, musx.coefs_offset+0x30, 0x00, musx.big_endian); /* used? */
|
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
break;
|
|
|
|
|
2010-05-02 21:16:35 +02:00
|
|
|
default:
|
2018-03-10 18:51:57 +01:00
|
|
|
goto fail;
|
2010-04-20 22:26:10 +02:00
|
|
|
}
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
if (musx.num_samples)
|
|
|
|
vgmstream->num_samples = musx.num_samples;
|
|
|
|
if (musx.loop_flag && musx.loop_start_sample)
|
|
|
|
vgmstream->loop_start_sample = musx.loop_start_sample;
|
|
|
|
if (musx.loop_flag && musx.loop_end_sample)
|
|
|
|
vgmstream->loop_end_sample = musx.loop_end_sample;
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
|
2018-03-10 18:51:57 +01:00
|
|
|
goto fail;
|
2010-05-02 21:16:35 +02:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2018-03-10 18:51:57 +01:00
|
|
|
close_vgmstream(vgmstream);
|
2010-05-02 21:16:35 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
static int parse_musx_stream(STREAMFILE* sf, musx_header* musx) {
|
|
|
|
uint32_t (*read_u32)(off_t,STREAMFILE*) = NULL;
|
2019-08-03 23:35:10 +02:00
|
|
|
int default_channels, default_sample_rate;
|
2010-05-02 21:16:35 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
if (musx->big_endian) {
|
2021-01-08 00:20:04 +01:00
|
|
|
read_u32 = read_u32be;
|
2019-08-03 23:35:10 +02:00
|
|
|
} else {
|
2021-01-08 00:20:04 +01:00
|
|
|
read_u32 = read_u32le;
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2010-05-02 21:16:35 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* autodetect for older versions that have no info */
|
|
|
|
if (musx->platform == 0) {
|
|
|
|
if (musx->big_endian) {
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->platform = get_id32be("GC02"); /* (fake) */
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
off_t offset = musx->stream_offset;
|
|
|
|
size_t max = 0x5000;
|
|
|
|
if (max > musx->stream_size)
|
|
|
|
max = musx->stream_size;
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (ps_check_format(sf, offset, max)) {
|
|
|
|
musx->platform = get_id32be("PS2_");
|
2019-08-03 23:35:10 +02:00
|
|
|
} else {
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->platform = get_id32be("XB02"); /* (fake) */
|
2018-03-10 18:51:57 +01:00
|
|
|
}
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
2010-05-02 21:16:35 +02:00
|
|
|
}
|
|
|
|
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* defaults */
|
|
|
|
switch(musx->platform) {
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x5053325F: /* "PS2_" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 32000;
|
|
|
|
musx->codec = PSX;
|
|
|
|
break;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x47435F5F: /* "GC__" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 32000;
|
|
|
|
musx->codec = DAT;
|
|
|
|
break;
|
2008-07-17 16:20:42 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x47433032: /* "GC02" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 32000;
|
|
|
|
if (musx->coefs_offset)
|
|
|
|
musx->codec = DSP;
|
|
|
|
else
|
|
|
|
musx->codec = IMA;
|
|
|
|
break;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x58425F5F: /* "XB__" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 44100;
|
|
|
|
musx->codec = DAT;
|
|
|
|
break;
|
2008-12-16 16:48:21 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x58423032: /* "XB02" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 44100;
|
|
|
|
musx->codec = XBOX;
|
|
|
|
break;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x5053505F: /* "PSP_" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 32768;
|
|
|
|
musx->codec = PSX;
|
|
|
|
break;
|
2018-03-16 22:57:46 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x5749495F: /* "WII_" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 44100;
|
|
|
|
musx->codec = DAT;
|
|
|
|
break;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x5053335F: /* "PS3_" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 44100;
|
|
|
|
musx->codec = DAT;
|
2018-03-10 18:51:57 +01:00
|
|
|
break;
|
2018-03-16 22:57:46 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x58455F5F: /* "XE__" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 32000;
|
|
|
|
musx->codec = DAT;
|
2019-11-23 21:36:57 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x50435F5F: /* "PC__" */
|
|
|
|
default_channels = 2;
|
|
|
|
default_sample_rate = 44100;
|
|
|
|
musx->codec = DAT;
|
2018-03-10 18:51:57 +01:00
|
|
|
break;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
2018-03-10 18:51:57 +01:00
|
|
|
default:
|
2019-08-03 23:35:10 +02:00
|
|
|
VGM_LOG("MUSX: unknown platform %x\n", musx->platform);
|
2018-03-10 18:51:57 +01:00
|
|
|
goto fail;
|
2010-05-02 21:16:35 +02:00
|
|
|
}
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
if (musx->channels == 0)
|
|
|
|
musx->channels = default_channels;
|
|
|
|
if (musx->sample_rate == 0)
|
|
|
|
musx->sample_rate = default_sample_rate;
|
|
|
|
|
|
|
|
|
|
|
|
/* parse loops and other info */
|
|
|
|
if (musx->tables_offset && musx->loops_offset) {
|
|
|
|
int i, cues2_count;
|
|
|
|
off_t cues2_offset;
|
|
|
|
|
|
|
|
/* cue/stream position table thing */
|
|
|
|
/* 0x00: cues1 entries (entry size 0x34 or 0x18)
|
|
|
|
* 0x04: cues2 entries (entry size 0x20 or 0x14)
|
|
|
|
* 0x08: header size (always 0x14)
|
|
|
|
* 0x0c: cues2 start
|
|
|
|
* 0x10: volume? (usually <= 100) */
|
|
|
|
|
|
|
|
/* find loops (cues1 also seems to have this info but this looks ok) */
|
2021-01-08 00:20:04 +01:00
|
|
|
cues2_count = read_u32(musx->loops_offset+0x04, sf);
|
|
|
|
cues2_offset = musx->loops_offset + read_u32(musx->loops_offset+0x0c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
for (i = 0; i < cues2_count; i++) {
|
|
|
|
uint32_t type, offset1, offset2;
|
|
|
|
|
|
|
|
if (musx->is_old) {
|
2021-01-08 00:20:04 +01:00
|
|
|
offset1 = read_u32(cues2_offset + i*0x20 + 0x04, sf);
|
|
|
|
type = read_u32(cues2_offset + i*0x20 + 0x08, sf);
|
|
|
|
offset2 = read_u32(cues2_offset + i*0x20 + 0x14, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
} else {
|
2021-01-08 00:20:04 +01:00
|
|
|
offset1 = read_u32(cues2_offset + i*0x14 + 0x04, sf);
|
|
|
|
type = read_u32(cues2_offset + i*0x14 + 0x08, sf);
|
|
|
|
offset2 = read_u32(cues2_offset + i*0x14 + 0x0c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* other types (0x0a, 0x09) look like section/end markers, 0x06/07 only seems to exist once */
|
|
|
|
if (type == 0x06 || type == 0x07) {
|
|
|
|
musx->loop_start = offset2;
|
|
|
|
musx->loop_end = offset1;
|
|
|
|
musx->loop_flag = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 00:20:04 +01:00
|
|
|
else if (musx->loops_offset && read_u32be(musx->loops_offset, sf) != 0xABABABAB) {
|
2019-08-03 23:35:10 +02:00
|
|
|
/* parse loop table (loop starts are -1 if non-looping)
|
|
|
|
* 0x00: version?
|
|
|
|
* 0x04: flags? (&1=loops)
|
|
|
|
* 0x08: loop start offset?
|
|
|
|
* 0x0c: loop end offset?
|
|
|
|
* 0x10: loop end sample
|
|
|
|
* 0x14: loop start sample
|
|
|
|
* 0x18: loop end offset
|
|
|
|
* 0x1c: loop start offset */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->loop_end_sample = read_s32le(musx->loops_offset+0x10, sf);
|
|
|
|
musx->loop_start_sample = read_s32le(musx->loops_offset+0x14, sf);
|
|
|
|
musx->loop_end = read_s32le(musx->loops_offset+0x18, sf);
|
|
|
|
musx->loop_start = read_s32le(musx->loops_offset+0x1c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
musx->num_samples = musx->loop_end_sample; /* preferable even if not looping as some files have padding */
|
|
|
|
musx->loop_flag = (musx->loop_start_sample >= 0);
|
|
|
|
}
|
|
|
|
|
2019-12-05 23:17:40 +01:00
|
|
|
/* fix some v10 platform (like PSP) sizes */
|
2019-08-03 23:35:10 +02:00
|
|
|
if (musx->stream_size == 0) {
|
|
|
|
musx->stream_size = musx->file_size - musx->stream_offset;
|
|
|
|
|
2019-12-05 23:17:40 +01:00
|
|
|
/* always padded to nearest 0x800 sector */
|
|
|
|
if (musx->stream_size > 0x800) {
|
|
|
|
uint8_t buf[0x800];
|
|
|
|
int pos;
|
|
|
|
off_t offset = musx->stream_offset + musx->stream_size - 0x800;
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (read_streamfile(buf, offset, sizeof(buf), sf) != 0x800)
|
2019-12-05 23:17:40 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
pos = 0x800 - 0x04;
|
|
|
|
while (pos > 0) {
|
|
|
|
if (get_u32be(buf + pos) != 0xABABABAB)
|
|
|
|
break;
|
|
|
|
musx->stream_size -= 0x04;
|
|
|
|
pos -= 0x04;
|
|
|
|
}
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
|
|
|
}
|
2008-11-08 20:03:28 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
return 1;
|
2008-11-08 20:03:28 +01:00
|
|
|
fail:
|
2019-08-03 23:35:10 +02:00
|
|
|
return 0;
|
2008-11-08 20:03:28 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
static int parse_musx(STREAMFILE* sf, musx_header* musx) {
|
|
|
|
int32_t (*read_s32)(off_t,STREAMFILE*) = NULL;
|
|
|
|
uint32_t (*read_u32)(off_t,STREAMFILE*) = NULL;
|
|
|
|
uint16_t (*read_u16)(off_t,STREAMFILE*) = NULL;
|
|
|
|
int target_subsong = sf->stream_index;
|
2008-11-08 20:03:28 +01:00
|
|
|
|
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* base header is LE */
|
2021-01-08 00:20:04 +01:00
|
|
|
/* 0x04: file ID (referenced in external .h, sometimes files are named [prefix](id)[suffix].sfx too) */
|
|
|
|
musx->version = read_u32le(0x08,sf);
|
|
|
|
musx->file_size = read_u32le(0x0c,sf);
|
2018-03-10 18:51:57 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
switch(musx->version) {
|
|
|
|
case 201: /* Sphinx and the Cursed Mummy (PS2), Buffy the Vampire Slayer: Chaos Bleeds (PS2/GC/Xbox) */
|
|
|
|
case 1: /* Athens 2004 (PS2) */
|
|
|
|
musx->platform = 0; /* guess later */
|
|
|
|
musx->tables_offset = 0x10;
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->big_endian = guess_endianness32bit(0x10, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
musx->is_old = 1;
|
2009-12-16 07:12:53 +01:00
|
|
|
break;
|
2008-07-17 16:20:42 +02:00
|
|
|
|
2019-11-24 21:45:36 +01:00
|
|
|
case 4: /* Spyro: A Hero's Tail (PS2/Xbox/GC), Athens 2004 (PC) */
|
2019-08-03 23:35:10 +02:00
|
|
|
case 5: /* Predator: Concrete Jungle (PS2/Xbox), Robots (GC) */
|
2019-11-24 21:45:36 +01:00
|
|
|
case 6: /* Batman Begins (GC), Ice Age 2 (PS2/PC) */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->platform = read_u32be(0x10,sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
/* 0x14: some id/hash? */
|
|
|
|
/* 0x18: platform number? */
|
|
|
|
/* 0x1c: null */
|
|
|
|
musx->tables_offset = 0x20;
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->big_endian = (musx->platform == get_id32be("GC__"));
|
2010-08-15 18:25:37 +02:00
|
|
|
break;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
case 10: /* 007: Quantum of Solace (PS2), Pirates of the Caribbean: At World's End (PSP), GoldenEye 007 (Wii), Rio (PS3) */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->platform = read_u32be(0x10,sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
/* 0x14: null */
|
|
|
|
/* 0x18: platform number? */
|
|
|
|
/* 0x1c: null */
|
|
|
|
/* 0x20: hash */
|
|
|
|
musx->tables_offset = 0; /* no tables */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->big_endian = (
|
|
|
|
musx->platform == get_id32be("GC__") ||
|
|
|
|
musx->platform == get_id32be("XE__") ||
|
|
|
|
musx->platform == get_id32be("PS3_") ||
|
|
|
|
musx->platform == get_id32be("WII_")
|
|
|
|
);
|
2009-12-16 07:12:53 +01:00
|
|
|
break;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
2009-12-16 07:12:53 +01:00
|
|
|
default:
|
2021-01-08 00:20:04 +01:00
|
|
|
VGM_LOG("MUSX: unknown version\n");
|
2009-12-16 07:12:53 +01:00
|
|
|
goto fail;
|
2018-03-10 18:51:57 +01:00
|
|
|
}
|
2008-07-17 16:20:42 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
if (musx->big_endian) {
|
2021-01-08 00:20:04 +01:00
|
|
|
read_s32 = read_s32be;
|
|
|
|
read_u32 = read_u32be;
|
|
|
|
read_u16 = read_u16be;
|
2019-08-03 23:35:10 +02:00
|
|
|
} else {
|
2021-01-08 00:20:04 +01:00
|
|
|
read_s32 = read_s32le;
|
|
|
|
read_u32 = read_u32le;
|
|
|
|
read_u16 = read_u16le;
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
|
|
|
|
2008-07-17 03:22:23 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* MUSX has multiple forms which seem external/implicit info so we need some crude autodetection */
|
|
|
|
if (musx->tables_offset) {
|
|
|
|
off_t table1_offset, table2_offset /*, table3_offset, table4_offset*/;
|
|
|
|
size_t /*table1_size, table2_size, table3_size,*/ table4_size;
|
2008-07-17 03:22:23 +02:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
table1_offset = read_u32(musx->tables_offset+0x00, sf);
|
|
|
|
//table1_size = read_u32(musx->tables_offset+0x04, sf);
|
|
|
|
table2_offset = read_u32(musx->tables_offset+0x08, sf);
|
|
|
|
//table2_size = read_u32(musx->tables_offset+0x0c, sf);
|
|
|
|
//table3_offset = read_u32(musx->tables_offset+0x10, sf);
|
|
|
|
//table3_size = read_u32(musx->tables_offset+0x14, sf);
|
|
|
|
//table4_offset = read_u32(musx->tables_offset+0x18, sf);
|
|
|
|
table4_size = read_u32(musx->tables_offset+0x1c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
if (table2_offset == 0 || table2_offset == 0xABABABAB) {
|
|
|
|
/* possibly a collection of IDs */
|
2021-01-08 00:20:04 +01:00
|
|
|
VGM_LOG("MUSX: unsupported ID type\n");
|
2019-08-03 23:35:10 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else if (table4_size != 0 && table4_size != 0xABABABAB) {
|
|
|
|
/* sfx banks have table1=id table? table2=header table, table=DSP coefs table (optional), table4=data */
|
|
|
|
musx->form = SFX_BANK;
|
|
|
|
}
|
2021-01-08 00:20:04 +01:00
|
|
|
else if (read_u32(table1_offset+0x00, sf) < 0x9 &&
|
|
|
|
read_u32(table1_offset+0x08, sf) == 0x14 &&
|
|
|
|
read_u32(table1_offset+0x10, sf) <= 100) {
|
2019-08-03 23:35:10 +02:00
|
|
|
/* streams have a info table with a certain format */
|
|
|
|
musx->form = MFX;
|
|
|
|
}
|
2021-01-08 00:20:04 +01:00
|
|
|
else if (read_u32(table1_offset+0x00, sf) == 0 &&
|
|
|
|
read_u32(table1_offset+0x04, sf) > read_u32(table1_offset+0x00, sf) &&
|
|
|
|
read_u32(table1_offset+0x08, sf) > read_u32(table1_offset+0x04, sf)) {
|
2019-08-03 23:35:10 +02:00
|
|
|
/* a list of offsets starting from 0, each stream then has info table at offset */
|
|
|
|
musx->form = MFX_BANK;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-08 00:20:04 +01:00
|
|
|
VGM_LOG("MUSX: unsupported unknown type\n");
|
2019-08-03 23:35:10 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2021-01-08 00:20:04 +01:00
|
|
|
if (is_id32be(0x800,sf, "SBNK")) {
|
|
|
|
/* SFX_BANK-like sound bank found on v10 Wii/PC games [Dead Space Extraction (Wii), G-Force (PC)] */
|
2019-08-03 23:35:10 +02:00
|
|
|
musx->form = SBNK;
|
|
|
|
}
|
2021-01-08 00:20:04 +01:00
|
|
|
else if (is_id32be(0x800,sf, "FORM")) {
|
2019-08-03 23:35:10 +02:00
|
|
|
/* RIFF-like sound bank found on v10 PSP games */
|
|
|
|
musx->form = FORM;
|
|
|
|
}
|
2021-01-08 00:20:04 +01:00
|
|
|
else if (is_id32be(0x800,sf, "ESPD")) {
|
2019-08-03 23:35:10 +02:00
|
|
|
/* projectdetails.sfx */
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-08 00:20:04 +01:00
|
|
|
/* simplest music type*/
|
2019-08-03 23:35:10 +02:00
|
|
|
musx->form = MFX;
|
2008-07-17 03:22:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
/* parse known forms */
|
|
|
|
switch(musx->form) {
|
|
|
|
case MFX:
|
2010-05-02 21:16:35 +02:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
if (musx->tables_offset) {
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->loops_offset = read_u32(musx->tables_offset+0x00, sf);
|
2008-11-09 12:37:21 +01:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->stream_offset = read_u32(musx->tables_offset+0x08, sf);
|
|
|
|
musx->stream_size = read_u32(musx->tables_offset+0x0c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-01-08 00:20:04 +01:00
|
|
|
if (read_u32be(0x30, sf) != 0xABABABAB) {
|
|
|
|
uint32_t miniheader = read_u32be(0x40, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
switch(miniheader) {
|
|
|
|
case 0x44415434: /* "DAT4" */
|
2019-12-03 20:20:03 +01:00
|
|
|
case 0x44415435: /* "DAT5" */
|
2019-08-03 23:35:10 +02:00
|
|
|
case 0x44415438: /* "DAT8" */
|
|
|
|
/* found on PS3/Wii (but not always?) */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->stream_size = read_u32le(0x44, sf);
|
|
|
|
musx->channels = read_u32le(0x48, sf);
|
|
|
|
musx->sample_rate = read_u32le(0x4c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
musx->loops_offset = 0x50;
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-08 00:20:04 +01:00
|
|
|
if (read_u32be(0x30, sf) == 0x00 &&
|
|
|
|
read_u32be(0x34, sf) == 0x00) {
|
2019-12-03 20:20:03 +01:00
|
|
|
/* no subheader [Spider-Man 4 (Wii)] */
|
|
|
|
musx->loops_offset = 0x00;
|
|
|
|
} else {
|
|
|
|
/* loop info only [G-Force (PS3)] */
|
|
|
|
musx->loops_offset = 0x30;
|
|
|
|
}
|
2019-08-03 23:35:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
musx->stream_offset = 0x800;
|
|
|
|
musx->stream_size = 0; /* read later */
|
|
|
|
}
|
2018-03-10 18:51:57 +01:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!parse_musx_stream(sf, musx))
|
2019-08-03 23:35:10 +02:00
|
|
|
goto fail;
|
|
|
|
break;
|
2008-11-09 12:37:21 +01:00
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case MFX_BANK: {
|
|
|
|
off_t target_offset, base_offset, data_offset;
|
2008-11-09 12:37:21 +01:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->total_subsongs = read_u32(musx->tables_offset+0x04, sf) / 0x04; /* size */
|
2019-08-03 23:35:10 +02:00
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong > musx->total_subsongs || musx->total_subsongs <= 0) goto fail;
|
2018-03-10 18:51:57 +01:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
base_offset = read_u32(musx->tables_offset+0x00, sf);
|
|
|
|
data_offset = read_u32(musx->tables_offset+0x08, sf);
|
2018-03-10 18:51:57 +01:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
target_offset = read_u32(base_offset + (target_subsong - 1)*0x04, sf) + data_offset;
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
/* 0x00: id? */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->stream_offset = read_u32(target_offset + 0x04, sf) + data_offset;
|
|
|
|
musx->stream_size = read_u32(target_offset + 0x08, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
musx->loops_offset = target_offset + 0x0c;
|
|
|
|
|
|
|
|
/* this looks correct for PS2 and Xbox, not sure about GC */
|
|
|
|
musx->channels = 1;
|
|
|
|
musx->sample_rate = 22050;
|
2008-11-09 12:37:21 +01:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!parse_musx_stream(sf, musx))
|
2019-08-03 23:35:10 +02:00
|
|
|
goto fail;
|
|
|
|
break;
|
2018-03-10 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
case SFX_BANK: {
|
|
|
|
off_t target_offset, head_offset, coef_offset, data_offset;
|
|
|
|
size_t coef_size;
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
//unk_offset = read_u32(musx->tables_offset+0x00, sf); /* ids and cue-like config? */
|
|
|
|
head_offset = read_u32(musx->tables_offset+0x08, sf);
|
|
|
|
coef_offset = read_u32(musx->tables_offset+0x10, sf);
|
|
|
|
coef_size = read_u32(musx->tables_offset+0x14, sf);
|
|
|
|
data_offset = read_u32(musx->tables_offset+0x18, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->total_subsongs = read_u32(head_offset+0x00, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong > musx->total_subsongs || musx->total_subsongs <= 0) goto fail;
|
|
|
|
|
|
|
|
|
|
|
|
if (musx->is_old) {
|
|
|
|
target_offset = head_offset + 0x04 + (target_subsong - 1)*0x28;
|
|
|
|
|
|
|
|
/* 0x00: flag? */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->stream_offset = read_u32(target_offset + 0x04, sf) + data_offset;
|
|
|
|
musx->stream_size = read_u32(target_offset + 0x08, sf);
|
|
|
|
musx->sample_rate = read_u32(target_offset + 0x0c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
/* 0x10: size? */
|
|
|
|
/* 0x14: channels? */
|
|
|
|
/* 0x18: always 4? */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->coefs_offset = read_u32(target_offset + 0x1c, sf) + coef_offset; /* may be set even for non-GC */
|
2019-08-03 23:35:10 +02:00
|
|
|
/* 0x20: null? */
|
|
|
|
/* 0x24: sub-id? */
|
|
|
|
musx->channels = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
target_offset = head_offset + 0x04 + (target_subsong - 1)*0x20;
|
|
|
|
|
|
|
|
/* 0x00: flag? */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->stream_offset = read_u32(target_offset + 0x04, sf) + data_offset;
|
|
|
|
musx->stream_size = read_u32(target_offset + 0x08, sf);
|
|
|
|
musx->sample_rate = read_u32(target_offset + 0x0c, sf);
|
2019-08-03 23:35:10 +02:00
|
|
|
/* 0x10: size? */
|
2021-01-08 00:20:04 +01:00
|
|
|
musx->coefs_offset = read_u32(target_offset + 0x14, sf) + coef_offset; /* may be set even for non-GC */
|
2019-08-03 23:35:10 +02:00
|
|
|
/* 0x18: null? */
|
|
|
|
/* 0x1c: sub-id? */
|
|
|
|
musx->channels = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (coef_size == 0)
|
|
|
|
musx->coefs_offset = 0; /* disable for later detection */
|
|
|
|
|
2021-01-08 00:20:04 +01:00
|
|
|
if (!parse_musx_stream(sf, musx))
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SBNK: {
|
|
|
|
off_t target_offset, head_offset, data_offset;
|
|
|
|
uint8_t codec;
|
|
|
|
|
|
|
|
/* 0x00: id */
|
|
|
|
/* 0x04: always 0x12? */
|
|
|
|
/* 0x08: file ID (same as base file) */
|
|
|
|
/* 0x0c: some ID? */
|
|
|
|
|
|
|
|
/* 0x10: table1 count */
|
|
|
|
/* 0x14: table1 offset (from here) */
|
|
|
|
/* 0x18: table2 count */
|
|
|
|
/* 0x1c: table2 offset (from here) */
|
|
|
|
|
|
|
|
/* 0x20: table3 count */
|
|
|
|
/* 0x24: table3 offset (from here) */
|
|
|
|
/* 0x28: table4 count */
|
|
|
|
/* 0x2c: table4 offset (from here) */
|
|
|
|
|
|
|
|
/* 0x30: table5 count (waves) */
|
|
|
|
/* 0x34: table5 offset (from here) */
|
|
|
|
/* 0x38: table6 count */
|
|
|
|
/* 0x3c: table6 offset (from here) */
|
|
|
|
|
|
|
|
/* 0x40: table7 count */
|
|
|
|
/* 0x44: table7 offset (from here) */
|
|
|
|
/* 0x48: data size */
|
|
|
|
/* 0x4c: data offset (absolute) */
|
|
|
|
|
|
|
|
musx->tables_offset = 0x800;
|
|
|
|
if (!is_id32be(musx->tables_offset+0x00, sf, "SBNK"))
|
2019-08-03 23:35:10 +02:00
|
|
|
goto fail;
|
2021-01-08 00:20:04 +01:00
|
|
|
|
|
|
|
musx->total_subsongs = read_u32(musx->tables_offset+0x30, sf);
|
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong > musx->total_subsongs || musx->total_subsongs <= 0) goto fail;
|
|
|
|
|
|
|
|
head_offset = read_u32(musx->tables_offset+0x34, sf) + musx->tables_offset + 0x34;
|
|
|
|
data_offset = read_u32(musx->tables_offset+0x4c, sf);
|
|
|
|
|
|
|
|
target_offset = head_offset + (target_subsong - 1)*0x1c;
|
|
|
|
;VGM_LOG("MUSX: ho=%lx, do=%lx, to=%lx\n", head_offset, data_offset, target_offset);
|
|
|
|
|
|
|
|
/* 0x00: subfile ID */
|
|
|
|
musx->num_samples = read_s32(target_offset + 0x04, sf);
|
|
|
|
musx->loop_start_sample = read_s32(target_offset + 0x08, sf); /* -1 if no loop */
|
|
|
|
musx->sample_rate = read_u16(target_offset + 0x0c, sf);
|
|
|
|
codec = read_u8 (target_offset + 0x0e, sf);
|
|
|
|
musx->channels = read_u8 (target_offset + 0x0f, sf);
|
|
|
|
musx->stream_offset = read_u32(target_offset + 0x10, sf) + data_offset;
|
|
|
|
musx->stream_size = read_u32(target_offset + 0x14, sf);
|
|
|
|
musx->loop_start = read_s32(target_offset + 0x18, sf);
|
|
|
|
|
|
|
|
musx->loop_end_sample = musx->num_samples;
|
|
|
|
musx->loop_flag = (musx->loop_start_sample >= 0);
|
|
|
|
|
|
|
|
switch(codec) {
|
|
|
|
case 0x11: musx->codec = DAT; break;
|
|
|
|
case 0x13: musx->codec = NGCA; break;
|
|
|
|
case 0x14: musx->codec = PCM; break;
|
|
|
|
default:
|
|
|
|
VGM_LOG("MUSX: unknown SBNK codec %x\n", codec);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
break;
|
2008-11-09 12:37:21 +01:00
|
|
|
}
|
2019-08-03 23:35:10 +02:00
|
|
|
|
|
|
|
default:
|
2021-01-08 00:20:04 +01:00
|
|
|
VGM_LOG("MUSX: unsupported form\n");
|
2019-08-03 23:35:10 +02:00
|
|
|
goto fail;
|
2008-11-09 12:37:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-03 23:35:10 +02:00
|
|
|
return 1;
|
2008-11-09 12:37:21 +01:00
|
|
|
fail:
|
2019-08-03 23:35:10 +02:00
|
|
|
return 0;
|
2008-11-09 12:37:21 +01:00
|
|
|
}
|