shithub: dumb

ref: 52c579491bf9c0e188a1713fdc17155bcd53c997
dir: /examples/moddec.c/

View raw version
#include <dumb.h>

#pragma lib "./libdumb.a$O"

#define MAX(a,b) ((a)>(b)?(a):(b))

enum {
	Nsamp = 4096,
};

static uchar b[Nsamp*2*2];

static void
usage(void)
{
	fprint(2, "usage: %s [-f FREQ] [-r RESAMPLER] [-s SECONDS]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	DUMB_IT_SIGRENDERER *itren;
	DUH_SIGRENDERER *ren;
	int n, sz, r, resamp;
	double pos, freq, ratio;
	sample_t **samp;
	char *data, *t;
	long nsamp;
	DUH *f;

	setfcr(getfcr() & ~(FPINVAL|FPOVFL));

	freq = 44100.0;
	resamp = DUMB_RQ_CUBIC;
	pos = 0.0;
	ARGBEGIN{
	case 'f':
		freq = atoi(EARGF(usage()));
		if(freq < 1000)
			sysfatal("invalid frequency %d", (int)freq);
		break;
	case 'r':
		resamp = atoi(EARGF(usage()));
		if(resamp < 0)
			sysfatal("invalid resampler %d", resamp);
		break;
	case 's':
		pos = atof(EARGF(usage()));
		break;
	default:
		usage();
	}ARGEND;

	if(argc != 0)
		usage();

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

	ratio = 65536.0/freq;

	if((f = dumb_read_any(dumbfile_open_memory(data, sz), 0, 0)) == nil)
		sysfatal("unknown/invalid mod");
	if((t = (char*)duh_get_tag(f, "TITLE")) != nil && *t)
		fprint(2, "%s\n", t);
	fprint(2, "duration: %g\n", duh_get_length(f)*ratio/100000.0);
	ren = duh_start_sigrenderer(f, 0, 2, pos*100000.0/ratio);
	itren = duh_get_it_sigrenderer(ren);
	dumb_it_set_loop_callback(itren, dumb_it_callback_terminate, nil);
	dumb_it_set_xm_speed_zero_callback(itren, dumb_it_callback_terminate, nil);
	dumb_it_set_resampling_quality(itren, resamp);
	if(pos > 0.0)
		fprint(2, "time: %g\n", pos);

	n = 0;
	for(;;){
		n = duh_render_int(ren, &samp, &nsamp, 16, 0, 1.0, ratio, MAX(n, Nsamp), b);
		if(n <= 0)
			break;
		if(write(1, b, n*2*2) != n*2*2)
			break;
	}

	exits(nil);
}