12static const int rateValues[32] = {0, 2048, 1536, 1280, 1024, 768, 640, 512,
13 384, 320, 256, 192, 160, 128, 96, 80,
14 64, 48, 40, 32, 24, 20, 16, 12,
15 10, 8, 6, 5, 4, 3, 2, 1};
17static const int rateOffsets[32] = {0, 0, 1040, 536, 0, 1040, 536, 0, 1040,
18 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
19 536, 0, 1040, 536, 0, 1040, 536, 0, 1040,
20 536, 0, 1040, 536, 0};
22static const int gaussValues[512] = {
23 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000, 0x000,
24 0x000, 0x000, 0x000, 0x000, 0x000, 0x001, 0x001, 0x001, 0x001, 0x001, 0x001,
25 0x001, 0x001, 0x001, 0x001, 0x001, 0x002, 0x002, 0x002, 0x002, 0x002, 0x002,
26 0x002, 0x003, 0x003, 0x003, 0x003, 0x003, 0x004, 0x004, 0x004, 0x004, 0x004,
27 0x005, 0x005, 0x005, 0x005, 0x006, 0x006, 0x006, 0x006, 0x007, 0x007, 0x007,
28 0x008, 0x008, 0x008, 0x009, 0x009, 0x009, 0x00a, 0x00a, 0x00a, 0x00b, 0x00b,
29 0x00b, 0x00c, 0x00c, 0x00d, 0x00d, 0x00e, 0x00e, 0x00f, 0x00f, 0x00f, 0x010,
30 0x010, 0x011, 0x011, 0x012, 0x013, 0x013, 0x014, 0x014, 0x015, 0x015, 0x016,
31 0x017, 0x017, 0x018, 0x018, 0x019, 0x01a, 0x01b, 0x01b, 0x01c, 0x01d, 0x01d,
32 0x01e, 0x01f, 0x020, 0x020, 0x021, 0x022, 0x023, 0x024, 0x024, 0x025, 0x026,
33 0x027, 0x028, 0x029, 0x02a, 0x02b, 0x02c, 0x02d, 0x02e, 0x02f, 0x030, 0x031,
34 0x032, 0x033, 0x034, 0x035, 0x036, 0x037, 0x038, 0x03a, 0x03b, 0x03c, 0x03d,
35 0x03e, 0x040, 0x041, 0x042, 0x043, 0x045, 0x046, 0x047, 0x049, 0x04a, 0x04c,
36 0x04d, 0x04e, 0x050, 0x051, 0x053, 0x054, 0x056, 0x057, 0x059, 0x05a, 0x05c,
37 0x05e, 0x05f, 0x061, 0x063, 0x064, 0x066, 0x068, 0x06a, 0x06b, 0x06d, 0x06f,
38 0x071, 0x073, 0x075, 0x076, 0x078, 0x07a, 0x07c, 0x07e, 0x080, 0x082, 0x084,
39 0x086, 0x089, 0x08b, 0x08d, 0x08f, 0x091, 0x093, 0x096, 0x098, 0x09a, 0x09c,
40 0x09f, 0x0a1, 0x0a3, 0x0a6, 0x0a8, 0x0ab, 0x0ad, 0x0af, 0x0b2, 0x0b4, 0x0b7,
41 0x0ba, 0x0bc, 0x0bf, 0x0c1, 0x0c4, 0x0c7, 0x0c9, 0x0cc, 0x0cf, 0x0d2, 0x0d4,
42 0x0d7, 0x0da, 0x0dd, 0x0e0, 0x0e3, 0x0e6, 0x0e9, 0x0ec, 0x0ef, 0x0f2, 0x0f5,
43 0x0f8, 0x0fb, 0x0fe, 0x101, 0x104, 0x107, 0x10b, 0x10e, 0x111, 0x114, 0x118,
44 0x11b, 0x11e, 0x122, 0x125, 0x129, 0x12c, 0x130, 0x133, 0x137, 0x13a, 0x13e,
45 0x141, 0x145, 0x148, 0x14c, 0x150, 0x153, 0x157, 0x15b, 0x15f, 0x162, 0x166,
46 0x16a, 0x16e, 0x172, 0x176, 0x17a, 0x17d, 0x181, 0x185, 0x189, 0x18d, 0x191,
47 0x195, 0x19a, 0x19e, 0x1a2, 0x1a6, 0x1aa, 0x1ae, 0x1b2, 0x1b7, 0x1bb, 0x1bf,
48 0x1c3, 0x1c8, 0x1cc, 0x1d0, 0x1d5, 0x1d9, 0x1dd, 0x1e2, 0x1e6, 0x1eb, 0x1ef,
49 0x1f3, 0x1f8, 0x1fc, 0x201, 0x205, 0x20a, 0x20f, 0x213, 0x218, 0x21c, 0x221,
50 0x226, 0x22a, 0x22f, 0x233, 0x238, 0x23d, 0x241, 0x246, 0x24b, 0x250, 0x254,
51 0x259, 0x25e, 0x263, 0x267, 0x26c, 0x271, 0x276, 0x27b, 0x280, 0x284, 0x289,
52 0x28e, 0x293, 0x298, 0x29d, 0x2a2, 0x2a6, 0x2ab, 0x2b0, 0x2b5, 0x2ba, 0x2bf,
53 0x2c4, 0x2c9, 0x2ce, 0x2d3, 0x2d8, 0x2dc, 0x2e1, 0x2e6, 0x2eb, 0x2f0, 0x2f5,
54 0x2fa, 0x2ff, 0x304, 0x309, 0x30e, 0x313, 0x318, 0x31d, 0x322, 0x326, 0x32b,
55 0x330, 0x335, 0x33a, 0x33f, 0x344, 0x349, 0x34e, 0x353, 0x357, 0x35c, 0x361,
56 0x366, 0x36b, 0x370, 0x374, 0x379, 0x37e, 0x383, 0x388, 0x38c, 0x391, 0x396,
57 0x39b, 0x39f, 0x3a4, 0x3a9, 0x3ad, 0x3b2, 0x3b7, 0x3bb, 0x3c0, 0x3c5, 0x3c9,
58 0x3ce, 0x3d2, 0x3d7, 0x3dc, 0x3e0, 0x3e5, 0x3e9, 0x3ed, 0x3f2, 0x3f6, 0x3fb,
59 0x3ff, 0x403, 0x408, 0x40c, 0x410, 0x415, 0x419, 0x41d, 0x421, 0x425, 0x42a,
60 0x42e, 0x432, 0x436, 0x43a, 0x43e, 0x442, 0x446, 0x44a, 0x44e, 0x452, 0x455,
61 0x459, 0x45d, 0x461, 0x465, 0x468, 0x46c, 0x470, 0x473, 0x477, 0x47a, 0x47e,
62 0x481, 0x485, 0x488, 0x48c, 0x48f, 0x492, 0x496, 0x499, 0x49c, 0x49f, 0x4a2,
63 0x4a6, 0x4a9, 0x4ac, 0x4af, 0x4b2, 0x4b5, 0x4b7, 0x4ba, 0x4bd, 0x4c0, 0x4c3,
64 0x4c5, 0x4c8, 0x4cb, 0x4cd, 0x4d0, 0x4d2, 0x4d5, 0x4d7, 0x4d9, 0x4dc, 0x4de,
65 0x4e0, 0x4e3, 0x4e5, 0x4e7, 0x4e9, 0x4eb, 0x4ed, 0x4ef, 0x4f1, 0x4f3, 0x4f5,
66 0x4f6, 0x4f8, 0x4fa, 0x4fb, 0x4fd, 0x4ff, 0x500, 0x502, 0x503, 0x504, 0x506,
67 0x507, 0x508, 0x50a, 0x50b, 0x50c, 0x50d, 0x50e, 0x50f, 0x510, 0x511, 0x511,
68 0x512, 0x513, 0x514, 0x514, 0x515, 0x516, 0x516, 0x517, 0x517, 0x517, 0x518,
69 0x518, 0x518, 0x518, 0x518, 0x519, 0x519};
72 memset(
ram, 0,
sizeof(
ram));
74 for (
int i = 0; i < 8; i++) {
78 memset(
channel[i].decodeBuffer, 0,
sizeof(
channel[i].decodeBuffer));
141 for (
int i = 0; i < 8; i++) {
158static int clamp16(
int val) {
159 return val < -0x8000 ? -0x8000 : (val > 0x7fff ? 0x7fff : val);
162static int clip16(
int val) {
return (int16_t)(val & 0xffff); }
165 if (rate == 0)
return false;
166 return ((
counter + rateOffsets[rate]) % rateValues[rate]) == 0;
175 int16_t ramSample =
aram_[adr] | (
aram_[(adr + 1) & 0xffff] << 8);
177 ramSample =
aram_[(adr + 2) & 0xffff] | (
aram_[(adr + 3) & 0xffff] << 8);
180 int sumL = 0, sumR = 0;
181 for (
int i = 0; i < 8; i++) {
190 sumL = clamp16(sumL) & ~1;
191 sumR = clamp16(sumR) & ~1;
202 aram_[adr] = echoL & 0xff;
203 aram_[(adr + 1) & 0xffff] = echoL >> 8;
204 aram_[(adr + 2) & 0xffff] = echoR & 0xff;
205 aram_[(adr + 3) & 0xffff] = echoR >> 8;
220 if (ch > 0 &&
channel[ch].pitchModulation) {
226 if (
channel[ch].startDelay == 0) samplePointer += 2;
228 aram_[samplePointer] | (
aram_[(samplePointer + 1) & 0xffff] << 8);
230 if (
channel[ch].startDelay > 0) {
231 if (
channel[ch].startDelay == 5) {
237 ram[0x7c] &= ~(1 << ch);
272 if (
channel[ch].startDelay == 0) {
276 if (
channel[ch].pitchCounter >= 0x4000) {
278 if (
channel[ch].blockOffset >= 7) {
279 if (
channel[ch].brrHeader & 0x1) {
281 ram[0x7c] |= 1 << ch;
296 ram[(ch << 4) | 9] = sample >> 8;
310 if (
channel[ch].adsrState == 3) {
316 switch (
channel[ch].adsrState) {
318 newGain += rate == 31 ? 1024 : 32;
321 newGain -= ((newGain - 1) >> 8) + 1;
324 newGain -= ((newGain - 1) >> 8) + 1;
330 switch (
channel[ch].gainMode) {
335 newGain -= ((newGain - 1) >> 8) + 1;
353 if (
channel[ch].adsrState == 1 && (newGain >> 8) == sustainLevel) {
359 if (newGain < 0 || newGain > 0x7ff) {
360 newGain = newGain < 0 ? 0 : 0x7ff;
361 if (
channel[ch].adsrState == 0) {
376 int out = (gaussValues[0xff - offset] * oldests) >> 11;
377 out += (gaussValues[0x1ff - offset] * olders) >> 11;
378 out += (gaussValues[0x100 + offset] * olds) >> 11;
379 out = clip16(out) + ((gaussValues[offset] * news) >> 11);
380 return clamp16(out) & ~1;
390 for (
int i = 0; i < 4; i++) {
402 s = (s << shift) >> 1;
408 s += old + (-old >> 4);
411 s += 2 * old + ((3 * -old) >> 5) - older + (older >> 4);
414 s += 2 * old + ((13 * -old) >> 6) - older + ((3 * older) >> 4);
549 for (
int i = 0; i < 8; i++) {
555 for (
int i = 0; i < 8; i++) {
576 for (
int i = 0; i < 8; i++) {
582 for (
int i = 0; i < 8; i++) {
588 for (
int i = 0; i < 8; i++) {
624 float wantedSamples = (pal_timing ? 641.0 : 534.0);
625 double adder = wantedSamples / samples_per_frame;
627 for (
int i = 0; i < samples_per_frame; i++) {
628 sample_data[i * 2] =
sample_buffer_[(((int)location) & 0x3ff) * 2];
629 sample_data[i * 2 + 1] =
sample_buffer_[(((int)location) & 0x3ff) * 2 + 1];
std::vector< uint8_t > & aram_
int16_t sampleBuffer[0x400 *2]
bool CheckCounter(int rate)
int16_t sample_buffer_[0x400 *2]
void CycleChannel(int ch)
void Write(uint8_t adr, uint8_t val)
int16_t GetSample(int ch)
uint8_t Read(uint8_t adr)
void GetSamples(int16_t *sample_data, int samples_per_frame, bool pal_timing)
uint32_t lastFrameBoundary