// 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 (no sequence, no count, no seed).
export const FPS = 24, SR = 48000;

// THE MAPPING. In the tape sections every term of a sequence gets one slot of time, and a term d
// (1 or 2) sounds at d x F1: a 1 is 220 Hz, a 2 is 440 Hz, plucked once per slot. The LEFT ear
// plays one sequence, the RIGHT ear plays that same sequence READ AS RUN LENGTHS (Brent's map:
// 1 repeated s1 times, 2 repeated s2 times, 1 repeated s3 times, ...). Both ears use one synthesis
// function of the digit and the absolute time, so wherever the two ears carry the same digits their
// samples are identical, and a sequence that is its own description sounds in mono.
//
// In section III the ears change job. Left: one sine at 3/2 x F1 = 330 Hz. Right: one sine at
// (the average of the first n terms) x F1. If half the terms are 1s the average term is 3/2 and the
// right ear sits a just fifth above the 1s, in unison with the left; any difference beats at
// F1 x |average - 3/2| per second.
export const F1 = 220;
export const TONE = (d) => d * F1;

// Sections, in seconds.
export const TITLE = [0, 9];
export const I = [9, 37];          // Kolakoski, left as itself, right as its own run lengths
export const COIN = [37, 49];      // fair coin flips, left as themselves, right read as run lengths
export const II = [49, 129];       // the readings: left read j times, right read j+1 times
export const III = [129, 183];     // the average term, as a pitch
export const CLOSE = [183, 191];
export const DUR = CLOSE[1];
export const TOTAL_FRAMES = FPS * DUR;

// The tape segments: when each starts, how many terms a second, how many terms. The slot counts are
// chosen to run past where the two ears part; they are a clock, not a measurement.
export const GAP = 0.6;
function readings() {
  const plan = [
    // [reading j, terms per second, seconds]
    [1, 8, 2.5], [2, 8, 2.5], [3, 8, 2.5], [4, 8, 3], [5, 8, 3.5], [6, 8, 4], [7, 8, 5], [8, 8, 7], [9, 8, 9.5],
    [10, 12, 9], [11, 18, 9], [12, 27, 9],
  ];
  let t = II[0] + 3;
  return plan.map(([j, rate, secs]) => { const s = { name: `r${j}`, j, t0: t, rate, n: Math.round(rate * secs) }; t += secs + GAP; return s; });
}
export const SEGMENTS = [
  { name: 'kolakoski', j: null, t0: I[0] + 1, rate: 4, n: 4 * 26 },
  { name: 'coins', j: 0, t0: COIN[0] + 1, rate: 4, n: 4 * 10 },
  ...readings(),
];
export const READ_END = SEGMENTS.at(-1).t0 + SEGMENTS.at(-1).n / SEGMENTS.at(-1).rate;
export const slotTime = (seg, k) => [seg.t0 + k / seg.rate, seg.t0 + (k + 1) / seg.rate];

// The pluck: rises over ATTACK, decays exponentially, and is back to zero RELEASE before the slot
// ends, so every term is heard as its own note and a slot depends on its own digit only.
export const ATTACK = 0.004, RELEASE = 0.006, AMP = 0.42, H3 = 0.18;   // H3: a quiet third harmonic, for timbre

// Section III: steady steps. Each holds the right ear at one frequency (no glide, phase continuous).
// The labels name what each step plays; the frequencies come from the data, not from here.
export const STEPS = (() => {
  const plan = [
    ['n1', 9], ['n2', 4.5], ['n3', 4.5], ['n4', 4.5], ['n5', 4], ['n6', 4], ['n8', 4], ['n12', 4], ['n20', 5], ['proved', 10.5],
  ];
  let t = III[0] + 0.0;
  return plan.map(([name, secs]) => { const s = { name, t0: t, t1: t + secs }; t += secs; return s; });
})();
export const III_END = STEPS.at(-1).t1;
export const VOICE = 0.4;
export const FADE_IN = 0.3, FADE_OUT = 1.2;   // section III fades in at its start and out at its end
// listen.mjs reads each step over its middle, leaving READ_MARGIN at each end.
export const READ_MARGIN = 0.4;

export const POSTER_FRAME = Math.round((SEGMENTS.find((s) => s.name === 'r9').t0 + 6.5) * FPS);
