@xromdev/apu 0.3.0 → 0.4.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 +5 -0
- package/dist/chip.d.ts +24 -1
- package/dist/chip.js +28 -1
- package/dist/chip.js.map +1 -1
- package/dist/expansion.d.ts +123 -0
- package/dist/expansion.js +337 -0
- package/dist/expansion.js.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/notes.d.ts +18 -1
- package/dist/notes.js +148 -0
- package/dist/notes.js.map +1 -1
- package/dist/render.d.ts +7 -0
- package/dist/render.js +31 -1
- package/dist/render.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two expansion sound chips the Famicom's own games carried most: the Disk
|
|
3
|
+
* System's wavetable voice (FDS) and Namco's 163 (N163). Each is a channel the
|
|
4
|
+
* 2A03 does not have — a waveform drawn in RAM, played back at a pitch — and
|
|
5
|
+
* games that use them put their melody there, so without them a soundtrack is
|
|
6
|
+
* its bass and drums.
|
|
7
|
+
*
|
|
8
|
+
* Like the APU here they run at the sample rate, not per CPU cycle: each sample
|
|
9
|
+
* advances the chip by `CPU_HZ / sampleRate` cycles' worth of its counters.
|
|
10
|
+
*
|
|
11
|
+
* Output is in the units `mixApu` returns (0..~1 for the whole 2A03), so a
|
|
12
|
+
* renderer adds it to the mix. The levels relative to the 2A03 follow the
|
|
13
|
+
* common reference players: FDS at full gain about as loud as both pulses at
|
|
14
|
+
* full, an N163 channel at full volume about as loud as one pulse — divided by
|
|
15
|
+
* the number of channels it time-shares its one output with, as the chip does.
|
|
16
|
+
*/
|
|
17
|
+
import { CPU_HZ } from './constants.js';
|
|
18
|
+
/** Full-scale output of the FDS (wave 63 × gain 32 at master volume 1), in `mixApu` units. */
|
|
19
|
+
const FDS_FULL = 0.25;
|
|
20
|
+
/** Full-scale output of one N163 sample step (|sample - 8| = 8 at volume 15), in `mixApu` units. */
|
|
21
|
+
const N163_FULL = 0.16;
|
|
22
|
+
/** The FDS master volume, `$4089` bits 0–1: 1, 2/3, 1/2, 2/5. */
|
|
23
|
+
const FDS_MASTER = [1, 2 / 3, 1 / 2, 2 / 5];
|
|
24
|
+
/** Mod table entries → how far each moves the mod counter; 4 resets it. */
|
|
25
|
+
const MOD_STEP = [0, 1, 2, 4, 0, -4, -2, -1];
|
|
26
|
+
export class FdsChip {
|
|
27
|
+
constructor(sampleRate) {
|
|
28
|
+
this.wave = new Uint8Array(64);
|
|
29
|
+
this.modTable = new Uint8Array(64);
|
|
30
|
+
this.modWritePos = 0;
|
|
31
|
+
this.frequency = 0;
|
|
32
|
+
this.haltWave = true;
|
|
33
|
+
this.envelopesOff = true;
|
|
34
|
+
this.waveWrite = false;
|
|
35
|
+
this.master = 0;
|
|
36
|
+
this.envSpeed = 0xe8;
|
|
37
|
+
// Volume envelope.
|
|
38
|
+
this.volDirect = true;
|
|
39
|
+
this.volIncrease = false;
|
|
40
|
+
this.volSpeed = 0;
|
|
41
|
+
this.volGain = 0;
|
|
42
|
+
this.volCounter = 0;
|
|
43
|
+
// Mod envelope and unit.
|
|
44
|
+
this.modDirect = true;
|
|
45
|
+
this.modIncrease = false;
|
|
46
|
+
this.modSpeed = 0;
|
|
47
|
+
this.modGain = 0;
|
|
48
|
+
this.modCounterEnv = 0;
|
|
49
|
+
this.modFrequency = 0;
|
|
50
|
+
this.modHalt = true;
|
|
51
|
+
this.modCounter = 0;
|
|
52
|
+
this.modPos = 0;
|
|
53
|
+
this.modAcc = 0;
|
|
54
|
+
this.waveAcc = 0;
|
|
55
|
+
this.wavePos = 0;
|
|
56
|
+
/** Gain latched at the start of each wave cycle, as the chip does: a level change waits for the cycle. */
|
|
57
|
+
this.latchedGain = 0;
|
|
58
|
+
this.cyclesPerSample = CPU_HZ / sampleRate;
|
|
59
|
+
}
|
|
60
|
+
/** A write to `$4040`–`$408A`. Returns false for any other address. */
|
|
61
|
+
write(address, value) {
|
|
62
|
+
const v = value & 0xff;
|
|
63
|
+
if (address >= 0x4040 && address <= 0x407f) {
|
|
64
|
+
if (this.waveWrite)
|
|
65
|
+
this.wave[address - 0x4040] = v & 0x3f;
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
switch (address) {
|
|
69
|
+
case 0x4080:
|
|
70
|
+
this.volDirect = (v & 0x80) !== 0;
|
|
71
|
+
this.volIncrease = (v & 0x40) !== 0;
|
|
72
|
+
this.volSpeed = v & 0x3f;
|
|
73
|
+
if (this.volDirect)
|
|
74
|
+
this.volGain = v & 0x3f;
|
|
75
|
+
this.volCounter = 0;
|
|
76
|
+
return true;
|
|
77
|
+
case 0x4082:
|
|
78
|
+
this.frequency = (this.frequency & 0xf00) | v;
|
|
79
|
+
return true;
|
|
80
|
+
case 0x4083:
|
|
81
|
+
this.frequency = (this.frequency & 0x0ff) | ((v & 0x0f) << 8);
|
|
82
|
+
this.haltWave = (v & 0x80) !== 0;
|
|
83
|
+
this.envelopesOff = (v & 0x40) !== 0;
|
|
84
|
+
if (this.haltWave) {
|
|
85
|
+
this.waveAcc = 0;
|
|
86
|
+
this.wavePos = 0;
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
case 0x4084:
|
|
90
|
+
this.modDirect = (v & 0x80) !== 0;
|
|
91
|
+
this.modIncrease = (v & 0x40) !== 0;
|
|
92
|
+
this.modSpeed = v & 0x3f;
|
|
93
|
+
if (this.modDirect)
|
|
94
|
+
this.modGain = v & 0x3f;
|
|
95
|
+
this.modCounterEnv = 0;
|
|
96
|
+
return true;
|
|
97
|
+
case 0x4085:
|
|
98
|
+
this.modCounter = ((v & 0x7f) << 25) >> 25; // 7-bit signed
|
|
99
|
+
return true;
|
|
100
|
+
case 0x4086:
|
|
101
|
+
this.modFrequency = (this.modFrequency & 0xf00) | v;
|
|
102
|
+
return true;
|
|
103
|
+
case 0x4087:
|
|
104
|
+
this.modFrequency = (this.modFrequency & 0x0ff) | ((v & 0x0f) << 8);
|
|
105
|
+
this.modHalt = (v & 0x80) !== 0;
|
|
106
|
+
if (this.modHalt)
|
|
107
|
+
this.modAcc = 0;
|
|
108
|
+
return true;
|
|
109
|
+
case 0x4088:
|
|
110
|
+
// Only while the mod unit is halted; each write fills two steps.
|
|
111
|
+
if (this.modHalt) {
|
|
112
|
+
this.modTable[this.modWritePos] = v & 7;
|
|
113
|
+
this.modTable[(this.modWritePos + 1) & 63] = v & 7;
|
|
114
|
+
this.modWritePos = (this.modWritePos + 2) & 63;
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
case 0x4089:
|
|
118
|
+
this.waveWrite = (v & 0x80) !== 0;
|
|
119
|
+
this.master = v & 3;
|
|
120
|
+
return true;
|
|
121
|
+
case 0x408a:
|
|
122
|
+
this.envSpeed = v;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
return address >= 0x4040 && address <= 0x4097;
|
|
126
|
+
}
|
|
127
|
+
/** The pitch the wave unit runs at this moment, after modulation (the chip's own arithmetic). */
|
|
128
|
+
modulatedFrequency() {
|
|
129
|
+
if (this.modHalt || this.modGain === 0)
|
|
130
|
+
return this.frequency;
|
|
131
|
+
let temp = this.modCounter * this.modGain;
|
|
132
|
+
const rem = temp & 0x0f;
|
|
133
|
+
temp >>= 4;
|
|
134
|
+
if (rem > 0 && (temp & 0x80) === 0)
|
|
135
|
+
temp += this.modCounter < 0 ? -1 : 2;
|
|
136
|
+
if (temp >= 192)
|
|
137
|
+
temp -= 256;
|
|
138
|
+
else if (temp < -64)
|
|
139
|
+
temp += 256;
|
|
140
|
+
temp *= this.frequency;
|
|
141
|
+
const rem2 = temp & 0x3f;
|
|
142
|
+
temp >>= 6;
|
|
143
|
+
if (rem2 >= 32)
|
|
144
|
+
temp += 1;
|
|
145
|
+
return Math.max(0, this.frequency + temp);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Advance `count` samples, ADDING the output to `out` from `offset` (in `mixApu` units, centred
|
|
149
|
+
* by the renderer's filters like the rest).
|
|
150
|
+
*/
|
|
151
|
+
render(count, out, offset = 0) {
|
|
152
|
+
const cps = this.cyclesPerSample;
|
|
153
|
+
const masterScale = (FDS_MASTER[this.master] * FDS_FULL) / (63 * 32);
|
|
154
|
+
for (let k = 0; k < count; k++) {
|
|
155
|
+
// Envelopes: one step every 8 × envSpeed × (speed + 1) cycles.
|
|
156
|
+
if (!this.envelopesOff && this.envSpeed > 0 && !this.haltWave) {
|
|
157
|
+
if (!this.volDirect) {
|
|
158
|
+
this.volCounter += cps;
|
|
159
|
+
const period = 8 * this.envSpeed * (this.volSpeed + 1);
|
|
160
|
+
while (this.volCounter >= period) {
|
|
161
|
+
this.volCounter -= period;
|
|
162
|
+
if (this.volIncrease) {
|
|
163
|
+
if (this.volGain < 32)
|
|
164
|
+
this.volGain++;
|
|
165
|
+
}
|
|
166
|
+
else if (this.volGain > 0)
|
|
167
|
+
this.volGain--;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (!this.modDirect) {
|
|
171
|
+
this.modCounterEnv += cps;
|
|
172
|
+
const period = 8 * this.envSpeed * (this.modSpeed + 1);
|
|
173
|
+
while (this.modCounterEnv >= period) {
|
|
174
|
+
this.modCounterEnv -= period;
|
|
175
|
+
if (this.modIncrease) {
|
|
176
|
+
if (this.modGain < 32)
|
|
177
|
+
this.modGain++;
|
|
178
|
+
}
|
|
179
|
+
else if (this.modGain > 0)
|
|
180
|
+
this.modGain--;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// Mod unit: steps through its table as its accumulator overflows 16 bits.
|
|
185
|
+
if (!this.modHalt && this.modFrequency > 0) {
|
|
186
|
+
this.modAcc += this.modFrequency * cps;
|
|
187
|
+
while (this.modAcc >= 0x10000) {
|
|
188
|
+
this.modAcc -= 0x10000;
|
|
189
|
+
const step = this.modTable[this.modPos];
|
|
190
|
+
this.modPos = (this.modPos + 1) & 63;
|
|
191
|
+
if (step === 4)
|
|
192
|
+
this.modCounter = 0;
|
|
193
|
+
else {
|
|
194
|
+
this.modCounter += MOD_STEP[step];
|
|
195
|
+
if (this.modCounter > 63)
|
|
196
|
+
this.modCounter -= 128;
|
|
197
|
+
else if (this.modCounter < -64)
|
|
198
|
+
this.modCounter += 128;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
// Wave unit: 64 steps a cycle, the accumulator 16 bits a step.
|
|
203
|
+
if (!this.haltWave && !this.waveWrite) {
|
|
204
|
+
this.waveAcc += this.modulatedFrequency() * cps;
|
|
205
|
+
while (this.waveAcc >= 0x10000) {
|
|
206
|
+
this.waveAcc -= 0x10000;
|
|
207
|
+
this.wavePos = (this.wavePos + 1) & 63;
|
|
208
|
+
if (this.wavePos === 0)
|
|
209
|
+
this.latchedGain = Math.min(32, this.volGain);
|
|
210
|
+
}
|
|
211
|
+
if (this.latchedGain === 0 && this.volGain > 0 && this.wavePos === 0)
|
|
212
|
+
this.latchedGain = Math.min(32, this.volGain);
|
|
213
|
+
}
|
|
214
|
+
const level = this.waveWrite ? 0 : this.wave[this.wavePos] * this.latchedGain;
|
|
215
|
+
out[offset + k] += level * masterScale;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
snapshot() {
|
|
219
|
+
return {
|
|
220
|
+
frequency: this.frequency,
|
|
221
|
+
gain: Math.min(32, this.volGain),
|
|
222
|
+
master: this.master,
|
|
223
|
+
wave: Array.from(this.wave),
|
|
224
|
+
halted: this.haltWave || this.waveWrite,
|
|
225
|
+
modGain: this.modHalt ? 0 : this.modGain,
|
|
226
|
+
modFrequency: this.modHalt ? 0 : this.modFrequency,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export class N163Chip {
|
|
231
|
+
constructor(sampleRate) {
|
|
232
|
+
this.ram = new Uint8Array(128);
|
|
233
|
+
this.address = 0;
|
|
234
|
+
this.autoIncrement = false;
|
|
235
|
+
this.disabled = false;
|
|
236
|
+
this.phase = new Float64Array(8);
|
|
237
|
+
this.cyclesPerSample = CPU_HZ / sampleRate;
|
|
238
|
+
}
|
|
239
|
+
/** A write to the address port (`$F800`), data port (`$4800`) or `$E000`. Returns false for others. */
|
|
240
|
+
write(address, value) {
|
|
241
|
+
const v = value & 0xff;
|
|
242
|
+
if (address >= 0xf800 && address <= 0xffff) {
|
|
243
|
+
this.address = v & 0x7f;
|
|
244
|
+
this.autoIncrement = (v & 0x80) !== 0;
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
if (address >= 0x4800 && address <= 0x4fff) {
|
|
248
|
+
this.ram[this.address] = v;
|
|
249
|
+
if (this.autoIncrement)
|
|
250
|
+
this.address = (this.address + 1) & 0x7f;
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
if (address >= 0xe000 && address <= 0xe7ff) {
|
|
254
|
+
this.disabled = (v & 0x40) !== 0;
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
/** A read of the data port, as the CPU makes it — auto-increment included. */
|
|
260
|
+
read() {
|
|
261
|
+
const v = this.ram[this.address];
|
|
262
|
+
if (this.autoIncrement)
|
|
263
|
+
this.address = (this.address + 1) & 0x7f;
|
|
264
|
+
return v;
|
|
265
|
+
}
|
|
266
|
+
get channels() {
|
|
267
|
+
return ((this.ram[0x7f] >> 4) & 7) + 1;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* A channel's eight bytes: frequency low, middle, and high two bits with the wave length above
|
|
271
|
+
* them; three bytes of phase; the wave's address; the volume. (Read off what Namco's own drivers
|
|
272
|
+
* write — Erika to Satoru and Mappy Kids both put `$E0`/`$F0`, a 32- or 16-sample wave, in the
|
|
273
|
+
* third byte.)
|
|
274
|
+
*/
|
|
275
|
+
channel(slot) {
|
|
276
|
+
const base = 0x40 + slot * 8;
|
|
277
|
+
const r = this.ram;
|
|
278
|
+
return {
|
|
279
|
+
frequency: r[base] | (r[base + 1] << 8) | ((r[base + 2] & 3) << 16),
|
|
280
|
+
length: 256 - (r[base + 2] & 0xfc),
|
|
281
|
+
waveAddress: r[base + 6],
|
|
282
|
+
volume: r[base + 7] & 0x0f,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
sample(index) {
|
|
286
|
+
const byte = this.ram[(index >> 1) & 0x7f];
|
|
287
|
+
return index & 1 ? byte >> 4 : byte & 0x0f;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Advance `count` samples, ADDING each active channel's output to `outs[slot]` (or, when a
|
|
291
|
+
* channel has no buffer of its own, to `outs[8]` if given) from `offset`.
|
|
292
|
+
*/
|
|
293
|
+
render(count, outs, offset = 0) {
|
|
294
|
+
if (this.disabled)
|
|
295
|
+
return;
|
|
296
|
+
const n = this.channels;
|
|
297
|
+
// Each channel is updated once every 15·n cycles, and the one DAC is shared n ways.
|
|
298
|
+
const updatesPerSample = this.cyclesPerSample / (15 * n);
|
|
299
|
+
const scale = N163_FULL / (8 * 15 * n);
|
|
300
|
+
for (let slot = 8 - n; slot < 8; slot++) {
|
|
301
|
+
const out = outs[slot] ?? outs[8];
|
|
302
|
+
if (!out)
|
|
303
|
+
continue;
|
|
304
|
+
const c = this.channel(slot);
|
|
305
|
+
if (c.volume === 0 || c.frequency === 0)
|
|
306
|
+
continue;
|
|
307
|
+
const step = c.frequency * updatesPerSample;
|
|
308
|
+
const wrap = c.length * 0x10000;
|
|
309
|
+
let phase = this.phase[slot];
|
|
310
|
+
for (let k = 0; k < count; k++) {
|
|
311
|
+
phase += step;
|
|
312
|
+
if (phase >= wrap)
|
|
313
|
+
phase %= wrap;
|
|
314
|
+
const s = this.sample(((phase / 0x10000) | 0) + c.waveAddress);
|
|
315
|
+
out[offset + k] += (s - 8) * c.volume * scale;
|
|
316
|
+
}
|
|
317
|
+
this.phase[slot] = phase;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
snapshot() {
|
|
321
|
+
const n = this.channels;
|
|
322
|
+
const active = [];
|
|
323
|
+
for (let slot = 8 - n; slot < 8; slot++) {
|
|
324
|
+
const c = this.channel(slot);
|
|
325
|
+
const wave = [];
|
|
326
|
+
for (let i = 0; i < Math.min(c.length, 256); i++)
|
|
327
|
+
wave.push(this.sample(c.waveAddress + i));
|
|
328
|
+
active.push({ slot, ...c, wave });
|
|
329
|
+
}
|
|
330
|
+
return { channels: this.disabled ? 0 : n, active: this.disabled ? [] : active };
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/** Hz of an FDS frequency register value. */
|
|
334
|
+
export const fdsHz = (frequency) => (CPU_HZ * frequency) / 4194304;
|
|
335
|
+
/** Hz of an N163 channel, from its frequency, wave length and the number of channels on. */
|
|
336
|
+
export const n163Hz = (frequency, length, channels) => (CPU_HZ * frequency) / (15 * 65536 * channels * length);
|
|
337
|
+
//# sourceMappingURL=expansion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expansion.js","sourceRoot":"","sources":["../src/expansion.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,8FAA8F;AAC9F,MAAM,QAAQ,GAAG,IAAI,CAAC;AACtB,oGAAoG;AACpG,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,iEAAiE;AACjE,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AAC5C,2EAA2E;AAC3E,MAAM,QAAQ,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAkB7C,MAAM,OAAO,OAAO;IAiClB,YAAY,UAAkB;QA/BrB,SAAI,GAAG,IAAI,U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,MAAM,EAAE,CAAC;IAClF,CAAC;CACF;AAED,6CAA6C;AAC7C,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,SAAiB,EAAE,EAAE,CAAC,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,OAAO,CAAC;AAE3E,4FAA4F;AAC5F,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,SAAiB,EAAE,MAAc,EAAE,QAAgB,EAAE,EAAE,CAC5E,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC,CAAC","sourcesContent":["/**\n * The two expansion sound chips the Famicom's own games carried most: the Disk\n * System's wavetable voice (FDS) and Namco's 163 (N163). Each is a channel the\n * 2A03 does not have — a waveform drawn in RAM, played back at a pitch — and\n * games that use them put their melody there, so without them a soundtrack is\n * its bass and drums.\n *\n * Like the APU here they run at the sample rate, not per CPU cycle: each sample\n * advances the chip by `CPU_HZ / sampleRate` cycles' worth of its counters.\n *\n * Output is in the units `mixApu` returns (0..~1 for the whole 2A03), so a\n * renderer adds it to the mix. The levels relative to the 2A03 follow the\n * common reference players: FDS at full gain about as loud as both pulses at\n * full, an N163 channel at full volume about as loud as one pulse — divided by\n * the number of channels it time-shares its one output with, as the chip does.\n */\n\nimport { CPU_HZ } from './constants.js';\n\n/** Full-scale output of the FDS (wave 63 × gain 32 at master volume 1), in `mixApu` units. */\nconst FDS_FULL = 0.25;\n/** Full-scale output of one N163 sample step (|sample - 8| = 8 at volume 15), in `mixApu` units. */\nconst N163_FULL = 0.16;\n/** The FDS master volume, `$4089` bits 0–1: 1, 2/3, 1/2, 2/5. */\nconst FDS_MASTER = [1, 2 / 3, 1 / 2, 2 / 5];\n/** Mod table entries → how far each moves the mod counter; 4 resets it. */\nconst MOD_STEP = [0, 1, 2, 4, 0, -4, -2, -1];\n\nexport interface FdsSnapshot {\n /** 12-bit wave frequency; Hz = CPU_HZ * frequency / 4194304. */\n frequency: number;\n /** Volume gain as the chip uses it, 0..32. */\n gain: number;\n /** `$4089` bits 0–1. */\n master: number;\n /** The 64 wave samples, 0..63. */\n wave: number[];\n /** Wave unit halted (`$4083` bit 7) or the wave being written (`$4089` bit 7). */\n halted: boolean;\n /** Modulation depth (mod gain, 0..63) and frequency: a vibrato when both are above 0. */\n modGain: number;\n modFrequency: number;\n}\n\nexport class FdsChip {\n private readonly cyclesPerSample: number;\n readonly wave = new Uint8Array(64);\n private readonly modTable = new Uint8Array(64);\n private modWritePos = 0;\n private frequency = 0;\n private haltWave = true;\n private envelopesOff = true;\n private waveWrite = false;\n private master = 0;\n private envSpeed = 0xe8;\n // Volume envelope.\n private volDirect = true;\n private volIncrease = false;\n private volSpeed = 0;\n private volGain = 0;\n private volCounter = 0;\n // Mod envelope and unit.\n private modDirect = true;\n private modIncrease = false;\n private modSpeed = 0;\n private modGain = 0;\n private modCounterEnv = 0;\n private modFrequency = 0;\n private modHalt = true;\n private modCounter = 0;\n private modPos = 0;\n private modAcc = 0;\n private waveAcc = 0;\n private wavePos = 0;\n /** Gain latched at the start of each wave cycle, as the chip does: a level change waits for the cycle. */\n private latchedGain = 0;\n\n constructor(sampleRate: number) {\n this.cyclesPerSample = CPU_HZ / sampleRate;\n }\n\n /** A write to `$4040`–`$408A`. Returns false for any other address. */\n write(address: number, value: number): boolean {\n const v = value & 0xff;\n if (address >= 0x4040 && address <= 0x407f) {\n if (this.waveWrite) this.wave[address - 0x4040] = v & 0x3f;\n return true;\n }\n switch (address) {\n case 0x4080:\n this.volDirect = (v & 0x80) !== 0;\n this.volIncrease = (v & 0x40) !== 0;\n this.volSpeed = v & 0x3f;\n if (this.volDirect) this.volGain = v & 0x3f;\n this.volCounter = 0;\n return true;\n case 0x4082:\n this.frequency = (this.frequency & 0xf00) | v;\n return true;\n case 0x4083:\n this.frequency = (this.frequency & 0x0ff) | ((v & 0x0f) << 8);\n this.haltWave = (v & 0x80) !== 0;\n this.envelopesOff = (v & 0x40) !== 0;\n if (this.haltWave) {\n this.waveAcc = 0;\n this.wavePos = 0;\n }\n return true;\n case 0x4084:\n this.modDirect = (v & 0x80) !== 0;\n this.modIncrease = (v & 0x40) !== 0;\n this.modSpeed = v & 0x3f;\n if (this.modDirect) this.modGain = v & 0x3f;\n this.modCounterEnv = 0;\n return true;\n case 0x4085:\n this.modCounter = ((v & 0x7f) << 25) >> 25; // 7-bit signed\n return true;\n case 0x4086:\n this.modFrequency = (this.modFrequency & 0xf00) | v;\n return true;\n case 0x4087:\n this.modFrequency = (this.modFrequency & 0x0ff) | ((v & 0x0f) << 8);\n this.modHalt = (v & 0x80) !== 0;\n if (this.modHalt) this.modAcc = 0;\n return true;\n case 0x4088:\n // Only while the mod unit is halted; each write fills two steps.\n if (this.modHalt) {\n this.modTable[this.modWritePos] = v & 7;\n this.modTable[(this.modWritePos + 1) & 63] = v & 7;\n this.modWritePos = (this.modWritePos + 2) & 63;\n }\n return true;\n case 0x4089:\n this.waveWrite = (v & 0x80) !== 0;\n this.master = v & 3;\n return true;\n case 0x408a:\n this.envSpeed = v;\n return true;\n }\n return address >= 0x4040 && address <= 0x4097;\n }\n\n /** The pitch the wave unit runs at this moment, after modulation (the chip's own arithmetic). */\n private modulatedFrequency(): number {\n if (this.modHalt || this.modGain === 0) return this.frequency;\n let temp = this.modCounter * this.modGain;\n const rem = temp & 0x0f;\n temp >>= 4;\n if (rem > 0 && (temp & 0x80) === 0) temp += this.modCounter < 0 ? -1 : 2;\n if (temp >= 192) temp -= 256;\n else if (temp < -64) temp += 256;\n temp *= this.frequency;\n const rem2 = temp & 0x3f;\n temp >>= 6;\n if (rem2 >= 32) temp += 1;\n return Math.max(0, this.frequency + temp);\n }\n\n /**\n * Advance `count` samples, ADDING the output to `out` from `offset` (in `mixApu` units, centred\n * by the renderer's filters like the rest).\n */\n render(count: number, out: Float64Array, offset = 0) {\n const cps = this.cyclesPerSample;\n const masterScale = (FDS_MASTER[this.master] * FDS_FULL) / (63 * 32);\n for (let k = 0; k < count; k++) {\n // Envelopes: one step every 8 × envSpeed × (speed + 1) cycles.\n if (!this.envelopesOff && this.envSpeed > 0 && !this.haltWave) {\n if (!this.volDirect) {\n this.volCounter += cps;\n const period = 8 * this.envSpeed * (this.volSpeed + 1);\n while (this.volCounter >= period) {\n this.volCounter -= period;\n if (this.volIncrease) {\n if (this.volGain < 32) this.volGain++;\n } else if (this.volGain > 0) this.volGain--;\n }\n }\n if (!this.modDirect) {\n this.modCounterEnv += cps;\n const period = 8 * this.envSpeed * (this.modSpeed + 1);\n while (this.modCounterEnv >= period) {\n this.modCounterEnv -= period;\n if (this.modIncrease) {\n if (this.modGain < 32) this.modGain++;\n } else if (this.modGain > 0) this.modGain--;\n }\n }\n }\n // Mod unit: steps through its table as its accumulator overflows 16 bits.\n if (!this.modHalt && this.modFrequency > 0) {\n this.modAcc += this.modFrequency * cps;\n while (this.modAcc >= 0x10000) {\n this.modAcc -= 0x10000;\n const step = this.modTable[this.modPos];\n this.modPos = (this.modPos + 1) & 63;\n if (step === 4) this.modCounter = 0;\n else {\n this.modCounter += MOD_STEP[step];\n if (this.modCounter > 63) this.modCounter -= 128;\n else if (this.modCounter < -64) this.modCounter += 128;\n }\n }\n }\n // Wave unit: 64 steps a cycle, the accumulator 16 bits a step.\n if (!this.haltWave && !this.waveWrite) {\n this.waveAcc += this.modulatedFrequency() * cps;\n while (this.waveAcc >= 0x10000) {\n this.waveAcc -= 0x10000;\n this.wavePos = (this.wavePos + 1) & 63;\n if (this.wavePos === 0) this.latchedGain = Math.min(32, this.volGain);\n }\n if (this.latchedGain === 0 && this.volGain > 0 && this.wavePos === 0) this.latchedGain = Math.min(32, this.volGain);\n }\n const level = this.waveWrite ? 0 : this.wave[this.wavePos] * this.latchedGain;\n out[offset + k] += level * masterScale;\n }\n }\n\n snapshot(): FdsSnapshot {\n return {\n frequency: this.frequency,\n gain: Math.min(32, this.volGain),\n master: this.master,\n wave: Array.from(this.wave),\n halted: this.haltWave || this.waveWrite,\n modGain: this.modHalt ? 0 : this.modGain,\n modFrequency: this.modHalt ? 0 : this.modFrequency,\n };\n }\n}\n\nexport interface N163ChannelSnapshot {\n /** 0..7 — the register slot, `$40 + 8·slot` in the chip's RAM; slot 7 ($78) is always on. */\n slot: number;\n /** 18-bit frequency; Hz = CPU_HZ * frequency / (15 · 65536 · channels · length). */\n frequency: number;\n /** Wave length in 4-bit samples. */\n length: number;\n /** Where the wave starts, in 4-bit samples. */\n waveAddress: number;\n volume: number;\n /** The wave as it stands in RAM, `length` samples 0..15. */\n wave: number[];\n}\n\nexport interface N163Snapshot {\n /** How many channels are on, 1..8 — the top ones, from slot 7 down. */\n channels: number;\n active: N163ChannelSnapshot[];\n}\n\nexport class N163Chip {\n private readonly cyclesPerSample: number;\n readonly ram = new Uint8Array(128);\n private address = 0;\n private autoIncrement = false;\n private disabled = false;\n private readonly phase = new Float64Array(8);\n\n constructor(sampleRate: number) {\n this.cyclesPerSample = CPU_HZ / sampleRate;\n }\n\n /** A write to the address port (`$F800`), data port (`$4800`) or `$E000`. Returns false for others. */\n write(address: number, value: number): boolean {\n const v = value & 0xff;\n if (address >= 0xf800 && address <= 0xffff) {\n this.address = v & 0x7f;\n this.autoIncrement = (v & 0x80) !== 0;\n return true;\n }\n if (address >= 0x4800 && address <= 0x4fff) {\n this.ram[this.address] = v;\n if (this.autoIncrement) this.address = (this.address + 1) & 0x7f;\n return true;\n }\n if (address >= 0xe000 && address <= 0xe7ff) {\n this.disabled = (v & 0x40) !== 0;\n return true;\n }\n return false;\n }\n\n /** A read of the data port, as the CPU makes it — auto-increment included. */\n read(): number {\n const v = this.ram[this.address];\n if (this.autoIncrement) this.address = (this.address + 1) & 0x7f;\n return v;\n }\n\n get channels(): number {\n return ((this.ram[0x7f] >> 4) & 7) + 1;\n }\n\n /**\n * A channel's eight bytes: frequency low, middle, and high two bits with the wave length above\n * them; three bytes of phase; the wave's address; the volume. (Read off what Namco's own drivers\n * write — Erika to Satoru and Mappy Kids both put `$E0`/`$F0`, a 32- or 16-sample wave, in the\n * third byte.)\n */\n private channel(slot: number) {\n const base = 0x40 + slot * 8;\n const r = this.ram;\n return {\n frequency: r[base] | (r[base + 1] << 8) | ((r[base + 2] & 3) << 16),\n length: 256 - (r[base + 2] & 0xfc),\n waveAddress: r[base + 6],\n volume: r[base + 7] & 0x0f,\n };\n }\n\n private sample(index: number): number {\n const byte = this.ram[(index >> 1) & 0x7f];\n return index & 1 ? byte >> 4 : byte & 0x0f;\n }\n\n /**\n * Advance `count` samples, ADDING each active channel's output to `outs[slot]` (or, when a\n * channel has no buffer of its own, to `outs[8]` if given) from `offset`.\n */\n render(count: number, outs: readonly (Float64Array | undefined)[], offset = 0) {\n if (this.disabled) return;\n const n = this.channels;\n // Each channel is updated once every 15·n cycles, and the one DAC is shared n ways.\n const updatesPerSample = this.cyclesPerSample / (15 * n);\n const scale = N163_FULL / (8 * 15 * n);\n for (let slot = 8 - n; slot < 8; slot++) {\n const out = outs[slot] ?? outs[8];\n if (!out) continue;\n const c = this.channel(slot);\n if (c.volume === 0 || c.frequency === 0) continue;\n const step = c.frequency * updatesPerSample;\n const wrap = c.length * 0x10000;\n let phase = this.phase[slot];\n for (let k = 0; k < count; k++) {\n phase += step;\n if (phase >= wrap) phase %= wrap;\n const s = this.sample(((phase / 0x10000) | 0) + c.waveAddress);\n out[offset + k] += (s - 8) * c.volume * scale;\n }\n this.phase[slot] = phase;\n }\n }\n\n snapshot(): N163Snapshot {\n const n = this.channels;\n const active: N163ChannelSnapshot[] = [];\n for (let slot = 8 - n; slot < 8; slot++) {\n const c = this.channel(slot);\n const wave: number[] = [];\n for (let i = 0; i < Math.min(c.length, 256); i++) wave.push(this.sample(c.waveAddress + i));\n active.push({ slot, ...c, wave });\n }\n return { channels: this.disabled ? 0 : n, active: this.disabled ? [] : active };\n }\n}\n\n/** Hz of an FDS frequency register value. */\nexport const fdsHz = (frequency: number) => (CPU_HZ * frequency) / 4194304;\n\n/** Hz of an N163 channel, from its frequency, wave length and the number of channels on. */\nexport const n163Hz = (frequency: number, length: number, channels: number) =>\n (CPU_HZ * frequency) / (15 * 65536 * channels * length);\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
export { CPU_HZ, DMC_PERIODS, DUTY_SEQUENCES, FRAME_HZ, LENGTH_TABLE, NOISE_PERIODS, QUARTER_FRAME_HZ, TRIANGLE_STEPS, } from './constants.js';
|
|
15
15
|
export { Apu, type ApuChipOptions, type ApuMemory, type ApuSnapshot, type ChannelBuffers, } from './chip.js';
|
|
16
16
|
export { mixApu, OutputFilters } from './mixer.js';
|
|
17
|
+
export { FdsChip, N163Chip, fdsHz, n163Hz, type FdsSnapshot, type N163ChannelSnapshot, type N163Snapshot, } from './expansion.js';
|
|
17
18
|
export { SWEEP_HZ, decodeSweep, sweepMutes, sweepTarget, sweepTrace, type SweepConfig, } from './sweep.js';
|
|
18
|
-
export { renderWrites, type Click, type RegisterWrite, type Render, type RenderOptions, } from './render.js';
|
|
19
|
-
export { bendAt, levelAt, notesToWrites, renderNotes, sampleMemory, sampleSeconds, type DmcSample, type SampleRow, type ApuInput, type ApuOptions, type ApuRender, type BendCurve, type LevelCurve, type DrumRow, type PitchedRow, secondsToTick, tickToSeconds, type TempoChange, } from './notes.js';
|
|
19
|
+
export { expansionOf, renderWrites, type Click, type RegisterWrite, type Render, type RenderOptions, } from './render.js';
|
|
20
|
+
export { bendAt, EXPANSION_CHANNELS, levelAt, notesToWrites, renderNotes, sampleMemory, sampleSeconds, type DmcSample, type SampleRow, type ApuInput, type ApuOptions, type ApuRender, type BendCurve, type LevelCurve, type DrumRow, type PitchedRow, secondsToTick, tickToSeconds, type TempoChange, } from './notes.js';
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
export { CPU_HZ, DMC_PERIODS, DUTY_SEQUENCES, FRAME_HZ, LENGTH_TABLE, NOISE_PERIODS, QUARTER_FRAME_HZ, TRIANGLE_STEPS, } from './constants.js';
|
|
15
15
|
export { Apu, } from './chip.js';
|
|
16
16
|
export { mixApu, OutputFilters } from './mixer.js';
|
|
17
|
+
export { FdsChip, N163Chip, fdsHz, n163Hz, } from './expansion.js';
|
|
17
18
|
export { SWEEP_HZ, decodeSweep, sweepMutes, sweepTarget, sweepTrace, } from './sweep.js';
|
|
18
|
-
export { renderWrites, } from './render.js';
|
|
19
|
-
export { bendAt, levelAt, notesToWrites, renderNotes, sampleMemory, sampleSeconds, secondsToTick, tickToSeconds, } from './notes.js';
|
|
19
|
+
export { expansionOf, renderWrites, } from './render.js';
|
|
20
|
+
export { bendAt, EXPANSION_CHANNELS, levelAt, notesToWrites, renderNotes, sampleMemory, sampleSeconds, secondsToTick, tickToSeconds, } from './notes.js';
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,GAAG,GAKJ,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EACL,QAAQ,EACR,WAAW,EACX,UAAU,EACV,WAAW,EACX,UAAU,GAEX,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,YAAY,GAKb,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,MAAM,EACN,OAAO,EACP,aAAa,EACb,WAAW,EACX,YAAY,EACZ,aAAa,EAUb,aAAa,EACb,aAAa,GAEd,MAAM,YAAY,CAAC","sourcesContent":["/**\n * @xrom/apu — the NES sound chip (2A03 APU) in TypeScript.\n *\n * Two layers, usable separately:\n *\n * - **The chip.** `renderWrites(writes, seconds)` plays a list of register\n * writes — `{ time, address: 0x4000..0x4017, value }` — and returns samples.\n * Every NES game's music reaches the speaker as writes like these, so this\n * plays any game's music once its writes are known (from an emulator trace,\n * say). `Apu` is the same chip one sample at a time, for live use.\n * - **The note adapter.** `renderNotes(input)` takes music already decoded into\n * notes and writes what a driver would have written to sound them.\n */\n\nexport {\n CPU_HZ,\n DMC_PERIODS,\n DUTY_SEQUENCES,\n FRAME_HZ,\n LENGTH_TABLE,\n NOISE_PERIODS,\n QUARTER_FRAME_HZ,\n TRIANGLE_STEPS,\n} from './constants.js';\nexport {\n Apu,\n type ApuChipOptions,\n type ApuMemory,\n type ApuSnapshot,\n type ChannelBuffers,\n} from './chip.js';\nexport { mixApu, OutputFilters } from './mixer.js';\nexport {\n SWEEP_HZ,\n decodeSweep,\n sweepMutes,\n sweepTarget,\n sweepTrace,\n type SweepConfig,\n} from './sweep.js';\nexport {\n renderWrites,\n type Click,\n type RegisterWrite,\n type Render,\n type RenderOptions,\n} from './render.js';\nexport {\n bendAt,\n levelAt,\n notesToWrites,\n renderNotes,\n sampleMemory,\n sampleSeconds,\n type DmcSample,\n type SampleRow,\n type ApuInput,\n type ApuOptions,\n type ApuRender,\n type BendCurve,\n type LevelCurve,\n type DrumRow,\n type PitchedRow,\n secondsToTick,\n tickToSeconds,\n type TempoChange,\n} from './notes.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EACL,MAAM,EACN,WAAW,EACX,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,GAAG,GAKJ,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EACL,OAAO,EACP,QAAQ,EACR,KAAK,EACL,MAAM,GAIP,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,QAAQ,EACR,WAAW,EACX,UAAU,EACV,WAAW,EACX,UAAU,GAEX,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,YAAY,GAKb,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,MAAM,EACN,kBAAkB,EAClB,OAAO,EACP,aAAa,EACb,WAAW,EACX,YAAY,EACZ,aAAa,EAUb,aAAa,EACb,aAAa,GAEd,MAAM,YAAY,CAAC","sourcesContent":["/**\n * @xrom/apu — the NES sound chip (2A03 APU) in TypeScript.\n *\n * Two layers, usable separately:\n *\n * - **The chip.** `renderWrites(writes, seconds)` plays a list of register\n * writes — `{ time, address: 0x4000..0x4017, value }` — and returns samples.\n * Every NES game's music reaches the speaker as writes like these, so this\n * plays any game's music once its writes are known (from an emulator trace,\n * say). `Apu` is the same chip one sample at a time, for live use.\n * - **The note adapter.** `renderNotes(input)` takes music already decoded into\n * notes and writes what a driver would have written to sound them.\n */\n\nexport {\n CPU_HZ,\n DMC_PERIODS,\n DUTY_SEQUENCES,\n FRAME_HZ,\n LENGTH_TABLE,\n NOISE_PERIODS,\n QUARTER_FRAME_HZ,\n TRIANGLE_STEPS,\n} from './constants.js';\nexport {\n Apu,\n type ApuChipOptions,\n type ApuMemory,\n type ApuSnapshot,\n type ChannelBuffers,\n} from './chip.js';\nexport { mixApu, OutputFilters } from './mixer.js';\nexport {\n FdsChip,\n N163Chip,\n fdsHz,\n n163Hz,\n type FdsSnapshot,\n type N163ChannelSnapshot,\n type N163Snapshot,\n} from './expansion.js';\nexport {\n SWEEP_HZ,\n decodeSweep,\n sweepMutes,\n sweepTarget,\n sweepTrace,\n type SweepConfig,\n} from './sweep.js';\nexport {\n expansionOf,\n renderWrites,\n type Click,\n type RegisterWrite,\n type Render,\n type RenderOptions,\n} from './render.js';\nexport {\n bendAt,\n EXPANSION_CHANNELS,\n levelAt,\n notesToWrites,\n renderNotes,\n sampleMemory,\n sampleSeconds,\n type DmcSample,\n type SampleRow,\n type ApuInput,\n type ApuOptions,\n type ApuRender,\n type BendCurve,\n type LevelCurve,\n type DrumRow,\n type PitchedRow,\n secondsToTick,\n tickToSeconds,\n type TempoChange,\n} from './notes.js';\n"]}
|
package/dist/notes.d.ts
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
*/
|
|
26
26
|
import { type RegisterWrite, type RenderOptions } from './render.js';
|
|
27
27
|
/**
|
|
28
|
-
* `[midi, startTick, durationTicks, channel
|
|
28
|
+
* `[midi, startTick, durationTicks, channel, volume, period, duty,
|
|
29
29
|
* volumeEnd, lengthTicks, bendIndex, envelope, sweep, levelIndex]` — `lengthTicks` counts at
|
|
30
30
|
* 120 Hz and 0 means unlimited; `bendIndex` points into `ApuInput.bends`, or is
|
|
31
31
|
* -1; `envelope` is the low five bits of `$4000` when the chip's own envelope
|
|
@@ -33,6 +33,12 @@ import { type RegisterWrite, type RenderOptions } from './render.js';
|
|
|
33
33
|
* `$4001` byte, or -1/absent when the data has none; `levelIndex`, on a pulse, points into
|
|
34
34
|
* `ApuInput.levels` for a level that does not go in a straight line from `volume` to `volumeEnd`,
|
|
35
35
|
* or is -1/absent.
|
|
36
|
+
*
|
|
37
|
+
* Channels 0..2 are the 2A03's pulses and triangle. 5 is the Disk System's wave channel (FDS) and
|
|
38
|
+
* 6..13 the N163's channels in the order the chip switches them on — see `EXPANSION_CHANNELS`. On
|
|
39
|
+
* those `period` is the frequency in hundredths of a hertz (their registers depend on the wave's
|
|
40
|
+
* length and the chip's channel count, which the adapter chooses itself), `volume` is 0..15, and
|
|
41
|
+
* `waveIndex` (the 14th field) points into `ApuInput.waves`: the waveform the note plays.
|
|
36
42
|
*/
|
|
37
43
|
export type PitchedRow = readonly [
|
|
38
44
|
number,
|
|
@@ -47,8 +53,14 @@ export type PitchedRow = readonly [
|
|
|
47
53
|
number,
|
|
48
54
|
number?,
|
|
49
55
|
number?,
|
|
56
|
+
number?,
|
|
50
57
|
number?
|
|
51
58
|
];
|
|
59
|
+
/** Pitched-row channels past the 2A03's: the FDS, then the N163's eight. */
|
|
60
|
+
export declare const EXPANSION_CHANNELS: {
|
|
61
|
+
readonly fds: 5;
|
|
62
|
+
readonly n163: readonly [6, 7, 8, 9, 10, 11, 12, 13];
|
|
63
|
+
};
|
|
52
64
|
/**
|
|
53
65
|
* A level curve: `[position 0..1, level 0..15]` at each change of level, in order. The level holds
|
|
54
66
|
* from one point to the next — a driver writes whole levels, a frame at a time.
|
|
@@ -92,6 +104,11 @@ export interface ApuInput {
|
|
|
92
104
|
bends?: readonly BendCurve[];
|
|
93
105
|
/** Level curves the pulse and noise notes point at — see `PitchedRow`'s `levelIndex`. Shared like `bends`. */
|
|
94
106
|
levels?: readonly LevelCurve[];
|
|
107
|
+
/**
|
|
108
|
+
* Waveforms the expansion notes play — see `PitchedRow`'s `waveIndex`: 64 samples 0..63 for the
|
|
109
|
+
* FDS, a multiple of 4 samples 0..15 for an N163 channel.
|
|
110
|
+
*/
|
|
111
|
+
waves?: readonly (readonly number[])[];
|
|
95
112
|
/** Seconds per tick. */
|
|
96
113
|
secondsPerTick: number;
|
|
97
114
|
/** Length of the piece in ticks. */
|
package/dist/notes.js
CHANGED
|
@@ -25,6 +25,8 @@
|
|
|
25
25
|
*/
|
|
26
26
|
import { CPU_HZ, DMC_PERIODS, FRAME_HZ, LENGTH_TABLE } from './constants.js';
|
|
27
27
|
import { renderWrites } from './render.js';
|
|
28
|
+
/** Pitched-row channels past the 2A03's: the FDS, then the N163's eight. */
|
|
29
|
+
export const EXPANSION_CHANNELS = { fds: 5, n163: [6, 7, 8, 9, 10, 11, 12, 13] };
|
|
28
30
|
/** The level a curve holds at `position`: the last point at or before it. */
|
|
29
31
|
export function levelAt(curve, position) {
|
|
30
32
|
let level = curve[0]?.[1] ?? 0;
|
|
@@ -357,8 +359,154 @@ export function notesToWrites(input, lead = 0) {
|
|
|
357
359
|
put(hit.end, 0x4015, enables(triangleOn(hit.end)));
|
|
358
360
|
}
|
|
359
361
|
});
|
|
362
|
+
expansionWrites(input, lead, voicesOf, frames, put);
|
|
360
363
|
return writes;
|
|
361
364
|
}
|
|
365
|
+
const hzOf = (row) => row[5] / 100;
|
|
366
|
+
/**
|
|
367
|
+
* The expansion channels' notes as writes: what a driver for the FDS or the N163 would write to
|
|
368
|
+
* sound them — the waveform when it changes, then the frequency and level, and once a frame the
|
|
369
|
+
* level while it ramps and the frequency while it bends.
|
|
370
|
+
*/
|
|
371
|
+
function expansionWrites(input, lead, voicesOf, frames, put) {
|
|
372
|
+
const bends = input.bends ?? [];
|
|
373
|
+
const waves = input.waves ?? [];
|
|
374
|
+
const curveOf = (row) => (row[9] >= 0 ? (bends[row[9]] ?? null) : null);
|
|
375
|
+
const levelsOf = (row) => row[12] !== undefined && row[12] >= 0 ? (input.levels?.[row[12]] ?? null) : null;
|
|
376
|
+
const levelAtOf = (row) => {
|
|
377
|
+
const curve = levelsOf(row);
|
|
378
|
+
return (at) => curve
|
|
379
|
+
? Math.min(15, Math.max(0, levelAt(curve, at)))
|
|
380
|
+
: Math.min(15, Math.max(0, Math.round(row[4] + (row[7] - row[4]) * at)));
|
|
381
|
+
};
|
|
382
|
+
// FDS.
|
|
383
|
+
{
|
|
384
|
+
const voices = voicesOf(input.pitched, ((row) => row[3] === EXPANSION_CHANNELS.fds && row[5] > 0));
|
|
385
|
+
if (voices.length > 0) {
|
|
386
|
+
put(0, 0x4087, 0x80); // modulation off: the bends carry the vibrato
|
|
387
|
+
put(0, 0x408a, 0xe8);
|
|
388
|
+
}
|
|
389
|
+
const freqOf = (hz) => Math.min(0xfff, Math.max(1, Math.round((hz * 4194304) / CPU_HZ)));
|
|
390
|
+
let lastWave = -1;
|
|
391
|
+
voices.forEach((voice, i) => {
|
|
392
|
+
const row = voice.row;
|
|
393
|
+
const waveIndex = row[13] ?? -1;
|
|
394
|
+
if (waveIndex >= 0 && waveIndex !== lastWave && waves[waveIndex]) {
|
|
395
|
+
put(voice.start, 0x4089, 0x80);
|
|
396
|
+
waves[waveIndex].slice(0, 64).forEach((v, k) => put(voice.start, 0x4040 + k, v & 0x3f));
|
|
397
|
+
put(voice.start, 0x4089, 0x00);
|
|
398
|
+
lastWave = waveIndex;
|
|
399
|
+
}
|
|
400
|
+
const level = levelAtOf(row);
|
|
401
|
+
const gain = (l) => 0x80 | Math.round((l * 32) / 15);
|
|
402
|
+
const freq = freqOf(hzOf(row));
|
|
403
|
+
put(voice.start, 0x4080, gain(level(0)));
|
|
404
|
+
put(voice.start, 0x4082, freq & 0xff);
|
|
405
|
+
put(voice.start, 0x4083, 0x40 | (freq >> 8));
|
|
406
|
+
const curve = curveOf(row);
|
|
407
|
+
const ramps = levelsOf(row) !== null || row[4] !== row[7];
|
|
408
|
+
if (curve || ramps) {
|
|
409
|
+
let lastLevel = level(0);
|
|
410
|
+
let lastFreq = freq;
|
|
411
|
+
for (const { time, position } of frames(voice.start, voice.end)) {
|
|
412
|
+
if (ramps) {
|
|
413
|
+
const now = level(position);
|
|
414
|
+
if (now !== lastLevel)
|
|
415
|
+
put(time, 0x4080, gain(now));
|
|
416
|
+
lastLevel = now;
|
|
417
|
+
}
|
|
418
|
+
if (curve) {
|
|
419
|
+
const now = freqOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12));
|
|
420
|
+
if (now !== lastFreq) {
|
|
421
|
+
put(time, 0x4082, now & 0xff);
|
|
422
|
+
if (now >> 8 !== lastFreq >> 8)
|
|
423
|
+
put(time, 0x4083, 0x40 | (now >> 8));
|
|
424
|
+
}
|
|
425
|
+
lastFreq = now;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const next = voices[i + 1];
|
|
430
|
+
if (!next || next.start > voice.end + 1e-9)
|
|
431
|
+
put(voice.end, 0x4080, 0x80);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
// N163: as many channels on as the piece uses, each with its own stretch of the chip's RAM.
|
|
435
|
+
const n163Voices = EXPANSION_CHANNELS.n163.map((channel) => voicesOf(input.pitched, ((row) => row[3] === channel && row[5] > 0)));
|
|
436
|
+
const used = n163Voices.reduce((most, voices, k) => (voices.length > 0 ? k + 1 : most), 0);
|
|
437
|
+
if (used === 0)
|
|
438
|
+
return;
|
|
439
|
+
const count = used;
|
|
440
|
+
// Registers take the top 8·count bytes; the rest is wave RAM, shared out evenly, in whole fours.
|
|
441
|
+
const freeNibbles = (0x80 - 8 * count) * 2;
|
|
442
|
+
const area = Math.max(4, Math.floor(freeNibbles / count / 4) * 4);
|
|
443
|
+
const ramWrite = (time, address, bytes) => {
|
|
444
|
+
put(time, 0xf800, 0x80 | (address & 0x7f));
|
|
445
|
+
for (const b of bytes)
|
|
446
|
+
put(time, 0x4800, b & 0xff);
|
|
447
|
+
};
|
|
448
|
+
// The channel count lives in the top nibble of $7F, beside slot 7's volume.
|
|
449
|
+
const countBits = (count - 1) << 4;
|
|
450
|
+
ramWrite(0, 0x7f, [countBits]);
|
|
451
|
+
n163Voices.forEach((voices, k) => {
|
|
452
|
+
const slot = 7 - k;
|
|
453
|
+
const base = 0x40 + slot * 8;
|
|
454
|
+
const start = k * area; // in nibbles
|
|
455
|
+
let lastWave = -1;
|
|
456
|
+
let length = area;
|
|
457
|
+
const volumeByte = (v) => (slot === 7 ? countBits : 0) | (v & 0x0f);
|
|
458
|
+
voices.forEach((voice, i) => {
|
|
459
|
+
const row = voice.row;
|
|
460
|
+
const waveIndex = row[13] ?? -1;
|
|
461
|
+
const wave = waveIndex >= 0 ? waves[waveIndex] : null;
|
|
462
|
+
if (wave && waveIndex !== lastWave) {
|
|
463
|
+
// A wave longer than the stretch this channel has is resampled to fit.
|
|
464
|
+
length = Math.min(area, Math.max(4, Math.floor(wave.length / 4) * 4));
|
|
465
|
+
const samples = Array.from({ length }, (_, n) => wave[Math.floor((n * wave.length) / length)] & 0x0f);
|
|
466
|
+
const bytes = [];
|
|
467
|
+
for (let n = 0; n < length; n += 2)
|
|
468
|
+
bytes.push(samples[n] | ((samples[n + 1] ?? 0) << 4));
|
|
469
|
+
ramWrite(voice.start, start >> 1, bytes);
|
|
470
|
+
lastWave = waveIndex;
|
|
471
|
+
}
|
|
472
|
+
const freqOf = (hz) => Math.min(0x3ffff, Math.max(1, Math.round((hz * 15 * 65536 * count * length) / CPU_HZ)));
|
|
473
|
+
const regs = (freq, volume) => [
|
|
474
|
+
freq & 0xff,
|
|
475
|
+
(freq >> 8) & 0xff,
|
|
476
|
+
((256 - length) & 0xfc) | ((freq >> 16) & 3),
|
|
477
|
+
0,
|
|
478
|
+
0,
|
|
479
|
+
0,
|
|
480
|
+
start & 0xff,
|
|
481
|
+
volumeByte(volume),
|
|
482
|
+
];
|
|
483
|
+
const level = levelAtOf(row);
|
|
484
|
+
const freq = freqOf(hzOf(row));
|
|
485
|
+
ramWrite(voice.start, base, regs(freq, level(0)));
|
|
486
|
+
const curve = curveOf(row);
|
|
487
|
+
const ramps = levelsOf(row) !== null || row[4] !== row[7];
|
|
488
|
+
if (curve || ramps) {
|
|
489
|
+
let lastLevel = level(0);
|
|
490
|
+
let lastFreq = freq;
|
|
491
|
+
for (const { time, position } of frames(voice.start, voice.end)) {
|
|
492
|
+
const nowLevel = ramps ? level(position) : lastLevel;
|
|
493
|
+
const nowFreq = curve ? freqOf(hzOf(row) * Math.pow(2, bendAt(curve, position) / 12)) : lastFreq;
|
|
494
|
+
if (nowFreq !== lastFreq) {
|
|
495
|
+
// The frequency bytes alone, not the phase after them: that is the chip's running count.
|
|
496
|
+
ramWrite(time, base, [nowFreq & 0xff, (nowFreq >> 8) & 0xff, ((256 - length) & 0xfc) | ((nowFreq >> 16) & 3)]);
|
|
497
|
+
}
|
|
498
|
+
if (nowLevel !== lastLevel)
|
|
499
|
+
ramWrite(time, base + 7, [volumeByte(nowLevel)]);
|
|
500
|
+
lastLevel = nowLevel;
|
|
501
|
+
lastFreq = nowFreq;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const next = voices[i + 1];
|
|
505
|
+
if (!next || next.start > voice.end + 1e-9)
|
|
506
|
+
ramWrite(voice.end, base + 7, [volumeByte(0)]);
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
}
|
|
362
510
|
/** Render a decoded piece: `notesToWrites` into `renderWrites`, with the tick-based extras. */
|
|
363
511
|
export function renderNotes(input, options = {}) {
|
|
364
512
|
const lead = options.lead ?? 0;
|