2008-07-11 02:41:21 +02:00
|
|
|
#include "meta.h"
|
|
|
|
#include "../util.h"
|
2008-08-03 16:58:03 +02:00
|
|
|
#include "../coding/nwa_decoder.h"
|
2008-07-11 03:43:15 +02:00
|
|
|
#include <string.h>
|
2008-07-20 07:41:41 +02:00
|
|
|
#include <ctype.h>
|
2008-07-11 03:43:15 +02:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define DIRSEP '\\'
|
|
|
|
#else
|
|
|
|
#define DIRSEP '/'
|
|
|
|
#endif
|
2008-07-11 02:41:21 +02:00
|
|
|
|
2008-08-03 16:58:03 +02:00
|
|
|
/* NWA - Visual Art's streams */
|
2008-07-11 02:41:21 +02:00
|
|
|
|
|
|
|
VGMSTREAM * init_vgmstream_nwa(STREAMFILE *streamFile) {
|
|
|
|
VGMSTREAM * vgmstream = NULL;
|
2013-05-27 05:55:50 +02:00
|
|
|
char filename[PATH_LIMIT];
|
2008-07-11 02:41:21 +02:00
|
|
|
int i;
|
|
|
|
int channel_count;
|
2008-07-11 03:43:15 +02:00
|
|
|
int loop_flag = 0;
|
2008-07-12 17:31:12 +02:00
|
|
|
int32_t loop_start_sample = 0;
|
|
|
|
int32_t loop_end_sample = 0;
|
2008-07-11 08:34:09 +02:00
|
|
|
int nwainfo_ini_found = 0;
|
|
|
|
int gameexe_ini_found = 0;
|
2008-08-03 16:58:03 +02:00
|
|
|
int just_pcm = 0;
|
|
|
|
int comp_level = -2;
|
|
|
|
nwa_codec_data *data = NULL;
|
2008-07-11 02:41:21 +02:00
|
|
|
|
|
|
|
/* check extension, case insensitive */
|
|
|
|
streamFile->get_name(streamFile,filename,sizeof(filename));
|
|
|
|
if (strcasecmp("nwa",filename_extension(filename))) goto fail;
|
|
|
|
|
2008-08-03 16:58:03 +02:00
|
|
|
channel_count = read_16bitLE(0x00,streamFile);
|
|
|
|
if (channel_count != 1 && channel_count != 2) goto fail;
|
|
|
|
|
|
|
|
/* check if we're using raw pcm */
|
2008-07-11 02:41:21 +02:00
|
|
|
if (
|
2008-08-03 16:58:03 +02:00
|
|
|
read_32bitLE(0x08,streamFile)==-1 || /* compression level */
|
|
|
|
read_32bitLE(0x10,streamFile)==0 || /* block count */
|
|
|
|
read_32bitLE(0x18,streamFile)==0 || /* compressed data size */
|
|
|
|
read_32bitLE(0x20,streamFile)==0 || /* block size */
|
|
|
|
read_32bitLE(0x24,streamFile)==0 /* restsize */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
just_pcm = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
comp_level = read_32bitLE(0x08,streamFile);
|
|
|
|
|
|
|
|
data = malloc(sizeof(nwa_codec_data));
|
|
|
|
if (!data) goto fail;
|
|
|
|
|
|
|
|
data->nwa = open_nwa(streamFile,filename);
|
|
|
|
if (!data->nwa) goto fail;
|
|
|
|
}
|
2008-07-11 02:41:21 +02:00
|
|
|
|
2008-07-11 03:43:15 +02:00
|
|
|
/* try to locate NWAINFO.INI in the same directory */
|
|
|
|
{
|
|
|
|
char ininame[260];
|
|
|
|
char * ini_lastslash;
|
2008-07-11 10:29:04 +02:00
|
|
|
char namebase_array[260];
|
|
|
|
char *namebase;
|
2008-07-11 03:43:15 +02:00
|
|
|
STREAMFILE *inistreamfile;
|
|
|
|
|
2008-07-11 10:29:04 +02:00
|
|
|
/* here we assume that the "special encoding" does not affect
|
|
|
|
* the directory separator */
|
2008-07-11 03:43:15 +02:00
|
|
|
strncpy(ininame,filename,sizeof(ininame));
|
|
|
|
ininame[sizeof(ininame)-1]='\0'; /* a pox on the stdlib! */
|
|
|
|
|
2008-07-11 10:29:04 +02:00
|
|
|
streamFile->get_realname(streamFile,namebase_array,sizeof(namebase_array));
|
|
|
|
|
2008-07-11 03:43:15 +02:00
|
|
|
ini_lastslash = strrchr(ininame,DIRSEP);
|
|
|
|
if (!ini_lastslash) {
|
|
|
|
strncpy(ininame,"NWAINFO.INI",sizeof(ininame));
|
2008-07-11 10:29:04 +02:00
|
|
|
namebase = namebase_array;
|
2008-07-11 03:43:15 +02:00
|
|
|
} else {
|
|
|
|
strncpy(ini_lastslash+1,"NWAINFO.INI",
|
|
|
|
sizeof(ininame)-(ini_lastslash+1-ininame));
|
2008-07-11 10:29:04 +02:00
|
|
|
namebase = strrchr(namebase_array,DIRSEP)+1;
|
2008-07-11 03:43:15 +02:00
|
|
|
}
|
|
|
|
ininame[sizeof(ininame)-1]='\0'; /* curse you, strncpy! */
|
|
|
|
|
|
|
|
inistreamfile = streamFile->open(streamFile,ininame,4096);
|
|
|
|
|
|
|
|
if (inistreamfile) {
|
|
|
|
/* ini found, try to find our name */
|
|
|
|
const char * ext;
|
|
|
|
int length;
|
|
|
|
int found;
|
|
|
|
off_t offset;
|
|
|
|
off_t file_size;
|
2008-07-14 12:48:46 +02:00
|
|
|
off_t found_off = -1;
|
2008-07-11 03:43:15 +02:00
|
|
|
|
2008-07-11 08:34:09 +02:00
|
|
|
nwainfo_ini_found = 1;
|
2008-07-11 03:43:15 +02:00
|
|
|
|
|
|
|
ext = filename_extension(namebase);
|
|
|
|
length = ext-1-namebase;
|
|
|
|
file_size = get_streamfile_size(inistreamfile);
|
|
|
|
|
|
|
|
for (found = 0, offset = 0; !found && offset<file_size; offset++) {
|
|
|
|
off_t suboffset;
|
|
|
|
/* Go for an n*m search 'cause it's easier than building an
|
|
|
|
* FSA for the search string. Just wanted to make the point that
|
|
|
|
* I'm not ignorant, just lazy. */
|
|
|
|
for (suboffset = offset;
|
|
|
|
suboffset<file_size &&
|
|
|
|
suboffset-offset<length &&
|
|
|
|
read_8bit(suboffset,inistreamfile)==
|
|
|
|
namebase[suboffset-offset];
|
|
|
|
suboffset++) {}
|
|
|
|
|
|
|
|
if (suboffset-offset==length &&
|
|
|
|
read_8bit(suboffset,inistreamfile)==0x09) { /* tab */
|
|
|
|
found=1;
|
|
|
|
found_off = suboffset+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
char loopstring[9]={0};
|
|
|
|
|
|
|
|
if (read_streamfile((uint8_t*)loopstring,found_off,8,
|
|
|
|
inistreamfile)==8)
|
|
|
|
{
|
|
|
|
loop_start_sample = atol(loopstring);
|
|
|
|
if (loop_start_sample > 0) loop_flag = 1;
|
|
|
|
}
|
|
|
|
} /* if found file name in INI */
|
|
|
|
|
|
|
|
close_streamfile(inistreamfile);
|
|
|
|
} /* if opened INI ok */
|
|
|
|
} /* INI block */
|
|
|
|
|
2008-07-11 08:34:09 +02:00
|
|
|
/* try to locate Gameexe.ini in the same directory */
|
|
|
|
{
|
|
|
|
char ininame[260];
|
|
|
|
char * ini_lastslash;
|
2008-07-11 10:29:04 +02:00
|
|
|
char namebase_array[260];
|
2008-07-11 08:34:09 +02:00
|
|
|
char * namebase;
|
|
|
|
STREAMFILE *inistreamfile;
|
|
|
|
|
|
|
|
strncpy(ininame,filename,sizeof(ininame));
|
|
|
|
ininame[sizeof(ininame)-1]='\0'; /* a pox on the stdlib! */
|
|
|
|
|
2008-07-11 10:29:04 +02:00
|
|
|
streamFile->get_realname(streamFile,namebase_array,sizeof(namebase_array));
|
|
|
|
|
2008-07-11 08:34:09 +02:00
|
|
|
ini_lastslash = strrchr(ininame,DIRSEP);
|
|
|
|
if (!ini_lastslash) {
|
|
|
|
strncpy(ininame,"Gameexe.ini",sizeof(ininame));
|
2008-07-11 10:29:04 +02:00
|
|
|
namebase = namebase_array;
|
2008-07-11 08:34:09 +02:00
|
|
|
} else {
|
|
|
|
strncpy(ini_lastslash+1,"Gameexe.ini",
|
|
|
|
sizeof(ininame)-(ini_lastslash+1-ininame));
|
2008-07-11 10:29:04 +02:00
|
|
|
namebase = strrchr(namebase_array,DIRSEP)+1;
|
2008-07-11 08:34:09 +02:00
|
|
|
}
|
|
|
|
ininame[sizeof(ininame)-1]='\0'; /* curse you, strncpy! */
|
|
|
|
|
|
|
|
inistreamfile = streamFile->open(streamFile,ininame,4096);
|
|
|
|
|
|
|
|
if (inistreamfile) {
|
|
|
|
/* ini found, try to find our name */
|
|
|
|
const char * ext;
|
|
|
|
int length;
|
|
|
|
int found;
|
|
|
|
off_t offset;
|
|
|
|
off_t file_size;
|
2008-07-14 12:48:46 +02:00
|
|
|
off_t found_off = -1;
|
2008-07-11 08:34:09 +02:00
|
|
|
|
|
|
|
gameexe_ini_found = 1;
|
|
|
|
|
|
|
|
ext = filename_extension(namebase);
|
|
|
|
length = ext-1-namebase;
|
|
|
|
file_size = get_streamfile_size(inistreamfile);
|
|
|
|
|
|
|
|
/* format of line is:
|
|
|
|
* #DSTRACK = 00000000 - eeeeeeee - ssssssss = "name" = "name2?"
|
|
|
|
* ^22 ^33 ^45 ^57
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (found = 0, offset = 0; !found && offset<file_size; offset++) {
|
|
|
|
off_t suboffset;
|
|
|
|
uint8_t buf[10];
|
|
|
|
|
|
|
|
if (read_8bit(offset,inistreamfile)!='#') continue;
|
|
|
|
if (read_streamfile(buf,offset+1,10,inistreamfile)!=10) break;
|
|
|
|
if (memcmp("DSTRACK = ",buf,10)) continue;
|
|
|
|
if (read_8bit(offset+44,inistreamfile)!='\"') continue;
|
|
|
|
|
|
|
|
for (suboffset = offset+45;
|
|
|
|
suboffset<file_size &&
|
|
|
|
suboffset-offset-45<length &&
|
2008-07-19 18:43:42 +02:00
|
|
|
tolower(read_8bit(suboffset,inistreamfile))==
|
|
|
|
tolower(namebase[suboffset-offset-45]);
|
2008-07-11 08:34:09 +02:00
|
|
|
suboffset++) {}
|
|
|
|
|
|
|
|
if (suboffset-offset-45==length &&
|
|
|
|
read_8bit(suboffset,inistreamfile)=='\"') { /* tab */
|
|
|
|
found=1;
|
|
|
|
found_off = offset+22; /* loop end */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
char loopstring[9]={0};
|
|
|
|
int start_ok = 0, end_ok = 0;
|
2008-07-19 18:16:16 +02:00
|
|
|
int32_t total_samples =
|
|
|
|
read_32bitLE(0x1c,streamFile)/channel_count;
|
2008-07-11 08:34:09 +02:00
|
|
|
|
|
|
|
if (read_streamfile((uint8_t*)loopstring,found_off,8,
|
2008-07-19 18:16:16 +02:00
|
|
|
inistreamfile)==8)
|
2008-07-11 08:34:09 +02:00
|
|
|
{
|
2008-07-19 18:16:16 +02:00
|
|
|
if (!memcmp("99999999",loopstring,8))
|
|
|
|
{
|
|
|
|
loop_end_sample = total_samples;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
loop_end_sample = atol(loopstring);
|
|
|
|
}
|
2008-07-11 08:34:09 +02:00
|
|
|
end_ok = 1;
|
|
|
|
}
|
|
|
|
if (read_streamfile((uint8_t*)loopstring,found_off+11,8,
|
2008-07-19 18:16:16 +02:00
|
|
|
inistreamfile)==8)
|
2008-07-11 08:34:09 +02:00
|
|
|
{
|
2008-07-19 18:16:16 +02:00
|
|
|
if (!memcmp("99999999",loopstring,8))
|
|
|
|
{
|
|
|
|
/* not ok to start at last sample,
|
|
|
|
* don't set start_ok flag */
|
|
|
|
}
|
2008-08-03 18:10:51 +02:00
|
|
|
else if (!memcmp("00000000",loopstring,8))
|
|
|
|
{
|
|
|
|
/* loops from the start aren't really loops */
|
|
|
|
}
|
2008-07-19 18:16:16 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
loop_start_sample = atol(loopstring);
|
|
|
|
start_ok = 1;
|
|
|
|
}
|
2008-07-11 08:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (start_ok && end_ok) loop_flag = 1;
|
|
|
|
} /* if found file name in INI */
|
|
|
|
|
|
|
|
close_streamfile(inistreamfile);
|
|
|
|
} /* if opened INI ok */
|
|
|
|
} /* INI block */
|
|
|
|
|
2008-07-11 02:41:21 +02:00
|
|
|
/* build the VGMSTREAM */
|
2008-07-11 03:43:15 +02:00
|
|
|
vgmstream = allocate_vgmstream(channel_count,loop_flag);
|
2008-07-11 02:41:21 +02:00
|
|
|
if (!vgmstream) goto fail;
|
|
|
|
|
|
|
|
/* fill in the vital statistics */
|
|
|
|
vgmstream->channels = channel_count;
|
|
|
|
vgmstream->sample_rate = read_32bitLE(0x04,streamFile);
|
2008-08-03 16:58:03 +02:00
|
|
|
|
2008-07-11 02:41:21 +02:00
|
|
|
vgmstream->num_samples = read_32bitLE(0x1c,streamFile)/channel_count;
|
2008-08-03 16:58:03 +02:00
|
|
|
|
|
|
|
if (just_pcm) {
|
|
|
|
switch (read_16bitLE(0x02,streamFile)) {
|
|
|
|
case 8:
|
|
|
|
vgmstream->coding_type = coding_PCM8;
|
|
|
|
vgmstream->interleave_block_size = 1;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
vgmstream->coding_type = coding_PCM16LE;
|
|
|
|
vgmstream->interleave_block_size = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (channel_count > 1) {
|
|
|
|
vgmstream->layout_type = layout_interleave;
|
|
|
|
} else {
|
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (comp_level)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
vgmstream->coding_type = coding_NWA0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
vgmstream->coding_type = coding_NWA1;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
vgmstream->coding_type = coding_NWA2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
vgmstream->coding_type = coding_NWA3;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
vgmstream->coding_type = coding_NWA4;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
vgmstream->coding_type = coding_NWA5;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
break;
|
|
|
|
}
|
2008-07-11 02:41:21 +02:00
|
|
|
vgmstream->layout_type = layout_none;
|
|
|
|
}
|
2008-07-11 03:43:15 +02:00
|
|
|
|
2008-07-11 08:34:09 +02:00
|
|
|
if (nwainfo_ini_found) {
|
|
|
|
vgmstream->meta_type = meta_NWA_NWAINFOINI;
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = loop_start_sample;
|
|
|
|
vgmstream->loop_end_sample = vgmstream->num_samples;
|
|
|
|
}
|
|
|
|
} else if (gameexe_ini_found) {
|
|
|
|
vgmstream->meta_type = meta_NWA_GAMEEXEINI;
|
|
|
|
if (loop_flag) {
|
|
|
|
vgmstream->loop_start_sample = loop_start_sample;
|
|
|
|
vgmstream->loop_end_sample = loop_end_sample;
|
|
|
|
}
|
2008-07-11 03:43:15 +02:00
|
|
|
} else {
|
|
|
|
vgmstream->meta_type = meta_NWA;
|
|
|
|
}
|
|
|
|
|
2008-07-11 02:41:21 +02:00
|
|
|
|
2008-08-03 16:58:03 +02:00
|
|
|
if (just_pcm) {
|
|
|
|
/* open the file for reading by each channel */
|
2008-07-11 02:41:21 +02:00
|
|
|
STREAMFILE *chstreamfile;
|
|
|
|
|
|
|
|
/* have both channels use the same buffer, as interleave is so small */
|
|
|
|
chstreamfile = streamFile->open(streamFile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
|
2008-07-11 03:43:15 +02:00
|
|
|
|
2008-07-11 02:41:21 +02:00
|
|
|
if (!chstreamfile) goto fail;
|
|
|
|
|
2008-07-11 05:29:34 +02:00
|
|
|
for (i=0;i<channel_count;i++) {
|
2008-07-11 02:41:21 +02:00
|
|
|
vgmstream->ch[i].streamfile = chstreamfile;
|
|
|
|
|
|
|
|
vgmstream->ch[i].channel_start_offset=
|
|
|
|
vgmstream->ch[i].offset=0x2c+(off_t)(i*vgmstream->interleave_block_size);
|
|
|
|
}
|
|
|
|
}
|
2008-08-03 16:58:03 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
vgmstream->codec_data = data;
|
|
|
|
}
|
2008-07-11 02:41:21 +02:00
|
|
|
|
|
|
|
return vgmstream;
|
|
|
|
|
|
|
|
/* clean up anything we may have opened */
|
|
|
|
fail:
|
|
|
|
if (vgmstream) close_vgmstream(vgmstream);
|
2008-08-03 16:58:03 +02:00
|
|
|
if (data) {
|
|
|
|
if (data->nwa)
|
|
|
|
{
|
|
|
|
close_nwa(data->nwa);
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
2008-07-11 02:41:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|