Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/ui/chirp.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / ui / chirp.c` 192 lines ยท 6982 bytes Raw Blame History 1 /* chirp.c -- the startup sound; see chirp.h. 2 * 3 * Every frame is worked out from where it sits on the night's timeline. 4 * With L the share of the inspiral left (1 at CK_CHIRP_START_MS, 0 at the 5 * merger), the holes' orbit goes as 1 - L^(5/8); so does the sound's 6 * phase, scaled so it starts at CK_CHIRP_FROM_HZ, which makes its pitch go 7 * as L^(-3/8). Once the pitch reaches CK_CHIRP_TO_HZ (the last few 8 * milliseconds) it holds there through the merger and the fade. Loudness 9 * goes as L^(-1/4), which is pitch^(2/3), reaching CK_CHIRP_PEAK where the 10 * pitch stops rising. Phases are in 2^32ths of a cycle; L and its powers in 11 * Q30. */ 12 #include "chirp.h" 13 14 #define F0_FRAME (CK_CHIRP_RATE / 1000u * (CK_CHIRP_START_MS - CK_CHIRP_IN_MS)) /* the sound comes in */ 15 #define F1_FRAME (CK_CHIRP_RATE / 1000u * CK_CHIRP_START_MS) /* the sweep begins */ 16 #define F2_FRAME (CK_CHIRP_RATE / 1000u * CK_NIGHT_MERGE_MS) /* the merger */ 17 #define Q30 (1ull << 30) 18 #define DECAY 1072996429ull /* exp(-1 / (30 ms * 48 kHz)) in Q30: the fade's time constant is 30 ms */ 19 20 static int16_t sin_t[4096]; /* Q14 */ 21 static uint32_t cap_frame; /* the pitch stops rising here */ 22 static uint64_t cap_l14; /* L^(1/4) there */ 23 static uint64_t cap_phase; /* the phase there */ 24 static int ready; 25 26 static uint64_t isqrt(uint64_t x) 27 { 28 uint64_t r = 0, bit = 1ull << 62; 29 while (bit > x) 30 bit >>= 2; 31 while (bit) { 32 if (x >= r + bit) { 33 x -= r + bit; 34 r = (r >> 1) + bit; 35 } else { 36 r >>= 1; 37 } 38 bit >>= 2; 39 } 40 return r; 41 } 42 43 /* L at a frame of the inspiral, and its eighth root (both Q30) */ 44 static uint64_t left_q30(uint32_t f) { return ((uint64_t)(F2_FRAME - f) << 30) / (F2_FRAME - F1_FRAME); } 45 46 static uint64_t root8(uint64_t l) 47 { 48 for (int i = 0; i < 3; i++) 49 l = isqrt(l << 30); 50 return l; 51 } 52 53 static uint64_t mul(uint64_t a, uint64_t b) { return a * b >> 30; } 54 55 /* phase over the inspiral before the cap: P1 + K (1 - L^(5/8)), K chosen so 56 * the pitch starts at CK_CHIRP_FROM_HZ: K = FROM_HZ * 8/5 * inspiral seconds */ 57 static uint64_t inspiral_phase(uint32_t f) 58 { 59 uint64_t r = root8(left_q30(f)), l58 = mul(mul(mul(mul(r, r), r), r), r); 60 uint64_t p1 = ((uint64_t)CK_CHIRP_FROM_HZ * (F1_FRAME - F0_FRAME) << 32) / CK_CHIRP_RATE; 61 uint64_t k = (uint64_t)CK_CHIRP_FROM_HZ * 8 * (F2_FRAME - F1_FRAME); /* K * 5 * RATE */ 62 return p1 + ((k * (Q30 - l58) << 2) / (5ull * CK_CHIRP_RATE)); 63 } 64 65 static void setup(void) 66 { 67 const int64_t C = `1073740561`, S = `1647099`; /* cos, sin of a 4096th of a turn, Q30 */ 68 int64_t c = 1ll << 30, s = 0; 69 for (unsigned k = 0; k <= 1024; k++) { 70 int16_t v = k == 1024 ? 16384 : (int16_t)((s + (1 << 15)) >> 16); 71 sin_t[k] = v; 72 sin_t[2048 - k] = v; 73 sin_t[(2048 + k) & 4095] = (int16_t)-v; 74 sin_t[(4096 - k) & 4095] = (int16_t)-v; 75 int64_t nc = (c * C - s * S) >> 30, ns = (s * C + c * S) >> 30; 76 c = nc, s = ns; 77 } 78 /* the first frame where FROM_HZ * L^(-3/8) reaches TO_HZ, i.e. where 79 * L^(3/8) falls to FROM_HZ / TO_HZ (L^(3/8) only falls) */ 80 uint64_t want = ((uint64_t)CK_CHIRP_FROM_HZ << 30) / CK_CHIRP_TO_HZ; 81 uint32_t lo = F1_FRAME, hi = F2_FRAME; 82 while (lo < hi) { 83 uint32_t mid = lo + (hi - lo) / 2; 84 uint64_t r = root8(left_q30(mid)); 85 if (mul(mul(r, r), r) <= want) 86 hi = mid; 87 else 88 lo = mid + 1; 89 } 90 cap_frame = lo; 91 uint64_t r = root8(left_q30(lo)); 92 cap_l14 = mul(r, r); 93 cap_phase = inspiral_phase(lo); 94 ready = 1; 95 } 96 97 static uint64_t phase_at(uint32_t f) 98 { 99 if (f < F1_FRAME) 100 return ((uint64_t)CK_CHIRP_FROM_HZ * (f - F0_FRAME) << 32) / CK_CHIRP_RATE; 101 if (f < cap_frame) 102 return inspiral_phase(f); 103 return cap_phase + ((uint64_t)CK_CHIRP_TO_HZ * (f - cap_frame) << 32) / CK_CHIRP_RATE; 104 } 105 106 static uint64_t decay_pow(uint32_t k) /* DECAY^k, Q30 */ 107 { 108 uint64_t r = Q30, b = DECAY; 109 while (k) { 110 if (k & 1) 111 r = mul(r, b); 112 b = mul(b, b); 113 k >>= 1; 114 } 115 return r; 116 } 117 118 uint32_t ck_chirp_level(uint32_t f) 119 { 120 if (!ready) 121 setup(); 122 if (f < F0_FRAME || f >= CK_CHIRP_FRAMES) 123 return 0; 124 uint64_t steady = (uint64_t)CK_CHIRP_PEAK * cap_l14 >> 30; /* the level at L = 1 */ 125 if (f < F1_FRAME) { /* comes in smoothly (smoothstep) */ 126 uint64_t x = ((uint64_t)(f - F0_FRAME) << 16) / (F1_FRAME - F0_FRAME), s = x * x * (3ull * 65536 - 2 * x); 127 return (uint32_t)(steady * s >> 48); 128 } 129 if (f < cap_frame) { 130 uint64_t r = root8(left_q30(f)); 131 return (uint32_t)((uint64_t)CK_CHIRP_PEAK * cap_l14 / mul(r, r)); 132 } 133 if (f < F2_FRAME) 134 return CK_CHIRP_PEAK; 135 return (uint32_t)((uint64_t)CK_CHIRP_PEAK * decay_pow(f - F2_FRAME) >> 30); 136 } 137 138 uint32_t ck_chirp_hz(uint32_t f) 139 { 140 if (!ready) 141 setup(); 142 if (f < F0_FRAME || f >= CK_CHIRP_FRAMES) 143 return 0; 144 if (f < F1_FRAME) 145 return CK_CHIRP_FROM_HZ; 146 if (f >= cap_frame) 147 return CK_CHIRP_TO_HZ; 148 uint64_t r = root8(left_q30(f)); 149 return (uint32_t)(((uint64_t)CK_CHIRP_FROM_HZ << 30) / mul(mul(r, r), r)); 150 } 151 152 void ck_chirp_fill(int16_t *out, uint32_t from, uint32_t n) 153 { 154 if (!ready) 155 setup(); 156 for (uint32_t i = 0; i < n; i++) { 157 uint32_t f = from + i, a = ck_chirp_level(f); 158 int32_t v = 0; 159 if (a) { 160 uint32_t ph = (uint32_t)phase_at(f), k = ph >> 20, frac = ph >> 4 & 0xffffu; 161 int32_t s0 = sin_t[k], s1 = sin_t[(k + 1) & 4095]; 162 int32_t s = s0 + (int32_t)(((int64_t)(s1 - s0) * frac) >> 16); /* Q14 */ 163 v = (int32_t)(((int64_t)s * a) >> 14); 164 } 165 out[2 * i] = out[2 * i + 1] = (int16_t)v; 166 } 167 } 168 169 /* The same chirp at 44.1 kHz for converters that take only that rate: frame 170 * j sits at 48 kHz frame j * 160 / 147, drawn as a straight line between the 171 * two 48 kHz frames either side (the chirp stays under 1000 Hz, far below 172 * either rate's limit). Each sample goes in the top 16 bits of a 32-bit slot, 173 * the layout a 24-bit converter reads (Linux: S32_LE with 24 bits used). */ 174 void ck_chirp_fill_441(int32_t *out, uint32_t from, uint32_t n) 175 { 176 int16_t src[2 * 144]; 177 while (n) { 178 uint32_t m = n < 128u ? n : 128u, s0 = (uint32_t)((uint64_t)from * 160u / 147u); 179 uint32_t s1 = (uint32_t)((uint64_t)(from + m - 1) * 160u / 147u) + 1; 180 ck_chirp_fill(src, s0, s1 - s0 + 1); 181 for (uint32_t i = 0; i < m; i++) { 182 uint64_t at = (uint64_t)(from + i) * 160u; 183 uint32_t s = (uint32_t)(at / 147u) - s0, frac = (uint32_t)(at % 147u); 184 int32_t a = src[2 * s], b = src[2 * s + 2]; 185 int32_t v = (a * (int32_t)(147u - frac) + b * (int32_t)frac) / 147; 186 out[2 * i] = out[2 * i + 1] = (int32_t)((uint32_t)v << 16); 187 } 188 out += 2 * m; 189 from += m; 190 n -= m; 191 } 192 }