shithub: ft2play

ref: 9f77a97152139c86ed8c86e8aa45060084314c66
dir: /ft2dec.c/

View raw version
#include "common.h"
#include "pmplay.h"
#include "pmp_mix.h"

enum
{
	Nsamp = 4096,
};

static void
usage(void)
{
	fprint(2, "usage: %s [-a amp] [-f freq] [-s seconds] [-v volume] <file.(xm|mod|ft) >/dev/audio\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	int amp, frq, vol, n, r, sz;
	s16int s[Nsamp*2];
	double pos;
	u8int *b;

	amp = 10;
	frq = 44100;
	pos = 0;
	vol = 256;
	ARGBEGIN{
	case 'a':
		amp = atoi(EARGF(usage()));
		assert(amp >= 1 && amp <= 32);
		break;
	case 'f':
		frq = atoi(EARGF(usage()));
		assert(frq >= 8000 && frq <= 96000);
		break;
	case 's':
		pos = atof(EARGF(usage()));
		assert(pos >= 0.0);
		break;
	case 'v':
		vol = atoi(EARGF(usage()));
		assert(vol >= 0 && vol <= 256);
		break;
	default:
		usage();
	}ARGEND

	if(!initMusic(frq, Nsamp, true, true))
		sysfatal("initMusic");
	setAmp(amp);
	setMasterVol(vol);

	sz = 32768;
	b = nil;
	for(n = 0;; n += r){
		if(sz-n < 65536){
			sz *= 2;
			if((b = realloc(b, sz)) == nil)
				sysfatal("memory");
		}
		if((r = read(0, b+n, sz-n)) < 0)
			sysfatal("%r");
		if(r == 0)
			break;
	}

	if(!loadMusicFromData(b, n))
		sysfatal("loadMusicFromData");
	if(!startMusic())
		sysfatal("startMusic");
	startPlaying();
	if(pos > 0.0)
		fprint(2, "time: %g\n", pos);
	for(; pos > 0.0; pos -= (double)n / frq){
		n = pos * frq;
		if(n < 1)
			break;
		if(n > Nsamp)
			n = Nsamp;
		mix_UpdateBuffer(s, n);
		if(dump_EndOfTune(song.len-1))
			break;
	}
	for(;;){
		if(dump_EndOfTune(song.len-1))
			break;
		mix_UpdateBuffer(s, Nsamp);
		write(1, s, sizeof(s));
	}

	stopMusic();
	freeMusic();

	exits(nil);
}