2018-01-13 17:47:40 +01:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../coding/coding.h"
|
2018-08-25 17:26:49 +02:00
|
|
|
#include "kma9_streamfile.h"
|
2018-01-13 17:47:40 +01:00
|
|
|
|
2018-08-25 17:26:49 +02:00
|
|
|
|
|
|
|
/* KMA9 - Koei Tecmo's interleaved ATRAC9 [Nobunaga no Yabou - Souzou (Vita)] */
|
2018-01-13 17:47:40 +01:00
|
|
|
VGMSTREAM * init_vgmstream_kma9(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2018-08-25 17:26:49 +02:00
|
|
|
STREAMFILE* temp_streamFile = NULL;
|
2018-01-13 17:47:40 +01:00
|
|
|
off_t start_offset;
|
2018-08-25 17:26:49 +02:00
|
|
|
size_t stream_size, interleave;
|
2018-01-13 17:47:40 +01:00
|
|
|
int loop_flag, channel_count;
|
|
|
|
int total_subsongs = 0, target_subsong = streamFile->stream_index;
|
|
|
|
|
|
|
|
|
2018-08-25 17:26:49 +02:00
|
|
|
/* checks */
|
2018-01-13 17:47:40 +01:00
|
|
|
if ( !check_extensions(streamFile,"km9") )
|
|
|
|
goto fail;
|
|
|
|
if (read_32bitBE(0x00,streamFile) != 0x4B4D4139) /* "KMA9" */
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
start_offset = read_32bitLE(0x04,streamFile);
|
|
|
|
channel_count = read_16bitLE(0x32,streamFile);
|
|
|
|
loop_flag = (read_32bitLE(0x28,streamFile) != 0);
|
|
|
|
|
|
|
|
total_subsongs = read_32bitLE(0x08,streamFile);
|
|
|
|
if (target_subsong == 0) target_subsong = 1;
|
|
|
|
if (target_subsong < 0 || target_subsong > total_subsongs || total_subsongs < 1) goto fail;
|
|
|
|
/* 0x0c: unknown */
|
2018-08-25 17:26:49 +02:00
|
|
|
interleave = read_32bitLE(0x10,streamFile); /* 1 superframe */
|
2018-01-28 00:41:25 +01:00
|
|
|
stream_size = read_32bitLE(0x14,streamFile); /* per subsong */
|
2018-01-13 17:47:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* build the VGMSTREAM */
|
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
vgmstream->sample_rate = read_32bitLE(0x34,streamFile);
|
|
|
|
vgmstream->num_samples = read_32bitLE(0x18,streamFile); /* without skip_samples? */
|
|
|
|
vgmstream->loop_start_sample = read_32bitLE(0x24,streamFile); /* with skip_samples? */
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples; /* 0x28 looks like end samples but isn't, no idea */
|
|
|
|
vgmstream->num_streams = total_subsongs;
|
2018-01-28 00:41:25 +01:00
|
|
|
vgmstream->stream_size = stream_size;
|
2018-01-13 17:47:40 +01:00
|
|
|
vgmstream->meta_type = meta_KMA9;
|
|
|
|
|
|
|
|
#ifdef VGM_USE_ATRAC9
|
|
|
|
{
|
|
|
|
atrac9_config cfg = {0};
|
|
|
|
|
|
|
|
cfg.channels = vgmstream->channels;
|
|
|
|
cfg.encoder_delay = read_32bitLE(0x20,streamFile);
|
2018-08-25 17:26:49 +02:00
|
|
|
cfg.config_data = read_32bitBE(0x5c,streamFile);
|
2018-01-13 17:47:40 +01:00
|
|
|
|
|
|
|
vgmstream->codec_data = init_atrac9(&cfg);
|
|
|
|
if (!vgmstream->codec_data) goto fail;
|
|
|
|
vgmstream->coding_type = coding_ATRAC9;
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
|
|
|
|
if (loop_flag) { /* seems correct but must double check */
|
|
|
|
vgmstream->loop_start_sample -= cfg.encoder_delay;
|
|
|
|
//vgmstream->loop_end_sample -= cfg.encoder_delay;
|
|
|
|
}
|
2018-08-25 17:26:49 +02:00
|
|
|
|
|
|
|
/* KMA9 interleaves one ATRAC9 frame per subsong */
|
|
|
|
temp_streamFile = setup_kma9_streamfile(streamFile, start_offset, stream_size, interleave, (target_subsong-1), total_subsongs);
|
|
|
|
if (!temp_streamFile) goto fail;
|
|
|
|
start_offset = 0;
|
2018-01-13 17:47:40 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
goto fail;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* open the file for reading */
|
2018-08-25 17:26:49 +02:00
|
|
|
if ( !vgmstream_open_stream(vgmstream, temp_streamFile, start_offset) )
|
2018-01-13 17:47:40 +01:00
|
|
|
goto fail;
|
2018-08-25 17:26:49 +02:00
|
|
|
close_streamfile(temp_streamFile);
|
2018-01-13 17:47:40 +01:00
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
fail:
|
2018-08-25 17:26:49 +02:00
|
|
|
close_streamfile(temp_streamFile);
|
2018-01-13 17:47:40 +01:00
|
|
|
close_vgmstream(vgmstream);
|
|
|
|
return NULL;
|
|
|
|
}
|