shithub: util

Download patch

ref: a03388e2a74912510f3c78f0c72f622e59364dde
parent: 12a71134893f718065e83ec1ece8e5ddff90eb34
author: glenda <glenda@kingship>
date: Tue Aug 15 02:00:19 EDT 2023

stuff

--- /dev/null
+++ b/beep.c
@@ -1,0 +1,61 @@
+// produce waveform for a "beep"
+#include <u.h>
+#include <libc.h>
+
+void
+usage(void)
+{
+	fprint(2, "usage: %s [-f hz] [-d decay] [-l samples] [-s slide] [-v volume]\n", argv0);
+	exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+	char *buffer;
+	short s;
+	int i;
+	double hz = 440;
+	double decay = 0.9998;
+	int samples = 11025;
+	double slide = 1.0001;
+	double volume = 0.33;
+	double *table;
+
+	ARGBEGIN{
+	case 'f':
+		hz = atof(EARGF(usage()));
+		break;
+	case 'd':
+		decay = atof(EARGF(usage()));
+		break;
+	case 'l':
+		samples = atoi(EARGF(usage()));
+		break;
+	case 's':
+		slide = atof(EARGF(usage()));
+		break;
+	case 'v':
+		volume = atof(EARGF(usage()))/100.0;
+		break;
+	default:
+		usage();
+	}ARGEND
+
+	volume = volume < 0? 0: volume > 1? 1: volume;
+
+	table = malloc(6283 * sizeof(double));
+	for (i = 0; i < 6283; i++) {
+		table[i] = cos(i/1000.0);
+	}
+
+	buffer = malloc(samples * 4);
+
+	for (i = 0; i < samples; i++) {
+		s = table[(int)(fmod(((double)i/44100*2*PI*(hz-pow(slide,i))),6.283)*1000.0)] * 32767 * volume * pow(decay,i);
+		buffer[i*4+0] = buffer[i*4+2] = s & 0xFF;
+		buffer[i*4+1] = buffer[i*4+3] = (s >> 8) & 0xFF;
+	}
+
+	write(1, buffer, samples * 4);
+}
--- /dev/null
+++ b/edges.c
@@ -1,0 +1,47 @@
+/*
+ * find and draw edges in images to produce coloring pages
+ * ex:
+ *     png -t < image.png | edges | topng > imageedges.png
+ */
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+
+void main() {
+	Memimage *i;
+	Memimage *o;
+	int x, y, c;
+	int nchan;
+	int width;
+	int height;
+
+	i = readmemimage(0);
+	o = allocmemimage(i->r, strtochan("k8"));
+	memfillcolor(o, DBlack);
+	nchan = i->nchan;
+	width = Dx(i->r);
+	height = Dy(i->r);
+	for (y = 1; y < (height-1); y++)
+		for (x = 1; x < (width-1); x++)
+			for (c = 0; c < nchan; c++) {
+				*(byteaddr(o, Pt(x, y))) += (255 - (
+									  (*(byteaddr(i, Pt(x-1, y-1))+c))*(-1)
+									+ (*(byteaddr(i, Pt(x-1, y+0))+c))*(-2)
+									+ (*(byteaddr(i, Pt(x-1, y+1))+c))*(-1)
+									+ (*(byteaddr(i, Pt(x+1, y-1))+c))*(1)
+									+ (*(byteaddr(i, Pt(x+1, y+0))+c))*(2)
+									+ (*(byteaddr(i, Pt(x+1, y+1))+c))*(1)
+									+ (*(byteaddr(i, Pt(x-1, y-1))+c))*(-1)
+									+ (*(byteaddr(i, Pt(x+0, y-1))+c))*(-2)
+									+ (*(byteaddr(i, Pt(x+1, y-1))+c))*(-1)
+									+ (*(byteaddr(i, Pt(x-1, y+1))+c))*(1)
+									+ (*(byteaddr(i, Pt(x+0, y+1))+c))*(2)
+									+ (*(byteaddr(i, Pt(x+1, y+1))+c))*(1)
+									))/nchan;
+			}
+	freememimage(i);
+	writememimage(1, o);
+	freememimage(o);
+}
+
--- /dev/null
+++ b/flip.c
@@ -1,0 +1,174 @@
+#include <u.h>
+#include <libc.h>
+
+Rune runes[][2] = {
+	{L'a',  L'ɐ'},
+	{L'b',  L'q'},
+	{L'c',  L'ɔ'},
+	{L'd',  L'p'},
+	{L'e',  L'ǝ'},
+	{L'f',  L'ɟ'},
+	{L'g',  L'ƃ'},
+	{L'h',  L'ɥ'},
+	{L'i',  L'ᴉ'},
+	{L'j',  L'ɾ'},
+	{L'k',  L'ʞ'},
+	{L'l',  L'˥'},
+	{L'm',  L'ɯ'},
+	{L'n',  L'u'},
+	{L'o',  L'o'},
+	{L'p',  L'd'},
+	{L'q',  L'b'},
+	{L'r',  L'ɹ'},
+	{L's',  L's'},
+	{L't',  L'ʇ'},
+	{L'u',  L'n'},
+	{L'v',  L'ʌ'},
+	{L'w',  L'ʍ'},
+	{L'x',  L'x'},
+	{L'y',  L'ʎ'},
+	{L'z',  L'z'},
+	{L'A',  L'∀'},
+	{L'B',  L'q'},
+	{L'C',  L'Ɔ'},
+	{L'D',  L'p'},
+	{L'E',  L'Ǝ'},
+	{L'F',  L'Ⅎ'},
+	{L'G',  L'פ'},
+	{L'H',  L'H'},
+	{L'I',  L'I'},
+	{L'J',  L'ſ'},
+	{L'K',  L'ʞ'},
+	{L'L',  L'˥'},
+	{L'M',  L'W'},
+	{L'N',  L'N'},
+	{L'O',  L'O'},
+	{L'P',  L'Ԁ'},
+	{L'Q',  L'Q'},
+	{L'R',  L'ʁ'},
+	{L'S',  L'S'},
+	{L'T',  L'┴'},
+	{L'U',  L'∩'},
+	{L'V',  L'Λ'},
+	{L'W',  L'M'},
+	{L'X',  L'X'},
+	{L'Y',  L'⅄'},
+	{L'Z',  L'Z'},
+	{L'\'', ','},
+	{L'"',  L','},
+	{L',',  L'\''},
+	{L'.',  L'˙'},
+	{L'!',  L'¡'},
+	{L'?',  L'¿'},
+	{L'_',  L'‾'},
+	{L'1',  L'Ɩ'},
+	{L'2',  L'ᄅ'},
+	{L'3',  L'Ɛ'},
+	{L'4',  L'ㄣ'},
+	{L'5',  L'ϛ'},
+	{L'6',  L'9'},
+	{L'7',  L'ㄥ'},
+	{L'8',  L'8'},
+	{L'9',  L'6'},
+	{L'0',  L'0'},
+	{L'ɐ',  L'a'},
+	{L'ɔ',  L'c'},
+	{L'ǝ',  L'e'},
+	{L'ɟ',  L'f'},
+	{L'ƃ',  L'g'},
+	{L'ɥ',  L'h'},
+	{L'ᴉ',  L'i'},
+	{L'ɾ',  L'j'},
+	{L'ʞ',  L'k'},
+	{L'˥',  L'l'},
+	{L'ɯ',  L'm'},
+	{L'ʁ',  L'r'},
+	{L'ʇ',  L't'},
+	{L'ʌ',  L'v'},
+	{L'ʍ',  L'w'},
+	{L'ʎ',  L'y'},
+	{L'∀',  L'A'},
+	{L'Ɔ',  L'C'},
+	{L'Ǝ',  L'E'},
+	{L'Ⅎ',  L'F'},
+	{L'פ',  L'G'},
+	{L'ſ',  L'J'},
+	{L'Ԁ',  L'P'},
+	{L'┴',  L'T'},
+	{L'∩',  L'U'},
+	{L'Λ',  L'V'},
+	{L'⅄',  L'Y'},
+	{L'˙',  L'.'},
+	{L'¡',  L'!'},
+	{L'¿',  L'?'},
+	{L'‾',  L'_'},
+	{L'Ɩ',  L'1'},
+	{L'ᄅ',  L'2'},
+	{L'Ɛ',  L'3'},
+	{L'ㄣ',  L'4'},
+	{L'ϛ',  L'5'},
+	{L'ㄥ',  L'7'},
+	{L'(',  L')'},
+	{L')',  L'('},
+	{L'[',  L']'},
+	{L']',  L'['},
+	{L'<',  L'>'},
+	{L'>',  L'<'},
+	{0, 0},
+};
+
+Rune
+flip(Rune in){
+	int i = 0;
+
+	while(runes[i][0]){
+		if(runes[i][0] == in)
+			return runes[i][1];
+		i++;
+	}
+
+	return in;
+}
+
+void
+main(int argc, char **argv){
+	Rune rune;
+	char buf[8192 + UTFmax];
+	char *ptr;
+	Rune out[8192 + UTFmax];
+	int r, i, j, l;
+
+	while((r = read(0, buf, sizeof buf - UTFmax)) != 0) {
+		if (r < 0)
+			sysfatal("read: %r");
+
+		buf[r] = '\0';
+
+		ptr = buf;
+		while(r > 0){
+			while(*ptr == ' ' || *ptr == '	'){
+				print("%c", *ptr++);
+				r--;
+			}
+
+			l = strcspn(ptr, "\r\n");
+			j = utfnlen(ptr, l);
+			out[j--] = L'\0';
+			while(j >= 0){
+				i = chartorune(&rune, ptr);
+				if(i == 1 && rune == Runeerror)
+					sysfatal("invalid utf-8");
+				r -= i;
+				ptr += i;
+				rune = flip(rune);
+				out[j--] = rune;
+			}
+
+			print("%S", out);
+			while(*ptr == '\r' || *ptr == '\n'){
+				print("%c", *ptr++);
+				r--;
+			}
+		}
+	}
+}
--- /dev/null
+++ b/iching.c
@@ -1,0 +1,54 @@
+#include <u.h>
+#include <libc.h>
+
+void
+main() {
+	int fd;
+	int i, j;
+	uchar bits[3];
+	int lines[6];
+	int linesproc[6];
+	long value;
+	char bytes[3];
+	uchar table[64] = { 2, 23, 8, 20, 16, 35, 45, 12,
+						15, 52, 39, 53, 62, 56, 31, 33,
+						7, 4, 29, 59, 40, 64, 47, 6,
+						46, 18, 48, 57, 32, 50, 28, 44,
+						24, 27, 3, 42, 51, 21, 17, 25,
+						36, 22, 63, 37, 55, 30, 49, 13,
+						19, 41, 60, 61, 54, 38, 58, 10,
+						11, 26, 5, 9, 34, 14, 43, 1 };
+	Rune first;
+	Rune changed;
+
+	fd = open("/dev/random", OREAD);
+	if (fd < 0)
+		sysfatal("open");
+
+	value = 0;
+	for (i = 0; i < 6; i++) {
+		if (readn(fd, bits, 3) != 3)
+			sysfatal("read");
+
+		lines[i] = 0;
+		for (j = 0; j < 3; j++)
+			lines[i] += bits[j] & 1? 2: 3;
+		print("%d ", lines[i]);
+		linesproc[i] = lines[i] & 1;
+		value += linesproc[i] == 1? 1 << (5 - i): 0;
+	}
+	print("\n");
+	value = 0x4DC0 - 1 + table[value];
+	first = value;
+
+	value = 0;
+	for (i = 0; i < 6; i++) {
+		if ((lines[i] % 3) == 0)
+			linesproc[i] = linesproc[i] == 1? 0: 1;
+		value += linesproc[i] == 1? 1 << (5 - i): 0;
+	}
+	value = 0x4DC0 - 1 + table[value];
+	changed = value;
+
+	print("%C %C\n", first, changed);
+}