@xromdev/apu 0.1.0
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/LICENSE +21 -0
- package/README.md +241 -0
- package/dist/chip.d.ts +155 -0
- package/dist/chip.js +629 -0
- package/dist/chip.js.map +1 -0
- package/dist/constants.d.ts +44 -0
- package/dist/constants.js +57 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/lfsr.d.ts +24 -0
- package/dist/lfsr.js +37 -0
- package/dist/lfsr.js.map +1 -0
- package/dist/mixer.d.ts +52 -0
- package/dist/mixer.js +85 -0
- package/dist/mixer.js.map +1 -0
- package/dist/notes.d.ts +108 -0
- package/dist/notes.js +263 -0
- package/dist/notes.js.map +1 -0
- package/dist/render.d.ts +69 -0
- package/dist/render.js +116 -0
- package/dist/render.js.map +1 -0
- package/dist/sweep.d.ts +51 -0
- package/dist/sweep.js +79 -0
- package/dist/sweep.js.map +1 -0
- package/package.json +59 -0
package/dist/chip.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chip.js","sourceRoot":"","sources":["../src/chip.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAoB,MAAM,YAAY,CAAC;AAEpF,4DAA4D;AAC5D,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC;AACzD,MAAM,aAAa,GAAG,UAA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C5B,iEAAiE;YACjE,IAAI,CAAC,CAAC,YAAY,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;gBAC7E,CAAC,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;gBACvD,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;YACtB,CAAC;YACD,IAAI,CAAC,CAAC,YAAY,KAAK,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;gBAC1C,CAAC,CAAC,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC;gBACvC,CAAC,CAAC,WAAW,GAAG,KAAK,CAAC;YACxB,CAAC;iBAAM,CAAC;gBACN,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC;YACtB,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["/**\n * The 2A03's sound half, register by register.\n *\n * `Apu` holds what the chip holds — timers, sequencer positions, length and\n * linear counters, envelopes, sweep units, the frame counter — and changes it\n * only the ways the chip does: through a write to `$4000`–`$4017`, or through\n * its own clocks. Nothing here knows what a note is. That is the point: every\n * NES game's music, whatever its driver, reaches the speaker as writes to\n * these registers, so a synth that takes writes plays any of them.\n *\n * It runs at a fixed sample rate rather than per CPU cycle: `render()` advances\n * the chip a run of samples and reports each channel's output level — 0..15 for\n * the pulses, triangle and noise, 0..127 for the DMC — at every one of them. The\n * mixer and the analogue stage are separate (`mixer.ts`), because a caller that\n * wants to hear one channel alone has to get at the levels before they are mixed.\n *\n * The DMC reads its samples from the cartridge, `$8000`–`$FFFF`; give it that\n * memory as `options.memory` (without it, it reads zeros — a sample that only\n * ever steps down).\n *\n * Not modelled: the DMC's and the frame counter's IRQs, the CPU stall of the\n * DMC's memory reads, and the handful of cycle-exact write/clock races. Periods\n * and levels are the hardware's; timing is exact to the sample.\n *\n * **Speed.** `levels()` runs once per sample — 88 200 times a second of music at\n * the default oversampling — so anything that only changes when a register is\n * written (a channel's step per sample, whether the sweep unit mutes it) is\n * worked out at the write and kept, not recomputed here. The expressions are the\n * same ones, evaluated at a different moment, so the output is bit for bit what\n * recomputing them every sample gives.\n */\n\nimport {\n CPU_HZ,\n DMC_PERIODS,\n DUTY_SEQUENCES,\n LENGTH_TABLE,\n NOISE_PERIODS,\n QUARTER_FRAME_HZ,\n TRIANGLE_STEPS,\n} from './constants.js';\nimport { LONG_CYCLE, LONG_HIGH_BEFORE, LONG_INDEX, LONG_STATES } from './lfsr.js';\nimport { decodeSweep, sweepMutes, sweepTarget, type SweepConfig } from './sweep.js';\n\n/** The four duty sequences flattened, `duty * 8 + step`. */\nconst DUTY_FLAT = Uint8Array.from(DUTY_SEQUENCES.flat());\nconst TRIANGLE_FLAT = Uint8Array.from(TRIANGLE_STEPS);\n\ninterface Envelope {\n start: boolean;\n divider: number;\n decay: number;\n}\n\ninterface Pulse {\n pulse1: boolean;\n duty: number;\n /** Bit 5 of `$4000`: halts the length counter AND loops the envelope — one bit, two jobs. */\n halt: boolean;\n constant: boolean;\n /** Bits 0..3 of `$4000`: the level when `constant`, the envelope's divider period when not. */\n volume: number;\n env: Envelope;\n sweep: SweepConfig;\n sweepDivider: number;\n sweepReload: boolean;\n /** The 11-bit timer period. */\n period: number;\n length: number;\n enabled: boolean;\n /** Position in the 8-step duty sequence, fractional. */\n phase: number;\n /** Steps of the sequence per sample, from `period` — kept, see \"Speed\". */\n step: number;\n /** Whether the sweep unit mutes the channel at this period — kept, see \"Speed\". */\n muted: boolean;\n}\n\ninterface Triangle {\n /** Bit 7 of `$4008`: halts the length counter and keeps reloading the linear counter. */\n control: boolean;\n linearReload: number;\n linearReloadFlag: boolean;\n linear: number;\n period: number;\n length: number;\n enabled: boolean;\n phase: number;\n step: number;\n}\n\ninterface Noise {\n halt: boolean;\n constant: boolean;\n volume: number;\n env: Envelope;\n /** Bit 7 of `$400E`: feedback from bit 6 instead of bit 1 — the short, metallic 93-step loop. */\n shortMode: boolean;\n periodIndex: number;\n length: number;\n enabled: boolean;\n /**\n * The register. In normal mode it is held as a position in the precomputed\n * cycle (`lfsrIndex`) and `lfsr` is stale; in short mode `lfsr` is the state.\n * A mode switch converts one into the other.\n */\n lfsr: number;\n lfsrIndex: number;\n /** Fractional timer position: how many register shifts are owed. */\n phase: number;\n step: number;\n}\n\n/**\n * What the chip is doing right now, channel by channel — for showing it, not\n * for driving it. Every field is a number the hardware itself holds.\n */\n/**\n * The delta modulation channel: 1-bit samples read from cartridge memory, each\n * bit stepping a 7-bit output up or down by 2.\n */\ninterface Dmc {\n loop: boolean;\n rateIndex: number;\n /** Output level, 0..127. `$4011` sets it directly. */\n level: number;\n /** `$4012`/`$4013` as addresses and byte counts. */\n startAddress: number;\n startLength: number;\n /** The memory reader: where it reads next, and how many bytes are left. */\n address: number;\n remaining: number;\n /** The one-byte sample buffer, or -1 when empty. */\n buffer: number;\n /** The output unit: its shift register, bits left in it, and whether it is silent. */\n shift: number;\n bits: number;\n silence: boolean;\n phase: number;\n step: number;\n}\n\n/** Where every channel's output levels go, sample by sample. */\nexport interface ChannelBuffers {\n pulse1: Uint8Array;\n pulse2: Uint8Array;\n triangle: Uint8Array;\n /** Fractional: the gate is averaged over the shifts in a sample. */\n noise: Float64Array;\n dmc: Uint8Array;\n}\n\n/** Cartridge memory for the DMC: the bytes of `$8000`–`$FFFF`, or a reader for any address. */\nexport type ApuMemory = Uint8Array | ((address: number) => number);\n\nexport interface ApuSnapshot {\n pulse: {\n period: number;\n duty: number;\n /** Length counter, in half-frames; 0 is silent. */\n length: number;\n /** Level the channel outputs when high: the written volume, or the envelope's decay. */\n level: number;\n constant: boolean;\n /** The sweep unit is muting the channel (period < 8, or target past $7FF). */\n muted: boolean;\n enabled: boolean;\n }[];\n triangle: {\n period: number;\n length: number;\n /** Linear counter, in quarter-frames; 0 is silent. */\n linear: number;\n /** The step the staircase is on — held while silent. */\n step: number;\n enabled: boolean;\n };\n noise: {\n periodIndex: number;\n shortMode: boolean;\n length: number;\n level: number;\n constant: boolean;\n enabled: boolean;\n };\n dmc: {\n /** Output level, 0..127. */\n level: number;\n rateIndex: number;\n loop: boolean;\n /** Bytes of the sample still to read; above 0 means it is playing. */\n remaining: number;\n /** Where the memory reader is. */\n address: number;\n enabled: boolean;\n };\n}\n\nexport interface ApuChipOptions {\n /**\n * Honour the length counters. Default true — it is hardware, and without it\n * a drum hit written with a short counter runs on as a wash. Off exists to\n * hear the difference.\n */\n lengthCounter?: boolean;\n /** Where the DMC reads its samples from — see `ApuMemory`. Reads 0 when absent. */\n memory?: ApuMemory;\n}\n\nconst newEnvelope = (): Envelope => ({ start: false, divider: 0, decay: 0 });\n\nconst newPulse = (pulse1: boolean): Pulse => ({\n pulse1,\n duty: 0,\n halt: false,\n constant: false,\n volume: 0,\n env: newEnvelope(),\n sweep: decodeSweep(0, pulse1),\n sweepDivider: 0,\n sweepReload: false,\n period: 0,\n length: 0,\n enabled: false,\n phase: 0,\n step: 0,\n muted: true,\n});\n\n/** One quarter-frame of an envelope, in the hardware's order. */\nfunction clockEnvelope(env: Envelope, period: number, loop: boolean) {\n if (env.start) {\n env.start = false;\n env.decay = 15;\n env.divider = period;\n } else if (env.divider === 0) {\n env.divider = period;\n if (env.decay > 0) env.decay -= 1;\n else if (loop) env.decay = 15;\n } else {\n env.divider -= 1;\n }\n}\n\nexport class Apu {\n readonly sampleRate: number;\n private readonly lengthCounter: boolean;\n\n private readonly pulses: [Pulse, Pulse] = [newPulse(true), newPulse(false)];\n private readonly triangle: Triangle = {\n control: false,\n linearReload: 0,\n linearReloadFlag: false,\n linear: 0,\n period: 0,\n length: 0,\n enabled: false,\n // Start on a zero step, so a chip that has not been written to is silent\n // rather than holding the top of the staircase as DC. The triangle's\n // position only moves while it sounds, so this is where it rests until the\n // first note — the part of power-on a listener would otherwise hear as a\n // thump.\n phase: 16,\n step: 0,\n };\n private readonly noise: Noise = {\n halt: false,\n constant: false,\n volume: 0,\n env: newEnvelope(),\n shortMode: false,\n periodIndex: 0,\n length: 0,\n enabled: false,\n lfsr: 1, // the chip powers up with bit 0 set\n lfsrIndex: LONG_INDEX[1],\n phase: 0,\n step: 0,\n };\n\n private readonly dmc: Dmc = {\n loop: false,\n rateIndex: 0,\n level: 0,\n startAddress: 0xc000,\n startLength: 1,\n address: 0xc000,\n remaining: 0,\n buffer: -1,\n shift: 0,\n bits: 8,\n silence: true,\n phase: 0,\n step: 0,\n };\n private readonly read: (address: number) => number;\n\n /** Frame counter: 5-step mode, which step is next, samples until it. */\n private fiveStep = false;\n private frameStep = 0;\n private frameCountdown: number;\n private readonly samplesPerQuarterFrame: number;\n\n private readonly out = [0, 0, 0, 0, 0];\n\n constructor(sampleRate: number, options: ApuChipOptions = {}) {\n this.sampleRate = sampleRate;\n this.lengthCounter = options.lengthCounter !== false;\n this.samplesPerQuarterFrame = sampleRate / QUARTER_FRAME_HZ;\n this.frameCountdown = this.samplesPerQuarterFrame;\n for (const p of this.pulses) this.retunePulse(p);\n this.retuneTriangle();\n this.retuneNoise();\n this.retuneDmc();\n const memory = options.memory;\n this.read =\n typeof memory === 'function'\n ? (address) => memory(address) & 0xff\n : memory && memory.length > 0\n ? (address) => memory[(address - 0x8000) % memory.length]\n : () => 0;\n }\n\n /** A CPU write to one of the APU's registers. Anything outside `$4000`–`$4017` is ignored. */\n write(address: number, value: number) {\n const v = value & 0xff;\n switch (address) {\n case 0x4000:\n case 0x4004: {\n const p = this.pulses[address === 0x4000 ? 0 : 1];\n p.duty = v >> 6;\n p.halt = (v & 0x20) !== 0;\n p.constant = (v & 0x10) !== 0;\n p.volume = v & 0x0f;\n return;\n }\n case 0x4001:\n case 0x4005: {\n const p = this.pulses[address === 0x4001 ? 0 : 1];\n p.sweep = decodeSweep(v, p.pulse1);\n p.sweepReload = true;\n p.muted = sweepMutes(p.period, p.sweep);\n return;\n }\n case 0x4002:\n case 0x4006: {\n const p = this.pulses[address === 0x4002 ? 0 : 1];\n p.period = (p.period & 0x700) | v;\n this.retunePulse(p);\n return;\n }\n case 0x4003:\n case 0x4007: {\n // The high byte does three things at once, and all three are audible:\n // it reloads the length counter, restarts the envelope, and puts the\n // duty sequencer back to its first step — which is why a driver that\n // rewrites it every frame for a vibrato makes the vibrato click.\n const p = this.pulses[address === 0x4003 ? 0 : 1];\n p.period = (p.period & 0xff) | ((v & 7) << 8);\n if (p.enabled) p.length = LENGTH_TABLE[v >> 3];\n p.env.start = true;\n p.phase = 0;\n this.retunePulse(p);\n return;\n }\n case 0x4008:\n this.triangle.control = (v & 0x80) !== 0;\n this.triangle.linearReload = v & 0x7f;\n return;\n case 0x400a:\n this.triangle.period = (this.triangle.period & 0x700) | v;\n this.retuneTriangle();\n return;\n case 0x400b:\n // No phase reset here: the triangle's sequencer is never restarted, so\n // a new note picks up the staircase wherever the last one left it.\n this.triangle.period = (this.triangle.period & 0xff) | ((v & 7) << 8);\n if (this.triangle.enabled) this.triangle.length = LENGTH_TABLE[v >> 3];\n this.triangle.linearReloadFlag = true;\n this.retuneTriangle();\n return;\n case 0x400c:\n this.noise.halt = (v & 0x20) !== 0;\n this.noise.constant = (v & 0x10) !== 0;\n this.noise.volume = v & 0x0f;\n return;\n case 0x400e: {\n const n = this.noise;\n const shortMode = (v & 0x80) !== 0;\n // Hand the register between its two representations: a position in the\n // precomputed cycle for normal mode, the raw state for short mode.\n if (shortMode && !n.shortMode) n.lfsr = LONG_STATES[n.lfsrIndex];\n if (!shortMode && n.shortMode) n.lfsrIndex = LONG_INDEX[n.lfsr];\n n.shortMode = shortMode;\n n.periodIndex = v & 0x0f;\n this.retuneNoise();\n return;\n }\n case 0x400f:\n if (this.noise.enabled) this.noise.length = LENGTH_TABLE[v >> 3];\n this.noise.env.start = true;\n return;\n case 0x4010:\n this.dmc.loop = (v & 0x40) !== 0;\n this.dmc.rateIndex = v & 0x0f;\n this.retuneDmc();\n return;\n case 0x4011:\n // Straight to the output — the way games play PCM without the DMC's\n // own memory reader, one write per sample.\n this.dmc.level = v & 0x7f;\n return;\n case 0x4012:\n this.dmc.startAddress = 0xc000 + v * 64;\n return;\n case 0x4013:\n this.dmc.startLength = v * 16 + 1;\n return;\n case 0x4015: {\n // Clearing a channel's bit empties its length counter at once — the\n // one way to silence a channel on the spot rather than at the next clock.\n const channels = [this.pulses[0], this.pulses[1], this.triangle, this.noise];\n channels.forEach((channel, i) => {\n channel.enabled = (v & (1 << i)) !== 0;\n if (!channel.enabled) channel.length = 0;\n });\n // Bit 4 is the DMC's: clear stops the sample (what is already in the\n // buffer still plays out); set starts it again if it had finished.\n const d = this.dmc;\n if ((v & 0x10) === 0) d.remaining = 0;\n else if (d.remaining === 0) {\n d.address = d.startAddress;\n d.remaining = d.startLength;\n this.fetchDmc();\n }\n return;\n }\n case 0x4017:\n this.fiveStep = (v & 0x80) !== 0;\n this.frameStep = 0;\n this.frameCountdown = this.samplesPerQuarterFrame;\n // Selecting 5-step mode clocks everything once, immediately.\n if (this.fiveStep) {\n this.clockQuarterFrame();\n this.clockHalfFrame();\n }\n return;\n default:\n return;\n }\n }\n\n /** Scratch buffers for `levels()`, one sample long. */\n private readonly one: ChannelBuffers = {\n pulse1: new Uint8Array(1),\n pulse2: new Uint8Array(1),\n triangle: new Uint8Array(1),\n noise: new Float64Array(1),\n dmc: new Uint8Array(1),\n };\n\n /**\n * Advance one sample and return the five channel levels, `[pulse1, pulse2,\n * triangle, noise, dmc]` (see `render`). The array is reused between calls.\n * For more than a sample at a time, `render` is several times faster and\n * gives the same numbers.\n */\n levels(): readonly number[] {\n const one = this.one;\n this.render(1, one);\n const out = this.out;\n out[0] = one.pulse1[0];\n out[1] = one.pulse2[0];\n out[2] = one.triangle[0];\n out[3] = one.noise[0];\n out[4] = one.dmc[0];\n return out;\n }\n\n /**\n * Advance `count` samples, writing each channel's level into its buffer from\n * `offset` on: the pulses, triangle and DMC as whole numbers (0..15, 0..127),\n * the noise fractional (its gate is averaged over the shifts in a sample). No\n * register writes happen inside — a caller with writes due splits the run at\n * them.\n *\n * The frame counter is the only thing that changes a channel's parameters\n * between writes, so the run is cut at its clocks and each piece is filled a\n * channel at a time (`fill`). Every sum is the one `levels()` would do, in the\n * same order — the buffers come out bit for bit the same as `count` calls.\n */\n render(count: number, buffers: ChannelBuffers, offset = 0) {\n let done = 0;\n while (done < count) {\n // Samples before the one on which the counter reaches zero. Subtracting\n // them in one go is exact: the counter holds a value whose fractional\n // bits are all representable after taking off a whole number.\n const free = Math.ceil(this.frameCountdown) - 1;\n if (free <= 0) {\n this.frameCountdown -= 1;\n while (this.frameCountdown <= 0) {\n this.frameCountdown += this.samplesPerQuarterFrame;\n this.clockFrameCounter();\n }\n this.fill(1, buffers, offset + done);\n done += 1;\n } else {\n const run = Math.min(free, count - done);\n this.frameCountdown -= run;\n this.fill(run, buffers, offset + done);\n done += run;\n }\n }\n }\n\n /** `count` samples with every channel's parameters fixed — tight loops on locals. */\n private fill(count: number, buffers: ChannelBuffers, at: number) {\n const end = at + count;\n const { pulse1, pulse2, triangle, noise } = buffers;\n\n for (let i = 0; i < 2; i++) {\n const p = this.pulses[i];\n const out = i === 0 ? pulse1 : pulse2;\n // A pulse steps through 8 entries per cycle and each entry takes\n // 2 × (period + 1) CPU cycles: CPU / (16 × (period + 1)) per cycle.\n const step = p.step;\n let phase = p.phase;\n if (p.length === 0 || p.muted) {\n for (let k = at; k < end; k++) {\n phase += step;\n if (phase >= 8) phase %= 8;\n out[k] = 0;\n }\n } else {\n const level = p.constant ? p.volume : p.env.decay;\n const base = p.duty * 8;\n for (let k = at; k < end; k++) {\n phase += step;\n if (phase >= 8) phase %= 8;\n out[k] = DUTY_FLAT[base + ((phase | 0) & 7)] * level;\n }\n }\n p.phase = phase;\n }\n\n const t = this.triangle;\n // The staircase only moves while both counters are non-zero. When either\n // runs out the sequencer STOPS where it is and the output holds that step:\n // the triangle is never \"at zero\" between notes, it is wherever it paused.\n if (t.length > 0 && t.linear > 0) {\n const step = t.step;\n let phase = t.phase;\n for (let k = at; k < end; k++) {\n phase += step;\n if (phase >= 32) phase %= 32;\n triangle[k] = TRIANGLE_FLAT[(phase | 0) & 31];\n }\n t.phase = phase;\n } else {\n triangle.fill(TRIANGLE_FLAT[(t.phase | 0) & 31], at, end);\n }\n\n // AVERAGE the register's output across the shifts each sample is worth,\n // rather than sampling wherever it lands. At the short periods the LFSR\n // runs well past the sample rate, and keeping only the last shift folds the\n // rest back as alias tones — drums like a broken speaker. The channel is\n // silenced while bit 0 is set: the register gates it.\n const n = this.noise;\n const level = n.length === 0 ? 0 : n.constant ? n.volume : n.env.decay;\n const step = n.step;\n let phase = n.phase;\n if (!n.shortMode) {\n let index = n.lfsrIndex;\n if (level === 0) {\n // Silent, but the register keeps shifting: advance it, output nothing.\n for (let k = at; k < end; k++) {\n phase += step;\n const shifts = Math.floor(phase);\n phase -= shifts;\n if (shifts > 0) index = (index + shifts) % LONG_CYCLE;\n }\n noise.fill(0, at, end);\n } else {\n for (let k = at; k < end; k++) {\n phase += step;\n const shifts = Math.floor(phase);\n phase -= shifts;\n let gate: number;\n if (shifts > 0) {\n gate = (LONG_HIGH_BEFORE[index + shifts] - LONG_HIGH_BEFORE[index]) / shifts;\n index = (index + shifts) % LONG_CYCLE;\n } else {\n gate = (LONG_STATES[index] & 1) === 0 ? 1 : 0;\n }\n noise[k] = gate * level;\n }\n }\n n.lfsrIndex = index;\n } else {\n let lfsr = n.lfsr;\n for (let k = at; k < end; k++) {\n phase += step;\n const shifts = Math.floor(phase);\n phase -= shifts;\n let high = 0;\n for (let s = 0; s < shifts; s++) {\n if ((lfsr & 1) === 0) high++;\n const feedback = (lfsr & 1) ^ ((lfsr >> 6) & 1);\n lfsr = (lfsr >> 1) | (feedback << 14);\n }\n const gate = shifts > 0 ? high / shifts : (lfsr & 1) === 0 ? 1 : 0;\n noise[k] = level === 0 ? 0 : gate * level;\n }\n n.lfsr = lfsr;\n }\n n.phase = phase;\n\n this.fillDmc(buffers.dmc, at, end);\n }\n\n /**\n * The DMC over `[at, end)`. At most a bit or two per sample even at its\n * fastest rate, so it is clocked bit by bit; a channel with nothing to play\n * only has its output-cycle count moved on, which is all that changes.\n */\n private fillDmc(out: Uint8Array, at: number, end: number) {\n const d = this.dmc;\n if (d.silence && d.remaining === 0 && d.buffer < 0) {\n // Nothing to play: the output holds, and only the timer and the bit count\n // move on. Worked out for the whole run at once — nothing audible depends\n // on the timer's exact fraction until a sample starts, and starting one\n // takes a register write, which ends the run.\n const total = d.phase + d.step * (end - at);\n const clocks = Math.floor(total);\n d.phase = total - clocks;\n // Each clock uses up a bit; an emptied cycle starts another, silent,\n // since there is nothing to load.\n d.bits -= clocks % 8;\n if (d.bits <= 0) d.bits += 8;\n out.fill(d.level, at, end);\n return;\n }\n for (let k = at; k < end; k++) {\n d.phase += d.step;\n while (d.phase >= 1) {\n d.phase -= 1;\n this.clockDmc();\n }\n out[k] = d.level;\n }\n }\n\n /** One DMC timer clock: step the output by the next bit, and start a new byte after eight. */\n private clockDmc() {\n const d = this.dmc;\n if (!d.silence) {\n if (d.shift & 1) {\n if (d.level <= 125) d.level += 2;\n } else if (d.level >= 2) {\n d.level -= 2;\n }\n }\n d.shift >>= 1;\n d.bits -= 1;\n if (d.bits === 0) {\n d.bits = 8;\n if (d.buffer < 0) {\n d.silence = true;\n } else {\n d.silence = false;\n d.shift = d.buffer;\n d.buffer = -1;\n this.fetchDmc();\n }\n }\n }\n\n /** The memory reader: refill the empty buffer from the cartridge, looping or stopping at the end. */\n private fetchDmc() {\n const d = this.dmc;\n if (d.buffer >= 0 || d.remaining === 0) return;\n d.buffer = this.read(d.address);\n d.address = d.address === 0xffff ? 0x8000 : d.address + 1;\n d.remaining -= 1;\n if (d.remaining === 0 && d.loop) {\n d.address = d.startAddress;\n d.remaining = d.startLength;\n }\n }\n\n private retuneDmc() {\n this.dmc.step = CPU_HZ / DMC_PERIODS[this.dmc.rateIndex] / this.sampleRate;\n }\n\n /** A copy of the channels' internal counters and levels — see `ApuSnapshot`. */\n snapshot(): ApuSnapshot {\n const t = this.triangle;\n const n = this.noise;\n return {\n pulse: this.pulses.map((p) => ({\n period: p.period,\n duty: p.duty,\n length: p.length,\n level: p.constant ? p.volume : p.env.decay,\n constant: p.constant,\n muted: p.muted,\n enabled: p.enabled,\n })),\n triangle: {\n period: t.period,\n length: t.length,\n linear: t.linear,\n step: TRIANGLE_FLAT[(t.phase | 0) & 31],\n enabled: t.enabled,\n },\n noise: {\n periodIndex: n.periodIndex,\n shortMode: n.shortMode,\n length: n.length,\n level: n.constant ? n.volume : n.env.decay,\n constant: n.constant,\n enabled: n.enabled,\n },\n dmc: {\n level: this.dmc.level,\n rateIndex: this.dmc.rateIndex,\n loop: this.dmc.loop,\n remaining: this.dmc.remaining,\n address: this.dmc.address,\n enabled: this.dmc.remaining > 0,\n },\n };\n }\n\n private retunePulse(p: Pulse) {\n p.step = CPU_HZ / (2 * (p.period + 1)) / this.sampleRate;\n p.muted = sweepMutes(p.period, p.sweep);\n }\n\n private retuneTriangle() {\n this.triangle.step = CPU_HZ / (this.triangle.period + 1) / this.sampleRate;\n }\n\n private retuneNoise() {\n this.noise.step = CPU_HZ / NOISE_PERIODS[this.noise.periodIndex] / this.sampleRate;\n }\n\n private clockFrameCounter() {\n // 4-step: quarter frames on every step, half frames on steps 2 and 4.\n // 5-step: quarter frames on 1, 2, 3 and 5, half frames on 2 and 5.\n const step = this.frameStep;\n if (this.fiveStep) {\n this.frameStep = (step + 1) % 5;\n if (step !== 3) this.clockQuarterFrame();\n if (step === 1 || step === 4) this.clockHalfFrame();\n } else {\n this.frameStep = (step + 1) % 4;\n this.clockQuarterFrame();\n if (step === 1 || step === 3) this.clockHalfFrame();\n }\n }\n\n private clockQuarterFrame() {\n for (const p of this.pulses) clockEnvelope(p.env, p.volume, p.halt);\n clockEnvelope(this.noise.env, this.noise.volume, this.noise.halt);\n\n const t = this.triangle;\n if (t.linearReloadFlag) t.linear = t.linearReload;\n else if (t.linear > 0) t.linear -= 1;\n if (!t.control) t.linearReloadFlag = false;\n }\n\n private clockHalfFrame() {\n if (this.lengthCounter) {\n for (const p of this.pulses) if (!p.halt && p.length > 0) p.length -= 1;\n if (!this.triangle.control && this.triangle.length > 0) this.triangle.length -= 1;\n if (!this.noise.halt && this.noise.length > 0) this.noise.length -= 1;\n }\n\n for (const p of this.pulses) {\n // The hardware's order: adjust first, then reload or count down.\n if (p.sweepDivider === 0 && p.sweep.enabled && p.sweep.shift > 0 && !p.muted) {\n p.period = Math.max(0, sweepTarget(p.period, p.sweep));\n this.retunePulse(p);\n }\n if (p.sweepDivider === 0 || p.sweepReload) {\n p.sweepDivider = p.sweep.dividerPeriod;\n p.sweepReload = false;\n } else {\n p.sweepDivider -= 1;\n }\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numbers that are the hardware's, not ours.
|
|
3
|
+
*
|
|
4
|
+
* NTSC throughout: the PAL console divides a different crystal and has its own
|
|
5
|
+
* noise and DMC tables. Everything here is quoted from how the 2A03 is wired,
|
|
6
|
+
* so a value that looks tunable is not.
|
|
7
|
+
*/
|
|
8
|
+
/** 21477272 / 12 — the NTSC CPU clock the APU divides down. */
|
|
9
|
+
export declare const CPU_HZ = 1789772.7272727273;
|
|
10
|
+
/**
|
|
11
|
+
* Quarter-frame rate of the frame counter, NTSC: one step every 7457.5 CPU
|
|
12
|
+
* cycles. Envelopes and the triangle's linear counter run at this rate; length
|
|
13
|
+
* counters and sweeps at half of it.
|
|
14
|
+
*/
|
|
15
|
+
export declare const QUARTER_FRAME_HZ: number;
|
|
16
|
+
/** `$400E` low nibble → noise timer period in CPU cycles, NTSC. */
|
|
17
|
+
export declare const NOISE_PERIODS: readonly [4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068];
|
|
18
|
+
/**
|
|
19
|
+
* The top five bits of `$4003`/`$4007`/`$400B`/`$400F` → length counter load,
|
|
20
|
+
* in half-frames. Not monotonic, and not meant to be: it interleaves two series
|
|
21
|
+
* so that one index can hold both a note length and a rest.
|
|
22
|
+
*/
|
|
23
|
+
export declare const LENGTH_TABLE: readonly [10, 254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14, 12, 16, 24, 18, 48, 20, 96, 22, 192, 24, 72, 26, 16, 28, 32, 30];
|
|
24
|
+
/**
|
|
25
|
+
* The pulse duty cycles as the hardware holds them: an 8-step sequence each.
|
|
26
|
+
*
|
|
27
|
+
* 75% is the inversion of 25%, not a fifth shape — which is why duty 1 and
|
|
28
|
+
* duty 3 sound the same on a speaker: a waveform and its inverse differ only in
|
|
29
|
+
* phase. Kept as the real bit patterns so the shape is the chip's.
|
|
30
|
+
*/
|
|
31
|
+
export declare const DUTY_SEQUENCES: readonly [readonly [0, 1, 0, 0, 0, 0, 0, 0], readonly [0, 1, 1, 0, 0, 0, 0, 0], readonly [0, 1, 1, 1, 1, 0, 0, 0], readonly [1, 0, 0, 1, 1, 1, 1, 1]];
|
|
32
|
+
/** The triangle's 32 steps: down 15→0, then back up 0→15. */
|
|
33
|
+
export declare const TRIANGLE_STEPS: readonly number[];
|
|
34
|
+
/**
|
|
35
|
+
* NTSC video frame rate — 29780.5 CPU cycles a frame. A sound driver runs once
|
|
36
|
+
* per frame (from the NMI), so this is the rate at which music reaches the
|
|
37
|
+
* registers, and the rate the note adapter updates a ramp or a bend at.
|
|
38
|
+
*/
|
|
39
|
+
export declare const FRAME_HZ: number;
|
|
40
|
+
/**
|
|
41
|
+
* `$4010` low nibble → DMC timer period in CPU cycles, NTSC: one delta bit per
|
|
42
|
+
* period, so 4.2 kHz at the slowest to 33.1 kHz at the fastest.
|
|
43
|
+
*/
|
|
44
|
+
export declare const DMC_PERIODS: readonly [428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 84, 72, 54];
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Numbers that are the hardware's, not ours.
|
|
3
|
+
*
|
|
4
|
+
* NTSC throughout: the PAL console divides a different crystal and has its own
|
|
5
|
+
* noise and DMC tables. Everything here is quoted from how the 2A03 is wired,
|
|
6
|
+
* so a value that looks tunable is not.
|
|
7
|
+
*/
|
|
8
|
+
/** 21477272 / 12 — the NTSC CPU clock the APU divides down. */
|
|
9
|
+
export const CPU_HZ = 1789772.7272727273;
|
|
10
|
+
/**
|
|
11
|
+
* Quarter-frame rate of the frame counter, NTSC: one step every 7457.5 CPU
|
|
12
|
+
* cycles. Envelopes and the triangle's linear counter run at this rate; length
|
|
13
|
+
* counters and sweeps at half of it.
|
|
14
|
+
*/
|
|
15
|
+
export const QUARTER_FRAME_HZ = CPU_HZ / 7457.5;
|
|
16
|
+
/** `$400E` low nibble → noise timer period in CPU cycles, NTSC. */
|
|
17
|
+
export const NOISE_PERIODS = [
|
|
18
|
+
4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068,
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* The top five bits of `$4003`/`$4007`/`$400B`/`$400F` → length counter load,
|
|
22
|
+
* in half-frames. Not monotonic, and not meant to be: it interleaves two series
|
|
23
|
+
* so that one index can hold both a note length and a rest.
|
|
24
|
+
*/
|
|
25
|
+
export const LENGTH_TABLE = [
|
|
26
|
+
10, 254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14, 12, 16, 24, 18, 48, 20, 96, 22,
|
|
27
|
+
192, 24, 72, 26, 16, 28, 32, 30,
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* The pulse duty cycles as the hardware holds them: an 8-step sequence each.
|
|
31
|
+
*
|
|
32
|
+
* 75% is the inversion of 25%, not a fifth shape — which is why duty 1 and
|
|
33
|
+
* duty 3 sound the same on a speaker: a waveform and its inverse differ only in
|
|
34
|
+
* phase. Kept as the real bit patterns so the shape is the chip's.
|
|
35
|
+
*/
|
|
36
|
+
export const DUTY_SEQUENCES = [
|
|
37
|
+
[0, 1, 0, 0, 0, 0, 0, 0],
|
|
38
|
+
[0, 1, 1, 0, 0, 0, 0, 0],
|
|
39
|
+
[0, 1, 1, 1, 1, 0, 0, 0],
|
|
40
|
+
[1, 0, 0, 1, 1, 1, 1, 1],
|
|
41
|
+
];
|
|
42
|
+
/** The triangle's 32 steps: down 15→0, then back up 0→15. */
|
|
43
|
+
export const TRIANGLE_STEPS = Array.from({ length: 32 }, (_, i) => i < 16 ? 15 - i : i - 16);
|
|
44
|
+
/**
|
|
45
|
+
* NTSC video frame rate — 29780.5 CPU cycles a frame. A sound driver runs once
|
|
46
|
+
* per frame (from the NMI), so this is the rate at which music reaches the
|
|
47
|
+
* registers, and the rate the note adapter updates a ramp or a bend at.
|
|
48
|
+
*/
|
|
49
|
+
export const FRAME_HZ = CPU_HZ / 29780.5;
|
|
50
|
+
/**
|
|
51
|
+
* `$4010` low nibble → DMC timer period in CPU cycles, NTSC: one delta bit per
|
|
52
|
+
* period, so 4.2 kHz at the slowest to 33.1 kHz at the fastest.
|
|
53
|
+
*/
|
|
54
|
+
export const DMC_PERIODS = [
|
|
55
|
+
428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 84, 72, 54,
|
|
56
|
+
];
|
|
57
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,+DAA+D;AAC/D,MAAM,CAAC,MAAM,MAAM,GAAG,kBAAkB,CAAC;AAEzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,GAAG,MAAM,CAAC;AAEhD,mEAAmE;AACnE,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,E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AC,EAAE,EAAE,CACnF,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CACzB,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,GAAG,OAAO,CAAC;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE;CACnE,CAAC","sourcesContent":["/**\n * Numbers that are the hardware's, not ours.\n *\n * NTSC throughout: the PAL console divides a different crystal and has its own\n * noise and DMC tables. Everything here is quoted from how the 2A03 is wired,\n * so a value that looks tunable is not.\n */\n\n/** 21477272 / 12 — the NTSC CPU clock the APU divides down. */\nexport const CPU_HZ = 1789772.7272727273;\n\n/**\n * Quarter-frame rate of the frame counter, NTSC: one step every 7457.5 CPU\n * cycles. Envelopes and the triangle's linear counter run at this rate; length\n * counters and sweeps at half of it.\n */\nexport const QUARTER_FRAME_HZ = CPU_HZ / 7457.5;\n\n/** `$400E` low nibble → noise timer period in CPU cycles, NTSC. */\nexport const NOISE_PERIODS = [\n 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068,\n] as const;\n\n/**\n * The top five bits of `$4003`/`$4007`/`$400B`/`$400F` → length counter load,\n * in half-frames. Not monotonic, and not meant to be: it interleaves two series\n * so that one index can hold both a note length and a rest.\n */\nexport const LENGTH_TABLE = [\n 10, 254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14, 12, 16, 24, 18, 48, 20, 96, 22,\n 192, 24, 72, 26, 16, 28, 32, 30,\n] as const;\n\n/**\n * The pulse duty cycles as the hardware holds them: an 8-step sequence each.\n *\n * 75% is the inversion of 25%, not a fifth shape — which is why duty 1 and\n * duty 3 sound the same on a speaker: a waveform and its inverse differ only in\n * phase. Kept as the real bit patterns so the shape is the chip's.\n */\nexport const DUTY_SEQUENCES = [\n [0, 1, 0, 0, 0, 0, 0, 0],\n [0, 1, 1, 0, 0, 0, 0, 0],\n [0, 1, 1, 1, 1, 0, 0, 0],\n [1, 0, 0, 1, 1, 1, 1, 1],\n] as const;\n\n/** The triangle's 32 steps: down 15→0, then back up 0→15. */\nexport const TRIANGLE_STEPS: readonly number[] = Array.from({ length: 32 }, (_, i) =>\n i < 16 ? 15 - i : i - 16\n);\n\n/**\n * NTSC video frame rate — 29780.5 CPU cycles a frame. A sound driver runs once\n * per frame (from the NMI), so this is the rate at which music reaches the\n * registers, and the rate the note adapter updates a ramp or a bend at.\n */\nexport const FRAME_HZ = CPU_HZ / 29780.5;\n\n/**\n * `$4010` low nibble → DMC timer period in CPU cycles, NTSC: one delta bit per\n * period, so 4.2 kHz at the slowest to 33.1 kHz at the fastest.\n */\nexport const DMC_PERIODS = [\n 428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 84, 72, 54,\n] as const;\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @xrom/apu — the NES sound chip (2A03 APU) in TypeScript.
|
|
3
|
+
*
|
|
4
|
+
* Two layers, usable separately:
|
|
5
|
+
*
|
|
6
|
+
* - **The chip.** `renderWrites(writes, seconds)` plays a list of register
|
|
7
|
+
* writes — `{ time, address: 0x4000..0x4017, value }` — and returns samples.
|
|
8
|
+
* Every NES game's music reaches the speaker as writes like these, so this
|
|
9
|
+
* plays any game's music once its writes are known (from an emulator trace,
|
|
10
|
+
* say). `Apu` is the same chip one sample at a time, for live use.
|
|
11
|
+
* - **The note adapter.** `renderNotes(input)` takes music already decoded into
|
|
12
|
+
* notes and writes what a driver would have written to sound them.
|
|
13
|
+
*/
|
|
14
|
+
export { CPU_HZ, DMC_PERIODS, DUTY_SEQUENCES, FRAME_HZ, LENGTH_TABLE, NOISE_PERIODS, QUARTER_FRAME_HZ, TRIANGLE_STEPS, } from './constants.js';
|
|
15
|
+
export { Apu, type ApuChipOptions, type ApuMemory, type ApuSnapshot, type ChannelBuffers, } from './chip.js';
|
|
16
|
+
export { mixApu, OutputFilters } from './mixer.js';
|
|
17
|
+
export { SWEEP_HZ, decodeSweep, sweepMutes, sweepTarget, sweepTrace, type SweepConfig, } from './sweep.js';
|
|
18
|
+
export { renderWrites, type Click, type RegisterWrite, type Render, type RenderOptions, } from './render.js';
|
|
19
|
+
export { bendAt, notesToWrites, renderNotes, type ApuInput, type ApuOptions, type ApuRender, type BendCurve, type DrumRow, type PitchedRow, } from './notes.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @xrom/apu — the NES sound chip (2A03 APU) in TypeScript.
|
|
3
|
+
*
|
|
4
|
+
* Two layers, usable separately:
|
|
5
|
+
*
|
|
6
|
+
* - **The chip.** `renderWrites(writes, seconds)` plays a list of register
|
|
7
|
+
* writes — `{ time, address: 0x4000..0x4017, value }` — and returns samples.
|
|
8
|
+
* Every NES game's music reaches the speaker as writes like these, so this
|
|
9
|
+
* plays any game's music once its writes are known (from an emulator trace,
|
|
10
|
+
* say). `Apu` is the same chip one sample at a time, for live use.
|
|
11
|
+
* - **The note adapter.** `renderNotes(input)` takes music already decoded into
|
|
12
|
+
* notes and writes what a driver would have written to sound them.
|
|
13
|
+
*/
|
|
14
|
+
export { CPU_HZ, DMC_PERIODS, DUTY_SEQUENCES, FRAME_HZ, LENGTH_TABLE, NOISE_PERIODS, QUARTER_FRAME_HZ, TRIANGLE_STEPS, } from './constants.js';
|
|
15
|
+
export { Apu, } from './chip.js';
|
|
16
|
+
export { mixApu, OutputFilters } from './mixer.js';
|
|
17
|
+
export { SWEEP_HZ, decodeSweep, sweepMutes, sweepTarget, sweepTrace, } from './sweep.js';
|
|
18
|
+
export { renderWrites, } from './render.js';
|
|
19
|
+
export { bendAt, notesToWrites, renderNotes, } from './notes.js';
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,GAAG,GAKJ,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EACL,QAAQ,EACR,WAAW,EACX,UAAU,EACV,WAAW,EACX,UAAU,GAEX,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,YAAY,GAKb,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,MAAM,EACN,aAAa,EACb,WAAW,GAOZ,MAAM,YAAY,CAAC","sourcesContent":["/**\n * @xrom/apu — the NES sound chip (2A03 APU) in TypeScript.\n *\n * Two layers, usable separately:\n *\n * - **The chip.** `renderWrites(writes, seconds)` plays a list of register\n * writes — `{ time, address: 0x4000..0x4017, value }` — and returns samples.\n * Every NES game's music reaches the speaker as writes like these, so this\n * plays any game's music once its writes are known (from an emulator trace,\n * say). `Apu` is the same chip one sample at a time, for live use.\n * - **The note adapter.** `renderNotes(input)` takes music already decoded into\n * notes and writes what a driver would have written to sound them.\n */\n\nexport {\n CPU_HZ,\n DMC_PERIODS,\n DUTY_SEQUENCES,\n FRAME_HZ,\n LENGTH_TABLE,\n NOISE_PERIODS,\n QUARTER_FRAME_HZ,\n TRIANGLE_STEPS,\n} from './constants.js';\nexport {\n Apu,\n type ApuChipOptions,\n type ApuMemory,\n type ApuSnapshot,\n type ChannelBuffers,\n} from './chip.js';\nexport { mixApu, OutputFilters } from './mixer.js';\nexport {\n SWEEP_HZ,\n decodeSweep,\n sweepMutes,\n sweepTarget,\n sweepTrace,\n type SweepConfig,\n} from './sweep.js';\nexport {\n renderWrites,\n type Click,\n type RegisterWrite,\n type Render,\n type RenderOptions,\n} from './render.js';\nexport {\n bendAt,\n notesToWrites,\n renderNotes,\n type ApuInput,\n type ApuOptions,\n type ApuRender,\n type BendCurve,\n type DrumRow,\n type PitchedRow,\n} from './notes.js';\n"]}
|
package/dist/lfsr.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The noise channel's shift register, precomputed.
|
|
3
|
+
*
|
|
4
|
+
* In its normal mode the 15-bit LFSR (feedback from bits 0 and 1) visits every
|
|
5
|
+
* non-zero state once before repeating: a single cycle of 32767. So its whole
|
|
6
|
+
* future is a table, and N shifts from any state are an index plus N — no loop.
|
|
7
|
+
* That matters because at the shortest periods the register shifts several
|
|
8
|
+
* times per output sample, every sample, whether or not the channel is audible.
|
|
9
|
+
*
|
|
10
|
+
* `LONG_HIGH_BEFORE[i]` counts, over the first `i` states of the cycle, those
|
|
11
|
+
* with bit 0 clear — the states in which the channel is NOT gated off. The
|
|
12
|
+
* table covers the cycle twice so a run of shifts can cross the seam without a
|
|
13
|
+
* wrap check.
|
|
14
|
+
*
|
|
15
|
+
* Short mode (feedback from bits 0 and 6) splits the states into several short
|
|
16
|
+
* loops instead of one, so it is stepped the plain way; it is rare in music.
|
|
17
|
+
*/
|
|
18
|
+
export declare const LONG_CYCLE = 32767;
|
|
19
|
+
/** State at each position of the cycle, starting from the power-on state 1. */
|
|
20
|
+
export declare const LONG_STATES: Uint16Array<ArrayBuffer>;
|
|
21
|
+
/** Position of each state in the cycle. Index 0 (the dead all-zero state) is unused. */
|
|
22
|
+
export declare const LONG_INDEX: Uint16Array<ArrayBuffer>;
|
|
23
|
+
/** Prefix counts of "bit 0 clear" over the cycle, doubled; see above. */
|
|
24
|
+
export declare const LONG_HIGH_BEFORE: Uint16Array<ArrayBuffer>;
|
package/dist/lfsr.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The noise channel's shift register, precomputed.
|
|
3
|
+
*
|
|
4
|
+
* In its normal mode the 15-bit LFSR (feedback from bits 0 and 1) visits every
|
|
5
|
+
* non-zero state once before repeating: a single cycle of 32767. So its whole
|
|
6
|
+
* future is a table, and N shifts from any state are an index plus N — no loop.
|
|
7
|
+
* That matters because at the shortest periods the register shifts several
|
|
8
|
+
* times per output sample, every sample, whether or not the channel is audible.
|
|
9
|
+
*
|
|
10
|
+
* `LONG_HIGH_BEFORE[i]` counts, over the first `i` states of the cycle, those
|
|
11
|
+
* with bit 0 clear — the states in which the channel is NOT gated off. The
|
|
12
|
+
* table covers the cycle twice so a run of shifts can cross the seam without a
|
|
13
|
+
* wrap check.
|
|
14
|
+
*
|
|
15
|
+
* Short mode (feedback from bits 0 and 6) splits the states into several short
|
|
16
|
+
* loops instead of one, so it is stepped the plain way; it is rare in music.
|
|
17
|
+
*/
|
|
18
|
+
export const LONG_CYCLE = 32767;
|
|
19
|
+
/** State at each position of the cycle, starting from the power-on state 1. */
|
|
20
|
+
export const LONG_STATES = new Uint16Array(LONG_CYCLE);
|
|
21
|
+
/** Position of each state in the cycle. Index 0 (the dead all-zero state) is unused. */
|
|
22
|
+
export const LONG_INDEX = new Uint16Array(32768);
|
|
23
|
+
/** Prefix counts of "bit 0 clear" over the cycle, doubled; see above. */
|
|
24
|
+
export const LONG_HIGH_BEFORE = new Uint16Array(2 * LONG_CYCLE + 1);
|
|
25
|
+
{
|
|
26
|
+
let state = 1;
|
|
27
|
+
for (let i = 0; i < LONG_CYCLE; i++) {
|
|
28
|
+
LONG_STATES[i] = state;
|
|
29
|
+
LONG_INDEX[state] = i;
|
|
30
|
+
const feedback = (state & 1) ^ ((state >> 1) & 1);
|
|
31
|
+
state = (state >> 1) | (feedback << 14);
|
|
32
|
+
}
|
|
33
|
+
for (let i = 0; i < 2 * LONG_CYCLE; i++) {
|
|
34
|
+
LONG_HIGH_BEFORE[i + 1] = LONG_HIGH_BEFORE[i] + ((LONG_STATES[i % LONG_CYCLE] & 1) === 0 ? 1 : 0);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=lfsr.js.map
|
package/dist/lfsr.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lfsr.js","sourceRoot":"","sources":["../src/lfsr.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,CAAC;AAEhC,+EAA+E;AAC/E,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,UAAU,CAAC,CAAC;AACvD,wFAAwF;AACxF,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,WAAW,CAAC,KAAK,CAAC,CAAC;AACjD,yEAAyE;AACzE,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,WAAW,CAAC,CAAC,GAAG,UAAU,GAAG,CAAC,CAAC,CAAC;AAEpE,CAAC;IACC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC,WAAW,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;QACvB,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACtB,MAAM,QAAQ,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAClD,KAAK,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC;IAC1C,CAAC;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,UAAU,EAAE,CAAC,EAAE,EAAE,CAAC;QACxC,gBAAgB,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACpG,CAAC;AACH,CAAC","sourcesContent":["/**\n * The noise channel's shift register, precomputed.\n *\n * In its normal mode the 15-bit LFSR (feedback from bits 0 and 1) visits every\n * non-zero state once before repeating: a single cycle of 32767. So its whole\n * future is a table, and N shifts from any state are an index plus N — no loop.\n * That matters because at the shortest periods the register shifts several\n * times per output sample, every sample, whether or not the channel is audible.\n *\n * `LONG_HIGH_BEFORE[i]` counts, over the first `i` states of the cycle, those\n * with bit 0 clear — the states in which the channel is NOT gated off. The\n * table covers the cycle twice so a run of shifts can cross the seam without a\n * wrap check.\n *\n * Short mode (feedback from bits 0 and 6) splits the states into several short\n * loops instead of one, so it is stepped the plain way; it is rare in music.\n */\n\nexport const LONG_CYCLE = 32767;\n\n/** State at each position of the cycle, starting from the power-on state 1. */\nexport const LONG_STATES = new Uint16Array(LONG_CYCLE);\n/** Position of each state in the cycle. Index 0 (the dead all-zero state) is unused. */\nexport const LONG_INDEX = new Uint16Array(32768);\n/** Prefix counts of \"bit 0 clear\" over the cycle, doubled; see above. */\nexport const LONG_HIGH_BEFORE = new Uint16Array(2 * LONG_CYCLE + 1);\n\n{\n let state = 1;\n for (let i = 0; i < LONG_CYCLE; i++) {\n LONG_STATES[i] = state;\n LONG_INDEX[state] = i;\n const feedback = (state & 1) ^ ((state >> 1) & 1);\n state = (state >> 1) | (feedback << 14);\n }\n for (let i = 0; i < 2 * LONG_CYCLE; i++) {\n LONG_HIGH_BEFORE[i + 1] = LONG_HIGH_BEFORE[i] + ((LONG_STATES[i % LONG_CYCLE] & 1) === 0 ? 1 : 0);\n }\n}\n"]}
|
package/dist/mixer.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The APU's mixer, from the hardware's own measured curves.
|
|
3
|
+
*
|
|
4
|
+
* Non-linear on purpose: two pulses at full volume are not twice one pulse, and
|
|
5
|
+
* the triangle/noise group is louder per unit than the pulses. It also means
|
|
6
|
+
* the channels are not independent — a triangle held high takes level from the
|
|
7
|
+
* noise beside it. Getting this wrong is what makes a naive NES synth sound thin
|
|
8
|
+
* and shrill.
|
|
9
|
+
*
|
|
10
|
+
* Inputs are each channel's output level — 0..15, and 0..127 for the DMC; the
|
|
11
|
+
* result is 0..~1 and never negative. With the DMC at 0 its term adds exactly
|
|
12
|
+
* nothing, so a mix without it is bit for bit the four-channel one. Centring it on zero is the output filters' job.
|
|
13
|
+
*/
|
|
14
|
+
export declare function mixApu(pulse1: number, pulse2: number, triangle: number, noise: number, dmc?: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* `mixApu`'s two halves for whole-number inputs, precomputed with the same
|
|
17
|
+
* expressions — so a lookup gives exactly the float the formula would.
|
|
18
|
+
*
|
|
19
|
+
* The pulses are always whole numbers (0..30 summed) unless a fader scales them;
|
|
20
|
+
* the triangle is a whole step, and the noise is only fractional while it
|
|
21
|
+
* sounds (its gate is averaged over the shifts in a sample). The renderer uses
|
|
22
|
+
* these for the common cases and the formula for the rest.
|
|
23
|
+
*/
|
|
24
|
+
export declare const PULSE_MIX: Float64Array<ArrayBuffer>;
|
|
25
|
+
/** Triangle alone, noise silent: `triangle / 8227 + 0 / 12241` through the curve. */
|
|
26
|
+
export declare const TRIANGLE_MIX: Float64Array<ArrayBuffer>;
|
|
27
|
+
/**
|
|
28
|
+
* The analogue stage after the mixer, as the console has it.
|
|
29
|
+
*
|
|
30
|
+
* Two RC high-passes (90 Hz and 440 Hz) and a low-pass at 14 kHz, one pole each.
|
|
31
|
+
* The 90 Hz one is what centres the signal on zero — without it every note
|
|
32
|
+
* starts and ends on a step in the DC level, and each drum hit arrives as a
|
|
33
|
+
* click the size of the sound itself.
|
|
34
|
+
*
|
|
35
|
+
* **The 440 Hz high-pass is off unless asked for.** It is real, but it costs
|
|
36
|
+
* 18 dB at 82 Hz, which is where a triangle bass line lives: on a 1980s
|
|
37
|
+
* television that loss was part of the sound, in headphones it just sounds
|
|
38
|
+
* thin. `consoleBass` puts it back.
|
|
39
|
+
*/
|
|
40
|
+
export declare class OutputFilters {
|
|
41
|
+
private hp90X;
|
|
42
|
+
private hp90Y;
|
|
43
|
+
private hp440X;
|
|
44
|
+
private hp440Y;
|
|
45
|
+
private lowPrev;
|
|
46
|
+
private readonly hp90;
|
|
47
|
+
private readonly hp440;
|
|
48
|
+
private readonly low;
|
|
49
|
+
private readonly consoleBass;
|
|
50
|
+
constructor(rate: number, consoleBass: boolean);
|
|
51
|
+
step(input: number): number;
|
|
52
|
+
}
|
package/dist/mixer.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The APU's mixer, from the hardware's own measured curves.
|
|
3
|
+
*
|
|
4
|
+
* Non-linear on purpose: two pulses at full volume are not twice one pulse, and
|
|
5
|
+
* the triangle/noise group is louder per unit than the pulses. It also means
|
|
6
|
+
* the channels are not independent — a triangle held high takes level from the
|
|
7
|
+
* noise beside it. Getting this wrong is what makes a naive NES synth sound thin
|
|
8
|
+
* and shrill.
|
|
9
|
+
*
|
|
10
|
+
* Inputs are each channel's output level — 0..15, and 0..127 for the DMC; the
|
|
11
|
+
* result is 0..~1 and never negative. With the DMC at 0 its term adds exactly
|
|
12
|
+
* nothing, so a mix without it is bit for bit the four-channel one. Centring it on zero is the output filters' job.
|
|
13
|
+
*/
|
|
14
|
+
export function mixApu(pulse1, pulse2, triangle, noise, dmc = 0) {
|
|
15
|
+
const pulseSum = pulse1 + pulse2;
|
|
16
|
+
const pulseOut = pulseSum === 0 ? 0 : 95.88 / (8128 / pulseSum + 100);
|
|
17
|
+
const tnd = triangle / 8227 + noise / 12241 + dmc / 22638;
|
|
18
|
+
const tndOut = tnd === 0 ? 0 : 159.79 / (1 / tnd + 100);
|
|
19
|
+
return pulseOut + tndOut;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `mixApu`'s two halves for whole-number inputs, precomputed with the same
|
|
23
|
+
* expressions — so a lookup gives exactly the float the formula would.
|
|
24
|
+
*
|
|
25
|
+
* The pulses are always whole numbers (0..30 summed) unless a fader scales them;
|
|
26
|
+
* the triangle is a whole step, and the noise is only fractional while it
|
|
27
|
+
* sounds (its gate is averaged over the shifts in a sample). The renderer uses
|
|
28
|
+
* these for the common cases and the formula for the rest.
|
|
29
|
+
*/
|
|
30
|
+
export const PULSE_MIX = Float64Array.from({ length: 31 }, (_, sum) => sum === 0 ? 0 : 95.88 / (8128 / sum + 100));
|
|
31
|
+
/** Triangle alone, noise silent: `triangle / 8227 + 0 / 12241` through the curve. */
|
|
32
|
+
export const TRIANGLE_MIX = Float64Array.from({ length: 16 }, (_, level) => {
|
|
33
|
+
const tnd = level / 8227 + 0 / 12241;
|
|
34
|
+
return tnd === 0 ? 0 : 159.79 / (1 / tnd + 100);
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* The analogue stage after the mixer, as the console has it.
|
|
38
|
+
*
|
|
39
|
+
* Two RC high-passes (90 Hz and 440 Hz) and a low-pass at 14 kHz, one pole each.
|
|
40
|
+
* The 90 Hz one is what centres the signal on zero — without it every note
|
|
41
|
+
* starts and ends on a step in the DC level, and each drum hit arrives as a
|
|
42
|
+
* click the size of the sound itself.
|
|
43
|
+
*
|
|
44
|
+
* **The 440 Hz high-pass is off unless asked for.** It is real, but it costs
|
|
45
|
+
* 18 dB at 82 Hz, which is where a triangle bass line lives: on a 1980s
|
|
46
|
+
* television that loss was part of the sound, in headphones it just sounds
|
|
47
|
+
* thin. `consoleBass` puts it back.
|
|
48
|
+
*/
|
|
49
|
+
export class OutputFilters {
|
|
50
|
+
constructor(rate, consoleBass) {
|
|
51
|
+
// Previous input and output of each stage, as plain numbers: this runs once
|
|
52
|
+
// per sample, and a fresh object per sample is garbage by the megabyte.
|
|
53
|
+
this.hp90X = 0;
|
|
54
|
+
this.hp90Y = 0;
|
|
55
|
+
this.hp440X = 0;
|
|
56
|
+
this.hp440Y = 0;
|
|
57
|
+
this.lowPrev = 0;
|
|
58
|
+
this.consoleBass = consoleBass;
|
|
59
|
+
const highPass = (hz) => {
|
|
60
|
+
const rc = 1 / (2 * Math.PI * hz);
|
|
61
|
+
return rc / (rc + 1 / rate);
|
|
62
|
+
};
|
|
63
|
+
const lowPass = (hz) => {
|
|
64
|
+
const rc = 1 / (2 * Math.PI * hz);
|
|
65
|
+
return 1 / rate / (rc + 1 / rate);
|
|
66
|
+
};
|
|
67
|
+
this.hp90 = highPass(90);
|
|
68
|
+
this.hp440 = highPass(440);
|
|
69
|
+
this.low = lowPass(14000);
|
|
70
|
+
}
|
|
71
|
+
step(input) {
|
|
72
|
+
const a = this.hp90 * (this.hp90Y + input - this.hp90X);
|
|
73
|
+
this.hp90X = input;
|
|
74
|
+
this.hp90Y = a;
|
|
75
|
+
let b = a;
|
|
76
|
+
if (this.consoleBass) {
|
|
77
|
+
b = this.hp440 * (this.hp440Y + a - this.hp440X);
|
|
78
|
+
this.hp440X = a;
|
|
79
|
+
this.hp440Y = b;
|
|
80
|
+
}
|
|
81
|
+
this.lowPrev += this.low * (b - this.lowPrev);
|
|
82
|
+
return this.lowPrev;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=mixer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mixer.js","sourceRoot":"","sources":["../src/mixer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,MAAM,CACpB,MAAc,EACd,MAAc,EACd,QAAgB,EAChB,KAAa,EACb,GAAG,GAAG,CAAC;IAEP,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;IACjC,MAAM,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AI,CAAC,MAAM,CAAC,CAAC;YACjD,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;YAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAClB,CAAC;QAED,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF","sourcesContent":["/**\n * The APU's mixer, from the hardware's own measured curves.\n *\n * Non-linear on purpose: two pulses at full volume are not twice one pulse, and\n * the triangle/noise group is louder per unit than the pulses. It also means\n * the channels are not independent — a triangle held high takes level from the\n * noise beside it. Getting this wrong is what makes a naive NES synth sound thin\n * and shrill.\n *\n * Inputs are each channel's output level — 0..15, and 0..127 for the DMC; the\n * result is 0..~1 and never negative. With the DMC at 0 its term adds exactly\n * nothing, so a mix without it is bit for bit the four-channel one. Centring it on zero is the output filters' job.\n */\nexport function mixApu(\n pulse1: number,\n pulse2: number,\n triangle: number,\n noise: number,\n dmc = 0\n): number {\n const pulseSum = pulse1 + pulse2;\n const pulseOut = pulseSum === 0 ? 0 : 95.88 / (8128 / pulseSum + 100);\n const tnd = triangle / 8227 + noise / 12241 + dmc / 22638;\n const tndOut = tnd === 0 ? 0 : 159.79 / (1 / tnd + 100);\n return pulseOut + tndOut;\n}\n\n/**\n * `mixApu`'s two halves for whole-number inputs, precomputed with the same\n * expressions — so a lookup gives exactly the float the formula would.\n *\n * The pulses are always whole numbers (0..30 summed) unless a fader scales them;\n * the triangle is a whole step, and the noise is only fractional while it\n * sounds (its gate is averaged over the shifts in a sample). The renderer uses\n * these for the common cases and the formula for the rest.\n */\nexport const PULSE_MIX = Float64Array.from({ length: 31 }, (_, sum) =>\n sum === 0 ? 0 : 95.88 / (8128 / sum + 100)\n);\n\n/** Triangle alone, noise silent: `triangle / 8227 + 0 / 12241` through the curve. */\nexport const TRIANGLE_MIX = Float64Array.from({ length: 16 }, (_, level) => {\n const tnd = level / 8227 + 0 / 12241;\n return tnd === 0 ? 0 : 159.79 / (1 / tnd + 100);\n});\n\n/**\n * The analogue stage after the mixer, as the console has it.\n *\n * Two RC high-passes (90 Hz and 440 Hz) and a low-pass at 14 kHz, one pole each.\n * The 90 Hz one is what centres the signal on zero — without it every note\n * starts and ends on a step in the DC level, and each drum hit arrives as a\n * click the size of the sound itself.\n *\n * **The 440 Hz high-pass is off unless asked for.** It is real, but it costs\n * 18 dB at 82 Hz, which is where a triangle bass line lives: on a 1980s\n * television that loss was part of the sound, in headphones it just sounds\n * thin. `consoleBass` puts it back.\n */\nexport class OutputFilters {\n // Previous input and output of each stage, as plain numbers: this runs once\n // per sample, and a fresh object per sample is garbage by the megabyte.\n private hp90X = 0;\n private hp90Y = 0;\n private hp440X = 0;\n private hp440Y = 0;\n private lowPrev = 0;\n private readonly hp90: number;\n private readonly hp440: number;\n private readonly low: number;\n private readonly consoleBass: boolean;\n\n constructor(rate: number, consoleBass: boolean) {\n this.consoleBass = consoleBass;\n const highPass = (hz: number) => {\n const rc = 1 / (2 * Math.PI * hz);\n return rc / (rc + 1 / rate);\n };\n const lowPass = (hz: number) => {\n const rc = 1 / (2 * Math.PI * hz);\n return 1 / rate / (rc + 1 / rate);\n };\n this.hp90 = highPass(90);\n this.hp440 = highPass(440);\n this.low = lowPass(14000);\n }\n\n step(input: number): number {\n const a = this.hp90 * (this.hp90Y + input - this.hp90X);\n this.hp90X = input;\n this.hp90Y = a;\n\n let b = a;\n if (this.consoleBass) {\n b = this.hp440 * (this.hp440Y + a - this.hp440X);\n this.hp440X = a;\n this.hp440Y = b;\n }\n\n this.lowPrev += this.low * (b - this.lowPrev);\n return this.lowPrev;\n }\n}\n"]}
|
package/dist/notes.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Notes in, register writes out — the adapter between a decoded score and the
|
|
3
|
+
* chip.
|
|
4
|
+
*
|
|
5
|
+
* A music driver's job is to turn its own data into register writes, once a
|
|
6
|
+
* frame. When a game's music has already been decoded into notes (start, length,
|
|
7
|
+
* period, volume, duty, and how each of those moves), this plays the driver's
|
|
8
|
+
* part: it writes what a driver would have written to sound those notes, and
|
|
9
|
+
* `renderWrites` does the rest. So the chip never learns what a note is, and
|
|
10
|
+
* anything the adapter gets wrong is visible as a register write rather than
|
|
11
|
+
* hidden inside a synth.
|
|
12
|
+
*
|
|
13
|
+
* The note shape is the one xrom.dev exports for Battletoads, but nothing in it
|
|
14
|
+
* is that game's: a tick is whatever the caller says it is in seconds.
|
|
15
|
+
*
|
|
16
|
+
* What it writes, per note:
|
|
17
|
+
* - at the start, the channel's full register set — which on a pulse restarts
|
|
18
|
+
* the duty sequencer and the envelope and reloads the length counter,
|
|
19
|
+
* exactly as the driver's own `$4003` write does;
|
|
20
|
+
* - once a frame after that, the level while it ramps and the period while it
|
|
21
|
+
* bends, as whole numbers, because the registers hold whole numbers — so a
|
|
22
|
+
* fade is a staircase at 60 Hz, as it is on the console;
|
|
23
|
+
* - at the end, silence — unless the next note on the channel starts at the
|
|
24
|
+
* same moment.
|
|
25
|
+
*/
|
|
26
|
+
import { type RegisterWrite, type RenderOptions } from './render.js';
|
|
27
|
+
/**
|
|
28
|
+
* `[midi, startTick, durationTicks, channel 0..2, volume, period, duty,
|
|
29
|
+
* volumeEnd, lengthTicks, bendIndex, envelope, sweep]` — `lengthTicks` counts at
|
|
30
|
+
* 120 Hz and 0 means unlimited; `bendIndex` points into `ApuInput.bends`, or is
|
|
31
|
+
* -1; `envelope` is the low five bits of `$4000` when the chip's own envelope
|
|
32
|
+
* runs (period, and bit 4 for loop), or -1 for constant volume; `sweep` is the
|
|
33
|
+
* `$4001` byte, or -1/absent when the data has none.
|
|
34
|
+
*/
|
|
35
|
+
export type PitchedRow = readonly [
|
|
36
|
+
number,
|
|
37
|
+
number,
|
|
38
|
+
number,
|
|
39
|
+
number,
|
|
40
|
+
number,
|
|
41
|
+
number,
|
|
42
|
+
number,
|
|
43
|
+
number,
|
|
44
|
+
number,
|
|
45
|
+
number,
|
|
46
|
+
number?,
|
|
47
|
+
number?
|
|
48
|
+
];
|
|
49
|
+
/** A pitch curve: `[position 0..1, semitones]`, in order. */
|
|
50
|
+
export type BendCurve = readonly (readonly [number, number])[];
|
|
51
|
+
/**
|
|
52
|
+
* `[periodIndex, startTick, durationTicks, volume, volumeEnd, lengthTicks,
|
|
53
|
+
* envelope]` — a noise-channel note. `periodIndex` is the low nibble of `$400E`.
|
|
54
|
+
*/
|
|
55
|
+
export type DrumRow = readonly [number, number, number, number, number, number, number?];
|
|
56
|
+
export interface ApuInput {
|
|
57
|
+
pitched: readonly PitchedRow[];
|
|
58
|
+
drums: readonly DrumRow[];
|
|
59
|
+
/** Pitch curves the notes point at. Shared, because the shapes repeat. */
|
|
60
|
+
bends?: readonly BendCurve[];
|
|
61
|
+
/** Seconds per tick. */
|
|
62
|
+
secondsPerTick: number;
|
|
63
|
+
/** Length of the piece in ticks. */
|
|
64
|
+
totalTicks: number;
|
|
65
|
+
/**
|
|
66
|
+
* Where the repeat starts, in ticks. Not always 0: a driver's first ticks are
|
|
67
|
+
* often one-time setup, so a track that loops on the whole buffer replays its
|
|
68
|
+
* intro every time round. Absent means the piece does not loop.
|
|
69
|
+
*/
|
|
70
|
+
loopStart?: number;
|
|
71
|
+
}
|
|
72
|
+
export interface ApuOptions extends Omit<RenderOptions, 'clicks' | 'tail'> {
|
|
73
|
+
/** Seconds of silence before the first note. */
|
|
74
|
+
lead?: number;
|
|
75
|
+
/** Click on every `metronome` ticks, loud on every fourth. 0 is off. */
|
|
76
|
+
metronome?: number;
|
|
77
|
+
}
|
|
78
|
+
export interface ApuRender {
|
|
79
|
+
samples: Float32Array;
|
|
80
|
+
sampleRate: number;
|
|
81
|
+
/** Seconds of silence before tick 0 — what the caller asked for as `lead`. */
|
|
82
|
+
lead: number;
|
|
83
|
+
/** Length of the whole buffer, tail included. */
|
|
84
|
+
duration: number;
|
|
85
|
+
/**
|
|
86
|
+
* Where the music itself ends, in seconds — the loop point. The buffer runs a
|
|
87
|
+
* little longer so the filters can ring out; looping on the buffer would play
|
|
88
|
+
* that tail as a gap every time round.
|
|
89
|
+
*/
|
|
90
|
+
loopEnd: number;
|
|
91
|
+
/** Where the repeat starts, in seconds — the other end of the seam. */
|
|
92
|
+
loopStart: number;
|
|
93
|
+
/** Loudest absolute sample, before clamping. */
|
|
94
|
+
peak: number;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Semitones at `position` through a note, straight-line between the curve's own
|
|
98
|
+
* points; outside it, the nearest end.
|
|
99
|
+
*/
|
|
100
|
+
export declare function bendAt(curve: BendCurve, position: number): number;
|
|
101
|
+
/**
|
|
102
|
+
* The writes that play `input`, as a driver would make them.
|
|
103
|
+
*
|
|
104
|
+
* @param lead seconds of silence before tick 0
|
|
105
|
+
*/
|
|
106
|
+
export declare function notesToWrites(input: ApuInput, lead?: number): RegisterWrite[];
|
|
107
|
+
/** Render a decoded piece: `notesToWrites` into `renderWrites`, with the tick-based extras. */
|
|
108
|
+
export declare function renderNotes(input: ApuInput, options?: ApuOptions): ApuRender;
|