#!/usr/bin/env node
// verify.mjs — recompute every number the page "There Is No Silence" asserts,
// so the page shows the check rather than claiming it. Run: node verify.mjs
//
// The spine is one cited anchor and one law of statistical physics:
//   • ANCHOR (from the acoustics literature): the thermal (Brownian) noise of
//     still air at ~20 °C, integrated over the audible band (20 Hz–20 kHz), is
//     about -23 dB SPL. (Modern acoustics; see SOURCES at the bottom. Harris
//     1968 gives ~-24 dB SPL in a 1 kHz band centred on 3 kHz — same ballpark.)
//   • LAW: thermal-noise mean-square pressure is proportional to absolute
//     temperature T (⟨p²⟩ ∝ k_B·T), so the RMS pressure scales as √T. At T → 0
//     the floor → 0: the only true silence is the death of all molecular motion.
//
// Everything else here is the dB ↔ pressure definition and arithmetic on top of
// those two facts. No claim in the page is asserted that this file does not print.

const REF_uPa = 20;          // 0 dB SPL ≡ 20 µPa RMS, the definitional reference @ 1 kHz
const T0 = 293.15;           // 20 °C in kelvin (the anchor temperature)
const FLOOR_dB_at_T0 = -23;  // cited full-band thermal-noise floor of air at ~20 °C

// dB SPL → RMS pressure in µPa, and back.
const dBtoUPa = (dB) => REF_uPa * Math.pow(10, dB / 20);
const uPaToDB = (uPa) => 20 * Math.log10(uPa / REF_uPa);

// Thermal-noise floor pressure (µPa) as a function of absolute temperature T (K).
// p(T) = p(T0) · √(T/T0), anchored to the cited -23 dB SPL at T0.
const floorUPa_at_T0 = dBtoUPa(FLOOR_dB_at_T0);
const floorUPa = (T) => floorUPa_at_T0 * Math.sqrt(T / T0);
const floorDB = (T) => uPaToDB(floorUPa(T));

let pass = 0, fail = 0;
const approx = (got, want, tol, label) => {
  const ok = Math.abs(got - want) <= tol;
  console.log(`  ${ok ? 'PASS' : 'FAIL'}  ${label}: got ${got.toFixed(4)}, want ${want} ±${tol}`);
  ok ? pass++ : fail++;
};

console.log('— dB SPL ↔ pressure (µPa), the definitional reference —');
approx(dBtoUPa(0),   20,    1e-9, '0 dB SPL                     = 20 µPa (definition)');
approx(dBtoUPa(-9),  7.096, 0.01, '-9 dB SPL (best ear @ ~3 kHz)= 7.1 µPa');
approx(dBtoUPa(-23), 1.418, 0.01, '-23 dB SPL (air floor, full band)');
approx(dBtoUPa(-24), 1.262, 0.01, '-24 dB SPL (Harris 1968, 3 kHz band)');
approx(dBtoUPa(-24.9), 1.139, 0.01, '-24.9 dBA (Orfield Labs, 2021 record)');

console.log('\n— the headroom: how close the ear sits to the air’s own heat —');
// Best human threshold is ~-9 dB SPL (2–5 kHz); the thermal floor in that band
// is ~-24 dB SPL (Harris). The margin is the whole point.
const headroom_dB = -9 - (-24);
approx(headroom_dB, 15, 1e-9, 'headroom, best threshold − thermal floor (dB)');
approx(Math.pow(10, headroom_dB / 20), 5.623, 0.01, 'same headroom as a pressure ratio (×)');

console.log('\n— the temperature law: thermal floor ∝ √T (the instrument’s model) —');
approx(floorDB(T0),    -23, 1e-9, 'floor at 293.15 K = anchor (-23 dB SPL)');
// Halving the pressure is a drop of 20·log10(2) ≈ 6.02 dB; that happens at T0/4.
approx(floorDB(T0 / 4), -23 - 20 * Math.log10(2), 1e-6, 'floor at T0/4 K is 6.02 dB quieter (√¼ = ½ pressure)');
approx(floorUPa(0),    0,   1e-12, 'floor at 0 K = 0 µPa  ← the ONLY true silence');
// At liquid-nitrogen temperature (77 K) the air would already be liquid, but the
// √T law still tells the story: the floor is meaningfully quieter, not gone.
approx(floorDB(77),   -23 + 20 * Math.log10(Math.sqrt(77 / T0)), 1e-6, 'floor at 77 K (illustrative, ∝√T)');

console.log('\n— 4′33″ arithmetic: the three premiere movements sum to the title —');
const mov = [30, 2 * 60 + 23, 1 * 60 + 40]; // I=30s, II=2′23″, III=1′40″ (premiere, 1952)
const total = mov.reduce((a, b) => a + b, 0);
approx(total, 273, 1e-9, '30 + 143 + 100 seconds');
approx(total, 4 * 60 + 33, 1e-9, '… = 4 min 33 s');
console.log('  NOTE  273 s ↔ −273 °C (absolute zero ≈ −273.15 °C) is a numerical');
console.log('        coincidence often pointed out; Cage’s own remarks on it were');
console.log('        evasive. The page presents it AS a coincidence, not as intent.');

console.log(`\n${fail === 0 ? '✓ ALL CHECKS PASS' : '✗ ' + fail + ' FAILED'}  (${pass} passed)`);

console.log(`
SOURCES (every figure above is anchored to one of these):
 • Threshold 0 dB SPL ≡ 20 µPa @ 1 kHz; best 2–5 kHz ≈ −9 dB SPL —
     en.wikipedia.org/wiki/Absolute_threshold_of_hearing
 • Thermal/Brownian noise of air ≈ −23 dB SPL (20 Hz–20 kHz, ~20 °C);
   Harris 1968 ≈ −24 dB SPL (1 kHz band @ 3 kHz); Sivian & White 1933 first
   proposed the threshold is thermal-noise-limited —
     acoustic-measurement.com/en/technology/can-we-create-absolute-silence-on-earth/
   (open caveat, named on the page: the exact margin, A-weighting, and how much
    is the eardrum’s OWN Brownian motion vs the air’s, are still discussed —
    see the 2024 JASA letter, PubMed 38829155.)
 • Quietest rooms: Orfield Labs −24.9 dBA (Nov 2021, Guinness); Microsoft
   Building 87 −20.35 dBA (2015) — cnn.com/style/article/anechoic-chamber-worlds-quietest-room
 • 4′33″ premiere (Tudor, 29 Aug 1952, Maverick Hall) & movement durations;
   the piece frames ambient sound, it is not silence —
     en.wikipedia.org/wiki/4%E2%80%B233%E2%80%B3
 • Cage’s 1951 anechoic-chamber anecdote AND its correction (you cannot hear
   your nervous system; the high tone is more likely tinnitus; Heller & Bergman
   1953 found >90% of normal-hearing listeners hear internal sound in quiet) —
     Peter Gena, via the 4′33″ literature; Heller & Bergman, Ann. Otol. Rhinol. Laryngol. 62:73-83 (1953).
`);

process.exit(fail === 0 ? 0 : 1);
