ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
test_pattern.h File Reference

Go to the source code of this file.

Functions

void audio_test_pattern (float *output, size_t count, uint64_t position, uint32_t sample_rate)

Function Documentation

◆ audio_test_pattern()

void audio_test_pattern ( float * output,
size_t count,
uint64_t position,
uint32_t sample_rate )

Generate a bounded, repeating 20-second mono diagnostic phrase. Position is in samples at sample_rate; arbitrary block sizes are equivalent. No allocations or mutable global state. Peak amplitude is at most 0.18.

Definition at line 8 of file audio/test_pattern.c.

9 {
10 if (!output || !sample_rate)
11 return;
12 const uint64_t period = (uint64_t)sample_rate * 20;
13 uint64_t current = position;
14 for (size_t i = 0; i < count; ++i) {
15 uint64_t phase = current % period;
16 current = current == UINT64_MAX ? 0 : current + 1;
17 double t = (double)phase / sample_rate;
18 int section = (int)(t / 4);
19 double u = t - section * 4;
20 double value = 0;
21 double fade = fmin(1.0, fmin(u, 4 - u) / 0.08);
22 fade = sin(1.5707963267948966 * fade);
23 fade *= fade;
24 if (section == 0) {
25 // A quiet major arpeggio tests pitch and transients without sharp clicks.
26 const double notes[] = {261.625565, 329.627557, 391.995436, 523.251131};
27 double beat = u - floor(u);
28 double envelope = sin(3.141592653589793 * beat);
29 value = 0.16 * envelope * envelope * tone(notes[(int)u], beat);
30 } else if (section == 1) {
31 // Soft harmonic chord: simultaneous low, middle, and upper frequencies.
32 value = 0.06 * tone(130.812783, u) + 0.05 * tone(329.627557, u) +
33 0.04 * tone(783.990872, u);
34 } else if (section == 2) {
35 // Smooth logarithmic sweep from 150 Hz to 3 kHz.
36 double k = log(20.0) / 4;
37 value = 0.12 * sin(6.283185307179586 * 150 * (exp(k * u) - 1) / k);
38 } else if (section == 3) {
39 // Deterministic broadband noise bursts, with a low level and soft edges.
40 // The hash uses the low 32 bits of each product and sum.
41 uint32_t hash =
42 (uint32_t)((uint64_t)(uint32_t)phase * 747796405u + 2891336453u);
43 hash = (uint32_t)((uint64_t)((hash >> ((hash >> 28) + 4)) ^ hash) *
44 277803737u);
45 hash = (hash >> 22) ^ hash;
46 double noise = (double)hash / 2147483648.0 - 1;
47 double beat = fmod(u, 1.0);
48 double envelope = beat < 0.5 ? sin(6.283185307179586 * beat) : 0;
49 value = 0.045 * noise * envelope * envelope;
50 } else if (u < 2) {
51 // Speech-like amplitude modulation, then two seconds of silence.
52 double envelope = 0.5 + 0.5 * tone(4, u);
53 double end = fmin(1.0, (2 - u) / 0.08);
54 value =
55 end * envelope *
56 (0.08 * tone(180, u) + 0.04 * tone(540, u) + 0.02 * tone(1260, u));
57 }
58 output[i] = (float)(value * fade);
59 }
60}
unsigned int uint32_t
unsigned long long uint64_t