@xromdev/apu 0.5.1 → 0.6.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/README.md +14 -6
- package/dist/chip.d.ts +20 -2
- package/dist/chip.js +28 -1
- package/dist/chip.js.map +1 -1
- package/dist/expansion.d.ts +57 -0
- package/dist/expansion.js +193 -1
- package/dist/expansion.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/notes.d.ts +19 -4
- package/dist/notes.js +143 -8
- package/dist/notes.js.map +1 -1
- package/dist/render.d.ts +3 -2
- package/dist/render.js +22 -2
- package/dist/render.js.map +1 -1
- package/dist/vrc7.d.ts +77 -0
- package/dist/vrc7.js +303 -0
- package/dist/vrc7.js.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,13 +15,16 @@ they come out. **Try it live in the browser:
|
|
|
15
15
|
[xrom.dev/utils/apu](https://xrom.dev/utils/apu).**
|
|
16
16
|
|
|
17
17
|
- All five channels: two pulses, the triangle, noise and the DMC (samples).
|
|
18
|
-
-
|
|
18
|
+
- Five Famicom expansion chips, when a game has them: the Disk System's
|
|
19
19
|
wavetable voice (FDS, `$4040`–`$408A`), Namco's 163 (up to eight wavetable
|
|
20
|
-
channels, through its ports at `$F800`/`$4800`)
|
|
21
|
-
with eight duties and a sawtooth, `$9000`–`$B002`)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
channels, through its ports at `$F800`/`$4800`), Konami's VRC6 (two pulses
|
|
21
|
+
with eight duties and a sawtooth, `$9000`–`$B002`), Nintendo's MMC5 (two more
|
|
22
|
+
2A03-style pulses and an 8-bit PCM, `$5000`–`$5015`) and Konami's VRC7 (six
|
|
23
|
+
channels of two-operator FM, a cut-down YM2413, through `$9010`/`$9030`, with
|
|
24
|
+
its fifteen built-in instruments). `renderWrites` turns them on when the writes
|
|
25
|
+
talk to them; `renderNotes` plays notes on them, the wavetables and the VRC7's
|
|
26
|
+
own instrument from `waves` (`EXPANSION_CHANNELS`: FDS is channel 5, the
|
|
27
|
+
N163's 6–13, the VRC6's 14–16, the MMC5's 17–18, the VRC7's 20–25).
|
|
25
28
|
- No dependencies, ~6 KB gzipped, runs anywhere JavaScript does (browser, worker, Node).
|
|
26
29
|
- Pure and deterministic: the same writes always give the same samples, so it
|
|
27
30
|
can be tested by measuring rather than by ear.
|
|
@@ -100,6 +103,11 @@ field, `levelIndex`. A curve is `[position 0..1, level 0..15]` at each change,
|
|
|
100
103
|
and holds from one point to the next, as a driver writes whole levels a frame at
|
|
101
104
|
a time. Pulse and noise notes take them; `levelAt(curve, position)` reads one.
|
|
102
105
|
|
|
106
|
+
A noise note can also carry `shortMode` after `levelIndex`: 1 strikes it in the
|
|
107
|
+
short mode, bit 7 of `$400E`, whose 93-step loop gives a metallic, near-pitched
|
|
108
|
+
buzz instead of the hiss — the low beat of The Punisher's boss theme. Left out
|
|
109
|
+
or 0, the note is the ordinary noise.
|
|
110
|
+
|
|
103
111
|
`notesToWrites` gives you the writes on their own, if you want to see them.
|
|
104
112
|
|
|
105
113
|
### Live
|
package/dist/chip.d.ts
CHANGED
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
* same ones, evaluated at a different moment, so the output is bit for bit what
|
|
30
30
|
* recomputing them every sample gives.
|
|
31
31
|
*/
|
|
32
|
-
import { FdsChip, N163Chip, Vrc6Chip, type FdsSnapshot, type N163Snapshot, type Vrc6Snapshot } from './expansion.js';
|
|
32
|
+
import { FdsChip, N163Chip, Vrc6Chip, Mmc5Chip, type FdsSnapshot, type Mmc5Snapshot, type N163Snapshot, type Vrc6Snapshot } from './expansion.js';
|
|
33
|
+
import { Vrc7Chip, type Vrc7Snapshot } from './vrc7.js';
|
|
33
34
|
/** Where every channel's output levels go, sample by sample. */
|
|
34
35
|
export interface ChannelBuffers {
|
|
35
36
|
pulse1: Uint8Array;
|
|
@@ -50,6 +51,16 @@ export interface ChannelBuffers {
|
|
|
50
51
|
* channel without a buffer of its own.
|
|
51
52
|
*/
|
|
52
53
|
vrc6?: (Float64Array | undefined)[];
|
|
54
|
+
/**
|
|
55
|
+
* The MMC5's channels — pulse 1, pulse 2, PCM — in `mixApu` units; index 3, if given, takes every
|
|
56
|
+
* channel without a buffer of its own.
|
|
57
|
+
*/
|
|
58
|
+
mmc5?: (Float64Array | undefined)[];
|
|
59
|
+
/**
|
|
60
|
+
* The VRC7's six FM channels in `mixApu` units; index 6, if given, takes every channel without a
|
|
61
|
+
* buffer of its own.
|
|
62
|
+
*/
|
|
63
|
+
vrc7?: (Float64Array | undefined)[];
|
|
53
64
|
}
|
|
54
65
|
/** Cartridge memory for the DMC: the bytes of `$8000`–`$FFFF`, or a reader for any address. */
|
|
55
66
|
export type ApuMemory = Uint8Array | ((address: number) => number);
|
|
@@ -87,6 +98,8 @@ export interface ApuSnapshot {
|
|
|
87
98
|
fds?: FdsSnapshot;
|
|
88
99
|
n163?: N163Snapshot;
|
|
89
100
|
vrc6?: Vrc6Snapshot;
|
|
101
|
+
mmc5?: Mmc5Snapshot;
|
|
102
|
+
vrc7?: Vrc7Snapshot;
|
|
90
103
|
dmc: {
|
|
91
104
|
/** Output level, 0..127. */
|
|
92
105
|
level: number;
|
|
@@ -111,7 +124,8 @@ export interface ApuChipOptions {
|
|
|
111
124
|
/**
|
|
112
125
|
* Expansion sound on the cartridge: `'FDS'` takes `$4040`–`$408A`, `'N163'` its address port
|
|
113
126
|
* (`$F800`), data port (`$4800`) and `$E000`, `'VRC6'` `$9000`–`$9003`, `$A000`–`$A002` and
|
|
114
|
-
* `$B000`–`$B002
|
|
127
|
+
* `$B000`–`$B002`, `'MMC5'` `$5000`–`$5015`, `'VRC7'` `$9010`/`$9030` (and `$E000` bit 6). Their
|
|
128
|
+
* output goes to `ChannelBuffers.fds`/`n163`/`vrc6`/`mmc5`/`vrc7`.
|
|
115
129
|
*/
|
|
116
130
|
expansion?: readonly string[];
|
|
117
131
|
}
|
|
@@ -123,6 +137,10 @@ export declare class Apu {
|
|
|
123
137
|
readonly n163: N163Chip | null;
|
|
124
138
|
/** Konami's VRC6, when `expansion` has `'VRC6'`. */
|
|
125
139
|
readonly vrc6: Vrc6Chip | null;
|
|
140
|
+
/** Nintendo's MMC5, when `expansion` has `'MMC5'`. */
|
|
141
|
+
readonly mmc5: Mmc5Chip | null;
|
|
142
|
+
/** Konami's VRC7, when `expansion` has `'VRC7'`. */
|
|
143
|
+
readonly vrc7: Vrc7Chip | null;
|
|
126
144
|
private readonly lengthCounter;
|
|
127
145
|
private readonly pulses;
|
|
128
146
|
private readonly triangle;
|
package/dist/chip.js
CHANGED
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
import { CPU_HZ, DMC_PERIODS, DUTY_SEQUENCES, LENGTH_TABLE, NOISE_PERIODS, QUARTER_FRAME_HZ, TRIANGLE_STEPS, } from './constants.js';
|
|
33
33
|
import { LONG_CYCLE, LONG_HIGH_BEFORE, LONG_INDEX, LONG_STATES } from './lfsr.js';
|
|
34
34
|
import { decodeSweep, sweepMutes, sweepTarget } from './sweep.js';
|
|
35
|
-
import { FdsChip, N163Chip, Vrc6Chip, } from './expansion.js';
|
|
35
|
+
import { FdsChip, N163Chip, Vrc6Chip, Mmc5Chip, } from './expansion.js';
|
|
36
|
+
import { Vrc7Chip } from './vrc7.js';
|
|
36
37
|
/** The four duty sequences flattened, `duty * 8 + step`. */
|
|
37
38
|
const DUTY_FLAT = Uint8Array.from(DUTY_SEQUENCES.flat());
|
|
38
39
|
const TRIANGLE_FLAT = Uint8Array.from(TRIANGLE_STEPS);
|
|
@@ -137,6 +138,8 @@ export class Apu {
|
|
|
137
138
|
this.fds = options.expansion?.includes('FDS') ? new FdsChip(sampleRate) : null;
|
|
138
139
|
this.n163 = options.expansion?.includes('N163') ? new N163Chip(sampleRate) : null;
|
|
139
140
|
this.vrc6 = options.expansion?.includes('VRC6') ? new Vrc6Chip(sampleRate) : null;
|
|
141
|
+
this.mmc5 = options.expansion?.includes('MMC5') ? new Mmc5Chip(sampleRate) : null;
|
|
142
|
+
this.vrc7 = options.expansion?.includes('VRC7') ? new Vrc7Chip(sampleRate) : null;
|
|
140
143
|
this.samplesPerQuarterFrame = sampleRate / QUARTER_FRAME_HZ;
|
|
141
144
|
this.frameCountdown = this.samplesPerQuarterFrame;
|
|
142
145
|
for (const p of this.pulses)
|
|
@@ -162,6 +165,10 @@ export class Apu {
|
|
|
162
165
|
return;
|
|
163
166
|
if (this.vrc6?.write(address, value))
|
|
164
167
|
return;
|
|
168
|
+
if (this.mmc5?.write(address, value))
|
|
169
|
+
return;
|
|
170
|
+
if (this.vrc7?.write(address, value))
|
|
171
|
+
return;
|
|
165
172
|
this.n163?.write(address, value);
|
|
166
173
|
return;
|
|
167
174
|
}
|
|
@@ -350,6 +357,24 @@ export class Apu {
|
|
|
350
357
|
}
|
|
351
358
|
this.vrc6.render(count, buffers.vrc6, offset);
|
|
352
359
|
}
|
|
360
|
+
if (this.mmc5 && buffers.mmc5) {
|
|
361
|
+
const cleared = new Set();
|
|
362
|
+
for (const out of buffers.mmc5)
|
|
363
|
+
if (out && !cleared.has(out)) {
|
|
364
|
+
out.fill(0, offset, offset + count);
|
|
365
|
+
cleared.add(out);
|
|
366
|
+
}
|
|
367
|
+
this.mmc5.render(count, buffers.mmc5, offset);
|
|
368
|
+
}
|
|
369
|
+
if (this.vrc7 && buffers.vrc7) {
|
|
370
|
+
const cleared = new Set();
|
|
371
|
+
for (const out of buffers.vrc7)
|
|
372
|
+
if (out && !cleared.has(out)) {
|
|
373
|
+
out.fill(0, offset, offset + count);
|
|
374
|
+
cleared.add(out);
|
|
375
|
+
}
|
|
376
|
+
this.vrc7.render(count, buffers.vrc7, offset);
|
|
377
|
+
}
|
|
353
378
|
let done = 0;
|
|
354
379
|
while (done < count) {
|
|
355
380
|
// Samples before the one on which the counter reaches zero. Subtracting
|
|
@@ -596,6 +621,8 @@ export class Apu {
|
|
|
596
621
|
...(this.fds ? { fds: this.fds.snapshot() } : {}),
|
|
597
622
|
...(this.n163 ? { n163: this.n163.snapshot() } : {}),
|
|
598
623
|
...(this.vrc6 ? { vrc6: this.vrc6.snapshot() } : {}),
|
|
624
|
+
...(this.mmc5 ? { mmc5: this.mmc5.snapshot() } : {}),
|
|
625
|
+
...(this.vrc7 ? { vrc7: this.vrc7.snapshot() } : {}),
|
|
599
626
|
dmc: {
|
|
600
627
|
level: this.dmc.level,
|
|
601
628
|
rateIndex: this.dmc.rateIndex,
|
package/dist/chip.js.map
CHANGED
|
@@ -1 +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;AACpF,OAAO,EACL,OAAO,EACP,QAAQ,EACR,QAAQ,GAIT,MAAM,gBAAgB,CAAC;AAExB,4DAA4D;AAC5D,MAAM,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC;AACzD,MAAM,aAAa,GAAG,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;AAyLtD,MAAM,WAAW,GAAG,GAAa,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;AAE7E,MAAM,QAAQ,GAAG,CAAC,MAAe,EAAS,EAAE,CAAC,CAAC;IAC5C,MAAM;IACN,IAAI,EAAE,CAAC;IACP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C7E,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';\nimport {\n FdsChip,\n N163Chip,\n Vrc6Chip,\n type FdsSnapshot,\n type N163Snapshot,\n type Vrc6Snapshot,\n} from './expansion.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 /** The FDS wave channel, in `mixApu` units (added to, not overwritten) — when the chip has one. */\n fds?: Float64Array;\n /**\n * The N163's channels by register slot 0..7 (slot 7, `$78`, is the one always on), in `mixApu`\n * units; index 8, if given, takes every slot without a buffer of its own.\n */\n n163?: (Float64Array | undefined)[];\n /**\n * The VRC6's channels — pulse 1, pulse 2, saw — in `mixApu` units; index 3, if given, takes every\n * channel without a buffer of its own.\n */\n vrc6?: (Float64Array | undefined)[];\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 /** The expansion chips' state, when the chip has them (`ApuChipOptions.expansion`). */\n fds?: FdsSnapshot;\n n163?: N163Snapshot;\n vrc6?: Vrc6Snapshot;\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 * Expansion sound on the cartridge: `'FDS'` takes `$4040`–`$408A`, `'N163'` its address port\n * (`$F800`), data port (`$4800`) and `$E000`, `'VRC6'` `$9000`–`$9003`, `$A000`–`$A002` and\n * `$B000`–`$B002`. Their output goes to `ChannelBuffers.fds`/`n163`/`vrc6`.\n */\n expansion?: readonly string[];\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 /** The Disk System's wave channel, when `expansion` has `'FDS'`. */\n readonly fds: FdsChip | null;\n /** Namco's 163, when `expansion` has `'N163'`. */\n readonly n163: N163Chip | null;\n /** Konami's VRC6, when `expansion` has `'VRC6'`. */\n readonly vrc6: Vrc6Chip | null;\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.fds = options.expansion?.includes('FDS') ? new FdsChip(sampleRate) : null;\n this.n163 = options.expansion?.includes('N163') ? new N163Chip(sampleRate) : null;\n this.vrc6 = options.expansion?.includes('VRC6') ? new Vrc6Chip(sampleRate) : null;\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 /**\n * A CPU write to one of the APU's registers — or an expansion chip's, when there is one.\n * Anything else is ignored.\n */\n write(address: number, value: number) {\n if (address > 0x4017) {\n if (this.fds?.write(address, value)) return;\n if (this.vrc6?.write(address, value)) return;\n this.n163?.write(address, value);\n return;\n }\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 if (this.fds && buffers.fds) {\n buffers.fds.fill(0, offset, offset + count);\n this.fds.render(count, buffers.fds, offset);\n }\n if (this.n163 && buffers.n163) {\n const cleared = new Set<Float64Array>();\n for (const out of buffers.n163) if (out && !cleared.has(out)) {\n out.fill(0, offset, offset + count);\n cleared.add(out);\n }\n this.n163.render(count, buffers.n163, offset);\n }\n if (this.vrc6 && buffers.vrc6) {\n const cleared = new Set<Float64Array>();\n for (const out of buffers.vrc6) if (out && !cleared.has(out)) {\n out.fill(0, offset, offset + count);\n cleared.add(out);\n }\n this.vrc6.render(count, buffers.vrc6, offset);\n }\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 // A few shifts per sample at most, so one conditional subtraction\n // wraps the index — no division. (Math.floor, not `| 0`: the phase is\n // a double, and a round trip through int32 measured 20% slower.)\n const shifts = Math.floor(phase);\n phase -= shifts;\n index += shifts;\n if (index >= LONG_CYCLE) index -= 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 += shifts;\n if (index >= LONG_CYCLE) index -= 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 ...(this.fds ? { fds: this.fds.snapshot() } : {}),\n ...(this.n163 ? { n163: this.n163.snapshot() } : {}),\n ...(this.vrc6 ? { vrc6: this.vrc6.snapshot() } : {}),\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"]}
|
|
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;AACp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vD,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';\nimport {\n FdsChip,\n N163Chip,\n Vrc6Chip,\n Mmc5Chip,\n type FdsSnapshot,\n type Mmc5Snapshot,\n type N163Snapshot,\n type Vrc6Snapshot,\n} from './expansion.js';\nimport { Vrc7Chip, type Vrc7Snapshot } from './vrc7.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 /** The FDS wave channel, in `mixApu` units (added to, not overwritten) — when the chip has one. */\n fds?: Float64Array;\n /**\n * The N163's channels by register slot 0..7 (slot 7, `$78`, is the one always on), in `mixApu`\n * units; index 8, if given, takes every slot without a buffer of its own.\n */\n n163?: (Float64Array | undefined)[];\n /**\n * The VRC6's channels — pulse 1, pulse 2, saw — in `mixApu` units; index 3, if given, takes every\n * channel without a buffer of its own.\n */\n vrc6?: (Float64Array | undefined)[];\n /**\n * The MMC5's channels — pulse 1, pulse 2, PCM — in `mixApu` units; index 3, if given, takes every\n * channel without a buffer of its own.\n */\n mmc5?: (Float64Array | undefined)[];\n /**\n * The VRC7's six FM channels in `mixApu` units; index 6, if given, takes every channel without a\n * buffer of its own.\n */\n vrc7?: (Float64Array | undefined)[];\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 /** The expansion chips' state, when the chip has them (`ApuChipOptions.expansion`). */\n fds?: FdsSnapshot;\n n163?: N163Snapshot;\n vrc6?: Vrc6Snapshot;\n mmc5?: Mmc5Snapshot;\n vrc7?: Vrc7Snapshot;\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 * Expansion sound on the cartridge: `'FDS'` takes `$4040`–`$408A`, `'N163'` its address port\n * (`$F800`), data port (`$4800`) and `$E000`, `'VRC6'` `$9000`–`$9003`, `$A000`–`$A002` and\n * `$B000`–`$B002`, `'MMC5'` `$5000`–`$5015`, `'VRC7'` `$9010`/`$9030` (and `$E000` bit 6). Their\n * output goes to `ChannelBuffers.fds`/`n163`/`vrc6`/`mmc5`/`vrc7`.\n */\n expansion?: readonly string[];\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 /** The Disk System's wave channel, when `expansion` has `'FDS'`. */\n readonly fds: FdsChip | null;\n /** Namco's 163, when `expansion` has `'N163'`. */\n readonly n163: N163Chip | null;\n /** Konami's VRC6, when `expansion` has `'VRC6'`. */\n readonly vrc6: Vrc6Chip | null;\n /** Nintendo's MMC5, when `expansion` has `'MMC5'`. */\n readonly mmc5: Mmc5Chip | null;\n /** Konami's VRC7, when `expansion` has `'VRC7'`. */\n readonly vrc7: Vrc7Chip | null;\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.fds = options.expansion?.includes('FDS') ? new FdsChip(sampleRate) : null;\n this.n163 = options.expansion?.includes('N163') ? new N163Chip(sampleRate) : null;\n this.vrc6 = options.expansion?.includes('VRC6') ? new Vrc6Chip(sampleRate) : null;\n this.mmc5 = options.expansion?.includes('MMC5') ? new Mmc5Chip(sampleRate) : null;\n this.vrc7 = options.expansion?.includes('VRC7') ? new Vrc7Chip(sampleRate) : null;\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 /**\n * A CPU write to one of the APU's registers — or an expansion chip's, when there is one.\n * Anything else is ignored.\n */\n write(address: number, value: number) {\n if (address > 0x4017) {\n if (this.fds?.write(address, value)) return;\n if (this.vrc6?.write(address, value)) return;\n if (this.mmc5?.write(address, value)) return;\n if (this.vrc7?.write(address, value)) return;\n this.n163?.write(address, value);\n return;\n }\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 if (this.fds && buffers.fds) {\n buffers.fds.fill(0, offset, offset + count);\n this.fds.render(count, buffers.fds, offset);\n }\n if (this.n163 && buffers.n163) {\n const cleared = new Set<Float64Array>();\n for (const out of buffers.n163) if (out && !cleared.has(out)) {\n out.fill(0, offset, offset + count);\n cleared.add(out);\n }\n this.n163.render(count, buffers.n163, offset);\n }\n if (this.vrc6 && buffers.vrc6) {\n const cleared = new Set<Float64Array>();\n for (const out of buffers.vrc6) if (out && !cleared.has(out)) {\n out.fill(0, offset, offset + count);\n cleared.add(out);\n }\n this.vrc6.render(count, buffers.vrc6, offset);\n }\n if (this.mmc5 && buffers.mmc5) {\n const cleared = new Set<Float64Array>();\n for (const out of buffers.mmc5) if (out && !cleared.has(out)) {\n out.fill(0, offset, offset + count);\n cleared.add(out);\n }\n this.mmc5.render(count, buffers.mmc5, offset);\n }\n if (this.vrc7 && buffers.vrc7) {\n const cleared = new Set<Float64Array>();\n for (const out of buffers.vrc7) if (out && !cleared.has(out)) {\n out.fill(0, offset, offset + count);\n cleared.add(out);\n }\n this.vrc7.render(count, buffers.vrc7, offset);\n }\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 // A few shifts per sample at most, so one conditional subtraction\n // wraps the index — no division. (Math.floor, not `| 0`: the phase is\n // a double, and a round trip through int32 measured 20% slower.)\n const shifts = Math.floor(phase);\n phase -= shifts;\n index += shifts;\n if (index >= LONG_CYCLE) index -= 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 += shifts;\n if (index >= LONG_CYCLE) index -= 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 ...(this.fds ? { fds: this.fds.snapshot() } : {}),\n ...(this.n163 ? { n163: this.n163.snapshot() } : {}),\n ...(this.vrc6 ? { vrc6: this.vrc6.snapshot() } : {}),\n ...(this.mmc5 ? { mmc5: this.mmc5.snapshot() } : {}),\n ...(this.vrc7 ? { vrc7: this.vrc7.snapshot() } : {}),\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"]}
|
package/dist/expansion.d.ts
CHANGED
|
@@ -180,6 +180,63 @@ export declare class Vrc6Chip {
|
|
|
180
180
|
render(count: number, outs: readonly (Float64Array | undefined)[], offset?: number): void;
|
|
181
181
|
snapshot(): Vrc6Snapshot;
|
|
182
182
|
}
|
|
183
|
+
/** One of the MMC5's two pulses, `$5000`–`$5003` or `$5004`–`$5007`. */
|
|
184
|
+
export interface Mmc5PulseSnapshot {
|
|
185
|
+
/** 11-bit period; Hz = CPU_HZ / (16 · (period + 1)), as a 2A03 pulse — see `mmc5PulseHz`. */
|
|
186
|
+
period: number;
|
|
187
|
+
/** The level it puts out now: the constant volume or the envelope's, 0..15. */
|
|
188
|
+
volume: number;
|
|
189
|
+
/** 0..3, the 2A03's duties. */
|
|
190
|
+
duty: number;
|
|
191
|
+
/** `$5000` bit 4: the volume as written, not the envelope's. */
|
|
192
|
+
constant: boolean;
|
|
193
|
+
/** `$5000` bit 5: the length counter halted, the envelope looping. */
|
|
194
|
+
halt: boolean;
|
|
195
|
+
/** The length counter, counting at 240 Hz. */
|
|
196
|
+
length: number;
|
|
197
|
+
/** Enabled in `$5015`. */
|
|
198
|
+
enabled: boolean;
|
|
199
|
+
}
|
|
200
|
+
export interface Mmc5Snapshot {
|
|
201
|
+
pulse: [Mmc5PulseSnapshot, Mmc5PulseSnapshot];
|
|
202
|
+
/** The PCM channel's 8-bit level, `$5011`. */
|
|
203
|
+
pcm: number;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Nintendo's MMC5: two pulses like the 2A03's — the same duties, envelope and length counter, no
|
|
207
|
+
* sweep — and an 8-bit PCM register, beside the 2A03 (Just Breed, Shin 4-nin Uchi Mahjong, Metal
|
|
208
|
+
* Slader Glory). The chip has no frame counter of its own: envelope and length both step at the
|
|
209
|
+
* quarter-frame rate, 240 Hz, whatever `$4017` says. A pulse with no sweep is never muted by a low
|
|
210
|
+
* period, so a period under 8 still sounds, as on the cartridge.
|
|
211
|
+
*
|
|
212
|
+
* Output is the average over each sample of the levels put out, as the VRC6's.
|
|
213
|
+
*/
|
|
214
|
+
export declare class Mmc5Chip {
|
|
215
|
+
private readonly cyclesPerSample;
|
|
216
|
+
private readonly cyclesPerQuarter;
|
|
217
|
+
private quarterTimer;
|
|
218
|
+
private readonly pulses;
|
|
219
|
+
private pcm;
|
|
220
|
+
private pcmRead;
|
|
221
|
+
private static newPulse;
|
|
222
|
+
constructor(sampleRate: number);
|
|
223
|
+
/** A write to `$5000`–`$5015`. Returns false for any other address. */
|
|
224
|
+
write(address: number, value: number): boolean;
|
|
225
|
+
/** `$5015` as the driver reads it: bit 0/1 set while that pulse's length counter runs. */
|
|
226
|
+
status(): number;
|
|
227
|
+
private static level;
|
|
228
|
+
/** The envelope and length counter, one quarter frame on. */
|
|
229
|
+
private quarter;
|
|
230
|
+
/**
|
|
231
|
+
* Advance `count` samples, ADDING each channel's output to `outs[0]` (pulse 1), `outs[1]` (pulse
|
|
232
|
+
* 2) and `outs[2]` (PCM) — or, for a channel without a buffer of its own, to `outs[3]` if given —
|
|
233
|
+
* from `offset`.
|
|
234
|
+
*/
|
|
235
|
+
render(count: number, outs: readonly (Float64Array | undefined)[], offset?: number): void;
|
|
236
|
+
snapshot(): Mmc5Snapshot;
|
|
237
|
+
}
|
|
238
|
+
/** Hz of an MMC5 pulse period: eight duty steps of `2 · (period + 1)` CPU cycles, as a 2A03 pulse. */
|
|
239
|
+
export declare const mmc5PulseHz: (period: number) => number;
|
|
183
240
|
/** Hz of an FDS frequency register value. */
|
|
184
241
|
export declare const fdsHz: (frequency: number) => number;
|
|
185
242
|
/** Hz of an N163 channel, from its frequency, wave length and the number of channels on. */
|