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;
|
2017-05-21 03:35:27 +02:00
|
|
|
int header_length = 0x80;
|
2008-03-04 08:15:25 +01:00
|
|
|
|
|
|
|
int32_t samples_l,samples_r;
|
|
|
|
int32_t start_sample = 0;
|
|
|
|
|
|
|
|
/* 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);
|
2018-08-19 22:51:14 +02:00
|
|
|
/*max_block = read_32bitBE(0x10,streamFile)/channel_count;*/
|
2008-03-04 08:15:25 +01:00
|
|
|
|
2017-05-21 03:35:27 +02:00
|
|
|
if (channel_count > 2) {
|
|
|
|
/* align the header length needed for the extra channels */
|
|
|
|
header_length = 0x10+0x38*channel_count;
|
|
|
|
header_length = (header_length+0x1f)/0x20*0x20;
|
|
|
|
}
|
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;
|
2017-05-21 03:35:27 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=1;i<channel_count;i++) {
|
|
|
|
samples_r = dsp_nibbles_to_samples(read_32bitBE(0x18+0x38*i,streamFile))+1;
|
|
|
|
if (samples_l != samples_r) goto fail;
|
|
|
|
}
|
2009-04-05 21:09:24 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
{
|
2017-05-21 03:35:27 +02:00
|
|
|
off_t offset = header_length, last_offset = 0;
|
2008-03-04 08:15:25 +01:00
|
|
|
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;
|
2017-05-21 03:35:27 +02:00
|
|
|
offset = header_length;
|
2008-03-04 08:15:25 +01:00
|
|
|
while (offset != loop_offset) {
|
2017-05-21 01:45:49 +02:00
|
|
|
start_nibble += read_32bitBE(offset+4,streamFile)+1;
|
2008-05-20 17:18:38 +02:00
|
|
|
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;
|
2018-03-29 19:00:04 +02:00
|
|
|
vgmstream->layout_type = layout_blocked_halpst;
|
2008-03-04 08:15:25 +01:00
|
|
|
vgmstream->meta_type = meta_HALPST;
|
|
|
|
|
|
|
|
/* load decode coefs */
|
|
|
|
{
|
2017-05-21 03:35:27 +02:00
|
|
|
int i,j;
|
|
|
|
for (i=0;i<channel_count;i++)
|
|
|
|
for (j=0;j<16;j++)
|
|
|
|
vgmstream->ch[i].adpcm_coef[j] = read_16bitBE(0x20+0x38*i+j*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++) {
|
2018-08-19 22:51:14 +02:00
|
|
|
vgmstream->ch[i].streamfile = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2008-03-04 08:15:25 +01:00
|
|
|
|
|
|
|
if (!vgmstream->ch[i].streamfile) goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start me up */
|
2018-03-29 19:00:04 +02:00
|
|
|
block_update_halpst(header_length,vgmstream);
|
2008-03-04 08:15:25 +01:00
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|