shithub: neindaw

Download patch

ref: 9acd599496f6e8013cc2ccd18d105de17f49fca8
parent: b56d23c78ede1dc4f74b32bc0f6092d87e79553e
author: Sigrid Haflínudóttir <[email protected]>
date: Tue Jan 21 12:05:09 EST 2020

add daw/sample command, which converts raw "data" into a finite sample file

--- /dev/null
+++ b/sample/mkfile
@@ -1,0 +1,12 @@
+</$objtype/mkfile
+
+TARG=\
+	sample\
+
+BIN=/$objtype/bin/daw
+CFLAGS=$CFLAGS
+OFILES=sample.$O
+
+default:V:	all
+
+</sys/src/cmd/mkone
--- /dev/null
+++ b/sample/sample.c
@@ -1,0 +1,72 @@
+#include <u.h>
+#include <libc.h>
+
+static void
+usage(void)
+{
+	print("usage: %s [-b buf_frames] [-c channels]\n", argv0);
+	exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+	int bufsz, bufframes, framesz, chan, n, r, end, i;
+	float f;
+	u8int *b;
+
+	bufframes = 2048;
+	chan = 1;
+
+	ARGBEGIN{
+	case 'b':
+		bufframes = atoi(EARGF(usage()));
+		break;
+	case 'c':
+		chan = atoi(EARGF(usage()));
+		break;
+	default:
+		usage();
+	}ARGEND
+
+	if (bufframes < 1)
+		sysfatal("invalid buffer size: %d frames", bufframes);
+	if (chan < 1)
+		sysfatal("invalid number of channels: %d", chan);
+
+	framesz = chan * 4;
+	bufsz = bufframes * framesz;
+	b = malloc(bufsz);
+
+	n = 0;
+	for (;;) {
+		r = read(0, b+n, bufsz-n);
+		if (r < 0)
+			exits("read failed");
+		else if (r == 0)
+			break;
+		n += r;
+
+		end = n & ~(framesz-1);
+		if (end < 1)
+			continue;
+
+		for (; end > 0; end -= framesz) {
+			for (i = 0; i < chan; i++) {
+				f = *(float*)(b + end - framesz - i*4);
+				if (f != 0)
+					goto out;
+			}
+		}
+out:
+		if (end == 0)
+			break;
+
+		if (write(1, b, end) != end)
+			exits("write failed");
+		n -= end;
+		memmove(b, b+end, n);
+	}
+
+	exits(nil);
+}