@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
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The 2A03's sound half, register by register.
|
|
3
|
+
*
|
|
4
|
+
* `Apu` holds what the chip holds — timers, sequencer positions, length and
|
|
5
|
+
* linear counters, envelopes, sweep units, the frame counter — and changes it
|
|
6
|
+
* only the ways the chip does: through a write to `$4000`–`$4017`, or through
|
|
7
|
+
* its own clocks. Nothing here knows what a note is. That is the point: every
|
|
8
|
+
* NES game's music, whatever its driver, reaches the speaker as writes to
|
|
9
|
+
* these registers, so a synth that takes writes plays any of them.
|
|
10
|
+
*
|
|
11
|
+
* It runs at a fixed sample rate rather than per CPU cycle: `render()` advances
|
|
12
|
+
* the chip a run of samples and reports each channel's output level — 0..15 for
|
|
13
|
+
* the pulses, triangle and noise, 0..127 for the DMC — at every one of them. The
|
|
14
|
+
* mixer and the analogue stage are separate (`mixer.ts`), because a caller that
|
|
15
|
+
* wants to hear one channel alone has to get at the levels before they are mixed.
|
|
16
|
+
*
|
|
17
|
+
* The DMC reads its samples from the cartridge, `$8000`–`$FFFF`; give it that
|
|
18
|
+
* memory as `options.memory` (without it, it reads zeros — a sample that only
|
|
19
|
+
* ever steps down).
|
|
20
|
+
*
|
|
21
|
+
* Not modelled: the DMC's and the frame counter's IRQs, the CPU stall of the
|
|
22
|
+
* DMC's memory reads, and the handful of cycle-exact write/clock races. Periods
|
|
23
|
+
* and levels are the hardware's; timing is exact to the sample.
|
|
24
|
+
*
|
|
25
|
+
* **Speed.** `levels()` runs once per sample — 88 200 times a second of music at
|
|
26
|
+
* the default oversampling — so anything that only changes when a register is
|
|
27
|
+
* written (a channel's step per sample, whether the sweep unit mutes it) is
|
|
28
|
+
* worked out at the write and kept, not recomputed here. The expressions are the
|
|
29
|
+
* same ones, evaluated at a different moment, so the output is bit for bit what
|
|
30
|
+
* recomputing them every sample gives.
|
|
31
|
+
*/
|
|
32
|
+
import { CPU_HZ, DMC_PERIODS, DUTY_SEQUENCES, LENGTH_TABLE, NOISE_PERIODS, QUARTER_FRAME_HZ, TRIANGLE_STEPS, } from './constants.js';
|
|
33
|
+
import { LONG_CYCLE, LONG_HIGH_BEFORE, LONG_INDEX, LONG_STATES } from './lfsr.js';
|
|
34
|
+
import { decodeSweep, sweepMutes, sweepTarget } from './sweep.js';
|
|
35
|
+
/** The four duty sequences flattened, `duty * 8 + step`. */
|
|
36
|
+
const DUTY_FLAT = Uint8Array.from(DUTY_SEQUENCES.flat());
|
|
37
|
+
const TRIANGLE_FLAT = Uint8Array.from(TRIANGLE_STEPS);
|
|
38
|
+
const newEnvelope = () => ({ start: false, divider: 0, decay: 0 });
|
|
39
|
+
const newPulse = (pulse1) => ({
|
|
40
|
+
pulse1,
|
|
41
|
+
duty: 0,
|
|
42
|
+
halt: false,
|
|
43
|
+
constant: false,
|
|
44
|
+
volume: 0,
|
|
45
|
+
env: newEnvelope(),
|
|
46
|
+
sweep: decodeSweep(0, pulse1),
|
|
47
|
+
sweepDivider: 0,
|
|
48
|
+
sweepReload: false,
|
|
49
|
+
period: 0,
|
|
50
|
+
length: 0,
|
|
51
|
+
enabled: false,
|
|
52
|
+
phase: 0,
|
|
53
|
+
step: 0,
|
|
54
|
+
muted: true,
|
|
55
|
+
});
|
|
56
|
+
/** One quarter-frame of an envelope, in the hardware's order. */
|
|
57
|
+
function clockEnvelope(env, period, loop) {
|
|
58
|
+
if (env.start) {
|
|
59
|
+
env.start = false;
|
|
60
|
+
env.decay = 15;
|
|
61
|
+
env.divider = period;
|
|
62
|
+
}
|
|
63
|
+
else if (env.divider === 0) {
|
|
64
|
+
env.divider = period;
|
|
65
|
+
if (env.decay > 0)
|
|
66
|
+
env.decay -= 1;
|
|
67
|
+
else if (loop)
|
|
68
|
+
env.decay = 15;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
env.divider -= 1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export class Apu {
|
|
75
|
+
constructor(sampleRate, options = {}) {
|
|
76
|
+
this.pulses = [newPulse(true), newPulse(false)];
|
|
77
|
+
this.triangle = {
|
|
78
|
+
control: false,
|
|
79
|
+
linearReload: 0,
|
|
80
|
+
linearReloadFlag: false,
|
|
81
|
+
linear: 0,
|
|
82
|
+
period: 0,
|
|
83
|
+
length: 0,
|
|
84
|
+
enabled: false,
|
|
85
|
+
// Start on a zero step, so a chip that has not been written to is silent
|
|
86
|
+
// rather than holding the top of the staircase as DC. The triangle's
|
|
87
|
+
// position only moves while it sounds, so this is where it rests until the
|
|
88
|
+
// first note — the part of power-on a listener would otherwise hear as a
|
|
89
|
+
// thump.
|
|
90
|
+
phase: 16,
|
|
91
|
+
step: 0,
|
|
92
|
+
};
|
|
93
|
+
this.noise = {
|
|
94
|
+
halt: false,
|
|
95
|
+
constant: false,
|
|
96
|
+
volume: 0,
|
|
97
|
+
env: newEnvelope(),
|
|
98
|
+
shortMode: false,
|
|
99
|
+
periodIndex: 0,
|
|
100
|
+
length: 0,
|
|
101
|
+
enabled: false,
|
|
102
|
+
lfsr: 1, // the chip powers up with bit 0 set
|
|
103
|
+
lfsrIndex: LONG_INDEX[1],
|
|
104
|
+
phase: 0,
|
|
105
|
+
step: 0,
|
|
106
|
+
};
|
|
107
|
+
this.dmc = {
|
|
108
|
+
loop: false,
|
|
109
|
+
rateIndex: 0,
|
|
110
|
+
level: 0,
|
|
111
|
+
startAddress: 0xc000,
|
|
112
|
+
startLength: 1,
|
|
113
|
+
address: 0xc000,
|
|
114
|
+
remaining: 0,
|
|
115
|
+
buffer: -1,
|
|
116
|
+
shift: 0,
|
|
117
|
+
bits: 8,
|
|
118
|
+
silence: true,
|
|
119
|
+
phase: 0,
|
|
120
|
+
step: 0,
|
|
121
|
+
};
|
|
122
|
+
/** Frame counter: 5-step mode, which step is next, samples until it. */
|
|
123
|
+
this.fiveStep = false;
|
|
124
|
+
this.frameStep = 0;
|
|
125
|
+
this.out = [0, 0, 0, 0, 0];
|
|
126
|
+
/** Scratch buffers for `levels()`, one sample long. */
|
|
127
|
+
this.one = {
|
|
128
|
+
pulse1: new Uint8Array(1),
|
|
129
|
+
pulse2: new Uint8Array(1),
|
|
130
|
+
triangle: new Uint8Array(1),
|
|
131
|
+
noise: new Float64Array(1),
|
|
132
|
+
dmc: new Uint8Array(1),
|
|
133
|
+
};
|
|
134
|
+
this.sampleRate = sampleRate;
|
|
135
|
+
this.lengthCounter = options.lengthCounter !== false;
|
|
136
|
+
this.samplesPerQuarterFrame = sampleRate / QUARTER_FRAME_HZ;
|
|
137
|
+
this.frameCountdown = this.samplesPerQuarterFrame;
|
|
138
|
+
for (const p of this.pulses)
|
|
139
|
+
this.retunePulse(p);
|
|
140
|
+
this.retuneTriangle();
|
|
141
|
+
this.retuneNoise();
|
|
142
|
+
this.retuneDmc();
|
|
143
|
+
const memory = options.memory;
|
|
144
|
+
this.read =
|
|
145
|
+
typeof memory === 'function'
|
|
146
|
+
? (address) => memory(address) & 0xff
|
|
147
|
+
: memory && memory.length > 0
|
|
148
|
+
? (address) => memory[(address - 0x8000) % memory.length]
|
|
149
|
+
: () => 0;
|
|
150
|
+
}
|
|
151
|
+
/** A CPU write to one of the APU's registers. Anything outside `$4000`–`$4017` is ignored. */
|
|
152
|
+
write(address, value) {
|
|
153
|
+
const v = value & 0xff;
|
|
154
|
+
switch (address) {
|
|
155
|
+
case 0x4000:
|
|
156
|
+
case 0x4004: {
|
|
157
|
+
const p = this.pulses[address === 0x4000 ? 0 : 1];
|
|
158
|
+
p.duty = v >> 6;
|
|
159
|
+
p.halt = (v & 0x20) !== 0;
|
|
160
|
+
p.constant = (v & 0x10) !== 0;
|
|
161
|
+
p.volume = v & 0x0f;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
case 0x4001:
|
|
165
|
+
case 0x4005: {
|
|
166
|
+
const p = this.pulses[address === 0x4001 ? 0 : 1];
|
|
167
|
+
p.sweep = decodeSweep(v, p.pulse1);
|
|
168
|
+
p.sweepReload = true;
|
|
169
|
+
p.muted = sweepMutes(p.period, p.sweep);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
case 0x4002:
|
|
173
|
+
case 0x4006: {
|
|
174
|
+
const p = this.pulses[address === 0x4002 ? 0 : 1];
|
|
175
|
+
p.period = (p.period & 0x700) | v;
|
|
176
|
+
this.retunePulse(p);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
case 0x4003:
|
|
180
|
+
case 0x4007: {
|
|
181
|
+
// The high byte does three things at once, and all three are audible:
|
|
182
|
+
// it reloads the length counter, restarts the envelope, and puts the
|
|
183
|
+
// duty sequencer back to its first step — which is why a driver that
|
|
184
|
+
// rewrites it every frame for a vibrato makes the vibrato click.
|
|
185
|
+
const p = this.pulses[address === 0x4003 ? 0 : 1];
|
|
186
|
+
p.period = (p.period & 0xff) | ((v & 7) << 8);
|
|
187
|
+
if (p.enabled)
|
|
188
|
+
p.length = LENGTH_TABLE[v >> 3];
|
|
189
|
+
p.env.start = true;
|
|
190
|
+
p.phase = 0;
|
|
191
|
+
this.retunePulse(p);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
case 0x4008:
|
|
195
|
+
this.triangle.control = (v & 0x80) !== 0;
|
|
196
|
+
this.triangle.linearReload = v & 0x7f;
|
|
197
|
+
return;
|
|
198
|
+
case 0x400a:
|
|
199
|
+
this.triangle.period = (this.triangle.period & 0x700) | v;
|
|
200
|
+
this.retuneTriangle();
|
|
201
|
+
return;
|
|
202
|
+
case 0x400b:
|
|
203
|
+
// No phase reset here: the triangle's sequencer is never restarted, so
|
|
204
|
+
// a new note picks up the staircase wherever the last one left it.
|
|
205
|
+
this.triangle.period = (this.triangle.period & 0xff) | ((v & 7) << 8);
|
|
206
|
+
if (this.triangle.enabled)
|
|
207
|
+
this.triangle.length = LENGTH_TABLE[v >> 3];
|
|
208
|
+
this.triangle.linearReloadFlag = true;
|
|
209
|
+
this.retuneTriangle();
|
|
210
|
+
return;
|
|
211
|
+
case 0x400c:
|
|
212
|
+
this.noise.halt = (v & 0x20) !== 0;
|
|
213
|
+
this.noise.constant = (v & 0x10) !== 0;
|
|
214
|
+
this.noise.volume = v & 0x0f;
|
|
215
|
+
return;
|
|
216
|
+
case 0x400e: {
|
|
217
|
+
const n = this.noise;
|
|
218
|
+
const shortMode = (v & 0x80) !== 0;
|
|
219
|
+
// Hand the register between its two representations: a position in the
|
|
220
|
+
// precomputed cycle for normal mode, the raw state for short mode.
|
|
221
|
+
if (shortMode && !n.shortMode)
|
|
222
|
+
n.lfsr = LONG_STATES[n.lfsrIndex];
|
|
223
|
+
if (!shortMode && n.shortMode)
|
|
224
|
+
n.lfsrIndex = LONG_INDEX[n.lfsr];
|
|
225
|
+
n.shortMode = shortMode;
|
|
226
|
+
n.periodIndex = v & 0x0f;
|
|
227
|
+
this.retuneNoise();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
case 0x400f:
|
|
231
|
+
if (this.noise.enabled)
|
|
232
|
+
this.noise.length = LENGTH_TABLE[v >> 3];
|
|
233
|
+
this.noise.env.start = true;
|
|
234
|
+
return;
|
|
235
|
+
case 0x4010:
|
|
236
|
+
this.dmc.loop = (v & 0x40) !== 0;
|
|
237
|
+
this.dmc.rateIndex = v & 0x0f;
|
|
238
|
+
this.retuneDmc();
|
|
239
|
+
return;
|
|
240
|
+
case 0x4011:
|
|
241
|
+
// Straight to the output — the way games play PCM without the DMC's
|
|
242
|
+
// own memory reader, one write per sample.
|
|
243
|
+
this.dmc.level = v & 0x7f;
|
|
244
|
+
return;
|
|
245
|
+
case 0x4012:
|
|
246
|
+
this.dmc.startAddress = 0xc000 + v * 64;
|
|
247
|
+
return;
|
|
248
|
+
case 0x4013:
|
|
249
|
+
this.dmc.startLength = v * 16 + 1;
|
|
250
|
+
return;
|
|
251
|
+
case 0x4015: {
|
|
252
|
+
// Clearing a channel's bit empties its length counter at once — the
|
|
253
|
+
// one way to silence a channel on the spot rather than at the next clock.
|
|
254
|
+
const channels = [this.pulses[0], this.pulses[1], this.triangle, this.noise];
|
|
255
|
+
channels.forEach((channel, i) => {
|
|
256
|
+
channel.enabled = (v & (1 << i)) !== 0;
|
|
257
|
+
if (!channel.enabled)
|
|
258
|
+
channel.length = 0;
|
|
259
|
+
});
|
|
260
|
+
// Bit 4 is the DMC's: clear stops the sample (what is already in the
|
|
261
|
+
// buffer still plays out); set starts it again if it had finished.
|
|
262
|
+
const d = this.dmc;
|
|
263
|
+
if ((v & 0x10) === 0)
|
|
264
|
+
d.remaining = 0;
|
|
265
|
+
else if (d.remaining === 0) {
|
|
266
|
+
d.address = d.startAddress;
|
|
267
|
+
d.remaining = d.startLength;
|
|
268
|
+
this.fetchDmc();
|
|
269
|
+
}
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
case 0x4017:
|
|
273
|
+
this.fiveStep = (v & 0x80) !== 0;
|
|
274
|
+
this.frameStep = 0;
|
|
275
|
+
this.frameCountdown = this.samplesPerQuarterFrame;
|
|
276
|
+
// Selecting 5-step mode clocks everything once, immediately.
|
|
277
|
+
if (this.fiveStep) {
|
|
278
|
+
this.clockQuarterFrame();
|
|
279
|
+
this.clockHalfFrame();
|
|
280
|
+
}
|
|
281
|
+
return;
|
|
282
|
+
default:
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Advance one sample and return the five channel levels, `[pulse1, pulse2,
|
|
288
|
+
* triangle, noise, dmc]` (see `render`). The array is reused between calls.
|
|
289
|
+
* For more than a sample at a time, `render` is several times faster and
|
|
290
|
+
* gives the same numbers.
|
|
291
|
+
*/
|
|
292
|
+
levels() {
|
|
293
|
+
const one = this.one;
|
|
294
|
+
this.render(1, one);
|
|
295
|
+
const out = this.out;
|
|
296
|
+
out[0] = one.pulse1[0];
|
|
297
|
+
out[1] = one.pulse2[0];
|
|
298
|
+
out[2] = one.triangle[0];
|
|
299
|
+
out[3] = one.noise[0];
|
|
300
|
+
out[4] = one.dmc[0];
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Advance `count` samples, writing each channel's level into its buffer from
|
|
305
|
+
* `offset` on: the pulses, triangle and DMC as whole numbers (0..15, 0..127),
|
|
306
|
+
* the noise fractional (its gate is averaged over the shifts in a sample). No
|
|
307
|
+
* register writes happen inside — a caller with writes due splits the run at
|
|
308
|
+
* them.
|
|
309
|
+
*
|
|
310
|
+
* The frame counter is the only thing that changes a channel's parameters
|
|
311
|
+
* between writes, so the run is cut at its clocks and each piece is filled a
|
|
312
|
+
* channel at a time (`fill`). Every sum is the one `levels()` would do, in the
|
|
313
|
+
* same order — the buffers come out bit for bit the same as `count` calls.
|
|
314
|
+
*/
|
|
315
|
+
render(count, buffers, offset = 0) {
|
|
316
|
+
let done = 0;
|
|
317
|
+
while (done < count) {
|
|
318
|
+
// Samples before the one on which the counter reaches zero. Subtracting
|
|
319
|
+
// them in one go is exact: the counter holds a value whose fractional
|
|
320
|
+
// bits are all representable after taking off a whole number.
|
|
321
|
+
const free = Math.ceil(this.frameCountdown) - 1;
|
|
322
|
+
if (free <= 0) {
|
|
323
|
+
this.frameCountdown -= 1;
|
|
324
|
+
while (this.frameCountdown <= 0) {
|
|
325
|
+
this.frameCountdown += this.samplesPerQuarterFrame;
|
|
326
|
+
this.clockFrameCounter();
|
|
327
|
+
}
|
|
328
|
+
this.fill(1, buffers, offset + done);
|
|
329
|
+
done += 1;
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
const run = Math.min(free, count - done);
|
|
333
|
+
this.frameCountdown -= run;
|
|
334
|
+
this.fill(run, buffers, offset + done);
|
|
335
|
+
done += run;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/** `count` samples with every channel's parameters fixed — tight loops on locals. */
|
|
340
|
+
fill(count, buffers, at) {
|
|
341
|
+
const end = at + count;
|
|
342
|
+
const { pulse1, pulse2, triangle, noise } = buffers;
|
|
343
|
+
for (let i = 0; i < 2; i++) {
|
|
344
|
+
const p = this.pulses[i];
|
|
345
|
+
const out = i === 0 ? pulse1 : pulse2;
|
|
346
|
+
// A pulse steps through 8 entries per cycle and each entry takes
|
|
347
|
+
// 2 × (period + 1) CPU cycles: CPU / (16 × (period + 1)) per cycle.
|
|
348
|
+
const step = p.step;
|
|
349
|
+
let phase = p.phase;
|
|
350
|
+
if (p.length === 0 || p.muted) {
|
|
351
|
+
for (let k = at; k < end; k++) {
|
|
352
|
+
phase += step;
|
|
353
|
+
if (phase >= 8)
|
|
354
|
+
phase %= 8;
|
|
355
|
+
out[k] = 0;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
const level = p.constant ? p.volume : p.env.decay;
|
|
360
|
+
const base = p.duty * 8;
|
|
361
|
+
for (let k = at; k < end; k++) {
|
|
362
|
+
phase += step;
|
|
363
|
+
if (phase >= 8)
|
|
364
|
+
phase %= 8;
|
|
365
|
+
out[k] = DUTY_FLAT[base + ((phase | 0) & 7)] * level;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
p.phase = phase;
|
|
369
|
+
}
|
|
370
|
+
const t = this.triangle;
|
|
371
|
+
// The staircase only moves while both counters are non-zero. When either
|
|
372
|
+
// runs out the sequencer STOPS where it is and the output holds that step:
|
|
373
|
+
// the triangle is never "at zero" between notes, it is wherever it paused.
|
|
374
|
+
if (t.length > 0 && t.linear > 0) {
|
|
375
|
+
const step = t.step;
|
|
376
|
+
let phase = t.phase;
|
|
377
|
+
for (let k = at; k < end; k++) {
|
|
378
|
+
phase += step;
|
|
379
|
+
if (phase >= 32)
|
|
380
|
+
phase %= 32;
|
|
381
|
+
triangle[k] = TRIANGLE_FLAT[(phase | 0) & 31];
|
|
382
|
+
}
|
|
383
|
+
t.phase = phase;
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
triangle.fill(TRIANGLE_FLAT[(t.phase | 0) & 31], at, end);
|
|
387
|
+
}
|
|
388
|
+
// AVERAGE the register's output across the shifts each sample is worth,
|
|
389
|
+
// rather than sampling wherever it lands. At the short periods the LFSR
|
|
390
|
+
// runs well past the sample rate, and keeping only the last shift folds the
|
|
391
|
+
// rest back as alias tones — drums like a broken speaker. The channel is
|
|
392
|
+
// silenced while bit 0 is set: the register gates it.
|
|
393
|
+
const n = this.noise;
|
|
394
|
+
const level = n.length === 0 ? 0 : n.constant ? n.volume : n.env.decay;
|
|
395
|
+
const step = n.step;
|
|
396
|
+
let phase = n.phase;
|
|
397
|
+
if (!n.shortMode) {
|
|
398
|
+
let index = n.lfsrIndex;
|
|
399
|
+
if (level === 0) {
|
|
400
|
+
// Silent, but the register keeps shifting: advance it, output nothing.
|
|
401
|
+
for (let k = at; k < end; k++) {
|
|
402
|
+
phase += step;
|
|
403
|
+
const shifts = Math.floor(phase);
|
|
404
|
+
phase -= shifts;
|
|
405
|
+
if (shifts > 0)
|
|
406
|
+
index = (index + shifts) % LONG_CYCLE;
|
|
407
|
+
}
|
|
408
|
+
noise.fill(0, at, end);
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
for (let k = at; k < end; k++) {
|
|
412
|
+
phase += step;
|
|
413
|
+
const shifts = Math.floor(phase);
|
|
414
|
+
phase -= shifts;
|
|
415
|
+
let gate;
|
|
416
|
+
if (shifts > 0) {
|
|
417
|
+
gate = (LONG_HIGH_BEFORE[index + shifts] - LONG_HIGH_BEFORE[index]) / shifts;
|
|
418
|
+
index = (index + shifts) % LONG_CYCLE;
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
gate = (LONG_STATES[index] & 1) === 0 ? 1 : 0;
|
|
422
|
+
}
|
|
423
|
+
noise[k] = gate * level;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
n.lfsrIndex = index;
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
let lfsr = n.lfsr;
|
|
430
|
+
for (let k = at; k < end; k++) {
|
|
431
|
+
phase += step;
|
|
432
|
+
const shifts = Math.floor(phase);
|
|
433
|
+
phase -= shifts;
|
|
434
|
+
let high = 0;
|
|
435
|
+
for (let s = 0; s < shifts; s++) {
|
|
436
|
+
if ((lfsr & 1) === 0)
|
|
437
|
+
high++;
|
|
438
|
+
const feedback = (lfsr & 1) ^ ((lfsr >> 6) & 1);
|
|
439
|
+
lfsr = (lfsr >> 1) | (feedback << 14);
|
|
440
|
+
}
|
|
441
|
+
const gate = shifts > 0 ? high / shifts : (lfsr & 1) === 0 ? 1 : 0;
|
|
442
|
+
noise[k] = level === 0 ? 0 : gate * level;
|
|
443
|
+
}
|
|
444
|
+
n.lfsr = lfsr;
|
|
445
|
+
}
|
|
446
|
+
n.phase = phase;
|
|
447
|
+
this.fillDmc(buffers.dmc, at, end);
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* The DMC over `[at, end)`. At most a bit or two per sample even at its
|
|
451
|
+
* fastest rate, so it is clocked bit by bit; a channel with nothing to play
|
|
452
|
+
* only has its output-cycle count moved on, which is all that changes.
|
|
453
|
+
*/
|
|
454
|
+
fillDmc(out, at, end) {
|
|
455
|
+
const d = this.dmc;
|
|
456
|
+
if (d.silence && d.remaining === 0 && d.buffer < 0) {
|
|
457
|
+
// Nothing to play: the output holds, and only the timer and the bit count
|
|
458
|
+
// move on. Worked out for the whole run at once — nothing audible depends
|
|
459
|
+
// on the timer's exact fraction until a sample starts, and starting one
|
|
460
|
+
// takes a register write, which ends the run.
|
|
461
|
+
const total = d.phase + d.step * (end - at);
|
|
462
|
+
const clocks = Math.floor(total);
|
|
463
|
+
d.phase = total - clocks;
|
|
464
|
+
// Each clock uses up a bit; an emptied cycle starts another, silent,
|
|
465
|
+
// since there is nothing to load.
|
|
466
|
+
d.bits -= clocks % 8;
|
|
467
|
+
if (d.bits <= 0)
|
|
468
|
+
d.bits += 8;
|
|
469
|
+
out.fill(d.level, at, end);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
for (let k = at; k < end; k++) {
|
|
473
|
+
d.phase += d.step;
|
|
474
|
+
while (d.phase >= 1) {
|
|
475
|
+
d.phase -= 1;
|
|
476
|
+
this.clockDmc();
|
|
477
|
+
}
|
|
478
|
+
out[k] = d.level;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/** One DMC timer clock: step the output by the next bit, and start a new byte after eight. */
|
|
482
|
+
clockDmc() {
|
|
483
|
+
const d = this.dmc;
|
|
484
|
+
if (!d.silence) {
|
|
485
|
+
if (d.shift & 1) {
|
|
486
|
+
if (d.level <= 125)
|
|
487
|
+
d.level += 2;
|
|
488
|
+
}
|
|
489
|
+
else if (d.level >= 2) {
|
|
490
|
+
d.level -= 2;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
d.shift >>= 1;
|
|
494
|
+
d.bits -= 1;
|
|
495
|
+
if (d.bits === 0) {
|
|
496
|
+
d.bits = 8;
|
|
497
|
+
if (d.buffer < 0) {
|
|
498
|
+
d.silence = true;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
d.silence = false;
|
|
502
|
+
d.shift = d.buffer;
|
|
503
|
+
d.buffer = -1;
|
|
504
|
+
this.fetchDmc();
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/** The memory reader: refill the empty buffer from the cartridge, looping or stopping at the end. */
|
|
509
|
+
fetchDmc() {
|
|
510
|
+
const d = this.dmc;
|
|
511
|
+
if (d.buffer >= 0 || d.remaining === 0)
|
|
512
|
+
return;
|
|
513
|
+
d.buffer = this.read(d.address);
|
|
514
|
+
d.address = d.address === 0xffff ? 0x8000 : d.address + 1;
|
|
515
|
+
d.remaining -= 1;
|
|
516
|
+
if (d.remaining === 0 && d.loop) {
|
|
517
|
+
d.address = d.startAddress;
|
|
518
|
+
d.remaining = d.startLength;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
retuneDmc() {
|
|
522
|
+
this.dmc.step = CPU_HZ / DMC_PERIODS[this.dmc.rateIndex] / this.sampleRate;
|
|
523
|
+
}
|
|
524
|
+
/** A copy of the channels' internal counters and levels — see `ApuSnapshot`. */
|
|
525
|
+
snapshot() {
|
|
526
|
+
const t = this.triangle;
|
|
527
|
+
const n = this.noise;
|
|
528
|
+
return {
|
|
529
|
+
pulse: this.pulses.map((p) => ({
|
|
530
|
+
period: p.period,
|
|
531
|
+
duty: p.duty,
|
|
532
|
+
length: p.length,
|
|
533
|
+
level: p.constant ? p.volume : p.env.decay,
|
|
534
|
+
constant: p.constant,
|
|
535
|
+
muted: p.muted,
|
|
536
|
+
enabled: p.enabled,
|
|
537
|
+
})),
|
|
538
|
+
triangle: {
|
|
539
|
+
period: t.period,
|
|
540
|
+
length: t.length,
|
|
541
|
+
linear: t.linear,
|
|
542
|
+
step: TRIANGLE_FLAT[(t.phase | 0) & 31],
|
|
543
|
+
enabled: t.enabled,
|
|
544
|
+
},
|
|
545
|
+
noise: {
|
|
546
|
+
periodIndex: n.periodIndex,
|
|
547
|
+
shortMode: n.shortMode,
|
|
548
|
+
length: n.length,
|
|
549
|
+
level: n.constant ? n.volume : n.env.decay,
|
|
550
|
+
constant: n.constant,
|
|
551
|
+
enabled: n.enabled,
|
|
552
|
+
},
|
|
553
|
+
dmc: {
|
|
554
|
+
level: this.dmc.level,
|
|
555
|
+
rateIndex: this.dmc.rateIndex,
|
|
556
|
+
loop: this.dmc.loop,
|
|
557
|
+
remaining: this.dmc.remaining,
|
|
558
|
+
address: this.dmc.address,
|
|
559
|
+
enabled: this.dmc.remaining > 0,
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
retunePulse(p) {
|
|
564
|
+
p.step = CPU_HZ / (2 * (p.period + 1)) / this.sampleRate;
|
|
565
|
+
p.muted = sweepMutes(p.period, p.sweep);
|
|
566
|
+
}
|
|
567
|
+
retuneTriangle() {
|
|
568
|
+
this.triangle.step = CPU_HZ / (this.triangle.period + 1) / this.sampleRate;
|
|
569
|
+
}
|
|
570
|
+
retuneNoise() {
|
|
571
|
+
this.noise.step = CPU_HZ / NOISE_PERIODS[this.noise.periodIndex] / this.sampleRate;
|
|
572
|
+
}
|
|
573
|
+
clockFrameCounter() {
|
|
574
|
+
// 4-step: quarter frames on every step, half frames on steps 2 and 4.
|
|
575
|
+
// 5-step: quarter frames on 1, 2, 3 and 5, half frames on 2 and 5.
|
|
576
|
+
const step = this.frameStep;
|
|
577
|
+
if (this.fiveStep) {
|
|
578
|
+
this.frameStep = (step + 1) % 5;
|
|
579
|
+
if (step !== 3)
|
|
580
|
+
this.clockQuarterFrame();
|
|
581
|
+
if (step === 1 || step === 4)
|
|
582
|
+
this.clockHalfFrame();
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
this.frameStep = (step + 1) % 4;
|
|
586
|
+
this.clockQuarterFrame();
|
|
587
|
+
if (step === 1 || step === 3)
|
|
588
|
+
this.clockHalfFrame();
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
clockQuarterFrame() {
|
|
592
|
+
for (const p of this.pulses)
|
|
593
|
+
clockEnvelope(p.env, p.volume, p.halt);
|
|
594
|
+
clockEnvelope(this.noise.env, this.noise.volume, this.noise.halt);
|
|
595
|
+
const t = this.triangle;
|
|
596
|
+
if (t.linearReloadFlag)
|
|
597
|
+
t.linear = t.linearReload;
|
|
598
|
+
else if (t.linear > 0)
|
|
599
|
+
t.linear -= 1;
|
|
600
|
+
if (!t.control)
|
|
601
|
+
t.linearReloadFlag = false;
|
|
602
|
+
}
|
|
603
|
+
clockHalfFrame() {
|
|
604
|
+
if (this.lengthCounter) {
|
|
605
|
+
for (const p of this.pulses)
|
|
606
|
+
if (!p.halt && p.length > 0)
|
|
607
|
+
p.length -= 1;
|
|
608
|
+
if (!this.triangle.control && this.triangle.length > 0)
|
|
609
|
+
this.triangle.length -= 1;
|
|
610
|
+
if (!this.noise.halt && this.noise.length > 0)
|
|
611
|
+
this.noise.length -= 1;
|
|
612
|
+
}
|
|
613
|
+
for (const p of this.pulses) {
|
|
614
|
+
// The hardware's order: adjust first, then reload or count down.
|
|
615
|
+
if (p.sweepDivider === 0 && p.sweep.enabled && p.sweep.shift > 0 && !p.muted) {
|
|
616
|
+
p.period = Math.max(0, sweepTarget(p.period, p.sweep));
|
|
617
|
+
this.retunePulse(p);
|
|
618
|
+
}
|
|
619
|
+
if (p.sweepDivider === 0 || p.sweepReload) {
|
|
620
|
+
p.sweepDivider = p.sweep.dividerPeriod;
|
|
621
|
+
p.sweepReload = false;
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
p.sweepDivider -= 1;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
//# sourceMappingURL=chip.js.map
|