aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / om / allen-audio / chirp.om
| 1 | note Allen's startup sound: the merger chirp of the iMac night, ported from aienux |
| 2 | note native/kernel/ui/chirp.c and chirp.h at da55660ea7337f2b6a42095fcd1fb6ddebe1cc7c (git blobs |
| 3 | note d3e732908a72a372816584d0fe3d62a3db256f1b and 359f0f4e63e5c194b005906d3bd7b03cfb7e8703; |
| 4 | note CK_NIGHT_MERGE_MS 12000 from ui/night.h). The same files are byte-identical at the iMac build |
| 5 | note 0930240d2aa550de95535c116eb10c2df466f616. That source makes exactly one sound: this chirp. It |
| 6 | note has no other boom, voice or opening sound; every speaker path (x86/hda_x86.c, dev/virtio_snd.c) |
| 7 | note plays ck_chirp_fill or ck_chirp_fill_441 from the night's current moment. |
| 8 | note |
| 9 | note The sound: silent until 640 ms before the merger, comes in over 40 ms (smoothstep) at 200 Hz, |
| 10 | note sweeps up as L^(-3/8) in pitch and L^(-1/4) in loudness (L the share of the 600 ms inspiral |
| 11 | note left) until the pitch reaches 1000 Hz, holds 1000 Hz at peak 22937 to the merger at 12000 ms, |
| 12 | note then decays with a 30 ms time constant until 12300 ms. Integer only, exactly as the C: |
| 13 | note phases in 2^32ths of a cycle, L and its powers in Q30, a 4096-entry Q14 sine table built by |
| 14 | note rotation. Every function below mirrors one C function and keeps its rounding order. |
| 15 | note |
| 16 | note Not a program by itself: check.sh concatenates this unit with asset.om (the web asset and the |
| 17 | note browser script) or native.om (the oracle layout of aienux ui/tests/chirp_test.c). |
| 18 | note |
| 19 | note Frame constants, from chirp.h with CK_CHIRP_RATE 48000 and CK_NIGHT_MERGE_MS 12000, checked |
| 20 | note with WolframLanguageEvaluator: {48000/1000*(12000-600-40), 48000/1000*(12000-600), |
| 21 | note 48000/1000*12000, 48000/1000*(12000+300)} -> {545280, 547200, 576000, 590400}; |
| 22 | note Round[Exp[-1/(30/1000*48000)]*2^30] -> 1072996429 (the C literal DECAY); |
| 23 | note Round[Cos[2 Pi/4096]*2^30] -> 1073740561, Round[Sin[2 Pi/4096]*2^30] -> 1647099 (C, S). |
| 24 | |
| 25 | const RATE ulong 48000 |
| 26 | const FROMHZ ulong 200 |
| 27 | const TOHZ ulong 1000 |
| 28 | const PEAK ulong 22937 |
| 29 | const FZERO ulong 545280 |
| 30 | const FONE ulong 547200 |
| 31 | const FTWO ulong 576000 |
| 32 | const FEND ulong 590400 |
| 33 | const DECAY ulong 1072996429 |
| 34 | const QONE ulong 1073741824 |
| 35 | const COSQ long 1073740561 |
| 36 | const SINQ long 1647099 |
| 37 | |
| 38 | note sint: the Q14 sine table. cap: 0 cap_frame, 1 cap_l14, 2 cap_phase (chirp.c setup). |
| 39 | global sint long 4096 |
| 40 | global cap ulong 3 |
| 41 | |
| 42 | note Output: one buffered stream, written to the descriptor in ofd. |
| 43 | global obuf uchar 65536 |
| 44 | global oused ulong 1 |
| 45 | global ofd int 1 |
| 46 | |
| 47 | text riff "RIFF" |
| 48 | text wavefmt "WAVEfmt " |
| 49 | text datatag "data" |
| 50 | |
| 51 | note Integer square root, chirp.c isqrt. Each loop divides bit by 4 from 2^62, so neither runs more |
| 52 | note than 32 passes. |
| 53 | fn isqrt x ulong to ulong |
| 54 | r = ulong 0 |
| 55 | b = shl ulong 1 62 |
| 56 | v = x |
| 57 | while gt b v bound 40 |
| 58 | b = shr b 2 |
| 59 | end |
| 60 | while ne b 0 bound 40 |
| 61 | if ge v add r b |
| 62 | v = sub v add r b |
| 63 | r = add shr r 1 b |
| 64 | else |
| 65 | r = shr r 1 |
| 66 | end |
| 67 | b = shr b 2 |
| 68 | end |
| 69 | ret r |
| 70 | end |
| 71 | |
| 72 | note L at a frame of the inspiral, Q30 (chirp.c left_q30). |
| 73 | fn leftq f ulong to ulong |
| 74 | ret div shl sub FTWO f 30 sub FTWO FONE |
| 75 | end |
| 76 | |
| 77 | note Eighth root of a Q30 value (chirp.c root8: three square roots). |
| 78 | fn root8 l ulong to ulong |
| 79 | v = isqrt shl l 30 |
| 80 | v = isqrt shl v 30 |
| 81 | v = isqrt shl v 30 |
| 82 | ret v |
| 83 | end |
| 84 | |
| 85 | fn mulq a ulong b ulong to ulong |
| 86 | ret shr mul a b 30 |
| 87 | end |
| 88 | |
| 89 | note Phase over the inspiral before the cap: P1 + K (1 - L^(5/8)) (chirp.c inspiral_phase). |
| 90 | fn insphase f ulong to ulong |
| 91 | r = root8 leftq f |
| 92 | l58 = mulq mulq mulq mulq r r r r r |
| 93 | p1 = div shl mul FROMHZ sub FONE FZERO 32 RATE |
| 94 | k = mul mul FROMHZ 8 sub FTWO FONE |
| 95 | ret add p1 div shl mul k sub QONE l58 2 mul 5 RATE |
| 96 | end |
| 97 | |
| 98 | note The sine table by rotation, chirp.c setup, same store order. k runs 0..1024: 1025 passes. |
| 99 | fn sintab to void |
| 100 | c = long 1073741824 |
| 101 | s = long 0 |
| 102 | k = ulong 0 |
| 103 | while le k 1024 bound 1025 |
| 104 | v = long 16384 |
| 105 | if ne k 1024 |
| 106 | v = shr add s 32768 16 |
| 107 | end |
| 108 | put sint k v |
| 109 | put sint sub 2048 k v |
| 110 | put sint and add 2048 k 4095 neg v |
| 111 | put sint and sub 4096 k 4095 neg v |
| 112 | nc = shr sub mul c COSQ mul s SINQ 30 |
| 113 | ns = shr add mul s COSQ mul c SINQ 30 |
| 114 | c = nc |
| 115 | s = ns |
| 116 | k = add k 1 |
| 117 | end |
| 118 | end |
| 119 | |
| 120 | note The first frame where L^(3/8) falls to FROMHZ / TOHZ: binary search over the 28800 inspiral |
| 121 | note frames, at most 15 passes. |
| 122 | fn capfind to ulong |
| 123 | want = div shl FROMHZ 30 TOHZ |
| 124 | lo = add FONE 0 |
| 125 | hi = add FTWO 0 |
| 126 | while lt lo hi bound 64 |
| 127 | mid = add lo div sub hi lo 2 |
| 128 | r = root8 leftq mid |
| 129 | if le mulq mulq r r r want |
| 130 | hi = mid |
| 131 | else |
| 132 | lo = add mid 1 |
| 133 | end |
| 134 | end |
| 135 | ret lo |
| 136 | end |
| 137 | |
| 138 | fn setup to void |
| 139 | sintab |
| 140 | lo = capfind |
| 141 | r = root8 leftq lo |
| 142 | put cap 0 lo |
| 143 | put cap 1 mulq r r |
| 144 | put cap 2 insphase lo |
| 145 | end |
| 146 | |
| 147 | note chirp.c phase_at; called only for frames with a nonzero level (f >= FZERO). |
| 148 | fn phaseat f ulong to ulong |
| 149 | if lt f FONE |
| 150 | ret div shl mul FROMHZ sub f FZERO 32 RATE |
| 151 | end |
| 152 | if lt f get cap 0 |
| 153 | ret insphase f |
| 154 | end |
| 155 | ret add get cap 2 div shl mul TOHZ sub f get cap 0 32 RATE |
| 156 | end |
| 157 | |
| 158 | note DECAY^k in Q30 by squaring; k < 2^14 here, so at most 14 passes. |
| 159 | fn decaypow k ulong to ulong |
| 160 | r = add QONE 0 |
| 161 | b = add DECAY 0 |
| 162 | n = k |
| 163 | while ne n 0 bound 70 |
| 164 | if eq and n 1 1 |
| 165 | r = mulq r b |
| 166 | end |
| 167 | b = mulq b b |
| 168 | n = shr n 1 |
| 169 | end |
| 170 | ret r |
| 171 | end |
| 172 | |
| 173 | note chirp.c ck_chirp_level: 0..PEAK at a frame. |
| 174 | fn level f ulong to ulong |
| 175 | if or lt f FZERO ge f FEND |
| 176 | ret 0 |
| 177 | end |
| 178 | if lt f FONE |
| 179 | steady = shr mul PEAK get cap 1 30 |
| 180 | x = div shl sub f FZERO 16 sub FONE FZERO |
| 181 | s = mul mul x x sub mul ulong 3 65536 mul 2 x |
| 182 | ret shr mul steady s 48 |
| 183 | end |
| 184 | if lt f get cap 0 |
| 185 | r = root8 leftq f |
| 186 | ret div mul PEAK get cap 1 mulq r r |
| 187 | end |
| 188 | if lt f FTWO |
| 189 | ret PEAK |
| 190 | end |
| 191 | ret shr mul PEAK decaypow sub f FTWO 30 |
| 192 | end |
| 193 | |
| 194 | note One sample of chirp.c ck_chirp_fill: the table read at the phase's top 12 bits, a straight |
| 195 | note line by the next 16, scaled by the level. Signed shr keeps the sign, as the C's int64 shifts. |
| 196 | fn sample f ulong to long |
| 197 | a = level f |
| 198 | if eq a 0 |
| 199 | ret long 0 |
| 200 | end |
| 201 | ph = and phaseat f 4294967295 |
| 202 | k = shr ph 20 |
| 203 | fr = and shr ph 4 65535 |
| 204 | s0 = get sint k |
| 205 | s1 = get sint and add k 1 4095 |
| 206 | s = add s0 shr mul sub s1 s0 long fr 16 |
| 207 | ret shr mul s long a 14 |
| 208 | end |
| 209 | |
| 210 | fn oflush to void |
| 211 | p = ulong 0 |
| 212 | while lt p get oused 0 bound 65536 |
| 213 | n = write get ofd 0 obuf p sub get oused 0 p |
| 214 | if le n 0 |
| 215 | trap 3 |
| 216 | end |
| 217 | p = add p ulong n |
| 218 | end |
| 219 | put oused 0 0 |
| 220 | end |
| 221 | |
| 222 | fn obyte c uchar to void |
| 223 | if eq get oused 0 len obuf |
| 224 | oflush |
| 225 | end |
| 226 | put obuf get oused 0 c |
| 227 | put oused 0 add get oused 0 1 |
| 228 | end |
| 229 | |
| 230 | fn ole16 v ulong to void |
| 231 | obyte uchar and v 255 |
| 232 | obyte uchar and shr v 8 255 |
| 233 | end |
| 234 | |
| 235 | fn ole32 v ulong to void |
| 236 | ole16 and v 65535 |
| 237 | ole16 shr v 16 |
| 238 | end |
| 239 | |
| 240 | note A 16-bit little-endian sample (two's complement). |
| 241 | fn osample v long to void |
| 242 | u = ulong 0 |
| 243 | if lt v 0 |
| 244 | u = ulong add v 65536 |
| 245 | else |
| 246 | u = ulong v |
| 247 | end |
| 248 | ole16 u |
| 249 | end |
| 250 | |
| 251 | note The canonical 44-byte PCM WAV header (the layout of chirp_test.c write_wav). |
| 252 | fn owav ch ulong frames ulong to void |
| 253 | bytes = mul frames mul 2 ch |
| 254 | for i ulong len riff |
| 255 | obyte get riff i |
| 256 | end |
| 257 | ole32 add 36 bytes |
| 258 | for i ulong len wavefmt |
| 259 | obyte get wavefmt i |
| 260 | end |
| 261 | ole32 16 |
| 262 | ole16 1 |
| 263 | ole16 ch |
| 264 | ole32 RATE |
| 265 | ole32 mul RATE mul 2 ch |
| 266 | ole16 mul 2 ch |
| 267 | ole16 16 |
| 268 | for i ulong len datatag |
| 269 | obyte get datatag i |
| 270 | end |
| 271 | ole32 bytes |
| 272 | end |