// spine.mjs: the film's clock, and the one mapping it uses. Every other file reads its times here,
// and so does listen.mjs, which knows nothing else about the film.
export const FPS = 24, SR = 48000, DUR = 197;
export const TOTAL_FRAMES = FPS * DUR;

// THE MAPPING. A cell with n sides sounds the n-th harmonic of F0 in the LEFT ear, as loud as its
// share of the cells. The RIGHT ear is one sine at (the average number of sides) x F0.
// So the left ear's loudness-weighted centre is the right ear's pitch, and a share rides on loudness
// (approximate after compression) while the average rides on pitch (exact after compression).
export const F0 = 55;
export const HARMONICS = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];

export const TITLE = [0, 12];
export const I = [12, 36];
export const II = [36, 66];
export const III = [66, 95];
export const IV = [95, 121];
export const V = [121, 148];
export const VI = [148, 184];
export const CLOSE = [184, 197];

// II: the crack schedule. Crack k (1..300) lands at CRACK0 + CRACKT * (k/300)^0.45.
export const NCRACKS = 300, CRACK0 = II[0] + 2.5, CRACKT = 23.5;
export const crackTime = (k) => CRACK0 + CRACKT * (k / NCRACKS) ** 0.45;
// III: pull j (1..npulls) lands at PULL0 + PULLT * j / npulls
export const PULL0 = III[0] + 1, PULLT = 22;
// IV, V: one scatter every SCAT seconds
export const WRAP0 = IV[0] + 1, BOUND0 = IV[0] + 13.6, BOUND1 = IV[0] + 19.45;
export const DISC0 = V[0] + 1, CAUSE0 = V[0] + 21.25;
export const PHIS = [0.1, 0.2, 0.3, 0.4, 0.5];
// VI: pips. Each pip is 7 periods of F0 long; the ears are read over its middle 5 periods.
export const PIP = 7 / F0, PIP_READ = 5 / F0, NPIPS = 64;
export const NAIVE0 = VI[0] + 2, REAL0 = VI[0] + 13;
export const pipWindow = (t0, i) => { const a = t0 + i * PIP + (PIP - PIP_READ) / 2; return [a, a + PIP_READ]; };
export const CAUSE2 = VI[0] + 21.6, RECOUNT0 = VI[0] + 27, RECOUNT = 1.4;

// steady windows: [from, to]. Inside each, neither ear changes.
export const W = {
  oreilly: [I[0] + 1.5, I[0] + 11],
  hetenyi: [I[0] + 12.6, I[0] + 23.6],
  square: [II[0] + 0.6, II[0] + 2.3],
  pulled: [III[0] + 23.6, III[0] + 28.6],
  wrapped: [0, 1, 2].map((i) => [WRAP0 + 4 * i + 0.2, WRAP0 + 4 * i + 3.8]),
  bounded: [[BOUND0 + 0.6, BOUND1 - 0.15], [BOUND1 + 0.3, IV[1] - 0.2]],
  discs: [0, 1, 2, 3, 4].map((i) => [DISC0 + 4 * i + 0.2, DISC0 + 4 * i + 3.8]),
  causeway: [CAUSE0 + 0.45, V[1] - 1.5],
  causeway2: [CAUSE2 + 0.6, CAUSE2 + 4.6],
  recounts: [0, 1, 2, 3, 4, 5].map((i) => [RECOUNT0 + RECOUNT * i + 0.1, RECOUNT0 + RECOUNT * i + RECOUNT - 0.1]),
};
// the right ear across II is read in one-second windows
export const W_CRACK = Array.from({ length: 27 }, (_, i) => [II[0] + 2.6 + i, II[0] + 3.4 + i]);

export const RAMP = 0.02;          // every change of either ear is a raised-cosine crossfade this long
export const POSTER_FRAME = Math.round((VI[0] + 17) * FPS);
