Fix some MUSX .sfx [Sphinx (PC)]

This commit is contained in:
bnnm 2022-02-19 12:36:33 +01:00
parent 57bd304b64
commit 34112d8dbb
3 changed files with 46 additions and 5 deletions

View File

@ -48,7 +48,7 @@ size_t ms_ima_bytes_to_samples(size_t bytes, int block_align, int channels);
size_t xbox_ima_bytes_to_samples(size_t bytes, int channels);
size_t dat4_ima_bytes_to_samples(size_t bytes, int channels);
size_t apple_ima4_bytes_to_samples(size_t bytes, int channels);
int xbox_check_format(STREAMFILE* sf, uint32_t offset, uint32_t max, int channels);
/* ngc_dsp_decoder */
void decode_ngc_dsp(VGMSTREAMCHANNEL* stream, sample_t* outbuf, int channelspacing, int32_t first_sample, int32_t samples_to_do);

View File

@ -1326,3 +1326,27 @@ size_t apple_ima4_bytes_to_samples(size_t bytes, int channels) {
return (bytes / block_align) * (block_align - 0x02*channels) * 2 / channels
+ ((bytes % block_align) ? ((bytes % block_align) - 0x02*channels) * 2 / channels : 0);
}
/* test XBOX-ADPCM frames for correctness */
int xbox_check_format(STREAMFILE* sf, uint32_t offset, uint32_t max, int channels) {
off_t max_offset = offset + max;
int ch;
if (max_offset > get_streamfile_size(sf))
max_offset = get_streamfile_size(sf);
if (!channels)
return 0;
while (offset < max_offset) {
for (ch = 0; ch < channels; ch++) {
uint16_t step = read_u16le(offset + 0x04 * ch + 0x02,sf);
if (step > 88)
return 0;
}
offset += 0x24 * channels;
}
return 1;
}

View File

@ -1,5 +1,6 @@
#include "meta.h"
#include "../coding/coding.h"
#include "../util/endianness.h"
typedef enum { MFX, MFX_BANK, SFX_BANK, SBNK, FORM } musx_form;
typedef enum { PSX, DSP, XBOX, IMA, DAT, NGCA, PCM } musx_codec;
@ -187,16 +188,24 @@ static int parse_musx_stream(STREAMFILE* sf, musx_header* musx) {
musx->platform = get_id32be("GC02"); /* (fake) */
}
else {
int channels = musx->channels;
off_t offset = musx->stream_offset;
size_t max = 0x5000;
if (max > musx->stream_size)
max = musx->stream_size;
if (!channels)
channels = 2;
/* since engine seems to hardcode codecs no apparent way to detect in some cases
* [Sphinx and the Cursed Mummy (multi), Buffy the Vampire Slayer: Chaos Bleeds (multi)] */
if (ps_check_format(sf, offset, max)) {
musx->platform = get_id32be("PS2_");
} else {
} else if (xbox_check_format(sf, offset, max, channels)) {
musx->platform = get_id32be("XB02"); /* (fake) */
}
else {
musx->platform = get_id32be("PC02"); /* (fake) */
}
}
}
@ -267,6 +276,12 @@ static int parse_musx_stream(STREAMFILE* sf, musx_header* musx) {
musx->codec = DAT;
break;
case 0x50433032: /* "PC02" */
default_channels = 2;
default_sample_rate = 32000;
musx->codec = IMA;
break;
default:
VGM_LOG("MUSX: unknown platform %x\n", musx->platform);
goto fail;
@ -307,7 +322,7 @@ static int parse_musx_stream(STREAMFILE* sf, musx_header* musx) {
}
/* other types (0x0a, 0x09) look like section/end markers, 0x06/07 only seems to exist once */
if (type == 0x06 || type == 0x07) {
if (type == 0x06 || type == 0x07) { /* loop / goto */
musx->loop_start = offset2;
musx->loop_end = offset1;
musx->loop_flag = 1;
@ -361,6 +376,8 @@ fail:
return 0;
}
//TODO: check possible info here:
// https://sphinxandthecursedmummy.fandom.com/wiki/SFX
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;
@ -378,7 +395,7 @@ static int parse_musx(STREAMFILE* sf, musx_header* musx) {
case 1: /* Athens 2004 (PS2) */
musx->platform = 0; /* guess later */
musx->tables_offset = 0x10;
musx->big_endian = guess_endianness32bit(0x10, sf);
musx->big_endian = guess_endian32(0x10, sf);
musx->is_old = 1;
break;
@ -670,7 +687,7 @@ static int parse_musx(STREAMFILE* sf, musx_header* musx) {
data_offset = read_u32(musx->tables_offset+0x3c, 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);
//;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);