shithub: dumb

ref: 0120570de7ff3b444b08aa4fa628c5484f3731d1
dir: /examples/moddec.c/

View raw version
#include <dumb.h>

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

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define RATIO (65536.0/44100.0)

enum {
	Nsamp = 4096,
};

static uchar b[Nsamp*2*2];

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

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

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

	noaudio = 0;
	pos = 0.0;
	ARGBEGIN{
	case 'n':
		noaudio = 1;
		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;
	}

	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);
	if(noaudio)
		exits(nil);
	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, DUMB_RQ_CUBIC);
	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);
}