@amritk/nish-aarch64-linux 0.39.0 → 0.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/INSTALL.md +79 -133
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +112 -33
  4. package/package.json +1 -1
  5. package/runtime/nish.d.ts +21 -17
  6. package/runtime/nish.h +10 -0
  7. package/runtime/page-frame.d.ts +123 -0
  8. package/runtime/page-frame.mjs +736 -0
  9. package/runtime/page-game.d.ts +132 -0
  10. package/runtime/page-game.mjs +738 -0
  11. package/runtime/runtime-aes.c +85 -8
  12. package/runtime/runtime-bytes.c +88 -4
  13. package/runtime/runtime-net.c +32 -0
  14. package/runtime/runtime-sandbox.c +55 -28
  15. package/runtime/runtime-uring.c +1194 -0
  16. package/runtime/runtime-wasm.c +422 -0
  17. package/runtime/vite.mjs +1 -1
  18. package/scripts/build.sh +22 -6
  19. package/std/README.md +74 -87
  20. package/std/array.ts +36 -3
  21. package/std/collections.ts +51 -0
  22. package/std/crypto/hmac.ts +37 -10
  23. package/std/crypto/p256.ts +1388 -2664
  24. package/std/crypto/pki.ts +116 -4
  25. package/std/crypto/sha256.ts +34 -6
  26. package/std/crypto/x509.ts +7 -0
  27. package/std/host/game.ts +273 -0
  28. package/std/input.ts +56 -0
  29. package/std/json-tokenizer.ts +325 -112
  30. package/std/json-typed.ts +43 -8
  31. package/std/json-value.ts +144 -3
  32. package/std/json.ts +63 -8
  33. package/std/net/fetch.ts +22 -4
  34. package/std/net/http-server.ts +3382 -0
  35. package/std/net/http1-client-codec.ts +6 -2
  36. package/std/net/http1-codec.ts +137 -12
  37. package/std/net/http1-server.ts +83 -29
  38. package/std/net/http2.ts +47 -14
  39. package/std/net/http3-server.ts +29 -3
  40. package/std/net/http3.ts +180 -7
  41. package/std/net/prefork.ts +49 -13
  42. package/std/net/quic-recovery.ts +240 -9
  43. package/std/net/quic-stream.ts +21 -1
  44. package/std/net/quic.ts +98 -4
  45. package/std/net/router.ts +378 -31
  46. package/std/net/serve.ts +873 -65
  47. package/std/net/tls/verify.ts +19 -1
  48. package/std/net/url.ts +83 -35
  49. package/std/net/websocket.ts +74 -1
  50. package/std/net/webtransport.ts +7 -1
  51. package/std/raster.ts +837 -0
  52. package/std/release.ts +593 -0
  53. package/std/synth.ts +143 -0
  54. package/std/temporal.ts +3091 -0
  55. package/std/threads.ts +298 -0
  56. package/std/yaml-json.ts +368 -28
  57. package/std/yaml.ts +122 -27
package/std/synth.ts ADDED
@@ -0,0 +1,143 @@
1
+ /**
2
+ * `nish/synth` — short sounds made from nothing: square, triangle and noise
3
+ * voices with a pitch slide and a decaying envelope, mixed into an array of
4
+ * `f64` samples. Nothing is sampled or copied; a sound is arithmetic.
5
+ *
6
+ * A sound starts as silence of its length and gains voices one at a time,
7
+ * each added into the samples already there:
8
+ *
9
+ * import { SYNTH_SQUARE, SYNTH_TRIANGLE, synthSemitone, synthSilence, synthTone } from "nish/synth";
10
+ *
11
+ * const RATE: i32 = 22050;
12
+ * const out: f64[] = synthSilence(0.2, RATE);
13
+ * synthTone(out, RATE, 0.0, 0.2, 440.0, 220.0, SYNTH_SQUARE, 0.25, 8.0);
14
+ * synthTone(out, RATE, 0.05, 0.1, synthSemitone(440.0, 7), synthSemitone(440.0, 7), SYNTH_TRIANGLE, 0.2, 12.0);
15
+ *
16
+ * The samples are not clipped: a voice stays inside `-volume..volume`, so
17
+ * voices whose volumes sum to at most 1 stay inside `-1..1`, and what plays
18
+ * them (a WAV writer, a page's audio buffer) clips the rest. The sample rate
19
+ * is the caller's, passed to every function, so one program can make sounds
20
+ * for a 22050 Hz file and a 48000 Hz page alike.
21
+ *
22
+ * Pure: no capability, no I/O and no clock. The noise voice is a fixed
23
+ * xorshift sequence, so the same call gives the same samples every time.
24
+ */
25
+
26
+ /** A square wave: +1 for the first half of each period, -1 for the second. */
27
+ export const SYNTH_SQUARE: i32 = 0
28
+
29
+ /** A triangle wave: a straight rise from -1 to +1 and a straight fall back. */
30
+ export const SYNTH_TRIANGLE: i32 = 1
31
+
32
+ /** Noise: a fresh random level each period of the pitch, so a falling pitch rumbles. */
33
+ export const SYNTH_NOISE: i32 = 2
34
+
35
+ /** The noise voice's starting state. */
36
+ const SYNTH_NOISE_SEED: i32 = 22695477
37
+
38
+ /** A position or length in samples past this is refused rather than converted. */
39
+ const SYNTH_SAMPLE_LIMIT: f64 = 2147483647.0
40
+
41
+ /**
42
+ * `seconds` of silence: an array of zero samples, one at least.
43
+ *
44
+ * @param seconds - how long; zero, negative or NaN is one sample
45
+ * @param sampleRate - samples a second
46
+ * @returns `seconds × sampleRate` samples of 0.0, at least one
47
+ */
48
+ export const synthSilence = (seconds: f64, sampleRate: i32): f64[] => {
49
+ const want: f64 = seconds * toF64(sampleRate)
50
+ const n: i32 = want >= 1.0 && want < SYNTH_SAMPLE_LIMIT ? toI32(want) : 1
51
+ const out: f64[] = new Array<f64>(n)
52
+ out.fill(0.0)
53
+ return out
54
+ }
55
+
56
+ /** The next value of the noise voice's xorshift generator. */
57
+ const synthXorshift = (x: i32): i32 => {
58
+ let y: i32 = x ^ (x << 13)
59
+ y = y ^ (y >> 17)
60
+ return y ^ (y << 5)
61
+ }
62
+
63
+ /**
64
+ * Add a voice into `out` from `start` seconds for `length` seconds. Its pitch
65
+ * slides from `f0` to `f1` Hz on a log scale, and its loudness starts at
66
+ * `volume` and falls by a factor of e every `1 / decay` seconds. A 2 ms attack
67
+ * and a 5 ms release keep the ends from clicking. The voice stops at the end
68
+ * of `out`; it never grows the array.
69
+ *
70
+ * Nothing is added for a sample rate, start or length that is not positive
71
+ * (a start of 0 is fine), for a frequency that is not positive, or for a
72
+ * start or length too long to count in samples.
73
+ *
74
+ * @param out - the samples to add into
75
+ * @param sampleRate - samples a second that `out` is at
76
+ * @param start - where the voice begins, in seconds from the first sample
77
+ * @param length - how long the voice lasts, in seconds
78
+ * @param f0 - the starting pitch in Hz
79
+ * @param f1 - the ending pitch in Hz
80
+ * @param wave - `SYNTH_SQUARE`, `SYNTH_TRIANGLE` or `SYNTH_NOISE`; any other value is noise
81
+ * @param volume - the starting peak level; a negative level inverts the wave
82
+ * @param decay - how fast the level falls, per second; 0 holds it
83
+ */
84
+ export const synthTone = (
85
+ out: f64[],
86
+ sampleRate: i32,
87
+ start: f64,
88
+ length: f64,
89
+ f0: f64,
90
+ f1: f64,
91
+ wave: i32,
92
+ volume: f64,
93
+ decay: f64
94
+ ): void => {
95
+ const rate: f64 = toF64(sampleRate)
96
+ if (!(rate > 0.0 && start >= 0.0 && length > 0.0 && f0 > 0.0 && f1 > 0.0)) {
97
+ return
98
+ }
99
+ if (!(start * rate < SYNTH_SAMPLE_LIMIT && length * rate < SYNTH_SAMPLE_LIMIT)) {
100
+ return
101
+ }
102
+ const from: i32 = toI32(start * rate)
103
+ const n: i32 = toI32(length * rate)
104
+ const ratio: f64 = Math.log(f1 / f0)
105
+ let phase: f64 = 0.0
106
+ let noise: i32 = SYNTH_NOISE_SEED
107
+ let held: f64 = 0.0
108
+ for (let i: i32 = 0; i < n && from + i < toI32(out.length); i++) {
109
+ const t: f64 = toF64(i) / rate
110
+ const f: f64 = f0 * Math.exp((ratio * toF64(i)) / toF64(n))
111
+ const before: f64 = phase
112
+ phase = phase + f / rate
113
+ if (phase >= 1.0) {
114
+ phase = phase - Math.floor(phase)
115
+ }
116
+ let v: f64 = 0.0
117
+ if (wave === SYNTH_SQUARE) {
118
+ v = phase < 0.5 ? 1.0 : -1.0
119
+ } else if (wave === SYNTH_TRIANGLE) {
120
+ const rise: f64 = 4.0 * phase
121
+ v = phase < 0.5 ? rise - 1.0 : 3.0 - rise
122
+ } else {
123
+ if (phase < before || i === 0) {
124
+ noise = synthXorshift(noise)
125
+ held = toF64(noise & 65535) / 32767.5 - 1.0
126
+ }
127
+ v = held
128
+ }
129
+ const attack: f64 = Math.min(t / 0.002, 1.0)
130
+ const release: f64 = Math.min((length - t) / 0.005, 1.0)
131
+ out[from + i] = out[from + i] + v * volume * attack * release * Math.exp(-t * decay)
132
+ }
133
+ }
134
+
135
+ /**
136
+ * The frequency `semitones` equal-tempered steps above `base`.
137
+ *
138
+ * @param base - the starting frequency in Hz
139
+ * @param semitones - how many steps up; negative steps go down
140
+ * @returns `base × 2^(semitones / 12)`
141
+ */
142
+ export const synthSemitone = (base: f64, semitones: i32): f64 =>
143
+ base * Math.exp((toF64(semitones) * Math.log(2.0)) / 12.0)