interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / ui / tests / chirp_test.c

224 lines · 9352 bytes Raw Blame History
1/* chirp_test.c -- host test of the startup sound (ui/chirp.c). Writes the
2 * whole sound as chirp.wav into the folder given, for a person to listen to.
3 * Prints CHIRP_TEST: PASS (n checks) or the first failure. */
4#define _POSIX_C_SOURCE 200809L /* fdopen, O_CLOEXEC under -std=c11 */
5#include <fcntl.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
11
12#include "../chirp.h"
13
14static int checks, failed;
15
16static void check(int ok, const char *what)
17{
18 checks++;
19 if (!ok && !failed) {
20 failed = 1;
21 printf("CHIRP_TEST: FAIL %s\n", what);
22 }
23}
24
25static uint32_t ms(uint32_t t) { return CK_CHIRP_RATE / 1000u * t; }
26
27/* the pitch heard over [a, b): rising zero crossings over the time */
28static double heard_hz(const int16_t *s, uint32_t a, uint32_t b)
29{
30 uint32_t first = 0, last = 0, n = 0;
31 for (uint32_t f = a + 1; f < b; f++)
32 if (s[2 * (f - 1)] < 0 && s[2 * f] >= 0) {
33 if (!n)
34 first = f;
35 last = f;
36 n++;
37 }
38 return n < 2 ? 0 : (double)(n - 1) * CK_CHIRP_RATE / (last - first);
39}
40
41static void put32(FILE *f, uint32_t v)
42{
43 for (int i = 0; i < 4; i++)
44 fputc(v >> 8 * i & 255, f);
45}
46
47static void put16(FILE *f, uint32_t v)
48{
49 fputc(v & 255, f);
50 fputc(v >> 8 & 255, f);
51}
52
53static int write_wav(const char *path, const int16_t *s, uint32_t frames)
54{
55 /* 0644, not fopen's 0666 & ~umask: the sound file must not be world-writable. */
56 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
57 FILE *f = fd < 0 ? NULL : fdopen(fd, "wb");
58 if (fd >= 0 && !f)
59 close(fd);
60 if (!f)
61 return -1;
62 uint32_t bytes = frames * 4;
63 fwrite("RIFF", 1, 4, f);
64 put32(f, 36 + bytes);
65 fwrite("WAVEfmt ", 1, 8, f);
66 put32(f, 16);
67 put16(f, 1); /* PCM */
68 put16(f, 2);
69 put32(f, CK_CHIRP_RATE);
70 put32(f, CK_CHIRP_RATE * 4);
71 put16(f, 4);
72 put16(f, 16);
73 fwrite("data", 1, 4, f);
74 put32(f, bytes);
75 for (uint32_t i = 0; i < 2 * frames; i++)
76 put16(f, (uint16_t)s[i]);
77 return fclose(f);
78}
79
80int main(int argc, char **argv)
81{
82 const uint32_t N = CK_CHIRP_FRAMES, extra = ms(200);
83 int16_t *s = calloc(2 * (size_t)(N + extra), sizeof *s), *p = calloc(2 * (size_t)(N + extra), sizeof *p);
84 if (!s || !p)
85 return 2;
86 ck_chirp_fill(s, 0, N + extra);
87
88 /* silence while the stars come out, and after the end */
89 int quiet = 1;
90 for (uint32_t f = 0; f < ms(CK_CHIRP_START_MS - CK_CHIRP_IN_MS); f++)
91 quiet &= s[2 * f] == 0 && s[2 * f + 1] == 0;
92 check(quiet, "silent until just before the sweep");
93 uint32_t first = 0, last = 0;
94 for (uint32_t f = 0; f < N; f++)
95 if (s[2 * f]) {
96 first = first ? first : f;
97 last = f;
98 }
99 printf("note: sound from %u ms to %u ms (%u ms)\n", first * 1000 / CK_CHIRP_RATE, last * 1000 / CK_CHIRP_RATE,
100 (last - first) * 1000 / CK_CHIRP_RATE);
101 check(first && last - first < ms(1000), "a quick blirp: under a second from first sound to last");
102 quiet = 1;
103 for (uint32_t f = N; f < N + extra; f++)
104 quiet &= s[2 * f] == 0;
105 check(quiet, "silent after the end");
106 check(ck_chirp_level(N - 1) < 2, "faded out by the end");
107
108 /* both channels the same, nothing past the peak */
109 int same = 1, top = 0;
110 for (uint32_t f = 0; f < N; f++) {
111 same &= s[2 * f] == s[2 * f + 1];
112 int v = abs(s[2 * f]);
113 top = v > top ? v : top;
114 }
115 check(same, "left and right the same");
116 check(top <= CK_CHIRP_PEAK && top > CK_CHIRP_PEAK * 98 / 100, "loudest sample at the peak level");
117
118 /* no jump big enough to click: a sine at TO_HZ moves at most 2 pi f / rate of its height a frame */
119 int jump = 0;
120 for (uint32_t f = 1; f < N + extra; f++) {
121 int d = abs(s[2 * f] - s[2 * (f - 1)]);
122 jump = d > jump ? d : jump;
123 }
124 printf("note: largest step between frames %d (a %u Hz sine at the peak: %d)\n", jump, CK_CHIRP_TO_HZ,
125 (int)(6.2832 * CK_CHIRP_TO_HZ / CK_CHIRP_RATE * CK_CHIRP_PEAK));
126 check(jump <= (int)(6.2832 * CK_CHIRP_TO_HZ / CK_CHIRP_RATE * CK_CHIRP_PEAK) + 64, "no clicks");
127
128 /* loudness: comes in, then only rises to the merger, then falls */
129 uint32_t a = ck_chirp_level(ms(CK_CHIRP_START_MS - CK_CHIRP_IN_MS)), b = ck_chirp_level(ms(CK_CHIRP_START_MS) - 1);
130 check(a < 4 && b > 1000, "the sound comes in over CK_CHIRP_IN_MS");
131 check(ck_chirp_level(ms(CK_CHIRP_START_MS)) - b < 8, "no step in loudness when the sweep starts");
132 int up = 1, down = 1;
133 for (uint32_t f = ms(CK_CHIRP_START_MS - CK_CHIRP_IN_MS) + 1; f < ms(CK_NIGHT_MERGE_MS); f++)
134 up &= ck_chirp_level(f) >= ck_chirp_level(f - 1);
135 for (uint32_t f = ms(CK_NIGHT_MERGE_MS) + 1; f < N; f++)
136 down &= ck_chirp_level(f) <= ck_chirp_level(f - 1);
137 check(up, "louder all the way to the merger");
138 check(down, "quieter all the way after it");
139 check(ck_chirp_level(ms(CK_NIGHT_MERGE_MS) - 1) == CK_CHIRP_PEAK, "the peak at the merger");
140 check(ck_chirp_level(ms(CK_NIGHT_MERGE_MS) + ms(60)) < CK_CHIRP_PEAK * 40 / 100, "the ring fades in tens of ms");
141
142 /* pitch: steady, then only rises, reaching TO_HZ in the last moments */
143 check(ck_chirp_hz(ms(CK_CHIRP_START_MS - CK_CHIRP_IN_MS)) == CK_CHIRP_FROM_HZ &&
144 ck_chirp_hz(ms(CK_CHIRP_START_MS)) == CK_CHIRP_FROM_HZ,
145 "steady pitch while it comes in");
146 up = 1;
147 uint32_t reach = 0;
148 for (uint32_t f = ms(CK_CHIRP_START_MS) + 1; f < ms(CK_NIGHT_MERGE_MS); f++) {
149 up &= ck_chirp_hz(f) >= ck_chirp_hz(f - 1);
150 if (!reach && ck_chirp_hz(f) == CK_CHIRP_TO_HZ)
151 reach = f;
152 }
153 check(up, "pitch only rises in the sweep");
154 printf("note: pitch mid-sweep %u Hz, 100 ms before the merger %u Hz; the top reached %u ms before it\n",
155 ck_chirp_hz(ms(CK_CHIRP_START_MS + CK_CHIRP_RISE_MS / 2)), ck_chirp_hz(ms(CK_NIGHT_MERGE_MS - 100)), (ms(CK_NIGHT_MERGE_MS) - reach) * 1000 / CK_CHIRP_RATE);
156 check(reach && ms(CK_NIGHT_MERGE_MS) - reach < ms(60), "the top pitch only in the last 60 ms");
157
158 /* what is heard matches: zero crossings */
159 const uint32_t mid = CK_CHIRP_START_MS + CK_CHIRP_RISE_MS / 2;
160 double h0 = heard_hz(s, ms(CK_CHIRP_START_MS - CK_CHIRP_IN_MS / 2), ms(CK_CHIRP_START_MS)),
161 h1 = heard_hz(s, ms(mid - 50), ms(mid + 50)),
162 h2 = heard_hz(s, ms(CK_NIGHT_MERGE_MS), ms(CK_NIGHT_MERGE_MS) + ms(100));
163 printf("note: heard %.1f Hz as it comes in, %.1f Hz mid-sweep (asked %u), %.1f Hz in the ring\n", h0, h1,
164 ck_chirp_hz(ms(mid)), h2);
165 check(h0 > CK_CHIRP_FROM_HZ - 3.0 && h0 < CK_CHIRP_FROM_HZ + 3.0, "heard pitch as it comes in");
166 check(h1 > ck_chirp_hz(ms(mid - 50)) - 2.0 && h1 < ck_chirp_hz(ms(mid + 50)) + 2.0, "heard pitch in the sweep");
167 check(h2 > CK_CHIRP_TO_HZ - 5.0 && h2 < CK_CHIRP_TO_HZ + 5.0, "heard pitch in the ring");
168
169 /* any frame in any order: backwards in odd pieces gives the same sound */
170 for (uint32_t end = N + extra; end;) {
171 uint32_t n = end < 997 ? end : 997;
172 end -= n;
173 ck_chirp_fill(p + 2 * (size_t)end, end, n);
174 }
175 check(!memcmp(s, p, 4 * (size_t)(N + extra)), "pieces in any order give the same sound");
176
177 /* the 44.1 kHz copy: where the two rates meet (every 147th frame) it is
178 * the 48 kHz sample exactly, in the top 16 bits; in between it lies
179 * between its neighbours; pieces agree; silent after the end */
180 const uint32_t N4 = CK_CHIRP_FRAMES_441, extra4 = CK_CHIRP_RATE_441 / 5u;
181 int32_t *q = calloc(2 * (size_t)(N4 + extra4), sizeof *q), *r = calloc(2 * (size_t)(N4 + extra4), sizeof *r);
182 if (!q || !r)
183 return 2;
184 ck_chirp_fill_441(q, 0, N4 + extra4);
185 int meet = 1, between = 1, lr = 1, low = 1;
186 for (uint32_t j = 0; j < N4; j++) {
187 uint32_t f = j * 160u / 147u;
188 int32_t a = s[2 * f], b = s[2 * f + 2], v = q[2 * j] >> 16;
189 if (j % 147u == 0 && q[2 * j] != (int32_t)((uint32_t)a << 16))
190 meet = 0;
191 if (v < (a < b ? a : b) || v > (a < b ? b : a))
192 between = 0;
193 lr &= q[2 * j] == q[2 * j + 1];
194 low &= (q[2 * j] & 0xffff) == 0;
195 }
196 check(meet, "44.1 kHz copy equals the 48 kHz sound where the rates meet");
197 check(between, "44.1 kHz copy lies between its 48 kHz neighbours");
198 check(lr && low, "44.1 kHz copy: left equals right, sample in the top 16 bits");
199 for (uint32_t end = N4 + extra4; end;) {
200 uint32_t n = end < 1009 ? end : 1009;
201 end -= n;
202 ck_chirp_fill_441(r + 2 * (size_t)end, end, n);
203 }
204 check(!memcmp(q, r, 8 * (size_t)(N4 + extra4)), "44.1 kHz pieces in any order give the same sound");
205 quiet = 1;
206 for (uint32_t j = N4 + 2; j < N4 + extra4; j++)
207 quiet &= q[2 * j] == 0;
208 check(quiet, "44.1 kHz copy silent after the end");
209 free(q);
210 free(r);
211
212 if (argc > 1) {
213 char path[4096];
214 snprintf(path, sizeof path, "%s/chirp.wav", argv[1]);
215 check(write_wav(path, s, N) == 0, "wav written");
216 printf("note: wrote %s (%u frames, %u ms)\n", path, N, N * 1000 / CK_CHIRP_RATE);
217 }
218 free(s);
219 free(p);
220 if (failed)
221 return 1;
222 printf("CHIRP_TEST: PASS (%d checks)\n", checks);
223 return 0;
224}