shithub: neindaw

ref: d110794e66d44f71191c74a10a079a1951a893a6
dir: /repeat/repeat.c/

View raw version
#include <u.h>
#include <libc.h>

static void
usage(void)
{
	print("usage: %s -l len_frames [-t times] [-c channels] [-f file]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	int length, chan, t, f, r, n;
	u8int *b;

	length = 0;
	chan = 1;
	t = -1;
	f = 0;

	ARGBEGIN{
	case 'l':
		length = atoi(EARGF(usage()));
		break;
	case 't':
		t = atoi(EARGF(usage()));
		break; 
	case 'c':
		chan = atoi(EARGF(usage()));
		break;
	case 'f':
		if ((f = open(EARGF(usage()), OREAD)) < 0)
			sysfatal("%r");
		break;
	default:
		usage();
	}ARGEND

	if (length < 1)
		sysfatal("invalid length: %d frames", length);
	if (chan < 1)
		sysfatal("invalid number of channels: %d", chan);

	length *= chan * 4;
	if ((b = malloc(length)) == nil)
		sysfatal("couldn't allocate %d bytes", length);

	n = 0;
	while (t != 0) {
		if (f != 0 || n == 0) {
			seek(f, 0, 0);

			for (n = 0; n < length; n += r) {
				r = read(f, b+n, length-n);
				if (r < 0)
					exits("read failed");
				else if (r == 0)
					break;
			}
			memset(b+n, 0, length-n);
		}

		if (write(1, b, length) != length)
			break;

		if (t > 0)
			t--;
	}

	exits(nil);
}