|
| 1 | +// Regression test for the 49.7-day millisecond-clock rollover. |
| 2 | +// |
| 3 | +// amy_sysclock() is u32 milliseconds, so it wraps every 2^32 ms = 49.71 days. |
| 4 | +// Two things used to break at that instant, and neither is reachable from the |
| 5 | +// audio-rendering test suite because you cannot render 50 days of samples -- |
| 6 | +// so this test fast-forwards amy_global.total_blocks instead. |
| 7 | +// |
| 8 | +// 1. sequencer_check_and_fill() anchored its 64-bit next_amy_tick_us |
| 9 | +// accumulator to the 32-bit clock. At the rollover now_us collapsed to ~0 |
| 10 | +// while next_amy_tick_us stayed at ~4.3e12 us, so the catch-up guard |
| 11 | +// (which only handles falling *behind*) and the tick loop both went quiet |
| 12 | +// and the sequencer stopped forever -- taking tulip.defer() and every |
| 13 | +// seq callback with it. |
| 14 | +// |
| 15 | +// 2. The delta queue is time-sorted with a plain `>=`. A note_off scheduled |
| 16 | +// a few ms past the rollover has a tiny d->time while its own note_on is |
| 17 | +// still near 2^32, so the release sorted ahead of the attack, fired |
| 18 | +// first, and left the note droning forever. |
| 19 | +// |
| 20 | +// Build/run with `make ctest`. |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdint.h> |
| 24 | +#include <inttypes.h> |
| 25 | +#include "amy.h" |
| 26 | +#include "sequencer.h" |
| 27 | + |
| 28 | +static int failures = 0; |
| 29 | + |
| 30 | +#define CHECK(cond, fmt, ...) do { \ |
| 31 | + if (cond) { printf(" ok " fmt "\n", ##__VA_ARGS__); } \ |
| 32 | + else { printf(" FAIL " fmt "\n", ##__VA_ARGS__); failures++; } \ |
| 33 | +} while (0) |
| 34 | + |
| 35 | +// Blocks per second of synthesized audio. |
| 36 | +static const uint64_t BPS = AMY_SAMPLE_RATE / AMY_BLOCK_SIZE; |
| 37 | + |
| 38 | +static uint32_t ticks_seen = 0; |
| 39 | +static void count_tick(uint32_t t) { (void)t; ticks_seen++; } |
| 40 | + |
| 41 | +// Park the synth clock at an arbitrary uptime without rendering our way there. |
| 42 | +static void set_clock_ms(uint64_t ms) { |
| 43 | + amy_global.total_blocks = (uint32_t)((ms * AMY_SAMPLE_RATE) / (1000ULL * AMY_BLOCK_SIZE)); |
| 44 | + amy_global.total_samples = amy_global.total_blocks * AMY_BLOCK_SIZE; |
| 45 | +} |
| 46 | + |
| 47 | +// Run the real render loop for `secs` of audio. amy_simple_fill_buffer() does |
| 48 | +// execute_deltas -> render -> fill, and the fill is what advances total_blocks, |
| 49 | +// so this drives the clock exactly the way the audio callback does. Rendering |
| 50 | +// (not delta execution) is also what retires a finished note to SYNTH_OFF. |
| 51 | +static uint32_t advance_secs(double secs) { |
| 52 | + ticks_seen = 0; |
| 53 | + uint64_t n = (uint64_t)(BPS * secs); |
| 54 | + for (uint64_t i = 0; i < n; i++) amy_simple_fill_buffer(); |
| 55 | + return ticks_seen; |
| 56 | +} |
| 57 | + |
| 58 | +static int osc_audible(int osc) { |
| 59 | + return synth[osc] != NULL && synth[osc]->status == SYNTH_AUDIBLE; |
| 60 | +} |
| 61 | + |
| 62 | +static void note_on(int osc, float midi_note, uint32_t time) { |
| 63 | + amy_event e = amy_default_event(); |
| 64 | + e.time = time; e.osc = osc; e.wave = SINE; e.velocity = 1.0f; e.midi_note = midi_note; |
| 65 | + amy_add_event(&e); |
| 66 | +} |
| 67 | + |
| 68 | +static void note_off(int osc, uint32_t time) { |
| 69 | + amy_event e = amy_default_event(); |
| 70 | + e.time = time; e.osc = osc; e.velocity = 0; |
| 71 | + amy_add_event(&e); |
| 72 | +} |
| 73 | + |
| 74 | +// 2^32 ms -- the instant amy_sysclock() rolls over. |
| 75 | +#define WRAP_MS 4294967296ULL |
| 76 | + |
| 77 | +static void test_sequencer_survives_wrap(void) { |
| 78 | + printf("sequencer keeps ticking across the 49.7-day rollover\n"); |
| 79 | + amy_global.config.amy_external_sequencer_hook = count_tick; |
| 80 | + |
| 81 | + double expected_per_10s = 10.0 * 1000000.0 / amy_global.us_per_tick; |
| 82 | + |
| 83 | + set_clock_ms(WRAP_MS - 30000); // 30 s out |
| 84 | + sequencer_recompute(); |
| 85 | + uint32_t before = advance_secs(10); |
| 86 | + CHECK(before > expected_per_10s * 0.9, |
| 87 | + "before wrap: %u ticks/10s (expected ~%.0f)", before, expected_per_10s); |
| 88 | + |
| 89 | + advance_secs(25); // cross it |
| 90 | + CHECK(amy_sysclock() < 30000, "sysclock rolled over: now %u", amy_sysclock()); |
| 91 | + CHECK(amy_sysclock64() > WRAP_MS, "64-bit clock kept counting: %" PRIu64, amy_sysclock64()); |
| 92 | + |
| 93 | + uint32_t after = advance_secs(10); |
| 94 | + CHECK(after > expected_per_10s * 0.9, |
| 95 | + "after wrap: %u ticks/10s (expected ~%.0f)", after, expected_per_10s); |
| 96 | + |
| 97 | + uint32_t much_later = advance_secs(60); |
| 98 | + CHECK(much_later > expected_per_10s * 5.9, |
| 99 | + "wrap+1min: %u ticks/60s (expected ~%.0f)", much_later, expected_per_10s * 6); |
| 100 | + |
| 101 | + amy_global.config.amy_external_sequencer_hook = NULL; |
| 102 | +} |
| 103 | + |
| 104 | +static void test_no_stuck_note_across_wrap(void) { |
| 105 | + printf("a note whose note_off lands past the rollover still stops\n"); |
| 106 | + |
| 107 | + set_clock_ms(WRAP_MS - 500); |
| 108 | + uint32_t t = amy_sysclock(); |
| 109 | + note_on(0, 64, t); |
| 110 | + note_off(0, t + 1000); // this time value wraps past 2^32 |
| 111 | + CHECK(t + 1000 < t, "note_off time wrapped as intended (on=%u off=%u)", t, t + 1000); |
| 112 | + |
| 113 | + advance_secs(0.2); |
| 114 | + CHECK(osc_audible(0), "note is sounding before the wrap"); |
| 115 | + |
| 116 | + advance_secs(2.0); // well past both the wrap and the note_off |
| 117 | + CHECK(!osc_audible(0), "note stopped after the wrap (not stuck on)"); |
| 118 | +} |
| 119 | + |
| 120 | +static void test_scheduling_works_after_wrap(void) { |
| 121 | + printf("scheduling still works once the clock has rolled over\n"); |
| 122 | + |
| 123 | + set_clock_ms(WRAP_MS + 5000); |
| 124 | + note_on(1, 60, amy_sysclock()); |
| 125 | + advance_secs(0.2); |
| 126 | + CHECK(osc_audible(1), "immediate note_on sounds"); |
| 127 | + |
| 128 | + note_off(1, amy_sysclock()); |
| 129 | + advance_secs(0.5); |
| 130 | + CHECK(!osc_audible(1), "immediate note_off stops it"); |
| 131 | + |
| 132 | + note_on(2, 67, amy_sysclock() + 500); |
| 133 | + advance_secs(0.2); |
| 134 | + CHECK(!osc_audible(2), "note scheduled +500ms has not fired yet"); |
| 135 | + advance_secs(0.6); |
| 136 | + CHECK(osc_audible(2), "note scheduled +500ms fired on time"); |
| 137 | +} |
| 138 | + |
| 139 | +static void test_sustained_note_across_sample_wrap(void) { |
| 140 | + // amy_global.total_samples is u32 and wraps every 2^32 samples (27.05 h at |
| 141 | + // 44.1 kHz). Envelope math is `total_samples - note_on_clock` in u32, which |
| 142 | + // is modular-correct, so this should be a non-event -- pin that down. |
| 143 | + printf("held note is unaffected by the 27-hour total_samples wrap\n"); |
| 144 | + |
| 145 | + uint32_t wrap_blocks = (uint32_t)((1ULL << 32) / AMY_BLOCK_SIZE); |
| 146 | + amy_global.total_blocks = wrap_blocks - (uint32_t)(BPS * 2); |
| 147 | + amy_global.total_samples = amy_global.total_blocks * AMY_BLOCK_SIZE; |
| 148 | + |
| 149 | + note_on(3, 69, amy_sysclock()); |
| 150 | + advance_secs(1.0); |
| 151 | + CHECK(osc_audible(3), "sounding before the sample-counter wrap"); |
| 152 | + advance_secs(3.0); |
| 153 | + CHECK(amy_global.total_samples < BPS * AMY_BLOCK_SIZE * 4, "total_samples wrapped"); |
| 154 | + CHECK(osc_audible(3), "still sounding after the sample-counter wrap"); |
| 155 | + note_off(3, amy_sysclock()); |
| 156 | + advance_secs(1.0); |
| 157 | +} |
| 158 | + |
| 159 | +// examples.c calls this; the example binaries each define their own. This test |
| 160 | +// drives the clock by hand and never sleeps, so nothing here should reach it. |
| 161 | +void delay_ms(uint32_t ms) { (void)ms; } |
| 162 | + |
| 163 | +int main(void) { |
| 164 | + amy_config_t c = amy_default_config(); |
| 165 | + c.features.startup_bleep = 0; |
| 166 | + amy_start(c); |
| 167 | + |
| 168 | + test_sequencer_survives_wrap(); |
| 169 | + test_no_stuck_note_across_wrap(); |
| 170 | + test_scheduling_works_after_wrap(); |
| 171 | + test_sustained_note_across_sample_wrap(); |
| 172 | + |
| 173 | + if (failures) { |
| 174 | + printf("\n%d check(s) FAILED\n", failures); |
| 175 | + return 1; |
| 176 | + } |
| 177 | + printf("\nall clock-wrap checks passed\n"); |
| 178 | + return 0; |
| 179 | +} |
0 commit comments