/* MIT License Copyright (c) 2026 Joonas Nägel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * Estonian quantity, as arithmetic — and as sound. * * Estonian writes `kolli` and means two different words by it. One is the * genitive of `koll`, the bogeyman; the other is the partitive. Nothing on the * page distinguishes them. Nothing in the segments distinguishes them either: * the same consonants, the same vowels, in the same order. What distinguishes * them is how long the first syllable is *compared with the second*. * * That is Ilse Lehiste's result. In 1960 she showed that the three Estonian * quantity degrees are carried not by absolute duration but by the ratio of the * first syllable's duration to the second's, and half a century of re-measuring * has left the ratio standing as the system's most stable characteristic. * * This file is that ratio, and the sound it makes. Two things follow from * building it as a computation rather than as a set of recordings: * * 1. The category boundaries are DERIVED, not asserted. The three published * centres are 0.67, 1.5 and 2.0. A reader's ratio is assigned to whichever * centre is nearest in LOG space — ratios are multiplicative, so log space * is the space in which "nearest" means anything — and the boundaries fall * out as the geometric means, 1.00 and 1.73. This page did not choose * those; they are what the published centres imply. * * 2. Rate invariance is testable rather than claimed. Change the total length * of the foot and both syllables change with it; the ratio does not, and * neither does the word. That is exactly the claim, and `degreeOf` cannot * see total duration at all, so the page cannot cheat it. * * The audio is synthesised sample by sample from a source-filter model — a * glottal pulse train through three Klatt resonators — so that every sound on * this page is the arithmetic above, running. There is no recording here. */ /** The synthesis rate. F3 for Estonian front vowels reaches ~3 kHz; this clears it. */ export const SAMPLE_RATE = 22050; /** A quantity degree, as the literature numbers them. */ export type Degree = 1 | 2 | 3; /** * The published S1:S2 duration ratios for the three degrees. * * Lehiste 1960, restated across the later literature: approximately 0.67 for * Q1 (short), 1.5 for Q2 (long) and 2.0 for Q3 (overlong). They are ratios of * the first syllable's duration to the second's, and they are the numbers this * whole page turns on. */ export const CENTRES: Record = { 1: 0.67, 2: 1.5, 3: 2.0 }; /** * Where one degree gives way to the next: the geometric mean of the two * neighbouring centres. Not a measured boundary and not presented as one — it * is the line equidistant, in log space, from the two published centres it * separates. It lands at 1.00 and at 1.73. */ export const BOUNDARIES: [number, number] = [ Math.sqrt(CENTRES[1] * CENTRES[2]), Math.sqrt(CENTRES[2] * CENTRES[3]), ]; /** * How far an early fall in fundamental frequency can drag the Q2/Q3 decision, * in log-ratio units. * * Lehiste and Danforth's ordering of the cues is duration first, pitch second: * when the ratio is ambiguous, an f0 peak early in the first syllable followed * by a fall within that syllable pushes the percept towards Q3, while a level * or late peak holds it at Q2. This constant is the weight of that second cue * in this page's decision rule. It is the page's own modelling choice, not a * measured quantity, and the page says so where it uses it: the published * literature orders the cues but does not hand out an exchange rate between * them. */ export const PITCH_WEIGHT = 0.22; /** * Where the f0 peak sits inside the first syllable's rhyme, as a fraction of * it, for a canonical realisation of each degree. * * The literature states the contours rather than the fractions: in Q1 and Q2 * the pitch rises or stays flat through the first syllable and falls in the * unstressed one, so the peak is late; in Q3 the peak sits at roughly the first * third of the stressed vowel and the pitch falls through the rest of that * syllable and on into the second. A Q3 word has already begun falling by the * time the first syllable ends, and a Q2 word has not. These three numbers are * this page's reading of those contours. */ export const PEAK_AT: Record = { 1: 0.85, 2: 0.85, 3: 0.33 }; /** * And the fact that makes the pitch cue less mysterious than it looks. * * The absolute timing of the peak is reported to be very similar across the * three degrees — around a hundred milliseconds from the start of the stressed * vowel. Since that vowel gets longer from Q1 to Q3, a peak that always arrives * at the same absolute moment arrives RELATIVELY earlier in a longer syllable. * The second cue is not independent of the first; it partly falls out of it. * * The page draws this as a curve across the plane rather than asserting it, and * the reader can move the point off it — which is what putting the two cues in * conflict means, and what Lehiste and Danforth did to find out which one wins. */ export const PEAK_CONSTANT_MS = 100; /** Where a peak that always arrives at PEAK_CONSTANT_MS would fall in a rhyme of this length. */ export function naturalPeak(rhyme1Ms: number): number { return clamp(PEAK_CONSTANT_MS / Math.max(1, rhyme1Ms), 0, 1); } /** A foot's total duration at an ordinary speaking rate, ms. */ export const DEFAULT_TOTAL_MS = 480; /** The two syllable durations a total and a ratio imply, in ms. */ export function durations(totalMs: number, ratio: number): { s1: number; s2: number } { const s2 = totalMs / (1 + ratio); return { s1: totalMs - s2, s2 }; } /** And the inverse: the ratio two durations make. */ export function ratioOf(s1Ms: number, s2Ms: number): number { return s2Ms > 0 ? s1Ms / s2Ms : Infinity; } /** * Which degree a point in the quantity plane falls in. * * `peakAt` is where the f0 peak sits within the first syllable, 0 at its start * and 1 at its end; an early peak means an early fall. The effect is applied as * a shift along the log-ratio axis, which is what makes the boundaries on the * page's plane slanted rather than vertical — the whole point being that the * two cues trade against each other rather than one overriding the other. * * Note what is NOT an argument here: total duration. The claim is that the * category does not depend on speaking rate, so the function that decides the * category is not given the rate. */ export function degreeOf(ratio: number, peakAt = 0.5): Degree { const x = Math.log(ratio) + PITCH_WEIGHT * (0.5 - clamp(peakAt, 0, 1)); let best: Degree = 1; let bestD = Infinity; for (const d of [1, 2, 3] as Degree[]) { const dist = Math.abs(x - Math.log(CENTRES[d])); if (dist < bestD) { bestD = dist; best = d; } } return best; } /** How far a ratio sits from its own degree's published centre, in log units. */ export function distanceFromCentre(ratio: number, degree: Degree): number { return Math.log(ratio) - Math.log(CENTRES[degree]); } const clamp = (v: number, lo: number, hi: number) => (v < lo ? lo : v > hi ? hi : v); /** * The bracket the boundary hunt searches, and how many times it halves it. * * The plane's axis runs over the same range, and reads it from here so that a * boundary found by bisection and an axis drawn by hand cannot come from two * different numbers. Forty halvings of a bracket eight-and-a-half-fold wide is * a ratio settled to about 1e-12, far below anything a reader can drag. */ export const BISECTION: { lo: number; hi: number; halvings: number } = { lo: 0.4, hi: 3.4, halvings: 40 }; /** * The ratio at which `degreeOf` changes its mind, found by asking it. * * `peakAt` is where the f0 peak sits in the first rhyme, 0 at its start and 1 * at its end; `lowDegree` is the degree on the left of the line. The search is * a bisection in LOG space — the midpoint of a bracket is its geometric mean, * because ratios multiply — run until the verdict flips from `lowDegree` to * `lowDegree + 1`. * * Nothing here knows the closed form. That is the point: the picture the plane * draws is the decision function itself rather than an illustration of it, so * a change to CENTRES or PITCH_WEIGHT moves the regions without anyone * remembering to redraw. `pitchBand` below compares the two and throws if they * part, which is the only place the closed form is written down. * * Closed form, for the record: * * degreeOf flips where ln ρ + PITCH_WEIGHT·(0.5 − p) = ln B, * so ρ = B · exp(PITCH_WEIGHT·(p − 0.5)) with B = BOUNDARIES[lowDegree − 1]. */ export function boundaryAt(peakAt: number, lowDegree: 1 | 2): number { let lo = BISECTION.lo; let hi = BISECTION.hi; for (let i = 0; i < BISECTION.halvings; i++) { const mid = Math.sqrt(lo * hi); if (degreeOf(mid, peakAt) <= lowDegree) lo = mid; else hi = mid; } return Math.sqrt(lo * hi); } /** What a boundary would be if it were computed rather than hunted: B·exp(w·(p − ½)). */ export function boundaryClosed(peakAt: number, lowDegree: 1 | 2): number { return BOUNDARIES[lowDegree - 1]! * Math.exp(PITCH_WEIGHT * (clamp(peakAt, 0, 1) - 0.5)); } /** * How wide the window is in which the pitch cue decides anything at all. * * The boundary slides with the peak, and only that far: below `lo` the word is * the lower degree whatever the fall does, above `hi` it is the higher one. * For Q2/Q3 that window is 1.552 to 1.933 — a sixth of a step, which is the * honest size of a cue the literature orders second. * * lo = boundaryAt(0, d) (the peak at the very start: the earliest fall) * hi = boundaryAt(1, d) (the peak at the very end: no fall inside the rhyme) * * `closed` is the same two numbers from B·exp(±w/2). The bisection and the * algebra must agree to 1e-6 or this throws, which is what keeps `boundaryAt` * from quietly drifting away from the decision rule it is meant to trace. */ export function pitchBand(lowDegree: 1 | 2): { lo: number; hi: number; closed: { lo: number; hi: number } } { const lo = boundaryAt(0, lowDegree); const hi = boundaryAt(1, lowDegree); const closed = { lo: boundaryClosed(0, lowDegree), hi: boundaryClosed(1, lowDegree) }; if (Math.abs(lo - closed.lo) > 1e-6 || Math.abs(hi - closed.hi) > 1e-6) { throw new Error(`pitchBand(${lowDegree}): the bisection and the closed form disagree — ${lo} vs ${closed.lo}, ${hi} vs ${closed.hi}`); } return { lo, hi, closed }; } /** The page's own modelling choices, as opposed to the published constants: named so lib/gaps.ts can count them. */ export const MODELLING_CHOICES = ['PITCH_WEIGHT'] as const; /* ---------------------------------------------------------------------- * * The vowels. * ---------------------------------------------------------------------- */ /** * How firm the formant table below is: not firm at all, and the page says so. * * A formant table is a measurement of particular speakers in a particular room, * and the nine sets of three numbers here are a reading of the published * Estonian tables rather than a measurement of anybody. Every vowel carries * this word so that lib/gaps.ts can count them instead of the page claiming a * precision it has not got. */ export const VOWELS_PROVENANCE = 'approximate' as const; /** A vowel, as the three resonances that make it that vowel. */ export interface Vowel { /** The letter Estonian spells it with. */ ch: string; /** IPA. */ ipa: string; /** First, second and third formant, Hz. */ f: [number, number, number]; /** Always VOWELS_PROVENANCE: these are approximate, and counted as such. */ provenance: typeof VOWELS_PROVENANCE; } /** * Formant targets for the nine Estonian vowels. * * These are the numbers that turn the arithmetic above into a sound you can * recognise as a word. Where they come from, and how firm they are, is stated * on the page itself and in the gaps block: a formant table is a measurement of * particular speakers, and any one table is one room, one microphone and one * set of mouths. */ export const VOWELS: Record = { i: { ch: 'i', ipa: 'i', f: [300, 2200, 3000], provenance: VOWELS_PROVENANCE }, ü: { ch: 'ü', ipa: 'y', f: [300, 1800, 2400], provenance: VOWELS_PROVENANCE }, u: { ch: 'u', ipa: 'u', f: [350, 800, 2400], provenance: VOWELS_PROVENANCE }, e: { ch: 'e', ipa: 'e', f: [450, 2000, 2700], provenance: VOWELS_PROVENANCE }, ö: { ch: 'ö', ipa: 'ø', f: [450, 1600, 2300], provenance: VOWELS_PROVENANCE }, õ: { ch: 'õ', ipa: 'ɤ', f: [450, 1200, 2400], provenance: VOWELS_PROVENANCE }, o: { ch: 'o', ipa: 'o', f: [500, 950, 2500], provenance: VOWELS_PROVENANCE }, ä: { ch: 'ä', ipa: 'æ', f: [700, 1800, 2600], provenance: VOWELS_PROVENANCE }, a: { ch: 'a', ipa: 'ɑ', f: [750, 1300, 2600], provenance: VOWELS_PROVENANCE }, }; /* ---------------------------------------------------------------------- * * The synthesiser. * ---------------------------------------------------------------------- */ /** One stretch of the word: what it sounds like and how long it lasts. */ export interface Segment { /** The letter, for drawing. */ ch: string; /** Milliseconds. */ ms: number; /** Formant targets, Hz. A stop closure has none. */ f: [number, number, number] | null; /** 0 for silence, 1 for a full vowel; sonorants sit between. */ gain: number; /** True where the vocal folds are running. */ voiced: boolean; } /** * A two-pole resonator, in the form Klatt wrote it: * * y[n] = A x[n] + B y[n-1] + C y[n-2] * * with B = 2 e^(-pi BW T) cos(2 pi F T), C = -e^(-2 pi BW T) and A chosen so * the response is unity at DC. Three of them in cascade over a pulse train is * the whole of the vocal tract, for this purpose. */ function resonatorCoeffs(freqHz: number, bwHz: number, sr: number): [number, number, number] { const T = 1 / sr; const c = -Math.exp(-2 * Math.PI * bwHz * T); const b = 2 * Math.exp(-Math.PI * bwHz * T) * Math.cos(2 * Math.PI * freqHz * T); return [1 - b - c, b, c]; } /** Bandwidths, Hz. Wider for the higher formants, the way a real tract is. */ const BANDWIDTHS: [number, number, number] = [60, 90, 150]; /** * The fundamental frequency over the whole word. * * It rises to a peak and falls away from it. `peakSample` is where that peak * sits, and the page puts it inside the first syllable — because in Estonian * what separates the long from the overlong, once duration has had its say, is * whether the fall has already happened by the time the first syllable ends. * * This is a separate function so that it can be checked on its own terms. A * contour inferred back off filtered audio is not a contour; it is a guess * about formants. */ export function f0Contour(n: number, peakSample: number, hi: number, lo: number): Float32Array { const out = new Float32Array(n); const p = clamp(peakSample, 0, n); for (let i = 0; i < n; i++) { out[i] = i <= p ? lo + (hi - lo) * (p > 0 ? i / p : 1) : lo + (hi - lo) * Math.max(0, 1 - (i - p) / Math.max(1, n - p)); } return out; } /** * Render a word to samples. * * f0 runs from `f0Start` down to `f0End`, with the peak placed at `peakAt` * through the FIRST segment that carries a vowel — so moving the peak really * does move the fall, rather than relabelling it. */ /** * Where the f0 peak falls, in samples. * * `span` is the half-open range of segments the peak is measured inside — the * first rhyme. It has to be given rather than guessed: in `lina` the onset /l/ * is voiced and carries formants of its own, so a function that looked for the * first voiced segment would put the peak in the onset, where nothing about * quantity happens. */ export function peakSampleOf( segments: Segment[], peakAt: number, span: { from: number; to: number }, sampleRate = SAMPLE_RATE, ): number { let at = 0; const marks: number[] = []; for (const s of segments) { marks.push(at); at += Math.round((s.ms / 1000) * sampleRate); } marks.push(at); const a = marks[Math.max(0, Math.min(span.from, marks.length - 1))] ?? 0; const b = marks[Math.max(0, Math.min(span.to, marks.length - 1))] ?? at; return a + (b - a) * clamp(peakAt, 0, 1); } export function render( segments: Segment[], opts: { f0Start?: number; f0End?: number; peakAt?: number; peakSpan?: { from: number; to: number }; sampleRate?: number } = {}, ): { samples: Float32Array; marks: number[]; sampleRate: number } { const sr = opts.sampleRate ?? SAMPLE_RATE; const f0Hi = opts.f0Start ?? 200; const f0Lo = opts.f0End ?? 150; const peakAt = clamp(opts.peakAt ?? 0.5, 0, 1); const total = segments.reduce((a, s) => a + s.ms, 0); const n = Math.max(1, Math.round((total / 1000) * sr)); const out = new Float32Array(n); // Where each segment starts, in samples — also what the page draws its // boundaries from, so the picture and the sound cannot drift apart. const marks: number[] = []; let at = 0; for (const s of segments) { marks.push(at); at += Math.round((s.ms / 1000) * sr); } marks.push(n); // The first rhyme's span, which is where the f0 peak lives. Given explicitly // when the caller knows the word; otherwise the first voiced segment that // carries formants, which is right for a bare vowel and no worse than a guess // for anything else. const fallback = segments.findIndex((s) => s.f !== null && s.voiced); const span = opts.peakSpan ?? { from: Math.max(0, fallback), to: Math.max(0, fallback) + 1 }; const v0 = marks[Math.max(0, Math.min(span.from, marks.length - 1))] ?? 0; const v1 = marks[Math.max(0, Math.min(span.to, marks.length - 1))] ?? n; const peakSample = v0 + (v1 - v0) * peakAt; // Source: a pulse train whose period follows f0, shaped by one pole so it has // the falling spectrum a glottal wave has rather than the flat one an impulse // train has. const f0s = f0Contour(n, peakSample, f0Hi, f0Lo); let phase = 0; let tilt = 0; const src = new Float32Array(n); for (let i = 0; i < n; i++) { const f0 = f0s[i] ?? f0Lo; phase += f0 / sr; let pulse = 0; if (phase >= 1) { phase -= 1; pulse = 1; } tilt = tilt * 0.55 + pulse * 0.45; src[i] = tilt; } // Noise, for the segments the folds are not running through: the burst of a // stop, the friction of an /s/. Seeded rather than random, so that the same // word rendered twice is the same samples twice and the check script can // assert on them. let seed = 0x2b1c5f2d; const noise = () => { seed ^= seed << 13; seed >>>= 0; seed ^= seed >>> 17; seed ^= seed << 5; seed >>>= 0; return (seed / 0xffffffff) * 2 - 1; }; // Filter: three resonators in cascade, their targets interpolated across each // segment boundary so the formants glide the way they do in a mouth. let y1 = [0, 0, 0]; let y2 = [0, 0, 0]; for (let seg = 0; seg < segments.length; seg++) { const s = segments[seg]; if (!s) continue; const a = marks[seg] ?? 0; const b = marks[seg + 1] ?? n; const prev = segments[seg - 1]; const next = segments[seg + 1]; const from = s.f ?? prev?.f ?? next?.f ?? [500, 1500, 2500]; const to = next?.f ?? s.f ?? from; for (let i = a; i < b; i++) { const t = b > a ? (i - a) / (b - a) : 0; // Glide into the next target over the last third of the segment only. const g = t < 0.67 ? 0 : (t - 0.67) / 0.33; let x = (s.voiced ? (src[i] ?? 0) : noise()) * s.gain; for (let k = 0; k < 3; k++) { const f = (from[k] ?? 500) * (1 - g) + (to[k] ?? 500) * g; const [A, B, C] = resonatorCoeffs(f, BANDWIDTHS[k] ?? 100, sr); const yn = A * x + B * (y1[k] ?? 0) + C * (y2[k] ?? 0); y2[k] = y1[k] ?? 0; y1[k] = yn; x = yn; } out[i] = x; } } // A short fade at each end so nothing clicks, and a normalise so the loudness // does not wander as the reader drags. const fade = Math.min(Math.round(sr * 0.008), Math.floor(n / 2)); for (let i = 0; i < fade; i++) { const w = i / fade; out[i] = (out[i] ?? 0) * w; out[n - 1 - i] = (out[n - 1 - i] ?? 0) * w; } let peak = 0; for (let i = 0; i < n; i++) peak = Math.max(peak, Math.abs(out[i] ?? 0)); if (peak > 0) { const k = 0.85 / peak; for (let i = 0; i < n; i++) out[i] = (out[i] ?? 0) * k; } return { samples: out, marks, sampleRate: sr }; } /* ---------------------------------------------------------------------- * * The words. * ---------------------------------------------------------------------- */ /** * One sound in a word. * * A piece is either part of an ONSET, in which case it has a fixed length in * milliseconds, or part of a RHYME, in which case it takes a share of whatever * the rhyme has been given. * * That division is not tidiness. The duration of a syllable's initial consonant * is not contrastive in Estonian, so the phonetic literature leaves the onset * out of the comparison and computes the ratio over the RHYMES. The "syllable * ratio" everybody quotes after Lehiste is in practice a rhyme ratio, and a * page that stretched the onsets along with everything else would be measuring * something she did not measure. */ export interface Piece { ch: string; /** Share of its own rhyme's duration, for rhyme pieces. Rhyme shares sum to 1. */ share: number; /** Fixed length in ms, for onset pieces. */ ms: number; /** The vowel whose formants it wears, or null for a closure. */ v: string | null; gain: number; voiced: boolean; } /** * A word the page can say, cut at the joints the measurement is taken at. * * `kolli` is syllabified kol.li, so its first rhyme is the vowel plus the first * half of the geminate; `koli` is ko.li, so its first rhyme is the vowel alone. * The long and the overlong word have exactly the same pieces as each other — * which is the point. Nothing distinguishes them but the ratio. */ export interface Word { id: string; spelling: string; degree: Degree; gloss_et: string; gloss_en: string; onset1: Piece[]; rhyme1: Piece[]; onset2: Piece[]; rhyme2: Piece[]; } const rv = (ch: string, share: number, vowel: string, gain = 1): Piece => ({ ch, share, ms: 0, v: vowel, gain, voiced: true }); const rson = (ch: string, share: number, vowel: string): Piece => ({ ch, share, ms: 0, v: vowel, gain: 0.45, voiced: true }); /** An onset, at a fixed length. Onsets do not stretch, because they do not contrast. */ const ostop = (ch: string, ms: number): Piece => ({ ch, share: 0, ms, v: null, gain: 0, voiced: false }); const oburst = (ms: number): Piece => ({ ch: '', share: 0, ms, v: null, gain: 0.3, voiced: false }); const ofric = (ch: string, ms: number): Piece => ({ ch, share: 0, ms, v: null, gain: 0.22, voiced: false }); const oson = (ch: string, ms: number, vowel: string): Piece => ({ ch, share: 0, ms, v: vowel, gain: 0.45, voiced: true }); /** * The triples. * * `sada / saada / saada` is the one the Estonian quantity literature reaches * for first: one vowel quality, no consonant difference, only quantity. And * `koli / kolli / kolli` makes the sharpest point, because in both triples the * second and third words are THE SAME SPELLING. Estonian orthography writes the * short against the long and leaves the long against the overlong unwritten, so * a reader of Estonian recovers it from grammar, from context — and, when * spoken, from a ratio. */ export const TRIPLES: { id: string; words: [Word, Word, Word] }[] = [ { id: 'saada', words: [ { id: 'sada', spelling: 'sada', degree: 1, gloss_et: 'arv 100', gloss_en: 'a hundred', onset1: [ofric('s', 90)], rhyme1: [rv('a', 1, 'a')], onset2: [ostop('d', 30), oburst(10)], rhyme2: [rv('a', 1, 'a')] }, { id: 'saada2', spelling: 'saada', degree: 2, gloss_et: 'käskiv kõneviis, saada!', gloss_en: 'send! (imperative)', onset1: [ofric('s', 90)], rhyme1: [rv('aa', 1, 'a')], onset2: [ostop('d', 30), oburst(10)], rhyme2: [rv('a', 1, 'a')] }, { id: 'saada3', spelling: 'saada', degree: 3, gloss_et: 'da-tegevusnimi', gloss_en: 'to get (da-infinitive)', onset1: [ofric('s', 90)], rhyme1: [rv('aa', 1, 'a')], onset2: [ostop('d', 30), oburst(10)], rhyme2: [rv('a', 1, 'a')] }, ], }, { id: 'koll', words: [ { id: 'koli', spelling: 'koli', degree: 1, gloss_et: 'kila-kola, kraam', gloss_en: 'junk, clutter', onset1: [ostop('k', 50), oburst(12)], rhyme1: [rv('o', 1, 'o')], onset2: [oson('l', 55, 'i')], rhyme2: [rv('i', 1, 'i')] }, { id: 'kolli2', spelling: 'kolli', degree: 2, gloss_et: 'omastav, kolli jälg', gloss_en: 'of the bogeyman (genitive)', onset1: [ostop('k', 50), oburst(12)], rhyme1: [rv('o', 0.55, 'o'), rson('l', 0.45, 'o')], onset2: [oson('l', 55, 'i')], rhyme2: [rv('i', 1, 'i')] }, { id: 'kolli3', spelling: 'kolli', degree: 3, gloss_et: 'osastav, nägin kolli', gloss_en: 'bogeyman (partitive)', onset1: [ostop('k', 50), oburst(12)], rhyme1: [rv('o', 0.55, 'o'), rson('l', 0.45, 'o')], onset2: [oson('l', 55, 'i')], rhyme2: [rv('i', 1, 'i')] }, ], }, { id: 'lina', words: [ { id: 'lina', spelling: 'lina', degree: 1, gloss_et: 'linane kangas', gloss_en: 'linen, a sheet', onset1: [oson('l', 55, 'i')], rhyme1: [rv('i', 1, 'i')], onset2: [oson('n', 55, 'a')], rhyme2: [rv('a', 1, 'a')] }, { id: 'linna2', spelling: 'linna', degree: 2, gloss_et: 'omastav, linna vapp', gloss_en: 'of the town (genitive)', onset1: [oson('l', 55, 'i')], rhyme1: [rv('i', 0.5, 'i'), rson('n', 0.5, 'i')], onset2: [oson('n', 55, 'a')], rhyme2: [rv('a', 1, 'a')] }, { id: 'linna3', spelling: 'linna', degree: 3, gloss_et: 'sisseütlev, lähen linna', gloss_en: 'into the town (illative)', onset1: [oson('l', 55, 'i')], rhyme1: [rv('i', 0.5, 'i'), rson('n', 0.5, 'i')], onset2: [oson('n', 55, 'a')], rhyme2: [rv('a', 1, 'a')] }, ], }, ]; /** Every word, flat, for lookups. */ export const WORDS: Word[] = TRIPLES.flatMap((t) => t.words); /** How much of a foot the onsets take, before the ratio gets to divide anything. */ export function onsetMs(w: Word): number { return [...w.onset1, ...w.onset2].reduce((a, p) => a + p.ms, 0); } /** The two rhyme durations a total and a ratio imply, in ms. */ export function rhymes(w: Word, totalMs: number, ratio: number): { r1: number; r2: number } { const budget = Math.max(20, totalMs - onsetMs(w)); const { s1, s2 } = durations(budget, ratio); return { r1: s1, r2: s2 }; } /** * Lay a word out over a total duration and a ratio. * * Onsets keep their fixed lengths; the ratio divides what is left between the * two rhymes. Nothing here knows which degree the word "is" — that is the * point. A reader can hand `kolli` the ratio of `koli` and hear what comes out. */ export function wordSegments(w: Word, totalMs: number, ratio: number): Segment[] { const { r1, r2 } = rhymes(w, totalMs, ratio); const fixed = (pieces: Piece[]): Segment[] => pieces.map((p) => ({ ch: p.ch, ms: p.ms, f: p.v ? (VOWELS[p.v]?.f ?? null) : null, gain: p.gain, voiced: p.voiced })); const shared = (pieces: Piece[], ms: number): Segment[] => pieces.map((p) => ({ ch: p.ch, ms: ms * p.share, f: p.v ? (VOWELS[p.v]?.f ?? null) : null, gain: p.gain, voiced: p.voiced })); return [...fixed(w.onset1), ...shared(w.rhyme1, r1), ...fixed(w.onset2), ...shared(w.rhyme2, r2)]; } /** Where the first rhyme starts and ends among the laid-out segments. */ export function rhyme1Span(w: Word): { from: number; to: number } { return { from: w.onset1.length, to: w.onset1.length + w.rhyme1.length }; } /** Where the syllable line falls: at the end of the first rhyme. */ export function syllableSplit(w: Word): number { return w.onset1.length + w.rhyme1.length; } /** * The whole hero, as arithmetic: what the two rhymes of a word do at a given * speaking rate, and what a bare foot of two rhymes does at the same rate. * * Two numbers come out of this, and they are different, and the difference is * the honest part: * * BARE FOOT. Nothing but two rhymes, so the ratio divides the whole foot: * * s1 = T·ρ/(1 + ρ) * gap = T·(2/(1+2) − 1.5/(1+1.5)) = T·(2/3 − 3/5) = T/15 * * which at T = 480 ms is 320 − 288 = 32 ms. That is the figure the page is * named after, and it is a property of the two published ratios alone. * * THE WORD. `kolli` also has consonants — k, its burst, and the l that opens * the second syllable — and their duration does not contrast in Estonian, * so they keep a fixed length and the ratio divides only what is left: * * r1 = (T − onset)·ρ/(1 + ρ) * gap = (T − onset)/15 * * which at T = 480 ms and onset = 117 ms is 242 − 217.8 = 24.2 ms. The same * arithmetic, over a shorter budget. * * The two words of a triple must be built of the same pieces — that is the * claim the page makes about them — so their onsets must be equal, and this * throws if they are not. Likewise the two centres must fall in their own * degrees, or the picture would be labelled with degrees the decision function * does not agree with. */ export function wordGap( tripleId: string, totalMs: number, ): { totalMs: number; onsetMs: number; bare: { s1q2: number; s1q3: number; gapMs: number }; word: { r1q2: number; r2q2: number; r1q3: number; r2q3: number; gapMs: number }; degrees: { q2: Degree; q3: Degree }; } { const triple = TRIPLES.find((t) => t.id === tripleId); if (!triple) throw new Error(`quantity: no triple "${tripleId}"`); const [, w2, w3] = triple.words; const onset = onsetMs(w2); if (onset !== onsetMs(w3)) { throw new Error(`quantity: the two ${tripleId} words do not share their onsets (${onset} vs ${onsetMs(w3)}) — they are supposed to differ only in the ratio`); } const bare2 = durations(totalMs, CENTRES[2]).s1; const bare3 = durations(totalMs, CENTRES[3]).s1; const r2 = rhymes(w2, totalMs, CENTRES[2]); const r3 = rhymes(w3, totalMs, CENTRES[3]); const degrees = { q2: degreeOf(CENTRES[2], PEAK_AT[2]), q3: degreeOf(CENTRES[3], PEAK_AT[3]) }; if (degrees.q2 !== 2 || degrees.q3 !== 3) { throw new Error(`quantity: the published centres no longer land in their own degrees (Q2 → ${degrees.q2}, Q3 → ${degrees.q3})`); } return { totalMs, onsetMs: onset, bare: { s1q2: bare2, s1q3: bare3, gapMs: bare3 - bare2 }, word: { r1q2: r2.r1, r2q2: r2.r2, r1q3: r3.r1, r2q3: r3.r2, gapMs: r3.r1 - r2.r1 }, degrees, }; }