2019-12-16 20:31:03 +01:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../layout/layout.h"
|
|
|
|
#include "../coding/coding.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* SWVR - from EA games, demuxed from .av/trk/mis/etc [Future Cop L.A.P.D. (PS/PC), Freekstyle (PS2/GC), EA Sports Supercross (PS)] */
|
2021-05-31 22:25:48 +02:00
|
|
|
VGMSTREAM* init_vgmstream_ea_swvr(STREAMFILE* sf) {
|
2019-12-16 20:31:03 +01:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
|
|
|
off_t start_offset;
|
2021-05-31 22:25:48 +02:00
|
|
|
int loop_flag = 0, channels, sample_rate, big_endian;
|
2019-12-16 20:31:03 +01:00
|
|
|
coding_t coding;
|
|
|
|
uint32_t block_id;
|
|
|
|
int32_t (*read_32bit)(off_t,STREAMFILE*) = NULL;
|
|
|
|
int16_t (*read_16bit)(off_t,STREAMFILE*) = NULL;
|
2021-05-31 22:25:48 +02:00
|
|
|
int total_subsongs, target_subsong = sf->stream_index;
|
2019-12-16 20:31:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* checks */
|
2019-12-16 20:32:46 +01:00
|
|
|
/* .stream: common (found inside files)
|
|
|
|
* .str: shortened, probably unnecessary */
|
2021-05-31 22:25:48 +02:00
|
|
|
if (!check_extensions(sf,"stream,str"))
|
2019-12-16 20:31:03 +01:00
|
|
|
goto fail;
|
|
|
|
|
2021-05-31 22:25:48 +02:00
|
|
|
/* Files have no actual audio headers, so we inspect the first block for known values.
|
|
|
|
* Freekstyle uses multiblocks/subsongs (though some subsongs may be clones?) */
|
|
|
|
|
2019-12-16 20:31:03 +01:00
|
|
|
/* blocks ids are in machine endianness */
|
2021-05-31 22:25:48 +02:00
|
|
|
if (read_u32be(0x00,sf) == get_id32be("RVWS")) { /* PS1/PS2/PC */
|
2019-12-16 20:31:03 +01:00
|
|
|
big_endian = 0;
|
|
|
|
read_32bit = read_32bitLE;
|
|
|
|
read_16bit = read_16bitLE;
|
2021-05-31 22:25:48 +02:00
|
|
|
start_offset = read_32bit(0x04, sf);
|
2019-12-16 20:31:03 +01:00
|
|
|
}
|
2021-05-31 22:25:48 +02:00
|
|
|
else if (read_u32be(0x00,sf) == get_id32be("SWVR")) { /* GC */
|
2019-12-16 20:31:03 +01:00
|
|
|
big_endian = 1;
|
|
|
|
read_32bit = read_32bitBE;
|
|
|
|
read_16bit = read_16bitBE;
|
2021-05-31 22:25:48 +02:00
|
|
|
start_offset = read_32bit(0x04, sf);
|
2019-12-16 20:31:03 +01:00
|
|
|
}
|
2021-05-31 22:25:48 +02:00
|
|
|
else if (read_u32be(0x00,sf) == get_id32be("MGAV")) { /* Freekstyle (PS2) raw movies */
|
2019-12-16 20:31:03 +01:00
|
|
|
big_endian = 0;
|
|
|
|
read_32bit = read_32bitLE;
|
|
|
|
read_16bit = read_16bitLE;
|
|
|
|
start_offset = 0x00;
|
|
|
|
}
|
2021-05-31 22:25:48 +02:00
|
|
|
else if (read_u32be(0x00,sf) == get_id32be("DSPM")) { /* Freekstyle (GC) raw movies */
|
2019-12-16 20:31:03 +01:00
|
|
|
big_endian = 1;
|
|
|
|
read_32bit = read_32bitBE;
|
|
|
|
read_16bit = read_16bitBE;
|
|
|
|
start_offset = 0x00;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:25:48 +02:00
|
|
|
if (read_32bit(start_offset+0x00, sf) == get_id32be("PADD")) /* Freekstyle */
|
|
|
|
start_offset += read_32bit(start_offset+0x04, sf);
|
2019-12-16 20:31:03 +01:00
|
|
|
|
2021-05-31 22:25:48 +02:00
|
|
|
if (read_32bit(start_offset+0x00, sf) == get_id32be("FILL")) /* Freekstyle */
|
|
|
|
start_offset += read_32bit(start_offset+0x04, sf);
|
2019-12-16 20:31:03 +01:00
|
|
|
|
|
|
|
total_subsongs = 1;
|
2021-05-31 22:25:48 +02:00
|
|
|
block_id = read_32bit(start_offset, sf);
|
|
|
|
/* value after block id (usually at 0x38) is number of blocks of 0x6000 (results in file size, including FILLs) */
|
2019-12-16 20:31:03 +01:00
|
|
|
|
2021-05-31 22:25:48 +02:00
|
|
|
/* intended sample rate for PSX music (verified in emus) should be 14260, but is found in ELF as pitch value
|
|
|
|
* (ex. Nascar Rumble 0x052C in SLUS_010.68 at 0x000143BC): 0x052C * 44100 / 4096 ~= 14254.98046875hz
|
|
|
|
* Future Cop PSX pitch looks similar (comparing vs recordings). */
|
2019-12-16 20:31:03 +01:00
|
|
|
switch(block_id) {
|
2021-05-31 22:25:48 +02:00
|
|
|
case 0x5641474D: /* "VAGM" (stereo music) */
|
2019-12-16 20:31:03 +01:00
|
|
|
coding = coding_PSX;
|
2021-05-31 22:25:48 +02:00
|
|
|
if (read_16bit(start_offset+0x1a, sf) == 0x0024) {
|
|
|
|
total_subsongs = read_32bit(start_offset+0x0c, sf)+1;
|
|
|
|
sample_rate = 22050; /* Freekstyle (PS2) */
|
2019-12-16 20:31:03 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-05-31 22:25:48 +02:00
|
|
|
sample_rate = 1324 * 44100 / 4096; /* ~14254 [Future Cop (PS1), Nascar Rumble (PS1), EA Sports Motocross (PS1)] */
|
2019-12-16 20:31:03 +01:00
|
|
|
}
|
2021-05-31 22:25:48 +02:00
|
|
|
channels = 2;
|
2019-12-16 20:31:03 +01:00
|
|
|
break;
|
2021-05-31 22:25:48 +02:00
|
|
|
case 0x56414742: /* "VAGB" (mono sfx/voices)*/
|
2019-12-16 20:31:03 +01:00
|
|
|
coding = coding_PSX;
|
2021-05-31 22:25:48 +02:00
|
|
|
if (read_16bit(start_offset+0x1a, sf) == 0x6400) {
|
|
|
|
sample_rate = 22050; /* Freekstyle (PS2) */
|
2019-12-16 20:31:03 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-05-31 22:25:48 +02:00
|
|
|
/* approximate as pitches vary per file (ex. Nascar Rumble: engine=3779, heli=22050, commentary=11050) */
|
|
|
|
sample_rate = 1080 * 44100 / 4096; /* ~11627 [EA Sports Motocross (PS1)] */
|
2019-12-16 20:31:03 +01:00
|
|
|
}
|
2021-05-31 22:25:48 +02:00
|
|
|
channels = 1;
|
2019-12-16 20:31:03 +01:00
|
|
|
break;
|
2021-05-31 22:25:48 +02:00
|
|
|
case 0x4453504D: /* "DSPM" (stereo music) */
|
2019-12-16 20:31:03 +01:00
|
|
|
coding = coding_NGC_DSP;
|
2021-05-31 22:25:48 +02:00
|
|
|
total_subsongs = read_32bit(start_offset+0x0c, sf)+1;
|
|
|
|
sample_rate = 22050; /* Freekstyle (GC) */
|
|
|
|
channels = 2;
|
2019-12-16 20:31:03 +01:00
|
|
|
break;
|
2021-05-31 22:25:48 +02:00
|
|
|
case 0x44535042: /* "DSPB" (mono voices/sfx) */
|
2019-12-16 20:31:03 +01:00
|
|
|
coding = coding_NGC_DSP;
|
2021-05-31 22:25:48 +02:00
|
|
|
channels = 1;
|
|
|
|
sample_rate = 22050; /* Freekstyle (GC) */
|
2019-12-16 20:31:03 +01:00
|
|
|
break;
|
2021-05-31 22:25:48 +02:00
|
|
|
case 0x4D534943: /* "MSIC" (stereo music) */
|
2019-12-16 20:31:03 +01:00
|
|
|
coding = coding_PCM8_U_int;
|
2021-05-31 22:25:48 +02:00
|
|
|
channels = 2;
|
|
|
|
sample_rate = 14291; /* assumed, by comparing vs PSX output [Future Cop (PC)] */
|
2019-12-16 20:31:03 +01:00
|
|
|
break;
|
|
|
|
case 0x53484F43: /* "SHOC" (a generic block but hopefully has PC sounds) */
|
2021-05-31 22:25:48 +02:00
|
|
|
if (read_32bit(start_offset+0x10, sf) == get_id32be("SHDR")) { /* Future Cop (PC) */
|
|
|
|
/* there is a mini header? after SHDR
|
|
|
|
* 0x00: 5
|
|
|
|
* 0x04: "snds"
|
|
|
|
* 0x08: channels?
|
|
|
|
* 0x0c: data size without blocks/padding
|
|
|
|
* 0x10: null
|
|
|
|
* 0x14: null
|
|
|
|
* 0x18: null
|
|
|
|
* 0x1c: 1
|
|
|
|
* 0x20: 1
|
|
|
|
* 0x24: 0x4F430000 */
|
|
|
|
if (read_32bit(start_offset+0x18, sf) != get_id32be("snds"))
|
|
|
|
goto fail;
|
|
|
|
coding = coding_PCM8_U_int;
|
|
|
|
channels = 1;
|
2022-06-06 15:10:47 +02:00
|
|
|
sample_rate = 22050; /* assumed */
|
2019-12-27 01:48:29 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-05-31 22:25:48 +02:00
|
|
|
//todo
|
|
|
|
/* The Lord of the Rings - The Return of the King (PC) uses IMA for sfx (in non-RVWS chunks)
|
|
|
|
* and some unknown codec for streams (mixed with strings and cutscene(?) commands) */
|
2019-12-27 01:48:29 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-12-16 20:31:03 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VGM_LOG("EA SWVR: unknown block id\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
|
|
|
|
|
2021-05-31 22:25:48 +02:00
|
|
|
loop_flag = 0;//(channels > 1); /* some Future Cop LAPD tracks repeat but other games have fadeouts */
|
2019-12-16 20:31:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
2021-05-31 22:25:48 +02:00
|
|
|
vgmstream = allocate_vgmstream(channels, loop_flag);
|
2019-12-16 20:31:03 +01:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->meta_type = meta_EA_SWVR;
|
|
|
|
vgmstream->sample_rate = sample_rate;
|
|
|
|
vgmstream->codec_endian = big_endian;
|
|
|
|
vgmstream->num_streams = total_subsongs;
|
2021-05-31 22:25:48 +02:00
|
|
|
vgmstream->stream_size = get_streamfile_size(sf) / total_subsongs; /* approx... */
|
2019-12-16 20:31:03 +01:00
|
|
|
|
|
|
|
vgmstream->coding_type = coding;
|
|
|
|
vgmstream->layout_type = layout_blocked_ea_swvr;
|
|
|
|
/* DSP coefs are loaded per block */
|
|
|
|
/* some files (voices etc) decode with pops but seems a mastering problem */
|
|
|
|
|
2021-05-31 22:25:48 +02:00
|
|
|
if (!vgmstream_open_stream(vgmstream,sf,start_offset))
|
2019-12-16 20:31:03 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* calc num_samples manually */
|
|
|
|
{
|
|
|
|
int num_samples;
|
|
|
|
vgmstream->stream_index = target_subsong; /* needed to skip other subsong-blocks */
|
|
|
|
vgmstream->next_block_offset = start_offset;
|
|
|
|
do {
|
|
|
|
block_update(vgmstream->next_block_offset,vgmstream);
|
|
|
|
switch(vgmstream->coding_type) {
|
|
|
|
case coding_PSX: num_samples = ps_bytes_to_samples(vgmstream->current_block_size,1); break;
|
|
|
|
case coding_NGC_DSP: num_samples = dsp_bytes_to_samples(vgmstream->current_block_size,1); break;
|
|
|
|
case coding_PCM8_U_int: num_samples = pcm_bytes_to_samples(vgmstream->current_block_size,1,8); break;
|
|
|
|
default: num_samples = 0; break;
|
|
|
|
}
|
|
|
|
vgmstream->num_samples += num_samples;
|
|
|
|
}
|
2021-05-31 22:25:48 +02:00
|
|
|
while (vgmstream->next_block_offset < get_streamfile_size(sf));
|
2019-12-16 20:31:03 +01:00
|
|
|
block_update(start_offset, vgmstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = 0;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|