2008-02-04 13:11:40 +01:00
|
|
|
#define POSIXLY_CORRECT
|
|
|
|
#include <unistd.h>
|
2008-01-31 07:15:03 +01:00
|
|
|
#include "../src/vgmstream.h"
|
|
|
|
#include "../src/util.h"
|
2008-07-01 21:20:09 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2008-01-31 07:15:03 +01:00
|
|
|
|
|
|
|
#define BUFSIZE 4000
|
|
|
|
|
2008-02-04 13:11:40 +01:00
|
|
|
extern char * optarg;
|
|
|
|
extern int optind, opterr, optopt;
|
|
|
|
|
|
|
|
void usage(const char * name) {
|
2008-05-15 15:54:34 +02:00
|
|
|
fprintf(stderr,"vgmstream test decoder " VERSION " " __DATE__ "\n"
|
2008-05-16 22:28:36 +02:00
|
|
|
"Usage: %s [-o outfile.wav] [-l loop count]\n"
|
|
|
|
" [-f fade time] [-d fade delay] [-ipcmxeE] infile\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -o outfile.wav: name of output .wav file, default is dump.wav\n"
|
|
|
|
" -l loop count: loop count, default 2.0\n"
|
|
|
|
" -f fade time: fade time (seconds), default 10.0\n"
|
|
|
|
" -d fade delay: fade delay (seconds, default 0.0\n"
|
|
|
|
" -i: ignore looping information and play the whole stream once\n"
|
|
|
|
" -p: output to stdout (for piping into another program)\n"
|
|
|
|
" -P: output to stdout even if stdout is a terminal\n"
|
|
|
|
" -c: loop forever (continuously)\n"
|
|
|
|
" -m: print metadata only, don't decode\n"
|
|
|
|
" -x: decode and print adxencd command line to encode as ADX\n"
|
2008-09-29 06:08:03 +02:00
|
|
|
" -g: decode and print oggenc command line to encode as OGG\n"
|
2009-06-24 22:10:07 +02:00
|
|
|
" -b: decode and print batch variable commands\n"
|
2008-05-16 22:28:36 +02:00
|
|
|
" -e: force end-to-end looping\n"
|
|
|
|
" -E: force end-to-end looping even if file has real loop points\n"
|
2008-05-19 05:58:15 +02:00
|
|
|
" -r outfile2.wav: output a second time after resetting\n"
|
2008-03-04 08:40:41 +01:00
|
|
|
,name);
|
|
|
|
|
2008-02-04 13:11:40 +01:00
|
|
|
}
|
|
|
|
|
2008-01-31 07:15:03 +01:00
|
|
|
int main(int argc, char ** argv) {
|
|
|
|
VGMSTREAM * s;
|
2008-04-15 17:49:47 +02:00
|
|
|
sample * buf = NULL;
|
2008-01-31 07:15:03 +01:00
|
|
|
int32_t len;
|
2008-02-04 13:11:40 +01:00
|
|
|
int32_t fade_samples;
|
2008-01-31 07:15:03 +01:00
|
|
|
int i;
|
2008-02-04 13:11:40 +01:00
|
|
|
FILE * outfile = NULL;
|
2008-02-14 23:38:23 +01:00
|
|
|
char * outfilename = NULL;
|
2008-05-19 05:58:15 +02:00
|
|
|
char * reset_outfilename = NULL;
|
2008-02-04 13:11:40 +01:00
|
|
|
int opt;
|
|
|
|
int ignore_loop = 0;
|
2008-05-05 00:03:10 +02:00
|
|
|
int force_loop = 0;
|
|
|
|
int really_force_loop = 0;
|
2008-02-06 21:53:10 +01:00
|
|
|
int play = 0;
|
2008-05-10 06:08:01 +02:00
|
|
|
int play_wreckless = 0;
|
2008-02-06 21:53:10 +01:00
|
|
|
int forever = 0;
|
2008-02-14 23:38:23 +01:00
|
|
|
int metaonly = 0;
|
2008-03-04 08:40:41 +01:00
|
|
|
int adxencd = 0;
|
2008-09-29 06:08:03 +02:00
|
|
|
int oggenc = 0;
|
2009-06-24 22:10:07 +02:00
|
|
|
int batchvar = 0;
|
2008-02-04 13:11:40 +01:00
|
|
|
double loop_count = 2.0;
|
2008-05-16 22:28:36 +02:00
|
|
|
double fade_seconds = 10.0;
|
|
|
|
double fade_delay_seconds = 0.0;
|
2008-12-24 10:11:58 +01:00
|
|
|
|
2009-06-24 22:10:07 +02:00
|
|
|
while ((opt = getopt(argc, argv, "o:l:f:d:ipPcmxeEr:gb")) != -1) {
|
2008-02-04 13:11:40 +01:00
|
|
|
switch (opt) {
|
|
|
|
case 'o':
|
2008-02-14 23:38:23 +01:00
|
|
|
outfilename = optarg;
|
2008-02-04 13:11:40 +01:00
|
|
|
break;
|
|
|
|
case 'l':
|
2008-02-14 23:38:23 +01:00
|
|
|
loop_count = atof(optarg);
|
2008-02-04 13:11:40 +01:00
|
|
|
break;
|
|
|
|
case 'f':
|
2008-05-16 22:28:36 +02:00
|
|
|
fade_seconds = atof(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
fade_delay_seconds = atof(optarg);
|
2008-02-04 13:11:40 +01:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
ignore_loop = 1;
|
|
|
|
break;
|
2008-02-06 21:53:10 +01:00
|
|
|
case 'p':
|
|
|
|
play = 1;
|
|
|
|
break;
|
2008-05-10 06:08:01 +02:00
|
|
|
case 'P':
|
|
|
|
play_wreckless = 1;
|
|
|
|
play = 1;
|
|
|
|
break;
|
2008-02-06 21:53:10 +01:00
|
|
|
case 'c':
|
|
|
|
forever = 1;
|
|
|
|
break;
|
2008-02-14 23:38:23 +01:00
|
|
|
case 'm':
|
|
|
|
metaonly = 1;
|
|
|
|
break;
|
2008-03-04 08:40:41 +01:00
|
|
|
case 'x':
|
|
|
|
adxencd = 1;
|
|
|
|
break;
|
2008-09-29 06:08:03 +02:00
|
|
|
case 'g':
|
|
|
|
oggenc = 1;
|
|
|
|
break;
|
2009-06-24 22:10:07 +02:00
|
|
|
case 'b':
|
|
|
|
batchvar = 1;
|
|
|
|
break;
|
2008-05-05 00:03:10 +02:00
|
|
|
case 'e':
|
|
|
|
force_loop = 1;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
really_force_loop = 1;
|
|
|
|
break;
|
2008-05-19 05:58:15 +02:00
|
|
|
case 'r':
|
|
|
|
reset_outfilename = optarg;
|
|
|
|
break;
|
2008-02-04 13:11:40 +01:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-31 07:15:03 +01:00
|
|
|
|
2008-02-04 13:11:40 +01:00
|
|
|
if (optind!=argc-1) {
|
|
|
|
usage(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-02-06 21:53:10 +01:00
|
|
|
|
|
|
|
if (forever && !play) {
|
|
|
|
fprintf(stderr,"A file of infinite size? Not likely.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2008-05-05 00:03:10 +02:00
|
|
|
|
2008-05-10 06:08:01 +02:00
|
|
|
if (play && (!play_wreckless &&isatty(STDOUT_FILENO))) {
|
|
|
|
fprintf(stderr,"Are you sure you want to output wave data to the terminal?\nIf so use -P instead of -p.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-07-01 21:20:09 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
/* make stdout output work with windows */
|
|
|
|
if (play) {
|
|
|
|
_setmode(fileno(stdout),_O_BINARY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-05 00:03:10 +02:00
|
|
|
if (ignore_loop && force_loop) {
|
|
|
|
fprintf(stderr,"-e and -i are incompatible\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (ignore_loop && really_force_loop) {
|
|
|
|
fprintf(stderr,"-E and -i are incompatible\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (force_loop && really_force_loop) {
|
|
|
|
fprintf(stderr,"-E and -e are somewhat redundant, are you confused?\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-04 13:11:40 +01:00
|
|
|
s = init_vgmstream(argv[optind]);
|
2008-01-31 07:15:03 +01:00
|
|
|
|
|
|
|
if (!s) {
|
2008-02-04 13:11:40 +01:00
|
|
|
fprintf(stderr,"failed opening %s\n",argv[optind]);
|
2008-01-31 07:15:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-05-05 00:03:10 +02:00
|
|
|
/* force only if there aren't already loop points */
|
|
|
|
if (force_loop && !s->loop_flag) {
|
|
|
|
/* this requires a bit more messing with the VGMSTREAM than I'm
|
|
|
|
* comfortable with... */
|
|
|
|
s->loop_flag=1;
|
|
|
|
s->loop_start_sample=0;
|
|
|
|
s->loop_end_sample=s->num_samples;
|
|
|
|
s->loop_ch=calloc(s->channels,sizeof(VGMSTREAMCHANNEL));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* force even if there are loop points */
|
|
|
|
if (really_force_loop) {
|
|
|
|
if (!s->loop_flag) s->loop_ch=calloc(s->channels,sizeof(VGMSTREAMCHANNEL));
|
|
|
|
s->loop_flag=1;
|
|
|
|
s->loop_start_sample=0;
|
|
|
|
s->loop_end_sample=s->num_samples;
|
|
|
|
}
|
|
|
|
|
2008-02-04 13:11:40 +01:00
|
|
|
if (ignore_loop) s->loop_flag=0;
|
|
|
|
|
2008-02-06 21:53:10 +01:00
|
|
|
if (play) {
|
2008-02-14 23:38:23 +01:00
|
|
|
if (outfilename) {
|
2008-02-06 21:53:10 +01:00
|
|
|
fprintf(stderr,"either -p or -o, make up your mind\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
outfile = stdout;
|
2008-02-14 23:38:23 +01:00
|
|
|
} else if (!metaonly) {
|
|
|
|
if (!outfilename) outfilename = "dump.wav";
|
|
|
|
outfile = fopen(outfilename,"wb");
|
2008-02-04 13:11:40 +01:00
|
|
|
if (!outfile) {
|
2008-05-19 05:58:15 +02:00
|
|
|
fprintf(stderr,"failed to open %s for output\n",outfilename);
|
2008-02-04 13:11:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-06 21:53:10 +01:00
|
|
|
if (forever && !s->loop_flag) {
|
|
|
|
fprintf(stderr,"I could play a nonlooped track forever, but it wouldn't end well.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-14 23:38:23 +01:00
|
|
|
if (!play) {
|
2008-05-20 22:48:44 +02:00
|
|
|
if (adxencd) {
|
|
|
|
printf("adxencd");
|
|
|
|
if (!metaonly) printf(" \"%s\"",outfilename);
|
2008-03-04 08:40:41 +01:00
|
|
|
if (s->loop_flag) printf(" -lps%d -lpe%d",s->loop_start_sample,s->loop_end_sample);
|
|
|
|
printf("\n");
|
2008-09-29 06:08:03 +02:00
|
|
|
} else if (oggenc) {
|
|
|
|
printf("oggenc");
|
|
|
|
if (!metaonly) printf(" \"%s\"",outfilename);
|
|
|
|
if (s->loop_flag) printf(" -c LOOPSTART=%d -c LOOPLENGTH=%d",s->loop_start_sample,
|
|
|
|
s->loop_end_sample-s->loop_start_sample);
|
|
|
|
printf("\n");
|
2009-06-24 22:10:07 +02:00
|
|
|
} else if (batchvar) {
|
|
|
|
if (!metaonly) printf("set fname=\"%s\"\n",outfilename);
|
|
|
|
printf("set tsamp=%d\nset chan=%d\n", s->num_samples, s->channels);
|
|
|
|
if (s->loop_flag) printf("set lstart=%d\nset lend=%d\nset loop=1\n", s->loop_start_sample, s->loop_end_sample);
|
|
|
|
else printf("set loop=0\n");
|
2008-03-04 08:40:41 +01:00
|
|
|
}
|
2008-05-20 22:48:44 +02:00
|
|
|
else if (metaonly) printf("metadata for %s\n",argv[optind]);
|
2008-02-14 23:38:23 +01:00
|
|
|
else printf("decoding %s\n",argv[optind]);
|
2008-01-31 07:15:03 +01:00
|
|
|
}
|
2009-06-24 22:10:07 +02:00
|
|
|
if (!play && !adxencd && !oggenc && !batchvar) {
|
2008-03-11 02:27:59 +01:00
|
|
|
char description[1024];
|
|
|
|
description[0]='\0';
|
|
|
|
describe_vgmstream(s,description,1024);
|
|
|
|
printf("%s\n",description);
|
|
|
|
}
|
2008-02-14 23:38:23 +01:00
|
|
|
if (metaonly) {
|
|
|
|
close_vgmstream(s);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-01-31 07:15:03 +01:00
|
|
|
|
2008-04-15 17:49:47 +02:00
|
|
|
buf = malloc(BUFSIZE*sizeof(sample)*s->channels);
|
|
|
|
|
2008-05-16 22:28:36 +02:00
|
|
|
len = get_vgmstream_play_samples(loop_count,fade_seconds,fade_delay_seconds,s);
|
2010-02-26 17:36:25 +01:00
|
|
|
if (!play && !adxencd && !oggenc && !batchvar) printf("samples to play: %d (%.4lf seconds)\n",len,(double)len/s->sample_rate);
|
2008-05-16 22:28:36 +02:00
|
|
|
fade_samples = fade_seconds * s->sample_rate;
|
2008-01-31 07:15:03 +01:00
|
|
|
|
2008-02-04 11:34:18 +01:00
|
|
|
/* slap on a .wav header */
|
|
|
|
make_wav_header((uint8_t*)buf, len, s->sample_rate, s->channels);
|
|
|
|
fwrite(buf,1,0x2c,outfile);
|
|
|
|
|
2008-05-05 00:03:10 +02:00
|
|
|
/* decode forever */
|
2008-02-06 21:53:10 +01:00
|
|
|
while (forever) {
|
|
|
|
render_vgmstream(buf,BUFSIZE,s);
|
|
|
|
fwrite(buf,sizeof(sample)*s->channels,BUFSIZE,outfile);
|
|
|
|
}
|
|
|
|
|
2008-05-05 00:03:10 +02:00
|
|
|
/* decode */
|
2008-01-31 07:15:03 +01:00
|
|
|
for (i=0;i<len;i+=BUFSIZE) {
|
|
|
|
int toget=BUFSIZE;
|
|
|
|
if (i+BUFSIZE>len) toget=len-i;
|
|
|
|
render_vgmstream(buf,toget,s);
|
2008-02-04 13:11:40 +01:00
|
|
|
|
|
|
|
if (s->loop_flag && fade_samples > 0) {
|
|
|
|
int samples_into_fade = i - (len - fade_samples);
|
|
|
|
if (samples_into_fade + toget > 0) {
|
|
|
|
int j,k;
|
|
|
|
for (j=0;j<toget;j++,samples_into_fade++) {
|
|
|
|
if (samples_into_fade > 0) {
|
|
|
|
double fadedness = (double)(fade_samples-samples_into_fade)/fade_samples;
|
|
|
|
for (k=0;k<s->channels;k++) {
|
|
|
|
buf[j*s->channels+k] = buf[j*s->channels+k]*fadedness;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-04 11:48:04 +01:00
|
|
|
fwrite(buf,sizeof(sample)*s->channels,toget,outfile);
|
2008-01-31 07:15:03 +01:00
|
|
|
}
|
2008-05-19 05:58:15 +02:00
|
|
|
|
|
|
|
fclose(outfile); outfile = NULL;
|
|
|
|
|
2008-05-20 20:56:44 +02:00
|
|
|
#ifdef PROFILE_STREAMFILE
|
|
|
|
{
|
2008-05-20 22:17:36 +02:00
|
|
|
int i,j;
|
|
|
|
size_t total_bytes_read = 0;
|
2008-05-20 20:56:44 +02:00
|
|
|
for (i=0;i<s->channels;i++) {
|
|
|
|
size_t bytes_read = get_streamfile_bytes_read(s->ch[i].streamfile);
|
|
|
|
size_t file_size = get_streamfile_size(s->ch[i].streamfile);
|
2008-07-03 22:08:02 +02:00
|
|
|
int error_count = get_streamfile_error_count(s->ch[i].streamfile);
|
2008-05-20 22:17:36 +02:00
|
|
|
int already_reported = 0;
|
|
|
|
|
|
|
|
/* see if we've reported this STREAMFILE already */
|
|
|
|
for (j=i-1;!already_reported && j>=0;j--) {
|
|
|
|
if (s->ch[j].streamfile == s->ch[i].streamfile) {
|
|
|
|
already_reported=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (already_reported) continue;
|
|
|
|
|
|
|
|
total_bytes_read += bytes_read;
|
2008-07-03 22:08:02 +02:00
|
|
|
fprintf(stderr,"ch%d: %lf%% (%d bytes read, file is %d bytes) %d errors\n",i,
|
|
|
|
bytes_read*100.0/file_size,bytes_read,file_size,error_count);
|
2008-05-20 20:56:44 +02:00
|
|
|
}
|
2008-05-20 22:17:36 +02:00
|
|
|
fprintf(stderr,"total bytes read: %d\n",total_bytes_read);
|
2008-05-20 20:56:44 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-19 05:58:15 +02:00
|
|
|
if (reset_outfilename) {
|
|
|
|
outfile = fopen(reset_outfilename,"wb");
|
|
|
|
if (!outfile) {
|
|
|
|
fprintf(stderr,"failed to open %s for output\n",reset_outfilename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* slap on a .wav header */
|
|
|
|
make_wav_header((uint8_t*)buf, len, s->sample_rate, s->channels);
|
|
|
|
fwrite(buf,1,0x2c,outfile);
|
|
|
|
|
|
|
|
reset_vgmstream(s);
|
2008-05-19 19:20:35 +02:00
|
|
|
|
|
|
|
/* these manipulations are undone by reset */
|
|
|
|
|
|
|
|
/* force only if there aren't already loop points */
|
|
|
|
if (force_loop && !s->loop_flag) {
|
|
|
|
/* this requires a bit more messing with the VGMSTREAM than I'm
|
|
|
|
* comfortable with... */
|
|
|
|
s->loop_flag=1;
|
|
|
|
s->loop_start_sample=0;
|
|
|
|
s->loop_end_sample=s->num_samples;
|
|
|
|
s->loop_ch=calloc(s->channels,sizeof(VGMSTREAMCHANNEL));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* force even if there are loop points */
|
|
|
|
if (really_force_loop) {
|
|
|
|
if (!s->loop_flag) s->loop_ch=calloc(s->channels,sizeof(VGMSTREAMCHANNEL));
|
|
|
|
s->loop_flag=1;
|
|
|
|
s->loop_start_sample=0;
|
|
|
|
s->loop_end_sample=s->num_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ignore_loop) s->loop_flag=0;
|
|
|
|
|
2008-05-19 05:58:15 +02:00
|
|
|
/* decode */
|
|
|
|
for (i=0;i<len;i+=BUFSIZE) {
|
|
|
|
int toget=BUFSIZE;
|
|
|
|
if (i+BUFSIZE>len) toget=len-i;
|
|
|
|
render_vgmstream(buf,toget,s);
|
|
|
|
|
|
|
|
if (s->loop_flag && fade_samples > 0) {
|
|
|
|
int samples_into_fade = i - (len - fade_samples);
|
|
|
|
if (samples_into_fade + toget > 0) {
|
|
|
|
int j,k;
|
|
|
|
for (j=0;j<toget;j++,samples_into_fade++) {
|
|
|
|
if (samples_into_fade > 0) {
|
|
|
|
double fadedness = (double)(fade_samples-samples_into_fade)/fade_samples;
|
|
|
|
for (k=0;k<s->channels;k++) {
|
|
|
|
buf[j*s->channels+k] = buf[j*s->channels+k]*fadedness;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fwrite(buf,sizeof(sample)*s->channels,toget,outfile);
|
|
|
|
}
|
|
|
|
fclose(outfile); outfile = NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-31 07:15:03 +01:00
|
|
|
close_vgmstream(s);
|
2008-12-24 10:11:58 +01:00
|
|
|
free(buf);
|
2008-02-15 17:26:29 +01:00
|
|
|
|
|
|
|
return 0;
|
2008-01-31 07:15:03 +01:00
|
|
|
}
|