@xromdev/apu 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/notes.d.ts CHANGED
@@ -132,6 +132,16 @@ export interface ApuInput {
132
132
  * speeds its opening up this way — whose ticks are not all the same length.
133
133
  */
134
134
  tempo?: readonly TempoChange[];
135
+ /**
136
+ * The pulses' duty sequencers left running from note to note: a note's `$4003`/`$4007` is
137
+ * written only when its high byte differs from the last one written, as drivers that write the
138
+ * high byte only on a change do (Konami's Yie Ar Kung-Fu). The write restarts the sequencer, so
139
+ * by default — every note, as Battletoads' driver writes it — a repeated note against the same
140
+ * note held on the other pulse lands at whatever phase its start gives, and in opposite phase the
141
+ * two cancel: the note goes quiet. Only where the write does nothing else: a constant level with
142
+ * the length counter halted (no envelope to restart, no length to reload).
143
+ */
144
+ keepPulsePhase?: boolean;
135
145
  }
136
146
  export interface TempoChange {
137
147
  tick: number;
package/dist/notes.js CHANGED
@@ -193,6 +193,11 @@ export function notesToWrites(input, lead = 0) {
193
193
  // Pulses.
194
194
  for (const channel of [0, 1]) {
195
195
  const base = 0x4000 + channel * 4;
196
+ /**
197
+ * The high byte last written to `$4003`/`$4007`, for `keepPulsePhase` — or -1 where the next
198
+ * note must write it anyway: before the first, or after a note whose length counter ran.
199
+ */
200
+ let lastHigh = -1;
196
201
  const voices = voicesOf(input.pitched,
197
202
  // Period 0 would be a divide by zero on the chip too; a row with none is
198
203
  // a rest, not a note.
@@ -210,7 +215,11 @@ export function notesToWrites(input, lead = 0) {
210
215
  put(voice.start, base, reg0(level(0)));
211
216
  put(voice.start, base + 1, sweep !== undefined && sweep >= 0 ? sweep : 0x08);
212
217
  put(voice.start, base + 2, period & 0xff);
213
- put(voice.start, base + 3, (bits.load << 3) | ((period >> 8) & 7));
218
+ const high = (period >> 8) & 7;
219
+ const restart = !input.keepPulsePhase || !bits.constant || !bits.halt || high !== lastHigh;
220
+ if (restart)
221
+ put(voice.start, base + 3, (bits.load << 3) | high);
222
+ lastHigh = bits.constant && bits.halt ? high : -1;
214
223
  const curve = bendIndex >= 0 ? (bends[bendIndex] ?? null) : null;
215
224
  const ramps = bits.constant && (levelCurve !== null || volume !== volumeEnd);
216
225
  if (curve || ramps) {
@@ -231,8 +240,11 @@ export function notesToWrites(input, lead = 0) {
231
240
  put(time, base + 2, now & 0xff);
232
241
  // The high byte only when it changes, as drivers do — writing it
233
242
  // restarts the sequencer, which is an audible click.
234
- if (now >> 8 !== lastPeriod >> 8)
243
+ if (now >> 8 !== lastPeriod >> 8) {
235
244
  put(time, base + 3, (bits.load << 3) | (now >> 8));
245
+ if (lastHigh >= 0)
246
+ lastHigh = now >> 8;
247
+ }
236
248
  }
237
249
  lastPeriod = now;
238
250
  }
package/dist/notes.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"notes.js","sourceRoot":"","sources":["../src/notes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAsD,MAAM,aAAa,CAAC;AAwC/F,0GAA0G;AAC1G,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAW,CAAC;AAS9G,6EAA6E;AAC7E,MAAM,UAAU,OAAO,CAAC,KAAiB,EAAE,QAAgB;IACzD,IAAI,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC/B,KAAK,MAAM,CAAC,EAAE,EAAE,KAA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AAO,EAAE,MAAM,CAAC,OAAO;QACvB,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,IAAI;QACJ,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,SAAS,EAAE,IAAI,GAAG,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;QACxF,OAAO,EAAE,IAAI,GAAG,GAAG;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Notes in, register writes out — the adapter between a decoded score and the\n * chip.\n *\n * A music driver's job is to turn its own data into register writes, once a\n * frame. When a game's music has already been decoded into notes (start, length,\n * period, volume, duty, and how each of those moves), this plays the driver's\n * part: it writes what a driver would have written to sound those notes, and\n * `renderWrites` does the rest. So the chip never learns what a note is, and\n * anything the adapter gets wrong is visible as a register write rather than\n * hidden inside a synth.\n *\n * The note shape is the one xrom.dev exports for Battletoads, but nothing in it\n * is that game's: a tick is whatever the caller says it is in seconds.\n *\n * What it writes, per note:\n * - at the start, the channel's full register set — which on a pulse restarts\n * the duty sequencer and the envelope and reloads the length counter,\n * exactly as the driver's own `$4003` write does;\n * - once a frame after that, the level while it ramps and the period while it\n * bends, as whole numbers, because the registers hold whole numbers — so a\n * fade is a staircase at 60 Hz, as it is on the console;\n * - at the end, silence — unless the next note on the channel starts at the\n * same moment.\n */\n\nimport { CPU_HZ, DMC_PERIODS, FRAME_HZ, LENGTH_TABLE } from './constants.js';\nimport { renderWrites, type Click, type RegisterWrite, type RenderOptions } from './render.js';\n\n/**\n * `[midi, startTick, durationTicks, channel, volume, period, duty,\n * volumeEnd, lengthTicks, bendIndex, envelope, sweep, levelIndex]` — `lengthTicks` counts at\n * 120 Hz and 0 means unlimited; `bendIndex` points into `ApuInput.bends`, or is\n * -1; `envelope` is the low five bits of `$4000` when the chip's own envelope\n * runs (period, and bit 4 for loop), or -1 for constant volume; `sweep` is the\n * `$4001` byte, or -1/absent when the data has none; `levelIndex`, on a pulse, points into\n * `ApuInput.levels` for a level that does not go in a straight line from `volume` to `volumeEnd`,\n * or is -1/absent.\n *\n * Channels 0..2 are the 2A03's pulses and triangle. 5 is the Disk System's wave channel (FDS) and\n * 6..13 the N163's channels in the order the chip switches them on — see `EXPANSION_CHANNELS`. On\n * those `period` is the frequency in hundredths of a hertz (their registers depend on the wave's\n * length and the chip's channel count, which the adapter chooses itself), `volume` is 0..15, and\n * `waveIndex` (the 14th field) points into `ApuInput.waves`: the waveform the note plays.\n *\n * 14 and 15 are the VRC6's pulses, 16 its sawtooth. There too `period` is hundredths of a hertz\n * and `volume` 0..15; on a pulse `duty` is the chip's 0..7 (high for duty + 1 of 16 steps), or 8\n * for its mode bit — the volume put out flat; on the saw `volume` stands for the accumulator rate\n * on a 0..15 scale, written as rate = round(volume · 42 / 15) (42 is the loudest clean saw).\n */\nexport type PitchedRow = readonly [\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number?,\n number?,\n number?,\n number?,\n];\n\n/** Pitched-row channels past the 2A03's: the FDS, the N163's eight, then the VRC6's pulse, pulse, saw. */\nexport const EXPANSION_CHANNELS = { fds: 5, n163: [6, 7, 8, 9, 10, 11, 12, 13], vrc6: [14, 15, 16] } as const;\n\n\n/**\n * A level curve: `[position 0..1, level 0..15]` at each change of level, in order. The level holds\n * from one point to the next — a driver writes whole levels, a frame at a time.\n */\nexport type LevelCurve = readonly (readonly [number, number])[];\n\n/** The level a curve holds at `position`: the last point at or before it. */\nexport function levelAt(curve: LevelCurve, position: number): number {\n let level = curve[0]?.[1] ?? 0;\n for (const [at, value] of curve) {\n if (at > position + 1e-9) break;\n level = value;\n }\n return level;\n}\n\n/** A pitch curve: `[position 0..1, semitones]`, in order. */\nexport type BendCurve = readonly (readonly [number, number])[];\n\n/**\n * `[periodIndex, startTick, durationTicks, volume, volumeEnd, lengthTicks,\n * envelope, levelIndex]` — a noise-channel note. `periodIndex` is the low nibble of `$400E`;\n * `levelIndex` points into `ApuInput.levels`, as a pulse row's does, or is -1/absent.\n */\nexport type DrumRow = readonly [number, number, number, number, number, number, number?, number?];\n\n/**\n * `[sampleIndex, startTick, durationTicks, rate, loop, level]` — a sample struck\n * on the DMC: `sampleIndex` into `ApuInput.samples`, `rate` the low nibble of\n * `$4010`, `loop` 1 to repeat it until the row ends, `level` the 7-bit value a\n * driver loads into `$4011` before it (the jump is part of the sound — a kick's\n * thump), or -1/absent for none. It plays to the end of its bytes or of the\n * row, whichever comes first.\n */\nexport type SampleRow = readonly [number, number, number, number, number, number?];\n\n/**\n * A DMC sample: the 1-bit delta bytes the channel reads, and where it reads them\n * — `$C000` + 64·n, as `$4012` can point. The bytes are the cartridge's own.\n */\nexport interface DmcSample {\n address: number;\n bytes: readonly number[] | Uint8Array;\n}\n\nexport interface ApuInput {\n pitched: readonly PitchedRow[];\n drums: readonly DrumRow[];\n /** Samples struck on the DMC, pointing into `samples`. */\n hits?: readonly SampleRow[];\n /** The samples the hits play. */\n samples?: readonly DmcSample[];\n /** Pitch curves the notes point at. Shared, because the shapes repeat. */\n bends?: readonly BendCurve[];\n /** Level curves the pulse and noise notes point at — see `PitchedRow`'s `levelIndex`. Shared like `bends`. */\n levels?: readonly LevelCurve[];\n /**\n * Waveforms the expansion notes play — see `PitchedRow`'s `waveIndex`: 64 samples 0..63 for the\n * FDS, a multiple of 4 samples 0..15 for an N163 channel.\n */\n waves?: readonly (readonly number[])[];\n /** Seconds per tick. */\n secondsPerTick: number;\n /** Length of the piece in ticks. */\n totalTicks: number;\n /**\n * Where the repeat starts, in ticks. Not always 0: a driver's first ticks are\n * often one-time setup, so a track that loops on the whole buffer replays its\n * intro every time round. Absent means the piece does not loop.\n */\n loopStart?: number;\n /**\n * Changes of tempo, as `{ tick, secondsPerTick }` in tick order: from `tick`\n * on, a tick lasts `secondsPerTick`. Before the first change it is the\n * input's own `secondsPerTick`. For drivers with a tempo command — Battletoads\n * speeds its opening up this way — whose ticks are not all the same length.\n */\n tempo?: readonly TempoChange[];\n}\n\nexport interface TempoChange {\n tick: number;\n secondsPerTick: number;\n}\n\n/**\n * Seconds from the start to `tick`, through the tempo changes. With none it is\n * exactly `tick * secondsPerTick` — the same float, so a piece without changes\n * renders as it always has.\n */\nexport function tickToSeconds(input: Pick<ApuInput, 'secondsPerTick' | 'tempo'>, tick: number): number {\n const changes = input.tempo;\n if (!changes || changes.length === 0) return tick * input.secondsPerTick;\n let seconds = 0;\n let from = 0;\n let spt = input.secondsPerTick;\n for (const change of changes) {\n if (change.tick >= tick) break;\n seconds += (change.tick - from) * spt;\n from = change.tick;\n spt = change.secondsPerTick;\n }\n return seconds + (tick - from) * spt;\n}\n\n/** The tick at `seconds` — the inverse of `tickToSeconds`, fractional. */\nexport function secondsToTick(input: Pick<ApuInput, 'secondsPerTick' | 'tempo'>, seconds: number): number {\n const changes = input.tempo;\n if (!changes || changes.length === 0) return seconds / input.secondsPerTick;\n let at = 0;\n let from = 0;\n let spt = input.secondsPerTick;\n for (const change of changes) {\n const end = at + (change.tick - from) * spt;\n if (end >= seconds) break;\n at = end;\n from = change.tick;\n spt = change.secondsPerTick;\n }\n return from + (seconds - at) / spt;\n}\n\nexport interface ApuOptions extends Omit<RenderOptions, 'clicks' | 'tail'> {\n /** Seconds of silence before the first note. */\n lead?: number;\n /** Click on every `metronome` ticks, loud on every fourth. 0 is off. */\n metronome?: number;\n}\n\nexport interface ApuRender {\n samples: Float32Array;\n sampleRate: number;\n /** Seconds of silence before tick 0 — what the caller asked for as `lead`. */\n lead: number;\n /** Length of the whole buffer, tail included. */\n duration: number;\n /**\n * Where the music itself ends, in seconds — the loop point. The buffer runs a\n * little longer so the filters can ring out; looping on the buffer would play\n * that tail as a gap every time round.\n */\n loopEnd: number;\n /** Where the repeat starts, in seconds — the other end of the seam. */\n loopStart: number;\n /** Loudest absolute sample, before clamping. */\n peak: number;\n}\n\n/**\n * Semitones at `position` through a note, straight-line between the curve's own\n * points; outside it, the nearest end.\n */\nexport function bendAt(curve: BendCurve, position: number): number {\n if (curve.length === 0) return 0;\n if (position <= curve[0][0]) return curve[0][1];\n for (let i = 1; i < curve.length; i++) {\n const [x1, y1] = curve[i];\n if (position <= x1) {\n const [x0, y0] = curve[i - 1];\n const span = x1 - x0;\n return span <= 0 ? y1 : y0 + ((y1 - y0) * (position - x0)) / span;\n }\n }\n return curve[curve.length - 1][1];\n}\n\n/** Length-counter load → the index `$4003`'s top five bits must hold for it. */\nconst lengthIndex = (halfFrames: number) => {\n const index = LENGTH_TABLE.indexOf(halfFrames as (typeof LENGTH_TABLE)[number]);\n if (index < 0) throw new Error(`no length-table entry loads ${halfFrames} half-frames`);\n return index;\n};\n\n/** The longest load, 254 half-frames (2.1 s) — for a note with no counter that cannot halt it. */\nconst LONGEST = 1;\n\n/** The `$4015` value: pulses and noise always on; the triangle toggled to end its notes. */\nconst enables = (triangle: boolean) => 0x0b | (triangle ? 0x04 : 0);\n\n/** Seconds a sample of `length` bytes takes at `$4010` rate `rate`. */\nexport const sampleSeconds = (length: number, rate: number) =>\n (length * 8 * DMC_PERIODS[rate & 0x0f]) / CPU_HZ;\n\n/**\n * The bytes `$4013` makes the DMC read: 16·n + 1. A sample is stored that long;\n * one that is not is read to the next length the register can say.\n */\nconst lengthCode = (bytes: number) => Math.max(0, Math.min(255, Math.ceil((bytes - 1) / 16)));\n\n/**\n * The cartridge memory the DMC reads, from the samples a piece holds: each laid\n * at its own address, the rest zero. For `renderWrites`' `memory`.\n */\nexport function sampleMemory(samples: readonly DmcSample[]): Uint8Array {\n const memory = new Uint8Array(0x8000);\n for (const sample of samples) {\n const at = sample.address - 0x8000;\n for (let i = 0; i < sample.bytes.length && at + i < memory.length; i++) memory[at + i] = sample.bytes[i];\n }\n return memory;\n}\n\n/** A register value can only hold whole numbers; a period outside 11 bits is not one the chip has. */\nconst toPeriod = (value: number) => Math.min(0x7ff, Math.max(0, Math.round(value)));\n\ninterface Voice {\n start: number;\n end: number;\n row: PitchedRow | DrumRow;\n}\n\n/**\n * The bits `$4000`/`$400C` share, from a row's envelope and length counter.\n *\n * Bit 5 is one bit with two jobs — it halts the length counter AND loops the\n * envelope — so a note that wants its envelope to decay once cannot also have\n * an unlimited counter. The rows that ask for both (`lengthTicks` 0 with a\n * one-shot envelope) get the longest load the table has instead, which outlasts\n * any note the envelope has not already silenced.\n */\nfunction controlBits(envelope: number, lengthTicks: number) {\n if (envelope >= 0) {\n const loop = (envelope & 0x10) !== 0;\n return {\n halt: loop,\n load: lengthTicks > 0 ? lengthIndex(lengthTicks) : LONGEST,\n low: envelope & 0x0f,\n constant: false,\n };\n }\n return {\n halt: lengthTicks === 0,\n load: lengthTicks > 0 ? lengthIndex(lengthTicks) : 0,\n low: 0,\n constant: true,\n };\n}\n\n/** Frame moments inside a note, after its start: when a driver would next touch it. */\nfunction* frames(start: number, end: number) {\n for (let k = 1; ; k++) {\n const time = start + k / FRAME_HZ;\n if (time >= end) return;\n yield { time, position: (time - start) / (end - start) };\n }\n}\n\n/**\n * The writes that play `input`, as a driver would make them.\n *\n * @param lead seconds of silence before tick 0\n */\nexport function notesToWrites(input: ApuInput, lead = 0): RegisterWrite[] {\n const at = (tick: number) => tickToSeconds(input, tick);\n const bends = input.bends ?? [];\n const writes: RegisterWrite[] = [];\n const put = (time: number, address: number, value: number) =>\n writes.push({ time, address, value });\n\n // Power-on housekeeping, before any note: every channel enabled, and both\n // sweep units given `$08` (disabled, negate). That is what drivers write and\n // why: at the power-on value of 0 the sweep's target overflows for every\n // period from `$400` up, which would mute the low pulse notes of a track that\n // never mentions `$4001`.\n put(0, 0x4015, enables(true));\n put(0, 0x4001, 0x08);\n put(0, 0x4005, 0x08);\n\n const voicesOf = (rows: readonly (PitchedRow | DrumRow)[], keep: (row: never) => boolean) =>\n rows\n .filter(keep as (row: PitchedRow | DrumRow) => boolean)\n .map((row) => ({\n start: lead + at(row[1]),\n end: lead + at(row[1] + row[2]),\n row,\n }))\n .filter((voice) => voice.end > voice.start)\n .sort((a, b) => a.start - b.start);\n\n // Pulses.\n for (const channel of [0, 1]) {\n const base = 0x4000 + channel * 4;\n const voices: Voice[] = voicesOf(\n input.pitched,\n // Period 0 would be a divide by zero on the chip too; a row with none is\n // a rest, not a note.\n ((row: PitchedRow) => row[3] === channel && row[5] >= 1) as (row: never) => boolean\n );\n voices.forEach((voice, i) => {\n const [, , , , volume, period, duty, volumeEnd, lengthTicks, bendIndex, envelope, sweep, levelIndex] =\n voice.row as PitchedRow;\n const levelCurve =\n levelIndex !== undefined && levelIndex >= 0 ? (input.levels?.[levelIndex] ?? null) : null;\n const bits = controlBits(envelope ?? -1, lengthTicks);\n const reg0 = (level: number) =>\n ((duty & 3) << 6) |\n (bits.halt ? 0x20 : 0) |\n (bits.constant ? 0x10 | (level & 0x0f) : bits.low);\n const level = (at: number) =>\n levelCurve\n ? Math.min(15, Math.max(0, levelAt(levelCurve, at)))\n : Math.min(15, Math.max(0, Math.round(volume + (volumeEnd - volume) * at)));\n\n put(voice.start, base, reg0(level(0)));\n put(voice.start, base + 1, sweep !== undefined && sweep >= 0 ? sweep : 0x08);\n put(voice.start, base + 2, period & 0xff);\n put(voice.start, base + 3, (bits.load << 3) | ((period >> 8) & 7));\n\n const curve = bendIndex >= 0 ? (bends[bendIndex] ?? null) : null;\n const ramps = bits.constant && (levelCurve !== null || volume !== volumeEnd);\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastPeriod = period;\n for (const { time, position } of frames(voice.start, voice.end)) {\n if (ramps) {\n const now = level(position);\n if (now !== lastLevel) put(time, base, reg0(now));\n lastLevel = now;\n }\n if (curve) {\n // A semitone is a frequency ratio; the period is a divider, so it\n // moves the other way.\n const now = toPeriod((period + 1) * Math.pow(2, -bendAt(curve, position) / 12) - 1);\n if (now !== lastPeriod) {\n put(time, base + 2, now & 0xff);\n // The high byte only when it changes, as drivers do — writing it\n // restarts the sequencer, which is an audible click.\n if (now >> 8 !== lastPeriod >> 8) put(time, base + 3, (bits.load << 3) | (now >> 8));\n }\n lastPeriod = now;\n }\n }\n }\n\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, base, ((duty & 3) << 6) | 0x30);\n });\n }\n\n /*\n * The DMC's hits, worked out first: `$4015` is one register, and the triangle's\n * on/off writes to it clear the DMC's bit unless they set it — which stops a\n * sample dead. So every `$4015` write carries bit 4 while a sample is still\n * playing, and only then: set after it has ended, the bit starts it again.\n */\n const samples = input.samples ?? [];\n const hits = (input.hits ?? [])\n .map((row) => {\n const sample = samples[row[0]];\n if (!sample) return null;\n const start = lead + at(row[1]);\n const rowEnd = lead + at(row[1] + row[2]);\n const length = lengthCode(sample.bytes.length) * 16 + 1;\n const end = row[4] ? rowEnd : Math.min(rowEnd, start + sampleSeconds(length, row[3]));\n return end > start ? { row, sample, start, end, length } : null;\n })\n .filter((hit) => hit !== null)\n .sort((a, b) => a.start - b.start);\n const sampleOn = (time: number) => hits.some((hit) => hit.start <= time && time < hit.end);\n const withSample = (value: number, time: number) => (sampleOn(time) ? value | 0x10 : value);\n\n // Triangle. No volume: it is ended by switching it off in `$4015`, the one\n // way to stop it on the spot — and it then holds whatever step it stopped on.\n {\n const voices: Voice[] = voicesOf(input.pitched, ((row: PitchedRow) =>\n row[3] === 2 && row[5] >= 1) as (row: never) => boolean);\n let on = true;\n voices.forEach((voice, i) => {\n const [, , , , , period, , , lengthTicks, bendIndex] = voice.row as PitchedRow;\n // With a counter to honour, the control bit is clear and the linear\n // counter set to its longest (127 quarter-frames, 0.53 s) so the length\n // counter is the one that ends the note; with none, the control bit set\n // halts both.\n const load = lengthTicks > 0 ? lengthIndex(lengthTicks) : 0;\n if (!on) put(voice.start, 0x4015, withSample(enables(true), voice.start));\n on = true;\n put(voice.start, 0x4008, lengthTicks > 0 ? 0x7f : 0xff);\n put(voice.start, 0x400a, period & 0xff);\n put(voice.start, 0x400b, (load << 3) | ((period >> 8) & 7));\n\n const curve = bendIndex >= 0 ? (bends[bendIndex] ?? null) : null;\n if (curve) {\n let last = period;\n for (const { time, position } of frames(voice.start, voice.end)) {\n const now = toPeriod((period + 1) * Math.pow(2, -bendAt(curve, position) / 12) - 1);\n if (now !== last) {\n put(time, 0x400a, now & 0xff);\n if (now >> 8 !== last >> 8) put(time, 0x400b, (load << 3) | (now >> 8));\n }\n last = now;\n }\n }\n\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) {\n put(voice.end, 0x4015, withSample(enables(false), voice.end));\n on = false;\n }\n });\n }\n\n // Noise.\n {\n const voices: Voice[] = voicesOf(input.drums, (() => true) as (row: never) => boolean);\n voices.forEach((voice, i) => {\n const [periodIndex, , , volume, volumeEnd, lengthTicks, envelope, levelIndex] = voice.row as DrumRow;\n const bits = controlBits(envelope ?? -1, lengthTicks);\n const reg = (level: number) =>\n (bits.halt ? 0x20 : 0) | (bits.constant ? 0x10 | (level & 0x0f) : bits.low);\n // A drum struck at 8 and dropped to 2 the next frame — Darkwing Duck's — is that curve, not a line.\n const levelCurve =\n levelIndex !== undefined && levelIndex >= 0 ? (input.levels?.[levelIndex] ?? null) : null;\n const level = (at: number) =>\n levelCurve\n ? Math.min(15, Math.max(0, levelAt(levelCurve, at)))\n : Math.min(15, Math.max(0, Math.round(volume + (volumeEnd - volume) * at)));\n\n put(voice.start, 0x400c, reg(level(0)));\n put(voice.start, 0x400e, periodIndex & 0x0f);\n put(voice.start, 0x400f, bits.load << 3);\n\n if (bits.constant && (levelCurve !== null || volume !== volumeEnd)) {\n let last = level(0);\n for (const { time, position } of frames(voice.start, voice.end)) {\n const now = level(position);\n if (now !== last) put(time, 0x400c, reg(now));\n last = now;\n }\n }\n\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, 0x400c, 0x30);\n });\n }\n\n // The DMC: rate and loop, where the sample is and how long, then bit 4 of\n // `$4015` to start it. A hit cut short by its row — or by the next — is\n // stopped by clearing the bit. The triangle's own `$4015` state goes with it.\n hits.forEach((hit, i) => {\n const [, , , rate, loop, level] = hit.row;\n const triangleOn = (time: number) => {\n // The last word the triangle had on `$4015` before `time`: on unless it was switched off.\n let state = true;\n for (const w of writes) if (w.address === 0x4015 && w.time <= time) state = (w.value & 0x04) !== 0;\n return state;\n };\n put(hit.start, 0x4010, (loop ? 0x40 : 0) | (rate & 0x0f));\n if (level !== undefined && level >= 0) put(hit.start, 0x4011, level & 0x7f);\n put(hit.start, 0x4012, ((hit.sample.address - 0xc000) >> 6) & 0xff);\n put(hit.start, 0x4013, lengthCode(hit.sample.bytes.length));\n // Off and on: bit 4 set while the last sample is still reading does not restart it.\n put(hit.start, 0x4015, enables(triangleOn(hit.start)));\n put(hit.start, 0x4015, enables(triangleOn(hit.start)) | 0x10);\n const next = hits[i + 1];\n const natural = hit.start + sampleSeconds(hit.length, rate);\n if (hit.end < natural - 1e-9 && (!next || next.start > hit.end + 1e-9)) {\n put(hit.end, 0x4015, enables(triangleOn(hit.end)));\n }\n });\n\n expansionWrites(input, lead, voicesOf, frames, put);\n return writes;\n}\n\nconst hzOf = (row: PitchedRow) => row[5] / 100;\n\n/**\n * The expansion channels' notes as writes: what a driver for the FDS, the VRC6 or the N163 would write to\n * sound them — the waveform when it changes, then the frequency and level, and once a frame the\n * level while it ramps and the frequency while it bends.\n */\nfunction expansionWrites(\n input: ApuInput,\n lead: number,\n voicesOf: (rows: readonly (PitchedRow | DrumRow)[], keep: (row: never) => boolean) => Voice[],\n frames: (start: number, end: number) => Iterable<{ time: number; position: number }>,\n put: (time: number, address: number, value: number) => void\n) {\n const bends = input.bends ?? [];\n const waves = input.waves ?? [];\n const curveOf = (row: PitchedRow) => (row[9] >= 0 ? (bends[row[9]] ?? null) : null);\n const levelsOf = (row: PitchedRow) =>\n row[12] !== undefined && row[12] >= 0 ? (input.levels?.[row[12]] ?? null) : null;\n const levelAtOf = (row: PitchedRow) => {\n const curve = levelsOf(row);\n return (at: number) =>\n curve\n ? Math.min(15, Math.max(0, levelAt(curve, at)))\n : Math.min(15, Math.max(0, Math.round(row[4] + (row[7] - row[4]) * at)));\n };\n\n // FDS.\n {\n const voices = voicesOf(input.pitched, ((row: PitchedRow) =>\n row[3] === EXPANSION_CHANNELS.fds && row[5] > 0) as (row: never) => boolean);\n if (voices.length > 0) {\n put(0, 0x4087, 0x80); // modulation off: the bends carry the vibrato\n put(0, 0x408a, 0xe8);\n }\n const freqOf = (hz: number) => Math.min(0xfff, Math.max(1, Math.round((hz * 4194304) / CPU_HZ)));\n let lastWave = -1;\n voices.forEach((voice, i) => {\n const row = voice.row as PitchedRow;\n const waveIndex = row[13] ?? -1;\n if (waveIndex >= 0 && waveIndex !== lastWave && waves[waveIndex]) {\n put(voice.start, 0x4089, 0x80);\n waves[waveIndex].slice(0, 64).forEach((v, k) => put(voice.start, 0x4040 + k, v & 0x3f));\n put(voice.start, 0x4089, 0x00);\n lastWave = waveIndex;\n }\n const level = levelAtOf(row);\n const gain = (l: number) => 0x80 | Math.round((l * 32) / 15);\n const freq = freqOf(hzOf(row));\n put(voice.start, 0x4080, gain(level(0)));\n put(voice.start, 0x4082, freq & 0xff);\n put(voice.start, 0x4083, 0x40 | (freq >> 8));\n const curve = curveOf(row);\n const ramps = levelsOf(row) !== null || row[4] !== row[7];\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastFreq = freq;\n for (const { time, position } of frames(voice.start, voice.end)) {\n if (ramps) {\n const now = level(position);\n if (now !== lastLevel) put(time, 0x4080, gain(now));\n lastLevel = now;\n }\n if (curve) {\n const now = freqOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12));\n if (now !== lastFreq) {\n put(time, 0x4082, now & 0xff);\n if (now >> 8 !== lastFreq >> 8) put(time, 0x4083, 0x40 | (now >> 8));\n }\n lastFreq = now;\n }\n }\n }\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, 0x4080, 0x80);\n });\n }\n\n // VRC6: two pulses and the saw, each its three registers; the period counts CPU cycles.\n {\n let started = false;\n EXPANSION_CHANNELS.vrc6.forEach((channel, k) => {\n const voices = voicesOf(input.pitched, ((row: PitchedRow) => row[3] === channel && row[5] > 0) as (row: never) => boolean);\n if (voices.length === 0) return;\n if (!started) put(0, 0x9003, 0x00); // running, no frequency shift\n started = true;\n const base = 0x9000 + k * 0x1000;\n const saw = k === 2;\n const periodOf = (hz: number) =>\n Math.min(0xfff, Math.max(0, Math.round(CPU_HZ / ((saw ? 14 : 16) * hz)) - 1));\n voices.forEach((voice, i) => {\n const row = voice.row as PitchedRow;\n const level = levelAtOf(row);\n const control = saw\n ? (l: number) => Math.min(0x3f, Math.round((l * 42) / 15))\n : (l: number) => (row[6] >= 8 ? 0x80 : (Math.max(0, row[6]) & 7) << 4) | l;\n const period = periodOf(hzOf(row));\n put(voice.start, base, control(level(0)));\n put(voice.start, base + 1, period & 0xff);\n put(voice.start, base + 2, 0x80 | (period >> 8));\n const curve = curveOf(row);\n const ramps = levelsOf(row) !== null || row[4] !== row[7];\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastPeriod = period;\n for (const { time, position } of frames(voice.start, voice.end)) {\n if (ramps) {\n const now = level(position);\n if (now !== lastLevel) put(time, base, control(now));\n lastLevel = now;\n }\n if (curve) {\n const now = periodOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12));\n if (now !== lastPeriod) {\n put(time, base + 1, now & 0xff);\n // With E kept set: clearing it would restart the wave.\n if (now >> 8 !== lastPeriod >> 8) put(time, base + 2, 0x80 | (now >> 8));\n }\n lastPeriod = now;\n }\n }\n }\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, base + 2, period >> 8);\n });\n });\n }\n\n // N163: as many channels on as the piece uses, each with its own stretch of the chip's RAM.\n const n163Voices = EXPANSION_CHANNELS.n163.map((channel) =>\n voicesOf(input.pitched, ((row: PitchedRow) => row[3] === channel && row[5] > 0) as (row: never) => boolean)\n );\n const used = n163Voices.reduce((most, voices, k) => (voices.length > 0 ? k + 1 : most), 0);\n if (used === 0) return;\n const count = used;\n // Registers take the top 8·count bytes; the rest is wave RAM, shared out evenly, in whole fours.\n const freeNibbles = (0x80 - 8 * count) * 2;\n const area = Math.max(4, Math.floor(freeNibbles / count / 4) * 4);\n const ramWrite = (time: number, address: number, bytes: readonly number[]) => {\n put(time, 0xf800, 0x80 | (address & 0x7f));\n for (const b of bytes) put(time, 0x4800, b & 0xff);\n };\n // The channel count lives in the top nibble of $7F, beside slot 7's volume.\n const countBits = (count - 1) << 4;\n ramWrite(0, 0x7f, [countBits]);\n /*\n * When every waveform the piece plays fits in the wave RAM at once, each gets a place of its own,\n * written before the first note, and a channel just points at it — as Namco's drivers do, several\n * channels reading one wave. Only when they do not fit does each channel get a stretch of its own\n * that its waves are written into as they come (and squeezed into, if longer).\n */\n const n163Waves = [...new Set(n163Voices.flat().map((voice) => (voice.row as PitchedRow)[13] ?? -1))].filter(\n (index) => index >= 0 && waves[index]\n );\n const fitted = (index: number) => Math.min(256, Math.max(4, Math.floor(waves[index].length / 4) * 4));\n const shared = new Map<number, { start: number; length: number }>();\n if (n163Waves.reduce((sum, index) => sum + fitted(index), 0) <= freeNibbles) {\n let at = 0;\n for (const index of n163Waves) {\n const length = fitted(index);\n shared.set(index, { start: at, length });\n const wave = waves[index];\n const bytes: number[] = [];\n for (let n = 0; n < length; n += 2) bytes.push((wave[n] & 0x0f) | ((wave[n + 1] ?? 0) & 0x0f) << 4);\n ramWrite(0, at >> 1, bytes);\n at += length;\n }\n }\n n163Voices.forEach((voices, k) => {\n const slot = 7 - k;\n const base = 0x40 + slot * 8;\n let start = k * area; // in nibbles\n let lastWave = -1;\n let length = area;\n const volumeByte = (v: number) => (slot === 7 ? countBits : 0) | (v & 0x0f);\n voices.forEach((voice, i) => {\n const row = voice.row as PitchedRow;\n const waveIndex = row[13] ?? -1;\n const wave = waveIndex >= 0 ? waves[waveIndex] : null;\n const place = shared.get(waveIndex);\n if (place) {\n start = place.start;\n length = place.length;\n } else if (wave && waveIndex !== lastWave) {\n start = k * area;\n // A wave longer than the stretch this channel has is resampled to fit.\n length = Math.min(area, Math.max(4, Math.floor(wave.length / 4) * 4));\n const samples = Array.from({ length }, (_, n) => wave[Math.floor((n * wave.length) / length)] & 0x0f);\n const bytes: number[] = [];\n for (let n = 0; n < length; n += 2) bytes.push(samples[n] | ((samples[n + 1] ?? 0) << 4));\n ramWrite(voice.start, start >> 1, bytes);\n lastWave = waveIndex;\n }\n const freqOf = (hz: number) =>\n Math.min(0x3ffff, Math.max(1, Math.round((hz * 15 * 65536 * count * length) / CPU_HZ)));\n const regs = (freq: number, volume: number) => [\n freq & 0xff,\n 0,\n (freq >> 8) & 0xff,\n 0,\n ((256 - length) & 0xfc) | ((freq >> 16) & 3),\n 0,\n start & 0xff,\n volumeByte(volume),\n ];\n const level = levelAtOf(row);\n const freq = freqOf(hzOf(row));\n ramWrite(voice.start, base, regs(freq, level(0)));\n const curve = curveOf(row);\n const ramps = levelsOf(row) !== null || row[4] !== row[7];\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastFreq = freq;\n for (const { time, position } of frames(voice.start, voice.end)) {\n const nowLevel = ramps ? level(position) : lastLevel;\n const nowFreq = curve ? freqOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12)) : lastFreq;\n if (nowFreq !== lastFreq) {\n // The frequency bytes alone, not the phase after them: that is the chip's running count.\n ramWrite(time, base, [nowFreq & 0xff]);\n ramWrite(time, base + 2, [(nowFreq >> 8) & 0xff]);\n ramWrite(time, base + 4, [((256 - length) & 0xfc) | ((nowFreq >> 16) & 3)]);\n }\n if (nowLevel !== lastLevel) ramWrite(time, base + 7, [volumeByte(nowLevel)]);\n lastLevel = nowLevel;\n lastFreq = nowFreq;\n }\n }\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) ramWrite(voice.end, base + 7, [volumeByte(0)]);\n });\n });\n}\n\n/** Render a decoded piece: `notesToWrites` into `renderWrites`, with the tick-based extras. */\nexport function renderNotes(input: ApuInput, options: ApuOptions = {}): ApuRender {\n const lead = options.lead ?? 0;\n\n const clicks: Click[] = [];\n if (options.metronome && options.metronome > 0) {\n for (let tick = 0, beat = 0; tick < input.totalTicks; tick += options.metronome, beat++) {\n // Four beats to a bar: the downbeat is what makes a tempo readable.\n clicks.push({ time: lead + tickToSeconds(input, tick), strong: beat % 4 === 0 });\n }\n }\n\n const { lead: _lead, metronome: _metronome, ...render } = options;\n const end = tickToSeconds(input, input.totalTicks);\n const result = renderWrites(notesToWrites(input, lead), lead + end, {\n ...render,\n clicks,\n ...(input.samples?.length && !render.memory ? { memory: sampleMemory(input.samples) } : {}),\n });\n\n return {\n samples: result.samples,\n sampleRate: result.sampleRate,\n lead,\n duration: result.duration,\n loopStart: lead + tickToSeconds(input, Math.min(input.loopStart ?? 0, input.totalTicks)),\n loopEnd: lead + end,\n peak: result.peak,\n };\n}\n"]}
1
+ {"version":3,"file":"notes.js","sourceRoot":"","sources":["../src/notes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EAAE,YAAY,EAAsD,MAAM,aAAa,CAAC;AAwC/F,0GAA0G;AAC1G,MAAM,CAAC,MAAM,kBAAkB,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAW,CAAC;AAS9G,6EAA6E;AAC7E,MAAM,UAAU,OAAO,CAAC,KAAiB,EAAE,QAAgB;IACzD,IAAI,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC/B,KAAK,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,IAAI,K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,GAAG,OAAO,CAAC;IAClE,MAAM,GAAG,GAAG,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;IACnD,MAAM,MAAM,GAAG,YAAY,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,GAAG,EAAE;QAClE,GAAG,MAAM;QACT,MAAM;QACN,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC5F,CAAC,CAAC;IAEH,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,IAAI;QACJ,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,SAAS,EAAE,IAAI,GAAG,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;QACxF,OAAO,EAAE,IAAI,GAAG,GAAG;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;KAClB,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Notes in, register writes out — the adapter between a decoded score and the\n * chip.\n *\n * A music driver's job is to turn its own data into register writes, once a\n * frame. When a game's music has already been decoded into notes (start, length,\n * period, volume, duty, and how each of those moves), this plays the driver's\n * part: it writes what a driver would have written to sound those notes, and\n * `renderWrites` does the rest. So the chip never learns what a note is, and\n * anything the adapter gets wrong is visible as a register write rather than\n * hidden inside a synth.\n *\n * The note shape is the one xrom.dev exports for Battletoads, but nothing in it\n * is that game's: a tick is whatever the caller says it is in seconds.\n *\n * What it writes, per note:\n * - at the start, the channel's full register set — which on a pulse restarts\n * the duty sequencer and the envelope and reloads the length counter,\n * exactly as the driver's own `$4003` write does;\n * - once a frame after that, the level while it ramps and the period while it\n * bends, as whole numbers, because the registers hold whole numbers — so a\n * fade is a staircase at 60 Hz, as it is on the console;\n * - at the end, silence — unless the next note on the channel starts at the\n * same moment.\n */\n\nimport { CPU_HZ, DMC_PERIODS, FRAME_HZ, LENGTH_TABLE } from './constants.js';\nimport { renderWrites, type Click, type RegisterWrite, type RenderOptions } from './render.js';\n\n/**\n * `[midi, startTick, durationTicks, channel, volume, period, duty,\n * volumeEnd, lengthTicks, bendIndex, envelope, sweep, levelIndex]` — `lengthTicks` counts at\n * 120 Hz and 0 means unlimited; `bendIndex` points into `ApuInput.bends`, or is\n * -1; `envelope` is the low five bits of `$4000` when the chip's own envelope\n * runs (period, and bit 4 for loop), or -1 for constant volume; `sweep` is the\n * `$4001` byte, or -1/absent when the data has none; `levelIndex`, on a pulse, points into\n * `ApuInput.levels` for a level that does not go in a straight line from `volume` to `volumeEnd`,\n * or is -1/absent.\n *\n * Channels 0..2 are the 2A03's pulses and triangle. 5 is the Disk System's wave channel (FDS) and\n * 6..13 the N163's channels in the order the chip switches them on — see `EXPANSION_CHANNELS`. On\n * those `period` is the frequency in hundredths of a hertz (their registers depend on the wave's\n * length and the chip's channel count, which the adapter chooses itself), `volume` is 0..15, and\n * `waveIndex` (the 14th field) points into `ApuInput.waves`: the waveform the note plays.\n *\n * 14 and 15 are the VRC6's pulses, 16 its sawtooth. There too `period` is hundredths of a hertz\n * and `volume` 0..15; on a pulse `duty` is the chip's 0..7 (high for duty + 1 of 16 steps), or 8\n * for its mode bit — the volume put out flat; on the saw `volume` stands for the accumulator rate\n * on a 0..15 scale, written as rate = round(volume · 42 / 15) (42 is the loudest clean saw).\n */\nexport type PitchedRow = readonly [\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number,\n number?,\n number?,\n number?,\n number?,\n];\n\n/** Pitched-row channels past the 2A03's: the FDS, the N163's eight, then the VRC6's pulse, pulse, saw. */\nexport const EXPANSION_CHANNELS = { fds: 5, n163: [6, 7, 8, 9, 10, 11, 12, 13], vrc6: [14, 15, 16] } as const;\n\n\n/**\n * A level curve: `[position 0..1, level 0..15]` at each change of level, in order. The level holds\n * from one point to the next — a driver writes whole levels, a frame at a time.\n */\nexport type LevelCurve = readonly (readonly [number, number])[];\n\n/** The level a curve holds at `position`: the last point at or before it. */\nexport function levelAt(curve: LevelCurve, position: number): number {\n let level = curve[0]?.[1] ?? 0;\n for (const [at, value] of curve) {\n if (at > position + 1e-9) break;\n level = value;\n }\n return level;\n}\n\n/** A pitch curve: `[position 0..1, semitones]`, in order. */\nexport type BendCurve = readonly (readonly [number, number])[];\n\n/**\n * `[periodIndex, startTick, durationTicks, volume, volumeEnd, lengthTicks,\n * envelope, levelIndex]` — a noise-channel note. `periodIndex` is the low nibble of `$400E`;\n * `levelIndex` points into `ApuInput.levels`, as a pulse row's does, or is -1/absent.\n */\nexport type DrumRow = readonly [number, number, number, number, number, number, number?, number?];\n\n/**\n * `[sampleIndex, startTick, durationTicks, rate, loop, level]` — a sample struck\n * on the DMC: `sampleIndex` into `ApuInput.samples`, `rate` the low nibble of\n * `$4010`, `loop` 1 to repeat it until the row ends, `level` the 7-bit value a\n * driver loads into `$4011` before it (the jump is part of the sound — a kick's\n * thump), or -1/absent for none. It plays to the end of its bytes or of the\n * row, whichever comes first.\n */\nexport type SampleRow = readonly [number, number, number, number, number, number?];\n\n/**\n * A DMC sample: the 1-bit delta bytes the channel reads, and where it reads them\n * — `$C000` + 64·n, as `$4012` can point. The bytes are the cartridge's own.\n */\nexport interface DmcSample {\n address: number;\n bytes: readonly number[] | Uint8Array;\n}\n\nexport interface ApuInput {\n pitched: readonly PitchedRow[];\n drums: readonly DrumRow[];\n /** Samples struck on the DMC, pointing into `samples`. */\n hits?: readonly SampleRow[];\n /** The samples the hits play. */\n samples?: readonly DmcSample[];\n /** Pitch curves the notes point at. Shared, because the shapes repeat. */\n bends?: readonly BendCurve[];\n /** Level curves the pulse and noise notes point at — see `PitchedRow`'s `levelIndex`. Shared like `bends`. */\n levels?: readonly LevelCurve[];\n /**\n * Waveforms the expansion notes play — see `PitchedRow`'s `waveIndex`: 64 samples 0..63 for the\n * FDS, a multiple of 4 samples 0..15 for an N163 channel.\n */\n waves?: readonly (readonly number[])[];\n /** Seconds per tick. */\n secondsPerTick: number;\n /** Length of the piece in ticks. */\n totalTicks: number;\n /**\n * Where the repeat starts, in ticks. Not always 0: a driver's first ticks are\n * often one-time setup, so a track that loops on the whole buffer replays its\n * intro every time round. Absent means the piece does not loop.\n */\n loopStart?: number;\n /**\n * Changes of tempo, as `{ tick, secondsPerTick }` in tick order: from `tick`\n * on, a tick lasts `secondsPerTick`. Before the first change it is the\n * input's own `secondsPerTick`. For drivers with a tempo command — Battletoads\n * speeds its opening up this way — whose ticks are not all the same length.\n */\n tempo?: readonly TempoChange[];\n /**\n * The pulses' duty sequencers left running from note to note: a note's `$4003`/`$4007` is\n * written only when its high byte differs from the last one written, as drivers that write the\n * high byte only on a change do (Konami's Yie Ar Kung-Fu). The write restarts the sequencer, so\n * by default — every note, as Battletoads' driver writes it — a repeated note against the same\n * note held on the other pulse lands at whatever phase its start gives, and in opposite phase the\n * two cancel: the note goes quiet. Only where the write does nothing else: a constant level with\n * the length counter halted (no envelope to restart, no length to reload).\n */\n keepPulsePhase?: boolean;\n}\n\nexport interface TempoChange {\n tick: number;\n secondsPerTick: number;\n}\n\n/**\n * Seconds from the start to `tick`, through the tempo changes. With none it is\n * exactly `tick * secondsPerTick` — the same float, so a piece without changes\n * renders as it always has.\n */\nexport function tickToSeconds(input: Pick<ApuInput, 'secondsPerTick' | 'tempo'>, tick: number): number {\n const changes = input.tempo;\n if (!changes || changes.length === 0) return tick * input.secondsPerTick;\n let seconds = 0;\n let from = 0;\n let spt = input.secondsPerTick;\n for (const change of changes) {\n if (change.tick >= tick) break;\n seconds += (change.tick - from) * spt;\n from = change.tick;\n spt = change.secondsPerTick;\n }\n return seconds + (tick - from) * spt;\n}\n\n/** The tick at `seconds` — the inverse of `tickToSeconds`, fractional. */\nexport function secondsToTick(input: Pick<ApuInput, 'secondsPerTick' | 'tempo'>, seconds: number): number {\n const changes = input.tempo;\n if (!changes || changes.length === 0) return seconds / input.secondsPerTick;\n let at = 0;\n let from = 0;\n let spt = input.secondsPerTick;\n for (const change of changes) {\n const end = at + (change.tick - from) * spt;\n if (end >= seconds) break;\n at = end;\n from = change.tick;\n spt = change.secondsPerTick;\n }\n return from + (seconds - at) / spt;\n}\n\nexport interface ApuOptions extends Omit<RenderOptions, 'clicks' | 'tail'> {\n /** Seconds of silence before the first note. */\n lead?: number;\n /** Click on every `metronome` ticks, loud on every fourth. 0 is off. */\n metronome?: number;\n}\n\nexport interface ApuRender {\n samples: Float32Array;\n sampleRate: number;\n /** Seconds of silence before tick 0 — what the caller asked for as `lead`. */\n lead: number;\n /** Length of the whole buffer, tail included. */\n duration: number;\n /**\n * Where the music itself ends, in seconds — the loop point. The buffer runs a\n * little longer so the filters can ring out; looping on the buffer would play\n * that tail as a gap every time round.\n */\n loopEnd: number;\n /** Where the repeat starts, in seconds — the other end of the seam. */\n loopStart: number;\n /** Loudest absolute sample, before clamping. */\n peak: number;\n}\n\n/**\n * Semitones at `position` through a note, straight-line between the curve's own\n * points; outside it, the nearest end.\n */\nexport function bendAt(curve: BendCurve, position: number): number {\n if (curve.length === 0) return 0;\n if (position <= curve[0][0]) return curve[0][1];\n for (let i = 1; i < curve.length; i++) {\n const [x1, y1] = curve[i];\n if (position <= x1) {\n const [x0, y0] = curve[i - 1];\n const span = x1 - x0;\n return span <= 0 ? y1 : y0 + ((y1 - y0) * (position - x0)) / span;\n }\n }\n return curve[curve.length - 1][1];\n}\n\n/** Length-counter load → the index `$4003`'s top five bits must hold for it. */\nconst lengthIndex = (halfFrames: number) => {\n const index = LENGTH_TABLE.indexOf(halfFrames as (typeof LENGTH_TABLE)[number]);\n if (index < 0) throw new Error(`no length-table entry loads ${halfFrames} half-frames`);\n return index;\n};\n\n/** The longest load, 254 half-frames (2.1 s) — for a note with no counter that cannot halt it. */\nconst LONGEST = 1;\n\n/** The `$4015` value: pulses and noise always on; the triangle toggled to end its notes. */\nconst enables = (triangle: boolean) => 0x0b | (triangle ? 0x04 : 0);\n\n/** Seconds a sample of `length` bytes takes at `$4010` rate `rate`. */\nexport const sampleSeconds = (length: number, rate: number) =>\n (length * 8 * DMC_PERIODS[rate & 0x0f]) / CPU_HZ;\n\n/**\n * The bytes `$4013` makes the DMC read: 16·n + 1. A sample is stored that long;\n * one that is not is read to the next length the register can say.\n */\nconst lengthCode = (bytes: number) => Math.max(0, Math.min(255, Math.ceil((bytes - 1) / 16)));\n\n/**\n * The cartridge memory the DMC reads, from the samples a piece holds: each laid\n * at its own address, the rest zero. For `renderWrites`' `memory`.\n */\nexport function sampleMemory(samples: readonly DmcSample[]): Uint8Array {\n const memory = new Uint8Array(0x8000);\n for (const sample of samples) {\n const at = sample.address - 0x8000;\n for (let i = 0; i < sample.bytes.length && at + i < memory.length; i++) memory[at + i] = sample.bytes[i];\n }\n return memory;\n}\n\n/** A register value can only hold whole numbers; a period outside 11 bits is not one the chip has. */\nconst toPeriod = (value: number) => Math.min(0x7ff, Math.max(0, Math.round(value)));\n\ninterface Voice {\n start: number;\n end: number;\n row: PitchedRow | DrumRow;\n}\n\n/**\n * The bits `$4000`/`$400C` share, from a row's envelope and length counter.\n *\n * Bit 5 is one bit with two jobs — it halts the length counter AND loops the\n * envelope — so a note that wants its envelope to decay once cannot also have\n * an unlimited counter. The rows that ask for both (`lengthTicks` 0 with a\n * one-shot envelope) get the longest load the table has instead, which outlasts\n * any note the envelope has not already silenced.\n */\nfunction controlBits(envelope: number, lengthTicks: number) {\n if (envelope >= 0) {\n const loop = (envelope & 0x10) !== 0;\n return {\n halt: loop,\n load: lengthTicks > 0 ? lengthIndex(lengthTicks) : LONGEST,\n low: envelope & 0x0f,\n constant: false,\n };\n }\n return {\n halt: lengthTicks === 0,\n load: lengthTicks > 0 ? lengthIndex(lengthTicks) : 0,\n low: 0,\n constant: true,\n };\n}\n\n/** Frame moments inside a note, after its start: when a driver would next touch it. */\nfunction* frames(start: number, end: number) {\n for (let k = 1; ; k++) {\n const time = start + k / FRAME_HZ;\n if (time >= end) return;\n yield { time, position: (time - start) / (end - start) };\n }\n}\n\n/**\n * The writes that play `input`, as a driver would make them.\n *\n * @param lead seconds of silence before tick 0\n */\nexport function notesToWrites(input: ApuInput, lead = 0): RegisterWrite[] {\n const at = (tick: number) => tickToSeconds(input, tick);\n const bends = input.bends ?? [];\n const writes: RegisterWrite[] = [];\n const put = (time: number, address: number, value: number) =>\n writes.push({ time, address, value });\n\n // Power-on housekeeping, before any note: every channel enabled, and both\n // sweep units given `$08` (disabled, negate). That is what drivers write and\n // why: at the power-on value of 0 the sweep's target overflows for every\n // period from `$400` up, which would mute the low pulse notes of a track that\n // never mentions `$4001`.\n put(0, 0x4015, enables(true));\n put(0, 0x4001, 0x08);\n put(0, 0x4005, 0x08);\n\n const voicesOf = (rows: readonly (PitchedRow | DrumRow)[], keep: (row: never) => boolean) =>\n rows\n .filter(keep as (row: PitchedRow | DrumRow) => boolean)\n .map((row) => ({\n start: lead + at(row[1]),\n end: lead + at(row[1] + row[2]),\n row,\n }))\n .filter((voice) => voice.end > voice.start)\n .sort((a, b) => a.start - b.start);\n\n // Pulses.\n for (const channel of [0, 1]) {\n const base = 0x4000 + channel * 4;\n /**\n * The high byte last written to `$4003`/`$4007`, for `keepPulsePhase` — or -1 where the next\n * note must write it anyway: before the first, or after a note whose length counter ran.\n */\n let lastHigh = -1;\n const voices: Voice[] = voicesOf(\n input.pitched,\n // Period 0 would be a divide by zero on the chip too; a row with none is\n // a rest, not a note.\n ((row: PitchedRow) => row[3] === channel && row[5] >= 1) as (row: never) => boolean\n );\n voices.forEach((voice, i) => {\n const [, , , , volume, period, duty, volumeEnd, lengthTicks, bendIndex, envelope, sweep, levelIndex] =\n voice.row as PitchedRow;\n const levelCurve =\n levelIndex !== undefined && levelIndex >= 0 ? (input.levels?.[levelIndex] ?? null) : null;\n const bits = controlBits(envelope ?? -1, lengthTicks);\n const reg0 = (level: number) =>\n ((duty & 3) << 6) |\n (bits.halt ? 0x20 : 0) |\n (bits.constant ? 0x10 | (level & 0x0f) : bits.low);\n const level = (at: number) =>\n levelCurve\n ? Math.min(15, Math.max(0, levelAt(levelCurve, at)))\n : Math.min(15, Math.max(0, Math.round(volume + (volumeEnd - volume) * at)));\n\n put(voice.start, base, reg0(level(0)));\n put(voice.start, base + 1, sweep !== undefined && sweep >= 0 ? sweep : 0x08);\n put(voice.start, base + 2, period & 0xff);\n const high = (period >> 8) & 7;\n const restart = !input.keepPulsePhase || !bits.constant || !bits.halt || high !== lastHigh;\n if (restart) put(voice.start, base + 3, (bits.load << 3) | high);\n lastHigh = bits.constant && bits.halt ? high : -1;\n\n const curve = bendIndex >= 0 ? (bends[bendIndex] ?? null) : null;\n const ramps = bits.constant && (levelCurve !== null || volume !== volumeEnd);\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastPeriod = period;\n for (const { time, position } of frames(voice.start, voice.end)) {\n if (ramps) {\n const now = level(position);\n if (now !== lastLevel) put(time, base, reg0(now));\n lastLevel = now;\n }\n if (curve) {\n // A semitone is a frequency ratio; the period is a divider, so it\n // moves the other way.\n const now = toPeriod((period + 1) * Math.pow(2, -bendAt(curve, position) / 12) - 1);\n if (now !== lastPeriod) {\n put(time, base + 2, now & 0xff);\n // The high byte only when it changes, as drivers do — writing it\n // restarts the sequencer, which is an audible click.\n if (now >> 8 !== lastPeriod >> 8) {\n put(time, base + 3, (bits.load << 3) | (now >> 8));\n if (lastHigh >= 0) lastHigh = now >> 8;\n }\n }\n lastPeriod = now;\n }\n }\n }\n\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, base, ((duty & 3) << 6) | 0x30);\n });\n }\n\n /*\n * The DMC's hits, worked out first: `$4015` is one register, and the triangle's\n * on/off writes to it clear the DMC's bit unless they set it — which stops a\n * sample dead. So every `$4015` write carries bit 4 while a sample is still\n * playing, and only then: set after it has ended, the bit starts it again.\n */\n const samples = input.samples ?? [];\n const hits = (input.hits ?? [])\n .map((row) => {\n const sample = samples[row[0]];\n if (!sample) return null;\n const start = lead + at(row[1]);\n const rowEnd = lead + at(row[1] + row[2]);\n const length = lengthCode(sample.bytes.length) * 16 + 1;\n const end = row[4] ? rowEnd : Math.min(rowEnd, start + sampleSeconds(length, row[3]));\n return end > start ? { row, sample, start, end, length } : null;\n })\n .filter((hit) => hit !== null)\n .sort((a, b) => a.start - b.start);\n const sampleOn = (time: number) => hits.some((hit) => hit.start <= time && time < hit.end);\n const withSample = (value: number, time: number) => (sampleOn(time) ? value | 0x10 : value);\n\n // Triangle. No volume: it is ended by switching it off in `$4015`, the one\n // way to stop it on the spot — and it then holds whatever step it stopped on.\n {\n const voices: Voice[] = voicesOf(input.pitched, ((row: PitchedRow) =>\n row[3] === 2 && row[5] >= 1) as (row: never) => boolean);\n let on = true;\n voices.forEach((voice, i) => {\n const [, , , , , period, , , lengthTicks, bendIndex] = voice.row as PitchedRow;\n // With a counter to honour, the control bit is clear and the linear\n // counter set to its longest (127 quarter-frames, 0.53 s) so the length\n // counter is the one that ends the note; with none, the control bit set\n // halts both.\n const load = lengthTicks > 0 ? lengthIndex(lengthTicks) : 0;\n if (!on) put(voice.start, 0x4015, withSample(enables(true), voice.start));\n on = true;\n put(voice.start, 0x4008, lengthTicks > 0 ? 0x7f : 0xff);\n put(voice.start, 0x400a, period & 0xff);\n put(voice.start, 0x400b, (load << 3) | ((period >> 8) & 7));\n\n const curve = bendIndex >= 0 ? (bends[bendIndex] ?? null) : null;\n if (curve) {\n let last = period;\n for (const { time, position } of frames(voice.start, voice.end)) {\n const now = toPeriod((period + 1) * Math.pow(2, -bendAt(curve, position) / 12) - 1);\n if (now !== last) {\n put(time, 0x400a, now & 0xff);\n if (now >> 8 !== last >> 8) put(time, 0x400b, (load << 3) | (now >> 8));\n }\n last = now;\n }\n }\n\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) {\n put(voice.end, 0x4015, withSample(enables(false), voice.end));\n on = false;\n }\n });\n }\n\n // Noise.\n {\n const voices: Voice[] = voicesOf(input.drums, (() => true) as (row: never) => boolean);\n voices.forEach((voice, i) => {\n const [periodIndex, , , volume, volumeEnd, lengthTicks, envelope, levelIndex] = voice.row as DrumRow;\n const bits = controlBits(envelope ?? -1, lengthTicks);\n const reg = (level: number) =>\n (bits.halt ? 0x20 : 0) | (bits.constant ? 0x10 | (level & 0x0f) : bits.low);\n // A drum struck at 8 and dropped to 2 the next frame — Darkwing Duck's — is that curve, not a line.\n const levelCurve =\n levelIndex !== undefined && levelIndex >= 0 ? (input.levels?.[levelIndex] ?? null) : null;\n const level = (at: number) =>\n levelCurve\n ? Math.min(15, Math.max(0, levelAt(levelCurve, at)))\n : Math.min(15, Math.max(0, Math.round(volume + (volumeEnd - volume) * at)));\n\n put(voice.start, 0x400c, reg(level(0)));\n put(voice.start, 0x400e, periodIndex & 0x0f);\n put(voice.start, 0x400f, bits.load << 3);\n\n if (bits.constant && (levelCurve !== null || volume !== volumeEnd)) {\n let last = level(0);\n for (const { time, position } of frames(voice.start, voice.end)) {\n const now = level(position);\n if (now !== last) put(time, 0x400c, reg(now));\n last = now;\n }\n }\n\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, 0x400c, 0x30);\n });\n }\n\n // The DMC: rate and loop, where the sample is and how long, then bit 4 of\n // `$4015` to start it. A hit cut short by its row — or by the next — is\n // stopped by clearing the bit. The triangle's own `$4015` state goes with it.\n hits.forEach((hit, i) => {\n const [, , , rate, loop, level] = hit.row;\n const triangleOn = (time: number) => {\n // The last word the triangle had on `$4015` before `time`: on unless it was switched off.\n let state = true;\n for (const w of writes) if (w.address === 0x4015 && w.time <= time) state = (w.value & 0x04) !== 0;\n return state;\n };\n put(hit.start, 0x4010, (loop ? 0x40 : 0) | (rate & 0x0f));\n if (level !== undefined && level >= 0) put(hit.start, 0x4011, level & 0x7f);\n put(hit.start, 0x4012, ((hit.sample.address - 0xc000) >> 6) & 0xff);\n put(hit.start, 0x4013, lengthCode(hit.sample.bytes.length));\n // Off and on: bit 4 set while the last sample is still reading does not restart it.\n put(hit.start, 0x4015, enables(triangleOn(hit.start)));\n put(hit.start, 0x4015, enables(triangleOn(hit.start)) | 0x10);\n const next = hits[i + 1];\n const natural = hit.start + sampleSeconds(hit.length, rate);\n if (hit.end < natural - 1e-9 && (!next || next.start > hit.end + 1e-9)) {\n put(hit.end, 0x4015, enables(triangleOn(hit.end)));\n }\n });\n\n expansionWrites(input, lead, voicesOf, frames, put);\n return writes;\n}\n\nconst hzOf = (row: PitchedRow) => row[5] / 100;\n\n/**\n * The expansion channels' notes as writes: what a driver for the FDS, the VRC6 or the N163 would write to\n * sound them — the waveform when it changes, then the frequency and level, and once a frame the\n * level while it ramps and the frequency while it bends.\n */\nfunction expansionWrites(\n input: ApuInput,\n lead: number,\n voicesOf: (rows: readonly (PitchedRow | DrumRow)[], keep: (row: never) => boolean) => Voice[],\n frames: (start: number, end: number) => Iterable<{ time: number; position: number }>,\n put: (time: number, address: number, value: number) => void\n) {\n const bends = input.bends ?? [];\n const waves = input.waves ?? [];\n const curveOf = (row: PitchedRow) => (row[9] >= 0 ? (bends[row[9]] ?? null) : null);\n const levelsOf = (row: PitchedRow) =>\n row[12] !== undefined && row[12] >= 0 ? (input.levels?.[row[12]] ?? null) : null;\n const levelAtOf = (row: PitchedRow) => {\n const curve = levelsOf(row);\n return (at: number) =>\n curve\n ? Math.min(15, Math.max(0, levelAt(curve, at)))\n : Math.min(15, Math.max(0, Math.round(row[4] + (row[7] - row[4]) * at)));\n };\n\n // FDS.\n {\n const voices = voicesOf(input.pitched, ((row: PitchedRow) =>\n row[3] === EXPANSION_CHANNELS.fds && row[5] > 0) as (row: never) => boolean);\n if (voices.length > 0) {\n put(0, 0x4087, 0x80); // modulation off: the bends carry the vibrato\n put(0, 0x408a, 0xe8);\n }\n const freqOf = (hz: number) => Math.min(0xfff, Math.max(1, Math.round((hz * 4194304) / CPU_HZ)));\n let lastWave = -1;\n voices.forEach((voice, i) => {\n const row = voice.row as PitchedRow;\n const waveIndex = row[13] ?? -1;\n if (waveIndex >= 0 && waveIndex !== lastWave && waves[waveIndex]) {\n put(voice.start, 0x4089, 0x80);\n waves[waveIndex].slice(0, 64).forEach((v, k) => put(voice.start, 0x4040 + k, v & 0x3f));\n put(voice.start, 0x4089, 0x00);\n lastWave = waveIndex;\n }\n const level = levelAtOf(row);\n const gain = (l: number) => 0x80 | Math.round((l * 32) / 15);\n const freq = freqOf(hzOf(row));\n put(voice.start, 0x4080, gain(level(0)));\n put(voice.start, 0x4082, freq & 0xff);\n put(voice.start, 0x4083, 0x40 | (freq >> 8));\n const curve = curveOf(row);\n const ramps = levelsOf(row) !== null || row[4] !== row[7];\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastFreq = freq;\n for (const { time, position } of frames(voice.start, voice.end)) {\n if (ramps) {\n const now = level(position);\n if (now !== lastLevel) put(time, 0x4080, gain(now));\n lastLevel = now;\n }\n if (curve) {\n const now = freqOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12));\n if (now !== lastFreq) {\n put(time, 0x4082, now & 0xff);\n if (now >> 8 !== lastFreq >> 8) put(time, 0x4083, 0x40 | (now >> 8));\n }\n lastFreq = now;\n }\n }\n }\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, 0x4080, 0x80);\n });\n }\n\n // VRC6: two pulses and the saw, each its three registers; the period counts CPU cycles.\n {\n let started = false;\n EXPANSION_CHANNELS.vrc6.forEach((channel, k) => {\n const voices = voicesOf(input.pitched, ((row: PitchedRow) => row[3] === channel && row[5] > 0) as (row: never) => boolean);\n if (voices.length === 0) return;\n if (!started) put(0, 0x9003, 0x00); // running, no frequency shift\n started = true;\n const base = 0x9000 + k * 0x1000;\n const saw = k === 2;\n const periodOf = (hz: number) =>\n Math.min(0xfff, Math.max(0, Math.round(CPU_HZ / ((saw ? 14 : 16) * hz)) - 1));\n voices.forEach((voice, i) => {\n const row = voice.row as PitchedRow;\n const level = levelAtOf(row);\n const control = saw\n ? (l: number) => Math.min(0x3f, Math.round((l * 42) / 15))\n : (l: number) => (row[6] >= 8 ? 0x80 : (Math.max(0, row[6]) & 7) << 4) | l;\n const period = periodOf(hzOf(row));\n put(voice.start, base, control(level(0)));\n put(voice.start, base + 1, period & 0xff);\n put(voice.start, base + 2, 0x80 | (period >> 8));\n const curve = curveOf(row);\n const ramps = levelsOf(row) !== null || row[4] !== row[7];\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastPeriod = period;\n for (const { time, position } of frames(voice.start, voice.end)) {\n if (ramps) {\n const now = level(position);\n if (now !== lastLevel) put(time, base, control(now));\n lastLevel = now;\n }\n if (curve) {\n const now = periodOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12));\n if (now !== lastPeriod) {\n put(time, base + 1, now & 0xff);\n // With E kept set: clearing it would restart the wave.\n if (now >> 8 !== lastPeriod >> 8) put(time, base + 2, 0x80 | (now >> 8));\n }\n lastPeriod = now;\n }\n }\n }\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) put(voice.end, base + 2, period >> 8);\n });\n });\n }\n\n // N163: as many channels on as the piece uses, each with its own stretch of the chip's RAM.\n const n163Voices = EXPANSION_CHANNELS.n163.map((channel) =>\n voicesOf(input.pitched, ((row: PitchedRow) => row[3] === channel && row[5] > 0) as (row: never) => boolean)\n );\n const used = n163Voices.reduce((most, voices, k) => (voices.length > 0 ? k + 1 : most), 0);\n if (used === 0) return;\n const count = used;\n // Registers take the top 8·count bytes; the rest is wave RAM, shared out evenly, in whole fours.\n const freeNibbles = (0x80 - 8 * count) * 2;\n const area = Math.max(4, Math.floor(freeNibbles / count / 4) * 4);\n const ramWrite = (time: number, address: number, bytes: readonly number[]) => {\n put(time, 0xf800, 0x80 | (address & 0x7f));\n for (const b of bytes) put(time, 0x4800, b & 0xff);\n };\n // The channel count lives in the top nibble of $7F, beside slot 7's volume.\n const countBits = (count - 1) << 4;\n ramWrite(0, 0x7f, [countBits]);\n /*\n * When every waveform the piece plays fits in the wave RAM at once, each gets a place of its own,\n * written before the first note, and a channel just points at it — as Namco's drivers do, several\n * channels reading one wave. Only when they do not fit does each channel get a stretch of its own\n * that its waves are written into as they come (and squeezed into, if longer).\n */\n const n163Waves = [...new Set(n163Voices.flat().map((voice) => (voice.row as PitchedRow)[13] ?? -1))].filter(\n (index) => index >= 0 && waves[index]\n );\n const fitted = (index: number) => Math.min(256, Math.max(4, Math.floor(waves[index].length / 4) * 4));\n const shared = new Map<number, { start: number; length: number }>();\n if (n163Waves.reduce((sum, index) => sum + fitted(index), 0) <= freeNibbles) {\n let at = 0;\n for (const index of n163Waves) {\n const length = fitted(index);\n shared.set(index, { start: at, length });\n const wave = waves[index];\n const bytes: number[] = [];\n for (let n = 0; n < length; n += 2) bytes.push((wave[n] & 0x0f) | ((wave[n + 1] ?? 0) & 0x0f) << 4);\n ramWrite(0, at >> 1, bytes);\n at += length;\n }\n }\n n163Voices.forEach((voices, k) => {\n const slot = 7 - k;\n const base = 0x40 + slot * 8;\n let start = k * area; // in nibbles\n let lastWave = -1;\n let length = area;\n const volumeByte = (v: number) => (slot === 7 ? countBits : 0) | (v & 0x0f);\n voices.forEach((voice, i) => {\n const row = voice.row as PitchedRow;\n const waveIndex = row[13] ?? -1;\n const wave = waveIndex >= 0 ? waves[waveIndex] : null;\n const place = shared.get(waveIndex);\n if (place) {\n start = place.start;\n length = place.length;\n } else if (wave && waveIndex !== lastWave) {\n start = k * area;\n // A wave longer than the stretch this channel has is resampled to fit.\n length = Math.min(area, Math.max(4, Math.floor(wave.length / 4) * 4));\n const samples = Array.from({ length }, (_, n) => wave[Math.floor((n * wave.length) / length)] & 0x0f);\n const bytes: number[] = [];\n for (let n = 0; n < length; n += 2) bytes.push(samples[n] | ((samples[n + 1] ?? 0) << 4));\n ramWrite(voice.start, start >> 1, bytes);\n lastWave = waveIndex;\n }\n const freqOf = (hz: number) =>\n Math.min(0x3ffff, Math.max(1, Math.round((hz * 15 * 65536 * count * length) / CPU_HZ)));\n const regs = (freq: number, volume: number) => [\n freq & 0xff,\n 0,\n (freq >> 8) & 0xff,\n 0,\n ((256 - length) & 0xfc) | ((freq >> 16) & 3),\n 0,\n start & 0xff,\n volumeByte(volume),\n ];\n const level = levelAtOf(row);\n const freq = freqOf(hzOf(row));\n ramWrite(voice.start, base, regs(freq, level(0)));\n const curve = curveOf(row);\n const ramps = levelsOf(row) !== null || row[4] !== row[7];\n if (curve || ramps) {\n let lastLevel = level(0);\n let lastFreq = freq;\n for (const { time, position } of frames(voice.start, voice.end)) {\n const nowLevel = ramps ? level(position) : lastLevel;\n const nowFreq = curve ? freqOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12)) : lastFreq;\n if (nowFreq !== lastFreq) {\n // The frequency bytes alone, not the phase after them: that is the chip's running count.\n ramWrite(time, base, [nowFreq & 0xff]);\n ramWrite(time, base + 2, [(nowFreq >> 8) & 0xff]);\n ramWrite(time, base + 4, [((256 - length) & 0xfc) | ((nowFreq >> 16) & 3)]);\n }\n if (nowLevel !== lastLevel) ramWrite(time, base + 7, [volumeByte(nowLevel)]);\n lastLevel = nowLevel;\n lastFreq = nowFreq;\n }\n }\n const next = voices[i + 1];\n if (!next || next.start > voice.end + 1e-9) ramWrite(voice.end, base + 7, [volumeByte(0)]);\n });\n });\n}\n\n/** Render a decoded piece: `notesToWrites` into `renderWrites`, with the tick-based extras. */\nexport function renderNotes(input: ApuInput, options: ApuOptions = {}): ApuRender {\n const lead = options.lead ?? 0;\n\n const clicks: Click[] = [];\n if (options.metronome && options.metronome > 0) {\n for (let tick = 0, beat = 0; tick < input.totalTicks; tick += options.metronome, beat++) {\n // Four beats to a bar: the downbeat is what makes a tempo readable.\n clicks.push({ time: lead + tickToSeconds(input, tick), strong: beat % 4 === 0 });\n }\n }\n\n const { lead: _lead, metronome: _metronome, ...render } = options;\n const end = tickToSeconds(input, input.totalTicks);\n const result = renderWrites(notesToWrites(input, lead), lead + end, {\n ...render,\n clicks,\n ...(input.samples?.length && !render.memory ? { memory: sampleMemory(input.samples) } : {}),\n });\n\n return {\n samples: result.samples,\n sampleRate: result.sampleRate,\n lead,\n duration: result.duration,\n loopStart: lead + tickToSeconds(input, Math.min(input.loopStart ?? 0, input.totalTicks)),\n loopEnd: lead + end,\n peak: result.peak,\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xromdev/apu",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "The NES sound chip (2A03 APU) in TypeScript: register writes in, samples out.",
5
5
  "license": "MIT",
6
6
  "type": "module",