8static const int rateValues[32] = {0, 2048, 1536, 1280, 1024, 768, 640, 512,
9 384, 320, 256, 192, 160, 128, 96, 80,
10 64, 48, 40, 32, 24, 20, 16, 12,
11 10, 8, 6, 5, 4, 3, 2, 1};
13static const int rateOffsets[32] = {0, 0, 1040, 536, 0, 1040, 536, 0, 1040,
14 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
15 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
16 536, 0, 1040, 536, 0};
18static const int gaussValues[512] = {
19 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
20 0x000, 0x000, 0x000, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
21 0x001, 0x001, 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002,
22 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004,
23 0x005, 0x005, 0x005, 0x005, 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007,
24 0x008, 0x008, 0x008, 0x009, 0x009, 0x009, 0x00a, 0x00a, 0x00a, 0x00b, 0x00b,
25 0x00b, 0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00e, 0x00f, 0x00f, 0x00f, 0x010,
26 0x010, 0x011, 0x011, 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016,
27 0x017, 0x017, 0x018, 0x018, 0x019, 0x01a, 0x01b, 0x01b, 0x01c, 0x01d, 0x01d,
28 0x01e, 0x01f, 0x020, 0x020, 0x021, 0x022, 0x023, 0x024, 0x024, 0x025, 0x026,
29 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02c, 0x02d, 0x02e, 0x02f, 0x030, 0x031,
30 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, 0x03a, 0x03b, 0x03c, 0x03d,
31 0x03e, 0x040, 0x041, 0x042, 0x043, 0x045, 0x046, 0x047, 0x049, 0x04a, 0x04c,
32 0x04d, 0x04e, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, 0x05a, 0x05c,
33 0x05e, 0x05f, 0x061, 0x063, 0x064, 0x066, 0x068, 0x06a, 0x06b, 0x06d, 0x06f,
34 0x071, 0x073, 0x075, 0x076, 0x078, 0x07a, 0x07c, 0x07e, 0x080, 0x082, 0x084,
35 0x086, 0x089, 0x08b, 0x08d, 0x08f, 0x091, 0x093, 0x096, 0x098, 0x09a, 0x09c,
36 0x09f, 0x0a1, 0x0a3, 0x0a6, 0x0a8, 0x0ab, 0x0ad, 0x0af, 0x0b2, 0x0b4, 0x0b7,
37 0x0ba, 0x0bc, 0x0bf, 0x0c1, 0x0c4, 0x0c7, 0x0c9, 0x0cc, 0x0cf, 0x0d2, 0x0d4,
38 0x0d7, 0x0da, 0x0dd, 0x0e0, 0x0e3, 0x0e6, 0x0e9, 0x0ec, 0x0ef, 0x0f2, 0x0f5,
39 0x0f8, 0x0fb, 0x0fe, 0x101, 0x104, 0x107, 0x10b, 0x10e, 0x111, 0x114, 0x118,
40 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, 0x130, 0x133, 0x137, 0x13a, 0x13e,
41 0x141, 0x145, 0x148, 0x14c, 0x150, 0x153, 0x157, 0x15b, 0x15f, 0x162, 0x166,
42 0x16a, 0x16e, 0x172, 0x176, 0x17a, 0x17d, 0x181, 0x185, 0x189, 0x18d, 0x191,
43 0x195, 0x19a, 0x19e, 0x1a2, 0x1a6, 0x1aa, 0x1ae, 0x1b2, 0x1b7, 0x1bb, 0x1bf,
44 0x1c3, 0x1c8, 0x1cc, 0x1d0, 0x1d5, 0x1d9, 0x1dd, 0x1e2, 0x1e6, 0x1eb, 0x1ef,
45 0x1f3, 0x1f8, 0x1fc, 0x201, 0x205, 0x20a, 0x20f, 0x213, 0x218, 0x21c, 0x221,
46 0x226, 0x22a, 0x22f, 0x233, 0x238, 0x23d, 0x241, 0x246, 0x24b, 0x250, 0x254,
47 0x259, 0x25e, 0x263, 0x267, 0x26c, 0x271, 0x276, 0x27b, 0x280, 0x284, 0x289,
48 0x28e, 0x293, 0x298, 0x29d, 0x2a2, 0x2a6, 0x2ab, 0x2b0, 0x2b5, 0x2ba, 0x2bf,
49 0x2c4, 0x2c9, 0x2ce, 0x2d3, 0x2d8, 0x2dc, 0x2e1, 0x2e6, 0x2eb, 0x2f0, 0x2f5,
50 0x2fa, 0x2ff, 0x304, 0x309, 0x30e, 0x313, 0x318, 0x31d, 0x322, 0x326, 0x32b,
51 0x330, 0x335, 0x33a, 0x33f, 0x344, 0x349, 0x34e, 0x353, 0x357, 0x35c, 0x361,
52 0x366, 0x36b, 0x370, 0x374, 0x379, 0x37e, 0x383, 0x388, 0x38c, 0x391, 0x396,
53 0x39b, 0x39f, 0x3a4, 0x3a9, 0x3ad, 0x3b2, 0x3b7, 0x3bb, 0x3c0, 0x3c5, 0x3c9,
54 0x3ce, 0x3d2, 0x3d7, 0x3dc, 0x3e0, 0x3e5, 0x3e9, 0x3ed, 0x3f2, 0x3f6, 0x3fb,
55 0x3ff, 0x403, 0x408, 0x40c, 0x410, 0x415, 0x419, 0x41d, 0x421, 0x425, 0x42a,
56 0x42e, 0x432, 0x436, 0x43a, 0x43e, 0x442, 0x446, 0x44a, 0x44e, 0x452, 0x455,
57 0x459, 0x45d, 0x461, 0x465, 0x468, 0x46c, 0x470, 0x473, 0x477, 0x47a, 0x47e,
58 0x481, 0x485, 0x488, 0x48c, 0x48f, 0x492, 0x496, 0x499, 0x49c, 0x49f, 0x4a2,
59 0x4a6, 0x4a9, 0x4ac, 0x4af, 0x4b2, 0x4b5, 0x4b7, 0x4ba, 0x4bd, 0x4c0, 0x4c3,
60 0x4c5, 0x4c8, 0x4cb, 0x4cd, 0x4d0, 0x4d2, 0x4d5, 0x4d7, 0x4d9, 0x4dc, 0x4de,
61 0x4e0, 0x4e3, 0x4e5, 0x4e7, 0x4e9, 0x4eb, 0x4ed, 0x4ef, 0x4f1, 0x4f3, 0x4f5,
62 0x4f6, 0x4f8, 0x4fa, 0x4fb, 0x4fd, 0x4ff, 0x500, 0x502, 0x503, 0x504, 0x506,
63 0x507, 0x508, 0x50a, 0x50b, 0x50c, 0x50d, 0x50e, 0x50f, 0x510, 0x511, 0x511,
64 0x512, 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, 0x517, 0x518,
65 0x518, 0x518, 0x518, 0x518, 0x519, 0x519};
68 memset(
ram, 0,
sizeof(
ram));
70 for (
int i = 0; i < 8; i++) {
73 channel[i].pitchModulation =
false;
74 memset(
channel[i].decodeBuffer, 0,
sizeof(
channel[i].decodeBuffer));
85 channel[i].gainSustainLevel = 0;
137 for (
int i = 0; i < 8; i++) {
154static int clamp16(
int val) {
155 return val < -0x8000 ? -0x8000 : (val > 0x7fff ? 0x7fff : val);
158static int clip16(
int val) {
return (int16_t)(val & 0xffff); }
161 if (rate == 0)
return false;
162 return ((
counter + rateOffsets[rate]) % rateValues[rate]) == 0;
171 int16_t ramSample =
aram_[adr] | (
aram_[(adr + 1) & 0xffff] << 8);
173 ramSample =
aram_[(adr + 2) & 0xffff] | (
aram_[(adr + 3) & 0xffff] << 8);
176 int sumL = 0, sumR = 0;
177 for (
int i = 0; i < 8; i++) {
186 sumL = clamp16(sumL) & ~1;
187 sumR = clamp16(sumR) & ~1;
198 aram_[adr] = echoL & 0xff;
199 aram_[(adr + 1) & 0xffff] = echoL >> 8;
200 aram_[(adr + 2) & 0xffff] = echoR & 0xff;
201 aram_[(adr + 3) & 0xffff] = echoR >> 8;
216 if (ch > 0 &&
channel[ch].pitchModulation) {
217 pitch += ((
channel[ch - 1].sampleOut >> 5) * pitch) >> 10;
222 if (
channel[ch].startDelay == 0) samplePointer += 2;
224 aram_[samplePointer] | (
aram_[(samplePointer + 1) & 0xffff] << 8);
226 if (
channel[ch].startDelay > 0) {
227 if (
channel[ch].startDelay == 5) {
229 channel[ch].decodeOffset = sampleAdr;
233 ram[0x7c] &= ~(1 << ch);
239 channel[ch].pitchCounter = 0x4000;
250 sample = ((sample *
channel[ch].gain) >> 11) & ~1;
268 if (
channel[ch].startDelay == 0) {
272 if (
channel[ch].pitchCounter >= 0x4000) {
274 if (
channel[ch].blockOffset >= 7) {
275 if (
channel[ch].brrHeader & 0x1) {
276 channel[ch].decodeOffset = sampleAdr;
277 ram[0x7c] |= 1 << ch;
287 channel[ch].pitchCounter &= 0x3fff;
288 channel[ch].pitchCounter += pitch;
289 if (
channel[ch].pitchCounter > 0x7fff)
channel[ch].pitchCounter = 0x7fff;
292 ram[(ch << 4) | 9] = sample >> 8;
293 channel[ch].sampleOut = sample;
303 int newGain =
channel[ch].gain;
306 if (
channel[ch].adsrState == 3) {
312 switch (
channel[ch].adsrState) {
314 newGain += rate == 31 ? 1024 : 32;
317 newGain -= ((newGain - 1) >> 8) + 1;
320 newGain -= ((newGain - 1) >> 8) + 1;
325 rate =
channel[ch].adsrRates[3];
326 switch (
channel[ch].gainMode) {
331 newGain -= ((newGain - 1) >> 8) + 1;
337 newGain += (
channel[ch].preclampGain < 0x600) ? 32 : 8;
342 newGain =
channel[ch].gainValue;
347 int sustainLevel =
channel[ch].useGain ?
channel[ch].gainSustainLevel
349 if (
channel[ch].adsrState == 1 && (newGain >> 8) == sustainLevel) {
353 channel[ch].preclampGain = newGain & 0xffff;
355 if (newGain < 0 || newGain > 0x7ff) {
356 newGain = newGain < 0 ? 0 : 0x7ff;
357 if (
channel[ch].adsrState == 0) {
366 int pos = (
channel[ch].pitchCounter >> 12) +
channel[ch].bufferOffset;
367 int offset = (
channel[ch].pitchCounter >> 4) & 0xff;
368 int16_t news =
channel[ch].decodeBuffer[(pos + 3) % 12];
369 int16_t olds =
channel[ch].decodeBuffer[(pos + 2) % 12];
370 int16_t olders =
channel[ch].decodeBuffer[(pos + 1) % 12];
371 int16_t oldests =
channel[ch].decodeBuffer[pos % 12];
372 int out = (gaussValues[0xff - offset] * oldests) >> 11;
373 out += (gaussValues[0x1ff - offset] * olders) >> 11;
374 out += (gaussValues[0x100 + offset] * olds) >> 11;
375 out = clip16(out) + ((gaussValues[offset] * news) >> 11);
376 return clamp16(out) & ~1;
380 int shift =
channel[ch].brrHeader >> 4;
381 int filter = (
channel[ch].brrHeader & 0xc) >> 2;
382 int bOff =
channel[ch].bufferOffset;
383 int old =
channel[ch].decodeBuffer[bOff == 0 ? 11 : bOff - 1] >> 1;
384 int older =
channel[ch].decodeBuffer[bOff == 0 ? 10 : bOff - 2] >> 1;
386 for (
int i = 0; i < 4; i++) {
398 s = (s << shift) >> 1;
404 s += old + (-old >> 4);
407 s += 2 * old + ((3 * -old) >> 5) - older + (older >> 4);
410 s += 2 * old + ((13 * -old) >> 6) - older + ((3 * older) >> 4);
413 channel[ch].decodeBuffer[bOff + i] = clamp16(s) * 2;
415 old =
channel[ch].decodeBuffer[bOff + i] >> 1;
474 channel[ch].pitch = ((
channel[ch].pitch & 0x00ff) | (val << 8)) & 0x3fff;
496 channel[ch].adsrRates[0] = (val & 0xf) * 2 + 1;
497 channel[ch].adsrRates[1] = ((val & 0x70) >> 4) * 2 + 16;
498 channel[ch].useGain = (val & 0x80) == 0;
509 channel[ch].adsrRates[2] = val & 0x1f;
510 channel[ch].sustainLevel = (val & 0xe0) >> 5;
521 channel[ch].directGain = (val & 0x80) == 0;
522 channel[ch].gainMode = (val & 0x60) >> 5;
523 channel[ch].adsrRates[3] = val & 0x1f;
524 channel[ch].gainValue = (val & 0x7f) * 16;
525 channel[ch].gainSustainLevel = (val & 0xe0) >> 5;
545 for (
int i = 0; i < 8; i++) {
546 channel[i].keyOn = val & (1 << i);
551 for (
int i = 0; i < 8; i++) {
552 channel[i].keyOff = val & (1 << i);
572 for (
int i = 0; i < 8; i++) {
573 channel[i].pitchModulation = val & (1 << i);
578 for (
int i = 0; i < 8; i++) {
579 channel[i].useNoise = val & (1 << i);
584 for (
int i = 0; i < 8; i++) {
585 channel[i].echoEnable = val & (1 << i);
620 float wantedSamples = (pal_timing ? 641.0 : 534.0);
621 double adder = wantedSamples / samples_per_frame;
623 for (
int i = 0; i < samples_per_frame; i++) {
624 sample_data[i * 2] =
sample_buffer_[(((int)location) & 0x3ff) * 2];
625 sample_data[i * 2 + 1] =
sample_buffer_[(((int)location) & 0x3ff) * 2 + 1];
uint32_t lastFrameBoundary
void CycleChannel(int ch)
void GetSamples(int16_t *sample_data, int samples_per_frame, bool pal_timing)
int16_t sample_buffer_[0x400 *2]
int16_t sampleBuffer[0x400 *2]
uint8_t Read(uint8_t adr)
std::vector< uint8_t > & aram_
bool CheckCounter(int rate)
void Write(uint8_t adr, uint8_t val)
int16_t GetSample(int ch)
SNES Emulation and debugging tools.
Main namespace for the application.