Add WayForward single/segmented .wave [Shantae and the Pirate's Curse]

This commit is contained in:
bnnm 2018-03-11 19:24:06 +01:00
parent 78f4d5eb7d
commit 3eeddcc41e
9 changed files with 359 additions and 1 deletions

View File

@ -358,6 +358,7 @@ static const char* extension_list[] = {
"wam",
"was",
//"wav", //common
"wave",
"wavm",
"wb",
"wem",
@ -965,6 +966,8 @@ static const meta_info meta_info_list[] = {
{meta_SQEX_MAB, "Square-Enix MAB header"},
{meta_OGG_L2SD, "Ogg Vorbis (L2SD)"},
{meta_WAF, "KID WAF header"},
{meta_WAVE, "WayForward .WAVE header"},
{meta_WAVE_segmented, "WayForward .WAVE header (segmented)"},
#ifdef VGM_USE_MP4V2
{meta_MP4, "AAC header"},

View File

@ -1282,6 +1282,14 @@
RelativePath=".\meta\waf.c"
>
</File>
<File
RelativePath=".\meta\wave_segmented.c"
>
</File>
<File
RelativePath=".\meta\wave.c"
>
</File>
<File
RelativePath=".\meta\wii_04sw.c"
>

View File

@ -400,6 +400,8 @@
<ClCompile Include="meta\vxn.c" />
<ClCompile Include="meta\waa_wac_wad_wam.c" />
<ClCompile Include="meta\waf.c" />
<ClCompile Include="meta\wave_segmented.c" />
<ClCompile Include="meta\wave.c" />
<ClCompile Include="meta\wii_04sw.c" />
<ClCompile Include="meta\wii_bns.c" />
<ClCompile Include="meta\wii_mus.c" />

View File

@ -784,6 +784,12 @@
<ClCompile Include="meta\waf.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\wave_segmented.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\wave.c">
<Filter>meta\Source Files</Filter>
</ClCompile>
<ClCompile Include="meta\wii_04sw.c">
<Filter>meta\Source Files</Filter>
</ClCompile>

View File

@ -706,4 +706,8 @@ VGMSTREAM * init_vgmstream_atx(STREAMFILE *streamFile);
VGMSTREAM * init_vgmstream_sqex_sead(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_waf(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_wave(STREAMFILE * streamFile);
VGMSTREAM * init_vgmstream_wave_segmented(STREAMFILE * streamFile);
#endif /*_META_H*/

108
src/meta/wave.c Normal file
View File

@ -0,0 +1,108 @@
#include "meta.h"
#include "../coding/coding.h"
/* .WAVE - WayForward "EngineBlack" games [Mighty Switch Force! (3DS), Adventure Time: Hey Ice King! Why'd You Steal Our Garbage?! (3DS)] */
VGMSTREAM * init_vgmstream_wave(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t start_offset, extradata_offset;
int loop_flag = 0, channel_count, sample_rate, codec;
int32_t num_samples, loop_start = 0, loop_end = 0;
size_t interleave;
int big_endian;
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
//int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL;
/* checks */
if (!check_extensions(streamFile, "wave"))
goto fail;
if (read_32bitLE(0x00,streamFile) != 0xE5B7ECFE && /* header id */
read_32bitBE(0x00,streamFile) != 0xE5B7ECFE)
goto fail;
if (read_32bitBE(0x04,streamFile) != 0x00) /* version? */
goto fail;
/* assumed */
big_endian = read_32bitBE(0x00,streamFile) == 0xE5B7ECFE;
if (big_endian) {
read_32bit = read_32bitBE;
//read_16bit = read_16bitBE;
} else {
read_32bit = read_32bitLE;
//read_16bit = read_16bitLE;
}
channel_count = read_8bit(0x05,streamFile);
if (read_32bit(0x08,streamFile) != get_streamfile_size(streamFile))
goto fail;
if (read_8bit(0x0c,streamFile) != 0x00) /* ? */
goto fail;
/* sample rate in 32b float (WHY?)*/
{
uint32_t sample_int = (uint32_t)read_32bit(0x0c, streamFile);
float* sample_float;
sample_float = (float*)&sample_int;
sample_rate = (int)(*sample_float);
}
num_samples = read_32bit(0x10, streamFile);
loop_start = read_32bit(0x14, streamFile);
loop_end = read_32bit(0x18, streamFile);
codec = read_8bit(0x1c, streamFile);
channel_count = read_8bit(0x1d, streamFile);
if (read_8bit(0x1e, streamFile) != 0x00) goto fail; /* unknown */
if (read_8bit(0x1f, streamFile) != 0x00) goto fail; /* unknown */
start_offset = read_32bit(0x20, streamFile);
interleave = read_32bit(0x24, streamFile); /* typically half data_size */
extradata_offset = read_32bit(0x28, streamFile); /* OR: extradata size (0x2c) */
loop_flag = (loop_start > 0);
/* some songs (ex. Adventure Time's m_candykingdom_overworld.wave) do full loops, but there is no way
* to tell them apart from sfx/voices, so we try to detect if it's long enough. */
if(!loop_flag
&& loop_start == 0 && loop_end == num_samples /* full loop */
&& channel_count > 1
&& num_samples > 20*sample_rate) { /* in seconds */
loop_flag = 1;
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = sample_rate;
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = loop_start;
vgmstream->loop_end_sample = loop_end;
vgmstream->meta_type = meta_WAVE;
/* not sure if there are other codecs but anyway */
switch(codec) {
case 0x02:
vgmstream->coding_type = coding_NGC_DSP;
vgmstream->layout_type = layout_interleave;
vgmstream->interleave_block_size = interleave;
/* ADPCM setup: 0x20 coefs + 0x06 initial ps/hist1/hist2 + 0x06 loop ps/hist1/hist2, per channel */
dsp_read_coefs(vgmstream, streamFile, extradata_offset+0x00, 0x2c, big_endian);
dsp_read_hist(vgmstream, streamFile, extradata_offset+0x22, 0x2c, big_endian);
break;
default:
goto fail;
}
if (!vgmstream_open_stream(vgmstream,streamFile,start_offset))
goto fail;
return vgmstream;
fail:
close_vgmstream(vgmstream);
return NULL;
}

223
src/meta/wave_segmented.c Normal file
View File

@ -0,0 +1,223 @@
#include "meta.h"
#include "../coding/coding.h"
#include "../layout/layout.h"
#define MAX_SEGMENTS 4
/* .WAVE - WayForward "EngineBlack" games, segmented [Shantae and the Pirate's Curse (PC/3DS), TMNT: Danger of the Ooze (PS3/3DS)] */
VGMSTREAM * init_vgmstream_wave_segmented(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
off_t segments_offset;
int loop_flag = 0, channel_count, sample_rate;
int32_t num_samples, loop_start_sample = 0, loop_end_sample = 0;
segmented_layout_data *data = NULL;
int segment_count, loop_start_segment = 0, loop_end_segment = 0;
int big_endian;
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL;
/* checks */
if (!check_extensions(streamFile, "wave"))
goto fail;
if (read_32bitLE(0x00,streamFile) != 0x4DF72D4A && /* header id */
read_32bitBE(0x00,streamFile) != 0x4DF72D4A)
goto fail;
if (read_8bit(0x04,streamFile) != 0x01) /* version? */
goto fail;
/* PS3/X360 games */
big_endian = read_32bitBE(0x00,streamFile) == 0x4DF72D4A;
if (big_endian) {
read_32bit = read_32bitBE;
read_16bit = read_16bitBE;
} else {
read_32bit = read_32bitLE;
read_16bit = read_16bitLE;
}
channel_count = read_8bit(0x05,streamFile);
segment_count = read_16bit(0x06,streamFile);
if (segment_count > MAX_SEGMENTS || segment_count <= 0) goto fail;
loop_start_segment = read_16bit(0x08, streamFile);
loop_end_segment = read_16bit(0x0a, streamFile);
segments_offset = read_32bit(0x0c, streamFile);
sample_rate = read_32bit(0x10, streamFile);
num_samples = read_32bit(0x14, streamFile);
/* 0x18: unknown (usually 0, maybe some count) */
/* init layout */
data = init_layout_segmented(segment_count);
if (!data) goto fail;
/* parse segments (usually: preload + intro + loop + ending, intro/ending may be skipped)
* Often first segment is ADPCM and rest Ogg; may only have one segment. */
{
off_t extradata_offset, table_offset, segment_offset;
size_t segment_size;
int32_t segment_samples;
int codec;
int i, ch;
/* open each segment subfile */
for (i = 0; i < segment_count; i++) {
codec = read_8bit(segments_offset+0x10*i+0x00, streamFile);
/* 0x01(1): unknown (flag? usually 0x00/0x01/0x02) */
if (read_8bit(segments_offset+0x10*i+0x02, streamFile) != 0x01) goto fail; /* unknown */
if (read_8bit(segments_offset+0x10*i+0x03, streamFile) != 0x00) goto fail; /* unknown */
segment_samples = read_32bit(segments_offset+0x10*i+0x04, streamFile);
extradata_offset = read_32bit(segments_offset+0x10*i+0x08, streamFile);
table_offset = read_32bit(segments_offset+0x10*i+0x0c, streamFile);
/* create a sub-VGMSTREAM per segment
* (we'll reopen this streamFile as needed, so each sub-VGMSTREAM is fully independent) */
switch(codec) {
case 0x02: { /* "adpcm" */
data->segments[i] = allocate_vgmstream(channel_count, 0);
if (!data->segments[i]) goto fail;
data->segments[i]->sample_rate = sample_rate;
data->segments[i]->meta_type = meta_WAVE;
data->segments[i]->coding_type = coding_IMA_int;
data->segments[i]->layout_type = layout_none;
data->segments[i]->num_samples = segment_samples;
if (!vgmstream_open_stream(data->segments[i],streamFile,0x00))
goto fail;
/* bizarrely enough channel data isn't sequential (segment0 ch1+ may go after all other segments) */
for (ch = 0; ch < channel_count; ch++) {
segment_offset = read_32bit(table_offset + 0x04*ch, streamFile);
data->segments[i]->ch[ch].channel_start_offset =
data->segments[i]->ch[ch].offset = segment_offset;
/* ADPCM setup */
data->segments[i]->ch[ch].adpcm_history1_32 = read_16bit(extradata_offset+0x04*ch+0x00, streamFile);
data->segments[i]->ch[ch].adpcm_step_index = read_8bit(extradata_offset+0x04*ch+0x02, streamFile);
/* 0x03: reserved */
}
break;
}
case 0x03: { /* "dsp-adpcm" */
data->segments[i] = allocate_vgmstream(channel_count, 0);
if (!data->segments[i]) goto fail;
data->segments[i]->sample_rate = sample_rate;
data->segments[i]->meta_type = meta_WAVE;
data->segments[i]->coding_type = coding_NGC_DSP;
data->segments[i]->layout_type = layout_none;
data->segments[i]->num_samples = segment_samples;
if (!vgmstream_open_stream(data->segments[i],streamFile,0x00))
goto fail;
/* bizarrely enough channel data isn't sequential (segment0 ch1+ may go after all other segments) */
for (ch = 0; ch < channel_count; ch++) {
segment_offset = read_32bit(table_offset + 0x04*ch, streamFile);
data->segments[i]->ch[ch].channel_start_offset =
data->segments[i]->ch[ch].offset = segment_offset;
}
/* ADPCM setup: 0x06 initial ps/hist1/hist2 (per channel) + 0x20 coefs (per channel) */
dsp_read_hist(data->segments[i], streamFile, extradata_offset+0x02, 0x06, big_endian);
dsp_read_coefs(data->segments[i], streamFile, extradata_offset+0x06*channel_count+0x00, 0x20, big_endian);
break;
}
case 0x04: { /* "vorbis" */
char filename[PATH_LIMIT];
vgm_vorbis_info_t ovi = {0};
segment_offset = read_32bit(table_offset, streamFile);
segment_size = read_32bitBE(segment_offset, streamFile); /* always BE */
ovi.layout_type = layout_ogg_vorbis;
ovi.meta_type = meta_WAVE;
ovi.stream_size = segment_size;
streamFile->get_name(streamFile,filename,sizeof(filename));
data->segments[i] = init_vgmstream_ogg_vorbis_callbacks(streamFile, filename, NULL, segment_offset+0x04, &ovi);
if (!data->segments[i]) goto fail;
if (data->segments[i]->num_samples != segment_samples) {
VGM_LOG("WAVE: segment %i samples != num_samples\n", i);
goto fail;
}
break;
}
default: /* others: s16be/s16le/mp3 as referenced in the exe? */
VGM_LOG("WAVE: unknown codec\n");
goto fail;
}
//todo better memcpy (call setup_layout_segmented + validate segments, disable looping, etc?)
/* save start things so we can restart for seeking/looping */
memcpy(data->segments[i]->start_ch,data->segments[i]->ch,sizeof(VGMSTREAMCHANNEL)*data->segments[i]->channels);
memcpy(data->segments[i]->start_vgmstream,data->segments[i],sizeof(VGMSTREAM));
}
}
/* parse samples */
{
int32_t sample_count = 0;
int i;
loop_flag = (loop_start_segment > 0);
for (i = 0; i < segment_count; i++) {
if (loop_flag && loop_start_segment == i) {
loop_start_sample = sample_count;
}
sample_count += data->segments[i]->num_samples;
if (loop_flag && loop_end_segment-1 == i) {
loop_end_sample = sample_count;
}
}
if (sample_count != num_samples) {
VGM_LOG("WAVE: total segments samples %i != num_samples %i\n", sample_count, num_samples);
goto fail;
}
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count, loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = sample_rate;
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = loop_start_sample;
vgmstream->loop_end_sample = loop_end_sample;
vgmstream->meta_type = meta_WAVE_segmented;
/* .wave can mix codecs, usually first segment is a small ADPCM section) */
vgmstream->coding_type = (segment_count == 1 ? data->segments[0]->coding_type : data->segments[1]->coding_type);
vgmstream->layout_type = layout_segmented;
vgmstream->layout_data = data;
if (loop_flag)
data->loop_segment = (loop_start_segment);
return vgmstream;
fail:
free_layout_segmented(data);
close_vgmstream(vgmstream);
return NULL;
}

View File

@ -380,6 +380,8 @@ VGMSTREAM * (*init_vgmstream_functions[])(STREAMFILE *streamFile) = {
init_vgmstream_atx,
init_vgmstream_sqex_sead,
init_vgmstream_waf,
init_vgmstream_wave,
init_vgmstream_wave_segmented,
init_vgmstream_txth, /* should go at the end (lower priority) */
#ifdef VGM_USE_FFMPEG

View File

@ -662,6 +662,8 @@ typedef enum {
meta_SQEX_MAB, /* Square-Enix newest middleware (music) */
meta_OGG_L2SD, /* Ogg Vorbis with obfuscation [Lineage II Chronicle 4 (PC)] */
meta_WAF, /* KID WAF [Ever 17 (PC)] */
meta_WAVE, /* WayForward "EngineBlack" games [Mighty Switch Force! (3DS)] */
meta_WAVE_segmented, /* WayForward "EngineBlack" games, segmented [Shantae and the Pirate's Curse (PC)] */
#ifdef VGM_USE_MP4V2
meta_MP4, /* AAC (iOS) */