Clean MUS ACM meta (move parse code to its own function)

This commit is contained in:
bnnm 2018-03-16 15:42:54 +01:00
parent ce9a69aad1
commit 58c00577cf

View File

@ -1,7 +1,5 @@
#include "../vgmstream.h"
#include "meta.h" #include "meta.h"
#include "../util.h" #include "../coding/coding.h"
#include "../streamfile.h"
#include "../coding/acm_decoder.h" #include "../coding/acm_decoder.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -13,11 +11,94 @@
#define DIRSEP '/' #define DIRSEP '/'
#endif #endif
static char** parse_mus(STREAMFILE *streamFile, int *out_file_count, int *out_loop_flag, int *out_loop_start_index, int *out_loop_end_index);
static void clean_mus(char** mus_filenames, int file_count);
/* .MUS - playlist for InterPlay games [Planescape: Torment (PC), Baldur's Gate -Enhanced Edition- (PC)] */
VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
mus_acm_codec_data *data = NULL;
int channel_count, loop_flag = 0, loop_start_index = -1, loop_end_index = -1;
int32_t num_samples = 0, loop_start_samples = 0, loop_end_samples = 0;
char** mus_filenames = NULL;
int i, file_count = 0;
/* checks */
if (!check_extensions(streamFile, "mus"))
goto fail;
/* get file paths from the .MUS text file */
mus_filenames = parse_mus(streamFile, &file_count, &loop_flag, &loop_start_index, &loop_end_index);
if (!mus_filenames) goto fail;
/* init decoder */
data = init_acm(file_count);
if (!data) goto fail;
/* open each file */
for (i = 0; i < file_count; i++) {
ACMStream *acm_stream = NULL;
if (acm_open_decoder(&acm_stream,streamFile,mus_filenames[i]) != ACM_OK)
goto fail;
data->files[i]=acm_stream;
if (i==loop_start_index)
loop_start_samples = num_samples;
if (i==loop_end_index)
loop_end_samples = num_samples;
num_samples += data->files[i]->total_values / data->files[i]->info.channels;
if (i > 0) {
if (data->files[i]->info.channels != data->files[i-1]->info.channels ||
data->files[i]->info.rate != data->files[i-1]->info.rate) goto fail;
}
}
if (i==loop_end_index)
loop_end_samples = num_samples;
channel_count = data->files[0]->info.channels;
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->sample_rate = data->files[0]->info.rate;
vgmstream->num_samples = num_samples;
vgmstream->loop_start_sample = loop_start_samples;
vgmstream->loop_end_sample = loop_end_samples;
vgmstream->meta_type = meta_MUS_ACM;
vgmstream->coding_type = coding_ACM;
vgmstream->layout_type = layout_mus_acm;
data->loop_start_file = loop_start_index;
data->loop_end_file = loop_end_index;
/*data->end_file = -1;*/
vgmstream->codec_data = data;
clean_mus(mus_filenames, file_count);
return vgmstream;
fail:
clean_mus(mus_filenames, file_count);
free_acm(data);
close_vgmstream(vgmstream);
return NULL;
}
/* .mus text file parsing */
#define NAME_LENGTH PATH_LIMIT #define NAME_LENGTH PATH_LIMIT
static int exists(char *filename, STREAMFILE *streamfile) { static int exists(char *filename, STREAMFILE *streamfile) {
STREAMFILE * temp = STREAMFILE * temp = streamfile->open(streamfile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
streamfile->open(streamfile,filename,STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!temp) return 0; if (!temp) return 0;
close_streamfile(temp); close_streamfile(temp);
@ -91,57 +172,40 @@ fail:
return 1; return 1;
} }
/* MUS playlist for InterPlay ACM */ static char** parse_mus(STREAMFILE *streamFile, int *out_file_count, int *out_loop_flag, int *out_loop_start_index, int *out_loop_end_index) {
VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) { char** names = NULL;
VGMSTREAM * vgmstream = NULL;
ACMStream *acm_stream = NULL;
mus_acm_codec_data *data = NULL;
char filename[NAME_LENGTH]; char filename[NAME_LENGTH];
char line_buffer[NAME_LENGTH]; char line_buffer[NAME_LENGTH];
char * end_ptr; char * end_ptr;
char name_base[NAME_LENGTH]; char name_base[NAME_LENGTH];
char (*names)[NAME_LENGTH] = NULL;
char dir_name[NAME_LENGTH]; char dir_name[NAME_LENGTH];
char subdir_name[NAME_LENGTH]; char subdir_name[NAME_LENGTH];
int i;
int loop_flag = 0;
int channel_count;
int file_count; int file_count;
size_t line_bytes; size_t line_bytes;
int whole_line_read = 0; int whole_line_read = 0;
off_t mus_offset = 0; off_t mus_offset = 0;
int loop_end_index = -1; int i;
int loop_start_index = -1; int loop_flag = 0, loop_start_index = -1, loop_end_index = -1;
int32_t loop_start_samples = -1;
int32_t loop_end_samples = -1;
int32_t total_samples = 0;
/* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename));
if (strcasecmp("mus",filename_extension(filename))) goto fail;
/* read file name base */ /* read file name base */
line_bytes = get_streamfile_text_line(sizeof(line_buffer),line_buffer, line_bytes = get_streamfile_text_line(sizeof(line_buffer),line_buffer, mus_offset, streamFile, &whole_line_read);
mus_offset, streamFile, &whole_line_read);
if (!whole_line_read) goto fail; if (!whole_line_read) goto fail;
mus_offset += line_bytes; mus_offset += line_bytes;
memcpy(name_base,line_buffer,sizeof(name_base)); memcpy(name_base,line_buffer,sizeof(name_base));
/* uppercase name_base */ /* uppercase name_base */
{ {
int i; int j;
for (i=0;name_base[i];i++) name_base[i]=toupper(name_base[i]); for (j=0;name_base[j];j++)
name_base[j] = toupper(name_base[j]);
} }
/*printf("name base: %s\n",name_base);*/
/* read track entry count */ /* read track entry count */
line_bytes = get_streamfile_text_line(sizeof(line_buffer),line_buffer, line_bytes = get_streamfile_text_line(sizeof(line_buffer),line_buffer, mus_offset, streamFile, &whole_line_read);
mus_offset, streamFile, &whole_line_read);
if (!whole_line_read) goto fail; if (!whole_line_read) goto fail;
if (line_buffer[0] == '\0') goto fail; if (line_buffer[0] == '\0') goto fail;
mus_offset += line_bytes; mus_offset += line_bytes;
@ -149,24 +213,26 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
/* didn't parse whole line as an integer (optional opening whitespace) */ /* didn't parse whole line as an integer (optional opening whitespace) */
if (*end_ptr != '\0') goto fail; if (*end_ptr != '\0') goto fail;
/*printf("entries: %d\n",file_count);*/ /* set names */
names = calloc(file_count,sizeof(char*)); /* array of strings (size NAME_LENGTH) */
names = calloc(file_count,sizeof(names[0]));
if (!names) goto fail; if (!names) goto fail;
for (i = 0; i < file_count; i++) {
names[i] = calloc(1,sizeof(char)*NAME_LENGTH);
if (!names[i]) goto fail;
}
dir_name[0]='\0'; dir_name[0]='\0';
streamFile->get_name(streamFile,filename,sizeof(filename));
concatn(sizeof(dir_name),dir_name,filename); concatn(sizeof(dir_name),dir_name,filename);
/* find directory name for the directory contianing the MUS */
{ {
/* find directory name for the directory contianing the MUS */ char * last_slash = strrchr(dir_name,DIRSEP);
char * last_slash;
last_slash = strrchr(dir_name,DIRSEP);
if (last_slash != NULL) { if (last_slash != NULL) {
/* trim off the file name */ last_slash[1]='\0'; /* trim off the file name */
last_slash[1]='\0';
} else { } else {
/* no dir name? annihilate! */ dir_name[0] = '\0'; /* no dir name? annihilate! */
dir_name[0] = '\0';
} }
} }
@ -180,7 +246,8 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
char loop_name_temp[NAME_LENGTH]; char loop_name_temp[NAME_LENGTH];
char loop_name_base[NAME_LENGTH]; char loop_name_base[NAME_LENGTH];
char loop_name[NAME_LENGTH]; char loop_name[NAME_LENGTH];
for (i=0;i<file_count;i++)
for (i = 0; i < file_count; i++)
{ {
int fields_matched; int fields_matched;
line_bytes = line_bytes =
@ -192,7 +259,6 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
fields_matched = sscanf(line_buffer,"%s %s %s",name, fields_matched = sscanf(line_buffer,"%s %s %s",name,
loop_name_base_temp,loop_name_temp); loop_name_base_temp,loop_name_temp);
if (fields_matched < 1) goto fail; if (fields_matched < 1) goto fail;
if (fields_matched == 3 && loop_name_base_temp[0] != '@' && loop_name_temp[0] != '@') if (fields_matched == 3 && loop_name_base_temp[0] != '@' && loop_name_temp[0] != '@')
{ {
@ -222,14 +288,15 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
{ {
/* uppercase */ /* uppercase */
int j; int j;
for (j=0;j<strlen(name);j++) name[j]=toupper(name[j]); for (j=0;j<strlen(name);j++)
name[j]=toupper(name[j]);
} }
/* try looking in the common directory */ /* try looking in the common directory */
names[i][0] = '\0'; names[i][0] = '\0';
concatn(sizeof(names[0]),names[i],dir_name); concatn(NAME_LENGTH,names[i],dir_name);
concatn(sizeof(names[0]),names[i],name); concatn(NAME_LENGTH,names[i],name);
concatn(sizeof(names[0]),names[i],".ACM"); concatn(NAME_LENGTH,names[i],".ACM");
if (!exists(names[i],streamFile)) { if (!exists(names[i],streamFile)) {
@ -242,16 +309,14 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
} }
names[i][0] = '\0'; names[i][0] = '\0';
concatn(sizeof(names[0]),names[i],dir_name); concatn(NAME_LENGTH,names[i],dir_name);
concatn(sizeof(names[0]),names[i],subdir_name); concatn(NAME_LENGTH,names[i],subdir_name);
concatn(sizeof(names[0]),names[i],name_base); concatn(NAME_LENGTH,names[i],name_base);
concatn(sizeof(names[0]),names[i],name); concatn(NAME_LENGTH,names[i],name);
concatn(sizeof(names[0]),names[i],".ACM"); concatn(NAME_LENGTH,names[i],".ACM");
if (!exists(names[i],streamFile)) goto fail; if (!exists(names[i],streamFile)) goto fail;
} }
/*printf("%2d %s\n",i,names[i]);*/
} }
if (loop_end_index != -1) { if (loop_end_index != -1) {
@ -263,18 +328,15 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
concatn(sizeof(target_name),target_name,loop_name_base); concatn(sizeof(target_name),target_name,loop_name_base);
concatn(sizeof(target_name),target_name,loop_name); concatn(sizeof(target_name),target_name,loop_name);
concatn(sizeof(target_name),target_name,".ACM"); concatn(sizeof(target_name),target_name,".ACM");
/*printf("looking for loop %s\n",target_name);*/
for (i=0;i<file_count;i++) { for (i=0;i<file_count;i++) {
if (!strcmp(target_name,names[i])) if (!strcmp(target_name,names[i])) {
{
loop_start_index = i; loop_start_index = i;
break; break;
} }
} }
if (loop_start_index != -1) { if (loop_start_index != -1) {
/*printf("loop from %d to %d\n",loop_end_index,loop_start_index);*/
/*if (loop_start_index < file_count-1) loop_start_index++;*/ /*if (loop_start_index < file_count-1) loop_start_index++;*/
loop_end_index++; loop_end_index++;
loop_flag = 1; loop_flag = 1;
@ -283,78 +345,25 @@ VGMSTREAM * init_vgmstream_mus_acm(STREAMFILE *streamFile) {
} }
} }
/* set up the struct to track the files */ *out_loop_start_index = loop_start_index;
data = calloc(1,sizeof(mus_acm_codec_data)); *out_loop_end_index = loop_end_index;
if (!data) goto fail; *out_loop_flag = loop_flag;
*out_file_count = file_count;
data->files = calloc(file_count,sizeof(ACMStream *));
if (!data->files) {
free(data); data = NULL;
goto fail;
}
/* open each file... */ return names;
for (i=0;i<file_count;i++) {
/* gonna do this a little backwards, open and parse the file
before creating the vgmstream */
if (acm_open_decoder(&acm_stream,streamFile,names[i]) != ACM_OK) {
goto fail;
}
data->files[i]=acm_stream;
if (i==loop_start_index) loop_start_samples = total_samples;
if (i==loop_end_index) loop_end_samples = total_samples;
total_samples += acm_stream->total_values / acm_stream->info.channels;
if (i>0) {
if (acm_stream->info.channels != data->files[0]->info.channels ||
acm_stream->info.rate != data->files[0]->info.rate) goto fail;
}
}
if (i==loop_end_index) loop_end_samples = total_samples;
channel_count = data->files[0]->info.channels;
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
vgmstream->channels = channel_count;
vgmstream->sample_rate = data->files[0]->info.rate;
vgmstream->coding_type = coding_ACM;
vgmstream->num_samples = total_samples;
vgmstream->loop_start_sample = loop_start_samples;
vgmstream->loop_end_sample = loop_end_samples;
vgmstream->layout_type = layout_mus_acm;
vgmstream->meta_type = meta_MUS_ACM;
data->file_count = file_count;
data->current_file = 0;
data->loop_start_file = loop_start_index;
data->loop_end_file = loop_end_index;
/*data->end_file = -1;*/
vgmstream->codec_data = data;
free(names);
return vgmstream;
/* clean up anything we may have opened */
fail: fail:
if (data) { clean_mus(names, file_count);
int i;
for (i=0;i<data->file_count;i++) {
if (data->files[i]) {
acm_close(data->files[i]);
data->files[i] = NULL;
}
}
}
if (names) free(names);
if (vgmstream) close_vgmstream(vgmstream);
return NULL; return NULL;
} }
static void clean_mus(char** mus_filenames, int file_count) {
int i;
if (!mus_filenames) return;
for (i = 0; i < file_count; i++) {
free(mus_filenames[i]);
}
free(mus_filenames);
}