shithub: neindaw

Download patch

ref: 9068c05955073b4550f15380d0a2885d211f7e7e
parent: 601589dddd7b2c48fe22223d25ce8a969458016d
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Jan 23 11:21:38 EST 2020

add "repeat" util to loop over raw data

diff: cannot open b/repeat//null: file does not exist: 'b/repeat//null'
--- a/mkfile
+++ b/mkfile
@@ -6,6 +6,7 @@
 CMDS=\
 	cfg\
 	dsp\
+	repeat\
 	sample\
 
 BIN=/$objtype/bin/daw
--- /dev/null
+++ b/repeat/mkfile
@@ -1,0 +1,12 @@
+</$objtype/mkfile
+
+TARG=\
+	repeat\
+
+BIN=/$objtype/bin/daw
+CFLAGS=$CFLAGS
+OFILES=repeat.$O
+
+default:V:	all
+
+</sys/src/cmd/mkone
--- /dev/null
+++ b/repeat/repeat.c
@@ -1,0 +1,72 @@
+#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);
+}