2008-05-06 05:35:37 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
|
|
|
#include "../layout/layout.h"
|
2008-03-04 08:15:25 +01:00
|
|
|
#include "../util.h"
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
VGMSTREAM * init_vgmstream_halpst(STREAMFILE *streamFile) {
|
2008-03-04 08:15:25 +01:00
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2013-05-27 05:55:50 +02:00
|
|
|
char filename[PATH_LIMIT];
|
2008-03-04 08:15:25 +01:00
|
|
|
|
|
|
|
int channel_count;
|
|
|
|
int loop_flag = 0;
|
|
|
|
|
|
|
|
int32_t samples_l,samples_r;
|
|
|
|
int32_t start_sample = 0;
|
|
|
|
|
|
|
|
size_t max_block;
|
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
2008-05-20 17:18:38 +02:00
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
2008-03-04 08:15:25 +01:00
|
|
|
if (strcasecmp("hps",filename_extension(filename))) goto fail;
|
|
|
|
|
2008-05-20 17:18:38 +02:00
|
|
|
/* check header */
|
|
|
|
if ((uint32_t)read_32bitBE(0,streamFile)!=0x2048414C || /* " HAL" */
|
|
|
|
read_32bitBE(4,streamFile)!=0x50535400) /* "PST\0" */
|
2008-03-04 08:15:25 +01:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* details */
|
2008-05-20 17:18:38 +02:00
|
|
|
channel_count = read_32bitBE(0xc,streamFile);
|
2008-05-20 22:19:46 +02:00
|
|
|
max_block = read_32bitBE(0x10,streamFile)/channel_count;
|
2008-03-04 08:15:25 +01:00
|
|
|
|
2009-04-05 21:09:24 +02:00
|
|
|
if (channel_count != 1 && channel_count != 2) goto fail;
|
2008-03-04 08:15:25 +01:00
|
|
|
|
|
|
|
/* yay for redundancy, gives us something to test */
|
2008-07-23 08:25:27 +02:00
|
|
|
samples_l = dsp_nibbles_to_samples(read_32bitBE(0x18,streamFile))+1;
|
2009-04-05 21:09:24 +02:00
|
|
|
if (channel_count == 2) {
|
|
|
|
samples_r = dsp_nibbles_to_samples(read_32bitBE(0x50,streamFile))+1;
|
2008-03-04 08:15:25 +01:00
|
|
|
|
2009-04-05 21:09:24 +02:00
|
|
|
if (samples_l != samples_r) goto fail;
|
|
|
|
}
|
2008-03-04 08:15:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* looping info is implicit in the "next block" field of the final
|
|
|
|
* block, so we have to find that
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
off_t offset = 0x80, last_offset = 0;
|
|
|
|
off_t loop_offset;
|
|
|
|
|
|
|
|
/* determine if there is a loop */
|
|
|
|
while (offset > last_offset) {
|
|
|
|
last_offset = offset;
|
2008-05-20 17:18:38 +02:00
|
|
|
offset = read_32bitBE(offset+8,streamFile);
|
2008-03-04 08:15:25 +01:00
|
|
|
}
|
|
|
|
if (offset < 0) loop_flag = 0;
|
|
|
|
else {
|
|
|
|
/* one more pass to determine start sample */
|
|
|
|
int32_t start_nibble = 0;
|
|
|
|
loop_flag = 1;
|
|
|
|
|
|
|
|
loop_offset = offset;
|
|
|
|
offset = 0x80;
|
|
|
|
while (offset != loop_offset) {
|
2008-05-20 17:18:38 +02:00
|
|
|
start_nibble += read_32bitBE(offset,streamFile);
|
|
|
|
offset = read_32bitBE(offset+8,streamFile);
|
2008-03-04 08:15:25 +01:00
|
|
|
}
|
|
|
|
|
2008-07-23 08:25:27 +02:00
|
|
|
start_sample = dsp_nibbles_to_samples(start_nibble);
|
2008-03-04 08:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
|
|
|
vgmstream->num_samples = samples_l;
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->sample_rate = read_32bitBE(8,streamFile);
|
2008-03-04 08:15:25 +01:00
|
|
|
/* channels and loop flag are set by allocate_vgmstream */
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = start_sample;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
vgmstream->coding_type = coding_NGC_DSP;
|
|
|
|
vgmstream->layout_type = layout_halpst_blocked;
|
|
|
|
vgmstream->meta_type = meta_HALPST;
|
|
|
|
|
|
|
|
/* load decode coefs */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<16;i++)
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[0].adpcm_coef[i] = read_16bitBE(0x20+i*2,streamFile);
|
2009-04-05 21:09:24 +02:00
|
|
|
if (channel_count == 2)
|
|
|
|
for (i=0;i<16;i++)
|
|
|
|
vgmstream->ch[1].adpcm_coef[i] = read_16bitBE(0x58+i*2,streamFile);
|
2008-03-04 08:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file for reading by each channel */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0;i<channel_count;i++) {
|
2008-05-20 17:18:38 +02:00
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,
|
2008-03-04 08:15:25 +01:00
|
|
|
(i==0?
|
|
|
|
max_block+0x20: /* first buffer a bit bigger to
|
|
|
|
read block header without
|
|
|
|
inefficiency */
|
|
|
|
max_block
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!vgmstream->ch[i].streamfile) goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start me up */
|
|
|
|
halpst_block_update(0x80,vgmstream);
|
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|