interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / allen-audio / chirp.om

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