shithub: ft2play

ref: 1933d1c0d5a61983615cb3a1ddcafa50874a2989
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 / 44100.0){
		n = pos * 44100;
		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);
}